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: 399 454 87.9 %
Date: 2014-12-13 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       64804 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      33             : {
      34       64804 :         if (extension->op_array_ctor) {
      35           0 :                 extension->op_array_ctor(op_array);
      36             :         }
      37       64804 : }
      38             : 
      39       62575 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      40             : {
      41       62575 :         if (extension->op_array_dtor) {
      42           0 :                 extension->op_array_dtor(op_array);
      43             :         }
      44       62575 : }
      45             : 
      46       73452 : static void op_array_alloc_ops(zend_op_array *op_array, uint32_t size)
      47             : {
      48       73452 :         op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
      49       73452 : }
      50             : 
      51       65236 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC)
      52             : {
      53       65236 :         op_array->type = type;
      54             : 
      55       65236 :         op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
      56       65236 :         *op_array->refcount = 1;
      57       65236 :         op_array->last = 0;
      58       65236 :         op_array->opcodes = NULL;
      59       65236 :         op_array_alloc_ops(op_array, initial_ops_size);
      60             : 
      61       65236 :         op_array->last_var = 0;
      62       65236 :         op_array->vars = NULL;
      63             : 
      64       65236 :         op_array->T = 0;
      65             : 
      66       65236 :         op_array->function_name = NULL;
      67       65236 :         op_array->filename = zend_get_compiled_filename(TSRMLS_C);
      68       65236 :         op_array->doc_comment = NULL;
      69             : 
      70       65236 :         op_array->arg_info = NULL;
      71       65236 :         op_array->num_args = 0;
      72       65236 :         op_array->required_num_args = 0;
      73             : 
      74       65236 :         op_array->scope = NULL;
      75       65236 :         op_array->prototype = NULL;
      76             : 
      77       65236 :         op_array->brk_cont_array = NULL;
      78       65236 :         op_array->try_catch_array = NULL;
      79       65236 :         op_array->last_brk_cont = 0;
      80             : 
      81       65236 :         op_array->static_variables = NULL;
      82       65236 :         op_array->last_try_catch = 0;
      83             : 
      84       65236 :         op_array->this_var = -1;
      85             : 
      86       65236 :         op_array->fn_flags = 0;
      87             : 
      88       65236 :         op_array->early_binding = -1;
      89             : 
      90       65236 :         op_array->last_literal = 0;
      91       65236 :         op_array->literals = NULL;
      92             : 
      93       65236 :         op_array->run_time_cache = NULL;
      94       65236 :         op_array->last_cache_slot = 0;
      95             : 
      96       65236 :         memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
      97             : 
      98       65236 :         zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
      99       65236 : }
     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   103091179 : ZEND_API void zend_function_dtor(zval *zv)
     113             : {
     114   103091179 :         zend_function *function = Z_PTR_P(zv);
     115             :         TSRMLS_FETCH();
     116             : 
     117   103091179 :         if (function->type == ZEND_USER_FUNCTION) {
     118             :                 ZEND_ASSERT(function->common.function_name);
     119       53745 :                 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   103037434 :                 zend_string_release(function->common.function_name);
     126   103037434 :                 if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
     127   102990083 :                         pefree(function, 1);
     128             :                 }
     129             :         }
     130   103091179 : }
     131             : 
     132       31453 : ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array)
     133             : {
     134       31453 :         if (op_array->static_variables) {
     135        2022 :                 zend_hash_clean(op_array->static_variables);
     136             :         }
     137       31453 : }
     138             : 
     139        9002 : 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        9002 :         if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
     145             :                 zend_function *func;
     146             : 
     147       21417 :                 ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
     148       10029 :                         if (func->type == ZEND_USER_FUNCTION) {
     149       10008 :                                 zend_cleanup_op_array_data((zend_op_array *) func);
     150             :                         }
     151             :                 } ZEND_HASH_FOREACH_END();
     152             :         }
     153        9002 :         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        9001 : }
     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        7281 : void _destroy_zend_class_traits_info(zend_class_entry *ce)
     187             : {
     188        7281 :         if (ce->num_traits > 0 && ce->traits) {
     189         169 :                 efree(ce->traits);
     190             :         }
     191             :         
     192        7281 :         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        7281 :         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        7281 : }
     234             : 
     235     3691242 : ZEND_API void destroy_zend_class(zval *zv)
     236             : {
     237     3691242 :         zend_class_entry *ce = Z_PTR_P(zv);
     238             :         
     239     3691242 :         if (--ce->refcount > 0) {
     240        7193 :                 return;
     241             :         }
     242     3684049 :         switch (ce->type) {
     243             :                 case ZEND_USER_CLASS:
     244        7281 :                         if (ce->default_properties_table) {
     245             :                                 int i;
     246             : 
     247       22427 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     248       40408 :                                         if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
     249       20145 :                                                 zval_ptr_dtor(&ce->default_properties_table[i]);
     250             :                                     }
     251             :                                 }
     252        2223 :                                 efree(ce->default_properties_table);
     253             :                         }
     254        7281 :                         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        7281 :                         zend_hash_destroy(&ce->properties_info);
     265        7281 :                         zend_string_release(ce->name);
     266        7281 :                         zend_hash_destroy(&ce->function_table);
     267        7281 :                         zend_hash_destroy(&ce->constants_table);
     268        7281 :                         if (ce->num_interfaces > 0 && ce->interfaces) {
     269         610 :                                 efree(ce->interfaces);
     270             :                         }
     271        7281 :                         if (ce->info.user.doc_comment) {
     272          63 :                                 zend_string_release(ce->info.user.doc_comment);
     273             :                         }
     274             :                         
     275        7281 :                         _destroy_zend_class_traits_info(ce);
     276             :                         
     277        7281 :                         break;
     278             :                 case ZEND_INTERNAL_CLASS:
     279     3676768 :                         if (ce->default_properties_table) {
     280             :                                 int i;
     281             : 
     282     5577120 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     283     9377824 :                                         if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
     284     4688912 :                                                 zval_internal_ptr_dtor(&ce->default_properties_table[i]);
     285             :                                         }
     286             :                                 }
     287      888208 :                                 free(ce->default_properties_table);
     288             :                         }
     289     3676768 :                         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     3676768 :                         zend_hash_destroy(&ce->properties_info);
     298     3676768 :                         zend_string_release(ce->name);
     299     3676768 :                         zend_hash_destroy(&ce->function_table);
     300     3676768 :                         zend_hash_destroy(&ce->constants_table);
     301     3676768 :                         if (ce->num_interfaces > 0) {
     302     1425264 :                                 free(ce->interfaces);
     303             :                         }
     304     3676768 :                         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       81936 : ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC)
     317             : {
     318       81936 :         zval *literal = op_array->literals;
     319             :         zval *end;
     320             :         uint32_t i;
     321             : 
     322       81936 :         if (op_array->static_variables) {
     323        2145 :                 zend_hash_destroy(op_array->static_variables);
     324        2145 :                 FREE_HASHTABLE(op_array->static_variables);
     325             :         }
     326             : 
     327       81936 :         if (op_array->run_time_cache && !op_array->function_name) {
     328       24277 :                 efree(op_array->run_time_cache);
     329             :         }
     330             : 
     331       81936 :         if (--(*op_array->refcount)>0) {
     332       18897 :                 return;
     333             :         }
     334             : 
     335       63039 :         efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
     336             : 
     337       63039 :         if (op_array->vars) {
     338       46691 :                 i = op_array->last_var;
     339      297863 :                 while (i > 0) {
     340      204481 :                         i--;
     341      204481 :                         zend_string_release(op_array->vars[i]);
     342             :                 }
     343       46691 :                 efree(op_array->vars);
     344             :         }
     345             : 
     346       63039 :         if (literal) {
     347       63011 :                 end = literal + op_array->last_literal;
     348     1210513 :                 while (literal < end) {
     349             :                         zval_ptr_dtor_nogc(literal);
     350     1084491 :                         literal++;
     351             :                 }
     352       63011 :                 efree(op_array->literals);
     353             :         }
     354       63039 :         efree(op_array->opcodes);
     355             : 
     356       63039 :         if (op_array->function_name) {
     357       35562 :                 zend_string_release(op_array->function_name);
     358             :         }
     359       63039 :         if (op_array->doc_comment) {
     360        2004 :                 zend_string_release(op_array->doc_comment);
     361             :         }
     362       63039 :         if (op_array->brk_cont_array) {
     363       11823 :                 efree(op_array->brk_cont_array);
     364             :         }
     365       63039 :         if (op_array->try_catch_array) {
     366        1486 :                 efree(op_array->try_catch_array);
     367             :         }
     368       63039 :         if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
     369       62996 :                 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       63039 :         if (op_array->arg_info) {
     372      109642 :                 for (i=0; i<op_array->num_args; i++) {
     373       83169 :                         zend_string_release(op_array->arg_info[i].name);
     374       83169 :                         if (op_array->arg_info[i].class_name) {
     375         172 :                                 zend_string_release(op_array->arg_info[i].class_name);
     376             :                         }
     377             :                 }
     378       26473 :                 efree(op_array->arg_info);
     379             :         }
     380             : }
     381             : 
     382     2422281 : void init_op(zend_op *op TSRMLS_DC)
     383             : {
     384     2422281 :         memset(op, 0, sizeof(zend_op));
     385     2422281 :         op->lineno = CG(zend_lineno);
     386     2422281 :         SET_UNUSED(op->result);
     387     2422281 : }
     388             : 
     389     2289389 : zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
     390             : {
     391     2289389 :         uint32_t next_op_num = op_array->last++;
     392             :         zend_op *next_op;
     393             : 
     394     2289389 :         if (next_op_num >= CG(context).opcodes_size) {
     395        8216 :                 CG(context).opcodes_size *= 4;
     396        8216 :                 op_array_alloc_ops(op_array, CG(context).opcodes_size);
     397             :         }
     398             :         
     399     2289389 :         next_op = &(op_array->opcodes[next_op_num]);
     400             :         
     401     2289389 :         init_op(next_op TSRMLS_CC);
     402             : 
     403     2289389 :         return next_op;
     404             : }
     405             : 
     406      891034 : int get_next_op_number(zend_op_array *op_array)
     407             : {
     408      891034 :         return op_array->last;
     409             : }
     410             : 
     411       20002 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
     412             : {
     413       20002 :         op_array->last_brk_cont++;
     414       20002 :         op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
     415       20002 :         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       64361 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
     442             : {
     443       64361 :         if (extension->op_array_handler) {
     444           0 :                 extension->op_array_handler(op_array);
     445             :         }
     446       64361 : }
     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             :                         uint32_t fast_call_var;
     536             :                         
     537             :                         /* Must be ZEND_FAST_RET */
     538             :                         ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
     539          21 :                         fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
     540             : 
     541             :                         /* generate a FAST_CALL to finally block */
     542          21 :                     start_op = get_next_op_number(op_array);
     543             : 
     544          21 :                         opline = get_next_op(op_array TSRMLS_CC);
     545          21 :                         opline->opcode = ZEND_FAST_CALL;
     546          21 :                         opline->result_type = IS_TMP_VAR;
     547          21 :                         opline->result.var = fast_call_var;
     548          21 :                         SET_UNUSED(opline->op1);
     549          21 :                         SET_UNUSED(opline->op2);
     550          42 :                         zend_adjust_fast_call(op_array, start_op,
     551          21 :                                         op_array->try_catch_array[i].finally_op,
     552          21 :                                         op_array->try_catch_array[i].finally_end TSRMLS_CC);
     553          21 :                         if (op_array->try_catch_array[i].catch_op) {
     554           7 :                                 opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
     555           7 :                                 opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
     556           7 :                                 opline->op1.opline_num = get_next_op_number(op_array);
     557             :                                 /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
     558           7 :                                 opline = get_next_op(op_array TSRMLS_CC);
     559           7 :                                 opline->opcode = ZEND_FAST_CALL;
     560           7 :                                 opline->result_type = IS_TMP_VAR;
     561           7 :                                 opline->result.var = fast_call_var;
     562           7 :                                 SET_UNUSED(opline->op1);
     563           7 :                                 SET_UNUSED(opline->op2);
     564           7 :                                 zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
     565             :                         } else {
     566          14 :                                 zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
     567             :                         }
     568          21 :                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     569             : 
     570             :                         /* generate a sequence of FAST_CALL to upward finally block */
     571          66 :                         while (i > 0) {
     572          24 :                                 i--;
     573          80 :                                 if (op_array->try_catch_array[i].finally_op &&
     574          23 :                                         op_num >= op_array->try_catch_array[i].try_op &&
     575          23 :                                         op_num < op_array->try_catch_array[i].finally_op - 1 &&
     576           5 :                                         (dst_num < op_array->try_catch_array[i].try_op ||
     577           5 :                                          dst_num > op_array->try_catch_array[i].finally_end)) {
     578             : 
     579           5 :                                         opline = get_next_op(op_array TSRMLS_CC);
     580           5 :                                         opline->opcode = ZEND_FAST_CALL;
     581           5 :                                         opline->result_type = IS_TMP_VAR;
     582           5 :                                         opline->result.var = fast_call_var;
     583           5 :                                         SET_UNUSED(opline->op1);
     584           5 :                                         SET_UNUSED(opline->op2);
     585           5 :                                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     586             :                                 }
     587             :                         }
     588             : 
     589             :                         /* Finish the sequence with original opcode */
     590          21 :                         opline = get_next_op(op_array TSRMLS_CC);
     591          21 :                         *opline = op_array->opcodes[op_num];
     592             : 
     593             :                         /* Replace original opcode with jump to this sequence */
     594          21 :                         opline = op_array->opcodes + op_num;
     595          21 :                         opline->opcode = ZEND_JMP;
     596          21 :                         SET_UNUSED(opline->op1);
     597          21 :                         SET_UNUSED(opline->op2);
     598          21 :                         opline->op1.opline_num = start_op;
     599             : 
     600          21 :                         break;
     601             :                 }
     602             :         }       
     603         185 : }
     604             : 
     605          62 : static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num TSRMLS_DC)
     606             : {
     607             :         int i;
     608          62 :         uint32_t catch_op_num = 0, finally_op_num = 0;
     609             : 
     610         224 :         for (i = 0; i < op_array->last_try_catch; i++) {
     611         170 :                 if (op_array->try_catch_array[i].try_op > op_num) {
     612           8 :                         break;
     613             :                 }
     614         162 :                 if (op_num < op_array->try_catch_array[i].finally_op) {
     615          23 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     616             :                 }
     617         162 :                 if (op_num < op_array->try_catch_array[i].catch_op) {
     618          16 :                         catch_op_num = op_array->try_catch_array[i].catch_op;
     619             :                 }
     620             :         }
     621             : 
     622          70 :         if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
     623             :                 /* in case of unhandled exception return to upward finally block */
     624           8 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
     625           8 :                 op_array->opcodes[op_num].op2.opline_num = finally_op_num;
     626          54 :         } else if (catch_op_num) {
     627             :                 /* in case of unhandled exception return to upward catch block */
     628          11 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
     629          11 :                 op_array->opcodes[op_num].op2.opline_num = catch_op_num;
     630             :         }
     631          62 : }
     632             : 
     633          42 : static void zend_resolve_finally_calls(zend_op_array *op_array TSRMLS_DC)
     634             : {
     635             :         uint32_t i, j;
     636             :         zend_op *opline;
     637             : 
     638         755 :         for (i = 0, j = op_array->last; i < j; i++) {
     639         720 :                 opline = op_array->opcodes + i;
     640         720 :                 switch (opline->opcode) {
     641             :                         case ZEND_RETURN:
     642             :                         case ZEND_RETURN_BY_REF:
     643             :                         case ZEND_GENERATOR_RETURN:
     644          64 :                                 zend_resolve_finally_call(op_array, i, (uint32_t)-1 TSRMLS_CC);
     645          64 :                                 break;
     646             :                         case ZEND_BRK:
     647             :                         case ZEND_CONT:
     648             :                         {
     649             :                                 int nest_levels, array_offset;
     650             :                                 zend_brk_cont_element *jmp_to;
     651             : 
     652           9 :                                 nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
     653           9 :                                 if ((array_offset = opline->op1.opline_num) != -1) {
     654             :                                         do {
     655           8 :                                                 jmp_to = &op_array->brk_cont_array[array_offset];
     656           8 :                                                 if (nest_levels > 1) {
     657           0 :                                                         array_offset = jmp_to->parent;
     658             :                                                 }
     659           8 :                                         } while (--nest_levels > 0);
     660           8 :                                         zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont TSRMLS_CC);
     661           6 :                                         break;
     662             :                                 }
     663             :                         }
     664             :                         case ZEND_GOTO:
     665          18 :                                 if (Z_TYPE(op_array->literals[opline->op2.constant]) != IS_LONG) {
     666           8 :                                         uint32_t num = opline->op2.constant;
     667           8 :                                         opline->op2.zv = &op_array->literals[opline->op2.constant];
     668           8 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     669           8 :                                         opline->op2.constant = num;                                  
     670             :                                 }
     671             :                                 /* break omitted intentionally */
     672             :                         case ZEND_JMP:
     673         120 :                                 zend_resolve_finally_call(op_array, i, opline->op1.opline_num TSRMLS_CC);
     674         115 :                                 break;
     675             :                         case ZEND_FAST_CALL:
     676          66 :                                 zend_resolve_fast_call(op_array, i, i TSRMLS_CC);
     677          66 :                                 break;
     678             :                         case ZEND_FAST_RET:
     679          62 :                                 zend_resolve_finally_ret(op_array, i TSRMLS_CC);
     680             :                                 break;
     681             :                         default:
     682             :                                 break;
     683             :                 }
     684             :         }
     685          35 : }
     686             : 
     687       64974 : ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC)
     688             : {
     689             :         zend_op *opline, *end;
     690             : 
     691       64974 :         if (!ZEND_USER_CODE(op_array->type)) {
     692           0 :                 return 0;
     693             :         }
     694       64974 :         if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
     695          42 :                 zend_resolve_finally_calls(op_array TSRMLS_CC);
     696             :         }
     697       64967 :         if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
     698           0 :                 zend_update_extended_info(op_array TSRMLS_CC);
     699             :         }
     700       64967 :         if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
     701       64758 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
     702             :         }
     703             : 
     704       64967 :         if (CG(context).vars_size != op_array->last_var) {
     705       47244 :                 op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
     706       47244 :                 CG(context).vars_size = op_array->last_var;
     707             :         }
     708       64967 :         if (CG(context).opcodes_size != op_array->last) {
     709       64664 :                 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
     710       64664 :                 CG(context).opcodes_size = op_array->last;
     711             :         }
     712       64967 :         if (CG(context).literals_size != op_array->last_literal) {
     713       63810 :                 op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
     714       63810 :                 CG(context).literals_size = op_array->last_literal;
     715             :         }
     716       64967 :         opline = op_array->opcodes;
     717       64967 :         end = opline + op_array->last;
     718     2418847 :         while (opline < end) {
     719     2288918 :                 if (opline->op1_type == IS_CONST) {
     720      440139 :                         opline->op1.zv = &op_array->literals[opline->op1.constant];
     721     1848779 :                 } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
     722      534036 :                         opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
     723             :                 }
     724     2288918 :                 if (opline->op2_type == IS_CONST) {
     725      573493 :                         opline->op2.zv = &op_array->literals[opline->op2.constant];
     726     1715425 :                 } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
     727      127334 :                         opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
     728             :                 }
     729     2288918 :                 if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
     730      971630 :                         opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
     731             :                 }
     732     2288918 :                 switch (opline->opcode) {
     733             :                         case ZEND_DECLARE_INHERITED_CLASS:
     734             :                         case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
     735        1285 :                                 opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
     736        1285 :                                 break;
     737             :                         case ZEND_GOTO:
     738          34 :                                 if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
     739          15 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     740             :                                 }
     741             :                                 /* break omitted intentionally */
     742             :                         case ZEND_JMP:
     743             :                         case ZEND_FAST_CALL:
     744       62289 :                                 opline->op1.jmp_addr = &op_array->opcodes[opline->op1.opline_num];
     745       62289 :                                 break;
     746             :                         case ZEND_JMPZNZ:
     747             :                                 /* absolute index to relative offset */
     748           0 :                                 opline->extended_value = (char*)(op_array->opcodes + opline->extended_value) - (char*)opline;
     749             :                                 /* break omitted intentionally */
     750             :                         case ZEND_JMPZ:
     751             :                         case ZEND_JMPNZ:
     752             :                         case ZEND_JMPZ_EX:
     753             :                         case ZEND_JMPNZ_EX:
     754             :                         case ZEND_JMP_SET:
     755             :                         case ZEND_COALESCE:
     756             :                         case ZEND_NEW:
     757             :                         case ZEND_FE_RESET:
     758             :                         case ZEND_FE_FETCH:
     759      149477 :                                 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.opline_num];
     760      149477 :                                 break;
     761             :                         case ZEND_RETURN:
     762             :                         case ZEND_RETURN_BY_REF:
     763      108795 :                                 if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
     764         166 :                                         if (opline->op1_type != IS_CONST || Z_TYPE_P(opline->op1.zv) != IS_NULL) {
     765           2 :                                                 CG(zend_lineno) = opline->lineno;
     766           2 :                                                 zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
     767             :                                         }
     768             : 
     769          82 :                                         opline->opcode = ZEND_GENERATOR_RETURN;
     770             :                                 }
     771             :                                 break;
     772             :                 }
     773     2288913 :                 ZEND_VM_SET_OPCODE_HANDLER(opline);
     774     2288913 :                 opline++;
     775             :         }
     776             : 
     777       64962 :         op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
     778       64962 :         return 0;
     779             : }
     780             : 
     781           0 : int pass_two_wrapper(zval *el TSRMLS_DC)
     782             : {
     783           0 :         return pass_two((zend_op_array *) Z_PTR_P(el) TSRMLS_CC);
     784             : }
     785             : 
     786           0 : int print_class(zend_class_entry *class_entry TSRMLS_DC)
     787             : {
     788           0 :         printf("Class %s:\n", class_entry->name->val);
     789           0 :         zend_hash_apply(&class_entry->function_table, pass_two_wrapper TSRMLS_CC);
     790           0 :         printf("End of class %s.\n\n", class_entry->name->val);
     791           0 :         return 0;
     792             : }
     793             : 
     794         175 : ZEND_API unary_op_type get_unary_op(int opcode)
     795             : {
     796         175 :         switch (opcode) {
     797             :                 case ZEND_BW_NOT:
     798           1 :                         return (unary_op_type) bitwise_not_function;
     799             :                 case ZEND_BOOL_NOT:
     800         174 :                         return (unary_op_type) boolean_not_function;
     801             :                 default:
     802           0 :                         return (unary_op_type) NULL;
     803             :         }
     804             : }
     805             : 
     806        3076 : ZEND_API binary_op_type get_binary_op(int opcode)
     807             : {
     808        3076 :         switch (opcode) {
     809             :                 case ZEND_ADD:
     810             :                 case ZEND_ASSIGN_ADD:
     811         163 :                         return (binary_op_type) add_function;
     812             :                 case ZEND_SUB:
     813             :                 case ZEND_ASSIGN_SUB:
     814         180 :                         return (binary_op_type) sub_function;
     815             :                 case ZEND_MUL:
     816             :                 case ZEND_ASSIGN_MUL:
     817          64 :                         return (binary_op_type) mul_function;
     818             :                 case ZEND_POW:
     819           8 :                         return (binary_op_type) pow_function;
     820             :                 case ZEND_DIV:
     821             :                 case ZEND_ASSIGN_DIV:
     822          27 :                         return (binary_op_type) div_function;
     823             :                 case ZEND_MOD:
     824             :                 case ZEND_ASSIGN_MOD:
     825           4 :                         return (binary_op_type) mod_function;
     826             :                 case ZEND_SL:
     827             :                 case ZEND_ASSIGN_SL:
     828          32 :                         return (binary_op_type) shift_left_function;
     829             :                 case ZEND_SR:
     830             :                 case ZEND_ASSIGN_SR:
     831           3 :                         return (binary_op_type) shift_right_function;
     832             :                 case ZEND_CONCAT:
     833             :                 case ZEND_ASSIGN_CONCAT:
     834        1865 :                         return (binary_op_type) concat_function;
     835             :                 case ZEND_IS_IDENTICAL:
     836           1 :                         return (binary_op_type) is_identical_function;
     837             :                 case ZEND_IS_NOT_IDENTICAL:
     838           8 :                         return (binary_op_type) is_not_identical_function;
     839             :                 case ZEND_IS_EQUAL:
     840          56 :                         return (binary_op_type) is_equal_function;
     841             :                 case ZEND_IS_NOT_EQUAL:
     842         322 :                         return (binary_op_type) is_not_equal_function;
     843             :                 case ZEND_IS_SMALLER:
     844          39 :                         return (binary_op_type) is_smaller_function;
     845             :                 case ZEND_IS_SMALLER_OR_EQUAL:
     846           6 :                         return (binary_op_type) is_smaller_or_equal_function;
     847             :                 case ZEND_BW_OR:
     848             :                 case ZEND_ASSIGN_BW_OR:
     849         287 :                         return (binary_op_type) bitwise_or_function;
     850             :                 case ZEND_BW_AND:
     851             :                 case ZEND_ASSIGN_BW_AND:
     852           0 :                         return (binary_op_type) bitwise_and_function;
     853             :                 case ZEND_BW_XOR:
     854             :                 case ZEND_ASSIGN_BW_XOR:
     855           9 :                         return (binary_op_type) bitwise_xor_function;
     856             :                 case ZEND_BOOL_XOR:
     857           2 :                         return (binary_op_type) boolean_xor_function;
     858             :                 default:
     859           0 :                         return (binary_op_type) NULL;
     860             :         }
     861             : }
     862             : 
     863             : /*
     864             :  * Local variables:
     865             :  * tab-width: 4
     866             :  * c-basic-offset: 4
     867             :  * indent-tabs-mode: t
     868             :  * End:
     869             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:08 +0000 (8 days ago)

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