776e98c143d0fb309c20aa3b096aa60a6a3d49a6
[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
60 static inline ffi_abi psi_ffi_abi(const char *convention) {
61 return FFI_DEFAULT_ABI;
62 }
63 static inline ffi_type *psi_ffi_token_type(token_t t) {
64 switch (t) {
65 default:
66 ZEND_ASSERT(0);
67 /* no break */
68 case PSI_T_VOID:
69 return &ffi_type_void;
70 case PSI_T_INT8:
71 return &ffi_type_sint8;
72 case PSI_T_UINT8:
73 return &ffi_type_uint8;
74 case PSI_T_INT16:
75 return &ffi_type_sint16;
76 case PSI_T_UINT16:
77 return &ffi_type_uint16;
78 case PSI_T_INT32:
79 return &ffi_type_sint32;
80 case PSI_T_UINT32:
81 return &ffi_type_uint32;
82 case PSI_T_INT64:
83 return &ffi_type_sint64;
84 case PSI_T_UINT64:
85 return &ffi_type_uint64;
86 case PSI_T_BOOL:
87 return &ffi_type_uchar;
88 case PSI_T_INT:
89 return &ffi_type_sint;
90 case PSI_T_FLOAT:
91 return &ffi_type_float;
92 case PSI_T_DOUBLE:
93 return &ffi_type_double;
94 case PSI_T_POINTER:
95 return &ffi_type_pointer;
96 }
97 }
98 static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
99 switch (impl_type) {
100 case PSI_T_BOOL:
101 return &ffi_type_sint8;
102 case PSI_T_INT:
103 return &ffi_type_sint64;
104 case PSI_T_STRING:
105 return &ffi_type_pointer;
106 case PSI_T_FLOAT:
107 case PSI_T_DOUBLE:
108 return &ffi_type_double;
109 EMPTY_SWITCH_DEFAULT_CASE();
110 }
111 }
112 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
113 return psi_ffi_token_type(real_decl_type(type)->type);
114 }
115 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
116 if (darg->var->pointer_level) {
117 return &ffi_type_pointer;
118 } else {
119 return psi_ffi_decl_type(darg->type);
120 }
121 }
122
123 typedef struct PSI_LibffiContext {
124 ffi_cif signature;
125 ffi_type *params[2];
126 } PSI_LibffiContext;
127
128 typedef struct PSI_LibffiCall {
129 void *code;
130 ffi_closure *closure;
131 ffi_cif signature;
132 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
133 } PSI_LibffiCall;
134
135 static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
136 int rc;
137 size_t i, c = decl->args ? decl->args->count : 0;
138 PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
139
140 for (i = 0; i < c; ++i) {
141 call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
142 }
143 call->params[c] = NULL;
144
145 decl->call.info = call;
146 decl->call.rval = decl->func->ptr;
147 decl->call.argc = c;
148 decl->call.args = (void **) &call->params[c+1];
149
150 rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
151 c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
152 ZEND_ASSERT(FFI_OK == rc);
153
154 return call;
155 }
156
157 static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
158 PSI_LibffiContext *context = C->context;
159 int rc;
160
161 call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code);
162 ZEND_ASSERT(call->closure != NULL);
163
164 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
165 rc = ffi_prep_closure_loc(
166 call->closure,
167 &context->signature,
168 psi_ffi_handler,
169 impl,
170 call->code);
171
172 #elif PSI_HAVE_FFI_PREP_CLOSURE
173 rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl);
174 #else
175 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
176 #endif
177 ZEND_ASSERT(FFI_OK == rc);
178 }
179
180 static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
181 if (call->closure) {
182 psi_ffi_closure_free(call->closure);
183 }
184 free(call);
185 }
186
187 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
188 ffi_status rc;
189
190 if (!L) {
191 L = malloc(sizeof(*L));
192 }
193 memset(L, 0, sizeof(*L));
194
195 L->params[0] = &ffi_type_pointer;
196 L->params[1] = &ffi_type_pointer;
197 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
198 ZEND_ASSERT(rc == FFI_OK);
199
200 return L;
201 }
202
203 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
204 {
205 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
206 }
207
208 static void psi_ffi_init(PSI_Context *C)
209 {
210 C->context = PSI_LibffiContextInit(NULL);
211 }
212
213 static void psi_ffi_dtor(PSI_Context *C)
214 {
215 if (C->decls) {
216 size_t i;
217
218 for (i = 0; i < C->decls->count; ++i) {
219 decl *decl = C->decls->list[i];
220
221 if (decl->call.info) {
222 PSI_LibffiCallFree(decl->call.info);
223 }
224 }
225 }
226 free(C->context);
227 }
228
229 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
230 {
231 size_t i, j = 0;
232 zend_function_entry *zfe;
233
234 if (!C->impls) {
235 return NULL;
236 }
237
238 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
239 for (i = 0; i < C->impls->count; ++i) {
240 zend_function_entry *zf = &zfe[j];
241 PSI_LibffiCall *call;
242 impl *impl = C->impls->list[i];
243
244 if (!impl->decl) {
245 continue;
246 }
247
248 call = PSI_LibffiCallAlloc(C, impl->decl);
249 PSI_LibffiCallInitClosure(C, call, impl);
250
251 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
252 zf->num_args = impl->func->args->count;
253 zf->handler = call->code;
254 zf->arg_info = psi_internal_arginfo(impl);
255 ++j;
256 }
257
258 for (i = 0; i < C->decls->count; ++i) {
259 decl *decl = C->decls->list[i];
260
261 if (decl->impl) {
262 continue;
263 }
264
265 PSI_LibffiCallAlloc(C, decl);
266 }
267
268 return zfe;
269 }
270
271 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
272 PSI_LibffiCall *call = decl_call->info;
273
274 if (va) {
275 ffi_status rc;
276 ffi_cif signature;
277 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
278 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
279
280 for (i = 0; i < nfixedargs; ++i) {
281 params[i] = call->params[i];
282 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
283 }
284 for (i = 0; i < va->args->count; ++i) {
285 params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
286 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
287 }
288 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
289 rc = ffi_prep_cif_var(&signature, call->signature.abi,
290 nfixedargs, ntotalargs,
291 call->signature.rtype, (ffi_type **) params);
292 #else
293 /* FIXME: test in config.m4; assume we can just call anyway */
294 rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
295 call->signature.rtype, (ffi_type **) params);
296 #endif
297 ZEND_ASSERT(FFI_OK == rc);
298 ffi_call(&signature, FFI_FN(decl_call->sym), decl_call->rval, &params[ntotalargs + 1]);
299 free(params);
300 } else {
301 ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args);
302 }
303 }
304
305 static PSI_ContextOps ops = {
306 psi_ffi_init,
307 psi_ffi_dtor,
308 psi_ffi_compile,
309 psi_ffi_call,
310 };
311
312 PSI_ContextOps *PSI_Libffi(void)
313 {
314 return &ops;
315 }
316
317 #endif /* HAVE_LIBFFI */