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