<div dir="ltr"><div>Hi Eliot,</div><div>Yesterday I thought I would opt for 4) because it's TSTTCPW.</div><div>Of course it's very hackish and trampoline specific.</div><div>But loading stack and frame pointer is very specific.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le mer. 9 oct. 2019 à 22:10, Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Nicolas, Clément, et al,<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Oct 8, 2019 at 1:40 PM Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>More on the problem that Eliot is speaking about: it can happen in these conditions:</div><div>if is SIGIO is delivered while executing any trampoline transiting from SmalltalkToCStackSwitch.</div><div>What happens here?</div><div><br></div><div>We must load 64bits contents of memory (cStackPointerAddress) to the stack pointer register (SPReg = $rsp) - See genLoadCStackPointer.</div><div>This is done by using the CogRTLOpcode abstract instruction MoveAwR</div><div>But we have no matching instruction in IA-32 X64... We can only load a 64bits memory content in $rax<br></div><div>So the idea is to generate this sequence (CogX64Compiler>>concretizeMoveAwR):</div><div>    xchgq  %rsp, %rax</div><div>    movabsq 0x10027c338, %rax ; cStackPointerAddress<div>    xchgq  %rsp, %rax</div><div>That's clever because it preserves $rax which could be in use when we want to MoveAwR.</div></div></div></div></blockquote><div><br></div><div>Clever, but in the case of %rsp and %rbp, dead wrong :blush:.  The constraint is that x86_64 only provides a 64-bit load from an absolute address into %rax; no other register can be used.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div><div>But it has an unfortunate side effect: the stack pointer temporarily gets the contents of $rax and can thus temporarily point anywhere.</div></div></div></div></blockquote><div><br></div><div>Right; which is unacceptable.  It's a tiny window, but one we hit all the time.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>What happens when performing Squeak SocketTest is that we use some trampolines (I guess for invoking primitives for example) and we generate SIGIO (for some reasons, there are a lot of SIGIO generated on my particular macos machine, so I can trigger the bug more easily than Eliot).<br></div><div>We previously installed a handler for SIGIO via signal(). When we use signal(), the handler shares the stack pointer with user program.<br></div><div>If the event is delivered in between the two xchgq instructions above, the signal handler will then use a corrupted stack pointer pointing anywhere (depending no contents of $rax) when the VM enter the signal handler function, it uses stack pointer to save some states, and corrupt a memory zone, segfault or whatever.</div><div>In the case described by the opensmalltalk vm-dev thread, $rax was pointing to the generated code zone (jitted methodZone), so we corrupted the generated code and soon get punished for that. But it's probable that there might be other (rare) occurrences of this bug.</div><div><br></div><div>Not sure if it is causing the bugs described by Sean, but it's important to use the fix from Eliot ASAP and retry.</div><div>There might be other occurrence of signal(SIGIO,forceInterruptCheck) in minheadless flavour, I did not check if Eliot also corrected it, if not it should also be corrected ASAP, as should every usage of signal() be replaced by ussage of sigaction() with appropriate flags to use sigaltstack() - see Eliot's commit details.<br></div></div></div></blockquote><div><br></div><div>I want to discuss potential fixes with Clément and Nicolas, and anyone else interested. So...</div><div><br></div><div>1. the straight forward fix is to generate different code for setting %rsp and %rbp.  I shall do this very soon.  On x86_64 we dedicate a register for code generation purposes, this is called RISCTempReg, and is either %r8 (SysV ABI (unix)) or %r11 (Windows).<font face="arial, sans-serif">  <font color="#000000"><span>RISCTempReg is never assumed to be live except within a single instruction sequence.  </span></font>We can swap with this before assigning to %rsp, e.</font>g.</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:-webkit-standard;font-size:medium">    </span>xchgq  %r8, %rax</div><div>    movabsq 0x10027c338, %rax ; cStackPointerAddress</div><div>    xchgq  %r8, %rax<br></div><div>    movq %r8, %rsp</div><div><br></div><div>this adds a couple of bytes, but is reliable.</div><div><br></div><div>2. one could imagine exchanging TempReg and RISCTempReg, i.e. TempReg would be either %r8 or %r11, and RISCTemptReg would be %rax.  That would allow</div><div><br>    movabsq 0x10027c338, %rax ; cStackPointerAddress<br>    movq %rax, %rsp<br><br>this at least worked on Ryan's MIPS32 back end when it was in use (no one has tested it in as while AFAIA) where TempReg is S5 (r21), RISCTempReg is AT (r1) and CResultRegister is V0 (r2).  So this is worth investigating.  [I had tried something similar with HPS and it completely broke the code generator so my gut is trying to tell me this will never work; Ryan has proved otherwise].</div><div><br></div><div>3. I would much rather implement some form of tracking whether a particular register is live or not.  A trivial implementation would only track TempReg and only track being not live up until the first assignment to TempReg.  A more sophisticated approach could deal with control flow branching and merging of the liveness info.  I like the trivial approach for now.  That would simply track TempReg and only up until the first explicit assignment to TempReg.</div><div><br></div><div>4. we could simply treat assignments to SPReg and FPReg specially, knowing that these are done only in trampolines and enilopmarts (these are the pieces of code that sit b between JITted code and the rest of the run-time (interpreter, memory manager, primitives, etc), either to call the runtime from JITted code or, as in this case, to enter machine code from the run-time.  I suppose this is OK as long as we document it.  This would allow us to generate what we would generate for #2 above, i.e.</div><div><br>    movabsq 0x10027c338, %rax ; cStackPointerAddress<br>    movq %rax, %rsp</div><div><br>N. are there better ways?</div><div><br></div><div>Clément, Nicolas (et al), what do you think?  </div><div><br>Le dim. 6 oct. 2019 à 12:36, Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>> a écrit :</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div dir="ltr"><span></span></div><div dir="ltr">Hi Sean, Hi All,<div><br></div><div>    this may be because of the issue described here: <a href="http://forum.world.st/Difficult-to-debug-VM-crash-with-full-blocks-and-Sista-V1-tt5103810.html" target="_blank">http://forum.world.st/Difficult-to-debug-VM-crash-with-full-blocks-and-Sista-V1-tt5103810.html</a></div><div><br></div><div>This issue is characterized by the system <span style="background-color:rgba(255,255,255,0)">crashing soon after start up when some significant i/o is done, typically either to files or sockets.  It affects macOS only and may indeed affect only 64-bits.  We have strong evidence that it is caused by the dynamic linker being invoked in the signal handler for SIGIO when the signal is delivered while the VM is executing JITted code.  The symptom that causes the crash is corruption of a particular jitted method’s machine code, eg Delay class>>#startEventLoop, and we believe that the corruption is caused by the linker when it misinterprets a jitted Smalltalk stack frame as an ABI-compliant stack frame and attempts to scan code to link it.</span></div><div><br></div><div>Our diagnosis is speculative; this is extremely hard to reproduce.  Typically in repeating a crashing run SIGIO may no longer be delivered at the same point because any remote server has now woken up and delivers results sooner, etc.  However, Nicolas Cellier and I are both confident that we have correctly identified the bug.</div><div><br></div><div>The fix is simple; SIGIO should be delivered on a dedicated signal stack (see sigaltstack(2)).  I committed a fix yesterday evening and we should see within a week or so if these crashes have disappeared.</div><div><br></div><div>I encourage the Pharo vm maintainers to build and release vms that include <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/c24970eb2859a474065c6f69060c0324aef2b211" target="_blank">https://github.com/OpenSmalltalk/opensmalltalk-vm/commit/c24970eb2859a474065c6f69060c0324aef2b211</a> asap.<br><br><br>Cheers,</div><div>Eliot<br><div id="gmail-m_-6231937969017641788gmail-m_-6727203560026802229gmail-m_3969087781085901526AppleMailSignature" dir="ltr"><span style="background-color:rgba(255,255,255,0)">_,,,^..^,,,_ (phone)</span></div><div dir="ltr"><br>On Oct 3, 2019, at 1:24 PM, Sean P. DeNigris <<a href="mailto:sean@clipperadams.com" target="_blank">sean@clipperadams.com</a>> wrote:<br><br></div><blockquote type="cite"><div dir="ltr"><span>Segmentation fault Thu Oct  3 15:52:33 2019</span><br><span></span><br><span></span><br><span>VM: 201901051900 <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm.git" target="_blank">https://github.com/OpenSmalltalk/opensmalltalk-vm.git</a></span><br><span>Date: Sat Jan 5 20:00:11 2019 CommitHash: 7a3c6b6</span><br><span>Plugins: 201901051900 <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm.git" target="_blank">https://github.com/OpenSmalltalk/opensmalltalk-vm.git</a></span><br><span></span><br><span>C stack backtrace & registers:</span><br><span>    rax 0x0000000124380000 rbx 0x00007ffeebd00050 rcx 0x0000000000468260 rdx</span><br><span>0x0000000000dd6800</span><br><span>    rdi 0x0000000124cee5a0 rsi 0x0000000124cee5a0 rbp 0x00007ffeebcffe50 rsp</span><br><span>0x00007ffeebcffe50</span><br><span>    r8  0x00007fff3f2cefe5 r9  0x0000000000000b00 r10 0x0000000000006000 r11</span><br><span>0xfffffffffcd8d5a0</span><br><span>    r12 0x0000000000000002 r13 0x0000000035800000 r14 0x00007ffeebd00064 r15</span><br><span>0x0000000000002800</span><br><span>    rip 0x00007fff630f7d09</span><br><span>0   libsystem_platform.dylib            0x00007fff630f7d09</span><br><span>_platform_memmove$VARIANT$Haswell + 41</span><br><span>1   Pharo                               0x0000000103f52642 reportStackState</span><br><span>+ 952</span><br><span>2   Pharo                               0x0000000103f52987 sigsegv + 174</span><br><span>3   libsystem_platform.dylib            0x00007fff630fab3d _sigtramp + 29</span><br><span>4   ???                                 0x0000058900000a00 0x0 +</span><br><span>6085968660992</span><br><span>5   libGLImage.dylib                    0x00007fff3f2ce29e</span><br><span>glgProcessPixelsWithProcessor + 2149</span><br><span>6   AMDRadeonX5000GLDriver              0x000000010db16db1 glrATIStoreLevels</span><br><span>+ 1600</span><br><span>7   AMDRadeonX5000GLDriver              0x000000010db52c83</span><br><span>glrAMD_GFX9_LoadSysTextureStandard + 45</span><br><span>8   AMDRadeonX5000GLDriver              0x000000010db519bb glrUpdateTexture</span><br><span>+ 1346</span><br><span>9   libGPUSupportMercury.dylib          0x00007fff5181279d</span><br><span>gpusLoadCurrentTextures + 591</span><br><span>10  AMDRadeonX5000GLDriver              0x000000010db5a099 gldUpdateDispatch</span><br><span>+ 397</span><br><span>11  GLEngine                            0x00007fff3ff72078</span><br><span>gleDoDrawDispatchCore + 629</span><br><span>12  GLEngine                            0x00007fff3ff16369</span><br><span>glDrawArraysInstanced_STD_Exec + 264</span><br><span>13  GLEngine                            0x00007fff3ff1625a</span><br><span>glDrawArrays_UnpackThread + 40</span><br><span>14  GLEngine                            0x00007fff3ff6dce1 gleCmdProcessor +</span><br><span>77</span><br><span>15  libdispatch.dylib                   0x00007fff62ec2dcf</span><br><span>_dispatch_client_callout + 8</span><br><span>16  libdispatch.dylib                   0x00007fff62ecea2c</span><br><span>_dispatch_lane_barrier_sync_invoke_and_complete + 60</span><br><span>17  GLEngine                            0x00007fff3fec4b85</span><br><span>glFlush_ExecThread + 15</span><br><span>18  Pharo                               0x0000000103f4cc62</span><br><span>-[sqSqueakOSXOpenGLView drawRect:flush:] + 314</span><br><span>19  Pharo                               0x0000000103f4cb22 -</span><br><span>...</span><br><span></span><br><span>Smalltalk stack dump:</span><br><span>    0x7ffeebd14238 M DelaySemaphoreScheduler>unscheduleAtTimingPriority</span><br><span>0x10fab3ad0: a(n) DelaySemaphoreScheduler</span><br><span>    0x7ffeebd14270 M [] in</span><br><span>DelaySemaphoreScheduler(DelayBasicScheduler)>runBackendLoopAtTimingPriority</span><br><span>0x10fab3ad0: a(n) DelaySemaphoreScheduler</span><br><span>       0x1125923f8 s BlockClosure>ensure:</span><br><span>       0x111e88d30 s</span><br><span>DelaySemaphoreScheduler(DelayBasicScheduler)>runBackendLoopAtTimingPriority</span><br><span>       0x112590a50 s [] in</span><br><span>DelaySemaphoreScheduler(DelayBasicScheduler)>startTimerEventLoopPriority:</span><br><span>       0x111e88e08 s [] in BlockClosure>newProcess</span><br><span></span><br><span>Most recent primitives</span><br><span>@</span><br><span>actualScreenSize</span><br><span>millisecondClockValue</span><br><span>tempAt:</span><br><span></span><br><span></span><br><span></span><br><span>-----</span><br><span>Cheers,</span><br><span>Sean</span><br><span>--</span><br><span>Sent from: <a href="http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html" target="_blank">http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html</a></span><br><span></span><br></div></blockquote></div></div></div></blockquote></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div></div></div></div></div></div></div></div>
</blockquote></div>