<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 26, 2014 at 7:59 AM, Ben Coman <span dir="ltr">&lt;<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
Eliot Miranda wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 Hi Ben,<br>
<br>
On Sep 25, 2014, at 5:50 PM, Ben Coman &lt;btc@openInWorld.com&gt; wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I had a brief moment wondering about the general problem we have when calling out through FFI that we expose ourselves to bugs in the external library that crash the Image.  I wonder...<br>
<br>
1. Debuggers like gdb can run a program that crashes, without themselves itself crashing.  Could our FFI calls be wrapped in similar mechanism?<br>
<br>
2. Languages have exception handling, so that errors like divide by zero can be caught.  Could our FFI calls be wrapped in similar mechanism? cheers -ben<br>
</blockquote>
<br>
one thing that is pretty straight-forward to do is to catch and report errors during FFI calls as primitive failures with error codes.  I added this to the VisualWorks VM a while ago  It works like this:<br>
<br>
1. The VM installs a first-level exception handler (windows) or signal handlers for SIGSEGV et al (unix). (This is something the VM already does).<br>
<br>
2. When an FFI call out is made the VM  can find the activation they made an FDI call out.  It must be able to do this if it is to handle a callback.<br>
<br>
3. An FFI call out is modified to set a flag, eg inFFICall, that is tested by the first-level exception handler.  The flag is cleared when the FFI call completes or cleared when taking a callback and restored when the callback returns.<br>
<br>
4. When the first level exception/signal handler is invoked it tests the inFFICall flag.  If unset it does what it always did (exit, do nothing, etc).  If the flag is set it collects the error information, located the FFI activation for the FFI call, does a long jump that returns from the FFI call as a primitive failure, supplying the error information.<br>
<br>
Now this is useful because the VM doesn&#39;t crash (provided the error was localized in its effects and didnt eg corrupt the heap), and hence easy for the developer to find out which FFI call provoked the error, but not as useful as catching the error in gdb.  The exception info is typically an exception code and an address, which may be difficult for the programmer to decode.  Hence the mechanism needs to be made optional, typically in by default, and then disabled to investigate under gdb.<br>
<br>
Does this sound useful?<br>
<br>
Eliot (phone)<br>
</blockquote>
<br></div></div>
Its good to know how that works, and that it is standard already.  Now I wonder, (though it might not be portable) would it be feasible to use something like mprotect to make the Image memory read-only for the duration of the FFI call?   Maybe even use a separate segment just for FFI calls (is it even possible for processes to have additional segments than the usual heap/stack/?<br></blockquote><div><br></div><div>I suspect not on conventional hardware.  On the one hand, it would cost too much.  You might time how long it takes to mprotect the heap.  If you do let me know.  But since mprotect is a system call it will have overhead at least that of a typical FFI call.  On the other hand, some FFI calls are reads and are made to read data into the heap.  The complications of dealing with this (only protecting part of the heap, or allocating a temporary buffer and double-copying the data) are again costs and complications to be avoided.</div><div><br></div><div>But I was talking to David Chisnall at ESUG and he and colleagues are working on special hardware (implemented in FPGA) on which they do their OO and OS work.  This is a capability machine and it allows FFI calls to be invoked cheaply with a capability that excludes writing to the heap.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for the detailed reply Eliot, but note (and I should have said already), these are idle thoughts.  I&#39;ll file answers for later reference, but its not something I&#39;m working on right now.<br></blockquote><div><br></div><div>You&#39;re welcome. </div></div>-- <br>best,<div>Eliot</div>
</div></div>