意图:检查源代码中是否存在某些地址,在free掉之后还对其进行了访问。
cat hello_sani.cpp
- #include
-
- using namespace std;
-
- int main(int argc, char **argv)
- {
- int i = 1;
- int *A = new int[12];
- cout <<"newed A &A[0]="<<&A[0]<<" &A[1]="<<&A[1]<
-
- delete [] A;
- cout<<"deleted A"<
- A[i]=1; // =1
- A[i] = A[i] + 1; // =2
- A[i]=3; // =3
-
- return 0;
- }
Makefie
- hello_sani:
-
- %: %.cp
- clang++ -g -O -fsanitize=address $< -o $@
-
- %: %.cpp
- clang++ -g -O -o $@ $<
-
- .PHONY: clean
- clean:
- rm -rf hello_sani
2, 编译运行效果
2.1 带 use after free 检查
编译命令:
clang++ -g -O -fsanitize=address $< -o $@
带 use after free 检查的运行效果,其中的错误提示看图:

2.2不 带 use after free 检查
编译命令:
clang++ -g -O $< -o $@
不带 use after free 检查的运行效果:

3,应用场景
可以在 Makefile 中加一个编译命令控制
ifeq (SAN 1)
SAN_FLAGS := -fsanitize=address
else
SAN_FLAGS :=
endif
%.o: %.cpp
clang++ -g -O $(SAN_FLAGS) $< -o $@
具体编译:
不做 sani check 时的构建:
$ make
做 sani check 时 的构建:
$ make SAN=1
-
相关阅读:
第1章丨IRIS Globals 简介
奥巴马竞选演讲及相关视频下载(用迅雷可以下)
MyCat2的介绍与安装以及基本使用
使用VS编译Redis源码报错
消息队列 - RabbitMQ
【无标题】
力扣大厂热门面试算法题 12-14
基于Python实现的词汇相似度计算
统一系统脆弱性管理平台:让“网络安全漏洞”无处遁形
Codeforces Round #786 (Div. 3) 补题记录
-
原文地址:https://blog.csdn.net/eloudy/article/details/138037327