-
大小: 30KB文件类型: .gz金币: 2下载: 1 次发布日期: 2021-06-25
- 语言: C/C++
- 标签: electric-fen efence
资源简介
electric fence是Linux下的C语言内存越界访问检测工具,可检测如下两种情况:1、内存访问越界 2、使用释放后续的内存
使用方法:
1、make编译得到libefence.a静态库
2、在gcc编译时增加 -L . -lefence的选项
代码片段和文件信息
/*
* Electric Fence - Red-Zone memory allocator.
* Bruce Perens 1988 1993
*
* This is a special version of malloc() and company for debugging software
* that is suspected of overrunning or underrunning the boundaries of a
* malloc buffer or touching free memory.
*
* It arranges for each malloc buffer to be followed (or preceded)
* in the address space by an inaccessable virtual memory page
* and for free memory to be inaccessable. If software touches the
* inaccessable page it will get an immediate segmentation
* fault. It is then trivial to uncover the offending code using a debugger.
*
* An advantage of this product over most malloc debuggers is that this one
* detects reading out of bounds as well as writing and this one stops on
* the exact instruction that causes the error rather than waiting until the
* next boundary check.
*
* There is one product that debugs malloc buffer overruns
* better than Electric Fence: “Purify“ from Purify Systems and that‘s only
* a small part of what Purify does. I‘m not affiliated with Purify I just
* respect a job well done.
*
* This version of malloc() should not be linked into production software
* since it tremendously increases the time and memory overhead of malloc().
* Each malloc buffer will consume a minimum of two virtual memory pages
* this is 16 kilobytes on many systems. On some systems it will be necessary
* to increase the amount of swap space in order to debug large programs that
* perform lots of allocation because of the per-buffer overhead.
*/
#include “efence.h“
#include
#include
#include
#include
#include
#ifdef USE_SEMAPHORE
# include
# include
#endif
#ifdef malloc
#undef malloc
#endif
#ifdef calloc
#undef calloc
#endif
static const char version[] = “\n Electric Fence 2.2“
“ Copyright (C) 1987-1999 Bruce Perens \n“;
/*
* MEMORY_CREATION_SIZE is the amount of memory to get from the operating
* system at one time. We‘ll break that memory down into smaller pieces for
* malloc buffers. One megabyte is probably a good value.
*/
#define MEMORY_CREATION_SIZE 1024 * 1024
/*
* Enum Mode indicates the status of a malloc buffer.
*/
enum _Mode {
NOT_IN_USE = 0 /* Available to represent a malloc buffer. */
FREE /* A free buffer. */
ALLOCATED /* A buffer that is in use. */
PROTECTED /* A freed buffer that can not be allocated again. */
INTERNAL_USE /* A buffer used internally by malloc(). */
};
typedef enum _Mode Mode;
/*
* Struct Slot contains all of the information about a malloc buffer except
* for the contents of its memory.
*/
struct _Slot {
void * userAddress;
void * internalAddress;
size_t userSize;
size_t internalSize;
Mode mode;
};
typedef struct _Slot Slot;
static void *memalign_locked(size_t alignment size_t userSize);
static void free_locked(void *address);
/*
* EF_DISABLE_BANNER is a global variable used to c
- 上一篇:利用FFT计算电压有效值的C语言例程
- 下一篇:C语言实现的RC4算法
评论
共有 条评论