<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Throwing C++ exception after getting address of JITted function causes segfault."
   href="https://bugs.llvm.org/show_bug.cgi?id=34424">34424</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Throwing C++ exception after getting address of JITted function causes segfault.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>4.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>samuelmarinelli@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19078" name="attach_19078" title="Source file reproducing the bug.">attachment 19078</a> <a href="attachment.cgi?id=19078&action=edit" title="Source file reproducing the bug.">[details]</a></span>
Source file reproducing the bug.

I have a simple JIT implementation that JITs a function, gets its address,
calls it, and removes it from the JIT.  After doing this, I find that any throw
of a C++ exception, caught or uncaught, causes a segfault.

The attached source file is a minimal example.  In fact calling the JITted
function is unnecessary to produce the error; all that's necessary is to get
its address by calling llvm::JITSymbol::getAddress on the symbol produced by
llvm::orc::IRCompileLayer<llvm::orc::ObjectLinkingLayer<>>::findSymbol.

Removing the call to getAddress in line 88 produces a non-segfaulting
executable, as does removing the call to
IRCompileLayer<ObjectLinkingLayer<>>::removeModuleSet in line 90.

I compile the code using the following command.

    g++ -g main.cc `llvm-config --cxxflags --ldflags --system-libs --libs \
    core mcjit native` -O0 -o example -std=c++17 -Wall -Wextra            \
    -Wno-unused-function -Wno-unused-value -Wno-unused-parameter -Werror

I'm using LLVM 4.0.1 and GCC 7.1.1 on Arch Linux.  I explicitly turned on
exception handling in LLVM during the build using -DLLVM_ENABLE_EH=ON, thinking
that the default --fno-exceptions flag passed to the compiler might to blame,
but no such luck.

Here's the backtrace from GDB.

Program received signal SIGSEGV, Segmentation fault.
classify_object_over_fdes (ob=0x101437c00,
this_fde=this_fde@entry=0x7fe5b0413000) at
/build/gcc/src/gcc/libgcc/unwind-dw2-fde.c:641
641     /build/gcc/src/gcc/libgcc/unwind-dw2-fde.c: No such file or directory.
(gdb) bt
#0  classify_object_over_fdes (ob=0x101437c00,
this_fde=this_fde@entry=0x7fe5b0413000) at
/build/gcc/src/gcc/libgcc/unwind-dw2-fde.c:641
#1  0x00007fe5aeeab7d8 in init_object (ob=0x101437c00) at
/build/gcc/src/gcc/libgcc/unwind-dw2-fde.c:777
#2  search_object (ob=ob@entry=0x101437c00, pc=pc@entry=0x7fe5aeeaa17d
<_Unwind_RaiseException+61>) at /build/gcc/src/gcc/libgcc/unwind-dw2-fde.c:989
#3  0x00007fe5aeeac666 in _Unwind_Find_registered_FDE (bases=0x7fffffffddc8,
pc=0x7fe5aeeaa17d <_Unwind_RaiseException+61>)
    at /build/gcc/src/gcc/libgcc/unwind-dw2-fde.c:1066
#4  _Unwind_Find_FDE (pc=0x7fe5aeeaa17d <_Unwind_RaiseException+61>,
bases=bases@entry=0x7fffffffddc8) at
/build/gcc/src/gcc/libgcc/unwind-dw2-fde-dip.c:458
#5  0x00007fe5aeea8a73 in uw_frame_state_for
(context=context@entry=0x7fffffffdd20, fs=fs@entry=0x7fffffffdb70) at
/build/gcc/src/gcc/libgcc/unwind-dw2.c:1249
#6  0x00007fe5aeea9ce0 in uw_init_context_1
(context=context@entry=0x7fffffffdd20,
outer_cfa=outer_cfa@entry=0x7fffffffe0d0, outer_ra=
    0x7fe5af452ec6 <__cxxabiv1::__cxa_throw(void*, std::type_info*, void
(*)(void*))+54>) at /build/gcc/src/gcc/libgcc/unwind-dw2.c:1578
#7  0x00007fe5aeeaa17e in _Unwind_RaiseException (exc=exc@entry=0x101426b10) at
/build/gcc/src/gcc/libgcc/unwind.inc:88
#8  0x00007fe5af452ec6 in __cxxabiv1::__cxa_throw (obj=<optimized out>,
tinfo=0x7fe5af73ea90 <typeinfo for std::runtime_error>, 
    dest=0x7fe5af468500 <std::runtime_error::~runtime_error()>) at
/build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:88
#9  0x000000010012fa5e in main () at main.cc:94</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>