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