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