It attempts to use the virtual memory facilities of Linux to protect the memory used by malloc and free to halt the program at the point of memory corruption.
2. How to install
a). Get the source from the address: http://perens.com/FreeSoftware/ElectricFence/
b). use command tar to decomprss the file
c). just make >> make install >> make clean
3. How to use
a). Get a test file, which can be like below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr = (char *) malloc(1024);
ptr[0] = 0;
/* Now write beyond the block */
ptr[1024] = 0;
exit(0);
}
b). Use different way to compile it and check the effect
>>normal way without efence:
test way 1:
gcc -o test test.c
run command ./test and the result is just fine, no error return.
test way 2:
use gdb to run test, it need to be recompilled
gcc -g -o test test.c
run command gdb ./test then input commandrun the result as below :
Starting program: /home/test
[Inferior 1 (process 23732) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.9.i686
>>use the efence library
test way 1:
gcc -o test test.c -lefence -lpthread
run command ./test and the result is below:
Electric Fence 2.1 Copyright (C) 1987-1998 Bruce Perens.
段错误(吐核)
test way 2:
gcc -g -o test test.c -lefence -lpthread
run command gdb ./test then input commandrun the result as below :
Starting program: /home/test
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
Electric Fence 2.1 Copyright (C) 1987-1998 Bruce Perens.
Program received signal SIGSEGV, Segmentation fault.
0x0804889d in main () at test.c:8
8 ptr[1024] = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.9.i686
4.summary
Use the efence can easily to find out the risk of memory .

本文介绍ElectricFence,一种利用Linux虚拟内存机制保护malloc和free的工具,防止内存溢出引发的问题。通过实例演示了如何安装、使用该库,并对比了启用前后程序的行为差异。
707

被折叠的 条评论
为什么被折叠?



