Convert a delegate to a function (i.e. make a thunk)

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Apr 25 15:33:01 PDT 2012


On Thu, Apr 26, 2012 at 12:06:54AM +0200, David Nadlinger wrote:
> On Wednesday, 25 April 2012 at 21:27:30 UTC, Jonathan M Davis wrote:
> >But you can't convert a delegate to a function without passing the
> >delegate to the function one way or another, because the function
> >doesn't have a context (not being a delegate).
> 
> That's the whole point of the question - you _can_ pull this off if
> you create a small code fragment somewhere in memory which calls the
> delegate with that specific context argument, and then set your
> function pointer to its address (similar to thunks in most vtable
> implementations or SEH, if you are familiar with these). Now, the only
> problem is that executing data is not quite easy, mostly because of
> security mechanisms (W^X, NX and friends).

This is like GCC's implementation of nested function pointers using
trampolines:

	http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html

These nested function pointers have access to their containing lexical
scope, even though they are just regular, non-fat pointers.

Something similar can be used here, though for full-fledged D delegates
the trampoline would need to be in executable heap memory. I believe
nowadays heap memory is non-executable due to security concerns with
heap overflow exploits, right? So this may require special treatment.


T

-- 
PNP = Plug 'N' Pray


More information about the Digitalmars-d mailing list