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
;
87 // prepare args for the userland call
88 for (i
= 0; i
< decl
->args
->count
; ++i
) {
91 // marshal return value of the userland call
92 switch (decl
->func
->type
->type
) {
97 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
99 typedef struct PSI_LibffiContext
{
104 typedef struct PSI_LibffiCall
{
106 ffi_closure
*closure
;
108 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
111 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
112 return FFI_DEFAULT_ABI
;
115 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
117 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
118 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
120 for (i
= 0; i
< c
; ++i
) {
121 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
123 call
->params
[c
] = NULL
;
125 decl
->call
.info
= call
;
126 decl
->call
.rval
= &decl
->func
->ptr
;
128 decl
->call
.args
= (void **) &call
->params
[c
+1];
130 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
131 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
132 ZEND_ASSERT(FFI_OK
== rc
);
137 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
138 PSI_LibffiContext
*context
= C
->context
;
141 rc
= psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
142 ZEND_ASSERT(FFI_OK
== rc
);
145 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
147 psi_ffi_closure_free(call
->closure
);
152 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
158 return &ffi_type_void
;
160 return &ffi_type_sint8
;
162 return &ffi_type_uint8
;
164 return &ffi_type_sint16
;
166 return &ffi_type_uint16
;
168 return &ffi_type_sint32
;
170 return &ffi_type_uint32
;
172 return &ffi_type_sint64
;
174 return &ffi_type_uint64
;
176 return &ffi_type_uchar
;
179 return &ffi_type_sint
;
181 return &ffi_type_slong
;
183 return &ffi_type_float
;
185 return &ffi_type_double
;
186 #ifdef HAVE_LONG_DOUBLE
187 case PSI_T_LONG_DOUBLE
:
188 return &ffi_type_longdouble
;
192 return &ffi_type_pointer
;
195 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
198 return &ffi_type_sint8
;
200 return &ffi_type_sint64
;
202 return &ffi_type_pointer
;
205 return &ffi_type_double
;
206 EMPTY_SWITCH_DEFAULT_CASE();
210 static void psi_ffi_struct_type_dtor(void *type
) {
211 ffi_type
*strct
= type
;
213 if (strct
->elements
) {
216 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
219 free(strct
->elements
);
224 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
227 for (i
= 0; i
< padding
; ++i
) {
228 ffi_type
*pad
= malloc(sizeof(*pad
));
230 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
237 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
238 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
239 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
241 for (i
= 0; i
< strct
->args
->count
; ++i
) {
242 decl_arg
*darg
= strct
->args
->args
[i
];
243 ffi_type
*type
= malloc(sizeof(*type
));
246 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
248 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
250 if (type
->alignment
> maxalign
) {
251 maxalign
= type
->alignment
;
254 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
255 if (nels
+ padding
+ 1 > argc
) {
257 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
260 psi_ffi_struct_type_pad(&els
[nels
], padding
);
264 ZEND_ASSERT(offset
== darg
->layout
->pos
);
266 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
270 /* apply struct alignment padding */
271 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
273 ZEND_ASSERT(offset
<= strct
->size
);
274 if (offset
< strct
->size
) {
275 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
280 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
281 decl_type
*real
= real_decl_type(type
);
283 switch (real
->type
) {
285 if (!real
->func
->call
.sym
) {
286 PSI_LibffiCall
*call
= PSI_LibffiCallAlloc(&PSI_G(context
), real
->func
);
289 rc
= psi_ffi_prep_closure(&real
->func
->call
.closure
.data
, &real
->func
->call
.sym
,
290 &call
->signature
, psi_ffi_handler
, NULL
);
292 real
->func
->call
.info
= call
;
293 real
->func
->call
.closure
.dtor
= psi_ffi_closure_free
;
296 return &ffi_type_pointer
;
299 if (!real
->strct
->engine
.type
) {
300 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
302 strct
->type
= FFI_TYPE_STRUCT
;
304 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
306 real
->strct
->engine
.type
= strct
;
307 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
310 return real
->strct
->engine
.type
;
313 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
316 return psi_ffi_token_type(real
->type
);
319 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
320 if (darg
->var
->pointer_level
) {
321 return &ffi_type_pointer
;
323 return psi_ffi_decl_type(darg
->type
);
328 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
332 L
= malloc(sizeof(*L
));
334 memset(L
, 0, sizeof(*L
));
336 L
->params
[0] = &ffi_type_pointer
;
337 L
->params
[1] = &ffi_type_pointer
;
338 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
339 ZEND_ASSERT(rc
== FFI_OK
);
344 static void psi_ffi_init(PSI_Context
*C
)
346 C
->context
= PSI_LibffiContextInit(NULL
);
349 static void psi_ffi_dtor(PSI_Context
*C
)
354 for (i
= 0; i
< C
->decls
->count
; ++i
) {
355 decl
*decl
= C
->decls
->list
[i
];
357 if (decl
->call
.info
) {
358 PSI_LibffiCallFree(decl
->call
.info
);
365 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
368 zend_function_entry
*zfe
;
374 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
375 for (i
= 0; i
< C
->impls
->count
; ++i
) {
376 zend_function_entry
*zf
= &zfe
[j
];
377 PSI_LibffiCall
*call
;
378 impl
*impl
= C
->impls
->list
[i
];
384 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
385 PSI_LibffiCallInitClosure(C
, call
, impl
);
387 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
388 zf
->num_args
= impl
->func
->args
->count
;
389 zf
->handler
= call
->code
;
390 zf
->arg_info
= psi_internal_arginfo(impl
);
394 for (i
= 0; i
< C
->decls
->count
; ++i
) {
395 decl
*decl
= C
->decls
->list
[i
];
401 PSI_LibffiCallAlloc(C
, decl
);
407 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
408 PSI_LibffiCall
*call
= decl_call
->info
;
413 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
414 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
416 for (i
= 0; i
< nfixedargs
; ++i
) {
417 params
[i
] = call
->params
[i
];
418 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
420 for (i
= 0; i
< va
->args
->count
; ++i
) {
421 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
422 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
424 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
425 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
426 nfixedargs
, ntotalargs
,
427 call
->signature
.rtype
, (ffi_type
**) params
);
429 /* FIXME: test in config.m4; assume we can just call anyway */
430 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
431 call
->signature
.rtype
, (ffi_type
**) params
);
433 ZEND_ASSERT(FFI_OK
== rc
);
434 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
437 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
441 static PSI_ContextOps ops
= {
448 PSI_ContextOps
*PSI_Libffi(void)
453 #endif /* HAVE_LIBFFI */