update readme
[m6w6/ext-psi] / src / engine.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6 #include "php_psi.h"
7
8 #include "zend_exceptions.h"
9
10 #include "engine.h"
11 #include "calc.h"
12 #include "marshal.h"
13
14 size_t psi_t_alignment(token_t t)
15 {
16 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
17 switch (t) {
18 PSI_ALIGNOF(INT8);
19 PSI_ALIGNOF(UINT8);
20 PSI_ALIGNOF(INT16);
21 PSI_ALIGNOF(UINT16);
22 PSI_ALIGNOF(INT32);
23 PSI_ALIGNOF(UINT32);
24 PSI_ALIGNOF(INT64);
25 PSI_ALIGNOF(UINT64);
26 case PSI_T_FLOAT:
27 return ALIGNOF_FLOAT;
28 case PSI_T_DOUBLE:
29 return ALIGNOF_DOUBLE;
30 case PSI_T_POINTER:
31 case PSI_T_FUNCTION:
32 return ALIGNOF_VOID_P;
33 case PSI_T_ENUM:
34 return ALIGNOF_INT;
35 EMPTY_SWITCH_DEFAULT_CASE();
36 }
37 return 0;
38 }
39
40 size_t psi_t_size(token_t t)
41 {
42 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
43 switch (t) {
44 PSI_SIZEOF(INT8);
45 PSI_SIZEOF(UINT8);
46 PSI_SIZEOF(INT16);
47 PSI_SIZEOF(UINT16);
48 PSI_SIZEOF(INT32);
49 PSI_SIZEOF(UINT32);
50 PSI_SIZEOF(INT64);
51 PSI_SIZEOF(UINT64);
52 case PSI_T_FLOAT:
53 return SIZEOF_FLOAT;
54 case PSI_T_DOUBLE:
55 return SIZEOF_DOUBLE;
56 case PSI_T_POINTER:
57 case PSI_T_FUNCTION:
58 return SIZEOF_VOID_P;
59 case PSI_T_ENUM:
60 return SIZEOF_INT;
61 EMPTY_SWITCH_DEFAULT_CASE();
62 }
63 return 0;
64 }
65
66 int psi_internal_type(impl_type *type)
67 {
68 switch (type->type) {
69 case PSI_T_BOOL:
70 return _IS_BOOL;
71 case PSI_T_INT:
72 return IS_LONG;
73 case PSI_T_FLOAT:
74 case PSI_T_DOUBLE:
75 return IS_DOUBLE;
76 case PSI_T_STRING:
77 return IS_STRING;
78 case PSI_T_ARRAY:
79 return IS_ARRAY;
80 default:
81 return 0;
82 }
83 }
84
85 zend_internal_arg_info *psi_internal_arginfo(impl *impl)
86 {
87 size_t i;
88 zend_internal_arg_info *aip;
89 zend_internal_function_info *fi;
90
91 aip = calloc(impl->func->args->count + 1 + !!impl->func->args->vararg.name, sizeof(*aip));
92
93 fi = (zend_internal_function_info *) &aip[0];
94 fi->allow_null = 1;
95 fi->required_num_args = psi_num_min_args(impl);
96 fi->return_reference = impl->func->return_reference;
97 fi->type_hint = psi_internal_type(impl->func->return_type);
98
99 if (impl->func->args->vararg.name) {
100 impl_arg *vararg = impl->func->args->vararg.name;
101 zend_internal_arg_info *ai = &aip[impl->func->args->count];
102
103 ai->name = vararg->var->name;
104 ai->allow_null = 1;
105 ai->type_hint = psi_internal_type(vararg->type);
106 if (vararg->var->reference) {
107 ai->pass_by_reference = 1;
108 }
109 ai->is_variadic = 1;
110 }
111
112 for (i = 0; i < impl->func->args->count; ++i) {
113 impl_arg *iarg = impl->func->args->args[i];
114 zend_internal_arg_info *ai = &aip[i+1];
115
116 ai->name = iarg->var->name;
117 ai->type_hint = psi_internal_type(iarg->type);
118 if (iarg->var->reference) {
119 ai->pass_by_reference = 1;
120 }
121 //if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
122 ai->allow_null = 1;
123 //}
124 }
125
126 return aip;
127 }
128
129 size_t psi_num_min_args(impl *impl)
130 {
131 size_t i, n = impl->func->args->count;
132
133 for (i = 0; i < impl->func->args->count; ++i) {
134 if (impl->func->args->args[i]->def) {
135 --n;
136 }
137 }
138 return n;
139 }
140
141
142 static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
143 {
144 size_t i;
145 impl_arg *iarg;
146 zend_error_handling zeh;
147
148 zend_replace_error_handling(EH_THROW, zend_exception_get_default(), &zeh);
149
150 if (!impl->func->args->count) {
151 ZEND_RESULT_CODE rv;
152
153 rv = zend_parse_parameters_none();
154 zend_restore_error_handling(&zeh);
155 return rv;
156 }
157
158 ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->vararg.name ? -1 : impl->func->args->count)
159 nextarg:
160 if (impl->func->args->vararg.name && _i >= impl->func->args->count) {
161 impl_arg *varg = impl->func->args->vararg.name;
162 iarg = init_impl_arg(
163 init_impl_type(varg->type->type, varg->type->name),
164 init_impl_var(varg->var->name, varg->var->reference),
165 NULL);
166
167 Z_PARAM_OPTIONAL;
168 if (_i == impl->func->args->count) {
169 impl->func->args->vararg.args = init_impl_args(iarg);
170 } else {
171 add_impl_arg(impl->func->args->vararg.args, iarg);
172 }
173 } else {
174 iarg = impl->func->args->args[_i];
175 if (iarg->def) {
176 Z_PARAM_OPTIONAL;
177 }
178 }
179 if (PSI_T_BOOL == iarg->type->type) {
180 Z_PARAM_BOOL(iarg->val.zend.bval);
181 } else if (PSI_T_INT == iarg->type->type) {
182 Z_PARAM_LONG(iarg->val.zend.lval);
183 } else if (PSI_T_FLOAT == iarg->type->type || PSI_T_DOUBLE == iarg->type->type) {
184 Z_PARAM_DOUBLE(iarg->val.dval);
185 } else if (PSI_T_STRING == iarg->type->type) {
186 Z_PARAM_STR_EX(iarg->val.zend.str, 1, iarg->var->reference);
187 if (iarg->val.zend.str) {
188 zend_string_addref(iarg->val.zend.str);
189 }
190 } else if (PSI_T_ARRAY == iarg->type->type) {
191 Z_PARAM_PROLOGUE(0);
192 } else if (PSI_T_OBJECT == iarg->type->type) {
193 Z_PARAM_PROLOGUE(0);
194 } else if (PSI_T_MIXED == iarg->type->type) {
195 Z_PARAM_PROLOGUE(0);
196 } else {
197 error_code = ZPP_ERROR_FAILURE;
198 break;
199 }
200 iarg->_zv = _arg;
201 ZVAL_DEREF(iarg->_zv);
202 if (_i < _num_args) {
203 goto nextarg;
204 }
205 ZEND_PARSE_PARAMETERS_END_EX(
206 zend_restore_error_handling(&zeh);
207 return FAILURE
208 );
209
210 /* set up defaults */
211 for (i = 0; i < impl->func->args->count; ++i) {
212 if (i >= EX_NUM_ARGS() && iarg->def) {
213 iarg = impl->func->args->args[i];
214
215 switch (iarg->type->type) {
216 case PSI_T_BOOL:
217 iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
218 break;
219 case PSI_T_INT:
220 iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
221 break;
222 case PSI_T_FLOAT:
223 case PSI_T_DOUBLE:
224 iarg->val.dval = zend_strtod(iarg->def->text, NULL);
225 break;
226 case PSI_T_STRING:
227 /* FIXME */
228 iarg->val.zend.str = zend_string_init(&iarg->def->text[1], strlen(iarg->def->text) - 2, 0);
229 break;
230 }
231 }
232 }
233
234 zend_restore_error_handling(&zeh);
235 return SUCCESS;
236 }
237
238 static inline void *psi_do_calloc(let_calloc *alloc)
239 {
240 zend_long n = psi_long_num_exp(alloc->nmemb, NULL), s = psi_long_num_exp(alloc->size, NULL);
241 void *mem = safe_emalloc(n, s, sizeof(void *));
242 memset(mem, 0, n * s + sizeof(void *));
243 #if 0
244 fprintf(stderr, "calloc: %p\n", mem);
245 #endif
246 return mem;
247 }
248
249 static inline impl_val *psi_let_val(token_t let_func, impl_arg *iarg, impl_val *arg_val, decl_struct *strct, void **to_free)
250 {
251 switch (let_func) {
252 case PSI_T_BOOLVAL:
253 if (iarg->type->type == PSI_T_BOOL) {
254 arg_val->cval = iarg->val.zend.bval;
255 } else {
256 arg_val->cval = zend_is_true(iarg->_zv);
257 }
258 break;
259 case PSI_T_INTVAL:
260 if (iarg->type->type == PSI_T_INT) {
261 arg_val->lval = iarg->val.zend.lval;
262 } else {
263 arg_val->lval = zval_get_long(iarg->_zv);
264 }
265 break;
266 case PSI_T_FLOATVAL:
267 if (iarg->type->type == PSI_T_FLOAT || iarg->type->type == PSI_T_DOUBLE) {
268 arg_val->dval = iarg->val.dval;
269 } else {
270 arg_val->dval = zval_get_double(iarg->_zv);
271 }
272 break;
273 case PSI_T_PATHVAL:
274 case PSI_T_STRVAL:
275 if (iarg->type->type == PSI_T_STRING) {
276 if (iarg->val.zend.str) {
277 arg_val->ptr = estrndup(iarg->val.zend.str->val, iarg->val.zend.str->len);
278 *to_free = arg_val->ptr;
279 } else {
280 arg_val->ptr = "";
281 }
282 } else {
283 zend_string *zs = zval_get_string(iarg->_zv);
284 arg_val->ptr = estrdup(zs->val);
285 *to_free = arg_val->ptr;
286 zend_string_release(zs);
287 }
288 if (PSI_T_PATHVAL == let_func) {
289 if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
290 efree(arg_val->ptr);
291 return NULL;
292 }
293 }
294 break;
295 case PSI_T_STRLEN:
296 if (iarg->type->type == PSI_T_STRING) {
297 if (iarg->val.zend.str) {
298 arg_val->lval = iarg->val.zend.str->len;
299 } else {
300 arg_val->lval = 0;
301 }
302 } else {
303 zend_string *zs = zval_get_string(iarg->_zv);
304 arg_val->lval = zs->len;
305 zend_string_release(zs);
306 }
307 break;
308 case PSI_T_ARRVAL:
309 if (iarg->type->type == PSI_T_ARRAY) {
310 arg_val = psi_array_to_struct(strct, HASH_OF(iarg->_zv));
311 *to_free = arg_val;
312 }
313 break;
314 case PSI_T_OBJVAL:
315 if (iarg->type->type == PSI_T_OBJECT) {
316 psi_object *obj;
317
318 if (!instanceof_function(Z_OBJCE_P(iarg->_zv), psi_object_get_class_entry())) {
319 return NULL;
320 }
321
322 obj = PSI_OBJ(iarg->_zv, NULL);
323 arg_val->ptr = obj->data;
324 }
325 break;
326 EMPTY_SWITCH_DEFAULT_CASE();
327 }
328 return arg_val;
329 }
330
331 static inline void *psi_do_let(let_stmt *let)
332 {
333 decl_arg *darg = let->var->arg;
334 impl_val *arg_val = darg->ptr;
335 impl_arg *iarg;
336
337 switch (let->val ? let->val->kind : PSI_LET_NULL) {
338 case PSI_LET_TMP:
339 memcpy(arg_val, deref_impl_val(let->val->data.var->arg->let->ptr, let->val->data.var), sizeof(*arg_val));
340 #if 0
341 fprintf(stderr, "LET TMP: %p -> %p\n",
342 let->val->data.var->arg->let->ptr,
343 arg_val->ptr);
344 #endif
345 break;
346 case PSI_LET_NULL:
347 if (darg->var->array_size) {
348 arg_val->ptr = ecalloc(darg->var->array_size, sizeof(*arg_val));
349 darg->mem = arg_val->ptr;
350 } else {
351 memset(arg_val, 0, sizeof(*arg_val));
352 }
353 break;
354 case PSI_LET_CALLOC:
355 arg_val->ptr = psi_do_calloc(let->val->data.alloc);
356 darg->mem = arg_val->ptr;
357 break;
358 case PSI_LET_NUMEXP:
359 arg_val->zend.lval = psi_long_num_exp(let->val->data.num, NULL);
360 break;
361 case PSI_LET_FUNC:
362 iarg = let->val->data.func->arg;
363
364 if (!(darg->ptr = psi_let_val(let->val->data.func->type, iarg, darg->ptr, real_decl_type(darg->type)->strct, &darg->mem))) {
365 return NULL;
366 }
367 }
368
369 if (let->val && let->val->flags.one.is_reference) {
370 return let->ptr = &darg->ptr;
371 } else {
372 return let->ptr = darg->ptr;
373 }
374 }
375
376 static inline void psi_do_set(zval *return_value, set_value *set)
377 {
378 decl_arg *set_arg = set->vars->vars[0]->arg;
379
380 zval_dtor(return_value);
381 set->func->handler(return_value, set, set_arg->let ? set_arg->let->ptr : set_arg->ptr);
382 }
383
384 static inline void psi_do_return(zval *return_value, return_stmt *ret)
385 {
386 ret->set->func->handler(return_value, ret->set, ret->set->vars->vars[0]->arg->ptr);
387 }
388
389 static inline void psi_do_free(free_stmt *fre)
390 {
391 size_t i, j;
392
393 for (i = 0; i < fre->calls->count; ++i) {
394 free_call *f = fre->calls->list[i];
395
396 for (j = 0; j < f->vars->count; ++j) {
397 decl_var *dvar = f->vars->vars[j];
398 decl_arg *darg = dvar->arg;
399 impl_val *fval = darg->let ? darg->let->ptr : darg->ptr;
400
401 f->decl->call.args[j] = deref_impl_val(fval, dvar);
402 }
403
404 /* FIXME: check in validate_* that free functions return scalar */
405 PSI_ContextCall(&PSI_G(context), &f->decl->call, NULL);
406 }
407 }
408
409 static inline void psi_clean_array_struct(decl_arg *darg) {
410 if (darg->let
411 && darg->let->val->kind == PSI_LET_FUNC
412 && darg->let->val->data.func->type == PSI_T_ARRVAL) {
413 decl_type *type = real_decl_type(darg->type);
414
415 if (type->type == PSI_T_STRUCT) {
416 void **ptr = (void **) ((char *) darg->mem + type->strct->size);
417
418 while (*ptr) {
419 efree(*ptr++);
420 }
421 }
422 }
423 }
424
425 static inline void psi_do_clean(impl *impl)
426 {
427 size_t i;
428
429 if (impl->decl->func->ptr != &impl->decl->func->val) {
430 efree(impl->decl->func->ptr);
431 impl->decl->func->ptr = &impl->decl->func->val;
432 }
433 for (i = 0; i < impl->func->args->count; ++i ) {
434 impl_arg *iarg = impl->func->args->args[i];
435
436 switch (iarg->type->type) {
437 case PSI_T_STRING:
438 if (iarg->val.zend.str) {
439 zend_string_release(iarg->val.zend.str);
440 }
441 break;
442 }
443 }
444
445 if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) {
446 decl_arg *darg = impl->decl->args->args[i];
447
448 if (darg->mem) {
449 psi_clean_array_struct(darg);
450 efree(darg->mem);
451 darg->mem = NULL;
452 }
453 darg->ptr = &darg->val;
454 }
455
456 if (impl->func->args->vararg.args) {
457 free_impl_args(impl->func->args->vararg.args);
458 impl->func->args->vararg.args = NULL;
459 }
460 if (impl->func->args->vararg.types) {
461 efree(impl->func->args->vararg.types);
462 impl->func->args->vararg.types = NULL;
463 }
464 if (impl->func->args->vararg.values) {
465 efree(impl->func->args->vararg.values);
466 impl->func->args->vararg.values = NULL;
467 }
468 if (impl->func->args->vararg.free_list) {
469 void **list = impl->func->args->vararg.free_list;
470
471 while (*list) {
472 efree(*list++);
473 }
474
475 efree(impl->func->args->vararg.free_list);
476 impl->func->args->vararg.free_list = NULL;
477 }
478 }
479
480
481 static inline void psi_do_args(impl *impl) {
482 size_t i;
483
484 for (i = 0; i < impl->decl->args->count; ++i) {
485 impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
486 }
487
488 if (!impl->decl->func->var->pointer_level) {
489 decl_type *real = real_decl_type(impl->decl->func->type);
490
491 switch (real->type) {
492 case PSI_T_STRUCT:
493 impl->decl->func->ptr = psi_array_to_struct(real->strct, NULL);
494 break;
495 }
496 }
497 }
498
499 static inline impl_vararg *psi_do_varargs(impl *impl) {
500 size_t i, j;
501 impl_vararg *va = &impl->func->args->vararg;
502 size_t vacount = va->args->count;
503
504
505 if (!vacount) {
506 return NULL;
507 }
508
509 va->types = ecalloc(vacount, sizeof(*va->types));
510 va->values = ecalloc(vacount, sizeof(*va->values));
511
512 for (i = 0, j = 0; i < vacount; ++i) {
513 impl_arg *vaarg = va->args->args[i];
514 void *to_free = NULL;
515 token_t let_fn, vatype = va->name->type->type;
516
517 if (vatype == PSI_T_MIXED) {
518 switch (Z_TYPE_P(vaarg->_zv)) {
519 case IS_TRUE:
520 case IS_FALSE: vatype = PSI_T_BOOL; break;
521 case IS_LONG: vatype = PSI_T_INT; break;
522 case IS_DOUBLE: vatype = PSI_T_FLOAT; break;
523 default: vatype = PSI_T_STRING; break;
524 }
525 }
526
527
528 switch (vatype) {
529 case PSI_T_BOOL: let_fn = PSI_T_BOOLVAL; break;
530 case PSI_T_INT: let_fn = PSI_T_INTVAL; break;
531 case PSI_T_FLOAT:
532 case PSI_T_DOUBLE: let_fn = PSI_T_FLOATVAL;break;
533 case PSI_T_STRING: let_fn = PSI_T_STRVAL; break;
534 EMPTY_SWITCH_DEFAULT_CASE();
535 }
536
537 va->types[i] = vatype;
538 /* FIXME: varargs with struct-by-value :) */
539 if (!psi_let_val(let_fn, vaarg, &va->values[i], NULL, &to_free)) {
540 return NULL;
541 }
542
543 if (to_free) {
544 if (!va->free_list) {
545 va->free_list = ecalloc(vacount - i + 1, sizeof(*va->free_list));
546 }
547 va->free_list[j++] = to_free;
548 }
549 }
550
551 return va;
552 }
553
554 void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
555 {
556 size_t i;
557 impl_vararg *va = NULL;
558
559 memset(impl->decl->func->ptr, 0, sizeof(impl_val));
560
561 if (SUCCESS != psi_parse_args(execute_data, impl)) {
562 return;
563 }
564
565 for (i = 0; i < impl->stmts->let.count; ++i) {
566 let_stmt *let = impl->stmts->let.list[i];
567
568 if (!psi_do_let(let)) {
569 psi_do_return(return_value, impl->stmts->ret.list[0]);
570 psi_do_clean(impl);
571 return;
572 }
573 }
574
575 if (impl->decl->args) {
576 psi_do_args(impl);
577
578 if (impl->func->args->vararg.args) {
579 va = psi_do_varargs(impl);
580 }
581 }
582
583 PSI_ContextCall(&PSI_G(context), &impl->decl->call, va);
584 psi_do_return(return_value, impl->stmts->ret.list[0]);
585
586 for (i = 0; i < impl->stmts->set.count; ++i) {
587 set_stmt *set = impl->stmts->set.list[i];
588
589 if (set->arg->_zv) {
590 psi_do_set(set->arg->_zv, set->val);
591 }
592 }
593
594 for (i = 0; i < impl->stmts->fre.count; ++i) {
595 free_stmt *fre = impl->stmts->fre.list[i];
596
597 psi_do_free(fre);
598 }
599 psi_do_clean(impl);
600 }