This is an archive of the discontinued LLVM Phabricator instance.

Fix tls_get_addr handling for glibc >=2.25
ClosedPublic

Authored by thurston on Apr 3 2023, 11:10 AM.

Details

Summary

This changes the sanitizers' tls_get_addr handling from
a heuristic check of __signal_safe_memalign allocations
(which has only been used in a since deprecated version
of Google's runtime), to using the sanitizers' interface
function to check if it is a malloc allocation (used
since glibc >= 2.25).

This is one of the approaches proposed by Keno in
https://github.com/google/sanitizers/issues/1409#issuecomment-1214244142

This moves the weak annotation of __sanitizer_get_allocated_size/begin from the header to sanitizer_tls_get_addr.cpp, as suggested by Vitaly in D148060.

Diff Detail

Event Timeline

thurston created this revision.Apr 3 2023, 11:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 3 2023, 11:10 AM
Herald added a subscriber: Enna1. · View Herald Transcript
thurston requested review of this revision.Apr 3 2023, 11:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 3 2023, 11:10 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
thurston edited the summary of this revision. (Show Details)Apr 3 2023, 11:21 AM
vitalybuka added inline comments.Apr 3 2023, 1:33 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26
compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
17
143–147
147

don't use explicit nullptr checks

thurston added inline comments.Apr 3 2023, 1:57 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26

I get a linker error if they're not weak:

$ LIT_FILTER=get_allocated_begin ninja check-sanitizer
...
/usr/bin/ld: projects/compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.i386.dir/sanitizer_tls_get_addr.cpp.o: in function `__sanitizer::DTLS_on_tls_get_addr(void*, void*, unsigned int, unsigned int)':
/usr/local/google/home/thurston/llvm-projectB/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:130: undefined reference to `__sanitizer_get_allocated_begin'
/usr/bin/ld: /usr/local/google/home/thurston/llvm-projectB/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:133: undefined reference to `__sanitizer_get_allocated_size'
collect2: error: ld returned 1 exit status
thurston updated this revision to Diff 510604.Apr 3 2023, 2:06 PM

Avoid explicit nullptr check, per Vitaly's suggestion

vitalybuka accepted this revision.Apr 3 2023, 2:06 PM
vitalybuka added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26

we need to understand why and fix it

This revision is now accepted and ready to land.Apr 3 2023, 2:06 PM
thurston marked 3 inline comments as done.Apr 3 2023, 2:06 PM
vitalybuka requested changes to this revision.Apr 3 2023, 2:07 PM
This revision now requires changes to proceed.Apr 3 2023, 2:07 PM
vitalybuka added inline comments.Apr 3 2023, 2:08 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26

Is this for UBSAN case?

thurston added inline comments.Apr 3 2023, 3:25 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26

Is this for UBSAN case?

Yes, and also Nolibc:

Generating Sanitizer-x86_64-Test-Nolibc
...
/usr/bin/ld: libRTSanitizerCommon.test.nolibc.x86_64.a(sanitizer_tls_get_addr.cpp.o): in function `__sanitizer::DTLS_on_tls_get_addr(void*, void*, unsigned long, unsigned long)':
/usr/local/google/home/thurston/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:129: undefined reference to `__sanitizer_get_allocated_begin'
/usr/bin/ld: /usr/local/google/home/thurston/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:131: undefined reference to `__sanitizer_get_allocated_size'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Linking CXX shared library lib/clang/17/lib/i386-unknown-linux-gnu/libclang_rt.ubsan_standalone.so
...
/usr/bin/ld: projects/compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.i386.dir/sanitizer_tls_get_addr.cpp.o: in function `__sanitizer::DTLS_on_tls_get_addr(void*, void*, unsigned int, unsigned int)':
/usr/local/google/home/thurston/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:129: undefined reference to `__sanitizer_get_allocated_begin'
/usr/bin/ld: /usr/local/google/home/thurston/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp:131: undefined reference to `__sanitizer_get_allocated_size'
collect2: error: ld returned 1 exit status
Linking CXX shared library lib/clang/17/lib/x86_64-unknown-linux-gnu/libclang_rt.ubsan_standalone.so
...
thurston updated this revision to Diff 510648.Apr 3 2023, 4:57 PM

Remove XFAIL from compiler-rt/test/msan/dtls_test.c because it now passes.

vitalybuka added inline comments.Apr 3 2023, 5:21 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h
23–26

Is this for UBSAN case?

Yes, and also Nolibc:

sanitizer_tls_get_addr is very glibc specific, it should not be in nolibc

thurston updated this revision to Diff 512897.Apr 12 2023, 10:42 AM

This moves the weak annotation of __sanitizer_get_allocated_size/begin from the header to sanitizer_tls_get_addr.cpp, as suggested by Vitaly in D148060.

thurston edited the summary of this revision. (Show Details)Apr 12 2023, 10:42 AM
thurston marked 4 inline comments as done.
vitalybuka accepted this revision.Apr 12 2023, 10:45 AM
This revision is now accepted and ready to land.Apr 12 2023, 10:45 AM
thurston updated this revision to Diff 512899.Apr 12 2023, 10:46 AM

Retain const qualifer

This revision was automatically updated to reflect the committed changes.

Mondos did not show any regressions on google3 TAP targets (other than regular flakiness)