9.16 Use runtime heap checking

9.16 Use runtime heap checking

9.16.1 Basic tactic

Use a memory allocator that performs runtime checking of heap integrity.

9.16.2 Purpose

Identify corruption to the memory allocator data structures.

9.16.3 Questions answered

  • Which block of memory is being corrupted?

  • When is the corruption occurring?

9.16.4 Potential problems

  • If heap integrity is checked every time memory is allocated or freed, the application may become much slower.

  • If you only check the heap when memory is allocated and freed, many pointer references may occur in between. This tactic isn’t as strong as checking every pointer reference.

9.16.5 Refined tactics

Make every pointer reference through one of two macros. One of them just resolves to the pointer reference. The other makes a function call, which calls the heap integrity checker before dereferencing the pointer. Change the first macro to the second, file by file, until you find the problem. This will cause a drastic slowdown in the program.

9.16.6 Related tactics

  • Implement wrapper functions around the standard memory allocation and deallocation procedures. The functions should maintain and check their own list of allocated storage.

  • Use a high-level debugger or interpreter to do the following:

    • Set a breakpoint at a place where you would like to begin checking the integrity of the heap.

    • Add a command list to the breakpoint that calls a user procedure to perform checking of heap integrity, and then continue execution.

9.16.7 Choosing tactics

Use this tactic when one of the following conditions is true:

  • A bug occurs when the program is run standalone, but not when it’s run under a debugger.

  • A bug occurs when the program is run under one user account (and environment), but not when the program is run under another.

Use this tactic at least once before the program is put into production, as an additional testing strategy.

9.16.8 Language specifics

  • C++: You can overload new and delete. You can also create special versions of new or delete, using the placement argument, that perform heap checking before using the normal form of these procedures to allocate storage.

  • Java: Java guarantees heap integrity, so the tactic is unnecessary.

  • C: Several publicly available versions of malloc and free perform heap integrity checking.

  • Fortran: Heap integrity can only be compromised by invalid array subscripts.

Debugging by Thinking. A Multidisciplinary Approach
Debugging by Thinking: A Multidisciplinary Approach (HP Technologies)
ISBN: 1555583075
EAN: 2147483647
Year: 2002
Pages: 172

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net