Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes when app throws an exception #17

Closed
aguludunu opened this issue May 29, 2019 · 2 comments
Closed

Crashes when app throws an exception #17

aguludunu opened this issue May 29, 2019 · 2 comments

Comments

@aguludunu
Copy link

aguludunu commented May 29, 2019

My app can run with libmemory_profiler.so very well but it crashes when it throws an exception.

GCC : 7.4.0
Compile : g++ -Wall -Wextra -g -ggdb -O0 test.cpp -pthread
Run : sudo LD_PRELOAD=./libmemory_profiler.so ./a.out
Code : (I don't know how to make the code pretty, it looks suck.)

#include <unistd.h>
#include <stdio.h>
#include <sys/syscall.h>

#include   <array>
#include   <vector>
#include   <thread>
#include   <random>

using namespace std;

// Init random generator
mt19937 rand_generator(chrono::high_resolution_clock::now().time_since_epoch().count());
uniform_int_distribution<int> dist(100,1000);

class A {
public: 
    A() {   
        data_.push_back(0);
        data_.push_back(1);
        data_.push_back(2);
    }
private:
    vector<int> data_;
};
// Set a global value and it may alloca memory before main()
A a;

void ThreadEntry() {
    while(true) {   
        char*p = new char[128];
        *p += 1;
        
        try {
            if(0 == dist(rand_generator) % 2) {
                //throw "Throw a exception";   //crashes when it runs
                delete [] p;
            }
        } catch (const char* msg) {
            printf("catch exception [%s]\n", msg);
        }
        
        this_thread::sleep_for(chrono::milliseconds(10));
    }
}

int main() {

    array<thread, 1> thread_pool;
    
    for(auto &th : thread_pool)
        th = thread(ThreadEntry);
    
    for(auto &th : thread_pool)
        th.join();
}

Backtrace:

(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff6cca801 in __GI_abort () at abort.c:79
#2  0x00007ffff753e957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7544ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7544af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7544d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00005555555551c9 in ThreadEntry () at test.cpp:42
#7  0x00005555555563b3 in std::__invoke_impl<void, void (*)()> (__f=@0x55555576e098: 0x55555555514a <ThreadEntry()>) at /usr/include/c++/7/bits/invoke.h:60
#8  0x0000555555555dd1 in std::__invoke<void (*)()> (__fn=@0x55555576e098: 0x55555555514a <ThreadEntry()>) at /usr/include/c++/7/bits/invoke.h:95
#9  0x000055555555764a in std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul> (this=0x55555576e098) at /usr/include/c++/7/thread:234
#10 0x0000555555557606 in std::thread::_Invoker<std::tuple<void (*)()> >::operator() (this=0x55555576e098) at /usr/include/c++/7/thread:243
#11 0x00005555555575d6 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run (this=0x55555576e090) at /usr/include/c++/7/thread:186
#12 0x00007ffff756f66f in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007ffff70826db in start_thread (arg=0x7fffeffff700) at pthread_create.c:463
#14 0x00007ffff6dab88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
@koute
Copy link
Owner

koute commented May 29, 2019

Thanks for the report!

Hmm... interesting. It looks like the newer versions of libstdc++ have _Unwind_RaiseException statically linked... This is problematic. I'll have to think about how to work around this.

In the meantime you can turn off the shadow stack-based unwinding by doing this:

export MEMORY_PROFILER_USE_SHADOW_STACK=0

and then run the profiler. It will be slower, but it should work until I'll fix this properly.

@koute
Copy link
Owner

koute commented Jun 6, 2019

Should be fixed on master.

@koute koute closed this as completed Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants