I'm generating machine code for buffer overflow execution and wanted a quick and easy method to plug the bytecode into a program and see how it runs on my host machine.
I looked for ways to dynamically generate functions in C++ and came across this question with this very interesting answer. It has been upvoted quite a bit and there doesn't seem to be anyone challenging what they said.
However, when trying to implement what they wrote in my own program I get the error "Expression cannot be used as a function".
Here's my code:
int main()
{
uint8_t machinecode[] = {0x90, 0x0F, 0x01};
*reinterpret_cast<void**>(&machinecode)();
return 0;
}
As far as code validity for compilation goes, in the hope that I understand you question correctly, you need to cast to a callable, which in this case is void(*)()
, not simply void*
, and you need an extra set of parentheses:
(*reinterpret_cast<void(*)()>(bytecode))();
See here live, but I'm not sure this is anything you actually want to run, even in the context you have provided.