1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 Zend Technologies Ltd. (http://www.zend.com) |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 2.00 of the Zend license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.zend.com/license/2_00.txt. |
11 : | If you did not receive a copy of the Zend license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@zend.com so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Andi Gutmans <andi@zend.com> |
16 : | Zeev Suraski <zeev@zend.com> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: zend_opcode.c 281736 2009-06-05 23:20:58Z shire $ */
21 :
22 : #include <stdio.h>
23 :
24 : #include "zend.h"
25 : #include "zend_alloc.h"
26 : #include "zend_compile.h"
27 : #include "zend_extensions.h"
28 : #include "zend_API.h"
29 :
30 : #include "zend_vm.h"
31 :
32 : extern int zend_spprintf(char **message, int max_len, char *format, ...);
33 :
34 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC) /* {{{ */
35 0 : {
36 0 : if (extension->op_array_ctor) {
37 0 : extension->op_array_ctor(op_array);
38 : }
39 0 : }
40 : /* }}} */
41 :
42 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC) /* {{{ */
43 0 : {
44 0 : if (extension->op_array_dtor) {
45 0 : extension->op_array_dtor(op_array);
46 : }
47 0 : }
48 : /* }}} */
49 :
50 : static void op_array_alloc_ops(zend_op_array *op_array) /* {{{ */
51 55970 : {
52 55970 : op_array->opcodes = erealloc(op_array->opcodes, (op_array->size)*sizeof(zend_op));
53 55970 : }
54 : /* }}} */
55 :
56 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC) /* {{{ */
57 48736 : {
58 48736 : op_array->type = type;
59 :
60 48736 : op_array->backpatch_count = 0;
61 48736 : if (CG(interactive)) {
62 : /* We must avoid a realloc() on the op_array in interactive mode, since pointers to constants
63 : * will become invalid
64 : */
65 0 : initial_ops_size = 8192;
66 : }
67 :
68 48736 : op_array->refcount = (zend_uint *) emalloc(sizeof(zend_uint));
69 48736 : *op_array->refcount = 1;
70 48736 : op_array->size = initial_ops_size;
71 48736 : op_array->last = 0;
72 48736 : op_array->opcodes = NULL;
73 48736 : op_array_alloc_ops(op_array);
74 :
75 48736 : op_array->size_var = 0; /* FIXME:??? */
76 48736 : op_array->last_var = 0;
77 48736 : op_array->vars = NULL;
78 :
79 48736 : op_array->T = 0;
80 :
81 48736 : op_array->function_name.v = NULL;
82 48736 : op_array->filename = zend_get_compiled_filename(TSRMLS_C);
83 48736 : op_array->script_encoding = zend_get_compiled_script_encoding(TSRMLS_C);
84 48736 : op_array->doc_comment.v = NULL;
85 48736 : op_array->doc_comment_len = 0;
86 :
87 48736 : op_array->arg_info = NULL;
88 48736 : op_array->num_args = 0;
89 48736 : op_array->required_num_args = 0;
90 :
91 48736 : op_array->scope = NULL;
92 :
93 48736 : op_array->brk_cont_array = NULL;
94 48736 : op_array->try_catch_array = NULL;
95 48736 : op_array->last_brk_cont = 0;
96 48736 : op_array->current_brk_cont = -1;
97 :
98 48736 : op_array->static_variables = NULL;
99 48736 : op_array->last_try_catch = 0;
100 :
101 48736 : op_array->return_reference = 0;
102 48736 : op_array->done_pass_two = 0;
103 :
104 48736 : op_array->this_var = -1;
105 :
106 48736 : op_array->start_op = NULL;
107 :
108 48736 : op_array->fn_flags = CG(interactive)?ZEND_ACC_INTERACTIVE:0;
109 :
110 48736 : op_array->early_binding = -1;
111 :
112 48736 : memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
113 :
114 48736 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
115 48736 : }
116 : /* }}} */
117 :
118 : ZEND_API void destroy_zend_function(zend_function *function TSRMLS_DC) /* {{{ */
119 74104408 : {
120 74104408 : switch (function->type) {
121 : case ZEND_USER_FUNCTION:
122 31596 : destroy_op_array((zend_op_array *) function TSRMLS_CC);
123 : break;
124 : case ZEND_INTERNAL_FUNCTION:
125 : /* do nothing */
126 : break;
127 : }
128 74104408 : }
129 : /* }}} */
130 :
131 : ZEND_API void zend_function_dtor(zend_function *function) /* {{{ */
132 0 : {
133 : TSRMLS_FETCH();
134 :
135 0 : destroy_zend_function(function TSRMLS_CC);
136 0 : }
137 : /* }}} */
138 :
139 : ZEND_API void zend_u_function_dtor(zend_function *function) /* {{{ */
140 74104408 : {
141 : TSRMLS_FETCH();
142 :
143 74104408 : destroy_zend_function(function TSRMLS_CC);
144 74104408 : if (function->type == ZEND_INTERNAL_FUNCTION) {
145 74072812 : if (function->common.function_name.v) {
146 74072812 : free(function->common.function_name.v);
147 : }
148 74072812 : if (function->common.arg_info) {
149 61038223 : int n = function->common.num_args;
150 :
151 211296391 : while (n > 0) {
152 89219945 : --n;
153 89219945 : if (function->common.arg_info[n].name.v) {
154 89100671 : free(function->common.arg_info[n].name.v);
155 : }
156 89219945 : if (function->common.arg_info[n].class_name.v) {
157 2658163 : free(function->common.arg_info[n].class_name.v);
158 : }
159 : }
160 61038223 : free(function->common.arg_info);
161 : }
162 : }
163 74104408 : }
164 : /* }}} */
165 :
166 : static void zend_cleanup_op_array_data(zend_op_array *op_array) /* {{{ */
167 30879 : {
168 30879 : if (op_array->static_variables) {
169 910 : zend_hash_clean(op_array->static_variables);
170 : }
171 30879 : }
172 : /* }}} */
173 :
174 : ZEND_API int zend_cleanup_function_data(zend_function *function TSRMLS_DC) /* {{{ */
175 27335 : {
176 27335 : if (function->type == ZEND_USER_FUNCTION) {
177 10310 : zend_cleanup_op_array_data((zend_op_array *) function);
178 10310 : return ZEND_HASH_APPLY_KEEP;
179 : } else {
180 17025 : return ZEND_HASH_APPLY_STOP;
181 : }
182 : }
183 : /* }}} */
184 :
185 : ZEND_API int zend_cleanup_function_data_full(zend_function *function TSRMLS_DC) /* {{{ */
186 91824 : {
187 91824 : if (function->type == ZEND_USER_FUNCTION) {
188 20569 : zend_cleanup_op_array_data((zend_op_array *) function);
189 : }
190 91824 : return 0;
191 : }
192 : /* }}} */
193 :
194 : ZEND_API int zend_cleanup_class_data(zend_class_entry **pce TSRMLS_DC) /* {{{ */
195 2663308 : {
196 2663308 : if ((*pce)->type == ZEND_USER_CLASS) {
197 : /* Clean all parts that can contain run-time data */
198 : /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
199 : not contain objects and thus are not probelmatic */
200 7408 : zend_hash_apply(&(*pce)->function_table, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
201 7408 : (*pce)->static_members = NULL;
202 2655900 : } else if (CE_STATIC_MEMBERS(*pce)) {
203 10423 : zend_hash_destroy(CE_STATIC_MEMBERS(*pce));
204 10423 : FREE_HASHTABLE(CE_STATIC_MEMBERS(*pce));
205 : #ifdef ZTS
206 : CG(static_members)[(zend_intptr_t)((*pce)->static_members)] = NULL;
207 : #else
208 10423 : (*pce)->static_members = NULL;
209 : #endif
210 : }
211 2663308 : return 0;
212 : }
213 : /* }}} */
214 :
215 : ZEND_API void destroy_zend_class(zend_class_entry **pce) /* {{{ */
216 2670086 : {
217 2670086 : zend_class_entry *ce = *pce;
218 :
219 2670086 : if (--ce->refcount > 0) {
220 5948 : return;
221 : }
222 2664138 : switch (ce->type) {
223 : case ZEND_USER_CLASS:
224 6053 : zend_hash_destroy(&ce->default_properties);
225 6053 : zend_hash_destroy(&ce->properties_info);
226 6053 : zend_hash_destroy(&ce->default_static_members);
227 6053 : efree(ce->name.v);
228 6053 : zend_hash_destroy(&ce->function_table);
229 6053 : zend_hash_destroy(&ce->constants_table);
230 6053 : if (ce->num_interfaces > 0 && ce->interfaces) {
231 469 : efree(ce->interfaces);
232 : }
233 6053 : if (ce->doc_comment.v) {
234 11 : efree(ce->doc_comment.v);
235 : }
236 6053 : efree(ce);
237 6053 : break;
238 : case ZEND_INTERNAL_CLASS:
239 2658085 : zend_hash_destroy(&ce->default_properties);
240 2658085 : zend_hash_destroy(&ce->properties_info);
241 2658085 : zend_hash_destroy(&ce->default_static_members);
242 2658085 : free(ce->name.v);
243 2658085 : zend_hash_destroy(&ce->function_table);
244 2658085 : zend_hash_destroy(&ce->constants_table);
245 2658085 : if (ce->num_interfaces > 0) {
246 954184 : free(ce->interfaces);
247 : }
248 2658085 : if (ce->doc_comment.v) {
249 0 : free(ce->doc_comment.v);
250 : }
251 2658085 : free(ce);
252 : break;
253 : }
254 : }
255 : /* }}} */
256 :
257 : void zend_class_add_ref(zend_class_entry **ce) /* {{{ */
258 0 : {
259 0 : (*ce)->refcount++;
260 0 : }
261 : /* }}} */
262 :
263 : ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */
264 55456 : {
265 55456 : zend_op *opline = op_array->opcodes;
266 55456 : zend_op *end = op_array->opcodes+op_array->last;
267 : zend_uint i;
268 :
269 55456 : if (op_array->static_variables) {
270 952 : zend_hash_destroy(op_array->static_variables);
271 952 : FREE_HASHTABLE(op_array->static_variables);
272 : }
273 :
274 55456 : if (--(*op_array->refcount)>0) {
275 8062 : return;
276 : }
277 :
278 47394 : efree(op_array->refcount);
279 :
280 47394 : if (op_array->vars) {
281 34443 : i = op_array->last_var;
282 286147 : while (i > 0) {
283 217261 : i--;
284 217261 : efree(op_array->vars[i].name.v);
285 : }
286 34443 : efree(op_array->vars);
287 : }
288 :
289 1738311 : while (opline<end) {
290 1643523 : if (opline->op1.op_type==IS_CONST) {
291 : #if DEBUG_ZEND>2
292 : printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op1.u.constant);
293 : #endif
294 690939 : zval_dtor(&opline->op1.u.constant);
295 : }
296 1643523 : if (opline->op2.op_type==IS_CONST) {
297 : #if DEBUG_ZEND>2
298 : printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op2.u.constant);
299 : #endif
300 223211 : zval_dtor(&opline->op2.u.constant);
301 : }
302 1643523 : opline++;
303 : }
304 47394 : efree(op_array->opcodes);
305 :
306 47394 : if (op_array->function_name.v) {
307 23644 : efree(op_array->function_name.v);
308 : }
309 47394 : if (op_array->doc_comment.v) {
310 1930 : efree(op_array->doc_comment.v);
311 : }
312 47394 : if (op_array->brk_cont_array) {
313 8457 : efree(op_array->brk_cont_array);
314 : }
315 47394 : if (op_array->try_catch_array) {
316 1311 : efree(op_array->try_catch_array);
317 : }
318 47394 : if (op_array->done_pass_two) {
319 47370 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array TSRMLS_CC);
320 : }
321 47394 : if (op_array->arg_info) {
322 126946 : for (i=0; i<op_array->num_args; i++) {
323 110331 : efree(op_array->arg_info[i].name.v);
324 110331 : if (op_array->arg_info[i].class_name.v) {
325 130 : efree(op_array->arg_info[i].class_name.v);
326 : }
327 : }
328 16615 : efree(op_array->arg_info);
329 : }
330 : }
331 : /* }}} */
332 :
333 : void init_op(zend_op *op TSRMLS_DC) /* {{{ */
334 1818081 : {
335 1818081 : memset(op, 0, sizeof(zend_op));
336 1818081 : op->lineno = CG(zend_lineno);
337 1818081 : SET_UNUSED(op->result);
338 1818081 : }
339 : /* }}} */
340 :
341 : zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC) /* {{{ */
342 1732720 : {
343 1732720 : zend_uint next_op_num = op_array->last++;
344 : zend_op *next_op;
345 :
346 1732720 : if (next_op_num >= op_array->size) {
347 7234 : if (op_array->fn_flags & ZEND_ACC_INTERACTIVE) {
348 : /* we messed up */
349 0 : zend_printf("Ran out of opcode space!\n"
350 : "You should probably consider writing this huge script into a file!\n");
351 0 : zend_bailout();
352 : }
353 7234 : op_array->size *= 4;
354 7234 : op_array_alloc_ops(op_array);
355 : }
356 :
357 1732720 : next_op = &(op_array->opcodes[next_op_num]);
358 :
359 1732720 : init_op(next_op TSRMLS_CC);
360 :
361 1732720 : return next_op;
362 : }
363 : /* }}} */
364 :
365 : int get_next_op_number(zend_op_array *op_array) /* {{{ */
366 703383 : {
367 703383 : return op_array->last;
368 : }
369 : /* }}} */
370 :
371 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array) /* {{{ */
372 12176 : {
373 12176 : op_array->last_brk_cont++;
374 12176 : op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
375 12176 : return &op_array->brk_cont_array[op_array->last_brk_cont-1];
376 : }
377 : /* }}} */
378 :
379 : static void zend_update_extended_info(zend_op_array *op_array TSRMLS_DC) /* {{{ */
380 0 : {
381 0 : zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
382 :
383 0 : while (opline<end) {
384 0 : if (opline->opcode == ZEND_EXT_STMT) {
385 0 : if (opline+1<end) {
386 0 : if ((opline+1)->opcode == ZEND_EXT_STMT) {
387 0 : opline->opcode = ZEND_NOP;
388 0 : opline++;
389 0 : continue;
390 : }
391 0 : if (opline+1<end) {
392 0 : opline->lineno = (opline+1)->lineno;
393 : }
394 : } else {
395 0 : opline->opcode = ZEND_NOP;
396 : }
397 : }
398 0 : opline++;
399 : }
400 0 : }
401 : /* }}} */
402 :
403 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC) /* {{{ */
404 0 : {
405 0 : if (extension->op_array_handler) {
406 0 : extension->op_array_handler(op_array);
407 : }
408 0 : }
409 : /* }}} */
410 :
411 : ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */
412 48534 : {
413 : zend_op *opline, *end;
414 :
415 48534 : if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
416 0 : return 0;
417 : }
418 48534 : if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
419 0 : zend_update_extended_info(op_array TSRMLS_CC);
420 : }
421 48534 : if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
422 48368 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
423 : }
424 :
425 48534 : if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && op_array->size != op_array->last) {
426 48431 : op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
427 48431 : op_array->size = op_array->last;
428 : }
429 :
430 48534 : opline = op_array->opcodes;
431 48534 : end = opline + op_array->last;
432 1827576 : while (opline < end) {
433 1730511 : if (opline->op1.op_type == IS_CONST) {
434 766177 : Z_SET_ISREF(opline->op1.u.constant);
435 766177 : Z_SET_REFCOUNT(opline->op1.u.constant, 2); /* Make sure is_ref won't be reset */
436 : }
437 1730511 : if (opline->op2.op_type == IS_CONST) {
438 227136 : Z_SET_ISREF(opline->op2.u.constant);
439 227136 : Z_SET_REFCOUNT(opline->op2.u.constant, 2);
440 : }
441 1730511 : switch (opline->opcode) {
442 : case ZEND_GOTO:
443 16 : if (Z_TYPE(opline->op2.u.constant) != IS_LONG) {
444 15 : zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
445 : }
446 : /* break omitted intentionally */
447 : case ZEND_JMP:
448 90113 : opline->op1.u.jmp_addr = &op_array->opcodes[opline->op1.u.opline_num];
449 90113 : break;
450 : case ZEND_JMPZ:
451 : case ZEND_JMPNZ:
452 : case ZEND_JMPZ_EX:
453 : case ZEND_JMPNZ_EX:
454 : case ZEND_JMP_SET:
455 81558 : opline->op2.u.jmp_addr = &op_array->opcodes[opline->op2.u.opline_num];
456 : break;
457 : }
458 1730508 : ZEND_VM_SET_OPCODE_HANDLER(opline);
459 1730508 : opline++;
460 : }
461 :
462 48531 : op_array->done_pass_two = 1;
463 48531 : return 0;
464 : }
465 : /* }}} */
466 :
467 : int print_class(zend_class_entry *class_entry TSRMLS_DC) /* {{{ */
468 0 : {
469 : char *tmp;
470 :
471 0 : zend_spprintf(&tmp, 0, "Class %v:\n", class_entry->name);
472 0 : printf("%s", tmp);
473 0 : efree(tmp);
474 0 : zend_hash_apply(&class_entry->function_table, (apply_func_t) pass_two TSRMLS_CC);
475 0 : zend_spprintf(&tmp, 0, "End of class %v.\n\n", class_entry->name);
476 0 : printf("%s", tmp);
477 0 : efree(tmp);
478 0 : return 0;
479 : }
480 : /* }}} */
481 :
482 : ZEND_API unary_op_type get_unary_op(int opcode) /* {{{ */
483 0 : {
484 0 : switch (opcode) {
485 : case ZEND_BW_NOT:
486 0 : return (unary_op_type) bitwise_not_function;
487 : break;
488 : case ZEND_BOOL_NOT:
489 0 : return (unary_op_type) boolean_not_function;
490 : break;
491 : default:
492 0 : return (unary_op_type) NULL;
493 : break;
494 : }
495 : }
496 : /* }}} */
497 :
498 : ZEND_API binary_op_type get_binary_op(int opcode) /* {{{ */
499 0 : {
500 0 : switch (opcode) {
501 : case ZEND_ADD:
502 : case ZEND_ASSIGN_ADD:
503 0 : return (binary_op_type) add_function;
504 : break;
505 : case ZEND_SUB:
506 : case ZEND_ASSIGN_SUB:
507 0 : return (binary_op_type) sub_function;
508 : break;
509 : case ZEND_MUL:
510 : case ZEND_ASSIGN_MUL:
511 0 : return (binary_op_type) mul_function;
512 : break;
513 : case ZEND_DIV:
514 : case ZEND_ASSIGN_DIV:
515 0 : return (binary_op_type) div_function;
516 : break;
517 : case ZEND_MOD:
518 : case ZEND_ASSIGN_MOD:
519 0 : return (binary_op_type) mod_function;
520 : break;
521 : case ZEND_SL:
522 : case ZEND_ASSIGN_SL:
523 0 : return (binary_op_type) shift_left_function;
524 : break;
525 : case ZEND_SR:
526 : case ZEND_ASSIGN_SR:
527 0 : return (binary_op_type) shift_right_function;
528 : break;
529 : case ZEND_CONCAT:
530 : case ZEND_ASSIGN_CONCAT:
531 0 : return (binary_op_type) concat_function;
532 : break;
533 : case ZEND_IS_IDENTICAL:
534 0 : return (binary_op_type) is_identical_function;
535 : break;
536 : case ZEND_IS_NOT_IDENTICAL:
537 0 : return (binary_op_type) is_not_identical_function;
538 : break;
539 : case ZEND_IS_EQUAL:
540 0 : return (binary_op_type) is_equal_function;
541 : break;
542 : case ZEND_IS_NOT_EQUAL:
543 0 : return (binary_op_type) is_not_equal_function;
544 : break;
545 : case ZEND_IS_SMALLER:
546 0 : return (binary_op_type) is_smaller_function;
547 : break;
548 : case ZEND_IS_SMALLER_OR_EQUAL:
549 0 : return (binary_op_type) is_smaller_or_equal_function;
550 : break;
551 : case ZEND_BW_OR:
552 : case ZEND_ASSIGN_BW_OR:
553 0 : return (binary_op_type) bitwise_or_function;
554 : break;
555 : case ZEND_BW_AND:
556 : case ZEND_ASSIGN_BW_AND:
557 0 : return (binary_op_type) bitwise_and_function;
558 : break;
559 : case ZEND_BW_XOR:
560 : case ZEND_ASSIGN_BW_XOR:
561 0 : return (binary_op_type) bitwise_xor_function;
562 : break;
563 : case ZEND_BOOL_XOR:
564 0 : return (binary_op_type) boolean_xor_function;
565 : break;
566 : default:
567 0 : return (binary_op_type) NULL;
568 : break;
569 : }
570 : }
571 : /* }}} */
572 :
573 : /*
574 : * Local variables:
575 : * tab-width: 4
576 : * c-basic-offset: 4
577 : * indent-tabs-mode: t
578 : * End:
579 : */
|