<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>FYI: this is the third person I know of who has run into problems
getting a bugzilla account created. This needs addressed ASAP.</p>
<p>Philip<br>
</p>
<div class="moz-forward-container"><br>
<br>
-------- Forwarded Message --------
<table class="moz-email-headers-table" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">Subject:
</th>
<td>Re: [llvm-dev] Possible stack corruption during call to
JITSymbol::getAddress()</td>
</tr>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">Date: </th>
<td>Thu, 20 Apr 2017 09:42:39 -0700</td>
</tr>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">From: </th>
<td>David Lurton via llvm-dev
<a class="moz-txt-link-rfc2396E" href="mailto:llvm-dev@lists.llvm.org"><llvm-dev@lists.llvm.org></a></td>
</tr>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">Reply-To:
</th>
<td>David Lurton <a class="moz-txt-link-rfc2396E" href="mailto:dlurton@gmail.com"><dlurton@gmail.com></a></td>
</tr>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">To: </th>
<td>Lang Hames <a class="moz-txt-link-rfc2396E" href="mailto:lhames@gmail.com"><lhames@gmail.com></a></td>
</tr>
<tr>
<th align="RIGHT" valign="BASELINE" nowrap="nowrap">CC: </th>
<td>LLVM Developers Mailing List
<a class="moz-txt-link-rfc2396E" href="mailto:llvm-dev@lists.llvm.org"><llvm-dev@lists.llvm.org></a></td>
</tr>
</tbody>
</table>
<br>
<br>
<div dir="ltr">Well, 3 days later and so far nobody has responded
to my request for an account on <a moz-do-not-send="true"
href="http://bugs.llvm.org">bugs.llvm.org</a>.. so it doesn't
look like I'm going to be able to create that bug on my own,
unfortunately.</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Wed, Apr 19, 2017 at 6:27 PM, Lang
Hames <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi David,
<div><br>
</div>
<div>Thanks very much for that. I'll continue to dig in as
time permits, and I'll update the bug report with my
progress once it's filed.</div>
<div><br>
</div>
<div>Cheers,</div>
<div>Lang.</div>
</div>
<div class="HOEnZb">
<div class="h5">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Mon, Apr 17, 2017 at 6:42
PM, David Lurton <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:dlurton@gmail.com" target="_blank">dlurton@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Thanks Lang. I think I'll go the
bug creation route. I have an email out to
llvm-admin requesting an account on <a
moz-do-not-send="true"
href="http://bugs.llvm.org" target="_blank">bugs.llvm.org</a>.
I'll let you know when I've filed the bug.</div>
<div class="m_-3769178140563680007HOEnZb">
<div class="m_-3769178140563680007h5">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Sun, Apr 16,
2017 at 9:44 PM, Lang Hames <span
dir="ltr"><<a moz-do-not-send="true"
href="mailto:lhames@gmail.com"
target="_blank">lhames@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px
#ccc solid;padding-left:1ex">
<div dir="ltr">Hi David,
<div><br>
</div>
<div>This looks like bad eh-frame data
due to a failure to fix up the frame
descriptor entries:</div>
<div><br>
</div>
<div><font face="monospace, monospace"><debug:
adding frame> EHFrameAddr:
0x7feae5827000, EHFrameLoadAddr:
0x00000000e5827000, EHFrameSize:
60<br>
</font></div>
<div>
<div><font face="monospace,
monospace">==64588==ERROR:
AddressSanitizer: SEGV on
unknown address 0x7feae5827020
(pc 0x7feae886d970 bp
0x000000000001 sp 0x7ffca10e75f8
T0)</font></div>
</div>
<div><br>
</div>
<div>Eyeballing the code in
RuntimeDyldELF (vs RuntimeDyldMachO,
which is doing the right thing) I
see it lacks the necessary fixups.
If you're feeling game you can try
to port RuntimeDyldMachO's solution
to RuntimeDyldELF (where MachO uses
a template argument, you'll need to
switch over the RuntimeDyldImpl Arch
member to determine the pointer size
for the fixup). Otherwise you should
file a bug on <a
moz-do-not-send="true"
href="http://bugs.llvm.org"
target="_blank">bugs.llvm.org</a>
and CC me, and then I can CC some of
the ELF devs and see if anyone has
time.</div>
<div><br>
</div>
<div>In the mean time, turning off
exception support should fix this,
though I'm not sure whether that's a
viable option for your use case.</div>
<div><br>
</div>
<div>Cheers,<br>
</div>
<div>Lang.</div>
</div>
<div
class="m_-3769178140563680007m_7081830580728777428HOEnZb">
<div
class="m_-3769178140563680007m_7081830580728777428h5">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Thu,
Apr 13, 2017 at 10:53 AM, Lang
Hames <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:lhames@gmail.com"
target="_blank">lhames@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="margin:0 0 0
.8ex;border-left:1px #ccc
solid;padding-left:1ex">
<div dir="ltr">Hi David,
<div><br>
</div>
<div>This is definitely the
right place to ask.</div>
<div><br>
</div>
<div>Let me see if I can
reproduce this locally...</div>
<div><br>
</div>
<div>Cheers,</div>
<div>Lang.</div>
<div><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">
<div>
<div
class="m_-3769178140563680007m_7081830580728777428m_8577966870667922979h5">On
Sun, Apr 9, 2017 at
2:02 PM, David Lurton
via llvm-dev <span
dir="ltr"><<a
moz-do-not-send="true"
href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span>
wrote:<br>
</div>
</div>
<blockquote
class="gmail_quote"
style="margin:0 0 0
.8ex;border-left:1px
#ccc
solid;padding-left:1ex">
<div>
<div
class="m_-3769178140563680007m_7081830580728777428m_8577966870667922979h5">
<div dir="ltr">Firstly,
apologies if this
is not the right
place to be asking
this
question--feel
free to point me
in the correct
direction. I
could be doing
something wrong
here but
stackoverflow
didn't feel like
the correct place
for this since
there's so little
there about LLVM
ORC.
<div><br>
</div>
<div>
<div>
<div>Basically,
I have a
reproduction
case (below)
where if I
throw an
exception
before I call
JITSymbol::getAddress() everything works properly but throwing the same
exception
afterward will
result in a
SIGSEGV during
stack
unwinding.
This suggests
to me that
somehow the
stack is
getting
corrupted
during the
JITSymbol::getAddress()
call. </div>
</div>
</div>
<div><br>
</div>
<div>This problem
was initially
discovered while
working on my
own project.
While
troubleshooting
this I've
discvoered that
when LLVM is<span
style="font-family:monospace,monospace">-DLLVM_USE_SANITIZER:STRING=<wbr>Address </span><font
face="arial,
helvetica,
sans-serif">the
problem
happens at
different
points during
execution,
perhaps having
something to
do with the
padding around
the stack
variables
added by the
sanitizer?
See the note
after the call
to runTest()
in main().</font></div>
<div><br>
</div>
<div>I'm running
this under an
up-to-date
Antergos Linux,
clang version:
3.9.1 (tried
compiling LLVM
and the example
program below
with gcc 6.3.1
and the result
is the same)
clang set to
default compiler
by setting the
following
environment
variables:</div>
<div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
CC=/usr/bin/clang</font></div>
<div><font
face="monospace,
monospace">
CXX=/usr/bin/clang++</font></div>
<div><br>
</div>
<div>Commands
used to build
LLVM:</div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
git clone <a
moz-do-not-send="true" href="https://github.com/llvm-mirror/llvm.git"
target="_blank">https://github.com/llvm-mirror<wbr>/llvm.git</a></font></div>
<div><font
face="monospace,
monospace">
git checkout
release_40</font></div>
<div><font
face="monospace,
monospace">
cd llvm</font></div>
<div><font
face="monospace,
monospace">
mkdir build</font></div>
<div><font
face="monospace,
monospace">
cd build</font></div>
<div><font
face="monospace,
monospace">
cmake ..
-DLLVM_BUILD_LLVM_DYLIB:BOOL=O<wbr>N
-DLLVM_ENABLE_RTTI:BOOL=ON -DLLVM_ENABLE_EH:BOOL=ON
-DLLVM_USE_SANITIZER:STRING=Ad<wbr>dress
-DLLVM_PARALLEL_COMPILE_JOBS:S<wbr>TRING=8
-DLLVM_ENABLE_ASSERTIONS:BOOL=<wbr>ON</font></div>
<div><font
face="monospace,
monospace">
cmake --build
. -- -j 8</font></div>
<div><font
face="monospace,
monospace">
sudo cmake
--build .
--target
install</font></div>
<div><br>
</div>
<div>Command
used to build
test case
executable:</div>
<div><br>
</div>
<div> clang
test.cpp
-std=c++14
-lstdc++
-lLLVM-4.0
-Wall
-pedantic
-Wextra
-fstack-protector-all
-fsanitize=address -fexceptions</div>
<div><br>
</div>
<div>Then of
course:</div>
<div><br>
</div>
<div><font
face="monospace,
monospace">
./a.out</font></div>
</div>
<div><br>
</div>
<div>Output from
the a.out:</div>
<div><br>
</div>
<div>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div>
<div><font
face="monospace,
monospace">ASAN:DEADLYSIGNAL</font></div>
<div><font
face="monospace,
monospace">==============================<wbr>==============================<wbr>=====</font></div>
<div><font
face="monospace,
monospace">==6582==ERROR:
AddressSanitizer: SEGV on unknown address 0x7f59eeb06020 (pc
0x7f59f1b20930
bp
0x000000000001
sp
0x7ffc5e546218
T0)</font></div>
<div><font
face="monospace,
monospace">==6582==The
signal is
caused by a
READ memory
access.</font></div>
</div>
</blockquote>
</div>
<div>
<div><font
face="arial,
helvetica,
sans-serif"><br>
</font></div>
<div><font
face="arial,
helvetica,
sans-serif">The
result if
running
`backtrace` in
GDB while
execution is
paused after
the SIGSEGV
occurs:</font></div>
<div><font
face="arial,
helvetica,
sans-serif"><br>
</font></div>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div><font
face="monospace,
monospace">#0
read_encoded_value_with_base (encoding=encoding@entry=28 '\034',
base=base@entry=0,
p=p@entry=0x7fffe8a06020 <error: Cannot access memory at address
0x7fffe8a06020>,
val=val@entry=0x7fffffffd6d8) at /build/gcc/src/gcc/libgcc/unwi<wbr>nd-pe.h:252</font></div>
<div><font
face="monospace,
monospace">#1
0x00007fffeba05a61 in binary_search_single_encoding_<wbr>fdes
(pc=0x7fffeba04426
<_Unwind_Resume+54>, ob=0x0) at /build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2-fde.c:908</font></div>
<div><font
face="monospace,
monospace">#2
search_object
(ob=ob@entry=0x60400001d9d0, pc=pc@entry=0x7fffeba04426
<_Unwind_Resume+54>)
at
/build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2-fde.c:977</font></div>
<div><font
face="monospace,
monospace">#3
0x00007fffeba05fdd in _Unwind_Find_registered_FDE
(bases=0x7fffffffda78,
pc=0x7fffeba04426 <_Unwind_Resume+54>) at
/build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2-fde.c:1013</font></div>
<div><font
face="monospace,
monospace">#4
_Unwind_Find_FDE (pc=0x7fffeba04426 <_Unwind_Resume+54>,
bases=bases@entry=0x7fffffffda<wbr>78)
at
/build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2-fde-dip.c:454</font></div>
<div><font
face="monospace,
monospace">#5
0x00007fffeba02b23 in uw_frame_state_for (context=context@entry=0x7ffff<wbr>fffd9d0,
fs=fs@entry=0x7fffffffd820) at /build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2.c:1241</font></div>
<div><font
face="monospace,
monospace">#6
0x00007fffeba03d40 in uw_init_context_1 (context=context@entry=0x7ffff<wbr>fffd9d0,
outer_cfa=outer_cfa@entry=0x7f<wbr>ffffffdc00, outer_ra=0x5110fc) at
/build/gcc/src/gcc/libgcc/unwi<wbr>nd-dw2.c:1562</font></div>
<div><font
face="monospace,
monospace">#7
0x00007fffeba04427 in _Unwind_Resume (exc=0x60d00000c7b0) at
/build/gcc/src/gcc/libgcc/unwi<wbr>nd.inc:224</font></div>
<div><font
face="monospace,
monospace">#8
0x00000000005110fc in runTest () at /home/dave/projects/untitled/t<wbr>est.cpp:124</font></div>
<div><font
face="monospace,
monospace">#9
0x0000000000511138 in main (argc=1, argv=0x7fffffffe698) at
/home/dave/projects/untitled/t<wbr>est.cpp:132</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
</blockquote>
</div>
<div>My test-case
is below. In
runTest(), note
the commented
out throw
statement before
symbol.getAddress() and the uncommented one after it. Also note the
comments after
the call to
runTest() in
main().<br>
</div>
<div><br>
</div>
<div>Thanks.</div>
<div>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div><br>
</div>
<div><font
face="monospace,
monospace">#include
"llvm/ADT/STLExtras.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/Executio<wbr>nEngine.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/IR/IRBuilder.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/SectionM<wbr>emoryManager.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/Orc/Comp<wbr>ileUtils.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/Orc/IRCo<wbr>mpileLayer.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/Orc/Lamb<wbr>daResolver.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/ExecutionEngine/Orc/Obje<wbr>ctLinkingLayer.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/IR/Mangler.h"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/Support/DynamicLibrary.h<wbr>"</font></div>
<div><font
face="monospace,
monospace">#include
"llvm/Support/TargetSelect.h"</font></div>
<div><font
face="monospace,
monospace">#include
<iostream></font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">using
namespace
llvm;</font></div>
<div><font
face="monospace,
monospace">using
namespace
llvm::orc;</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">/**
This class
taken verbatim
from</font></div>
<div><font
face="monospace,
monospace"> *
<a
moz-do-not-send="true"
href="https://github.com/llvm-mirror/llvm/blob/release_40/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h"
target="_blank">https://github.com/llvm-mirror<wbr>/llvm/blob/release_40/examples<wbr>/Kaleidoscope/BuildingAJIT/Cha<wbr>pter1/KaleidoscopeJIT.h</a></font></div>
<div><font
face="monospace,
monospace"> *
This is from
the same
revision of
LLVM I am
using (the
release_40
branch as of
4/8/2017)</font></div>
<div><font
face="monospace,
monospace"> */</font></div>
<div><font
face="monospace,
monospace">class
KaleidoscopeJIT {</font></div>
<div><font
face="monospace,
monospace">private:</font></div>
<div><font
face="monospace,
monospace">
std::unique_ptr<TargetMachine> TM;</font></div>
<div><font
face="monospace,
monospace">
const
DataLayout DL;</font></div>
<div><font
face="monospace,
monospace">
ObjectLinkingLayer<> ObjectLayer;</font></div>
<div><font
face="monospace,
monospace">
IRCompileLayer<decltype(Object<wbr>Layer)> CompileLayer;</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">public:</font></div>
<div><font
face="monospace,
monospace">
typedef
decltype(CompileLayer)::Module<wbr>SetHandleT
ModuleHandle;</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
KaleidoscopeJIT()</font></div>
<div><font
face="monospace,
monospace">
:
TM(EngineBuilder().selectTarge<wbr>t()),
DL(TM->createDataLayout()),</font></div>
<div><font
face="monospace,
monospace">
CompileLayer(ObjectLayer,
SimpleCompiler(*TM)) {</font></div>
<div><font
face="monospace,
monospace">
llvm::sys::DynamicLibrary::Loa<wbr>dLibraryPermanently(nullptr);</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
TargetMachine
&getTargetMachine() { return *TM; }</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
ModuleHandle
addModule(std::unique_ptr<Modu<wbr>le>
M) {</font></div>
<div><font
face="monospace,
monospace">
// Build
our symbol
resolver:</font></div>
<div><font
face="monospace,
monospace">
// Lambda
1: Look back
into the JIT
itself to find
symbols that
are part of</font></div>
<div><font
face="monospace,
monospace">
//
the same
"logical
dylib".</font></div>
<div><font
face="monospace,
monospace">
// Lambda
2: Search for
external
symbols in the
host process.</font></div>
<div><font
face="monospace,
monospace">
auto
Resolver =
createLambdaResolver(</font></div>
<div><font
face="monospace,
monospace">
[&](const
std::string
&Name) {</font></div>
<div><font
face="monospace,
monospace">
if (auto Sym
=
CompileLayer.findSymbol(Name,
false))</font></div>
<div><font
face="monospace,
monospace">
return
Sym;</font></div>
<div><font
face="monospace,
monospace">
return
JITSymbol(nullptr);</font></div>
<div><font
face="monospace,
monospace">
},</font></div>
<div><font
face="monospace,
monospace">
[](const
std::string
&Name) {</font></div>
<div><font
face="monospace,
monospace">
if (auto
SymAddr =</font></div>
<div><font
face="monospace,
monospace">
RTDyldMemoryManager::getSymbol<wbr>AddressInProcess(Name))</font></div>
<div><font
face="monospace,
monospace">
return
JITSymbol(SymAddr,
JITSymbolFlags::Exported);</font></div>
<div><font
face="monospace,
monospace">
return
JITSymbol(nullptr);</font></div>
<div><font
face="monospace,
monospace">
});</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
// Build a
singleton
module set to
hold our
module.</font></div>
<div><font
face="monospace,
monospace">
std::vector<std::unique_ptr<Mo<wbr>dule>>
Ms;</font></div>
<div><font
face="monospace,
monospace">
Ms.push_back(std::move(M));</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
// Add the
set to the JIT
with the
resolver we
created above
and a newly</font></div>
<div><font
face="monospace,
monospace">
// created
SectionMemoryManager.</font></div>
<div><font
face="monospace,
monospace">
return
CompileLayer.addModuleSet(std:<wbr>:move(Ms),</font></div>
<div><font
face="monospace,
monospace">
make_unique<SectionMemoryMana<wbr>ger>(),</font></div>
<div><font
face="monospace,
monospace">
std::move(Resolver));</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
JITSymbol
findSymbol(const
std::string
Name) {</font></div>
<div><font
face="monospace,
monospace">
std::string
MangledName;</font></div>
<div><font
face="monospace,
monospace">
raw_string_ostream
MangledNameStream(MangledName)<wbr>;</font></div>
<div><font
face="monospace,
monospace">
Mangler::getNameWithPrefix(Man<wbr>gledNameStream,
Name, DL);</font></div>
<div><font
face="monospace,
monospace">
return
CompileLayer.findSymbol(Mangle<wbr>dNameStream.str(),
true);</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
void
removeModule(ModuleHandle
H) {</font></div>
<div><font
face="monospace,
monospace">
CompileLayer.removeModuleSet(H<wbr>);</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace">};</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">const
std::string
FUNC_NAME =
"someFunction";</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">void
runTest() {</font></div>
<div><font
face="monospace,
monospace">
llvm::LLVMContext context;</font></div>
<div><font
face="monospace,
monospace">
llvm::IRBuilder<> irBuilder{context};</font></div>
<div><font
face="monospace,
monospace">
KaleidoscopeJIT jit;</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
auto module =
std::make_unique<llvm::Module><wbr>("help", context);</font></div>
<div><font
face="monospace,
monospace">
module->setDataLayout(jit.getT<wbr>argetMachine().createDataLayou<wbr>t());</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
auto function
=
llvm::cast<llvm::Function>(mod<wbr>ule->getOrInsertFunction(FUNC_<wbr>NAME,</font></div>
<div><font
face="monospace,
monospace">
llvm::Type::getInt32Ty(contex<wbr>t), nullptr));</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
auto block =
llvm::BasicBlock::Create(conte<wbr>xt,
"functionBody", function);</font></div>
<div><font
face="monospace,
monospace">
irBuilder.SetInsertPoint(block<wbr>);</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
irBuilder.CreateRet(llvm::Cons<wbr>tantInt::get(context, llvm::APInt(32,
1, true)));</font></div>
<div><font
face="monospace,
monospace">
jit.addModule(std::move(module<wbr>));</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
llvm::JITSymbol symbol = jit.findSymbol(FUNC_NAME);</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
//Just to
ensure that
the symbol is
in fact valid
(symbol
evaluates to
true during
execution)</font></div>
<div><font
face="monospace,
monospace">
if(!symbol) {</font></div>
<div><font
face="monospace,
monospace">
throw
std::runtime_error("Symbol
not found");</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
//when
uncommented,
the throw
statement does
NOT cause a
SIGSEGV.</font></div>
<div><font
face="monospace,
monospace">
//throw
std::runtime_error("This
should not
crash.");</font></div>
<div><font
face="monospace,
monospace">
uint64_t ptr =
symbol.getAddress();</font></div>
<div><font
face="monospace,
monospace">
//HOWEVER... a
SIGSEGV occurs
during
stack-unwinding
while throwing
the exception
below.</font></div>
<div><font
face="monospace,
monospace">
//Hence, the
call to
symbol.getAddress()
must be
causing some
kind of memory
corruption.</font></div>
<div><font
face="monospace,
monospace">
//My guess is
that it's
clobbering the
stack.</font></div>
<div><font
face="monospace,
monospace">
throw
std::runtime_error("This
should not
crash but does
anyway.");</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
std::cout
<< "Ptr
is " <<
ptr <<
"\n";</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
int
(*someFuncPtr)()
=
reinterpret_cast<int
(*)()>(ptr);</font></div>
<div><font
face="monospace,
monospace">
//int
(*someFuncPtr)()
= (int
(*)())ptr;</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
int
returnValue =
someFuncPtr();</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
std::cout
<<
"Return value
is: " <<
returnValue
<< "\n";</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">int
main(int argc,
char **argv) {</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
llvm::InitializeNativeTarget()<wbr>;</font></div>
<div><font
face="monospace,
monospace">
llvm::InitializeAllAsmPrinters<wbr>();</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
try {</font></div>
<div><font
face="monospace,
monospace">
runTest();</font></div>
</blockquote>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div><font
face="monospace,
monospace">
//NOTE:
if LLVM is
compiled
without </font><span
style="font-family:monospace,monospace">-DLLVM_USE_SANITIZER:S<wbr>TRING=Address,
the last throw
in runTest()
does not cause</span></div>
</blockquote>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div><font
face="monospace,
monospace">
//a
SIGSEGV,
however this
throw will.</font></div>
</blockquote>
<blockquote
style="margin:0px
0px 0px
40px;border:none;padding:0px">
<div>
<div><font
face="monospace,
monospace">
//throw
std::runtime_error("This
should not
crash but does
anyway.");</font></div>
</div>
<div><span
style="font-family:monospace,monospace">
}
catch(std::runtime_error
&e) {</span><br>
</div>
<div><font
face="monospace,
monospace">
std::cout
<<
"Exception
caught: "
<<
e.what()
<< "\n";</font></div>
<div><font
face="monospace,
monospace">
}</font></div>
<div><font
face="monospace,
monospace"><br>
</font></div>
<div><font
face="monospace,
monospace">
llvm::llvm_shutdown();</font></div>
<div><font
face="monospace,
monospace">
return 0;</font></div>
<div><font
face="monospace,
monospace">}<br>
<br>
<br>
</font></div>
</blockquote>
</div>
<div><font
face="arial,
helvetica,
sans-serif"><br>
</font></div>
</div>
<br>
</div>
</div>
______________________________<wbr>_________________<br>
LLVM Developers mailing
list<br>
<a
moz-do-not-send="true"
href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a
moz-do-not-send="true"
href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev"
rel="noreferrer"
target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</body>
</html>