views:

35

answers:

1

Say i have loaded some random address like 0x00001234 into eax. Is there a way to test that this address is valid/exists before jumping to it or dereferencing it? Or do I have to implement an exception handler?

+2  A: 

Sure, if your operating system lets you take over the page fault mechanism, that's easy to do. Just implement your own fault handler, temporarily store a ret instruction at that address, then just call to that location.

You'll have to set a flag in the handler to indicate it was invalid (either for writing the ret or trying to execute it), and you'll need to replace the original contents if it was valid.

Alternatively, you could possibly walk the IDT/GDT/LDT tables to see if your address exists within any of them.

Good luck finding an OS that allows either of these to be done by mere mortals :-)

Perhaps if you could tell us why you want to do this, rather than just assuming this is the best way to achieve what you want, we could think of another solution.

paxdiablo