SEGV in in lua_resume after lua_yield from hook.

  • From: Theo Schlossnagle <jesus@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 29 Mar 2022 08:41:16 -0400

I have a (rather large) luajit runtime (on x86_64) and we implement
preempting by using timers on Linux to singal and lua_sethook with a count
of 1.  In the hook point, we check lua_isyieldable (if no luaL_error), but
if yes we lua_yield and lua_resume next time through the event loop.

This work a lot of the time, but frequently I get a SEGV in lua_resume and
it seems to depend on the BCIns at the point of lua_yield.  I'm out of my
depth, but in my limited cases I am able to prevent the segfault by
checking the following:

*(cframe_pc(cframe_raw(L->cframe))-1) == 1369;

(this is what is used to setframe_pc in the hooked yield case)

If the above is true, I return from the hook (without unsetting the hook)
such that I proceed to the next hook invocation.  Next time in the BCIns
there is different and we yield and later resume without issue.  It would
appear in my testing that we only segv on resume when the yield PC points
at 1369.

I am fairly certain I am looking at the wrong thing here as I don't really
understand the VM well.  I have an easily repeatable test environment
(though large and difficult to reduce).  I'd be happy to do further
inspection of the state of the world at the time of the "bad yield" if I
were given some pointers...

I am completely out of my depth here.  Is it possible that I've found a bug
resuming onto specific instructions in the VM?

-- 

Theo Schlossnagle

Other related posts: