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 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 if (real->type == PSI_T_STRUCT) {
196 if (!real->strct->engine.type) {
197 ffi_type *strct = calloc(1, sizeof(ffi_type));
198
199 strct->type = FFI_TYPE_STRUCT;
200 strct->size = 0;
201 strct->elements = psi_ffi_struct_type_elements(real->strct);
202
203 real->strct->engine.type = strct;
204 real->strct->engine.dtor = psi_ffi_struct_type_dtor;
205 }
206
207 return real->strct->engine.type;
208 }
209 return psi_ffi_token_type(real->type);
210 }
211 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
212 if (darg->var->pointer_level) {
213 return &ffi_type_pointer;
214 } else {
215 return psi_ffi_decl_type(darg->type);
216 }
217 }
218
219 typedef struct PSI_LibffiContext {
220 ffi_cif signature;
221 ffi_type *params[2];
222 } PSI_LibffiContext;
223
224 typedef struct PSI_LibffiCall {
225 void *code;
226 ffi_closure *closure;
227 ffi_cif signature;
228 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
229 } PSI_LibffiCall;
230
231 static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
232 int rc;
233 size_t i, c = decl->args ? decl->args->count : 0;
234 PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
235
236 for (i = 0; i < c; ++i) {
237 call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
238 }
239 call->params[c] = NULL;
240
241 decl->call.info = call;
242 decl->call.rval = &decl->func->ptr;
243 decl->call.argc = c;
244 decl->call.args = (void **) &call->params[c+1];
245
246 rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
247 c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
248 ZEND_ASSERT(FFI_OK == rc);
249
250 return call;
251 }
252
253 static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
254 PSI_LibffiContext *context = C->context;
255 int rc;
256
257 call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code);
258 ZEND_ASSERT(call->closure != NULL);
259
260 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
261 rc = ffi_prep_closure_loc(
262 call->closure,
263 &context->signature,
264 psi_ffi_handler,
265 impl,
266 call->code);
267
268 #elif PSI_HAVE_FFI_PREP_CLOSURE
269 rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl);
270 #else
271 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
272 #endif
273 ZEND_ASSERT(FFI_OK == rc);
274 }
275
276 static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
277 if (call->closure) {
278 psi_ffi_closure_free(call->closure);
279 }
280 free(call);
281 }
282
283 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
284 ffi_status rc;
285
286 if (!L) {
287 L = malloc(sizeof(*L));
288 }
289 memset(L, 0, sizeof(*L));
290
291 L->params[0] = &ffi_type_pointer;
292 L->params[1] = &ffi_type_pointer;
293 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
294 ZEND_ASSERT(rc == FFI_OK);
295
296 return L;
297 }
298
299 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
300 {
301 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
302 }
303
304 static void psi_ffi_init(PSI_Context *C)
305 {
306 C->context = PSI_LibffiContextInit(NULL);
307 }
308
309 static void psi_ffi_dtor(PSI_Context *C)
310 {
311 if (C->decls) {
312 size_t i;
313
314 for (i = 0; i < C->decls->count; ++i) {
315 decl *decl = C->decls->list[i];
316
317 if (decl->call.info) {
318 PSI_LibffiCallFree(decl->call.info);
319 }
320 }
321 }
322 free(C->context);
323 }
324
325 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
326 {
327 size_t i, j = 0;
328 zend_function_entry *zfe;
329
330 if (!C->impls) {
331 return NULL;
332 }
333
334 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
335 for (i = 0; i < C->impls->count; ++i) {
336 zend_function_entry *zf = &zfe[j];
337 PSI_LibffiCall *call;
338 impl *impl = C->impls->list[i];
339
340 if (!impl->decl) {
341 continue;
342 }
343
344 call = PSI_LibffiCallAlloc(C, impl->decl);
345 PSI_LibffiCallInitClosure(C, call, impl);
346
347 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
348 zf->num_args = impl->func->args->count;
349 zf->handler = call->code;
350 zf->arg_info = psi_internal_arginfo(impl);
351 ++j;
352 }
353
354 for (i = 0; i < C->decls->count; ++i) {
355 decl *decl = C->decls->list[i];
356
357 if (decl->impl) {
358 continue;
359 }
360
361 PSI_LibffiCallAlloc(C, decl);
362 }
363
364 return zfe;
365 }
366
367 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
368 PSI_LibffiCall *call = decl_call->info;
369
370 if (va) {
371 ffi_status rc;
372 ffi_cif signature;
373 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
374 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
375
376 for (i = 0; i < nfixedargs; ++i) {
377 params[i] = call->params[i];
378 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
379 }
380 for (i = 0; i < va->args->count; ++i) {
381 params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
382 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
383 }
384 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
385 rc = ffi_prep_cif_var(&signature, call->signature.abi,
386 nfixedargs, ntotalargs,
387 call->signature.rtype, (ffi_type **) params);
388 #else
389 /* FIXME: test in config.m4; assume we can just call anyway */
390 rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
391 call->signature.rtype, (ffi_type **) params);
392 #endif
393 ZEND_ASSERT(FFI_OK == rc);
394 ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, &params[ntotalargs + 1]);
395 free(params);
396 } else {
397 ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
398 }
399 }
400
401 static PSI_ContextOps ops = {
402 psi_ffi_init,
403 psi_ffi_dtor,
404 psi_ffi_compile,
405 psi_ffi_call,
406 };
407
408 PSI_ContextOps *PSI_Libffi(void)
409 {
410 return &ops;
411 }
412
413 #endif /* HAVE_LIBFFI */