google / sanitizers

AddressSanitizer, ThreadSanitizer, MemorySanitizer
Other
11.56k stars 1.04k forks source link

Asan will not report small out-of-bound of array #1803

Closed lijh8 closed 1 month ago

lijh8 commented 1 month ago

Asan will not report index out-of-bound of small offset in builtin array. Only if i increase the index to a rather big out-of-bound offset, it will report this.

What offset value should Asan report out-of-bound on?

Thanks!

Example 1

$ 
$ cat src/main/main.cpp                                                                                 
#include <iostream>

int main() {
  int a[3] = {1, 2, 3};
  int i = std::size(a);// + sizeof(char*) * 100;
  std::cout << "i: " << i << "\n";
  a[i] = 0;
  return 0;
}
$ 

$ make -C src/main && ./src/main/main
c++ -Wall -Warray-bounds -std=c++2a -g  -MMD -MP   -c -o main.o main.cpp
c++  -fsanitize=address   main.o   -o main
i: 3
zsh: abort      ./src/main/main
$ 

Example 2

$ 
$ cat src/main/main.cpp              
#include <iostream>

int main() {
  int a[3] = {1, 2, 3};
  int i = std::size(a) + sizeof(char*) * 100;
  std::cout << "i: " << i << "\n";
  a[i] = 0;
  return 0;
}
$ 

$ make -C src/main && ./src/main/main
c++ -Wall -Warray-bounds -std=c++2a -g  -MMD -MP   -c -o main.o main.cpp
c++  -fsanitize=address   main.o   -o main
i: 803
AddressSanitizer:DEADLYSIGNAL
=================================================================
==37299==ERROR: AddressSanitizer: stack-overflow on address 0x7ff7b50453a8 (pc 0x00010aebe254 bp 0x7ff7b5044730 sp 0x7ff7b5044710 T0)
    #0 0x10aebe254 in main main.cpp:10
    #1 0x10b71952d in start+0x1cd (dyld:x86_64+0x552d)

SUMMARY: AddressSanitizer: stack-overflow main.cpp:10 in main
==37299==ABORTING
zsh: abort      ./src/main/main
$ 
$ uname -a
Darwin ljhs-Mac-mini.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Jun 24 00:56:10 PDT 2024; root:xnu-8020.240.18.709.2~1/RELEASE_X86_64 x86_64
$ 

$ c++ --version  
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ 
Enna1 commented 1 month ago

You need to compile and link your program with asan. In your case, you just linked your program with asan, but not compiled with asan. You should do:

c++ -fsanitize=address -Wall -Warray-bounds -std=c++2a -g  -MMD -MP   -c -o main.o main.cpp
c++ -fsanitize=address   main.o   -o main
lijh8 commented 1 month ago

@Enna1 , thanks a lot!

lijh8 commented 1 week ago

@Enna1 , hi can you help confirm on this #1818, thanks