[Vm-dev] [ENH] Returning from context with sender == nil

Eliot Miranda eliot.miranda at gmail.com
Mon May 7 17:54:42 UTC 2012

On Sun, May 6, 2012 at 11:27 PM, Igor Stasenko <siguctua at gmail.com> wrote:

> Just an idea..
> currently, when returning from bottom context, which has no sender, VM
> generates a message #cannotReturn:
> You can checking by invoking:
> (thisContext privSender: nil). ^ 5
> which is fine, except that i think we can extend this behavior for use
> in cases when we leaving the normal "smalltalk" execution
> back to some C caller, namely:
> - callbacks
> - a "message send" from C
> upon return from method (or block) , an interpreter always checks if
> sender of a context (a caller context) is valid MethodContext
> instance.
> i am thinking about very small change, which will provide additional
> functionality:
>  - if context's sender in non-valid MethodContext, but instance of
> SmallInteger, then
>   we just assume that given smallinteger is an address of C function
> which should be called (by stripping the tag bit of course).

This completely breaks context-to-stack mapping in the COg VM where a
SmallInteger in the sender field is used to identify a context's frame's
frame pointer.  This is hidden from the image by the VM.  Whenever one
accesses the sender field form the image the VM computes the actual sender
object (nil or a context).

It is also unnecessary; return from callback can be performed by invoking a
primitive, as happens in the Alien callbacks.  It is easy to establish an
activation as the sender of the callback activation which receives the
result of the callback activation and returns it to the callback via the
primitive.  i.e.

Alien class>>invokeCallbackContext: vmCallbackContextAddress "<Integer>"
"The low-level entry-point for callbacks sent from the VM/IA32ABI plugin.
 Return via primReturnFromContext:through:.  thisContext's sender is the
 call-out context."
| callbackAlien type |
callbackAlien := (Smalltalk wordSize = 4
ifTrue: [VMCallbackContext32]
ifFalse: [VMCallbackContext64])
atAddress: vmCallbackContextAddress.
[type := Callback evaluateCallbackForContext: callbackAlien]
ifCurtailed: [self error: 'attempt to non-local return across a callback'].
type ifNil:
[type := 1. callbackAlien wordResult: -1].
callbackAlien primReturnAs: type fromContext: thisContext

VMCallbackContext32/VMCallbackContext64>>primReturnAs: typeCode
"<SmallInteger>" fromContext: context "<MethodContext>"
<primitive: 'primReturnAsFromContextThrough' module: 'IA32ABI' error: ec>
^self primitiveFailed

So please, no.

> The function is of kind:
>   void (fn*) (sqInt returnValue).
> i.e. upon discovering sender is smallint, an interpreter will call
> this function with return value as argument.
> As an option for additional safety, we can avoid using pure function
> address in small integer instance, but instead
> use a registry of valid addresses (so that smallint will be an index
> in a table, where cool hackers can register own "No-return" handlers).
> Then VM should check if sender is small integer, and if it is a valid
> registered index in  "No-return" handlers table, and if its ok, then
> call that function,
> otherwise, do as usual and raise "cannot return exception".
> You may argue, that same functionality can be achieved by using
> special primitive(s). Which is true for callbacks, since we definitely
> control the
> callback entry, and on return we can invoke special primitive to
> return back to C caller.
> But thinking about future, imagine that we have an API in VM, similar
> to Objective-C, which allows you to send a message to any object
> and get a result:
> /* C code */
> resultOop = vmSend(receiver, selector, 3 /*num args */ , arg1, arg2, arg3);
> here, we cannot control the execution , since potentially a C caller
> may pick any receiver , any selector, so the entry point, unlike
> from callbacks are not controllable at smalltalk side. However if we
> use the proposed trick, upon return from the context,
> we can clearly detect that it is time to return to C caller and handle
> it accordingly.
> What you think?
> --
> Best regards,
> Igor Stasenko.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20120507/903db4af/attachment.htm

More information about the Vm-dev mailing list