PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_opcode.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 412 467 88.2 %
Date: 2015-03-01 Functions: 25 30 83.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:21 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.