|
NAMEefence - Electric Fence Malloc DebuggerSYNOPSIS#include <stdlib.h> void * malloc (size_t size); void free (void *ptr); void * realloc (void *ptr, size_t size); void * calloc (size_t nelem, size_t elsize); void * memalign (size_t alignment, size_t size); void * valloc (size_t size); extern int EF_ALIGNMENT; extern int EF_PROTECT_BELOW; extern int EF_PROTECT_FREE; extern int EF_ALLOW_MALLOC_0; extern int EF_FILL; DESCRIPTIONElectric Fence helps you detect two common programming bugs: software that overruns the boundaries of a malloc() memory allocation, and software that touches a memory allocation that has been released by free(). Unlike other malloc() debuggers, Electric Fence will detect read accesses as well as writes, and it will pinpoint the exact instruction that causes an error. It has been in use at Pixar since 1987, and at many other sites for years.Electric Fence uses the virtual memory hardware of your computer to place an inaccessible memory page immediately after (or before, at the user's option) each memory allocation. When software reads or writes this inaccessible page, the hardware issues a segmentation fault, stopping the program at the offending instruction. It is then trivial to find the erroneous statement using your favorite debugger. In a similar manner, memory that has been released by free() is made inaccessible, and any code that touches it will get a segmentation fault. Simply linking your application with libefence.a will allow you to detect most, but not all, malloc buffer overruns and accesses of free memory. If you want to be reasonably sure that you've found all bugs of this type, you'll have to read and understand the rest of this man page. USAGELink your program with the library libefence.a . Make sure you are not linking with -lmalloc, -lmallocdebug, or with other malloc-debugger or malloc-enhancer libraries. You can only use one at a time. If your system administrator has installed Electric Fence for public use, you'll be able to use the -lefence argument to the linker, otherwise you'll have to put the path-name for libefence.a in the linker's command line. You can also use dynamic linking. If you're using a Bourne shell, the statement export LD_PRELOAD=libefence.so.0.0 will cause Electric Fence to be loaded to run all dynamic executables. The command ef command runs a single command under Electric Fence.Some systems will require special arguments to the linker to assure that you are using the Electric Fence malloc() and not the one from your C library. Run your program using a debugger. It's easier to work this way than to create a core file and post-mortem debug it. Electric Fence can create huge core files, and some operating systems will thus take minutes simply to dump core! Some operating systems will not create usable core files from programs that are linked with Electric Fence. If your program has one of the errors detected by Electric Fence, it will get a segmentation fault (SIGSEGV) at the offending instruction. Use the debugger to locate the erroneous statement, and repair it. GLOBAL AND ENVIRONMENT VARIABLESElectric Fence has four configuration switches that can be enabled via the shell environment, or by setting the value of global integer variables using a debugger. These switches change what bugs Electric Fence will detect, so it's important that you know how to use them.
WORD-ALIGNMENT AND OVERRUN DETECTIONThere is a conflict between the alignment restrictions that malloc() operates under and the debugging strategy used by Electric Fence. When detecting overruns, Electric Fence malloc() allocates two or more virtual memory pages for each allocation. The last page is made inaccessible in such a way that any read, write, or execute access will cause a segmentation fault. Then, Electric Fence malloc() will return an address such that the first byte after the end of the allocation is on the inaccessible page. Thus, any overrun of the allocation will cause a segmentation fault.It follows that the address returned by malloc() is the address of the inaccessible page minus the size of the memory allocation. Unfortunately, malloc() is required to return word-aligned allocations, since many CPUs can only access a word when its address is aligned. The conflict happens when software makes a memory allocation using a size that is not a multiple of the word size, and expects to do word accesses to that allocation. The location of the inaccessible page is fixed by hardware at a word-aligned address. If Electric Fence malloc() is to return an aligned address, it must increase the size of the allocation to a multiple of the word size. In addition, the functions memalign() and valloc() must honor explicit specifications on the alignment of the memory allocation, and this, as well can only be implemented by increasing the size of the allocation. Thus, there will be situations in which the end of a memory allocation contains some padding space, and accesses of that padding space will not be detected, even if they are overruns. Electric Fence provides the variable EF_ALIGNMENT so that the user can control the default alignment used by malloc(), calloc(), and realloc(). To debug overruns as small as a single byte, you can set EF_ALIGNMENT to zero. This will result in Electric Fence malloc() returning unaligned addresses for allocations with sizes that are not a multiple of the word size. This is not a problem in most cases, because compilers must pad the size of objects so that alignment restrictions are honored when storing those objects in arrays. The problem surfaces when software allocates odd-sized buffers for objects that must be word-aligned. One case of this is software that allocates a buffer to contain a structure and a string, and the string has an odd size (this example was in a popular TIFF library). If word references are made to un-aligned buffers, you will see a bus error (SIGBUS) instead of a segmentation fault. The only way to fix this is to re-write the offending code to make byte references or not make odd-sized allocations, or to set EF_ALIGNMENT to the word size. Another example of software incompatible with EF_ALIGNMENT < word-size is the strcmp() function and other string functions on SunOS (and probably Solaris), which make word-sized accesses to character strings, and may attempt to access up to three bytes beyond the end of a string. These result in a segmentation fault (SIGSEGV). The only way around this is to use versions of the string functions that perform byte references instead of word references. INSTRUCTIONS FOR DEBUGGING YOUR PROGRAM
MEMORY USAGE AND EXECUTION SPEEDSince Electric Fence uses at least two virtual memory pages for each of its allocations, it's a terrible memory hog. I've sometimes found it necessary to add a swap file using swapon(8) so that the system would have enough virtual memory to debug my program. Also, the way we manipulate memory results in various cache and translation buffer entries being flushed with each call to malloc or free. The end result is that your program will be much slower and use more resources while you are debugging it with Electric Fence.Don't leave libefence.a linked into production software! Use it only for debugging. MAILING LISTThere is a mailing list to support Electric Fence. You can subscribe using the mail form at http://lists.perens.com/mailman/listinfo/electric-fence .AUTHORBruce PerensWARNINGSI have tried to do as good a job as I can on this software, but I doubt that it is even theoretically possible to make it bug-free. This software has no warranty. It will not detect some bugs that you might expect it to detect, and will indicate that some non-bugs are bugs.LICENSECopyright 1987-1999 Bruce Perens. All rights reserved.This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, Version 2, as published by the Free Software Foundation. A copy of this license is distributed with this software in the file "COPYING". This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Read the file "COPYING" for more details. CONTACTING THE AUTHORBruce Perens 1563 Solano Ave. #349 Berkeley, CA 94707 Telephone: 510-526-1165 Internet: bruce@perens.com FILES/dev/zero: Source of memory pages (via mmap(2)).SEE ALSOmalloc(3), mmap(2), mprotect(2), swapon(8)DIAGNOSTICSSegmentation Fault: Examine the offending statement for violation of the boundaries of a memory allocation.Bus Error: See the section on WORD-ALIGNMENT AND OVERRUN DETECTION. in this manual page. BUGSMy explanation of the alignment issue could be improved.Some Sun systems running SunOS 4.1 were reported to signal an access to a protected page with SIGBUS rather than SIGSEGV, I suspect this is an undocumented feature of a particular Sun hardware version, not just the operating system. On these systems, eftest will fail with a bus error until you modify the Makefile to define PAGE_PROTECTION_VIOLATED_SIGNAL as SIGBUS. There are, without doubt, other bugs and porting issues. Please contact me via e-mail if you have any bug reports, ideas, etc. WHAT'S BETTERPurify does a much more thorough job than Electric Fence, and does not have the huge memory overhead. Checkergcc, a modified version of the GNU C Compiler that instruments all memory references, is available on Linux systems and where GCC is used. It performs some of the same tasks as Purify, but only on code that it has compiled.
Visit the GSP FreeBSD Man Page Interface. |