I'm running into a SIGSEGV when calling a JITed function. I'm following along with the JIT demo. I had a theory that this was because I dropped JIT engine, but keeping it alive did not change a thing.
Function.display() looks like this, which does not seem suspicious to me:
function u0:0(f64) -> f64 system_v {
block0(v0: f64):
v1 = f64const 0.0
return v0
}
@Ivan Chinenov that CLIF code looks correct; unfortunately it's probably hard to say more without digging in with a debugger
It would help to know whether the crash happens in the JIT'd code or somewhere else; also it might be helpful to try to instruction-step (si
in gdb) into the call from your code to the JIT function pointer and see what is happening
@Chris Fallin I have tried stepping into JITed function with LLDB, but LLDB errored out with "Can't read instruction at this address". Next "step into" and I end up in signal handler.
OK, that indicates then that the function pointer is probably invalid
if you post your code (a minimal reproduction of the problem) in a gist or something of the sort, someone here may be able to see something wrong; there's very little information here to help diagnose otherwise
Here is my jit code https://github.com/JohnDowson/CraneLisp/blob/main/src/jit.rs it is coincidentally just enough code as needed to try calling a JITed function
ok, thanks; possibly someone who has embedded the JIT API directly can see if there's something suspicious? (cc @bjorn3 maybe?)
in the meantime I'd encourage more fine-grained debugging; what is the actual function pointer value, where did it come from, where was that memory allocated, was it valid at one time then freed or not (memory watchpoints are maybe helpful here), etc
/me is looking...
One problem I see is that at https://github.com/JohnDowson/CraneLisp/blob/60846e2d4b773fa39792264011c13da00589c083/src/eval.rs#L95 you need to use extern "C" fn
and not plain fn
. The later will use the rust abi, which is unstable.
ah! at that line, could ptr
be a &*const u8? (auto-borrowing behavior of the match
on self
)
Update: after a closer look with a debugger, I realized what was wrong.
so the transmute is turning a poitner into the pointer into the code pointer
I was transmuting a ref to the pointer
yep, transmute
is a horribly dangerous thing, I usually try to sprinkle type annotations liberally around it to make sure I'm doing the right thing
anyway, happy the issue was found!
Thanks for your time @Chris Fallin @bjorn3
Chris Fallin has marked this topic as resolved.
Last updated: Nov 22 2024 at 17:03 UTC