This is an archive of the discontinued LLVM Phabricator instance.

[sanitizers] Add init function to set alignment of low level allocator
ClosedPublic

Authored by waltl on Oct 31 2017, 1:59 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

waltl created this revision.Oct 31 2017, 1:59 PM
kosarev added a subscriber: kosarev.Nov 1 2017, 1:38 AM
vitalybuka added inline comments.Nov 15 2017, 2:41 PM
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
185 ↗(On Diff #121052)

Why can't this be just a shadow granularity?

waltl added inline comments.Nov 16 2017, 8:29 AM
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
185 ↗(On Diff #121052)

It can, but I was not clear on how to get that value --
SHADOW_GRANULARITY is defined in an asan header and I don't see any sanitizer_common sources including any asan headers. And I'm not sure we wanted to move that to a sanitizer header.

vitalybuka edited edge metadata.Nov 16 2017, 8:38 AM

maybe we need to set this to low level allocator during initialization?
@alekseyshl WDYT?

alekseyshl edited edge metadata.Nov 16 2017, 3:25 PM

Why do we need this at all for internal allocator? It's internal to sanitizer and sanitizer code is not instrumented, that means no shadow.

waltl added a comment.Nov 16 2017, 4:53 PM

Why do we need this at all for internal allocator? It's internal to sanitizer and sanitizer code is not instrumented, that means no shadow.

Here is a typical backtrace. It seems that asan installs a callback to the internal allocator that poisons those memory.

#0 0xf7fd7c89 in kernel_vsyscall ()
#1 0xf7e3c627 in syscall () from /lib/i386-linux-gnu/libc.so.6
#2 0x0811c3cc in
sanitizer::internal_sched_yield() () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc:401
#3 0x08062c75 in LockSlow () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/../sanitizer_common/sanitizer_mutex.h:55
#4 0x08129696 in Lock () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h:32
#5 GenericScopedLock () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h:187
#6 GetOrInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc:21
#7 0x08128147 in Print () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc:35
#8 0x0810f31f in AsanCheckFailed () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:69
#9 0x08124884 in CheckFailed () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_termination.cc:79
#10 0x08108b26 in PoisonShadow () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_poisoning.cc:38
#11 0x0810f355 in __asan::OnLowLevelAllocate(unsigned long, unsigned long) () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:89
#12 0x0811328b in Allocate () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc:193
#13 0x0812b49a in operator new () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_common.h:937
#14 ChooseExternalSymbolizer () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:486
#15 ChooseSymbolizerTools () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:516
#16 PlatformInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:529
#17 0x081296aa in GetOrInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc:24
#18 0x0812b635 in LateInitialize () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:534
#19 0x0810ee62 in AsanInitInternal () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:479
#20 0xf7fe8de4 in ?? () from /lib/ld-linux.so.2
#21 0xf7fd9a5f in ?? () from /lib/ld-linux.so.2

Why do we need this at all for internal allocator? It's internal to sanitizer and sanitizer code is not instrumented, that means no shadow.

So if it's just internal allocator memory usage increase should not be significant?

Why do we need this at all for internal allocator? It's internal to sanitizer and sanitizer code is not instrumented, that means no shadow.

Here is a typical backtrace. It seems that asan installs a callback to the internal allocator that poisons those memory.

#0 0xf7fd7c89 in kernel_vsyscall ()
#1 0xf7e3c627 in syscall () from /lib/i386-linux-gnu/libc.so.6
#2 0x0811c3cc in
sanitizer::internal_sched_yield() () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc:401
#3 0x08062c75 in LockSlow () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/../sanitizer_common/sanitizer_mutex.h:55
#4 0x08129696 in Lock () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h:32
#5 GenericScopedLock () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h:187
#6 GetOrInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc:21
#7 0x08128147 in Print () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_libcdep.cc:35
#8 0x0810f31f in AsanCheckFailed () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:69
#9 0x08124884 in CheckFailed () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_termination.cc:79
#10 0x08108b26 in PoisonShadow () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_poisoning.cc:38
#11 0x0810f355 in __asan::OnLowLevelAllocate(unsigned long, unsigned long) () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:89
#12 0x0811328b in Allocate () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc:193
#13 0x0812b49a in operator new () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_common.h:937
#14 ChooseExternalSymbolizer () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:486
#15 ChooseSymbolizerTools () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:516
#16 PlatformInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:529
#17 0x081296aa in GetOrInit () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc:24
#18 0x0812b635 in LateInitialize () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc:534
#19 0x0810ee62 in AsanInitInternal () at /usr/local/google/home/waltl/Work/llvm-scale5/scale5/compiler-rt/lib/asan/asan_rtl.cc:479
#20 0xf7fe8de4 in ?? () from /lib/ld-linux.so.2
#21 0xf7fd9a5f in ?? () from /lib/ld-linux.so.2

Ah, right, thanks!

Ok then, we need SetLowLevelAllocatorMinAlignment to complement SetLowLevelAllocateCallback, default it to 8 and call it from ASan to set it to shadow granularity.

waltl updated this revision to Diff 123586.Nov 20 2017, 7:04 AM

Add SetLowLevelAllocateMinAlignment

waltl retitled this revision from [sanitizers] Increase alignment of low level allocator to [sanitizers] Add init function to set alignment of low level allocator.Nov 20 2017, 7:06 AM
waltl edited the summary of this revision. (Show Details)

Ok then, we need SetLowLevelAllocatorMinAlignment to complement SetLowLevelAllocateCallback, default it to 8 and call it from ASan to set it to shadow granularity.

Done.

alekseyshl added inline comments.Nov 20 2017, 10:53 AM
compiler-rt/lib/asan/asan_rtl.cc
411 ↗(On Diff #123586)

Swap the Set.. calls, set the min alignment first.

compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
210 ↗(On Diff #123586)

Did you really mean that alignment can only go up? How about this:

constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
...
low_level_alloc_min_alignment = Max(alignment, kLowLevelAllocatorDefaultAlignment);

waltl updated this revision to Diff 123633.Nov 20 2017, 12:31 PM
waltl edited the summary of this revision. (Show Details)

Address CR comments

waltl marked 2 inline comments as done.Nov 20 2017, 12:39 PM
waltl added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
210 ↗(On Diff #123586)

Done. Not sure if the final kLowLevelAllocatorDefaultAlignment is a typo, but I changed it to low_level_alloc_min_alignment so that if SetLowLevelAllocateMinAlignment is called multiple times, we get a max of all the input alignments.

alekseyshl accepted this revision.Nov 20 2017, 3:32 PM
This revision is now accepted and ready to land.Nov 20 2017, 3:32 PM
This revision was automatically updated to reflect the committed changes.
waltl marked an inline comment as done.