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