Debugging Dynamic Memory Usage Errors Using NonStop Native Inspect White Paper

Table 5 Commands for Monitoring Heap Corruption
DescriptionCommand
Toggles validation of calls to strcpy(), strncpy(),
memcpy(), memccpy(),memset(), memmove(),
bzero(), strdup(),and, bcopy().
set heap-check string <on/off>
Toggles the bounds-checking feature for detection of
heap-corruption in Native Inspect.
set heap-check bounds <on/off>
Displays a list of all the dangling pointers and dangling
blocks that are potential sources of memory corruption.
info corruption
Finding the precise line where heap corruption occurs requires some more effort. There are several
strategies to find this point. If an address in heap space is being overwritten, the user can restart
the application and then place a MAB, memory access breakpoint, using the suspected address
after the point of allocation. Also, the application could be restarted then run to the allocation point
for a corrupted block of memory. Then a process of binary searches could be performed to find
the precise point of corruption. The searching requires finding a good mid-point of execution
between the lines where you know the block is okay and where the info corruption command
reported a failure. If the corrupt block is a character string, the following section may help with
information on another method of finding the problem.
Monitoring String Corruption
The set heap-check string <on/off> command toggles the string corruption detection
feature. It enables you to detect string corruption if functions of the strcpy() family write
out-of-bounds of the allocated memory. Example 3 illustrates the use of the set heap-check
string command.
This command currently detects string corruption when writing out-of-bounds for strcpy(),
strncpy(), memcpy(), memccpy(),memset(), memmove(), bzero(), strdup(),
and bcopy() functions.
Example 3 Monitoring heap-corruption caused by erroneous handling of string functions
Sample Program
1 #include <stdio.h>
2
3 int main()
4 {
5 char *ptr, *ptr1;
6
7 ptr = (char*)malloc(10);
8 ptr1 = (char *)malloc(20);
9
10 strcpy(ptr, "Hello");
11 strcpy(ptr1, "Welcome to Native Inspect");
12
13 memcpy(ptr+5,ptr1,10);
14 }
Sample Debugging Session
(eInspect 1,1193): set heap-check on
(eInspect 1,1193): set heap-check string on
(eInspect 1,1193): c
Continuing.
warning: strcpy corrupted (address = 0x08429d18 size = 20)
#1 main() at \PELICAN.$DATA4.USER.EX3C:8
#2 _MAIN() at \SPEEDY.$RLSE.T8432H02.CPLMAINC:46
warning: Use command backtrace (bt) to see the current context.
Ignore top 4 frames belonging to leak detection library of eInspect.
14