7 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
12 # include <sys/mman.h>
13 # ifndef MAP_ANONYMOUS
14 # define MAP_ANONYMOUS MAP_ANON
19 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
21 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
22 return ffi_closure_alloc(s
, code
);
24 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
25 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
26 if (MAP_FAILED
== *code
) {
35 static void psi_ffi_closure_free(void *c
)
37 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
40 munmap(c
, sizeof(ffi_closure
));
44 static void handler(ffi_cif
*signature
, void *_result
, void **_args
, void *_data
);
46 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
47 return FFI_DEFAULT_ABI
;
49 static inline ffi_type
*psi_ffi_type(token_t t
) {
55 return &ffi_type_void
;
57 return &ffi_type_sint8
;
59 return &ffi_type_uint8
;
61 return &ffi_type_sint16
;
63 return &ffi_type_uint16
;
65 return &ffi_type_sint32
;
67 return &ffi_type_uint32
;
69 return &ffi_type_sint64
;
71 return &ffi_type_uint64
;
73 return &ffi_type_uchar
;
75 return &ffi_type_schar
;
77 return &ffi_type_sshort
;
79 return &ffi_type_sint
;
81 return &ffi_type_slong
;
83 return &ffi_type_float
;
85 return &ffi_type_double
;
88 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
89 return psi_ffi_type(real_decl_type(type
)->type
);
91 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
92 if (darg
->var
->pointer_level
) {
93 return &ffi_type_pointer
;
95 return psi_ffi_decl_type(darg
->type
);
99 typedef struct PSI_LibffiContext
{
103 struct PSI_LibffiData
**list
;
108 typedef struct PSI_LibffiData
{
109 PSI_LibffiContext
*context
;
111 zend_internal_arg_info
*arginfo
;
113 ffi_closure
*closure
;
118 static inline PSI_LibffiData
*PSI_LibffiDataAlloc(PSI_LibffiContext
*context
, impl
*impl
) {
120 size_t i
, c
= impl
->decl
->args
->count
;
121 PSI_LibffiData
*data
= malloc(sizeof(*data
) + c
* sizeof(ffi_type
*));
123 data
->context
= context
;
125 data
->arginfo
= psi_internal_arginfo(impl
);
126 for (i
= 0; i
< c
; ++i
) {
127 data
->params
[i
] = psi_ffi_decl_arg_type(impl
->decl
->args
->args
[i
]);
129 data
->params
[c
] = NULL
;
133 psi_ffi_abi(data
->impl
->decl
->abi
->convention
),
135 psi_ffi_decl_arg_type(data
->impl
->decl
->func
),
137 ZEND_ASSERT(FFI_OK
== rc
);
139 data
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &data
->code
);
140 ZEND_ASSERT(data
->closure
!= NULL
);
141 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
142 rc
= ffi_prep_closure_loc(
148 ZEND_ASSERT(FFI_OK
== rc
);
149 #elif PSI_HAVE_FFI_PREP_CLOSURE
150 rc
= ffi_prep_closure(data
->code
, &context
->signature
, handler
, data
);
151 ZEND_ASSERT(FFI_OK
== rc
);
153 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
156 context
->data
.list
= realloc(context
->data
.list
, ++context
->data
.count
* sizeof(*context
->data
.list
));
157 context
->data
.list
[context
->data
.count
-1] = data
;
162 static inline void PSI_LibffiDataFree(PSI_LibffiData
*data
) {
163 psi_ffi_closure_free(data
->closure
);
168 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
172 L
= malloc(sizeof(*L
));
174 memset(L
, 0, sizeof(*L
));
176 L
->params
[0] = &ffi_type_pointer
;
177 L
->params
[1] = &ffi_type_pointer
;
178 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
179 ZEND_ASSERT(rc
== FFI_OK
);
184 static inline void PSI_LibffiContextDtor(PSI_LibffiContext
*L
) {
187 for (i
= 0; i
< L
->data
.count
; ++i
) {
188 PSI_LibffiDataFree(L
->data
.list
[i
]);
195 static inline void PSI_LibffiContextFree(PSI_LibffiContext
**L
) {
197 PSI_LibffiContextDtor(*L
);
203 static void handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
205 PSI_LibffiData
*data
= _data
;
207 void **arg_ptr
= NULL
, **arg_prm
= NULL
;
210 if (SUCCESS
!= psi_parse_args(*(zend_execute_data
**)_args
[0], data
->impl
)) {
214 if (data
->impl
->decl
->args
->count
) {
215 arg_ptr
= malloc(data
->impl
->decl
->args
->count
* sizeof(*arg_ptr
));
216 arg_prm
= malloc(data
->impl
->decl
->args
->count
* sizeof(*arg_prm
));
218 for (i
= 0; i
< data
->impl
->decl
->args
->count
; ++i
) {
219 decl_arg
*darg
= data
->impl
->decl
->args
->args
[i
];
221 arg_ptr
[i
] = psi_do_let(darg
);
222 arg_prm
[i
] = (darg
->let
->val
&& darg
->let
->val
->is_reference
)
223 ? &arg_ptr
[i
] : arg_ptr
[i
];
225 darg
->let
->ptr
= arg_ptr
[i
];
229 ffi_call(&data
->signature
, FFI_FN(data
->impl
->decl
->dlptr
), &ret_val
, arg_prm
);
231 psi_do_return(data
->impl
, &ret_val
, *(zval
**)_args
[1]);
233 for (i
= 0; i
< data
->impl
->stmts
->set
.count
; ++i
) {
234 set_stmt
*set
= data
->impl
->stmts
->set
.list
[i
];
236 psi_do_set(set
->arg
->_zv
, set
->val
->func
, set
->val
->vars
);
239 for (i
= 0; i
< data
->impl
->stmts
->fre
.count
; ++i
) {
240 free_stmt
*fre
= data
->impl
->stmts
->fre
.list
[i
];
245 psi_do_clean(data
->impl
);
255 static void init(PSI_Context
*C
)
257 C
->context
= PSI_LibffiContextInit(NULL
);
260 static void dtor(PSI_Context
*C
)
262 PSI_LibffiContextFree((void *) &C
->context
);
265 static zend_function_entry
*compile(PSI_Context
*C
, PSI_Data
*D
)
268 zend_function_entry
*zfe
= calloc(D
->impls
->count
+ 1, sizeof(*zfe
));
269 PSI_LibffiContext
*ctx
= C
->context
;
271 for (i
= 0; i
< D
->impls
->count
; ++i
) {
272 zend_function_entry
*zf
= &zfe
[j
];
273 PSI_LibffiData
*data
;
275 if (!D
->impls
->list
[i
]->decl
) {
279 data
= PSI_LibffiDataAlloc(ctx
, D
->impls
->list
[i
]);
280 zf
->fname
= D
->impls
->list
[i
]->func
->name
+ (D
->impls
->list
[i
]->func
->name
[0] == '\\');
281 zf
->num_args
= D
->impls
->list
[i
]->func
->args
->count
;
282 zf
->handler
= data
->code
;
283 zf
->arg_info
= data
->arginfo
;
290 static PSI_ContextOps ops
= {
296 PSI_ContextOps
*PSI_Libffi(void)