This is an archive of the discontinued LLVM Phabricator instance.

[NFC][compiler-rt][hwasan] Move allocation functions into their own file
ClosedPublic

Authored by leonardchan on Jun 2 2021, 3:33 PM.

Details

Summary

This removes the __sanitizer_* allocation function definitions from hwasan_interceptors.cpp and moves them into their own file. This way implementations that do not use interceptors at all can just ignore (almost) everything in hwasan_interceptors.cpp.

Also remove some unused headers in hwasan_interceptors.cpp after the move.

Diff Detail

Event Timeline

leonardchan created this revision.Jun 2 2021, 3:33 PM
leonardchan requested review of this revision.Jun 2 2021, 3:33 PM
Herald added a subscriber: Restricted Project. · View Herald TranscriptJun 2 2021, 3:33 PM

s/vfork/fork/ in the description

I'm afraid removing the fork interceptor will break Android. This is a pretty uncommon condition, but still. It is interesting to note that ASan does not have this fork protection, see https://github.com/google/sanitizers/issues/774 for some history.

I wonder if this could be replaced with pthread_atfork. The order of callback execution seems right, as long as hwasan is registered first. Or, if you want to make progress, add SANITIZER_ANDROID ifdef somewhere.

I think probably the better refactoring here would be to split this file in two: one containing just the allocation functions; and a second with everything else.
On Fuchsia, we'll use the allocation functions but none of the rest of it. So the second whole file could just be under #if !SANITIZER_FUCHSIA.

I think probably the better refactoring here would be to split this file in two: one containing just the allocation functions; and a second with everything else.
On Fuchsia, we'll use the allocation functions but none of the rest of it. So the second whole file could just be under #if !SANITIZER_FUCHSIA.

I don't think we can move the __sanitizer_* allocation functions since they're used for aliases for their libc equivalents which require definitions in the same TU:

/usr/local/google/home/leonardchan/llvm-monorepo/llvm-project-4/compiler-rt/lib/hwasan/hwasan_interceptors.cpp:59:1: error: alias must point to a defined variable or function
INTERCEPTOR_ALIAS(void *, aligned_alloc, SIZE_T alignment, SIZE_T size);
^
/usr/local/google/home/leonardchan/llvm-monorepo/llvm-project-4/compiler-rt/lib/hwasan/hwasan_interceptors.cpp:53:7: note: expanded from macro 'INTERCEPTOR_ALIAS'
      ALIAS("__sanitizer_" #FN);                                             \
      ^

Perhaps it might just be easier to just wrap the part of hwasan_interceptors.cpp that don't include the allocation functions with #if !SANITIZER_FUCHSIA, or as @eugenis suggested wrap the fork interceptor with #if SANITIZER_ANDROID.

leonardchan retitled this revision from [NFC][compiler-rt][hwasan] Wrap vfork interceptor with HWASAN_WITH_INTERCEPTORS to [NFC][compiler-rt][hwasan] Wrap fork interceptor with HWASAN_WITH_INTERCEPTORS.Jun 3 2021, 3:34 PM
leonardchan edited the summary of this revision. (Show Details)

I don't think we can move the __sanitizer_* allocation functions since they're used for aliases for their libc equivalents which require definitions in the same TU:

What I suggested is moving *everything* related to the allocation functions into a separate file.

leonardchan retitled this revision from [NFC][compiler-rt][hwasan] Wrap fork interceptor with HWASAN_WITH_INTERCEPTORS to [NFC][compiler-rt][hwasan] Move allocation functions into their own file.
leonardchan edited the summary of this revision. (Show Details)

Followed up with Roland's suggestion and moved everything relating to the allocation functions.

leonardchan edited the summary of this revision. (Show Details)Jun 7 2021, 3:34 PM
mcgrathr added inline comments.Jun 7 2021, 10:15 PM
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
171

blank lines around #else and #endif lines. But here I think you just omit this. We can define the empty function in a fuchsia-specific file.

vitalybuka accepted this revision.Jun 8 2021, 10:26 AM
vitalybuka added inline comments.
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
171

+1 to "empty function in a fuchsia-specific file"

This revision is now accepted and ready to land.Jun 8 2021, 10:26 AM
leonardchan marked 2 inline comments as done.
leonardchan added inline comments.
compiler-rt/lib/hwasan/hwasan_interceptors.cpp
171

Will add in an eventual hwasan_fuchsia.cpp.

This revision was landed with ongoing or failed builds.Jun 8 2021, 12:09 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Jun 8 2021, 12:30 PM

this breaks tests: http://45.33.8.238/linux/48401/step_10.txt

// CHECK: #0 {{.*}} in {{.*}}free{{.*}} {{.*}}hwasan_interceptors.cpp

vs

13: #0 0x5647a467e9a0 in free ../../compiler-rt/lib/hwasan/hwasan_allocation_functions.cpp:75:3

need to update the path in that test

this breaks tests: http://45.33.8.238/linux/48401/step_10.txt

// CHECK: #0 {{.*}} in {{.*}}free{{.*}} {{.*}}hwasan_interceptors.cpp

vs

13: #0 0x5647a467e9a0 in free ../../compiler-rt/lib/hwasan/hwasan_allocation_functions.cpp:75:3

need to update the path in that test

Fixed in a9ea0a6a77b30305bfbe1b06c30bf6136f64c1ad.