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: 398 449 88.6 %
Date: 2014-09-19 Functions: 25 30 83.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:03 +0000 (3 days ago)

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