How do I make an exception when memory is written over?

Started by xanatose, May 25, 2005, 02:19:32 AM

Previous topic - Next topic

xanatose

Under XP

Is it possible to make the hardware throw an exception when a memory area is written. If so, how do you do that?

I want this for debugging so that the programs blows up in my debugger when a pointers goes out of bounds, instead of blowing up on some poor user machine    :P




Robert Collins

I'm not sure you want to throw an exception, that will occur anyway. What you want is to catch the exception in an error trapping routine. Kind of like you do in VB with the On Error or in Java with the catch(Exception e). I just don't know how you do it in Assembly.

hutch--

I would be lookiing for a code design that controls the range of addressing so it does not read or write to memory that it is not supposed to. The system will display an exception if you do it with no check mecahnism and if you really have to write code that has that risk, you would be looking at structured exception handling to handle the problem before the system does and shuts down your app.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

The only way I know of is to use VirtualProtect with the whole page set with PAGE_GUARD. But, this will generate an exception whenever ANY byte in the page (whole 4k) is accessed. Which means you can then check to see if the access was out of bounds (after you find out what array it was supposed to be in, and what that array's bounds are, etc.) And then act accordingly if it was, or carry on if not.
You also have to remember to re-set the page_guard status, as it's removed for each access ::)
Of course, all of this is nice and slllooowwwwwwwww :lol
No snowflake in an avalanche feels responsible.

Infro_X

PAGE_READONLY i do believe would make it so you'd know if the memmory was writen over, but still, another user could call virtualprotect on it and change it to readwrite. The only "quality" way to do this would either be by modifing the page table entries, and controlling interrupts, or modifying the GDT. Neither of which is a suitable solution. PAGE_GUARD maybe/probably is your best option. One last way of doing it is to create another process, from within your process, and debug your orignal process and set memmory breakpoint on the entire peice of memmory, a long task for something so simple. Perhaps there are better solutions, but i cannot think of them, or i am unaware of any. Best of luck. Infro