<div dir="ltr"><div dir="ltr">Hi Clément, Hi All,</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 28, 2019 at 1:36 PM Clément Béra <<a href="mailto:bera.clement@gmail.com">bera.clement@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Alistair,<div><br></div><div>I've just investigated the bug tonight and fixed it in VMMaker.oscog-cb.2595. I compiled a new VM from 2595 and I was able to run the 400 iterations of your script without any crashes. </div><div>Thanks for the easy reproduction! Last year when I used the GC benchmarks provided by Feenk, with ~10Gb workloads, for the DLS paper [1], I initially had an image crashing 9 times out of 10 </div><div>when going to 10Gb. I fixed a few bugs on the production GC back then (mainly on segment management) which led the benchmarks to run successfully 99% of the times. But it was still crashing</div><div>on 1%, since I was benchmarking on experimental GCs with various changes I thought the bug did not happen in the production GC, but it turns out I was wrong. And you found a reliable way to </div><div>reproduce :-). So I could investigate. It's so fun to do lemming debugging in the simulator.</div></div></blockquote><div><br></div><div>Yeah :-)  For those unaware of what this is, it is simply the ability to repeat GC crashes in the simulator.</div><div><br></div><div>What we do, bu default, is every time we run a GC in the simulator we first create a complete deep copy of the VM interpreter, memory manager and heap, and then run the GC in the deep copy.  If that crashes because of a bug in GC we can loop from then on, taking fresh copies of the VM state and running the GC until we understand and then fix the bug.  The problem with normal GHC debugging is that the final state of the heap is so corrupted one cannot make sense of it and cannot figure out where the bug is.  Here we alway preserve the initial, valid, state of the heap.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>The GC bug was basically that when Planning Compactor (Production Full GC compactor) decided to do a multiple pass compaction, if it managed to compact everything in one go then it would</div><div>get confused and attempt to compact objects upward instead of downward (address wise) on the second attempt, and that's broken and corrupts memory.</div></div></blockquote><div><br></div><div>Fantastic, Clément.  Thank you.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I started from this script:<br></div><div><pre style="white-space:pre-wrap">| aJson anArray |
aJson := ZnEasy get: '<a href="https://data.nasa.gov/resource/y77d-th95.json" target="_blank">https://data.nasa.gov/resource/y77d-th95.json</a>' asZnUrl.
Array streamContents: [ :aStream |
        400 timesRepeat: [ 
                aStream nextPutAll: (STON fromString: aJson contents).
                Smalltalk saveSession ] ].</pre></div><div><br></div><div>It makes me however very sad that you were not able to use the simulator to debug this issue, I used it and that's how I tracked down the bug in only a few hours. Tracking things down in lldb would have taken me weeks, and I would not have been able to do it since I work during the week :-).</div><div><br></div><div>Therefore I'm going to explain you my process to reproduce the bug in the simulator and to understand where the issue comes from. The mail is quite long, but it would be nice if you could track the bug quickly on your own next time using the simulator. Of course you can skip if you're not interested. @Eliot you may read since I explain how I set-up a Pharo 7 image for simulator debugging, that might come handy for you at some point.</div><div><br></div><div>1] The first thing I did was to reproduce your bug, based on the script, both on Cog and and Stack vm compiled from OpenSmalltalk-VM repository. I initially started with Pharo 8, but for some reason that image is quite broken (formatter issue? Integrator gone wild?). So I switched to Pharo 7 stable. It crashes on both VMs, so I knew the bug was unrelated to the JIT. Most bugs on the core VM (besides people mis-using FFI, which is by far the most common VM bug reported) is either JIT or GC. So we're tracking a GC bug.</div><div>I then built an image which runs your script at start-up (Smalltalk snapshot: true andQuit: true followed by your script, I select all and run do-it).</div><div><br></div><div>2] Then I started the image in the simulator. First thing I noticed is that Pharo 7 is using FFI calls in FreeType, from start-up, and even if you're not using text or if you disable FreeType from the setting browser, Pharo performs in the backgrounds FFI calls for freetype. FreeType FFI calls are incorrectly implemented (the C stack references heap object which are not pinned), therefore these calls corrupts the heap. </div></div></blockquote><div><br></div><div>This is a critical bug to fix.  It also blind sided me form m y own bug in the compactor.  Apologies to all for my stubbornness in pointing the finger at FreeType.  But the bug here is still serious.</div><div><br></div><div>Further, it is at least conceptually possible to simulate FFI calls, or rather simulate the marshaling of FFI calls so that FFI calls can be made within simulation.  We could therefore check in the simulator for unpinned objects, etc.  but we would still be vulnerable to usafe foreign code corrupting the system.  Any FFI call is potentially unsafe, because it is infeasible (performance wise) to e.g. page-protect the Smalltalk heap and VM state during an FFI call, and completely impossible to do this for a threaded (non-blocking) FFI call.</div><div>.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Running a corrupted heap on the VM has undefined behavior, therefore any usage of Pharo 7 right now, wether you actually text or not, wether freetype is enabled or not in the settings, is undefined behavior. I saw in the thread Nicolas/Eliot complaining that this is not a VM bug, indeed, pinning objects is image-side responsibility and it's not a VM bug. In addition, most reported bug comes from people mis-using FFI, so I understand their answer. There was however another bug in the GC, but it's very hard for us to debug it if it's hidden after image corrupting bugs like the FreeType one here. </div><div>So for that I made that change:</div><div><font face="monospace">FreeTypeSettings>>startUp: resuming<br>  "resuming ifTrue:[ self updateFreeType ]"</font><br></div><div>saved, restarted the image, and ensured it was not corrupted (leak checker + swizzling in simulation).</div><div><br></div><div>3] Then I started the image in the simulator. Turns out the image start-up raises error if libgit cannot be loaded, and then the start-up script is not executed due to the exception. So I made that change:</div><div><font face="monospace">LibGitLibrary>>startUp: isImageStarting<br>      "isImageStarting ifTrue: [ self uniqueInstance initializeLibGit2 ]"</font><br></div><div><font face="monospace"><br></font></div><div><font face="arial, sans-serif">4] Turns out ZnEasy does not work well in the simulator. So I preloaded this line </font><font face="monospace">aJson := ZnEasy get: '<a href="https://data.nasa.gov/resource/y77d-th95.json" target="_blank">https://data.nasa.gov/resource/y77d-th95.json</a>' asZnUrl</font>. into a Global variable. The rest of the script remains the same. <span style="font-family:arial,sans-serif">I can finally run your script in the simulator! Usually we simulate Squeak image and all these preliminary steps are not required. But! It is still easier to reproduce this bug that most bugs I have to deal with for Android at work, at least I don't need to buy an uncommon device from an obscure chinese vendor to reproduce :-).</span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div><span style="font-family:arial,sans-serif">5] To shortcut simulation time, since the bug happened around the 60th save for me, I build a different script which snapshots the image to different image names. With a crash at snapshot 59 (only change file written to disk), image 57 was the latest non corrupted image. I then started the simulator (The StackSimulator since we are debugging a GC bug, not the Cog simulator, simulation is faster and simpler). I used the standard script available in the workspace of the Cog dev image built from the guidelines. [2]</span></div><div><font face="monospace">| sis |<br>sis := StackInterpreterSimulator newWithOptions: #(ObjectMemory Spur64BitMemoryManager).<br>"sis desiredNumStackPages: 8." "Speeds up scavenging when simulating.  Set to e.g. 64 for something like the real VM."<br>"sis assertValidExecutionPointersAtEachStep: false." "Set this to true to turn on an expensive assert that checks for valid stack, frame pointers etc on each bytecode.  Useful when you're adding new bytecodes or exotic execution primitives."<br>sis openOn: 'Save57.image'.<br>sis openAsMorph; run</font><span style="font-family:arial,sans-serif"><br></span></div><div>I then let the simulator simulate, went swimming for 1h, and came back 1h30 later (with commute time). The bug happened in the simulator at save 90, I don't know how long it took to reproduce, but < 1h30. Then I had an assertion failure in the compactor:</div><div> <font face="monospace">self assert: (self validRelocationPlanInPass: finalPass) = 0.</font><br></div><div>Good! From there I debugged using lemming debugging (technique described in [3], Section 3.2). When the assertion has failed, simulation is the clone. I went up in the debugger to the point where the clone was made, and restarted the same GC approximately 40 times during debugging because once the heap is corrupted you cannot know anymore what the problem is, but you need to trigger the problem to understand. 40 lemmings over that cliff :-) Good lemmings.</div></div></blockquote><div><br></div><div>:-)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Then I quickly figured out that the GC was performing two successive compactions, and that the second compaction is broken right at the start (tries to move objects upward). Then I looked at the glue code in-between the 2 compactions, and yeah, in the case where the first compaction has compacted everything, the variables are incorrectly set for the second compaction. I tried fixing the variables but it's not that easy, so instead I just aborted compaction in that case (See VMMaker.oscog-cb.2595).<br></div><div><br></div><div>6] I then compiled a VM from the sources to check Slang translator would not complain, it did not. I then built a stack VM (Cog VM seems to be broken on tip of tree due on-going work for ARMv8 support) and run your script again. I was able to run the 400 iterations without crash. Bug seems to be fixed! </div><div><br></div><div>@Eliot now needs to fix tip of tree, generate the code and produce new VMs. ARMv8 support is quite exciting though, giving that MacBooks do not support 32 bits any more and that the next Macbooks are rumoured to be on ARMv8. One wouldn't want to run the VM in a virtual box intel image :-).</div></div></blockquote><div><br></div><div>Right.  I'm committing improvements to the ImageLeakChecker first so that it detects out-of-bounds oops as created by the compactor bug.  I have this working.  I am just committing/generating first.</div><div>Then I'll fix source generation, which is temporarily damaged by the ARMv8 work.  It should be easy to fix.  Then I can generate fresh VMs.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>Alistair, let me know if you have questions. I hope you can work with the simulator as efficiently as we can. If you've not seen it, there's this screencast where I showed how I used the simulator to debug JIT bugs [4]. Audio is not very good because my spoken English sucks, but it shows the main ideas.</div><div><br></div><div>[1] <a href="https://www.researchgate.net/publication/336422106_Lazy_pointer_update_for_low_heap_compaction_pause_times" target="_blank">https://www.researchgate.net/publication/336422106_Lazy_pointer_update_for_low_heap_compaction_pause_times</a></div><div>[2] <a href="http://www.mirandabanda.org/cogblog/build-image/" target="_blank">http://www.mirandabanda.org/cogblog/build-image/</a></div><div>[3] <a href="https://www.researchgate.net/publication/328509577_Two_Decades_of_Smalltalk_VM_Development_Live_VM_Development_through_Simulation_Tools" target="_blank">https://www.researchgate.net/publication/328509577_Two_Decades_of_Smalltalk_VM_Development_Live_VM_Development_through_Simulation_Tools</a></div><div>[4] <a href="https://clementbera.wordpress.com/2018/03/07/sista-vm-screencast/" target="_blank">https://clementbera.wordpress.com/2018/03/07/sista-vm-screencast/</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 27, 2019 at 12:24 PM Alistair Grant <<a href="mailto:notifications@github.com" target="_blank">notifications@github.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"> <p>If you get it reproducible in the simulator please let me know.  I'm working in gdb.</p>

<p style="font-size:small;color:rgb(102,102,102)">—<br>You are receiving this because you commented.<br>Reply to this email directly, <a href="https://github.com/OpenSmalltalk/opensmalltalk-vm/issues/444?email_source=notifications&email_token=AIJPEW42WBGG6FAUZKODAXDQVZKITA5CNFSM4JNBNJH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFJGINQ#issuecomment-559047734" target="_blank">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AIJPEW6JWKIV3TOLN3WI4CTQVZKITANCNFSM4JNBNJHQ" target="_blank">unsubscribe</a>.<img src="https://github.com/notifications/beacon/AIJPEW6B5CNR2G6THPQ3IMDQVZKITA5CNFSM4JNBNJH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFJGINQ.gif" height="1" width="1" alt=""></p>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><span style="font-size:12.8px">Clément Béra<br></span><span style="color:rgb(0,0,238)"><a href="https://clementbera.github.io/" target="_blank">https://clementbera.github.io/</a></span><div style="font-size:12.8px"><a href="https://clementbera.wordpress.com/" target="_blank">https://clementbera.wordpress.com/</a></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div>