Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Program crashes when throwing exception under memory sanitizer. #30850

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR31877
Status NEW
Importance P normal
Reported by Andrey Khalyavin (halyavin@chromium.org)
Reported on 2017-02-06 08:59:36 -0800
Last modified on 2018-06-14 14:51:48 -0700
Version 4.0
Hardware PC Linux
CC alekseyshlgit@gmail.com, eugeni.stepanov@gmail.com, kcc@google.com, llvm-bugs@lists.llvm.org, mclow.lists@gmail.com, vitalybuka@google.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
The following program crashes with memory sanitizer.

test.cpp:

#include <stdio.h>
#include <exception>

// Poison parameters shadow.
__attribute__ ((noinline)) void func(int, int, int, int, int, int) {
    try {
        throw std::exception();
    } catch (std::exception& e) {
        printf("here\n");
    }
}

int main() {
    int a, b, c, d, e, f;
    func(a, b, c, d, e, f);
}

Compilation:
1. Compile and copy memory-sanitized libc++.so and libc++abi.so to the same
folder as test.cpp.
2. clang++ test.cpp -o test -stdlib=libc++ -L. -Wl,-rpath=. -fsanitize=memory

Output:
./test
==3802==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x7f51243b3313 in get_thrown_object_ptr /home/halyavin/other/llvm/projects/libcxxabi/src/cxa_personality.cpp:490:27
    #1 0x7f51243b3313 in __cxxabiv1::scan_eh_tab(__cxxabiv1::(anonymous namespace)::scan_results&, _Unwind_Action, bool, _Unwind_Exception*, _Unwind_Context*) /home/halyavin/other/llvm/projects/libcxxabi/src/cxa_personality.cpp:736
    #2 0x7f51243b14e5 in __gxx_personality_v0 /home/halyavin/other/llvm/projects/libcxxabi/src/cxa_personality.cpp:951:9
    #3 0x7f5124995262  (/lib/x86_64-linux-gnu/libgcc_s.so.1+0x10262)
    #4 0x7f51243aff6d in __cxa_throw /home/halyavin/other/llvm/projects/libcxxabi/src/cxa_exception.cpp:238:5
    #5 0x489534 in func(int, int, int, int, int, int) (/home/halyavin/other/msan-exceptions/test+0x489534)
    #6 0x4897f4 in main (/home/halyavin/other/msan-exceptions/test+0x4897f4)
    #7 0x7f51245dc82f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #8 0x419c88 in _start (/home/halyavin/other/msan-exceptions/test+0x419c88)

The problem is that libgcc_s.so is not memory sanitized and so it does not
update parameters shadow before calling __gxx_personality_v0 function.
Quuxplusone commented 7 years ago

An interceptor for __cxa_throw might fix this.

Quuxplusone commented 7 years ago

One approach is to unpoison all parameters of __gxx_personality_v0. I don't see any crashes once I have done this for libcxxrt library we use for exception handling.