<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 18 Sep 2015, at 05:55, Ryan Macnak &lt;<a href="mailto:rmacnak@gmail.com" class="">rmacnak@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Tue, Sep 15, 2015 at 8:29 AM, Esteban Lorenzano <span dir="ltr" class="">&lt;<a href="mailto:estebanlm@gmail.com" target="_blank" class="">estebanlm@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br class="">
Hi,<br class="">
<br class="">
I’m working on merge FFI with Alien as a way to provide a single callback mechanism (I already talked with Eliot and he agrees this is the way to go in the future).<br class="">
That means review certain stuff, a couple of them I already submitted in my last commit.<br class="">
Now I have this problem. This is my example method:<br class="">
<br class="">
exampleCqsort<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; | cb rand values orig sort |<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; rand := Random new.<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; values := FFIExternalArray newType: 'double' size: 100.<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; 1 to: 100 do: [ :i| values at: i put: rand next ].<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; orig := (1 to: 100) collect: [:i| values at: i].<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; cb := Callback<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signature:&nbsp; #(int (*)(const void *, const void *))<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block: [ :arg1 :arg2 | ((arg1 doubleAt: 1) - (arg2 doubleAt: 1)) sign ].<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; [<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self primQsort: values getHandle with: 100 with: values type typeSize with: cb thunk.<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort := values asArray ]<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; ensure: [ values free ].<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; ^orig -&gt; sort<br class="">
<br class="">
primQsort: array with: count with: size with: compare<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &lt;cdecl: void 'qsort' (void* ulong ulong void*) module: 'libc’&gt;<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; ^ self primitiveFailed<br class="">
<br class="">
… that basically works. But this depends on this method in Callback:<br class="">
<br class="">
voidstarvoidstarRetint: callbackContext sp: spAlien<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &lt;signature: #(int (*)(const void *, const void *)) abi: 'IA32'&gt;<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; | value |<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; value := block<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: (Alien forPointer: (spAlien unsignedLongAt: 1))<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: (Alien forPointer: (spAlien unsignedLongAt: 5)).<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; ^ callbackContext wordResult: value<br class="">
<br class="">
And I want to remove Alien from the equation (there will still be some… and there will be the primitives of course, but I want to reduce their use at the minimum for obvious reasons: hide the extra complexity to users).<br class="">
So this function would be something like this instead:<br class="">
<br class="">
voidstarvoidstarRetint: callbackContext sp: spAlien<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &lt;signature: #(int (*)(const void *, const void *)) abi: 'IA32'&gt;<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; | value |<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; value := block<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: (spAlien pointerAt: 1) “It will answer an ExternalAddress”<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: ((spAlien pointerAt: 5). “It will answer an ExternalAddress”<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; ^ callbackContext wordResult: value<br class="">
<br class="">
&nbsp;So… this *should* work but it doesn’t because in primitiveFFIDoubleAt: I have a call to<br class="">
<br class="">
ffiAddressOf: rcvr startingAt: byteOffset size: byteSize<br class="">
<br class="">
who mades this check:<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "don't you dare to read from object memory!"<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (addr == 0 or:[interpreterProxy isInMemory: addr])<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ifTrue:[^interpreterProxy primitiveFail].<br class="">
<br class="">
and well… since I’m calling things from a callback the #isInMemory: method fails.<br class="">
<br class="">
If I took out that method everything works ok and since the equivalent IA32ABIPlugin does not have that check I can suppose it is ok to remove the validation.<br class="">
<br class="">
But! Since there was that comment ("don't you dare to read from object memory!”)… I’m afraid I can be making a mistake.<br class="">
<br class="">
What do you think?</blockquote><div class=""><br class=""></div><div class="">The elements of your&nbsp;FFIExternalArray&nbsp;should be in malloc's heap, not in the Smalltalk heap, so the check should pass.&nbsp;</div></div></div></div>
</div></blockquote></div><br class=""><div class="">it doesn’t, that’s the point.&nbsp;</div><div class="">also IMO that’s incorrect: if I want to read some structure in smalltalk memory who will be passed as a ByteArray (bah, a byte*) to FFI, that check will fail too… and that’s incorrect.</div><div class="">and since that check was removed in IA32ABIPlugin, I think this has been taken into account.</div><div class=""><br class=""></div><div class="">Esteban</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>