1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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$ */
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
32static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
33{
34    if (extension->op_array_ctor) {
35        extension->op_array_ctor(op_array);
36    }
37}
38
39static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
40{
41    if (extension->op_array_dtor) {
42        extension->op_array_dtor(op_array);
43    }
44}
45
46static void op_array_alloc_ops(zend_op_array *op_array, uint32_t size)
47{
48    op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
49}
50
51void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size)
52{
53    op_array->type = type;
54
55    op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
56    *op_array->refcount = 1;
57    op_array->last = 0;
58    op_array->opcodes = NULL;
59    op_array_alloc_ops(op_array, initial_ops_size);
60
61    op_array->last_var = 0;
62    op_array->vars = NULL;
63
64    op_array->T = 0;
65
66    op_array->function_name = NULL;
67    op_array->filename = zend_get_compiled_filename();
68    op_array->doc_comment = NULL;
69
70    op_array->arg_info = NULL;
71    op_array->num_args = 0;
72    op_array->required_num_args = 0;
73
74    op_array->scope = NULL;
75    op_array->prototype = NULL;
76
77    op_array->brk_cont_array = NULL;
78    op_array->try_catch_array = NULL;
79    op_array->last_brk_cont = 0;
80
81    op_array->static_variables = NULL;
82    op_array->last_try_catch = 0;
83
84    op_array->this_var = -1;
85
86    op_array->fn_flags = 0;
87
88    op_array->early_binding = -1;
89
90    op_array->last_literal = 0;
91    op_array->literals = NULL;
92
93    op_array->run_time_cache = NULL;
94    op_array->cache_size = 0;
95
96    memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
97
98    zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array);
99}
100
101ZEND_API void destroy_zend_function(zend_function *function)
102{
103    if (function->type == ZEND_USER_FUNCTION) {
104        destroy_op_array(&function->op_array);
105    } else {
106        ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
107        ZEND_ASSERT(function->common.function_name);
108        zend_string_release(function->common.function_name);
109    }
110}
111
112ZEND_API void zend_function_dtor(zval *zv)
113{
114    zend_function *function = Z_PTR_P(zv);
115
116    if (function->type == ZEND_USER_FUNCTION) {
117        ZEND_ASSERT(function->common.function_name);
118        destroy_op_array(&function->op_array);
119        /* op_arrays are allocated on arena, so we don't have to free them */
120    } else {
121        ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
122        ZEND_ASSERT(function->common.function_name);
123        zend_string_release(function->common.function_name);
124        if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
125            pefree(function, 1);
126        }
127    }
128}
129
130ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array)
131{
132    if (op_array->static_variables &&
133        !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
134        zend_hash_clean(op_array->static_variables);
135    }
136}
137
138ZEND_API void zend_cleanup_user_class_data(zend_class_entry *ce)
139{
140    /* Clean all parts that can contain run-time data */
141    /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
142       not contain objects and thus are not probelmatic */
143    if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
144        zend_function *func;
145
146        ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
147            if (func->type == ZEND_USER_FUNCTION) {
148                zend_cleanup_op_array_data((zend_op_array *) func);
149            }
150        } ZEND_HASH_FOREACH_END();
151    }
152    if (ce->static_members_table) {
153        zval *static_members = ce->static_members_table;
154        int count = ce->default_static_members_count;
155        int i;
156
157        ce->default_static_members_count = 0;
158        ce->default_static_members_table = ce->static_members_table = NULL;
159        for (i = 0; i < count; i++) {
160            zval_ptr_dtor(&static_members[i]);
161        }
162        efree(static_members);
163    }
164}
165
166ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
167{
168    if (CE_STATIC_MEMBERS(ce)) {
169        zval *static_members = CE_STATIC_MEMBERS(ce);
170        int i;
171
172#ifdef ZTS
173        CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
174#else
175        ce->static_members_table = NULL;
176#endif
177        for (i = 0; i < ce->default_static_members_count; i++) {
178            zval_ptr_dtor(&static_members[i]);
179        }
180        efree(static_members);
181    }
182}
183
184void _destroy_zend_class_traits_info(zend_class_entry *ce)
185{
186    if (ce->num_traits > 0 && ce->traits) {
187        efree(ce->traits);
188    }
189
190    if (ce->trait_aliases) {
191        size_t i = 0;
192        while (ce->trait_aliases[i]) {
193            if (ce->trait_aliases[i]->trait_method) {
194                if (ce->trait_aliases[i]->trait_method->method_name) {
195                    zend_string_release(ce->trait_aliases[i]->trait_method->method_name);
196                }
197                if (ce->trait_aliases[i]->trait_method->class_name) {
198                    zend_string_release(ce->trait_aliases[i]->trait_method->class_name);
199                }
200                efree(ce->trait_aliases[i]->trait_method);
201            }
202
203            if (ce->trait_aliases[i]->alias) {
204                zend_string_release(ce->trait_aliases[i]->alias);
205            }
206
207            efree(ce->trait_aliases[i]);
208            i++;
209        }
210
211        efree(ce->trait_aliases);
212    }
213
214    if (ce->trait_precedences) {
215        size_t i = 0;
216
217        while (ce->trait_precedences[i]) {
218            zend_string_release(ce->trait_precedences[i]->trait_method->method_name);
219            zend_string_release(ce->trait_precedences[i]->trait_method->class_name);
220            efree(ce->trait_precedences[i]->trait_method);
221
222            if (ce->trait_precedences[i]->exclude_from_classes) {
223                size_t j = 0;
224                zend_trait_precedence *cur_precedence = ce->trait_precedences[i];
225                while (cur_precedence->exclude_from_classes[j].class_name) {
226                    zend_string_release(cur_precedence->exclude_from_classes[j].class_name);
227                    j++;
228                }
229                efree(ce->trait_precedences[i]->exclude_from_classes);
230            }
231            efree(ce->trait_precedences[i]);
232            i++;
233        }
234        efree(ce->trait_precedences);
235    }
236}
237
238ZEND_API void destroy_zend_class(zval *zv)
239{
240    zend_property_info *prop_info;
241    zend_class_entry *ce = Z_PTR_P(zv);
242
243    if (--ce->refcount > 0) {
244        return;
245    }
246    switch (ce->type) {
247        case ZEND_USER_CLASS:
248            if (ce->default_properties_table) {
249                int i;
250
251                for (i = 0; i < ce->default_properties_count; i++) {
252                    if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
253                        zval_ptr_dtor(&ce->default_properties_table[i]);
254                    }
255                }
256                efree(ce->default_properties_table);
257            }
258            if (ce->default_static_members_table) {
259                int i;
260
261                for (i = 0; i < ce->default_static_members_count; i++) {
262                    if (Z_TYPE(ce->default_static_members_table[i]) != IS_UNDEF) {
263                        zval_ptr_dtor(&ce->default_static_members_table[i]);
264                    }
265                }
266                efree(ce->default_static_members_table);
267            }
268            ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
269                if (prop_info->ce == ce || (prop_info->flags & ZEND_ACC_SHADOW)) {
270                    zend_string_release(prop_info->name);
271                    if (prop_info->doc_comment) {
272                        zend_string_release(prop_info->doc_comment);
273                    }
274                }
275            } ZEND_HASH_FOREACH_END();
276            zend_hash_destroy(&ce->properties_info);
277            zend_string_release(ce->name);
278            zend_hash_destroy(&ce->function_table);
279            zend_hash_destroy(&ce->constants_table);
280            if (ce->num_interfaces > 0 && ce->interfaces) {
281                efree(ce->interfaces);
282            }
283            if (ce->info.user.doc_comment) {
284                zend_string_release(ce->info.user.doc_comment);
285            }
286
287            _destroy_zend_class_traits_info(ce);
288
289            break;
290        case ZEND_INTERNAL_CLASS:
291            if (ce->default_properties_table) {
292                int i;
293
294                for (i = 0; i < ce->default_properties_count; i++) {
295                    if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
296                        zval_internal_ptr_dtor(&ce->default_properties_table[i]);
297                    }
298                }
299                free(ce->default_properties_table);
300            }
301            if (ce->default_static_members_table) {
302                int i;
303
304                for (i = 0; i < ce->default_static_members_count; i++) {
305                    zval_internal_ptr_dtor(&ce->default_static_members_table[i]);
306                }
307                free(ce->default_static_members_table);
308            }
309            zend_hash_destroy(&ce->properties_info);
310            zend_string_release(ce->name);
311            zend_hash_destroy(&ce->function_table);
312            zend_hash_destroy(&ce->constants_table);
313            if (ce->num_interfaces > 0) {
314                free(ce->interfaces);
315            }
316            free(ce);
317            break;
318    }
319}
320
321void zend_class_add_ref(zval *zv)
322{
323    zend_class_entry *ce = Z_PTR_P(zv);
324
325    ce->refcount++;
326}
327
328ZEND_API void destroy_op_array(zend_op_array *op_array)
329{
330    zval *literal = op_array->literals;
331    zval *end;
332    uint32_t i;
333
334    if (op_array->static_variables &&
335        !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
336        if (--GC_REFCOUNT(op_array->static_variables) == 0) {
337            zend_array_destroy(op_array->static_variables);
338        }
339    }
340
341    if (op_array->run_time_cache && !op_array->function_name) {
342        efree(op_array->run_time_cache);
343    }
344
345    if (!op_array->refcount || --(*op_array->refcount)>0) {
346        return;
347    }
348
349    efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
350
351    if (op_array->vars) {
352        i = op_array->last_var;
353        while (i > 0) {
354            i--;
355            zend_string_release(op_array->vars[i]);
356        }
357        efree(op_array->vars);
358    }
359
360    if (literal) {
361        end = literal + op_array->last_literal;
362        while (literal < end) {
363            zval_ptr_dtor_nogc(literal);
364            literal++;
365        }
366        efree(op_array->literals);
367    }
368    efree(op_array->opcodes);
369
370    if (op_array->function_name) {
371        zend_string_release(op_array->function_name);
372    }
373    if (op_array->doc_comment) {
374        zend_string_release(op_array->doc_comment);
375    }
376    if (op_array->brk_cont_array) {
377        efree(op_array->brk_cont_array);
378    }
379    if (op_array->try_catch_array) {
380        efree(op_array->try_catch_array);
381    }
382    if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
383        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
384    }
385    if (op_array->arg_info) {
386        int32_t num_args = op_array->num_args;
387        zend_arg_info *arg_info = op_array->arg_info;
388        int32_t i;
389
390        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
391            arg_info--;
392            num_args++;
393        }
394        if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
395            num_args++;
396        }
397        for (i = 0 ; i < num_args; i++) {
398            if (arg_info[i].name) {
399                zend_string_release(arg_info[i].name);
400            }
401            if (arg_info[i].class_name) {
402                zend_string_release(arg_info[i].class_name);
403            }
404        }
405        efree(arg_info);
406    }
407}
408
409void init_op(zend_op *op)
410{
411    memset(op, 0, sizeof(zend_op));
412    op->lineno = CG(zend_lineno);
413    SET_UNUSED(op->result);
414}
415
416zend_op *get_next_op(zend_op_array *op_array)
417{
418    uint32_t next_op_num = op_array->last++;
419    zend_op *next_op;
420
421    if (next_op_num >= CG(context).opcodes_size) {
422        CG(context).opcodes_size *= 4;
423        op_array_alloc_ops(op_array, CG(context).opcodes_size);
424    }
425
426    next_op = &(op_array->opcodes[next_op_num]);
427
428    init_op(next_op);
429
430    return next_op;
431}
432
433int get_next_op_number(zend_op_array *op_array)
434{
435    return op_array->last;
436}
437
438zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
439{
440    op_array->last_brk_cont++;
441    op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
442    return &op_array->brk_cont_array[op_array->last_brk_cont-1];
443}
444
445static void zend_update_extended_info(zend_op_array *op_array)
446{
447    zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
448
449    while (opline<end) {
450        if (opline->opcode == ZEND_EXT_STMT) {
451            if (opline+1<end) {
452                if ((opline+1)->opcode == ZEND_EXT_STMT) {
453                    opline->opcode = ZEND_NOP;
454                    opline++;
455                    continue;
456                }
457                if (opline+1<end) {
458                    opline->lineno = (opline+1)->lineno;
459                }
460            } else {
461                opline->opcode = ZEND_NOP;
462            }
463        }
464        opline++;
465    }
466}
467
468static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
469{
470    if (extension->op_array_handler) {
471        extension->op_array_handler(op_array);
472    }
473}
474
475static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
476{
477    int i;
478
479    for (i = 0; i < op_array->last_try_catch; i++) {
480        if ((op_num < op_array->try_catch_array[i].finally_op ||
481                    op_num >= op_array->try_catch_array[i].finally_end)
482                && (dst_num >= op_array->try_catch_array[i].finally_op &&
483                     dst_num <= op_array->try_catch_array[i].finally_end)) {
484            CG(in_compilation) = 1;
485            CG(active_op_array) = op_array;
486            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
487            zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
488        } else if ((op_num >= op_array->try_catch_array[i].finally_op
489                    && op_num <= op_array->try_catch_array[i].finally_end)
490                && (dst_num > op_array->try_catch_array[i].finally_end
491                    || dst_num < op_array->try_catch_array[i].finally_op)) {
492            CG(in_compilation) = 1;
493            CG(active_op_array) = op_array;
494            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
495            zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
496        }
497    }
498}
499
500static void zend_adjust_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t start, uint32_t end)
501{
502    int i;
503    uint32_t op_num = 0;
504
505    for (i = 0; i < op_array->last_try_catch; i++) {
506        if (op_array->try_catch_array[i].finally_op > start
507                && op_array->try_catch_array[i].finally_end < end) {
508            op_num = op_array->try_catch_array[i].finally_op;
509            start = op_array->try_catch_array[i].finally_end;
510        }
511    }
512
513    if (op_num) {
514        /* Must be ZEND_FAST_CALL */
515        ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
516        op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
517        op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
518    }
519}
520
521static void zend_resolve_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t op_num)
522{
523    int i;
524    uint32_t finally_op_num = 0;
525
526    for (i = 0; i < op_array->last_try_catch; i++) {
527        if (op_num >= op_array->try_catch_array[i].finally_op
528                && op_num < op_array->try_catch_array[i].finally_end) {
529            finally_op_num = op_array->try_catch_array[i].finally_op;
530        }
531    }
532
533    if (finally_op_num) {
534        /* Must be ZEND_FAST_CALL */
535        ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
536        if (op_array->opcodes[fast_call].extended_value == 0) {
537            op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
538            op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
539        }
540    }
541}
542
543static void zend_resolve_finally_call(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
544{
545    uint32_t start_op;
546    zend_op *opline;
547    uint32_t i = op_array->last_try_catch;
548
549    if (dst_num != (uint32_t)-1) {
550        zend_check_finally_breakout(op_array, op_num, dst_num);
551    }
552
553    /* the backward order is mater */
554    while (i > 0) {
555        i--;
556        if (op_array->try_catch_array[i].finally_op &&
557            op_num >= op_array->try_catch_array[i].try_op &&
558            op_num < op_array->try_catch_array[i].finally_op - 1 &&
559            (dst_num < op_array->try_catch_array[i].try_op ||
560             dst_num > op_array->try_catch_array[i].finally_end)) {
561            /* we have a jump out of try block that needs executing finally */
562            uint32_t fast_call_var;
563
564            /* Must be ZEND_FAST_RET */
565            ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
566            fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
567
568            /* generate a FAST_CALL to finally block */
569            start_op = get_next_op_number(op_array);
570
571            opline = get_next_op(op_array);
572            opline->opcode = ZEND_FAST_CALL;
573            opline->result_type = IS_TMP_VAR;
574            opline->result.var = fast_call_var;
575            SET_UNUSED(opline->op1);
576            SET_UNUSED(opline->op2);
577            zend_adjust_fast_call(op_array, start_op,
578                    op_array->try_catch_array[i].finally_op,
579                    op_array->try_catch_array[i].finally_end);
580            if (op_array->try_catch_array[i].catch_op) {
581                opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
582                opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
583                opline->op1.opline_num = get_next_op_number(op_array);
584                /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
585                opline = get_next_op(op_array);
586                opline->opcode = ZEND_FAST_CALL;
587                opline->result_type = IS_TMP_VAR;
588                opline->result.var = fast_call_var;
589                SET_UNUSED(opline->op1);
590                SET_UNUSED(opline->op2);
591                zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2);
592            } else {
593                zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2);
594            }
595            opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
596
597            /* generate a sequence of FAST_CALL to upward finally block */
598            while (i > 0) {
599                i--;
600                if (op_array->try_catch_array[i].finally_op &&
601                    op_num >= op_array->try_catch_array[i].try_op &&
602                    op_num < op_array->try_catch_array[i].finally_op - 1 &&
603                    (dst_num < op_array->try_catch_array[i].try_op ||
604                     dst_num > op_array->try_catch_array[i].finally_end)) {
605
606                    opline = get_next_op(op_array);
607                    opline->opcode = ZEND_FAST_CALL;
608                    opline->result_type = IS_TMP_VAR;
609                    opline->result.var = fast_call_var;
610                    SET_UNUSED(opline->op1);
611                    SET_UNUSED(opline->op2);
612                    opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
613                }
614            }
615
616            /* Finish the sequence with original opcode */
617            opline = get_next_op(op_array);
618            *opline = op_array->opcodes[op_num];
619
620            /* Replace original opcode with jump to this sequence */
621            opline = op_array->opcodes + op_num;
622            opline->opcode = ZEND_JMP;
623            SET_UNUSED(opline->op1);
624            SET_UNUSED(opline->op2);
625            opline->op1.opline_num = start_op;
626
627            break;
628        }
629    }
630}
631
632static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num)
633{
634    int i;
635    uint32_t catch_op_num = 0, finally_op_num = 0;
636
637    for (i = 0; i < op_array->last_try_catch; i++) {
638        if (op_array->try_catch_array[i].try_op > op_num) {
639            break;
640        }
641        if (op_num < op_array->try_catch_array[i].finally_op) {
642            finally_op_num = op_array->try_catch_array[i].finally_op;
643        }
644        if (op_num < op_array->try_catch_array[i].catch_op) {
645            catch_op_num = op_array->try_catch_array[i].catch_op;
646        }
647    }
648
649    if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
650        /* in case of unhandled exception return to upward finally block */
651        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
652        op_array->opcodes[op_num].op2.opline_num = finally_op_num;
653    } else if (catch_op_num) {
654        /* in case of unhandled exception return to upward catch block */
655        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
656        op_array->opcodes[op_num].op2.opline_num = catch_op_num;
657    }
658}
659
660static void zend_resolve_finally_calls(zend_op_array *op_array)
661{
662    uint32_t i, j;
663    zend_op *opline;
664
665    for (i = 0, j = op_array->last; i < j; i++) {
666        opline = op_array->opcodes + i;
667        switch (opline->opcode) {
668            case ZEND_RETURN:
669            case ZEND_RETURN_BY_REF:
670            case ZEND_GENERATOR_RETURN:
671                zend_resolve_finally_call(op_array, i, (uint32_t)-1);
672                break;
673            case ZEND_BRK:
674            case ZEND_CONT:
675            {
676                int nest_levels, array_offset;
677                zend_brk_cont_element *jmp_to;
678
679                nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
680                if ((array_offset = opline->op1.opline_num) != -1) {
681                    do {
682                        jmp_to = &op_array->brk_cont_array[array_offset];
683                        if (nest_levels > 1) {
684                            array_offset = jmp_to->parent;
685                        }
686                    } while (--nest_levels > 0);
687                    zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont);
688                    break;
689                }
690            }
691            case ZEND_GOTO:
692                if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) != IS_LONG) {
693                    uint32_t num = opline->op2.constant;
694
695                    ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
696                    zend_resolve_goto_label(op_array, opline, 1);
697                    opline->op2.constant = num;
698                }
699                /* break omitted intentionally */
700            case ZEND_JMP:
701                zend_resolve_finally_call(op_array, i, opline->op1.opline_num);
702                break;
703            case ZEND_FAST_CALL:
704                zend_resolve_fast_call(op_array, i, i);
705                break;
706            case ZEND_FAST_RET:
707                zend_resolve_finally_ret(op_array, i);
708                break;
709            default:
710                break;
711        }
712    }
713}
714
715ZEND_API int pass_two(zend_op_array *op_array)
716{
717    zend_op *opline, *end;
718
719    if (!ZEND_USER_CODE(op_array->type)) {
720        return 0;
721    }
722    if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
723        zend_resolve_finally_calls(op_array);
724    }
725    if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
726        zend_update_extended_info(op_array);
727    }
728    if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
729        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
730    }
731
732    if (CG(context).vars_size != op_array->last_var) {
733        op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
734        CG(context).vars_size = op_array->last_var;
735    }
736    if (CG(context).opcodes_size != op_array->last) {
737        op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
738        CG(context).opcodes_size = op_array->last;
739    }
740    if (CG(context).literals_size != op_array->last_literal) {
741        op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
742        CG(context).literals_size = op_array->last_literal;
743    }
744    opline = op_array->opcodes;
745    end = opline + op_array->last;
746    while (opline < end) {
747        if (opline->op1_type == IS_CONST) {
748            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
749        } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
750            opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
751        }
752        if (opline->op2_type == IS_CONST) {
753            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
754        } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
755            opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
756        }
757        if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
758            opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
759        }
760        switch (opline->opcode) {
761            case ZEND_DECLARE_INHERITED_CLASS:
762            case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
763                opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
764                break;
765            case ZEND_GOTO:
766                if (Z_TYPE_P(RT_CONSTANT(op_array, opline->op2)) != IS_LONG) {
767                    zend_resolve_goto_label(op_array, opline, 1);
768                }
769                /* break omitted intentionally */
770            case ZEND_JMP:
771            case ZEND_FAST_CALL:
772                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
773                break;
774            case ZEND_JMPZNZ:
775                /* absolute index to relative offset */
776                opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
777                /* break omitted intentionally */
778            case ZEND_JMPZ:
779            case ZEND_JMPNZ:
780            case ZEND_JMPZ_EX:
781            case ZEND_JMPNZ_EX:
782            case ZEND_JMP_SET:
783            case ZEND_COALESCE:
784            case ZEND_NEW:
785            case ZEND_FE_RESET_R:
786            case ZEND_FE_RESET_RW:
787            case ZEND_FE_FETCH_R:
788            case ZEND_FE_FETCH_RW:
789            case ZEND_ASSERT_CHECK:
790                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
791                break;
792            case ZEND_VERIFY_RETURN_TYPE:
793                if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
794                    MAKE_NOP(opline);
795                }
796                break;
797            case ZEND_RETURN:
798            case ZEND_RETURN_BY_REF:
799                if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
800                    opline->opcode = ZEND_GENERATOR_RETURN;
801                }
802                break;
803        }
804        ZEND_VM_SET_OPCODE_HANDLER(opline);
805        opline++;
806    }
807
808    op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
809    return 0;
810}
811
812int pass_two_wrapper(zval *el)
813{
814    return pass_two((zend_op_array *) Z_PTR_P(el));
815}
816
817int print_class(zend_class_entry *class_entry)
818{
819    printf("Class %s:\n", class_entry->name->val);
820    zend_hash_apply(&class_entry->function_table, pass_two_wrapper);
821    printf("End of class %s.\n\n", class_entry->name->val);
822    return 0;
823}
824
825ZEND_API unary_op_type get_unary_op(int opcode)
826{
827    switch (opcode) {
828        case ZEND_BW_NOT:
829            return (unary_op_type) bitwise_not_function;
830        case ZEND_BOOL_NOT:
831            return (unary_op_type) boolean_not_function;
832        default:
833            return (unary_op_type) NULL;
834    }
835}
836
837ZEND_API binary_op_type get_binary_op(int opcode)
838{
839    switch (opcode) {
840        case ZEND_ADD:
841        case ZEND_ASSIGN_ADD:
842            return (binary_op_type) add_function;
843        case ZEND_SUB:
844        case ZEND_ASSIGN_SUB:
845            return (binary_op_type) sub_function;
846        case ZEND_MUL:
847        case ZEND_ASSIGN_MUL:
848            return (binary_op_type) mul_function;
849        case ZEND_POW:
850            return (binary_op_type) pow_function;
851        case ZEND_DIV:
852        case ZEND_ASSIGN_DIV:
853            return (binary_op_type) div_function;
854        case ZEND_MOD:
855        case ZEND_ASSIGN_MOD:
856            return (binary_op_type) mod_function;
857        case ZEND_SL:
858        case ZEND_ASSIGN_SL:
859            return (binary_op_type) shift_left_function;
860        case ZEND_SR:
861        case ZEND_ASSIGN_SR:
862            return (binary_op_type) shift_right_function;
863        case ZEND_FAST_CONCAT:
864        case ZEND_CONCAT:
865        case ZEND_ASSIGN_CONCAT:
866            return (binary_op_type) concat_function;
867        case ZEND_IS_IDENTICAL:
868            return (binary_op_type) is_identical_function;
869        case ZEND_IS_NOT_IDENTICAL:
870            return (binary_op_type) is_not_identical_function;
871        case ZEND_IS_EQUAL:
872            return (binary_op_type) is_equal_function;
873        case ZEND_IS_NOT_EQUAL:
874            return (binary_op_type) is_not_equal_function;
875        case ZEND_IS_SMALLER:
876            return (binary_op_type) is_smaller_function;
877        case ZEND_IS_SMALLER_OR_EQUAL:
878            return (binary_op_type) is_smaller_or_equal_function;
879        case ZEND_SPACESHIP:
880            return (binary_op_type) compare_function;
881        case ZEND_BW_OR:
882        case ZEND_ASSIGN_BW_OR:
883            return (binary_op_type) bitwise_or_function;
884        case ZEND_BW_AND:
885        case ZEND_ASSIGN_BW_AND:
886            return (binary_op_type) bitwise_and_function;
887        case ZEND_BW_XOR:
888        case ZEND_ASSIGN_BW_XOR:
889            return (binary_op_type) bitwise_xor_function;
890        case ZEND_BOOL_XOR:
891            return (binary_op_type) boolean_xor_function;
892        default:
893            return (binary_op_type) NULL;
894    }
895}
896
897/*
898 * Local variables:
899 * tab-width: 4
900 * c-basic-offset: 4
901 * indent-tabs-mode: t
902 * End:
903 */
904