<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"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Ben,</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 27, 2019 at 10:31 PM Ben Coman <<a href="mailto:btc@openinworld.com">btc@openinworld.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"><div>Hi Eliot,</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 7 Aug 2018 at 03:32, Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@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"> <br>
Hi Ben,<br>
<br>
<br>
> On Aug 5, 2018, at 6:34 PM, Ben Coman <<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>> wrote:<br>
> <br>
> <br>
>> On 5 August 2018 at 23:10, Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>> wrote:<br>
>> <br>
>> Hi Ben,<br>
>> <br>
>> <br>
>>> On Aug 4, 2018, at 8:40 AM, Ben Coman <<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>> wrote:<br>
>>> <br>
>>> <br>
>>> A problem with FFI is that if a callout segfaults, all of memory<br>
>>> including that of the Image is suspect, and execution of the Image terminates.<br>
>>> <br>
>>> Occasionally I hunt around hoping to find technology to mitigate that problem.<br>
>>> Maybe this time in I found something... Memory Protection Keys [1]<br>
>>> Perhaps these could ensure Image memory safe when an FFI callout segfaults.<br>
>>> <br>
>>> IIUC the main problem with protecting Image memory on every FFI callout<br>
>>> is the time it would take update the flags on every page of Image memory.<br>
>>> Would being able to change the protection of a massive number of pages<br>
>>> with one syscall make it feasible to wrap them around FFI callouts?<br>
>>> <br>
>>> This may be useful at least where the FFI use is more about reuse of<br>
>>> existing functionality than about performance.<br>
>>> Or at least useful while someone is learning/experimenting with FFI for<br>
>>> the first time or while becoming familiar with some external library.<br>
>>> Further info at [2] & [3].<br>
>> <br>
>> I think there’s a much simpler improvement that doesn’t go this far.  I implemented it in VisualWorks and it’s been in production for more than a decade.  It should be easy to add to Cog.<br>
>> The idea is simply to add a flag that tracks if the VM is in an FFI call or not and to test this flag in the VM’s exception handlers for SIGBUS, SIGILL, SIGSEGV and their equivalents on Windows.  The exception handlers then respond when in an FFI call by failing the FFI call primitive, answering a primitive fail code that includes the exception information.  Recently we extended Cog’s failure codes to allow a structured object (I font have the details handy; I’ll check soon).  In this case we need a pc and/or address and an exception code.<br>
>> Would this approach satisfy you?<br>
> <br>
> That sounds good.  Although the argument I've seen is that a memory<br>
> access error means you "cant recover because you don't know what may have been corrupted"<br>
> I think its worthwhile to be optimistic that the Image may last a bit<br>
> longer to get more information about what call from the Image invoked the FFI failure.<br>
> And if you've been notified (e.g. via Growl message) you can still<br>
> take steps to move to a new Image if the current one is suspect.<br>
<br>>> While it’s possible that an FFI call could damage the Smalltalk heap and VM state it’s often not the case do one wants to be able to at least reap the error code and hence identify where the error occurred, inspect the arguments to the call, etc.  It’s hence about having enough functionality to gather what information is available from outside the call, not about being able to continue for a long time after.<br>
<br>
> Also, the approach you suggest would be a pre-requisite for what I<br>
> suggested anyway, and make it easier to later experiment with MPKs.<br>
<br>
Cool.<br>
<br>
> Let me know what I can do to help (probably more capable on the testing side).<br>
<br>
Will do.  If you’re happy with C programming and the simulator part of it is adding a global flag variable, setting and u setting it in FFI calls and callbacks, and then responding to the flag in the exception handler.  The tricky bit is arranging failure.  That I can work on when time allows.<br></blockquote><div><br></div><div>I believe you did some work on this catching of segfaults in FFI callouts to return a primitive failure.  </div><div>Where did it get up to?  Can you point me at the code that sets/tests this flag and sets up the primitive failure?</div></div></div></blockquote><div><br></div><div>Indeed I did.  Here's the status.  It works on Unix and MacOS but fails on Windows due to a failure in structur4edd exception handling (stack walking) with the MinGW toolchain.  I may revisit the code in the context of MSVC Community Edition 2017, which I'm using for the Terf VM.  Thanks for the reminder.  here's the structure of the code:</div><div><br></div><div>In SmalltalkImage>>#recreateSpecialObjectsArray the error table is extended to include a prototype instance of ExceptionInFFICallError.  When wanting to deliver such an error the VM creates a shallow copy of this object, fills it in. and supplies it as the errorCode in an FFI primitive method.  This change was introduced in System-eem.1041.</div><div><br></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>newArray at: 52 put: #(nil "nil => generic error" #'bad receiver'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                 </span>#'bad argument' #'bad index'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                   </span>#'bad number of arguments'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                     </span>#'inappropriate operation'  #'unsupported operation'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                  </span>#'no modification' #'insufficient object memory'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                       </span>#'insufficient C memory' #'not found' #'bad method'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                    </span>#'internal error in named primitive machinery'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                 </span>#'object may move' #'resource limit exceeded'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                  </span>#'object is pinned' #'primitive write beyond end of object'</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                    </span>#'object moved' #'object not pinned' #'callback error'),</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                       </span>{PrimitiveError new errorName: #'operating system error'; yourself.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                                    </span> ExceptionInFFICallError new errorName: #'exception in FFI call'; yourself}.</div></div><div><br></div><div>ExceptionInFFICallError allInstVarNames #('errorName' 'errorCode' 'pc')<br></div><div>So errorName is #'exception in FFI call', errorCode will be either the second argument to the signal handler on Unix, or the Win32 exception code on Win32.  The pc is the pc at which the exception took place.  The error code will only be delivered if the method contains a primitive error code.  There is a flag in the VM to provide overriding of this behavior, but as yet there is no primitive to access this flag.  See the two implementors of primitiveFailForFFIException:at: in the VMMaker source code.</div><div><br></div><div>Within the VM the fatal exception handlers (sigsegv in the Unix & MacOS VMs; squeakExceptionHandler within the win32 VM) always call primitiveFailForFFIExceptionat.  primitiveFailForFFIExceptionat checks to see if the VM is in an FFI call and if not, simply returns.  If so, it does the relevant stack switching actions to discard the C stack and fail the primitive with the supplied error code & pc.</div><div><br></div><div>For reasons unknown, the exception handler squeakExceptionHandler seems not to be reached if the VM is compiled with clang and/or gcc on win32.  [I have to confirm this; it's been ten months].</div><div><br></div><div>N.B. I had to add an error code variable to ExternalFunction>>#invokeWithArguments:. Pharo should ensure it also has one.</div><div><br></div><div>HTH</div><div>_,,,^..^,,,_<br></div></div><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>best, Eliot</div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>