This is an archive of the discontinued LLVM Phabricator instance.

[ubsan] Fix print_stacktrace=1:fast_unwind_on_fatal=0 to correctly fallback to fast unwinder
ClosedPublic

Authored by MaskRay on Apr 11 2022, 7:11 PM.

Details

Summary

ubsan_GetStackTrace (from 52b751088b11547e0f4ef0589ebbe5e57752c68c) called by
~ScopeReport leaves top/bottom zeroes in the
!WillUseFastUnwind(request_fast_unwind) code path.
When BufferedStackTrace::Unwind falls back to UnwindFast,
if (stack_top < 4096) return; will return early, leaving just one frame in the stack trace.

Fix this by always initializing top/bottom like 261d6e05d5574bec753ea6b7e9a7f99229927753.

Diff Detail

Event Timeline

MaskRay created this revision.Apr 11 2022, 7:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 11 2022, 7:11 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
MaskRay requested review of this revision.Apr 11 2022, 7:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 11 2022, 7:11 PM
yln accepted this revision.Apr 12 2022, 10:55 AM

Thank you for continuing to improve this code! I see my TODOs from 3 years ago when I tried and failed.

This revision is now accepted and ready to land.Apr 12 2022, 10:55 AM

I suspect this may have regressed:

Failed Tests (2):

UBSan-AddressSanitizer-arm :: TestCases/Misc/Linux/diag-stacktrace.cpp
UBSan-Standalone-arm :: TestCases/Misc/Linux/diag-stacktrace.cpp

https://lab.llvm.org/buildbot/#/builders/77/builds/16407

PTAL

MaskRay added a comment.EditedApr 12 2022, 1:02 PM

I suspect this may have regressed:

Failed Tests (2):

UBSan-AddressSanitizer-arm :: TestCases/Misc/Linux/diag-stacktrace.cpp
UBSan-Standalone-arm :: TestCases/Misc/Linux/diag-stacktrace.cpp

https://lab.llvm.org/buildbot/#/builders/77/builds/16407

PTAL

Thanks for the report. I added UNSUPPORTED: android in 9f526057d649fe6d1e7cc7a52706aab5a3e34575.

There are very few print_stacktrace=1 tests, but the new test is the first testing several stack frames. Before that we did not have tests verifying the stack trace quality.
From the log it seems that the fast unwinder (frame chain based) does not work (the frames for g, h, and main are skipped):

// RUN: %clangxx -fsanitize=return %gmlt -O2 -fno-omit-frame-pointer -fno-exceptions -fno-asynchronous-unwind-tables %s -o %t
// RUN: %env_ubsan_opts=print_stacktrace=1:fast_unwind_on_fatal=0 not %run %t 2>&1 | FileCheck %s

Hope the information can be useful for someone who is interested in making it work on Android, or perhaps just arm-linux-android

           1: /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:13:31: runtime error: execution reached the end of a value-returning function without returning a value 
           2:  #0 0x56e08d4 in f() /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:13:35 
next:15'0                                                                                                                                                                                    X~~~ error: no match found
next:15'1                                                                                                                                                                                         with "@LINE+1" equal to "16"
           3:  #1 0xf2f1fcec in ubsan_GetStackTrace /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/lib/ubsan/ubsan_diag.cpp:41:10 
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
next:15'2                                                                                                                                                 ?                             possible intended match
           4:  #2 0xf2f1fcec in MaybePrintStackTrace /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/lib/ubsan/ubsan_diag.cpp:51:3 
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           5:  #3 0xf2f1fcec in __ubsan::ScopedReport::~ScopedReport() /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/lib/ubsan/ubsan_diag.cpp:387:3 
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           6:  #4 0xf2f21e44 in handleMissingReturnImpl(__ubsan::UnreachableData*, __ubsan::ReportOptions) /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/lib/ubsan/ubsan_handlers.cpp:428:1 
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           7:  #5 0xf2f21da8 in __ubsan_handle_missing_return /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/lib/ubsan/ubsan_handlers.cpp:432:3 
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           8:  
next:15'0     ~
           9: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:13:31 in  
next:15'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>
vitalybuka accepted this revision.Apr 12 2022, 1:21 PM
pirama added a subscriber: pirama.Apr 12 2022, 2:04 PM
vitalybuka added inline comments.Apr 12 2022, 11:02 PM
compiler-rt/lib/ubsan/ubsan_diag.cpp
37–41
MaskRay closed this revision.Apr 13 2022, 12:33 AM
MaskRay marked an inline comment as done.

Phab did not auto close Differential for some time yesterday. Closed by fdd424e37abf3fa411c07f1e08fee72cfe7bb25b

compiler-rt/lib/ubsan/ubsan_diag.cpp
37–41

Thanks! Simplified the arguments in 63f2d1f4d4b8ee284b4ab977242e322a9458a168

MaskRay added inline comments.Apr 13 2022, 8:31 PM
compiler-rt/lib/ubsan/ubsan_diag.cpp
37–41

Seems that stack->Unwind(pc, bp, context, request_fast); cannot be called => this Unwind overload calls UnwindImpl which isn't defined in scudo.

mgorny added a subscriber: mgorny.May 5 2022, 8:57 AM

This one's failing for me on Gentoo. Apparently, the test executable outputs:

# env UBSAN_OPTIONS=print_stacktrace=1:fast_unwind_on_fatal=1 not /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt_build/test/ubsan/Standalone-i386/TestCases/Misc/Linux/Output/diag-stacktrace.cpp.tmp
/var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:31: runtime error: execution reached the end of a value-returning function without returning a value
LLVMSymbolizer: error reading file: No such file or directory
    #0 0x565edeee in f() /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:35
    #1 0x565edf05 in g() /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:17:38
    #2 0x565edf25 in h() /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:20:38
    #3 0x565edf45 in main /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:23:14
    #4 0xf7a694a4 in __libc_start_call_main /tmp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #5 0xff8daf7a  ([stack]+0x20f7a)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /var/tmp/portage/sys-libs/compiler-rt-sanitizers-15.0.0.9999/work/compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:31 in

but FileCheck doesn't expect this line:

LLVMSymbolizer: error reading file: No such file or directory

From a quick strace, it seems that it's trying to open… [stack]?

[pid 350704] openat(AT_FDCWD, "[stack]", O_RDONLY|O_CLOEXEC <unfinished ...>
[pid 350703] read(3,  <unfinished ...>
[pid 350704] <... openat resumed>)      = -1 ENOENT (No such file or directory)
[pid 350704] write(2, "LLVMSymbolizer: error reading fi"..., 36LLVMSymbolizer: error reading file: ) = 36
[pid 350704] write(2, "No such file or directory", 25No such file or directory) = 25
[pid 350704] write(2, "\n", 1
)          = 1
mgorny added a comment.May 5 2022, 9:01 AM

Ah, sorry, now I see that a few other tests are also affected by this, so I guess something else has changed. Probably glibc upgrade once again :-(.