C++进程内存泄露检测工具——Valgrind & GCC

1、valgrind使用

(1)    机器上要安装valgrid

修改内存大小限制,修改配置文件

coregrind/m_aspacemgr/aspacemgr-linux.c

#if  defined(VGPV_arm_linux_android)  \

       || defined(VGPV_x86_linux_android)  \

       || defined(VGPV_mips32_linux_android) \

       || defined(VGPV_arm64_linux_android)

# define  VG_N_SEGMENTS  500000

#else

# define  VG_N_SEGMENTS  3000000

#endif

重新编译安装

(2)    启动程序的命令

后台启动:

nohup valgrind  --leak-check=full  --show-reachable=yes  --trace-children=yes  --error-limit=no  --log-file=./valgrind_output.log  ./xlongsrv &

前台启动:

valgrind --leak-check=full  ./ xlongsrv


2、GCC内存越界检查


如gets()到buffer的内容长度超出了buffer大小,则编译、运行时都会提示。

-fstatck_protector

-fsanitize=address高版本gcc(gcc version4.8.5 20150623 (Red Hat 4.8.5-4))支持

可能需要安装libasan(sudo yum install libasan)

编译器所采用的以 Canaries 探测为主的堆栈保护技术,

并且以 GCC 为例展示了 SSP 的实现方式和实际效果。

最后又简单介绍了突破编译器保护的一些方法

g++ -fsanitize=address -g -o test_vsnptest_vsnp.cpp



猜你喜欢

转载自blog.csdn.net/robotcat123/article/details/80593259