⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libefence.3

📁 网络时间协议NTP 源码 版本v4.2.0b 该源码用于linux平台下
💻 3
字号:
.TH efence 3 27-April-1993.SH NAMEefence \- Electric Fence Malloc Debugger.SH SYNOPSIS.nf.ft B#include <stdlib.h>.ft.fi.LP.nf.ft Bvoid * malloc (size_t size);.ft.fi.LP.nf.ft Bvoid free (void *ptr);.ft.fi.LP.nf.ft Bvoid * realloc (void *ptr, size_t size);.ft.fi.LP.nf.ft Bvoid * calloc (size_t nelem, size_t elsize);.ft.fi.LP.nf.ft Bvoid * memalign (size_t alignment, size_t size);.ft.fi.LP.nf.ft Bvoid * valloc (size_t size);.ft.fi.LP.nf.ft Bextern int EF_ALIGNMENT;.ft.fi.LP.nf.ft Bextern int EF_PROTECT_BELOW;.ft.fi.LP.nf.ft Bextern int EF_PROTECT_FREE;.ft.fi.SH DESCRIPTION.I Electric Fencehelps you detect two common programming bugs:software that overruns the boundaries of a malloc() memoryallocation, and software that touches a memory allocation that has beenreleased by free(). Unlike other malloc() debuggers, Electric Fence willdetect.I readaccesses as well as writes, and it will pinpoint the exact instruction thatcauses an error. It has been in use at Pixar since 1987, and at many othersites for years..LPElectric Fence uses the virtual memory hardware of your computer to place aninaccessible memory page immediately after (or before, at the user's option)each memory allocation. When software reads or writes this inaccessible page,thehardware issues a segmentation fault, stopping the program at the offendinginstruction. It is then trivial to find the erroneous statement using yourfavorite debugger. In a similar manner, memory that has been released byfree() is made inaccessible, and any code that touches it will get asegmentation fault..LPSimply linking your application with libefence.a will allow you to detectmost, but not all, malloc buffer overruns and accesses of free memory.If you want to be reasonably sure that you've found.I allbugs of this type, you'll have to read and understand the rest of thisman page..SH USAGELink your program with the library.B libefence.a .Make sure you are.I notlinking with.B -lmalloc,.B -lmallocdebug,or with other malloc-debugger or malloc-enhancer libraries.You can only use one at a time.If your system administratorhas installed Electric Fence for public use, you'll be able to use the.B -lefenceargument to the linker, otherwise you'll have to put the path-name for.B libefence.ain the linker's command line.Some systems will require special arguments to the linker to assure thatyou are using the Electric Fence malloc() and not the one from your C library.On AIX systems, you may have to use the flags.br.B -bnso.B -bnodelcsect.B -bI:/lib/syscalls.exp.brOn Sun systems running SunOS 4.X, you'll probably have to use.B -Bstatic..LPRun your program.I using a debugger. It's easier to work this way than to create a.B corefile and post-mortem debug it. Electric Fence can create.I hugecore files, and some operating systems will thus take minutes simply to dumpcore! Some operating systems will not create usable core files from programsthat are linked with Electric Fence.If your program has one of the errors detected by Electric Fence, it willget a segmentation fault (SIGSEGV) at the offending instruction. Use thedebugger to locate the erroneous statement, and repair it..SH GLOBAL AND ENVIRONMENT VARIABLESElectric Fence has four configuration switches that can be enabled viathe shell environment, or by setting the value of global integer variablesusing a debugger. These switches change what bugs Electric Fence will detect,so it's important that you know how to use them..TPEF_ALIGNMENTThis is an integer that specifies the alignment for any memory allocationsthat will be returned by malloc(), calloc(), and realloc().The value is specified inbytes, thus a value of 4 will cause memory to be aligned to 32-bit boundariesunless your system doesn't have a 8-bit characters. EF_ALIGNMENT is set tosizeof(int) by default, since that is generally the word-size of your CPU.If your program requires that allocations be aligned to 64-bitboundaries and you have a 32-bit.B intyou'll have to set this value to 8. This is the case when compiling with the.B -mips2flag on MIPS-based systems such as those from SGI.The memory allocation that is returned by Electric Fence malloc() is alignedusing the value in EF_ALIGNMENT, and.I its size the multiple of.I that valuethat is greater than or equal to the requested size.For this reason, you will sometimes want to set EF_ALIGNMENT to 0 (noalignment), so thatyou can detect overruns of less than your CPU's word size. Be sure to readthe section.I WORD-ALIGNMENT AND OVERRUN DETECTIONin this manual page before you try this.To change this value, set EF_ALIGNMENT in the shell environment to aninteger value, or assignto the global integer variable EF_ALIGNMENT using a debugger..TPEF_PROTECT_BELOWElectric Fence usually places an inaccessible page immediately after eachmemory allocation, so that software that runs past the end of the allocationwill be detected. Setting EF_PROTECT_BELOW to 1 causes Electric Fenceto place the inaccessible page.I beforethe allocation in the address space, so that under-runs will be detectedinstead of over-runs.When EF_PROTECT_BELOW is set, the EF_ALIGNMENT parameter is ignored.All allocations will be aligned to virtual-memory-page boundaries, andtheir size will be the exact size that was requested.To change this value, set EF_PROTECT_BELOW in the shell environment to aninteger value, or assign to the global integer variable EF_PROTECT_BELOW usinga debugger..TPEF_PROTECT_FREEElectric Fence usually returns free memory to a pool from which it may bere-allocated. If you suspect that a program may be touching free memory,set EF_PROTECT_FREE to 1. This will cause Electric Fence to never re-allocatememory once it has been freed, so that any access to free memory will bedetected. Some programs will use tremendous amounts of memory when thisparameter is set.To change this value, set EF_PROTECT_FREE in the shell environment to aninteger value, or assign to the global integer variable EF_PROTECT_FREE usinga debugger..TPEF_ALLOW_MALLOC_0By default, Electric Fence traps calls to malloc() with a size of zero, becausethey are often the result of a software bug. If EF_ALLOW_MALLOC_0 is non-zero,the software will not trap calls to malloc() with a size of zero.To change this value, set EF_ALLOC_MALLOC_0 in the shell environment to aninteger value, or assign to the global integer variable EF_ALLOC_MALLOC_0 usinga debugger..SH WORD-ALIGNMENT AND OVERRUN DETECTIONThere is a conflict between the alignment restrictions that malloc() operatesunder and the debugging strategy used by Electric Fence. When detectingoverruns, Electric Fence malloc() allocates two or more virtual memorypages for each allocation. The last page is made inaccessible in such a waythat any read, write, or execute access will cause a segmentation fault.Then, Electric Fence malloc() will return an address such that the firstbyte afterthe end of the allocation is on the inaccessible page.Thus, any overrunof the allocation will cause a segmentation fault..LPIt follows that theaddress returned by malloc() is the address of the inaccessible page minusthe size of the memory allocation.Unfortunately, malloc() is required to return.I word-alignedallocations, 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 thatis not a multiple of the word size, and expects to do word accesses to thatallocation. The location of the inaccessible page is fixed by hardware ata word-aligned address. If Electric Fence malloc() is to return an alignedaddress, it must increase the size of the allocation to a multiple of theword size.In addition, the functions memalign() and valloc() must honor explicitspecifications on the alignment of the memory allocation, and this, as wellcan only be implemented by increasing the size of the allocation.Thus, there will be situations in which the end of a memory allocationcontains some padding space, and accesses of that padding space will notbe detected, even if they are overruns..LPElectric Fence provides the variable EF_ALIGNMENT so that the user cancontrol the default alignment used by malloc(), calloc(), and realloc().To debug overruns as small as a single byte, you can set EF_ALIGNMENT tozero. This will result in Electric Fence malloc() returning unalignedaddresses for allocations with sizes that are not a multiple of the wordsize. This is not a problem in most cases, because compilers must pad thesize of objects so that alignment restrictions are honored when storingthose objects in arrays. The problem surfaces when software allocatesodd-sized buffers for objects that must be word-aligned. One case of thisis software that allocates a buffer to contain a structure and astring, and the string has an odd size (this example was in a popular TIFFlibrary). If word references are made to un-aligned buffers, you will seea bus error (SIGBUS) instead of a segmentation fault. The only way to fixthis is to re-write the offending code to make byte references or not makeodd-sized allocations, or to set EF_ALIGNMENT to the word size..LPAnother example of software incompatible withEF_ALIGNMENT < word-sizeis the strcmp() function and other string functions on SunOS (and probablySolaris), which make word-sized accesses to character strings, and mayattempt to access up to three bytes beyond the end of a string. Theseresult in a segmentation fault (SIGSEGV). The only way around this is touse versions of the string functions that perform byte references insteadof word references..SH INSTRUCTIONS FOR DEBUGGING YOUR PROGRAM.TP1.Link with libefence.a as explained above..TP2.Run your program in a debugger and fix any overruns or accesses to free memory..TP3.Quit the debugger..TP4.Set EF_PROTECT_BELOW = 1 in the shell environment..TP5.Repeat step 2, this time repairing underruns if they occur..TP6.Quit the debugger..TP7.Read the restrictions in the section on.I WORD-ALIGNMENT AND OVERRUN DETECTION.See if you canset EF_ALIGNMENT to 0 and repeat step 2. Sometimes this will be too much work,or there will be problems with library routines for which you don't have thesource, that will prevent you from doing this..SH MEMORY USAGE AND EXECUTION SPEEDSince Electric Fence uses at least two virtual memory pages for each of itsallocations, it's a terrible memory hog. I've sometimes found it necessary toadd a swap file using swapon(8) so that the system would have enough virtualmemory to debug my program. Also, the way we manipulate memory results invarious cache and translation buffer entries being flushed with each callto malloc or free. The end result is that your program will be much slowerand use more resources while you are debugging it with Electric Fence..LPDon't leave libefence.a linked into production software! Use it onlyfor debugging..SH PORTINGElectric Fence is written for ANSI C. You should be able to port it withsimple changes to the Makefile and to page.c,which contains the memory management primitives .Many POSIX platforms will require only a re-compile.The operating system facilities required to port Electric Fence are:.IPA way to allocate memory pages.brA way to make selected pages inaccessible..brA way to make the pages accessible again..brA way to detect when a program touches an inaccessible page..brA way to print messages..LPPlease e-mail me a copy of any changes you have to make, so that I canmerge them into the distribution..SH AUTHORBruce Perens.SH WARNINGSI have tried to do as good a job as I can on this software, but I doubtthat it is even theoretically possible to make it bug-free.This software has no warranty. It will not detect some bugs that you mightexpect it to detect, and will indicate that some non-bugs are bugs.Bruce Perens and/or Pixar will not be liable to any claims resultingfrom the use of this software or the ideas within it.The entire responsibility for its use mustbe assumed by the user. If you use it and it results in loss of lifeand/or property, tough. If it leads you on a wild goose chase and you wastetwo weeks debugging something, too bad.If you can't deal with the above, please don't use the software! I've writtenthis in an attempt to help other people, not to get myself sued or prosecuted..SH LICENSECopyright 1987-1995 Bruce Perens. All rights reserved..brThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License, Version 2,as published by the Free Software Foundation. A copy of this license isdistributed 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Read thefile "COPYING" for more details..SH CONTACTING THE AUTHOR.nfBruce Perensc/o Pixar1001 West Cutting Blvd., Suite 200Richmond, CA 94804Telephone: 510-215-3502Fax: 510-236-0388Internet: Bruce@Pixar.com.fi.ft.SH FILES/dev/zero: Source of memory pages (via mmap(2))..SH SEE ALSOmalloc(3), mmap(2), mprotect(2), swapon(8).SH DIAGNOSTICSSegmentation Fault: Examine the offending statement for violation of theboundaries of a memory allocation..brBus Error: See the section on.I WORD-ALIGNMENT AND OVERRUN DETECTION.in this manual page..SH BUGSMy explanation of the alignment issue could be improved..LPSome Sun systems running SunOS 4.1 are reported to signal an access to aprotected page with.B  SIGBUSrather than.B SIGSEGV,I suspect this is an undocumented feature of a particular Sun hardwareversion, not just the operating system.On these systems, eftest will fail with a bus error until you modify theMakefile to define.B PAGE_PROTECTION_VIOLATED_SIGNALas.B SIGBUS..LPThere are, without doubt, other bugs and porting issues. Please contact me viae-mail if you have any bug reports, ideas, etc..SH WHAT'S BETTERPURIFY, from Purify Systems, does a much better job than Electric Fence, anddoes much more. It's available at this writing on SPARC and HP.I'm not affiliated with Purify, I just think it's a wonderful productand you should check it out.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -