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