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: 400 459 87.1 %
Date: 2015-01-26 Functions: 24 30 80.0 %
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       65535 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
      33             : {
      34       65535 :         if (extension->op_array_ctor) {
      35           0 :                 extension->op_array_ctor(op_array);
      36             :         }
      37       65535 : }
      38             : 
      39       63384 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
      40             : {
      41       63384 :         if (extension->op_array_dtor) {
      42           0 :                 extension->op_array_dtor(op_array);
      43             :         }
      44       63384 : }
      45             : 
      46       74292 : static void op_array_alloc_ops(zend_op_array *op_array, uint32_t size)
      47             : {
      48       74292 :         op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
      49       74292 : }
      50             : 
      51       65967 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size)
      52             : {
      53       65967 :         op_array->type = type;
      54             : 
      55       65967 :         op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
      56       65967 :         *op_array->refcount = 1;
      57       65967 :         op_array->last = 0;
      58       65967 :         op_array->opcodes = NULL;
      59       65967 :         op_array_alloc_ops(op_array, initial_ops_size);
      60             : 
      61       65967 :         op_array->last_var = 0;
      62       65967 :         op_array->vars = NULL;
      63             : 
      64       65967 :         op_array->T = 0;
      65             : 
      66       65967 :         op_array->function_name = NULL;
      67       65967 :         op_array->filename = zend_get_compiled_filename();
      68       65967 :         op_array->doc_comment = NULL;
      69             : 
      70       65967 :         op_array->arg_info = NULL;
      71       65967 :         op_array->num_args = 0;
      72       65967 :         op_array->required_num_args = 0;
      73             : 
      74       65967 :         op_array->scope = NULL;
      75       65967 :         op_array->prototype = NULL;
      76             : 
      77       65967 :         op_array->brk_cont_array = NULL;
      78       65967 :         op_array->try_catch_array = NULL;
      79       65967 :         op_array->last_brk_cont = 0;
      80             : 
      81       65967 :         op_array->static_variables = NULL;
      82       65967 :         op_array->last_try_catch = 0;
      83             : 
      84       65967 :         op_array->this_var = -1;
      85             : 
      86       65967 :         op_array->fn_flags = 0;
      87             : 
      88       65967 :         op_array->early_binding = -1;
      89             : 
      90       65967 :         op_array->last_literal = 0;
      91       65967 :         op_array->literals = NULL;
      92             : 
      93       65967 :         op_array->run_time_cache = NULL;
      94       65967 :         op_array->last_cache_slot = 0;
      95             : 
      96       65967 :         memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
      97             : 
      98       65967 :         zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array);
      99       65967 : }
     100             : 
     101           0 : ZEND_API void destroy_zend_function(zend_function *function)
     102             : {
     103           0 :         if (function->type == ZEND_USER_FUNCTION) {
     104           0 :                 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           0 : }
     111             : 
     112   105054852 : ZEND_API void zend_function_dtor(zval *zv)
     113             : {
     114   105054852 :         zend_function *function = Z_PTR_P(zv);
     115             : 
     116   105054852 :         if (function->type == ZEND_USER_FUNCTION) {
     117             :                 ZEND_ASSERT(function->common.function_name);
     118       54596 :                 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   105000256 :                 zend_string_release(function->common.function_name);
     125   105000256 :                 if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
     126   104952887 :                         pefree(function, 1);
     127             :                 }
     128             :         }
     129   105054852 : }
     130             : 
     131       31928 : ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array)
     132             : {
     133       31928 :         if (op_array->static_variables) {
     134        2027 :                 zend_hash_clean(op_array->static_variables);
     135             :         }
     136       31928 : }
     137             : 
     138        9003 : 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        9003 :         if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
     144             :                 zend_function *func;
     145             : 
     146       21569 :                 ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
     147       10101 :                         if (func->type == ZEND_USER_FUNCTION) {
     148       10080 :                                 zend_cleanup_op_array_data((zend_op_array *) func);
     149             :                         }
     150             :                 } ZEND_HASH_FOREACH_END();
     151             :         }
     152        9003 :         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         864 :                 for (i = 0; i < count; i++) {
     160         539 :                         zval_ptr_dtor(&static_members[i]);
     161             :                 }
     162         325 :                 efree(static_members);
     163             :         }
     164        9002 : }
     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        7278 : void _destroy_zend_class_traits_info(zend_class_entry *ce)
     185             : {
     186        7278 :         if (ce->num_traits > 0 && ce->traits) {
     187         171 :                 efree(ce->traits);
     188             :         }
     189             : 
     190        7278 :         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        7278 :         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        7278 : }
     232             : 
     233     3741249 : ZEND_API void destroy_zend_class(zval *zv)
     234             : {
     235     3741249 :         zend_class_entry *ce = Z_PTR_P(zv);
     236             : 
     237     3741249 :         if (--ce->refcount > 0) {
     238        7191 :                 return;
     239             :         }
     240     3734058 :         switch (ce->type) {
     241             :                 case ZEND_USER_CLASS:
     242        7278 :                         if (ce->default_properties_table) {
     243             :                                 int i;
     244             : 
     245       22427 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     246       40408 :                                         if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
     247       20145 :                                                 zval_ptr_dtor(&ce->default_properties_table[i]);
     248             :                                     }
     249             :                                 }
     250        2223 :                                 efree(ce->default_properties_table);
     251             :                         }
     252        7278 :                         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        7278 :                         zend_hash_destroy(&ce->properties_info);
     263        7278 :                         zend_string_release(ce->name);
     264        7278 :                         zend_hash_destroy(&ce->function_table);
     265        7278 :                         zend_hash_destroy(&ce->constants_table);
     266        7278 :                         if (ce->num_interfaces > 0 && ce->interfaces) {
     267         611 :                                 efree(ce->interfaces);
     268             :                         }
     269        7278 :                         if (ce->info.user.doc_comment) {
     270          63 :                                 zend_string_release(ce->info.user.doc_comment);
     271             :                         }
     272             : 
     273        7278 :                         _destroy_zend_class_traits_info(ce);
     274             : 
     275        7278 :                         break;
     276             :                 case ZEND_INTERNAL_CLASS:
     277     3726780 :                         if (ce->default_properties_table) {
     278             :                                 int i;
     279             : 
     280     5621400 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     281     9452280 :                                         if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
     282     4726140 :                                                 zval_internal_ptr_dtor(&ce->default_properties_table[i]);
     283             :                                         }
     284             :                                 }
     285      895260 :                                 free(ce->default_properties_table);
     286             :                         }
     287     3726780 :                         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     3726780 :                         zend_hash_destroy(&ce->properties_info);
     296     3726780 :                         zend_string_release(ce->name);
     297     3726780 :                         zend_hash_destroy(&ce->function_table);
     298     3726780 :                         zend_hash_destroy(&ce->constants_table);
     299     3726780 :                         if (ce->num_interfaces > 0) {
     300     1436580 :                                 free(ce->interfaces);
     301             :                         }
     302     3726780 :                         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       83157 : ZEND_API void destroy_op_array(zend_op_array *op_array)
     315             : {
     316       83157 :         zval *literal = op_array->literals;
     317             :         zval *end;
     318             :         uint32_t i;
     319             : 
     320       83157 :         if (op_array->static_variables) {
     321        2151 :                 zend_hash_destroy(op_array->static_variables);
     322        2151 :                 FREE_HASHTABLE(op_array->static_variables);
     323             :         }
     324             : 
     325       83157 :         if (op_array->run_time_cache && !op_array->function_name) {
     326       28151 :                 efree(op_array->run_time_cache);
     327             :         }
     328             : 
     329       83157 :         if (--(*op_array->refcount)>0) {
     330       19309 :                 return;
     331             :         }
     332             : 
     333       63848 :         efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
     334             : 
     335       63848 :         if (op_array->vars) {
     336       47344 :                 i = op_array->last_var;
     337      304078 :                 while (i > 0) {
     338      209390 :                         i--;
     339      209390 :                         zend_string_release(op_array->vars[i]);
     340             :                 }
     341       47344 :                 efree(op_array->vars);
     342             :         }
     343             : 
     344       63848 :         if (literal) {
     345       63820 :                 end = literal + op_array->last_literal;
     346     1225133 :                 while (literal < end) {
     347             :                         zval_ptr_dtor_nogc(literal);
     348     1097493 :                         literal++;
     349             :                 }
     350       63820 :                 efree(op_array->literals);
     351             :         }
     352       63848 :         efree(op_array->opcodes);
     353             : 
     354       63848 :         if (op_array->function_name) {
     355       36003 :                 zend_string_release(op_array->function_name);
     356             :         }
     357       63848 :         if (op_array->doc_comment) {
     358        2395 :                 zend_string_release(op_array->doc_comment);
     359             :         }
     360       63848 :         if (op_array->brk_cont_array) {
     361       12174 :                 efree(op_array->brk_cont_array);
     362             :         }
     363       63848 :         if (op_array->try_catch_array) {
     364        1492 :                 efree(op_array->try_catch_array);
     365             :         }
     366       63848 :         if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
     367       63805 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
     368             :         }
     369       63848 :         if (op_array->arg_info) {
     370       26900 :                 uint32_t num_args = op_array->num_args;
     371             : 
     372       26900 :                 if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
     373          36 :                         num_args++;
     374             :                 }
     375      111294 :                 for (i = 0; i < num_args; i++) {
     376       84394 :                         zend_string_release(op_array->arg_info[i].name);
     377       84394 :                         if (op_array->arg_info[i].class_name) {
     378         173 :                                 zend_string_release(op_array->arg_info[i].class_name);
     379             :                         }
     380             :                 }
     381       26900 :                 efree(op_array->arg_info);
     382             :         }
     383             : }
     384             : 
     385     2450391 : void init_op(zend_op *op)
     386             : {
     387     2450391 :         memset(op, 0, sizeof(zend_op));
     388     2450391 :         op->lineno = CG(zend_lineno);
     389     2450391 :         SET_UNUSED(op->result);
     390     2450391 : }
     391             : 
     392     2315060 : zend_op *get_next_op(zend_op_array *op_array)
     393             : {
     394     2315060 :         uint32_t next_op_num = op_array->last++;
     395             :         zend_op *next_op;
     396             : 
     397     2315060 :         if (next_op_num >= CG(context).opcodes_size) {
     398        8325 :                 CG(context).opcodes_size *= 4;
     399        8325 :                 op_array_alloc_ops(op_array, CG(context).opcodes_size);
     400             :         }
     401             : 
     402     2315060 :         next_op = &(op_array->opcodes[next_op_num]);
     403             : 
     404     2315060 :         init_op(next_op);
     405             : 
     406     2315060 :         return next_op;
     407             : }
     408             : 
     409      900768 : int get_next_op_number(zend_op_array *op_array)
     410             : {
     411      900768 :         return op_array->last;
     412             : }
     413             : 
     414       20336 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
     415             : {
     416       20336 :         op_array->last_brk_cont++;
     417       20336 :         op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
     418       20336 :         return &op_array->brk_cont_array[op_array->last_brk_cont-1];
     419             : }
     420             : 
     421           0 : static void zend_update_extended_info(zend_op_array *op_array)
     422             : {
     423           0 :         zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
     424             : 
     425           0 :         while (opline<end) {
     426           0 :                 if (opline->opcode == ZEND_EXT_STMT) {
     427           0 :                         if (opline+1<end) {
     428           0 :                                 if ((opline+1)->opcode == ZEND_EXT_STMT) {
     429           0 :                                         opline->opcode = ZEND_NOP;
     430           0 :                                         opline++;
     431           0 :                                         continue;
     432             :                                 }
     433           0 :                                 if (opline+1<end) {
     434           0 :                                         opline->lineno = (opline+1)->lineno;
     435             :                                 }
     436             :                         } else {
     437           0 :                                 opline->opcode = ZEND_NOP;
     438             :                         }
     439             :                 }
     440           0 :                 opline++;
     441             :         }
     442           0 : }
     443             : 
     444       65083 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
     445             : {
     446       65083 :         if (extension->op_array_handler) {
     447           0 :                 extension->op_array_handler(op_array);
     448             :         }
     449       65083 : }
     450             : 
     451         128 : static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
     452             : {
     453             :         int i;
     454             : 
     455         493 :         for (i = 0; i < op_array->last_try_catch; i++) {
     456        1093 :                 if ((op_num < op_array->try_catch_array[i].finally_op ||
     457         156 :                                         op_num >= op_array->try_catch_array[i].finally_end)
     458         339 :                                 && (dst_num >= op_array->try_catch_array[i].finally_op &&
     459         226 :                                          dst_num <= op_array->try_catch_array[i].finally_end)) {
     460           3 :                         CG(in_compilation) = 1;
     461           3 :                         CG(active_op_array) = op_array;
     462           3 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     463           3 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
     464         956 :                 } else if ((op_num >= op_array->try_catch_array[i].finally_op
     465         524 :                                         && op_num <= op_array->try_catch_array[i].finally_end)
     466          33 :                                 && (dst_num > op_array->try_catch_array[i].finally_end
     467          63 :                                         || dst_num < op_array->try_catch_array[i].finally_op)) {
     468           4 :                         CG(in_compilation) = 1;
     469           4 :                         CG(active_op_array) = op_array;
     470           4 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     471           4 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
     472             :                 }
     473             :         }
     474         121 : }
     475             : 
     476          21 : static void zend_adjust_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t start, uint32_t end)
     477             : {
     478             :         int i;
     479          21 :         uint32_t op_num = 0;
     480             : 
     481          78 :         for (i = 0; i < op_array->last_try_catch; i++) {
     482         125 :                 if (op_array->try_catch_array[i].finally_op > start
     483          68 :                                 && op_array->try_catch_array[i].finally_end < end) {
     484           0 :                         op_num = op_array->try_catch_array[i].finally_op;
     485           0 :                         start = op_array->try_catch_array[i].finally_end;
     486             :                 }
     487             :         }
     488             : 
     489          21 :         if (op_num) {
     490             :                 /* Must be ZEND_FAST_CALL */
     491             :                 ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
     492           0 :                 op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
     493           0 :                 op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
     494             :         }
     495          21 : }
     496             : 
     497          87 : static void zend_resolve_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t op_num)
     498             : {
     499             :         int i;
     500          87 :         uint32_t finally_op_num = 0;
     501             : 
     502         340 :         for (i = 0; i < op_array->last_try_catch; i++) {
     503         597 :                 if (op_num >= op_array->try_catch_array[i].finally_op
     504         344 :                                 && op_num < op_array->try_catch_array[i].finally_end) {
     505          16 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     506             :                 }
     507             :         }
     508             : 
     509          87 :         if (finally_op_num) {
     510             :                 /* Must be ZEND_FAST_CALL */
     511             :                 ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
     512           9 :                 if (op_array->opcodes[fast_call].extended_value == 0) {
     513           9 :                         op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
     514           9 :                         op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
     515             :                 }
     516             :         }
     517          87 : }
     518             : 
     519         193 : static void zend_resolve_finally_call(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
     520             : {
     521             :         uint32_t start_op;
     522             :         zend_op *opline;
     523         193 :         uint32_t i = op_array->last_try_catch;
     524             : 
     525         193 :         if (dst_num != (uint32_t)-1) {
     526         128 :                 zend_check_finally_breakout(op_array, op_num, dst_num);
     527             :         }
     528             : 
     529             :         /* the backward order is mater */
     530         804 :         while (i > 0) {
     531         453 :                 i--;
     532        1435 :                 if (op_array->try_catch_array[i].finally_op &&
     533         436 :                     op_num >= op_array->try_catch_array[i].try_op &&
     534         368 :                     op_num < op_array->try_catch_array[i].finally_op - 1 &&
     535          89 :                     (dst_num < op_array->try_catch_array[i].try_op ||
     536          89 :                      dst_num > op_array->try_catch_array[i].finally_end)) {
     537             :                         /* we have a jump out of try block that needs executing finally */
     538             :                         uint32_t fast_call_var;
     539             : 
     540             :                         /* Must be ZEND_FAST_RET */
     541             :                         ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
     542          21 :                         fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
     543             : 
     544             :                         /* generate a FAST_CALL to finally block */
     545          21 :                     start_op = get_next_op_number(op_array);
     546             : 
     547          21 :                         opline = get_next_op(op_array);
     548          21 :                         opline->opcode = ZEND_FAST_CALL;
     549          21 :                         opline->result_type = IS_TMP_VAR;
     550          21 :                         opline->result.var = fast_call_var;
     551          21 :                         SET_UNUSED(opline->op1);
     552          21 :                         SET_UNUSED(opline->op2);
     553          42 :                         zend_adjust_fast_call(op_array, start_op,
     554          21 :                                         op_array->try_catch_array[i].finally_op,
     555          21 :                                         op_array->try_catch_array[i].finally_end);
     556          21 :                         if (op_array->try_catch_array[i].catch_op) {
     557           7 :                                 opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
     558           7 :                                 opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
     559           7 :                                 opline->op1.opline_num = get_next_op_number(op_array);
     560             :                                 /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
     561           7 :                                 opline = get_next_op(op_array);
     562           7 :                                 opline->opcode = ZEND_FAST_CALL;
     563           7 :                                 opline->result_type = IS_TMP_VAR;
     564           7 :                                 opline->result.var = fast_call_var;
     565           7 :                                 SET_UNUSED(opline->op1);
     566           7 :                                 SET_UNUSED(opline->op2);
     567           7 :                                 zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2);
     568             :                         } else {
     569          14 :                                 zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2);
     570             :                         }
     571          21 :                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     572             : 
     573             :                         /* generate a sequence of FAST_CALL to upward finally block */
     574          66 :                         while (i > 0) {
     575          24 :                                 i--;
     576          80 :                                 if (op_array->try_catch_array[i].finally_op &&
     577          23 :                                         op_num >= op_array->try_catch_array[i].try_op &&
     578          23 :                                         op_num < op_array->try_catch_array[i].finally_op - 1 &&
     579           5 :                                         (dst_num < op_array->try_catch_array[i].try_op ||
     580           5 :                                          dst_num > op_array->try_catch_array[i].finally_end)) {
     581             : 
     582           5 :                                         opline = get_next_op(op_array);
     583           5 :                                         opline->opcode = ZEND_FAST_CALL;
     584           5 :                                         opline->result_type = IS_TMP_VAR;
     585           5 :                                         opline->result.var = fast_call_var;
     586           5 :                                         SET_UNUSED(opline->op1);
     587           5 :                                         SET_UNUSED(opline->op2);
     588           5 :                                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     589             :                                 }
     590             :                         }
     591             : 
     592             :                         /* Finish the sequence with original opcode */
     593          21 :                         opline = get_next_op(op_array);
     594          21 :                         *opline = op_array->opcodes[op_num];
     595             : 
     596             :                         /* Replace original opcode with jump to this sequence */
     597          21 :                         opline = op_array->opcodes + op_num;
     598          21 :                         opline->opcode = ZEND_JMP;
     599          21 :                         SET_UNUSED(opline->op1);
     600          21 :                         SET_UNUSED(opline->op2);
     601          21 :                         opline->op1.opline_num = start_op;
     602             : 
     603          21 :                         break;
     604             :                 }
     605             :         }
     606         186 : }
     607             : 
     608          62 : static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num)
     609             : {
     610             :         int i;
     611          62 :         uint32_t catch_op_num = 0, finally_op_num = 0;
     612             : 
     613         224 :         for (i = 0; i < op_array->last_try_catch; i++) {
     614         170 :                 if (op_array->try_catch_array[i].try_op > op_num) {
     615           8 :                         break;
     616             :                 }
     617         162 :                 if (op_num < op_array->try_catch_array[i].finally_op) {
     618          23 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     619             :                 }
     620         162 :                 if (op_num < op_array->try_catch_array[i].catch_op) {
     621          16 :                         catch_op_num = op_array->try_catch_array[i].catch_op;
     622             :                 }
     623             :         }
     624             : 
     625          70 :         if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
     626             :                 /* in case of unhandled exception return to upward finally block */
     627           8 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
     628           8 :                 op_array->opcodes[op_num].op2.opline_num = finally_op_num;
     629          54 :         } else if (catch_op_num) {
     630             :                 /* in case of unhandled exception return to upward catch block */
     631          11 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
     632          11 :                 op_array->opcodes[op_num].op2.opline_num = catch_op_num;
     633             :         }
     634          62 : }
     635             : 
     636          42 : static void zend_resolve_finally_calls(zend_op_array *op_array)
     637             : {
     638             :         uint32_t i, j;
     639             :         zend_op *opline;
     640             : 
     641         754 :         for (i = 0, j = op_array->last; i < j; i++) {
     642         719 :                 opline = op_array->opcodes + i;
     643         719 :                 switch (opline->opcode) {
     644             :                         case ZEND_RETURN:
     645             :                         case ZEND_RETURN_BY_REF:
     646             :                         case ZEND_GENERATOR_RETURN:
     647          64 :                                 zend_resolve_finally_call(op_array, i, (uint32_t)-1);
     648          64 :                                 break;
     649             :                         case ZEND_BRK:
     650             :                         case ZEND_CONT:
     651             :                         {
     652             :                                 int nest_levels, array_offset;
     653             :                                 zend_brk_cont_element *jmp_to;
     654             : 
     655           9 :                                 nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
     656           9 :                                 if ((array_offset = opline->op1.opline_num) != -1) {
     657             :                                         do {
     658           8 :                                                 jmp_to = &op_array->brk_cont_array[array_offset];
     659           8 :                                                 if (nest_levels > 1) {
     660           0 :                                                         array_offset = jmp_to->parent;
     661             :                                                 }
     662           8 :                                         } while (--nest_levels > 0);
     663           8 :                                         zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont);
     664           6 :                                         break;
     665             :                                 }
     666             :                         }
     667             :                         case ZEND_GOTO:
     668          18 :                                 if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) != IS_LONG) {
     669           8 :                                         uint32_t num = opline->op2.constant;
     670             : 
     671           8 :                                         ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
     672           8 :                                         zend_resolve_goto_label(op_array, opline, 1);
     673           8 :                                         opline->op2.constant = num;
     674             :                                 }
     675             :                                 /* break omitted intentionally */
     676             :                         case ZEND_JMP:
     677         121 :                                 zend_resolve_finally_call(op_array, i, opline->op1.opline_num);
     678         116 :                                 break;
     679             :                         case ZEND_FAST_CALL:
     680          66 :                                 zend_resolve_fast_call(op_array, i, i);
     681          66 :                                 break;
     682             :                         case ZEND_FAST_RET:
     683          62 :                                 zend_resolve_finally_ret(op_array, i);
     684             :                                 break;
     685             :                         default:
     686             :                                 break;
     687             :                 }
     688             :         }
     689          35 : }
     690             : 
     691       65696 : ZEND_API int pass_two(zend_op_array *op_array)
     692             : {
     693             :         zend_op *opline, *end;
     694             : 
     695       65696 :         if (!ZEND_USER_CODE(op_array->type)) {
     696           0 :                 return 0;
     697             :         }
     698       65696 :         if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
     699          42 :                 zend_resolve_finally_calls(op_array);
     700             :         }
     701       65689 :         if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
     702           0 :                 zend_update_extended_info(op_array);
     703             :         }
     704       65689 :         if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
     705       65480 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
     706             :         }
     707             : 
     708       65689 :         if (CG(context).vars_size != op_array->last_var) {
     709       47844 :                 op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
     710       47844 :                 CG(context).vars_size = op_array->last_var;
     711             :         }
     712       65689 :         if (CG(context).opcodes_size != op_array->last) {
     713       65301 :                 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
     714       65301 :                 CG(context).opcodes_size = op_array->last;
     715             :         }
     716       65689 :         if (CG(context).literals_size != op_array->last_literal) {
     717       64263 :                 op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
     718       64263 :                 CG(context).literals_size = op_array->last_literal;
     719             :         }
     720       65689 :         opline = op_array->opcodes;
     721       65689 :         end = opline + op_array->last;
     722     2445966 :         while (opline < end) {
     723     2314593 :                 if (opline->op1_type == IS_CONST) {
     724      443600 :                         ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
     725     1870993 :                 } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
     726      538404 :                         opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
     727             :                 }
     728     2314593 :                 if (opline->op2_type == IS_CONST) {
     729      581598 :                         ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
     730     1732995 :                 } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
     731      129516 :                         opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
     732             :                 }
     733     2314593 :                 if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
     734      982096 :                         opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
     735             :                 }
     736     2314593 :                 switch (opline->opcode) {
     737             :                         case ZEND_DECLARE_INHERITED_CLASS:
     738             :                         case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
     739        1289 :                                 opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
     740        1289 :                                 break;
     741             :                         case ZEND_GOTO:
     742          34 :                                 if (Z_TYPE_P(RT_CONSTANT(op_array, opline->op2)) != IS_LONG) {
     743          15 :                                         zend_resolve_goto_label(op_array, opline, 1);
     744             :                                 }
     745             :                                 /* break omitted intentionally */
     746             :                         case ZEND_JMP:
     747             :                         case ZEND_FAST_CALL:
     748       63014 :                                 ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
     749       63014 :                                 break;
     750             :                         case ZEND_JMPZNZ:
     751             :                                 /* absolute index to relative offset */
     752           0 :                                 opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
     753             :                                 /* break omitted intentionally */
     754             :                         case ZEND_JMPZ:
     755             :                         case ZEND_JMPNZ:
     756             :                         case ZEND_JMPZ_EX:
     757             :                         case ZEND_JMPNZ_EX:
     758             :                         case ZEND_JMP_SET:
     759             :                         case ZEND_COALESCE:
     760             :                         case ZEND_NEW:
     761             :                         case ZEND_FE_RESET:
     762             :                         case ZEND_FE_FETCH:
     763      151505 :                                 ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
     764      151505 :                                 break;
     765             :                         case ZEND_RETURN:
     766             :                         case ZEND_RETURN_BY_REF:
     767      110203 :                                 if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
     768         168 :                                         if (opline->op1_type != IS_CONST || Z_TYPE_P(RT_CONSTANT(op_array, opline->op1)) != IS_NULL) {
     769           2 :                                                 CG(zend_lineno) = opline->lineno;
     770           2 :                                                 zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
     771             :                                         }
     772             : 
     773          83 :                                         opline->opcode = ZEND_GENERATOR_RETURN;
     774             :                                 }
     775             :                                 break;
     776             :                 }
     777     2314588 :                 ZEND_VM_SET_OPCODE_HANDLER(opline);
     778     2314588 :                 opline++;
     779             :         }
     780             : 
     781       65684 :         op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
     782       65684 :         return 0;
     783             : }
     784             : 
     785           0 : int pass_two_wrapper(zval *el)
     786             : {
     787           0 :         return pass_two((zend_op_array *) Z_PTR_P(el));
     788             : }
     789             : 
     790           0 : int print_class(zend_class_entry *class_entry)
     791             : {
     792           0 :         printf("Class %s:\n", class_entry->name->val);
     793           0 :         zend_hash_apply(&class_entry->function_table, pass_two_wrapper);
     794           0 :         printf("End of class %s.\n\n", class_entry->name->val);
     795           0 :         return 0;
     796             : }
     797             : 
     798         175 : ZEND_API unary_op_type get_unary_op(int opcode)
     799             : {
     800         175 :         switch (opcode) {
     801             :                 case ZEND_BW_NOT:
     802           1 :                         return (unary_op_type) bitwise_not_function;
     803             :                 case ZEND_BOOL_NOT:
     804         174 :                         return (unary_op_type) boolean_not_function;
     805             :                 default:
     806           0 :                         return (unary_op_type) NULL;
     807             :         }
     808             : }
     809             : 
     810        3159 : ZEND_API binary_op_type get_binary_op(int opcode)
     811             : {
     812        3159 :         switch (opcode) {
     813             :                 case ZEND_ADD:
     814             :                 case ZEND_ASSIGN_ADD:
     815         154 :                         return (binary_op_type) add_function;
     816             :                 case ZEND_SUB:
     817             :                 case ZEND_ASSIGN_SUB:
     818         176 :                         return (binary_op_type) sub_function;
     819             :                 case ZEND_MUL:
     820             :                 case ZEND_ASSIGN_MUL:
     821          64 :                         return (binary_op_type) mul_function;
     822             :                 case ZEND_POW:
     823           8 :                         return (binary_op_type) pow_function;
     824             :                 case ZEND_DIV:
     825             :                 case ZEND_ASSIGN_DIV:
     826          27 :                         return (binary_op_type) div_function;
     827             :                 case ZEND_MOD:
     828             :                 case ZEND_ASSIGN_MOD:
     829           4 :                         return (binary_op_type) mod_function;
     830             :                 case ZEND_SL:
     831             :                 case ZEND_ASSIGN_SL:
     832          32 :                         return (binary_op_type) shift_left_function;
     833             :                 case ZEND_SR:
     834             :                 case ZEND_ASSIGN_SR:
     835           3 :                         return (binary_op_type) shift_right_function;
     836             :                 case ZEND_CONCAT:
     837             :                 case ZEND_ASSIGN_CONCAT:
     838        1876 :                         return (binary_op_type) concat_function;
     839             :                 case ZEND_IS_IDENTICAL:
     840           1 :                         return (binary_op_type) is_identical_function;
     841             :                 case ZEND_IS_NOT_IDENTICAL:
     842          18 :                         return (binary_op_type) is_not_identical_function;
     843             :                 case ZEND_IS_EQUAL:
     844          68 :                         return (binary_op_type) is_equal_function;
     845             :                 case ZEND_IS_NOT_EQUAL:
     846         385 :                         return (binary_op_type) is_not_equal_function;
     847             :                 case ZEND_IS_SMALLER:
     848          39 :                         return (binary_op_type) is_smaller_function;
     849             :                 case ZEND_IS_SMALLER_OR_EQUAL:
     850           6 :                         return (binary_op_type) is_smaller_or_equal_function;
     851             :                 case ZEND_BW_OR:
     852             :                 case ZEND_ASSIGN_BW_OR:
     853         287 :                         return (binary_op_type) bitwise_or_function;
     854             :                 case ZEND_BW_AND:
     855             :                 case ZEND_ASSIGN_BW_AND:
     856           0 :                         return (binary_op_type) bitwise_and_function;
     857             :                 case ZEND_BW_XOR:
     858             :                 case ZEND_ASSIGN_BW_XOR:
     859           9 :                         return (binary_op_type) bitwise_xor_function;
     860             :                 case ZEND_BOOL_XOR:
     861           2 :                         return (binary_op_type) boolean_xor_function;
     862             :                 default:
     863           0 :                         return (binary_op_type) NULL;
     864             :         }
     865             : }
     866             : 
     867             : /*
     868             :  * Local variables:
     869             :  * tab-width: 4
     870             :  * c-basic-offset: 4
     871             :  * indent-tabs-mode: t
     872             :  * End:
     873             :  */

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:40 +0000 (2 days ago)

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