macOS下AddressSanitizer与leaks工具无法检测内存泄漏求助
现象描述
1. AddressSanitizer运行报错
用-fsanitize=address编译后的程序运行时,仅返回以下错误,完全没有内存泄漏提示:
a.out(45811,0x7ff856c70680) malloc: nano zone abandoned due to inability to reserve vm space.
2. leaks工具检测报错
使用leaks工具分析进程时,返回一系列错误信息:
a.out(46256,0x7ff856c70680) malloc: nano zone abandoned due to inability to reserve vm space.
a.out(46256) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
a.out(46256) MallocStackLogging: zone[0] is not the normal default zone so can't turn on lite mode.
Can't examine target process's malloc zone asan_0x1108535b8, so memory analysis will be incomplete or incorrect.
Reason: for security, cannot load non-system library /Library/Developer/CommandLineTools/usr/lib/clang/16/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
测试代码
为验证问题,使用一段存在明确内存泄漏的测试代码:
#include <stdlib.h> int main(void) { char *str; str = (char *)malloc(10*sizeof(char)); str[0] = 'a'; str[1] = 0; exit(0); }
编译命令
编译代码使用的命令:
cc -Wextra -Wall -Werror -fsanitize=address main.c
疑问
这段代码在Ubuntu系统中用AddressSanitizer可以正常检测到内存泄漏,但在macOS下始终无法触发泄漏提示,仅返回上述错误。请问这一问题的原因是什么?
内容的提问来源于stack exchange,提问作者Jose

