1 /* -----------------------------------------------------------------------------
2 * Foreign export adjustor thunks
6 * ---------------------------------------------------------------------------*/
8 /* A little bit of background...
10 An adjustor thunk is a dynamically allocated code snippet that allows
11 Haskell closures to be viewed as C function pointers.
13 Stable pointers provide a way for the outside world to get access to,
14 and evaluate, Haskell heap objects, with the RTS providing a small
15 range of ops for doing so. So, assuming we've got a stable pointer in
16 our hand in C, we can jump into the Haskell world and evaluate a callback
17 procedure, say. This works OK in some cases where callbacks are used, but
18 does require the external code to know about stable pointers and how to deal
19 with them. We'd like to hide the Haskell-nature of a callback and have it
20 be invoked just like any other C function pointer.
22 Enter adjustor thunks. An adjustor thunk is a little piece of code
23 that's generated on-the-fly (one per Haskell closure being exported)
24 that, when entered using some 'universal' calling convention (e.g., the
25 C calling convention on platform X), pushes an implicit stable pointer
26 (to the Haskell callback) before calling another (static) C function stub
27 which takes care of entering the Haskell code via its stable pointer.
29 An adjustor thunk is allocated on the C heap, and is called from within
30 Haskell just before handing out the function pointer to the Haskell (IO)
31 action. User code should never have to invoke it explicitly.
33 An adjustor thunk differs from a C function pointer in one respect: when
34 the code is through with it, it has to be freed in order to release Haskell
35 and C resources. Failure to do so result in memory leaks on both the C and
43 /* Heavily arch-specific, I'm afraid.. */
44 #if defined(i386_TARGET_ARCH)
46 createAdjustor(int cconv, StgStablePtr hptr, StgFunPtr wptr)
49 unsigned char* adj_code;
50 size_t sizeof_adjustor;
52 if (cconv == 0) { /* the adjustor will be _stdcall'ed */
54 /* Magic constant computed by inspecting the code length of
55 the following assembly language snippet
56 (offset and machine code prefixed):
58 <0>: 58 popl %eax # temp. remove ret addr..
59 <1>: 68 fd fc fe fa pushl 0xfafefcfd # constant is large enough to
61 <6>: 50 pushl %eax # put back ret. addr
62 <7>: b8 fa ef ff 00 movl $0x00ffeffa, %eax # load up wptr
63 <c>: ff e0 jmp %eax # and jump to it.
64 # the callee cleans up the stack
66 sizeof_adjustor = 14*sizeof(char);
68 if ((adjustor = stgMallocBytes(sizeof_adjustor,"createAdjustor")) == NULL) {
72 adj_code = (unsigned char*)adjustor;
73 adj_code[0x00] = (unsigned char)0x58; /* popl %eax */
75 adj_code[0x01] = (unsigned char)0x68; /* pushl hptr (which is a dword immediate ) */
76 *((StgStablePtr*)(adj_code + 0x02)) = (StgStablePtr)hptr;
78 adj_code[0x06] = (unsigned char)0x50; /* pushl %eax */
80 adj_code[0x07] = (unsigned char)0xb8; /* movl $wptr, %eax */
81 *((StgFunPtr*)(adj_code + 0x08)) = (StgFunPtr)wptr;
83 adj_code[0x0c] = (unsigned char)0xff; /* jmp %eax */
84 adj_code[0x0d] = (unsigned char)0xe0;
87 } else { /* the adjustor will be _ccall'ed */
89 /* Magic constant computed by inspecting the code length of
90 the following assembly language snippet
91 (offset and machine code prefixed):
93 <00>: 68 ef be ad de pushl $0xdeadbeef # constant is large enough to
95 <05>: b8 fa ef ff 00 movl $0x00ffeffa, %eax # load up wptr
96 <0a>: ff d0 call %eax # and call it.
97 <0c>: 83 c4 04 addl $0x4,%esp # remove stable pointer.
98 <0f>: c3 ret # return to where you came from.
100 The ccall'ing version is a tad different, passing in the return
101 address of the caller to the auto-generated C stub (which enters
102 via the stable pointer.) (The auto-generated C stub is in on this
103 game, don't worry :-)
105 The adjustor makes the assumption that any return value
106 coming back from the C stub is not stored on the stack.
107 That's (thankfully) the case here with the restricted set of
108 return types that we support.
110 sizeof_adjustor = 16*sizeof(char);
112 if ((adjustor = stgMallocBytes(sizeof_adjustor,"createAdjustor")) == NULL) {
116 adj_code = (unsigned char*)adjustor;
118 adj_code[0x00] = (unsigned char)0x68; /* pushl hptr (which is a dword immediate ) */
119 *((StgStablePtr*)(adj_code+0x01)) = (StgStablePtr)hptr;
121 adj_code[0x05] = (unsigned char)0xb8; /* movl $wptr, %eax */
122 *((StgFunPtr*)(adj_code + 0x06)) = (StgFunPtr)wptr;
124 adj_code[0x0a] = (unsigned char)0xff; /* call %eax */
125 adj_code[0x0b] = (unsigned char)0xd0;
127 adj_code[0x0c] = (unsigned char)0x83; /* addl $0x4, %esp */
128 adj_code[0x0d] = (unsigned char)0xc4;
129 adj_code[0x0e] = (unsigned char)0x04;
131 adj_code[0x0f] = (unsigned char)0xc3; /* ret */
136 return ((void*)adjustor);
140 freeHaskellFunctionPtr(void* ptr)
142 if ( *(unsigned char*)ptr != 0x68 &&
143 *(unsigned char*)ptr != 0x58 ) {
144 fprintf(stderr, "freeHaskellFunctionPtr: not for me, guv! %p\n", ptr);
148 /* Free the stable pointer first..*/
149 if (*(unsigned char*)ptr == 0x68) { /* Aha, a ccall adjustor! */
150 freeStablePtr(*((StgStablePtr*)((unsigned char*)ptr + 0x01)));
152 freeStablePtr(*((StgStablePtr*)((unsigned char*)ptr + 0x02)));
154 *((unsigned char*)ptr) = '\0';
159 #endif /* i386_TARGET_ARCH */