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