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