Technical article 112241

What memory checking tools are built into UnixWare 7 and SCO OpenServer 5?

OpenServer 6.0.0OpenServer 5.0.5OpenServer 5.0.6OpenServer 5.0.7UnixWare 7.0.0UnixWare 7.0.1UnixWare 7.1.0UnixWare 7.1.1UnixWare 7.1.2UnixWare 7.1.3UnixWare 7.1.4Open UNIX 8

Written · Last revised

Problem

What memory checking tools are built into UnixWare 7 and SCO OpenServer 5?

Solution

Memory checking tools are built into UnixWare 7 libc and SCO OpenServer 5 when the UnixWare/OpenServer Development Kit (UODK) has been installed. These tools are described in detail in an SCO World magazine article from July/August2000 Developer's Corner, written by SCO development engineers:

Advanced Debugging on SCO: Memory Checking
Petr Sorfa and Dave Prosser

Here's a guide on how to activate and use the memory-checking tools in UnixWare 7 and OpenServer 5.

Memory checking is by far the most sought-after memory debugging feature for developers. It allows the developer to detect memory violations, such as the use of unallocated memory, the use of deallocated memory, and memory leaks. Commercial tools exist for detecting these problems, but those tools currently are not available for SCO operating systems. Now, with UnixWare 7.1.0, libc's memory routines have built-in memory checking capabilities you can activate at any time. These capabilities also exist on OpenServer 5. You must install OpenServer 5 with the UnixWare 7 compatibility package and use it with programs built with the UnixWare and OpenServer UDK (Universal Developers Kit). MALLOC_CHECKS and MALLOC_STATS provide some of the key capabilities of the memory-checking functionality present in commercial products.

An additional attribute of the memory-checking tools is not requiring any debugging information in the program executables. The programs can be fully optimized. Compilation with the -g option isn't necessary, but is recommended.

The Allocation Process

To understand how the MALLOC_CHECKS and MALLOC_STATS variables do memory checking, it is necessary to examine how memory is allocated.

When malloc() provides a memory block to its caller, the address returned has a minimum alignment of 8 bytes and a block header of 4 bytes preceding the returned address. The memory block allocated may be larger than requested for various reasons. The reasons include ensuring the alignment of the next block and ensuring the best-fitting, currently-free block is slightly bigger than needed. The extra bytes are called spare block space.

For example, when you are requesting 8 bytes, the minimum space actually allocated is shown in Listing 1. The spare 4 bytes are required to make sure the next block has an 8-byte alignment. The information needed to keep track of the entire space managed by malloc() (the arena) is intermixed with the blocks owned by those requesting dynamic memory allocation. Therefore, it is quite easy for a misbehaving program to alter block headers. A corrupt header may cause problems as the program runs. A minor change in an unrelated part of your program may well trigger a latent failure.

Minor changes can also be a problem when porting the program to another OS using a different memory-allocation algorithm. In fact, a common complaint when porting to another OS is the program functions incorrectly or causes core dumps for no apparent reason. A perfect example is the struct1.c program shown in Listing 2. If compiled with the UDK, the program executes with no problems on UnixWare 7. However, when compiled on Linux (for example, RedHat 6.2), the program dumps core. The result is usually a brash denouncement of the OS, when, in fact, the problem lies in the program code itself.

The MALLOC_CHECKS Variable
The MALLOC_CHECKS environment variable activates memory checking
in the UnixWare 7 libc (or the UDK libc on OpenServer 5).  The
values appropriate for assigning to this variable are 1 or
greater.  (The higher the number, the more or different checking
occurs.) Be aware, as a rule, the higher the number, the more
memory MALLOC_CHECKS uses. Program performance will most likely
be affected. In fact, program execution may fail due to memory
exhaustion.

Let us consider the program shown in Listing 2. After the compilation process is complete, you can execute the program, normally using the ./struct1 command. The output will be as shown in Listing 3. Although mistakes and memory violations are present in this program, it both executes and exits normally. Now, execute the program with MALLOC_CHECKS set to 3, using the MALLOCS_CHECKS=3 ./struct1 command.

Listing 4 shows the output of the preceding command. Let's look at the output line by line.

In line 2 of Listing 4, alloc error indicates an error took place for the memory libc manages. The (0x8049748/owner @0x8048568) output shows the error occurred for memory located at 0x8049748. A function at 0x8048568 allocated it. (0x8048568 is a hex location inside main.) The spare block space modified message implies the spare block was altered.

By examining the source, you can determine that line 16 of Listing 2 caused this error message. The error occurred because only 8 bytes were allocated and 15 bytes ("hello universe\0") have been written to the allocated space.

In line 3 of Listing 4, block header was modified indicates a block header was touched. In this case, it happened in the last 4 bytes of our first call to malloc(). Again, line 16 of Listing 2 is responsible for this error message. The call to strcpy() steps on the spare block space and some of the header block of the next memory block. Only 8 bytes were allocated (with 16 bytes overall-with an additional 4 bytes for the spare block and 4 for the header block). Therefore, the copying of hello universe (15 bytes including the trailing string delimiter \0) overwrote not only the contents of the spare block but also the first 3 bytes of the next header block as well.

In line 4 of Listing 4, free()d block was modified shows a free()d memory block has been altered. The responsible source line is Line 19 of Listing 2. Other errors, such as modified spare and header blocks, are not reported, since this memory area has been already free()d.

In line 6 of Listing 4 block multiply free()d indicates an attempt to free memory that has already been free()d. The source line responsible for this error is line 21 of Listing 2.

In line 7 of Listing 4, the error message occurs for the same reasons as in line 4. In line 8 we see realloc() of just free()d block, since the memory block has just been free()d and then reused.

The lines 9, 10, and 11 explanations are similar to those for lines 2, 3, and 4.

In line 12, instead of hello galaxy, the result is garbage output. The reason for this output is MALLOC_CHECKS assists further memory debugging by populating memory, when appropriate, with the 0xCA hex byte value.

Note that line 24 of Listing 2, strcpy (p, "hello galaxy"),
contains a memory error (as detected by lines 9, 10, and 11)
and a semantic error. The semantic error exists because the
realloc() function allocated new memory, rather than using the
given memory address.  The call to malloc() reallocated this
address in line 23. Thus, the "p" in line 24 points to the same
memory as originally allocated for "q".
The MALLOC_STATS Variable

The MALLOC_STATS environment variable displays the memory statistics for a program. You can re-execute the struct1 program, with MALLOC_STATS set to 2, using the MALLOC_STATS=2 ./struct1 command. This command results in the output shown in Listing 5. A line-by-line analysis follows.

In line 1 of Listing 5, malloc(8=>16-4) indicates the call to malloc() was for 8 bytes (size of struct foo_t). The total allocation is 16 bytes, 4 of which are for the spare block. The remaining 4 bytes are part of the header block. The exact location in the program that called malloc() is @0x8048568. The returned value from malloc() is->0x8049748.

The analysis of line 2 is quite similar to line 1, except the allocated memory is 16384 bytes. However, looking at Listing 2, there is no indication of where this call to malloc() could have originated. To determine where this malloc originated, we need to locate the function at 0xbffa1624. The UDK debugger can locate this function. For further details, refer to Listing 6. Using the debugger, we see the malloc() call originated from the printf() function on line 17 of Listing 2.

In line 4 of Listing 5, free(0x8049748) shows us the memory block located at 0x8049748 is being free()d. The notation @0x80485a4 is an address showing the exact point in the code responsible for the call to free (). In this case, it is line 18 of Listing 2, determined by the technique described in Listing 6.

The message in line 6 of Listing 5 is the same as in line 4, except free() is called from line 21 in Listing 2.

In line 7 of Listing 5, realloc (0x8049748,3=>8-4) indicates a reallocated request has been placed for the original 16-byte memory area (at 0x8049748). The request is to allocate 3 bytes-which, with the header block (4 bytes) and spare block (1 byte), is 8 bytes. In this line @0x80485d9 indicates the call to realloc() originated from line 22 of Listing 2. And ->0x804d8d8 is the return value from realloc(). Note this address is not associated with the original address passed to realloc. The realloc algorithm determined it would be more appropriate to allocate a new memory block of 8 bytes rather than to reuse part of the memory address passed.

In line 8 of Listing 5, the output is similar to line 1, except the call to malloc() occurs on line 23 of Listing 2.

Line 9 of Listing 5 is identical to line 4 in functionality.

An additional observation shows line 7 is the location of a memory leak, since the allocated/reallocated memory is never free()d. You can determine this leak by examining the MALLOC_STATS output and matching malloc()s, realloc()s, and free()s.

More assistance with tracking memory leaks through these
environment variables will become available in a future UDK
update.  Then, help will include listing all still-allocated
blocks at normal program termination.

Petr Sorfa, a member of SCO's Development Systems Group, is responsible for the debugger and the upcoming ANSI C++ standard library. An SCO ACE instructor turned developer, Petr has a special interest in open-source and graphical-development environments and debuggers. Incidentally, he's also the creator of the Skunkware artwork. His email address is petrs@sco.com.

Dave Prosser, a Development Systems architect, is a long-standing member of the ANSI C committee. His responsibilities include the C compiler, C library, and math library. You can reach him at dfp@sco.com.

Listing 1. The minimum space allocated
--------------------
| header | requested bytes | spare |
--------------------
| 4 bytes | 8 bytes | 4 bytes |
--------------------
^
address returned (8 byte aligned)
LISTING 2. The struct1.c program

1 #include <stdlib.h> 2 3 typedef struct foo 4 { 5 char letter; 6 int number; 7 } foo_t; 8 9 int 10 main (int argc, char **argv) 11 { 12 foo_t *p, *q; 13 14 p = malloc (sizeof (foo_t)); 15 16 strcpy (p, "hello universe"); 17 printf ("%s\n", p); 18 free (p); 19 strcpy (p, "hello world"); 20 printf ("%s\n", p); 21 free (p); 22 realloc (p, 3); 23 q = malloc (sizeof (foo_t)); 24 strcpy (p, "hello galaxy"); 25 free (q); 25 printf ("%s\n", p); 26 }

LISTING 3. Output of struct1 on UnixWare 7
hello world
hello universe
hello galaxy
LISTING 4. Output from MALLOC_CHECKS=3 ./struct1]

1 hello universe 2 alloc error (0x8049748/owner @0x8048568): spare block space was modified [for ./struct1] 3 alloc error (0x8049758): block header was modified [for ./struct1] 4 alloc error (0x8049748/owner @0x8048568): free()d block was modified [for ./struct1] 5 hello world 6 alloc error (0x8049748/owner @0x8048568): block multiply free()d [for ./struct1] 7 alloc error (0x8049748/owner @0x8048568): free()d block was modified [for ./struct1] 8 alloc error (0x8049748/owner @0x8048568): realloc() of just free()d block [for ./struct1] 9 alloc error (0x8049748/owner @0x80485e3): spare block space was modified [for ./struct1] 10 alloc error (0x8049758): block header was modified [for ./struct1] 11 alloc error (0x8049748/owner @0x80485e3): free()d block was modified [for ./struct1] 12 EEEEEEEEEEEEy

Listing 5. Output from MALLOC_STATS=2 ./struct1]

1 malloc(8=>16-4)@0x8048568->0x8049748 2 malloc(16384=>16392-4)@0xbffa1624->0x80498d0 3 hello universe 4 free(0x8049748)@0x80485a4 5 hello world 6 free(0x8049748)@0x80485d0 7 realloc(0x8049748,3=>8-4)@0x80485d9->0x804d8d8 8 malloc(8=>16-4)@0x80485e3->0x8049748 9 free(0x8049748)@0x804860c 10 hello galaxy

Listing 6. Output from debugging program struct1]
# Start up the debugger in command line
mode
$ debug -ic
# Start debugging the program struct1
debug> create struct1
New program struct1 (process p1) created
HALTED p1 [main in struct1.c]
14: p = malloc (sizeof (foo_t));
# Use the map command to determine where
the address 0xbffa1624 is located.
# By looking at the map output, we deduce
that the address is located in
# /usr/lib/libc.so.1 since it is in the
address range of 0xbff80000 and
# 0xbfffcfff. Note that the access
permissions are R_X (read and execute),
# indicates that this is the text area of
libc.so.1, i.e. executable code.
debug> map
Address Map for p1
Address Range Size Access Object File
0x08047000..0x08047fff 0x00001000 RWX
[STACK]
0x08048000..0x08048fff 0x00001000 R_X
struct1
0x08049000..0x08049fff 0x00001000 RWX
struct1
0xbff80000..0xbfffcfff 0x0007d000 R_X
/usr/lib/libc.so.1
0xbfffd000..0xbfffefff 0x00002000 RWX
/usr/lib/libc.so.1
# Now disassemble the code at address
0xbffa1624. The output shows that
# the code belongs to a function called
_findbuf
debug> dis 0xbffa1624
Listing 6. Output from debugging program
struct1] (continued)
Disassembly for p1, Program struct1
0xbffa1624 (_findbuf+164:) addl $0x4,
%esp
0xbffa1627 (_findbuf+167:) movl %eax,
%esi
0xbffa1629 (_findbuf+169:) testl %esi,
%esi
0xbffa162b (_findbuf+171:) jne +0xf
<bffa163c> [ _findbuf ]
0xbffa162d (_findbuf+173:) movl $0x2,
100(%esp)
0xbffa1635 (_findbuf+181:) leal 14(%ebp),
%esi
0xbffa1638 (_findbuf+184:) jmp +0xb
<bffa1645> [ _findbuf ]
0xbffa163a (_findbuf+186:) cmpl %edi,
%edi
0xbffa163c (_findbuf+188:) movb 12(%ebp),
%dl
0xbffa163f (_findbuf+191:) orb $0x8, %dl
# This is not particularly useful, as it
still does not tell us how program
# execution reached this point. To do so,
set a breakpoint at the address
debug> stop 0xbffa1624
EVENT [1] assigned
# Now run the program until the
breakpoint is reached.
debug> run
STOP EVENT TRIGGERED: 0xbffa1624 in p1
[_findbuf]
0xbffa1624 (_findbuf+164:) addl $0x4,
%esp
# Use the stack command to list the
current stack trace. Looking
# at the output, one can see that the
printf() function is responsible
# for the call to malloc().
debug> stack
Stack Trace for p1, Program struct1
*[0] _findbuf(presumed: 0xbfffd3c4,
0x8047dcc, 0x8047dec) [0xbffa1624]
[1] printf(0x80496e0, 0x8049748)
[0xbffb176b]
[2] main(argc=1, argv=0x8047dec,
0x8047df4) [struct1.c@17]
[3] _start() [0x8048504]
# Quit the debugger
debug> quit
)Venture Publishing, Inc.
)SCO World Magazine

See also

ta109645, "What is SLS OSS486A, the UDK Installation Supplement for SCO OpenServer 5.0.5?"

ta110499, "Basic differences between the UnixWare and OpenServer Development Kit (UDK) and the SCO OpenServer Development System."

ta111009, "How do I install the UnixWare and OpenServer Development Kit (UDK) on my SCO OpenServer Release 5 system?"

From the Xinuos technical article database, record 112241. Reproduced with permission. The original is at www.sco.com/ta/112241, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.