How to alloc a executable memory buffer?

后端 未结 4 1706
时光取名叫无心
时光取名叫无心 2020-11-30 07:19

I would like to alloc a buffer that I can execute on Win32 but I have an exception in visual studio cuz the malloc function returns a non executable memory zone. I read that

4条回答
  •  迷失自我
    2020-11-30 07:51

    You don't use malloc for that. Why would you anyway, in a C++ program? You also don't use new for executable memory, however. There's the Windows-specific VirtualAlloc function to reserve memory which you then mark as executable with the VirtualProtect function applying, for instance, the PAGE_EXECUTE_READ flag.

    When you have done that, you can cast the pointer to the allocated memory to an appropriate function pointer type and just call the function. Don't forget to call VirtualFree when you are done.

    Here is some very basic example code with no error handling or other sanity checks, just to show you how this can be accomplished in modern C++ (the program prints 5):

    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        std::vector const code =
        {
            0xb8,                   // move the following value to EAX:
            0x05, 0x00, 0x00, 0x00, // 5
            0xc3                    // return what's currently in EAX
        };    
    
        SYSTEM_INFO system_info;
        GetSystemInfo(&system_info);
        auto const page_size = system_info.dwPageSize;
    
        // prepare the memory in which the machine code will be put (it's not executable yet):
        auto const buffer = VirtualAlloc(nullptr, page_size, MEM_COMMIT, PAGE_READWRITE);
    
        // copy the machine code into that memory:
        std::memcpy(buffer, code.data(), code.size());
    
        // mark the memory as executable:
        DWORD dummy;
        VirtualProtect(buffer, code.size(), PAGE_EXECUTE_READ, &dummy);
    
        // interpret the beginning of the (now) executable memory as the entry
        // point of a function taking no arguments and returning a 4-byte int:
        auto const function_ptr = reinterpret_cast(buffer);
    
        // call the function and store the result in a local std::int32_t object:
        auto const result = function_ptr();
    
        // free the executable memory:
        VirtualFree(buffer, 0, MEM_RELEASE);
    
        // use your std::int32_t:
        std::cout << result << "\n";
    }
    

    It's very unusual compared to normal C++ memory management, but not really rocket science. The hard part is to get the actual machine code right. Note that my example here is just very basic x64 code.

提交回复
热议问题