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