made the real decl_type a union
[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_STRUCT:
274 if (!real->real.strct->engine.type) {
275 ffi_type *strct = calloc(1, sizeof(ffi_type));
276
277 strct->type = FFI_TYPE_STRUCT;
278 strct->size = 0;
279 strct->elements = psi_ffi_struct_type_elements(real->real.strct);
280
281 real->real.strct->engine.type = strct;
282 real->real.strct->engine.dtor = psi_ffi_struct_type_dtor;
283 }
284
285 return real->real.strct->engine.type;
286
287 case PSI_T_UNION:
288 return psi_ffi_decl_arg_type(real->real.unn->args->args[0]);
289
290 default:
291 return psi_ffi_token_type(real->type);
292 }
293 }
294 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
295 if (darg->var->pointer_level) {
296 return &ffi_type_pointer;
297 } else {
298 return psi_ffi_decl_type(darg->type);
299 }
300 }
301
302
303 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
304 ffi_status rc;
305
306 if (!L) {
307 L = malloc(sizeof(*L));
308 }
309 memset(L, 0, sizeof(*L));
310
311 L->params[0] = &ffi_type_pointer;
312 L->params[1] = &ffi_type_pointer;
313 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
314 ZEND_ASSERT(rc == FFI_OK);
315
316 return L;
317 }
318
319 static inline void PSI_LibffiContextFree(PSI_LibffiContext **L) {
320 if (*L) {
321 free(*L);
322 *L = NULL;
323 }
324 }
325
326 static void psi_ffi_init(PSI_Context *C)
327 {
328 C->context = PSI_LibffiContextInit(NULL);
329 }
330
331 static void psi_ffi_dtor(PSI_Context *C)
332 {
333 if (C->decls) {
334 size_t i;
335
336 for (i = 0; i < C->decls->count; ++i) {
337 decl *decl = C->decls->list[i];
338
339 if (decl->call.info) {
340 PSI_LibffiCallFree(decl->call.info);
341 }
342 }
343
344 }
345 if (C->impls) {
346 size_t i, j;
347
348 for (i = 0; i < C->impls->count; ++i) {
349 impl *impl = C->impls->list[i];
350
351 for (j = 0; j < impl->stmts->let.count; ++j) {
352 let_stmt *let = impl->stmts->let.list[j];
353
354 if (let->val && let->val->kind == PSI_LET_CALLBACK) {
355 let_callback *cb = let->val->data.callback;
356
357 if (cb->decl && cb->decl->call.info) {
358 PSI_LibffiCallFree(cb->decl->call.info);
359 }
360 }
361 }
362 }
363 }
364 PSI_LibffiContextFree((void *) &C->context);
365 }
366
367 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
368 {
369 size_t c, i, j = 0;
370 zend_function_entry *zfe;
371
372 if (!C->impls) {
373 return NULL;
374 }
375
376 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
377 for (i = 0; i < C->impls->count; ++i) {
378 zend_function_entry *zf = &zfe[j];
379 PSI_LibffiCall *call;
380 impl *impl = C->impls->list[i];
381
382 if (!impl->decl) {
383 continue;
384 }
385
386 if ((call = PSI_LibffiCallAlloc(C, impl->decl))) {
387 if (FFI_OK != PSI_LibffiCallInitClosure(C, call, impl)) {
388 PSI_LibffiCallFree(call);
389 continue;
390 }
391 }
392
393 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
394 zf->num_args = impl->func->args->count;
395 zf->handler = call->code;
396 zf->arg_info = psi_internal_arginfo(impl);
397 ++j;
398
399 for (c = 0; c < impl->stmts->let.count; ++c) {
400 let_stmt *let = impl->stmts->let.list[c];
401
402 if (let->val && let->val->kind == PSI_LET_CALLBACK) {
403 let_callback *cb = let->val->data.callback;
404
405 if ((call = PSI_LibffiCallAlloc(C, cb->decl))) {
406 if (FFI_OK != PSI_LibffiCallInitCallbackClosure(C, call, cb)) {
407 PSI_LibffiCallFree(call);
408 continue;
409 }
410
411 cb->decl->call.sym = call->code;
412 }
413 }
414 }
415 }
416
417 for (i = 0; i < C->decls->count; ++i) {
418 decl *decl = C->decls->list[i];
419
420 if (decl->call.info) {
421 continue;
422 }
423
424 PSI_LibffiCallAlloc(C, decl);
425 }
426
427 return zfe;
428 }
429
430 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
431 PSI_LibffiCall *call = decl_call->info;
432
433 if (va) {
434 ffi_status rc;
435 ffi_cif signature;
436 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
437 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
438
439 for (i = 0; i < nfixedargs; ++i) {
440 params[i] = call->params[i];
441 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
442 }
443 for (i = 0; i < va->args->count; ++i) {
444 params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
445 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
446 }
447 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
448 rc = ffi_prep_cif_var(&signature, call->signature.abi,
449 nfixedargs, ntotalargs,
450 call->signature.rtype, (ffi_type **) params);
451 #else
452 /* FIXME: test in config.m4; assume we can just call anyway */
453 rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
454 call->signature.rtype, (ffi_type **) params);
455 #endif
456 ZEND_ASSERT(FFI_OK == rc);
457 ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, &params[ntotalargs + 1]);
458 free(params);
459 } else {
460 ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
461 }
462 }
463
464 static PSI_ContextOps ops = {
465 psi_ffi_init,
466 psi_ffi_dtor,
467 psi_ffi_compile,
468 psi_ffi_call,
469 };
470
471 PSI_ContextOps *PSI_Libffi(void)
472 {
473 return &ops;
474 }
475
476 #endif /* HAVE_LIBFFI */