llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[ASAN] LLVM 17 does not detect ODR violations previously found using ASAN

Open justincady opened this issue 3 years ago • 5 comments

Using ASAN with LLVM 17 fails to detect ODR violations that are detected with LLVM 16 and earlier.

I bisected to this commit: https://github.com/llvm/llvm-project/commit/a8d3ae712290d6f85db2deb9164181058f5c1307

I'm not sure if this is intended behavior (which would surprise me as the symbol is used) or an unintended consequence. Here's a reproducer:

// shared.c
int in_shared(void) { return 1; }
int (*fptr)(void) = in_shared;
// static.c
int in_static(void) { return 2; }
int (*fptr)(void) = in_static;
// main.c
extern int in_shared(void);
extern int in_static(void);
extern int (*fptr)(void);
int main() { return fptr() + in_static() + in_shared(); }
#!/bin/bash

CLANG=/path/to/clang
$CLANG --version | head -1 | cut -d " " -f1-3
$CLANG -fsanitize=address -fPIC -shared shared.c -o shared.so
$CLANG -fsanitize=address -c static.c
ar rs static.a static.o
$CLANG -fuse-ld=lld -fsanitize=address -Wl,-rpath,'$ORIGIN' shared.so static.a main.c
./a.out

Running it with LLVM 16:

$ ./build.sh
clang version 16.0.6
=================================================================
==28316==ERROR: AddressSanitizer: odr-violation (0x55e177c12b40):
  [1] size=8 'fptr' static.c
  [2] size=8 'fptr' shared.c
These globals were registered at these points:
  [1]:
    #0 0x55e177b4b217 in __asan_register_globals /[...]/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:356:3
    #1 0x55e177c0d33b in asan.module_ctor static.c

  [2]:
    #0 0x55e177b4b217 in __asan_register_globals /[...]/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:356:3
    #1 0x7fcfa36daf6b in asan.module_ctor shared.c

==28316==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
SUMMARY: AddressSanitizer: odr-violation: global 'fptr' at static.c
==28316==ABORTING
$

Running it with LLVM 17:

$ ./build.sh
clang version 17.0.1
$

Flipping the default back by applying -fno-sanitize-address-globals-dead-stripping brings back the error with LLVM 17:

$ ./build.sh
clang version 17.0.1
=================================================================
==28382==ERROR: AddressSanitizer: odr-violation (0x557d7e463e20):
  [1] size=8 'fptr' static.c
  [2] size=8 'fptr' shared.c
[...]

But the default behavior appears to hide ODR violations, which seems like a bug to me.

cc @MaskRay

justincady avatar Sep 28 '23 13:09 justincady

/cherry-pick 1a4b9b6f73391d94f4f96cc964cbf89cfdd59b5b

Would be nice to have this in 17.0.2

MaskRay avatar Sep 29 '23 17:09 MaskRay

/branch llvm/llvm-project-release-prs/issue67677

llvmbot avatar Sep 29 '23 18:09 llvmbot

/pull-request llvm/llvm-project-release-prs#714

llvmbot avatar Sep 29 '23 18:09 llvmbot

https://github.com/llvm/llvm-project/commit/1a4b9b6f73391d94f4f96cc964cbf89cfdd59b5b had an issue and was reverted with a reland.

/cherry-pick 16eed8c906875e748c3cb610f3dc4b875f3882aa

MaskRay avatar Sep 29 '23 22:09 MaskRay

/branch llvm/llvm-project-release-prs/issue67677

llvmbot avatar Sep 29 '23 22:09 llvmbot