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

ASAN on ARM fails on fast unwind

Open rengolin opened this issue 11 years ago • 10 comments

Bugzilla Link 22741
Version unspecified
OS Linux
Attachments stack traces: stage1, stage2, no-fast-unwind
CC @eugenis,@AnnaZaks,@kcc,@rengolin,@vitalybuka

Extended Description

When walking the stack, ASAN fails to get the proper frames on ARM. On stage1 (GCC compiled ASAN), the stack unwinds with the right size (three), but the addresses are bogus. On stage2 (Clang's), the stack doesn't even have the right size and all you can see is the current frame.

When using ASAN_OPTIONS=fast_unwind_on_malloc=0, the stack is correctly unwounded (correct.out attached).

rengolin avatar Feb 28 '15 20:02 rengolin

Do we still need to do anything about it?

vitalybuka avatar Jun 09 '18 02:06 vitalybuka

Yes this is my understanding as well. The asan tests that rely on correct stacktrace pass with a compiler-rt built with clang, but also fails if -mthumb is used.

Also the fast stacktrace code for ARM will fail if you mix thumb and arm code even compiler-rt is built with clang (for instance if you call a function in thum mode that call a function in arm). Also it will fail if you mix clang and GCC compiled code.

Based on this I do not think the fast stacktrace code should be enabled for ARM, it has very limited used currently.

zatrazz avatar Dec 03 '15 23:12 zatrazz

To be clear, the problem is specific to Thumb, right? From my investigation in bug 18505, thumb frame pointers in both clang and gcc are generated in a way that can not be used for stack unwinding - basically, there is no way to step to the next (previous) frame w/o knowing the size of the current frame.

eugenis avatar Dec 03 '15 20:12 eugenis

As stated in bug 18505 the problem is the difference on how the __builtin_frame_address is handle on GCC and CLANG.

Using the test/asan/TestCases/debug_stacks.cc as reference, the stacktrace is first hit on the malloc interceptor:

(gdb) bt
#​0  __interceptor_malloc () at /.../projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52
#​1  0x000ecb80 in func1() () at /.../projects/compiler-rt/test/asan/TestCases/debug_stacks.cc:13
#​2  0x000ecd24 in main () at /.../projects/compiler-rt/test/asan/TestCases/debug_stacks.cc:24

The interceptor code does:

51      INTERCEPTOR(void*, malloc, uptr size) {
52        GET_STACK_TRACE_MALLOC;
53        return asan_malloc(size, &stack);
54      }

#define GET_STACK_TRACE(max_size, fast)                                        \
[...]
    GetStackTraceWithPcBpAndContext(&stack, max_size,                          \
                                    StackTrace::GetCurrentPc(),                \
                                    GET_CURRENT_FRAME(), 0, fast);             \

And finally:

# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)

So basically the current frame address is obtained and passed along the function which will walk on it and get the backtrace information (basically the PC addresses).

The problems is for ARM the frame address (also as stated on Bug 18505) returned is different for GCC and CLANG. For GCC:

Dump of assembler code for function __interceptor_malloc(__sanitizer::uptr):
=> 0x000995b8 <+0>:     stmdb   sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}
   0x000995bc <+4>:     subw    sp, sp, #&#8203;1060   ; 0x424
   0x000995c0 <+8>:     add     r7, sp, #&#8203;16

And the 'r7' value will be propagated through code until it is used to call GetStackTraceWithPcBpAndContext

In the other hand, for clang:

Dump of assembler code for function __interceptor_malloc():

   0x000c0474 <+0>:     push    {r4, r5, r6, r7, r8, r9, r11, lr}
   0x000c0478 <+4>:     add     r11, sp, #&#8203;24
   0x000c047c <+8>:     sub     sp, sp, #&#8203;40     ; 0x28
   0x000c0480 <+12>:    sub     sp, sp, #&#8203;1024   ; 0x400
   [...]

And the 'r11' value will be propagated through code until to be used for the 'bp' argument in GetStackTraceWithPcBpAndContext.

So for clang the frame address used is without the stack adjustment done by the function. This allow the backtrace walk done in 'lib/sanitizer_common/sanitizer_stacktrace.cc' (the same test work if compiler-rt is built with clang).

The compile-rt function GetCanonicFrame still try to make some frame adjustments based on the BP value, but it works only if the function itself does not have any stack allocation. And I see no easy way to get the current value in the GCC case since there is no easy to the stack allocation size used in the calling function to get the correct frame address.

Any ideas?

zatrazz avatar Dec 03 '15 14:12 zatrazz

Not yet, but from last comment at Bug 18505 [1] my understanding is compiler is generating thumb frame pointers in incorrect way. I will check if there is a way to get some information on compiler-rt.

[1] https://llvm.org/bugs/show_bug.cgi?id=18505

zatrazz avatar Dec 01 '15 14:12 zatrazz

Hi Kostya,

I ran it again, and both with and without fast_unwind the results are similar.

Adhemerval, did you have a look at this?

cheers, --renato

rengolin avatar Dec 01 '15 12:12 rengolin

Is this still a problem?

kcc avatar Dec 01 '15 02:12 kcc

See also bug 18505

kcc avatar Mar 02 '15 23:03 kcc

You can reproduce this by re-enabling the test:

test/asan/TestCases/debug_stacks.cc

by removing the line:

// REQUIRES: stable-runtime

or by manually running it:

$ clang \
    --driver-mode=g++ \
    -fsanitize=address \
    -mno-omit-leaf-frame-pointer \
    -fno-omit-frame-pointer \
    -fno-optimize-sibling-calls \
    -gline-tables-only \
    -O0 debug_stacks.cc
$ ./a.out

rengolin avatar Mar 02 '15 12:03 rengolin

assigned to @zatrazz

rengolin avatar Feb 28 '15 20:02 rengolin