【发布时间】:2018-05-17 22:28:48
【问题描述】:
我做了什么。
测试1
1 #include <stdio.h>
2
3 int test[16];
4
5 int main()
6 {
7 test[17] = -1;
8 }
/tmp $ gcc ./main.c -o main -fsanitize=address
/tmp $ ./main
/tmp $
测试2
1 #include <stdio.h>
2
3 int test[16] = {1};
4
5 int main()
6 {
7 test[17] = -1;
8 }
/tmp $ gcc ./main.c -o main -fsanitize=address
/tmp $ ./main
=================================================================
==19776==ERROR: AddressSanitizer: global-buffer-overflow on address
...
看起来全局缓冲区溢出检测不适用于放置在 bss 中的全局变量(是这样吗?)。这背后的原因是什么?
更新:
存储的代码未优化。 系统信息:
$ gcc --version
gcc (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
【问题讨论】:
-
你试过
clang吗? -
我不确定分配是否会生成任何代码。
-
@EugeneSh。不生成代码是可以的(因为它无论如何都是 UB,即使没有,没有
volatile也没有可观察到的行为),但是当这取决于变量是否被初始化时,我会感到惊讶...... -
@Jean-François Fabre 我没试过 clang,我现在只对 gcc 感兴趣。
-
godbolt of the program seems to show a call to __asan_report_store4 适用于 gcc 和 clang ...嗯
标签: c memory-management buffer-overflow address-sanitizer