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

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:11 +0000 (5 days ago)

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