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