0

I am trying to reset the program-counter (AKA instruction-pointer) to 0.

I had expected the following C code to work (but it didn't):

typedef void(*func)();
func reset = NULL;
reset();

Here is the dis-assembly when using VS2013 compiler:

mov  dword ptr[reset],0
mov  esi,esp
call dword ptr[reset]

I realize that this issue is not dictated by the C-language standard, but is rather a matter of specific compiler implementation. Nevertheless, I would expect it to work pretty much on every decent compiler.

What could a function-call be compiled into, besides setting the PC/IP to the address of that function?

Thanks

4

1 回答 1

1

It really depends on the hardware you're targeting, but it will probably compile to the same thing as any other function pointer call. It's also possible for the compiler to recognize the constant value given to reset, and optimize it as such. If nothing else, you could always do:

((void (*)())NULL)();

which basically casts NULL to a parameterless function of type void.


Whether or not the call succeeds is an entirely different matter: on most platforms using virtual memory, the kernel purposefully leaves the NULL address + some amount of space unmapped (maybe a few KB, maybe a few MB). Your instruction pointer will probably still go to 0, but as soon as the CPU tries to fetch an instruction from that address, KABOOM.

于 2014-08-13T18:03:44.440 回答