Memory debug vs Range checking

delphi package - automated exception handling
Post Reply
clickbutt
Posts: 1
Joined: Mon Aug 01, 2016 12:17 pm

Memory debug vs Range checking

Post by clickbutt »

How is the memory debug mode different than simply enabling range checking?
Also, why cant we check for both overrun and underrun at the same time?

ps. Thank you for creating such an amazing tool. The thread deadlock detection has saved me hours of debugging!
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Memory debug vs Range checking

Post by madshi »

Range checking is a compiler feature. With that option enabled, the compiler adds code everywhere you access fixed size arrays to manually raise exceptions when you access such arrays out of bounds. However, this only works for arrays of fixed size where the compiler knows exactly where and how to check. If you do pointer math, or use dynamic arrays, range checking doesn't work.

The memory debug mode works very differently. It doesn't add any code when you access arrays (or anything else). Instead, the memory debug mode allocates memory pages in such a way that in the moment when you overrun an allocated buffer, you actually access a new page which is not allocated at all, resulting in a direct access violation exception being raised by the OS. This method works very well to detect overruns, but it's expensive in terms of RAM consumption, because basically for every allocation you make (even if it's only 1 byte long), your memory range loses 2 pages: 1 page is allocated for your buffer. Another page is not allocated, but reserved (so can't be allocated). Your allocated buffer is then placed at the end of the allocated page, so that when you overwrite the buffer, you run into the non-allocated page.

Buffer underrun works similar, but the non-allocated page must be placed *before* the buffer, obviously, so a different allocation technique must be used.

Doing buffer overrun and underrun detection can't be done at the same time, at least not when using this memory allocation technique - except for buffers that are exactly 1 page long.
Post Reply