7 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
12 # include <sys/mman.h>
16 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
18 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
19 return ffi_closure_alloc(s
, code
);
21 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
22 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
23 if (MAP_FAILED
== *code
) {
32 static void psi_ffi_closure_free(void *c
)
34 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
37 munmap(c
, sizeof(ffi_closure
));
41 static void handler(ffi_cif
*signature
, void *_result
, void **_args
, void *_data
);
43 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
44 return FFI_DEFAULT_ABI
;
46 static inline ffi_type
*psi_ffi_type(token_t t
) {
52 return &ffi_type_void
;
54 return &ffi_type_sint8
;
56 return &ffi_type_uint8
;
58 return &ffi_type_sint16
;
60 return &ffi_type_uint16
;
62 return &ffi_type_sint32
;
64 return &ffi_type_uint32
;
66 return &ffi_type_sint64
;
68 return &ffi_type_uint64
;
70 return &ffi_type_uchar
;
72 return &ffi_type_schar
;
74 return &ffi_type_sshort
;
76 return &ffi_type_sint
;
78 return &ffi_type_slong
;
80 return &ffi_type_float
;
82 return &ffi_type_double
;
85 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
86 return psi_ffi_type(real_decl_type(type
)->type
);
88 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
89 if (darg
->var
->pointer_level
) {
90 return &ffi_type_pointer
;
92 return psi_ffi_decl_type(darg
->type
);
96 typedef struct PSI_LibffiContext
{
100 struct PSI_LibffiData
**list
;
105 typedef struct PSI_LibffiData
{
106 PSI_LibffiContext
*context
;
108 zend_internal_arg_info
*arginfo
;
110 ffi_closure
*closure
;
115 static inline PSI_LibffiData
*PSI_LibffiDataAlloc(PSI_LibffiContext
*context
, impl
*impl
) {
117 size_t i
, c
= impl
->decl
->args
->count
;
118 PSI_LibffiData
*data
= malloc(sizeof(*data
) + c
* sizeof(ffi_type
*));
120 data
->context
= context
;
122 data
->arginfo
= psi_internal_arginfo(impl
);
123 for (i
= 0; i
< c
; ++i
) {
124 data
->params
[i
] = psi_ffi_decl_arg_type(impl
->decl
->args
->args
[i
]);
126 data
->params
[c
] = NULL
;
130 psi_ffi_abi(data
->impl
->decl
->abi
->convention
),
132 psi_ffi_decl_arg_type(data
->impl
->decl
->func
),
134 ZEND_ASSERT(FFI_OK
== rc
);
136 data
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &data
->code
);
137 ZEND_ASSERT(data
->closure
!= NULL
);
138 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
139 rc
= ffi_prep_closure_loc(
145 ZEND_ASSERT(FFI_OK
== rc
);
146 #elif PSI_HAVE_FFI_PREP_CLOSURE
147 rc
= ffi_prep_closure(data
->code
, &context
->signature
, handler
, data
);
148 ZEND_ASSERT(FFI_OK
== rc
);
150 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
153 context
->data
.list
= realloc(context
->data
.list
, ++context
->data
.count
* sizeof(*context
->data
.list
));
154 context
->data
.list
[context
->data
.count
-1] = data
;
159 static inline void PSI_LibffiDataFree(PSI_LibffiData
*data
) {
160 psi_ffi_closure_free(data
->closure
);
165 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
169 L
= malloc(sizeof(*L
));
171 memset(L
, 0, sizeof(*L
));
173 L
->params
[0] = &ffi_type_pointer
;
174 L
->params
[1] = &ffi_type_pointer
;
175 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
176 ZEND_ASSERT(rc
== FFI_OK
);
181 static inline void PSI_LibffiContextDtor(PSI_LibffiContext
*L
) {
184 for (i
= 0; i
< L
->data
.count
; ++i
) {
185 PSI_LibffiDataFree(L
->data
.list
[i
]);
192 static inline void PSI_LibffiContextFree(PSI_LibffiContext
**L
) {
194 PSI_LibffiContextDtor(*L
);
200 static void handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
202 PSI_LibffiData
*data
= _data
;
204 void **arg_ptr
= NULL
, **arg_prm
= NULL
;
207 if (SUCCESS
!= psi_parse_args(*(zend_execute_data
**)_args
[0], data
->impl
)) {
211 if (data
->impl
->decl
->args
->count
) {
212 arg_ptr
= malloc(data
->impl
->decl
->args
->count
* sizeof(*arg_ptr
));
213 arg_prm
= malloc(data
->impl
->decl
->args
->count
* sizeof(*arg_prm
));
215 for (i
= 0; i
< data
->impl
->decl
->args
->count
; ++i
) {
216 decl_arg
*darg
= data
->impl
->decl
->args
->args
[i
];
218 arg_ptr
[i
] = psi_do_let(darg
);
219 arg_prm
[i
] = darg
->let
->val
->is_reference
? &arg_ptr
[i
] : arg_ptr
[i
];
221 darg
->let
->ptr
= arg_ptr
[i
];
225 ffi_call(&data
->signature
, FFI_FN(data
->impl
->decl
->dlptr
), &ret_val
, arg_prm
);
227 psi_do_return(data
->impl
, &ret_val
, *(zval
**)_args
[1]);
229 for (i
= 0; i
< data
->impl
->stmts
->set
.count
; ++i
) {
230 set_stmt
*set
= data
->impl
->stmts
->set
.list
[i
];
232 psi_do_set(set
->arg
->_zv
, set
->val
->func
, set
->val
->vars
);
235 for (i
= 0; i
< data
->impl
->stmts
->fre
.count
; ++i
) {
236 free_stmt
*fre
= data
->impl
->stmts
->fre
.list
[i
];
241 psi_do_clean(data
->impl
);
251 static void init(PSI_Context
*C
)
253 C
->context
= PSI_LibffiContextInit(NULL
);
256 static void dtor(PSI_Context
*C
)
258 PSI_LibffiContextFree((void *) &C
->context
);
261 static zend_function_entry
*compile(PSI_Context
*C
, PSI_Data
*D
)
264 zend_function_entry
*zfe
= calloc(D
->impls
->count
+ 1, sizeof(*zfe
));
265 PSI_LibffiContext
*ctx
= C
->context
;
267 for (i
= 0; i
< D
->impls
->count
; ++i
) {
268 zend_function_entry
*zf
= &zfe
[j
];
269 PSI_LibffiData
*data
;
271 if (!D
->impls
->list
[i
]->decl
) {
275 data
= PSI_LibffiDataAlloc(ctx
, D
->impls
->list
[i
]);
276 zf
->fname
= D
->impls
->list
[i
]->func
->name
+ (D
->impls
->list
[i
]->func
->name
[0] == '\\');
277 zf
->num_args
= D
->impls
->list
[i
]->func
->args
->count
;
278 zf
->handler
= data
->code
;
279 zf
->arg_info
= data
->arginfo
;
286 static PSI_ContextOps ops
= {
292 PSI_ContextOps
*PSI_Libffi(void)