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