basic callback support
[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 #include "engine.h"
12
13 #undef PACKAGE
14 #undef PACKAGE_BUGREPORT
15 #undef PACKAGE_NAME
16 #undef PACKAGE_STRING
17 #undef PACKAGE_TARNAME
18 #undef PACKAGE_VERSION
19
20 #include <ffi.h>
21
22 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
23 # if HAVE_UNISTD_H
24 # include <unistd.h>
25 # endif
26 # if HAVE_SYS_MMAN_H
27 # include <sys/mman.h>
28 # ifndef MAP_ANONYMOUS
29 # define MAP_ANONYMOUS MAP_ANON
30 # endif
31 # endif
32 #endif
33
34 static void *psi_ffi_closure_alloc(size_t s, void **code)
35 {
36 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
37 return ffi_closure_alloc(s, code);
38 #elif HAVE_MMAP
39 *code = mmap(NULL, s, PROT_EXEC|PROT_WRITE|PROT_READ,
40 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
41 if (MAP_FAILED == *code) {
42 return NULL;
43 }
44 return *code;
45 #else
46 # error "Neither ffi_closure_alloc() nor mmap() available"
47 #endif
48 }
49
50 static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) {
51 *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code);
52 ZEND_ASSERT(*closure != NULL);
53
54 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
55 return ffi_prep_closure_loc(*closure, sig, handler, data, *code);
56
57 #elif PSI_HAVE_FFI_PREP_CLOSURE
58 return ffi_prep_closure(*code, sig, handler, data);
59 #else
60 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
61 #endif
62
63 }
64
65 static void psi_ffi_closure_free(void *c)
66 {
67 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
68 ffi_closure_free(c);
69 #elif HAVE_MMAP
70 munmap(c, sizeof(ffi_closure));
71 #endif
72 }
73
74 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
75 {
76 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
77 }
78
79 static void psi_ffi_callback(ffi_cif *_sig, void *_result, void **_args, void *_data)
80 {
81 size_t i;
82 unsigned argc = _sig->nargs;
83 void **argv = _args;
84 let_callback *cb = _data;
85 decl *decl_cb = cb->decl;
86 impl_arg *iarg = cb->func->var->arg;
87 zval return_value, *zargv = calloc(argc, sizeof(*zargv));
88 void *result, *to_free = NULL;
89
90 ZEND_ASSERT(argc == cb->decl->args->count);
91
92 /* prepare args for the userland call */
93 for (i = 0; i < argc; ++i) {
94 cb->decl->args->args[i]->ptr = argv[i];
95 }
96 for (i = 0; i < cb->args->count; ++i) {
97 psi_do_set(&zargv[i], cb->args->vals[i]);
98 }
99 zend_fcall_info_argp(&iarg->val.zend.cb->fci, cb->args->count, zargv);
100
101 /* callback into userland */
102 ZVAL_UNDEF(&return_value);
103 iarg->_zv = &return_value;
104 zend_fcall_info_call(&iarg->val.zend.cb->fci, &iarg->val.zend.cb->fcc, iarg->_zv, NULL);
105
106 /* marshal return value of the userland call */
107 switch (iarg->type->type) {
108 case PSI_T_BOOL: zend_parse_arg_bool(iarg->_zv, &iarg->val.zend.bval, NULL, 0); break;
109 case PSI_T_LONG: zend_parse_arg_long(iarg->_zv, &iarg->val.zend.lval, NULL, 0, 1); break;
110 case PSI_T_FLOAT:
111 case PSI_T_DOUBLE: zend_parse_arg_double(iarg->_zv, &iarg->val.dval, NULL, 0); break;
112 case PSI_T_STRING: zend_parse_arg_str(iarg->_zv, &iarg->val.zend.str, 0); break;
113 }
114 result = cb->func->handler(_result, decl_cb->func->type, iarg, &to_free);
115
116 if (result != _result) {
117 *(void **)_result = result;
118 }
119 }
120
121 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
122
123 typedef struct PSI_LibffiContext {
124 ffi_cif signature;
125 ffi_type *params[2];
126 } PSI_LibffiContext;
127
128 typedef struct PSI_LibffiCall {
129 void *code;
130 ffi_closure *closure;
131 ffi_cif signature;
132 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
133 } PSI_LibffiCall;
134
135 static inline ffi_abi psi_ffi_abi(const char *convention) {
136 return FFI_DEFAULT_ABI;
137 }
138
139 static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
140 int rc;
141 size_t i, c = decl->args ? decl->args->count : 0;
142 PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
143
144 for (i = 0; i < c; ++i) {
145 call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
146 }
147 call->params[c] = NULL;
148
149 decl->call.info = call;
150 decl->call.rval = &decl->func->ptr;
151 decl->call.argc = c;
152 decl->call.args = (void **) &call->params[c+1];
153
154 rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
155 c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
156 ZEND_ASSERT(FFI_OK == rc);
157
158 return call;
159 }
160
161 static inline ffi_status PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
162 PSI_LibffiContext *context = C->context;
163
164 return psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl);
165 }
166
167 static inline ffi_status PSI_LibffiCallInitCallbackClosure(PSI_Context *C, PSI_LibffiCall *call, let_callback *cb) {
168 return psi_ffi_prep_closure(&call->closure, &call->code, &call->signature, psi_ffi_callback, cb);
169 }
170
171 static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
172 if (call->closure) {
173 psi_ffi_closure_free(call->closure);
174 }
175 free(call);
176 }
177
178 static inline ffi_type *psi_ffi_token_type(token_t t) {
179 switch (t) {
180 default:
181 ZEND_ASSERT(0);
182 /* no break */
183 case PSI_T_VOID:
184 return &ffi_type_void;
185 case PSI_T_INT8:
186 return &ffi_type_sint8;
187 case PSI_T_UINT8:
188 return &ffi_type_uint8;
189 case PSI_T_INT16:
190 return &ffi_type_sint16;
191 case PSI_T_UINT16:
192 return &ffi_type_uint16;
193 case PSI_T_INT32:
194 return &ffi_type_sint32;
195 case PSI_T_UINT32:
196 return &ffi_type_uint32;
197 case PSI_T_INT64:
198 return &ffi_type_sint64;
199 case PSI_T_UINT64:
200 return &ffi_type_uint64;
201 case PSI_T_BOOL:
202 return &ffi_type_uchar;
203 case PSI_T_INT:
204 case PSI_T_ENUM:
205 return &ffi_type_sint;
206 case PSI_T_LONG:
207 return &ffi_type_slong;
208 case PSI_T_FLOAT:
209 return &ffi_type_float;
210 case PSI_T_DOUBLE:
211 return &ffi_type_double;
212 #ifdef HAVE_LONG_DOUBLE
213 case PSI_T_LONG_DOUBLE:
214 return &ffi_type_longdouble;
215 #endif
216 case PSI_T_POINTER:
217 case PSI_T_FUNCTION:
218 return &ffi_type_pointer;
219 }
220 }
221 static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
222 switch (impl_type) {
223 case PSI_T_BOOL:
224 return &ffi_type_sint8;
225 case PSI_T_INT:
226 return &ffi_type_sint64;
227 case PSI_T_STRING:
228 return &ffi_type_pointer;
229 case PSI_T_FLOAT:
230 case PSI_T_DOUBLE:
231 return &ffi_type_double;
232 EMPTY_SWITCH_DEFAULT_CASE();
233 }
234 return NULL;
235 }
236 static void psi_ffi_struct_type_dtor(void *type) {
237 ffi_type *strct = type;
238
239 if (strct->elements) {
240 ffi_type **ptr;
241
242 for (ptr = strct->elements; *ptr; ++ptr) {
243 free(*ptr);
244 }
245 free(strct->elements);
246 }
247 free(strct);
248 }
249
250 static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) {
251 size_t i;
252
253 for (i = 0; i < padding; ++i) {
254 ffi_type *pad = malloc(sizeof(*pad));
255
256 memcpy(pad, &ffi_type_schar, sizeof(*pad));
257 *els++ = pad;
258 }
259
260 return padding;
261 }
262
263 static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
264 size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0;
265 ffi_type **els = calloc(argc + 1, sizeof(*els));
266
267 for (i = 0; i < strct->args->count; ++i) {
268 decl_arg *darg = strct->args->args[i];
269 ffi_type *type = malloc(sizeof(*type));
270 size_t padding;
271
272 memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type));
273
274 ZEND_ASSERT(type->size == darg->layout->len);
275
276 if (type->alignment > maxalign) {
277 maxalign = type->alignment;
278 }
279
280 if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) {
281 if (nels + padding + 1 > argc) {
282 argc += padding;
283 els = realloc(els, (argc + 1) * sizeof(*els));
284 els[argc] = NULL;
285 }
286 psi_ffi_struct_type_pad(&els[nels], padding);
287 nels += padding;
288 offset += padding;
289 }
290 ZEND_ASSERT(offset == darg->layout->pos);
291
292 offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1);
293 els[nels++] = type;
294 }
295
296 /* apply struct alignment padding */
297 offset = (offset + maxalign - 1) & ~(maxalign - 1);
298
299 ZEND_ASSERT(offset <= strct->size);
300 if (offset < strct->size) {
301 psi_ffi_struct_type_pad(&els[nels], strct->size - offset);
302 }
303
304 return els;
305 }
306 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
307 decl_type *real = real_decl_type(type);
308
309 switch (real->type) {
310 case PSI_T_FUNCTION:
311 return &ffi_type_pointer;
312
313 case PSI_T_STRUCT:
314 if (!real->strct->engine.type) {
315 ffi_type *strct = calloc(1, sizeof(ffi_type));
316
317 strct->type = FFI_TYPE_STRUCT;
318 strct->size = 0;
319 strct->elements = psi_ffi_struct_type_elements(real->strct);
320
321 real->strct->engine.type = strct;
322 real->strct->engine.dtor = psi_ffi_struct_type_dtor;
323 }
324
325 return real->strct->engine.type;
326
327 case PSI_T_UNION:
328 return psi_ffi_decl_arg_type(real->unn->args->args[0]);
329
330 default:
331 return psi_ffi_token_type(real->type);
332 }
333 }
334 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
335 if (darg->var->pointer_level) {
336 return &ffi_type_pointer;
337 } else {
338 return psi_ffi_decl_type(darg->type);
339 }
340 }
341
342
343 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
344 ffi_status rc;
345
346 if (!L) {
347 L = malloc(sizeof(*L));
348 }
349 memset(L, 0, sizeof(*L));
350
351 L->params[0] = &ffi_type_pointer;
352 L->params[1] = &ffi_type_pointer;
353 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
354 ZEND_ASSERT(rc == FFI_OK);
355
356 return L;
357 }
358
359 static void psi_ffi_init(PSI_Context *C)
360 {
361 C->context = PSI_LibffiContextInit(NULL);
362 }
363
364 static void psi_ffi_dtor(PSI_Context *C)
365 {
366 if (C->decls) {
367 size_t i;
368
369 for (i = 0; i < C->decls->count; ++i) {
370 decl *decl = C->decls->list[i];
371
372 if (decl->call.info) {
373 PSI_LibffiCallFree(decl->call.info);
374 }
375 }
376 }
377 free(C->context);
378 }
379
380 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
381 {
382 size_t c, i, j = 0;
383 zend_function_entry *zfe;
384
385 if (!C->impls) {
386 return NULL;
387 }
388
389 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
390 for (i = 0; i < C->impls->count; ++i) {
391 zend_function_entry *zf = &zfe[j];
392 PSI_LibffiCall *call;
393 impl *impl = C->impls->list[i];
394
395 if (!impl->decl) {
396 continue;
397 }
398
399 call = PSI_LibffiCallAlloc(C, impl->decl);
400 if (FFI_OK != PSI_LibffiCallInitClosure(C, call, impl)) {
401 PSI_LibffiCallFree(call);
402 continue;
403 }
404
405 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
406 zf->num_args = impl->func->args->count;
407 zf->handler = call->code;
408 zf->arg_info = psi_internal_arginfo(impl);
409 ++j;
410
411 for (c = 0; c < impl->stmts->let.count; ++c) {
412 let_stmt *let = impl->stmts->let.list[c];
413
414 if (let->val->kind == PSI_LET_CALLBACK) {
415 let_callback *cb = let->val->data.callback;
416
417 call = PSI_LibffiCallAlloc(C, cb->decl);
418 if (FFI_OK != PSI_LibffiCallInitCallbackClosure(C, call, cb)) {
419 PSI_LibffiCallFree(call);
420 continue;
421 }
422
423 cb->decl->call.sym = call->code;
424 }
425 }
426 }
427
428 for (i = 0; i < C->decls->count; ++i) {
429 decl *decl = C->decls->list[i];
430
431 // if (decl->impl) {
432 // continue;
433 // }
434 if (decl->call.info) {
435 continue;
436 }
437
438 PSI_LibffiCallAlloc(C, decl);
439 }
440
441 return zfe;
442 }
443
444 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
445 PSI_LibffiCall *call = decl_call->info;
446
447 if (va) {
448 ffi_status rc;
449 ffi_cif signature;
450 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
451 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
452
453 for (i = 0; i < nfixedargs; ++i) {
454 params[i] = call->params[i];
455 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
456 }
457 for (i = 0; i < va->args->count; ++i) {
458 params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
459 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
460 }
461 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
462 rc = ffi_prep_cif_var(&signature, call->signature.abi,
463 nfixedargs, ntotalargs,
464 call->signature.rtype, (ffi_type **) params);
465 #else
466 /* FIXME: test in config.m4; assume we can just call anyway */
467 rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
468 call->signature.rtype, (ffi_type **) params);
469 #endif
470 ZEND_ASSERT(FFI_OK == rc);
471 ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, &params[ntotalargs + 1]);
472 free(params);
473 } else {
474 ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
475 }
476 }
477
478 static PSI_ContextOps ops = {
479 psi_ffi_init,
480 psi_ffi_dtor,
481 psi_ffi_compile,
482 psi_ffi_call,
483 };
484
485 PSI_ContextOps *PSI_Libffi(void)
486 {
487 return &ops;
488 }
489
490 #endif /* HAVE_LIBFFI */