Stream: cranelift

Topic: ✔ How to debug SIGSEGV?


view this post on Zulip Ivan Chinenov (Nov 11 2021 at 19:40):

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
}

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:42):

@Ivan Chinenov that CLIF code looks correct; unfortunately it's probably hard to say more without digging in with a debugger

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:43):

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

view this post on Zulip Ivan Chinenov (Nov 11 2021 at 19:45):

@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.

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:45):

OK, that indicates then that the function pointer is probably invalid

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:46):

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

view this post on Zulip Ivan Chinenov (Nov 11 2021 at 19:49):

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

Lisp in rust using cranelift as compiler backend. Contribute to JohnDowson/CraneLisp development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:53):

ok, thanks; possibly someone who has embedded the JIT API directly can see if there's something suspicious? (cc @bjorn3 maybe?)

view this post on Zulip Chris Fallin (Nov 11 2021 at 19:54):

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

view this post on Zulip bjorn3 (Nov 11 2021 at 19:56):

/me is looking...

view this post on Zulip bjorn3 (Nov 11 2021 at 19:59):

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.

Lisp in rust using cranelift as compiler backend. Contribute to JohnDowson/CraneLisp development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Nov 11 2021 at 20:00):

ah! at that line, could ptr be a &*const u8? (auto-borrowing behavior of the match on self)

view this post on Zulip Ivan Chinenov (Nov 11 2021 at 20:01):

Update: after a closer look with a debugger, I realized what was wrong.

view this post on Zulip Chris Fallin (Nov 11 2021 at 20:01):

so the transmute is turning a poitner into the pointer into the code pointer

view this post on Zulip Ivan Chinenov (Nov 11 2021 at 20:01):

I was transmuting a ref to the pointer

view this post on Zulip Chris Fallin (Nov 11 2021 at 20:01):

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

view this post on Zulip Chris Fallin (Nov 11 2021 at 20:02):

anyway, happy the issue was found!

view this post on Zulip Ivan Chinenov (Nov 11 2021 at 20:03):

Thanks for your time @Chris Fallin @bjorn3

view this post on Zulip Notification Bot (Nov 11 2021 at 20:05):

Chris Fallin has marked this topic as resolved.


Last updated: Nov 22 2024 at 17:03 UTC