Execute Instructions From The Heap

后端 未结 4 1260
失恋的感觉
失恋的感觉 2020-12-11 09:53

Can I allocate a block on the heap, set its bytes to values that correspond to a function call and its parameters, then use the function call and dereference operators to ex

4条回答
  •  死守一世寂寞
    2020-12-11 10:20

    In windows, for example, this is now very hard to do when it was once very easy. I used to be able to take an array of bytes in C and then cast it to a function pointer type to execute it... but not any more.

    Now, you can do this if you can call Global or VirtualAlloc functions and specifically ask for executable memory. On most platforms its either completely open or massively locked down. Doing this sort of thing on iOS, for example, is a massive headache and it will cause a submission fail on the app store if discovered.

    here is some fantastically out of date and crusty code where i did the original thing you described:

    https://code.google.com/p/fridgescript/source/browse/trunk/src/w32/Code/Platform_FSCompiledCode.cpp

    using bytes from https://code.google.com/p/fsassembler

    you may notice in there that i need to provide platform (windows) specific allocation functions to get some executable memory:

    https://code.google.com/p/fridgescript/source/browse/trunk/src/w32/Core/Platform_FSExecutableAlloc.cpp

提交回复
热议问题