passing structs
[m6w6/ext-psi] / src / libffi.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6
7 #ifdef HAVE_LIBFFI
8
9 #include "php_psi.h"
10 #include "libffi.h"
11
12 #undef PACKAGE
13 #undef PACKAGE_BUGREPORT
14 #undef PACKAGE_NAME
15 #undef PACKAGE_STRING
16 #undef PACKAGE_TARNAME
17 #undef PACKAGE_VERSION
18
19 #include <ffi.h>
20
21 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
22 # if HAVE_UNISTD_H
23 # include <unistd.h>
24 # endif
25 # if HAVE_SYS_MMAN_H
26 # include <sys/mman.h>
27 # ifndef MAP_ANONYMOUS
28 # define MAP_ANONYMOUS MAP_ANON
29 # endif
30 # endif
31 #endif
32
33 static void *psi_ffi_closure_alloc(size_t s, void **code)
34 {
35 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
36 return ffi_closure_alloc(s, code);
37 #elif HAVE_MMAP
38 *code = mmap(NULL, s, PROT_EXEC|PROT_WRITE|PROT_READ,
39 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
40 if (MAP_FAILED == *code) {
41 return NULL;
42 }
43 return *code;
44 #else
45 return NULL;
46 #endif
47 }
48
49 static void psi_ffi_closure_free(void *c)
50 {
51 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
52 ffi_closure_free(c);
53 #elif HAVE_MMAP
54 munmap(c, sizeof(ffi_closure));
55 #endif
56 }
57
58 static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data);
59
60 static inline ffi_abi psi_ffi_abi(const char *convention) {
61 return FFI_DEFAULT_ABI;
62 }
63 static inline ffi_type *psi_ffi_token_type(token_t t) {
64 switch (t) {
65 default:
66 ZEND_ASSERT(0);
67 /* no break */
68 case PSI_T_VOID:
69 return &ffi_type_void;
70 case PSI_T_INT8:
71 return &ffi_type_sint8;
72 case PSI_T_UINT8:
73 return &ffi_type_uint8;
74 case PSI_T_INT16:
75 return &ffi_type_sint16;
76 case PSI_T_UINT16:
77 return &ffi_type_uint16;
78 case PSI_T_INT32:
79 return &ffi_type_sint32;
80 case PSI_T_UINT32:
81 return &ffi_type_uint32;
82 case PSI_T_INT64:
83 return &ffi_type_sint64;
84 case PSI_T_UINT64:
85 return &ffi_type_uint64;
86 case PSI_T_BOOL:
87 return &ffi_type_uchar;
88 case PSI_T_INT:
89 return &ffi_type_sint;
90 case PSI_T_FLOAT:
91 return &ffi_type_float;
92 case PSI_T_DOUBLE:
93 return &ffi_type_double;
94 case PSI_T_POINTER:
95 return &ffi_type_pointer;
96 }
97 }
98 static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
99 switch (impl_type) {
100 case PSI_T_BOOL:
101 return &ffi_type_sint8;
102 case PSI_T_INT:
103 return &ffi_type_sint64;
104 case PSI_T_STRING:
105 return &ffi_type_pointer;
106 case PSI_T_FLOAT:
107 case PSI_T_DOUBLE:
108 return &ffi_type_double;
109 EMPTY_SWITCH_DEFAULT_CASE();
110 }
111 }
112 static void psi_ffi_struct_type_dtor(void *type) {
113 ffi_type *strct = type;
114
115 if (strct->elements) {
116 free(strct->elements);
117 }
118 free(strct);
119 }
120 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
121 static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
122 ffi_type **els = calloc(strct->args->count + 1, sizeof(*els));
123 size_t i;
124
125 for (i = 0; i < strct->args->count; ++i) {
126 els[i] = psi_ffi_decl_arg_type(strct->args->args[i]);
127 }
128 els[i] = NULL;
129
130 return els;
131 }
132 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
133 decl_type *real = real_decl_type(type);
134
135 if (real->type == PSI_T_STRUCT) {
136 if (!real->strct->engine.type) {
137 ffi_type *strct = calloc(1, sizeof(ffi_type));
138
139 strct->type = FFI_TYPE_STRUCT;
140 strct->size = real->strct->size;
141 strct->alignment = 0;
142 strct->elements = psi_ffi_struct_type_elements(real->strct);
143
144 real->strct->engine.type = strct;
145 real->strct->engine.dtor = psi_ffi_struct_type_dtor;
146 }
147
148 return real->strct->engine.type;
149 }
150 return psi_ffi_token_type(real->type);
151 }
152 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
153 if (darg->var->pointer_level) {
154 return &ffi_type_pointer;
155 } else {
156 return psi_ffi_decl_type(darg->type);
157 }
158 }
159
160 typedef struct PSI_LibffiContext {
161 ffi_cif signature;
162 ffi_type *params[2];
163 } PSI_LibffiContext;
164
165 typedef struct PSI_LibffiCall {
166 void *code;
167 ffi_closure *closure;
168 ffi_cif signature;
169 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
170 } PSI_LibffiCall;
171
172 static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
173 int rc;
174 size_t i, c = decl->args ? decl->args->count : 0;
175 PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
176
177 for (i = 0; i < c; ++i) {
178 call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
179 }
180 call->params[c] = NULL;
181
182 decl->call.info = call;
183 decl->call.rval = decl->func->ptr;
184 decl->call.argc = c;
185 decl->call.args = (void **) &call->params[c+1];
186
187 rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
188 c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
189 ZEND_ASSERT(FFI_OK == rc);
190
191 return call;
192 }
193
194 static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
195 PSI_LibffiContext *context = C->context;
196 int rc;
197
198 call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code);
199 ZEND_ASSERT(call->closure != NULL);
200
201 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
202 rc = ffi_prep_closure_loc(
203 call->closure,
204 &context->signature,
205 psi_ffi_handler,
206 impl,
207 call->code);
208
209 #elif PSI_HAVE_FFI_PREP_CLOSURE
210 rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl);
211 #else
212 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
213 #endif
214 ZEND_ASSERT(FFI_OK == rc);
215 }
216
217 static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
218 if (call->closure) {
219 psi_ffi_closure_free(call->closure);
220 }
221 free(call);
222 }
223
224 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
225 ffi_status rc;
226
227 if (!L) {
228 L = malloc(sizeof(*L));
229 }
230 memset(L, 0, sizeof(*L));
231
232 L->params[0] = &ffi_type_pointer;
233 L->params[1] = &ffi_type_pointer;
234 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
235 ZEND_ASSERT(rc == FFI_OK);
236
237 return L;
238 }
239
240 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
241 {
242 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
243 }
244
245 static void psi_ffi_init(PSI_Context *C)
246 {
247 C->context = PSI_LibffiContextInit(NULL);
248 }
249
250 static void psi_ffi_dtor(PSI_Context *C)
251 {
252 if (C->decls) {
253 size_t i;
254
255 for (i = 0; i < C->decls->count; ++i) {
256 decl *decl = C->decls->list[i];
257
258 if (decl->call.info) {
259 PSI_LibffiCallFree(decl->call.info);
260 }
261 }
262 }
263 free(C->context);
264 }
265
266 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
267 {
268 size_t i, j = 0;
269 zend_function_entry *zfe;
270
271 if (!C->impls) {
272 return NULL;
273 }
274
275 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
276 for (i = 0; i < C->impls->count; ++i) {
277 zend_function_entry *zf = &zfe[j];
278 PSI_LibffiCall *call;
279 impl *impl = C->impls->list[i];
280
281 if (!impl->decl) {
282 continue;
283 }
284
285 call = PSI_LibffiCallAlloc(C, impl->decl);
286 PSI_LibffiCallInitClosure(C, call, impl);
287
288 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
289 zf->num_args = impl->func->args->count;
290 zf->handler = call->code;
291 zf->arg_info = psi_internal_arginfo(impl);
292 ++j;
293 }
294
295 for (i = 0; i < C->decls->count; ++i) {
296 decl *decl = C->decls->list[i];
297
298 if (decl->impl) {
299 continue;
300 }
301
302 PSI_LibffiCallAlloc(C, decl);
303 }
304
305 return zfe;
306 }
307
308 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
309 PSI_LibffiCall *call = decl_call->info;
310
311 if (va) {
312 ffi_status rc;
313 ffi_cif signature;
314 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
315 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
316
317 for (i = 0; i < nfixedargs; ++i) {
318 params[i] = call->params[i];
319 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
320 }
321 for (i = 0; i < va->args->count; ++i) {
322 params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
323 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
324 }
325 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
326 rc = ffi_prep_cif_var(&signature, call->signature.abi,
327 nfixedargs, ntotalargs,
328 call->signature.rtype, (ffi_type **) params);
329 #else
330 /* FIXME: test in config.m4; assume we can just call anyway */
331 rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
332 call->signature.rtype, (ffi_type **) params);
333 #endif
334 ZEND_ASSERT(FFI_OK == rc);
335 ffi_call(&signature, FFI_FN(decl_call->sym), decl_call->rval, &params[ntotalargs + 1]);
336 free(params);
337 } else {
338 ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args);
339 }
340 }
341
342 static PSI_ContextOps ops = {
343 psi_ffi_init,
344 psi_ffi_dtor,
345 psi_ffi_compile,
346 psi_ffi_call,
347 };
348
349 PSI_ContextOps *PSI_Libffi(void)
350 {
351 return &ops;
352 }
353
354 #endif /* HAVE_LIBFFI */