Are the test failures relevant?
Let's add a test case: the RTS should blow up with a fatal error if we try to re-enter it during an unsafe foreign call, so it should be possible to make a test case that will fail after this patch and succeed before it.
Otherwise, if it works I think we can go with this. We're doing more work than we need to to tidy up the stack, but speed is not the primary goal for the interpreter anyway.
I think you can expand this around the SAVE_THREAD_STATE() too.
same, extend it to cover LOAD_THREAD_STATE().
At the moment the UnsafeReenterGhci test hangs here,
#0 pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185 #1 0x00007fffeee62209 in waitCondition (pCond=pCond@entry=0x754ea0, pMut=pMut@entry=0x754ed0) at rts/posix/OSThreads.c:117 #2 0x00007fffeee43c33 in waitForReturnCapability (task=task@entry=0x754e90) at rts/Capability.c:714 #3 0x00007fffeee444ac in waitForCapability (pCap=pCap@entry=0x7fffe77fdc28, task=0x754e90) at rts/Capability.c:806 #4 0x00007fffeee43248 in rts_lock () at rts/RtsAPI.c:597 #5 0x00007fffe65fba21 in zdmainzdMainzdMainzuwrapzzuf () from /tmp/ghc11809_0/libghc_7.so #6 0x00007fffec11d813 in hello () from /tmp/ghc11809_0/libghc_1.so #7 0x00007fffeec18914 in ffi_call_unix64 () from /opt/exp/ghc/ghc-landing/rts/dist/build/libffi.so.6 #8 0x00007fffeec18336 in ffi_call () from /opt/exp/ghc/ghc-landing/rts/dist/build/libffi.so.6 #9 0x00007fffeee460d6 in interpretBCO (cap=0x7fffef08ba40 <MainCapability>) at rts/Interpreter.c:1697 #10 0x00007fffeee3f3e2 in schedule (initialCapability=<optimized out>, task=task@entry=0x754e90) at rts/Schedule.c:471 #11 0x00007fffeee404fc in scheduleWorker (cap=<optimized out>, task=0x754e90) at rts/Schedule.c:2574 #12 0x00007fffee1bf494 in start_thread (arg=0x7fffe77fe700) at pthread_create.c:333 #13 0x00007fffed76faff in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97
It seems that we never get to Schedule, where we would detect the unsafe reentrancy. Need to work out why.