[cfe-dev] lli and exceptions

Stammerjohann, Kai via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 16 08:16:21 PDT 2016


Hi,

I have a bunch of cpp files that I compile with clang to bitcode:

/data/kais/clang/bin_gcc-6.2.0/bin/clang-3.9 -O0 -fexceptions -fcxx-exceptions -std=c++11 -I/data/kais/clang/test/src/runtime -I/data/kais/clang/test/src/scripts -emit-llvm -c /data/kais/clang/test/src/runtime/*.cpp /data/kais/clang/test/src/scripts/*.cpp

Then, I use lli to run this testfunction (test.test.cpp):

void notify(int i); // implemented in different compilationunit: notify.cpp / notify.bc

struct Obj
{
    Obj()
    {
        notify(0);
    }

    ~Obj()
    {
        notify(1);
    }
};

int test_func_123()
{
    try
    {
        Obj obj;
        notify(4);
        throw 4;        // this exception is not supposed to leave jitcode
    }
    catch(int i)        // because its caught here
    {
        notify(2);
    }

    notify(3);
    return 0;
}

extern "C" void test_func()
{
    test_func_123();
}

This is the notify func (notify.bc):

void notify(int i)
{
    printf("%d\n", i);
}

So even though there is lots of code generated, only very little is used by the test func.

The expected output is:
0
4
1
2
3

But this happens when i run it with lli:

/data/kais/clang/bin_gcc-6.2.0/bin/lli -entry-function=test_func -extra-module=notify.bc -extra-module=exception.bc -extra-module=map.bc -extra-module=memory.bc -extra-module=string.bc -extra-module=thread.bc -extra-module=type_id.bc -extra-module=value.bc test.test.bc
0
4
terminate called after throwing an instance of 'int'
#0 0x00000000016713ca llvm::sys::PrintStackTrace(llvm::raw_ostream&) /data/kais/clang/src/lib/Support/Unix/Signals.inc:402:0
#1 0x0000000001671792 PrintStackTraceSignalHandler(void*) /data/kais/clang/src/lib/Support/Unix/Signals.inc:470:0
#2 0x000000000166f8aa llvm::sys::RunSignalHandlers() /data/kais/clang/src/lib/Support/Signals.cpp:44:0
#3 0x0000000001670c7c SignalHandler(int) /data/kais/clang/src/lib/Support/Unix/Signals.inc:246:0
#4 0x00007fe800950850 __restore_rt (/lib64/libpthread.so.0+0xf850)
#5 0x00007fe7ff573875 __GI_raise (/lib64/libc.so.6+0x32875)
#6 0x00007fe7ff574e51 __GI_abort (/lib64/libc.so.6+0x33e51)
#7 0x00007fe7ffde5f6d __gnu_cxx::__verbose_terminate_handler() /data/kais/gcc-6.2.0/build/x86_64-pc-linux-gnu/libstdc++-v3/libsupc++/../../../../src/libstdc++-v3/libsupc++/vterminate.cc:95:0
#8 0x00007fe7ffde3f26 __cxxabiv1::__terminate(void (*)()) /data/kais/gcc-6.2.0/build/x86_64-pc-linux-gnu/libstdc++-v3/libsupc++/../../../../src/libstdc++-v3/libsupc++/eh_terminate.cc:51:0
#9 0x00007fe7ffde3f71 (/data/kais/gcc-6.2.0/bin/lib64/libstdc++.so.6+0x96f71)
#10 0x00007fe7ffde4188 (/data/kais/gcc-6.2.0/bin/lib64/libstdc++.so.6+0x97188)
#11 0x00007fe800d70b3c
Stack dump:
0.      Program arguments: /data/kais/clang/bin_gcc-6.2.0/bin/lli -entry-function=test_func -extra-module=notify.bc -extra-module=exception.bc -extra-module=map.bc -extra-module=memory.bc -extra-module=string.bc -extra-module=thread.bc -extra-module=type_id.bc -extra-module=value.bc test.test.bc
Aborted

Does the error mean it doesnt find a matching catch-handler?

Moving around the source of test_func() in its cpp file seems to have impact on whether it runs or crashs.

Adding seemingly unrelated option (-time-passes) also makes it work:
/data/kais/clang/bin_gcc-6.2.0/bin/lli -entry-function=test_func -extra-module=notify.bc -extra-module=exception.bc -extra-module=map.bc -extra-module=memory.bc -extra-module=string.bc -extra-module=thread.bc -extra-module=type_id.bc -extra-module=value.bc -time-passes test.test.bc
0
4
1
2
3
===-------------------------------------------------------------------------===
                      ... Pass execution timing report ...
===-------------------------------------------------------------------------===
  Total Execution Time: 470.4054 seconds (471.9124 wall clock)

   ---User Time---   --System Time--   --User+System--   ---Wall Time---  --- Name ---
  137.0326 ( 29.2%)   0.1880 ( 20.1%)  137.2206 ( 29.2%)  137.4367 ( 29.1%)  X86 DAG->DAG Instruction Selection
  35.2502 (  7.5%)   0.0400 (  4.3%)  35.2902 (  7.5%)  35.3602 (  7.5%)  Greedy Register Allocator
...

Running lli through valgrind (yes, i'm that desperate) also makes it work (valgrind doesnt find anything suspicous):
/data/kais/valgrind-3.11.0/bin/bin/valgrind --error-limit=no  -v --num-callers=25 --log-file=valgrind.out -- /data/kais/clang/bin_gcc-6.2.0/bin/lli -entry-function=test_func -extra-module=notify.bc -extra-module=exception.bc -extra-module=map.bc -extra-module=memory.bc -extra-module=string.bc -extra-module=thread.bc -extra-module=type_id.bc -extra-module=value.bc test.test.bc
0
4
1
2
3

Running it multiple times seems to always produce the same outcome (either it 100% runs, or 100% crashs).

I'm using a debug version of llvm/clang 3.9 on linux, built with gcc 6.2:

/data/kais/clang/bin_gcc-6.2.0/bin/lli --version
LLVM (http://llvm.org/):
  LLVM version 3.9.0
  DEBUG build with assertions.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: ivybridge

Building it with or without LLVM_ENABLE_EH doesnt make a difference.

Are exceptions thrown in jit, that don't leave JIT, supposed to work with lli on linux?

Thanks



More information about the cfe-dev mailing list