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