257fb48521b73f965a9534410daedf595da5a1e0
[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 }
95 }
96 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
97 return psi_ffi_token_type(real_decl_type(type)->type);
98 }
99 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
100 if (darg->var->pointer_level) {
101 return &ffi_type_pointer;
102 } else {
103 return psi_ffi_decl_type(darg->type);
104 }
105 }
106
107 typedef struct PSI_LibffiContext {
108 ffi_cif signature;
109 ffi_type *params[2];
110 } PSI_LibffiContext;
111
112 typedef struct PSI_LibffiCall {
113 void *code;
114 ffi_closure *closure;
115 ffi_cif signature;
116 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
117 } PSI_LibffiCall;
118
119 static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
120 int rc;
121 size_t i, c = decl->args ? decl->args->count : 0;
122 PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
123
124 for (i = 0; i < c; ++i) {
125 call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
126 }
127 call->params[c] = NULL;
128
129 decl->call.info = call;
130 decl->call.rval = decl->func->ptr;
131 decl->call.argc = c;
132 decl->call.args = (void **) &call->params[c+1];
133
134 rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
135 c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
136 ZEND_ASSERT(FFI_OK == rc);
137
138 return call;
139 }
140
141 static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
142 PSI_LibffiContext *context = C->context;
143 int rc;
144
145 call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code);
146 ZEND_ASSERT(call->closure != NULL);
147
148 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
149 rc = ffi_prep_closure_loc(
150 call->closure,
151 &context->signature,
152 psi_ffi_handler,
153 impl,
154 call->code);
155
156 #elif PSI_HAVE_FFI_PREP_CLOSURE
157 rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl);
158 #else
159 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
160 #endif
161 ZEND_ASSERT(FFI_OK == rc);
162 }
163
164 static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
165 if (call->closure) {
166 psi_ffi_closure_free(call->closure);
167 }
168 free(call);
169 }
170
171 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
172 ffi_status rc;
173
174 if (!L) {
175 L = malloc(sizeof(*L));
176 }
177 memset(L, 0, sizeof(*L));
178
179 L->params[0] = &ffi_type_pointer;
180 L->params[1] = &ffi_type_pointer;
181 rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
182 ZEND_ASSERT(rc == FFI_OK);
183
184 return L;
185 }
186
187 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
188 {
189 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
190 }
191
192 static void psi_ffi_init(PSI_Context *C)
193 {
194 C->context = PSI_LibffiContextInit(NULL);
195 }
196
197 static void psi_ffi_dtor(PSI_Context *C)
198 {
199 if (C->decls) {
200 size_t i;
201
202 for (i = 0; i < C->decls->count; ++i) {
203 decl *decl = C->decls->list[i];
204
205 if (decl->call.info) {
206 PSI_LibffiCallFree(decl->call.info);
207 }
208 }
209 }
210 free(C->context);
211 }
212
213 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
214 {
215 size_t i, j = 0;
216 zend_function_entry *zfe;
217
218 if (!C->impls) {
219 return NULL;
220 }
221
222 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
223 for (i = 0; i < C->impls->count; ++i) {
224 zend_function_entry *zf = &zfe[j];
225 PSI_LibffiCall *call;
226 impl *impl = C->impls->list[i];
227
228 if (!impl->decl) {
229 continue;
230 }
231
232 call = PSI_LibffiCallAlloc(C, impl->decl);
233 PSI_LibffiCallInitClosure(C, call, impl);
234
235 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
236 zf->num_args = impl->func->args->count;
237 zf->handler = call->code;
238 zf->arg_info = psi_internal_arginfo(impl);
239 ++j;
240 }
241
242 for (i = 0; i < C->decls->count; ++i) {
243 decl *decl = C->decls->list[i];
244
245 if (decl->impl) {
246 continue;
247 }
248
249 PSI_LibffiCallAlloc(C, decl);
250 }
251
252 return zfe;
253 }
254
255 static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call) {
256 PSI_LibffiCall *call = decl_call->info;
257
258 ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args);
259 }
260
261 static PSI_ContextOps ops = {
262 psi_ffi_init,
263 psi_ffi_dtor,
264 psi_ffi_compile,
265 psi_ffi_call,
266 };
267
268 PSI_ContextOps *PSI_Libffi(void)
269 {
270 return &ops;
271 }
272
273 #endif /* HAVE_LIBFFI */