72ebbf095f25482577dbb2bc6618f2b0948f34d8
14 #undef PACKAGE_BUGREPORT
17 #undef PACKAGE_TARNAME
18 #undef PACKAGE_VERSION
22 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
27 # include <sys/mman.h>
28 # ifndef MAP_ANONYMOUS
29 # define MAP_ANONYMOUS MAP_ANON
34 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
36 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
37 return ffi_closure_alloc(s
, code
);
39 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
40 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
41 if (MAP_FAILED
== *code
) {
46 # error "Neither ffi_closure_alloc() nor mmap() available"
50 static ffi_status
psi_ffi_prep_closure(ffi_closure
**closure
, void **code
, ffi_cif
*sig
, void (*handler
)(ffi_cif
*,void*,void**,void*), void *data
) {
51 *closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), code
);
52 ZEND_ASSERT(*closure
!= NULL
);
54 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
55 return ffi_prep_closure_loc(*closure
, sig
, handler
, data
, *code
);
57 #elif PSI_HAVE_FFI_PREP_CLOSURE
58 return ffi_prep_closure(*code
, sig
, handler
, data
);
60 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
65 static void psi_ffi_closure_free(void *c
)
67 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
70 munmap(c
, sizeof(ffi_closure
));
74 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
76 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
79 static void psi_ffi_callback(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
81 unsigned argc
= _sig
->nargs
;
83 ffi_arg
*res
= _result
;
85 decl_arg
*darg
= let
->var
->arg
;
86 decl
*decl_cb
= darg
->type
->func
;
87 let_callback
*cb
= let
->val
->data
.callback
;
88 impl_arg
*iarg
= cb
->func
->arg
;
89 size_t i
, argc
= cb
->args
->count
;
90 zval
*argv
= calloc(argc
, sizeof(*argv
));
92 // prepare args for the userland call
93 for (i
= 0; i
< decl_cb
->args
->count
; ++i
) {
96 for (i
= 0; i
< cb
->args
->count
; ++i
) {
97 psi_do_set(&argv
[i
], cb
->args
->vals
[i
]);
99 zend_fcall_info_argp(iarg
->val
.zend
.cb
->fci
, argc
, argv
);
100 // marshal return value of the userland call
101 darg
->ptr
= psi_let_val(cb
->func
->type
, iarg
, darg
->ptr
, real_decl_type(darg
->type
)->strct
, &darg
->mem
);
104 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
106 typedef struct PSI_LibffiContext
{
111 typedef struct PSI_LibffiCall
{
113 ffi_closure
*closure
;
115 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
118 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
119 return FFI_DEFAULT_ABI
;
122 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
124 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
125 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
127 for (i
= 0; i
< c
; ++i
) {
128 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
130 call
->params
[c
] = NULL
;
132 decl
->call
.info
= call
;
133 decl
->call
.rval
= &decl
->func
->ptr
;
135 decl
->call
.args
= (void **) &call
->params
[c
+1];
137 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
138 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
139 ZEND_ASSERT(FFI_OK
== rc
);
144 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
145 PSI_LibffiContext
*context
= C
->context
;
148 rc
= psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
149 ZEND_ASSERT(FFI_OK
== rc
);
152 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
154 psi_ffi_closure_free(call
->closure
);
159 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
165 return &ffi_type_void
;
167 return &ffi_type_sint8
;
169 return &ffi_type_uint8
;
171 return &ffi_type_sint16
;
173 return &ffi_type_uint16
;
175 return &ffi_type_sint32
;
177 return &ffi_type_uint32
;
179 return &ffi_type_sint64
;
181 return &ffi_type_uint64
;
183 return &ffi_type_uchar
;
186 return &ffi_type_sint
;
188 return &ffi_type_slong
;
190 return &ffi_type_float
;
192 return &ffi_type_double
;
193 #ifdef HAVE_LONG_DOUBLE
194 case PSI_T_LONG_DOUBLE
:
195 return &ffi_type_longdouble
;
199 return &ffi_type_pointer
;
202 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
205 return &ffi_type_sint8
;
207 return &ffi_type_sint64
;
209 return &ffi_type_pointer
;
212 return &ffi_type_double
;
213 EMPTY_SWITCH_DEFAULT_CASE();
217 static void psi_ffi_struct_type_dtor(void *type
) {
218 ffi_type
*strct
= type
;
220 if (strct
->elements
) {
223 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
226 free(strct
->elements
);
231 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
234 for (i
= 0; i
< padding
; ++i
) {
235 ffi_type
*pad
= malloc(sizeof(*pad
));
237 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
244 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
245 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
246 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
248 for (i
= 0; i
< strct
->args
->count
; ++i
) {
249 decl_arg
*darg
= strct
->args
->args
[i
];
250 ffi_type
*type
= malloc(sizeof(*type
));
253 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
255 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
257 if (type
->alignment
> maxalign
) {
258 maxalign
= type
->alignment
;
261 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
262 if (nels
+ padding
+ 1 > argc
) {
264 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
267 psi_ffi_struct_type_pad(&els
[nels
], padding
);
271 ZEND_ASSERT(offset
== darg
->layout
->pos
);
273 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
277 /* apply struct alignment padding */
278 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
280 ZEND_ASSERT(offset
<= strct
->size
);
281 if (offset
< strct
->size
) {
282 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
287 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
288 decl_type
*real
= real_decl_type(type
);
290 switch (real
->type
) {
292 if (!real
->func
->call
.sym
) {
293 PSI_LibffiCall
*call
= PSI_LibffiCallAlloc(&PSI_G(context
), real
->func
);
296 rc
= psi_ffi_prep_closure(
297 (void *) &real
->func
->call
.closure
.data
,
298 &real
->func
->call
.sym
, &call
->signature
, psi_ffi_handler
, NULL
);
300 real
->func
->call
.info
= call
;
301 real
->func
->call
.closure
.dtor
= psi_ffi_closure_free
;
304 return &ffi_type_pointer
;
307 if (!real
->strct
->engine
.type
) {
308 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
310 strct
->type
= FFI_TYPE_STRUCT
;
312 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
314 real
->strct
->engine
.type
= strct
;
315 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
318 return real
->strct
->engine
.type
;
321 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
324 return psi_ffi_token_type(real
->type
);
327 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
328 if (darg
->var
->pointer_level
) {
329 return &ffi_type_pointer
;
331 return psi_ffi_decl_type(darg
->type
);
336 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
340 L
= malloc(sizeof(*L
));
342 memset(L
, 0, sizeof(*L
));
344 L
->params
[0] = &ffi_type_pointer
;
345 L
->params
[1] = &ffi_type_pointer
;
346 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
347 ZEND_ASSERT(rc
== FFI_OK
);
352 static void psi_ffi_init(PSI_Context
*C
)
354 C
->context
= PSI_LibffiContextInit(NULL
);
357 static void psi_ffi_dtor(PSI_Context
*C
)
362 for (i
= 0; i
< C
->decls
->count
; ++i
) {
363 decl
*decl
= C
->decls
->list
[i
];
365 if (decl
->call
.info
) {
366 PSI_LibffiCallFree(decl
->call
.info
);
373 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
376 zend_function_entry
*zfe
;
382 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
383 for (i
= 0; i
< C
->impls
->count
; ++i
) {
384 zend_function_entry
*zf
= &zfe
[j
];
385 PSI_LibffiCall
*call
;
386 impl
*impl
= C
->impls
->list
[i
];
392 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
393 PSI_LibffiCallInitClosure(C
, call
, impl
);
395 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
396 zf
->num_args
= impl
->func
->args
->count
;
397 zf
->handler
= call
->code
;
398 zf
->arg_info
= psi_internal_arginfo(impl
);
402 for (i
= 0; i
< C
->decls
->count
; ++i
) {
403 decl
*decl
= C
->decls
->list
[i
];
409 PSI_LibffiCallAlloc(C
, decl
);
415 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
416 PSI_LibffiCall
*call
= decl_call
->info
;
421 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
422 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
424 for (i
= 0; i
< nfixedargs
; ++i
) {
425 params
[i
] = call
->params
[i
];
426 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
428 for (i
= 0; i
< va
->args
->count
; ++i
) {
429 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
430 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
432 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
433 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
434 nfixedargs
, ntotalargs
,
435 call
->signature
.rtype
, (ffi_type
**) params
);
437 /* FIXME: test in config.m4; assume we can just call anyway */
438 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
439 call
->signature
.rtype
, (ffi_type
**) params
);
441 ZEND_ASSERT(FFI_OK
== rc
);
442 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
445 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
449 static PSI_ContextOps ops
= {
456 PSI_ContextOps
*PSI_Libffi(void)
461 #endif /* HAVE_LIBFFI */