您不要为此使用malloc。你为什么要在 C++ 程序中呢?但是,您也不要将new 用于可执行内存。有特定于 Windows 的 VirtualAlloc 函数来保留内存,然后您可以使用 VirtualProtect 函数将其标记为可执行文件,例如应用 PAGE_EXECUTE_READ 标志。
完成后,您可以将指向已分配内存的指针转换为适当的函数指针类型,然后调用该函数。完成后不要忘记致电VirtualFree。
这里是一些非常基本的示例代码,没有错误处理或其他完整性检查,只是为了向您展示如何在现代 C++ 中实现这一点(程序打印 5):
#include <windows.h>
#include <vector>
#include <iostream>
#include <cstring>
int main()
{
std::vector<unsigned char> 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<std::int32_t(*)()>(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";
}
与普通的 C++ 内存管理相比,这是非常不寻常的,但并不是真正的火箭科学。困难的部分是正确获取实际的机器代码。请注意,我这里的示例只是非常基本的 x64 代码。