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 - ext/opcache - zend_accelerator_util_funcs.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 290 449 64.6 %
Date: 2016-05-28 Functions: 19 21 90.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include "zend_API.h"
      23             : #include "zend_constants.h"
      24             : #include "zend_accelerator_util_funcs.h"
      25             : #include "zend_persist.h"
      26             : #include "zend_shared_alloc.h"
      27             : 
      28             : #if SIZEOF_SIZE_T <= SIZEOF_ZEND_LONG
      29             : /* If sizeof(void*) == sizeof(ulong) we can use zend_hash index functions */
      30             : # define accel_xlat_set(old, new)       zend_hash_index_add_new_ptr(&ZCG(bind_hash), (zend_ulong)(zend_uintptr_t)(old), (new))
      31             : # define accel_xlat_get(old)            zend_hash_index_find_ptr(&ZCG(bind_hash), (zend_ulong)(zend_uintptr_t)(old))
      32             : #else
      33             : # define accel_xlat_set(old, new)       zend_hash_str_add_new_ptr(&ZCG(bind_hash), (char*)&(old), sizeof(void*), (old))
      34             : # define accel_xlat_get(old)        zend_hash_str_find_ptr(&ZCG(bind_hash), (char*)&(old), sizeof(void*))
      35             : #endif
      36             : 
      37             : #define ARENA_REALLOC(ptr) \
      38             :         (void*)(((char*)(ptr)) + ((char*)ZCG(arena_mem) - (char*)ZCG(current_persistent_script)->arena_mem))
      39             : 
      40             : typedef int (*id_function_t)(void *, void *);
      41             : typedef void (*unique_copy_ctor_func_t)(void *pElement);
      42             : 
      43             : static zend_ast *zend_ast_clone(zend_ast *ast);
      44             : 
      45           4 : static void zend_accel_destroy_zend_function(zval *zv)
      46             : {
      47           4 :         zend_function *function = Z_PTR_P(zv);
      48             : 
      49           4 :         if (function->type == ZEND_USER_FUNCTION) {
      50           4 :                 if (function->op_array.static_variables) {
      51           0 :                         if (!(GC_FLAGS(function->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
      52           0 :                                 if (--GC_REFCOUNT(function->op_array.static_variables) == 0) {
      53           0 :                                         FREE_HASHTABLE(function->op_array.static_variables);
      54             :                                 }
      55             :                         }
      56           0 :                         function->op_array.static_variables = NULL;
      57             :                 }
      58             :         }
      59             : 
      60           4 :         destroy_zend_function(function);
      61           4 : }
      62             : 
      63           4 : static void zend_accel_destroy_zend_class(zval *zv)
      64             : {
      65           4 :         zend_class_entry *ce = Z_PTR_P(zv);
      66           4 :         ce->function_table.pDestructor = zend_accel_destroy_zend_function;
      67           4 :         destroy_zend_class(zv);
      68           4 : }
      69             : 
      70         433 : zend_persistent_script* create_persistent_script(void)
      71             : {
      72         433 :         zend_persistent_script *persistent_script = (zend_persistent_script *) emalloc(sizeof(zend_persistent_script));
      73         433 :         memset(persistent_script, 0, sizeof(zend_persistent_script));
      74             : 
      75         433 :         zend_hash_init(&persistent_script->script.function_table, 128, NULL, ZEND_FUNCTION_DTOR, 0);
      76             :         /* class_table is usually destroyed by free_persistent_script() that
      77             :          * overrides destructor. ZEND_CLASS_DTOR may be used by standard
      78             :          * PHP compiler
      79             :          */
      80         433 :         zend_hash_init(&persistent_script->script.class_table, 16, NULL, ZEND_CLASS_DTOR, 0);
      81             : 
      82         433 :         return persistent_script;
      83             : }
      84             : 
      85           3 : void free_persistent_script(zend_persistent_script *persistent_script, int destroy_elements)
      86             : {
      87           3 :         if (destroy_elements) {
      88           3 :                 persistent_script->script.function_table.pDestructor = zend_accel_destroy_zend_function;
      89           3 :                 persistent_script->script.class_table.pDestructor = zend_accel_destroy_zend_class;
      90             :         } else {
      91           0 :                 persistent_script->script.function_table.pDestructor = NULL;
      92           0 :                 persistent_script->script.class_table.pDestructor = NULL;
      93             :         }
      94             : 
      95           3 :         zend_hash_destroy(&persistent_script->script.function_table);
      96           3 :         zend_hash_destroy(&persistent_script->script.class_table);
      97             : 
      98           3 :         if (persistent_script->script.filename) {
      99           0 :                 zend_string_release(persistent_script->script.filename);
     100             :         }
     101             : 
     102           3 :         efree(persistent_script);
     103           3 : }
     104             : 
     105        6592 : static int is_not_internal_function(zval *zv)
     106             : {
     107        6592 :         zend_function *function = Z_PTR_P(zv);
     108        6592 :         return(function->type != ZEND_INTERNAL_FUNCTION);
     109             : }
     110             : 
     111           3 : void zend_accel_free_user_functions(HashTable *ht)
     112             : {
     113           3 :         dtor_func_t orig_dtor = ht->pDestructor;
     114             : 
     115           3 :         ht->pDestructor = NULL;
     116           3 :         zend_hash_apply(ht, (apply_func_t) is_not_internal_function);
     117           3 :         ht->pDestructor = orig_dtor;
     118           3 : }
     119             : 
     120         430 : void zend_accel_move_user_functions(HashTable *src, HashTable *dst)
     121             : {
     122             :         Bucket *p;
     123         430 :         dtor_func_t orig_dtor = src->pDestructor;
     124             : 
     125         430 :         src->pDestructor = NULL;
     126         430 :         zend_hash_extend(dst, dst->nNumUsed + src->nNumUsed, 0);
     127         954 :         ZEND_HASH_REVERSE_FOREACH_BUCKET(src, p) {
     128         454 :                 zend_function *function = Z_PTR(p->val);
     129             : 
     130         454 :                 if (EXPECTED(function->type == ZEND_USER_FUNCTION)) {
     131          24 :                         _zend_hash_append_ptr(dst, p->key, function);
     132          24 :                         zend_hash_del_bucket(src, p);
     133             :                 } else {
     134         430 :                         break;
     135             :                 }
     136             :         } ZEND_HASH_FOREACH_END();
     137         430 :         src->pDestructor = orig_dtor;
     138         430 : }
     139             : 
     140      830991 : static int copy_internal_function(zval *zv, HashTable *function_table)
     141             : {
     142      830991 :         zend_internal_function *function = Z_PTR_P(zv);
     143      830991 :         if (function->type == ZEND_INTERNAL_FUNCTION) {
     144      830991 :                 zend_hash_update_mem(function_table, function->function_name, function, sizeof(zend_internal_function));
     145             :         }
     146      830991 :         return 0;
     147             : }
     148             : 
     149         378 : void zend_accel_copy_internal_functions(void)
     150             : {
     151         378 :         zend_hash_apply_with_argument(CG(function_table), (apply_func_arg_t)copy_internal_function, &ZCG(function_table));
     152         378 :         ZCG(internal_functions_count) = zend_hash_num_elements(&ZCG(function_table));
     153         378 : }
     154             : 
     155           7 : static inline void zend_clone_zval(zval *src)
     156             : {
     157             :         void *ptr;
     158             : 
     159           7 :         if (Z_TYPE_P(src) == IS_REFERENCE) {
     160           0 :                 ptr = accel_xlat_get(Z_REF_P(src));
     161           0 :                 if (ptr != NULL) {
     162           0 :                         Z_REF_P(src) = ptr;
     163           0 :                         return;
     164             :                 } else {
     165           0 :                         zend_reference *old = Z_REF_P(src);
     166           0 :                         ZVAL_NEW_REF(src, &old->val);
     167           0 :                         Z_REF_P(src)->gc = old->gc;
     168           0 :                         accel_xlat_set(old, Z_REF_P(src));
     169           0 :                         src = Z_REFVAL_P(src);
     170             :                 }
     171             :         }
     172           7 :         if (Z_TYPE_P(src) == IS_CONSTANT_AST) {
     173           0 :                 if (Z_REFCOUNT_P(src) > 1 && (ptr = accel_xlat_get(Z_AST_P(src))) != NULL) {
     174           0 :                         Z_AST_P(src) = ptr;
     175             :                 } else {
     176           0 :                         zend_ast_ref *old = Z_AST_P(src);
     177             : 
     178           0 :                         ZVAL_NEW_AST(src, old->ast);
     179           0 :                         Z_AST_P(src)->gc = old->gc;
     180           0 :                         if (Z_REFCOUNT_P(src) > 1) {
     181           0 :                                 accel_xlat_set(old, Z_AST_P(src));
     182             :                         }
     183           0 :                         Z_ASTVAL_P(src) = zend_ast_clone(Z_ASTVAL_P(src));
     184             :                 }
     185             :         }
     186             : }
     187             : 
     188           0 : static zend_ast *zend_ast_clone(zend_ast *ast)
     189             : {
     190             :         uint32_t i;
     191             : 
     192           0 :         if (ast->kind == ZEND_AST_ZVAL) {
     193           0 :                 zend_ast_zval *copy = emalloc(sizeof(zend_ast_zval));
     194           0 :                 copy->kind = ZEND_AST_ZVAL;
     195           0 :                 copy->attr = ast->attr;
     196           0 :                 ZVAL_COPY_VALUE(&copy->val, zend_ast_get_zval(ast));
     197           0 :                 return (zend_ast *) copy;
     198           0 :         } else if (zend_ast_is_list(ast)) {
     199           0 :                 zend_ast_list *list = zend_ast_get_list(ast);
     200           0 :                 zend_ast_list *copy = emalloc(
     201             :                         sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
     202           0 :                 copy->kind = list->kind;
     203           0 :                 copy->attr = list->attr;
     204           0 :                 copy->children = list->children;
     205           0 :                 for (i = 0; i < list->children; i++) {
     206           0 :                         if (list->child[i]) {
     207           0 :                                 copy->child[i] = zend_ast_clone(list->child[i]);
     208             :                         } else {
     209           0 :                                 copy->child[i] = NULL;
     210             :                         }
     211             :                 }
     212           0 :                 return (zend_ast *) copy;
     213             :         } else {
     214           0 :                 uint32_t children = zend_ast_get_num_children(ast);
     215           0 :                 zend_ast *copy = emalloc(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
     216           0 :                 copy->kind = ast->kind;
     217           0 :                 copy->attr = ast->attr;
     218           0 :                 for (i = 0; i < children; i++) {
     219           0 :                         if (ast->child[i]) {
     220           0 :                                 copy->child[i] = zend_ast_clone(ast->child[i]);
     221             :                         } else {
     222           0 :                                 copy->child[i] = NULL;
     223             :                         }
     224             :                 }
     225           0 :                 return copy;
     226             :         }
     227             : }
     228             : 
     229           7 : static void zend_hash_clone_constants(HashTable *ht, HashTable *source)
     230             : {
     231             :         Bucket *p, *q, *end;
     232             :         zend_ulong nIndex;
     233             :         zend_class_constant *c;
     234             : 
     235           7 :         ht->nTableSize = source->nTableSize;
     236           7 :         ht->nTableMask = source->nTableMask;
     237           7 :         ht->nNumUsed = 0;
     238           7 :         ht->nNumOfElements = source->nNumOfElements;
     239           7 :         ht->nNextFreeElement = source->nNextFreeElement;
     240           7 :         ht->pDestructor = ZVAL_PTR_DTOR;
     241           7 :         ht->u.flags = (source->u.flags & HASH_FLAG_INITIALIZED) | HASH_FLAG_APPLY_PROTECTION;
     242           7 :         ht->nInternalPointer = source->nNumOfElements ? 0 : HT_INVALID_IDX;
     243             : 
     244           7 :         if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
     245           6 :                 ht->arData = source->arData;
     246           6 :                 return;
     247             :         }
     248             : 
     249             :         ZEND_ASSERT((source->u.flags & HASH_FLAG_PACKED) == 0);
     250           1 :         HT_SET_DATA_ADDR(ht, emalloc(HT_SIZE(ht)));
     251           1 :         HT_HASH_RESET(ht);
     252             : 
     253           1 :         p = source->arData;
     254           1 :         end = p + source->nNumUsed;
     255           6 :         for (; p != end; p++) {
     256          10 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     257           5 :                 nIndex = p->h | ht->nTableMask;
     258             : 
     259             :                 /* Insert into hash collision list */
     260           5 :                 q = ht->arData + ht->nNumUsed;
     261           5 :                 Z_NEXT(q->val) = HT_HASH(ht, nIndex);
     262           5 :                 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(ht->nNumUsed++);
     263             : 
     264             :                 /* Initialize key */
     265           5 :                 q->h = p->h;
     266           5 :                 q->key = p->key;
     267             : 
     268             :                 /* Copy data */
     269           5 :                 c = ARENA_REALLOC(Z_PTR(p->val));
     270           5 :                 ZVAL_PTR(&q->val, c);
     271             : 
     272           5 :                 zend_clone_zval(&c->value);
     273          10 :                 if ((void*)c->ce >= ZCG(current_persistent_script)->arena_mem &&
     274           5 :                     (void*)c->ce < (void*)((char*)ZCG(current_persistent_script)->arena_mem + ZCG(current_persistent_script)->arena_size)) {
     275           5 :                         c->ce = ARENA_REALLOC(c->ce);
     276             :                 }
     277             :         }
     278             : }
     279             : 
     280           7 : static void zend_hash_clone_methods(HashTable *ht, HashTable *source, zend_class_entry *old_ce, zend_class_entry *ce)
     281             : {
     282             :         Bucket *p, *q, *end;
     283             :         zend_ulong nIndex;
     284             :         zend_op_array *new_entry;
     285             : 
     286           7 :         ht->nTableSize = source->nTableSize;
     287           7 :         ht->nTableMask = source->nTableMask;
     288           7 :         ht->nNumUsed = 0;
     289           7 :         ht->nNumOfElements = source->nNumOfElements;
     290           7 :         ht->nNextFreeElement = source->nNextFreeElement;
     291           7 :         ht->pDestructor = ZEND_FUNCTION_DTOR;
     292           7 :         ht->u.flags = (source->u.flags & HASH_FLAG_INITIALIZED);
     293           7 :         ht->nInternalPointer = source->nNumOfElements ? 0 : HT_INVALID_IDX;
     294             : 
     295           7 :         if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
     296           2 :                 ht->arData = source->arData;
     297           2 :                 return;
     298             :         }
     299             : 
     300             :         ZEND_ASSERT(!(source->u.flags & HASH_FLAG_PACKED));
     301           5 :         HT_SET_DATA_ADDR(ht, emalloc(HT_SIZE(ht)));
     302           5 :         HT_HASH_RESET(ht);
     303             : 
     304           5 :         p = source->arData;
     305           5 :         end = p + source->nNumUsed;
     306          14 :         for (; p != end; p++) {
     307          18 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     308             : 
     309           9 :                 nIndex = p->h | ht->nTableMask;
     310             : 
     311             :                 /* Insert into hash collision list */
     312           9 :                 q = ht->arData + ht->nNumUsed;
     313           9 :                 Z_NEXT(q->val) = HT_HASH(ht, nIndex);
     314           9 :                 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(ht->nNumUsed++);
     315             : 
     316             :                 /* Initialize key */
     317           9 :                 q->h = p->h;
     318             :                 ZEND_ASSERT(p->key != NULL);
     319           9 :                 q->key = p->key;
     320             : 
     321             :                 /* Copy data */
     322           9 :                 ZVAL_PTR(&q->val, ARENA_REALLOC(Z_PTR(p->val)));
     323           9 :                 new_entry = (zend_op_array*)Z_PTR(q->val);
     324             : 
     325          18 :                 if ((void*)new_entry->scope >= ZCG(current_persistent_script)->arena_mem &&
     326           9 :                     (void*)new_entry->scope < (void*)((char*)ZCG(current_persistent_script)->arena_mem + ZCG(current_persistent_script)->arena_size)) {
     327             : 
     328           9 :                         new_entry->scope = ARENA_REALLOC(new_entry->scope);
     329             : 
     330             :                         /* update prototype */
     331           9 :                         if (new_entry->prototype) {
     332           2 :                                 new_entry->prototype = ARENA_REALLOC(new_entry->prototype);
     333             :                         }
     334             :                 }
     335             :         }
     336             : }
     337             : 
     338           7 : static void zend_hash_clone_prop_info(HashTable *ht, HashTable *source, zend_class_entry *old_ce)
     339             : {
     340             :         Bucket *p, *q, *end;
     341             :         zend_ulong nIndex;
     342             :         zend_property_info *prop_info;
     343             : 
     344           7 :         ht->nTableSize = source->nTableSize;
     345           7 :         ht->nTableMask = source->nTableMask;
     346           7 :         ht->nNumUsed = 0;
     347           7 :         ht->nNumOfElements = source->nNumOfElements;
     348           7 :         ht->nNextFreeElement = source->nNextFreeElement;
     349           7 :         ht->pDestructor = NULL;
     350           7 :         ht->u.flags = (source->u.flags & HASH_FLAG_INITIALIZED);
     351           7 :         ht->nInternalPointer = source->nNumOfElements ? 0 : HT_INVALID_IDX;
     352             : 
     353           7 :         if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
     354           6 :                 ht->arData = source->arData;
     355           6 :                 return;
     356             :         }
     357             : 
     358             :         ZEND_ASSERT(!(source->u.flags & HASH_FLAG_PACKED));
     359           1 :         HT_SET_DATA_ADDR(ht, emalloc(HT_SIZE(ht)));
     360           1 :         HT_HASH_RESET(ht);
     361             : 
     362           1 :         p = source->arData;
     363           1 :         end = p + source->nNumUsed;
     364           3 :         for (; p != end; p++) {
     365           4 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     366             : 
     367           2 :                 nIndex = p->h | ht->nTableMask;
     368             : 
     369             :                 /* Insert into hash collision list */
     370           2 :                 q = ht->arData + ht->nNumUsed;
     371           2 :                 Z_NEXT(q->val) = HT_HASH(ht, nIndex);
     372           2 :                 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(ht->nNumUsed++);
     373             : 
     374             :                 /* Initialize key */
     375           2 :                 q->h = p->h;
     376             :                 ZEND_ASSERT(p->key != NULL);
     377           2 :                 q->key = p->key;
     378             : 
     379             :                 /* Copy data */
     380           2 :                 prop_info = ARENA_REALLOC(Z_PTR(p->val));
     381           2 :                 ZVAL_PTR(&q->val, prop_info);
     382             : 
     383           4 :                 if ((void*)prop_info->ce >= ZCG(current_persistent_script)->arena_mem &&
     384           2 :                     (void*)prop_info->ce < (void*)((char*)ZCG(current_persistent_script)->arena_mem + ZCG(current_persistent_script)->arena_size)) {
     385           2 :                         prop_info->ce = ARENA_REALLOC(prop_info->ce);
     386             :                 }
     387             :         }
     388             : }
     389             : 
     390             : #define zend_update_inherited_handler(handler) \
     391             : { \
     392             :         if (ce->handler != NULL) { \
     393             :                 ce->handler = ARENA_REALLOC(ce->handler); \
     394             :         } \
     395             : }
     396             : 
     397             : /* Protects class' refcount, copies default properties, functions and class name */
     398           7 : static void zend_class_copy_ctor(zend_class_entry **pce)
     399             : {
     400           7 :         zend_class_entry *ce = *pce;
     401           7 :         zend_class_entry *old_ce = ce;
     402             :         zval *src, *dst, *end;
     403             : 
     404           7 :         *pce = ce = ARENA_REALLOC(old_ce);
     405           7 :         ce->refcount = 1;
     406             : 
     407           7 :         if (old_ce->default_properties_table) {
     408           0 :                 ce->default_properties_table = emalloc(sizeof(zval) * old_ce->default_properties_count);
     409           0 :                 src = old_ce->default_properties_table;
     410           0 :                 end = src + old_ce->default_properties_count;
     411           0 :                 dst = ce->default_properties_table;
     412           0 :                 for (; src != end; src++, dst++) {
     413           0 :                         ZVAL_COPY_VALUE(dst, src);
     414           0 :                         zend_clone_zval(dst);
     415             :                 }
     416             :         }
     417             : 
     418           7 :         zend_hash_clone_methods(&ce->function_table, &old_ce->function_table, old_ce, ce);
     419             : 
     420             :         /* static members */
     421           7 :         if (old_ce->default_static_members_table) {
     422           1 :                 ce->default_static_members_table = emalloc(sizeof(zval) * old_ce->default_static_members_count);
     423           1 :                 src = old_ce->default_static_members_table;
     424           1 :                 end = src + old_ce->default_static_members_count;
     425           1 :                 dst = ce->default_static_members_table;
     426           3 :                 for (; src != end; src++, dst++) {
     427           2 :                         ZVAL_COPY_VALUE(dst, src);
     428           2 :                         zend_clone_zval(dst);
     429             :                 }
     430             :         }
     431           7 :         ce->static_members_table = ce->default_static_members_table;
     432             : 
     433             :         /* properties_info */
     434           7 :         zend_hash_clone_prop_info(&ce->properties_info, &old_ce->properties_info, old_ce);
     435             : 
     436             :         /* constants table */
     437           7 :         zend_hash_clone_constants(&ce->constants_table, &old_ce->constants_table);
     438           7 :         ce->constants_table.u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
     439             : 
     440             :         /* interfaces aren't really implemented, so we create a new table */
     441           7 :         if (ce->num_interfaces) {
     442           0 :                 ce->interfaces = emalloc(sizeof(zend_class_entry *) * ce->num_interfaces);
     443           0 :                 memset(ce->interfaces, 0, sizeof(zend_class_entry *) * ce->num_interfaces);
     444             :         } else {
     445           7 :                 ce->interfaces = NULL;
     446             :         }
     447             : 
     448           7 :         if (ce->parent) {
     449           3 :                 ce->parent = ARENA_REALLOC(ce->parent);
     450             :         }
     451             : 
     452           7 :         zend_update_inherited_handler(constructor);
     453           7 :         zend_update_inherited_handler(destructor);
     454           7 :         zend_update_inherited_handler(clone);
     455           7 :         zend_update_inherited_handler(__get);
     456           7 :         zend_update_inherited_handler(__set);
     457           7 :         zend_update_inherited_handler(__call);
     458             : /* 5.1 stuff */
     459           7 :         zend_update_inherited_handler(serialize_func);
     460           7 :         zend_update_inherited_handler(unserialize_func);
     461           7 :         zend_update_inherited_handler(__isset);
     462           7 :         zend_update_inherited_handler(__unset);
     463             : /* 5.2 stuff */
     464           7 :         zend_update_inherited_handler(__tostring);
     465             : 
     466             : /* 5.3 stuff */
     467           7 :         zend_update_inherited_handler(__callstatic);
     468           7 :         zend_update_inherited_handler(__debugInfo);
     469             : 
     470             : /* 5.4 traits */
     471           7 :         if (ce->trait_aliases) {
     472             :                 zend_trait_alias **trait_aliases;
     473           0 :                 int i = 0;
     474             : 
     475           0 :                 while (ce->trait_aliases[i]) {
     476           0 :                         i++;
     477             :                 }
     478           0 :                 trait_aliases = emalloc(sizeof(zend_trait_alias*) * (i + 1));
     479           0 :                 i = 0;
     480           0 :                 while (ce->trait_aliases[i]) {
     481           0 :                         trait_aliases[i] = emalloc(sizeof(zend_trait_alias));
     482           0 :                         memcpy(trait_aliases[i], ce->trait_aliases[i], sizeof(zend_trait_alias));
     483           0 :                         trait_aliases[i]->trait_method = emalloc(sizeof(zend_trait_method_reference));
     484           0 :                         memcpy(trait_aliases[i]->trait_method, ce->trait_aliases[i]->trait_method, sizeof(zend_trait_method_reference));
     485           0 :                         i++;
     486             :                 }
     487           0 :                 trait_aliases[i] = NULL;
     488           0 :                 ce->trait_aliases = trait_aliases;
     489             :         }
     490             : 
     491           7 :         if (ce->trait_precedences) {
     492             :                 zend_trait_precedence **trait_precedences;
     493           0 :                 int i = 0;
     494             : 
     495           0 :                 while (ce->trait_precedences[i]) {
     496           0 :                         i++;
     497             :                 }
     498           0 :                 trait_precedences = emalloc(sizeof(zend_trait_precedence*) * (i + 1));
     499           0 :                 i = 0;
     500           0 :                 while (ce->trait_precedences[i]) {
     501           0 :                         trait_precedences[i] = emalloc(sizeof(zend_trait_precedence));
     502           0 :                         memcpy(trait_precedences[i], ce->trait_precedences[i], sizeof(zend_trait_precedence));
     503           0 :                         trait_precedences[i]->trait_method = emalloc(sizeof(zend_trait_method_reference));
     504           0 :                         memcpy(trait_precedences[i]->trait_method, ce->trait_precedences[i]->trait_method, sizeof(zend_trait_method_reference));
     505             : 
     506           0 :                         if (trait_precedences[i]->exclude_from_classes) {
     507             :                                 zend_string **exclude_from_classes;
     508           0 :                                 int j = 0;
     509             : 
     510           0 :                                 while (trait_precedences[i]->exclude_from_classes[j].class_name) {
     511           0 :                                         j++;
     512             :                                 }
     513           0 :                                 exclude_from_classes = emalloc(sizeof(zend_string*) * (j + 1));
     514           0 :                                 j = 0;
     515           0 :                                 while (trait_precedences[i]->exclude_from_classes[j].class_name) {
     516           0 :                                         exclude_from_classes[j] =
     517           0 :                                                 trait_precedences[i]->exclude_from_classes[j].class_name;
     518           0 :                                         j++;
     519             :                                 }
     520           0 :                                 exclude_from_classes[j] = NULL;
     521           0 :                                 trait_precedences[i]->exclude_from_classes = (void*)exclude_from_classes;
     522             :                         }
     523           0 :                         i++;
     524             :                 }
     525           0 :                 trait_precedences[i] = NULL;
     526           0 :                 ce->trait_precedences = trait_precedences;
     527             :         }
     528           7 : }
     529             : 
     530           0 : static void zend_accel_function_hash_copy(HashTable *target, HashTable *source)
     531             : {
     532             :         zend_function *function1, *function2;
     533             :         Bucket *p, *end;
     534             :         zval *t;
     535             : 
     536           0 :         zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
     537           0 :         p = source->arData;
     538           0 :         end = p + source->nNumUsed;
     539           0 :         for (; p != end; p++) {
     540           0 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     541             :                 ZEND_ASSERT(p->key);
     542           0 :                 t = zend_hash_find(target, p->key);
     543           0 :                 if (UNEXPECTED(t != NULL)) {
     544           0 :                         if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
     545             :                                 /* Mangled key */
     546           0 :                                 t = zend_hash_update(target, p->key, &p->val);
     547             :                         } else {
     548             :                                 goto failure;
     549             :                         }
     550             :                 } else {
     551           0 :                         _zend_hash_append_ptr(target, p->key, Z_PTR(p->val));
     552             :                 }
     553             :         }
     554           0 :         target->nInternalPointer = target->nNumOfElements ? 0 : HT_INVALID_IDX;
     555           0 :         return;
     556             : 
     557             : failure:
     558           0 :         function1 = Z_PTR(p->val);
     559           0 :         function2 = Z_PTR_P(t);
     560           0 :         CG(in_compilation) = 1;
     561           0 :         zend_set_compiled_filename(function1->op_array.filename);
     562           0 :         CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
     563           0 :         if (function2->type == ZEND_USER_FUNCTION
     564           0 :                 && function2->op_array.last > 0) {
     565           0 :                 zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
     566           0 :                                    ZSTR_VAL(function1->common.function_name),
     567           0 :                                    ZSTR_VAL(function2->op_array.filename),
     568           0 :                                    (int)function2->op_array.opcodes[0].lineno);
     569             :         } else {
     570           0 :                 zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name));
     571             :         }
     572             : }
     573             : 
     574          24 : static void zend_accel_function_hash_copy_from_shm(HashTable *target, HashTable *source)
     575             : {
     576             :         zend_function *function1, *function2;
     577             :         Bucket *p, *end;
     578             :         zval *t;
     579             : 
     580          24 :         zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
     581          24 :         p = source->arData;
     582          24 :         end = p + source->nNumUsed;
     583          48 :         for (; p != end; p++) {
     584          48 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     585             :                 ZEND_ASSERT(p->key);
     586          24 :                 t = zend_hash_find(target, p->key);
     587          24 :                 if (UNEXPECTED(t != NULL)) {
     588           0 :                         if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
     589             :                                 /* Mangled key */
     590           0 :                                 zend_hash_update_ptr(target, p->key, ARENA_REALLOC(Z_PTR(p->val)));
     591             :                         } else {
     592             :                                 goto failure;
     593             :                         }
     594             :                 } else {
     595          24 :                         _zend_hash_append_ptr(target, p->key, ARENA_REALLOC(Z_PTR(p->val)));
     596             :                 }
     597             :         }
     598          24 :         target->nInternalPointer = target->nNumOfElements ? 0 : HT_INVALID_IDX;
     599          24 :         return;
     600             : 
     601             : failure:
     602           0 :         function1 = Z_PTR(p->val);
     603           0 :         function2 = Z_PTR_P(t);
     604           0 :         CG(in_compilation) = 1;
     605           0 :         zend_set_compiled_filename(function1->op_array.filename);
     606           0 :         CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
     607           0 :         if (function2->type == ZEND_USER_FUNCTION
     608           0 :                 && function2->op_array.last > 0) {
     609           0 :                 zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
     610           0 :                                    ZSTR_VAL(function1->common.function_name),
     611           0 :                                    ZSTR_VAL(function2->op_array.filename),
     612           0 :                                    (int)function2->op_array.opcodes[0].lineno);
     613             :         } else {
     614           0 :                 zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name));
     615             :         }
     616             : }
     617             : 
     618           4 : static void zend_accel_class_hash_copy(HashTable *target, HashTable *source, unique_copy_ctor_func_t pCopyConstructor)
     619             : {
     620             :         zend_class_entry *ce1;
     621             :         Bucket *p, *end;
     622             :         zval *t;
     623             : 
     624           4 :         zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
     625           4 :         p = source->arData;
     626           4 :         end = p + source->nNumUsed;
     627          18 :         for (; p != end; p++) {
     628          28 :                 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
     629             :                 ZEND_ASSERT(p->key);
     630           7 :                 t = zend_hash_find(target, p->key);
     631           7 :                 if (UNEXPECTED(t != NULL)) {
     632           0 :                         if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
     633             :                                 /* Mangled key - ignore and wait for runtime */
     634           0 :                                 continue;
     635           0 :                         } else if (UNEXPECTED(!ZCG(accel_directives).ignore_dups)) {
     636           0 :                                 goto failure;
     637             :                         }
     638             :                 } else {
     639          14 :                         t = _zend_hash_append_ptr(target, p->key, Z_PTR(p->val));
     640           7 :                         if (pCopyConstructor) {
     641           7 :                                 pCopyConstructor(&Z_PTR_P(t));
     642             :                         }
     643             :                 }
     644             :         }
     645           4 :         target->nInternalPointer = target->nNumOfElements ? 0 : HT_INVALID_IDX;
     646           4 :         return;
     647             : 
     648             : failure:
     649           0 :         ce1 = Z_PTR(p->val);
     650           0 :         CG(in_compilation) = 1;
     651           0 :         zend_set_compiled_filename(ce1->info.user.filename);
     652           0 :         CG(zend_lineno) = ce1->info.user.line_start;
     653           0 :         zend_error(E_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce1), ZSTR_VAL(ce1->name));
     654             : }
     655             : 
     656             : #ifdef __SSE2__
     657             : #include <mmintrin.h>
     658             : #include <emmintrin.h>
     659             : 
     660             : static zend_always_inline void fast_memcpy(void *dest, const void *src, size_t size)
     661             : {
     662          28 :         __m128i *dqdest = (__m128i*)dest;
     663          28 :         const __m128i *dqsrc  = (const __m128i*)src;
     664          28 :         const __m128i *end  = (const __m128i*)((const char*)src + size);
     665             : 
     666             :         do {
     667         186 :                 _mm_prefetch(dqsrc + 4, _MM_HINT_NTA);
     668         186 :                 _mm_prefetch(dqdest + 4, _MM_HINT_T0);
     669             : 
     670         186 :                 __m128i xmm0 = _mm_load_si128(dqsrc + 0);
     671         372 :                 __m128i xmm1 = _mm_load_si128(dqsrc + 1);
     672         372 :                 __m128i xmm2 = _mm_load_si128(dqsrc + 2);
     673         372 :                 __m128i xmm3 = _mm_load_si128(dqsrc + 3);
     674         186 :                 dqsrc  += 4;
     675             :                 _mm_store_si128(dqdest + 0, xmm0);
     676         186 :                 _mm_store_si128(dqdest + 1, xmm1);
     677         186 :                 _mm_store_si128(dqdest + 2, xmm2);
     678         186 :                 _mm_store_si128(dqdest + 3, xmm3);
     679         186 :                 dqdest += 4;
     680         186 :         } while (dqsrc != end);
     681             : }
     682             : #endif
     683             : 
     684         430 : zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script, int from_shared_memory)
     685             : {
     686             :         zend_op_array *op_array;
     687             : 
     688         430 :         op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
     689         430 :         *op_array = persistent_script->script.main_op_array;
     690             : 
     691         430 :         if (EXPECTED(from_shared_memory)) {
     692         430 :                 zend_hash_init(&ZCG(bind_hash), 10, NULL, NULL, 0);
     693             : 
     694         430 :                 ZCG(current_persistent_script) = persistent_script;
     695         430 :                 ZCG(arena_mem) = NULL;
     696         430 :                 if (EXPECTED(persistent_script->arena_size)) {
     697             : #ifdef __SSE2__
     698             :                         /* Target address must be aligned to 64-byte boundary */
     699          56 :                         ZCG(arena_mem) = zend_arena_alloc(&CG(arena), persistent_script->arena_size + 64);
     700          28 :                         ZCG(arena_mem) = (void*)(((zend_uintptr_t)ZCG(arena_mem) + 63L) & ~63L);
     701          28 :                         fast_memcpy(ZCG(arena_mem), persistent_script->arena_mem, persistent_script->arena_size);
     702             : #else
     703             :                         ZCG(arena_mem) = zend_arena_alloc(&CG(arena), persistent_script->arena_size);
     704             :                         memcpy(ZCG(arena_mem), persistent_script->arena_mem, persistent_script->arena_size);
     705             : #endif
     706             :                 }
     707             : 
     708             :                 /* Copy all the necessary stuff from shared memory to regular memory, and protect the shared script */
     709         430 :                 if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
     710           4 :                         zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table, (unique_copy_ctor_func_t) zend_class_copy_ctor);
     711             :                 }
     712             :                 /* we must first to copy all classes and then prepare functions, since functions may try to bind
     713             :                    classes - which depend on pre-bind class entries existent in the class table */
     714         430 :                 if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
     715          24 :                         zend_accel_function_hash_copy_from_shm(CG(function_table), &persistent_script->script.function_table);
     716             :                 }
     717             : 
     718             :                 /* Register __COMPILER_HALT_OFFSET__ constant */
     719         525 :                 if (persistent_script->compiler_halt_offset != 0 &&
     720          95 :                     persistent_script->script.filename) {
     721             :                         zend_string *name;
     722          95 :                         char haltoff[] = "__COMPILER_HALT_OFFSET__";
     723             : 
     724          95 :                         name = zend_mangle_property_name(haltoff, sizeof(haltoff) - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename), 0);
     725          95 :                         if (!zend_hash_exists(EG(zend_constants), name)) {
     726           0 :                                 zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), persistent_script->compiler_halt_offset, CONST_CS, 0);
     727             :                         }
     728             :                         zend_string_release(name);
     729             :                 }
     730             : 
     731         430 :                 zend_hash_destroy(&ZCG(bind_hash));
     732         430 :                 ZCG(current_persistent_script) = NULL;
     733             :         } else /* if (!from_shared_memory) */ {
     734           0 :                 if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
     735           0 :                         zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
     736             :                 }
     737           0 :                 if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
     738           0 :                         zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table, NULL);
     739             :                 }
     740             :         }
     741             : 
     742         430 :         if (op_array->early_binding != (uint32_t)-1) {
     743           0 :                 zend_string *orig_compiled_filename = CG(compiled_filename);
     744           0 :                 CG(compiled_filename) = persistent_script->script.filename;
     745           0 :                 zend_do_delayed_early_binding(op_array);
     746           0 :                 CG(compiled_filename) = orig_compiled_filename;
     747             :         }
     748             : 
     749         430 :         if (UNEXPECTED(!from_shared_memory)) {
     750           0 :                 free_persistent_script(persistent_script, 0); /* free only hashes */
     751             :         }
     752             : 
     753         430 :         return op_array;
     754             : }
     755             : 
     756             : /*
     757             :  * zend_adler32() is based on zlib implementation
     758             :  * Computes the Adler-32 checksum of a data stream
     759             :  *
     760             :  * Copyright (C) 1995-2005 Mark Adler
     761             :  * For conditions of distribution and use, see copyright notice in zlib.h
     762             :  *
     763             :  * Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
     764             :  *
     765             :  *  This software is provided 'as-is', without any express or implied
     766             :  *  warranty.  In no event will the authors be held liable for any damages
     767             :  *  arising from the use of this software.
     768             :  *
     769             :  *  Permission is granted to anyone to use this software for any purpose,
     770             :  *  including commercial applications, and to alter it and redistribute it
     771             :  *  freely, subject to the following restrictions:
     772             :  *
     773             :  *  1. The origin of this software must not be misrepresented; you must not
     774             :  *     claim that you wrote the original software. If you use this software
     775             :  *     in a product, an acknowledgment in the product documentation would be
     776             :  *     appreciated but is not required.
     777             :  *  2. Altered source versions must be plainly marked as such, and must not be
     778             :  *     misrepresented as being the original software.
     779             :  *  3. This notice may not be removed or altered from any source distribution.
     780             :  *
     781             :  */
     782             : 
     783             : #define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
     784             : #define ADLER32_NMAX 5552
     785             : /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
     786             : 
     787             : #define ADLER32_DO1(buf)        {s1 += *(buf); s2 += s1;}
     788             : #define ADLER32_DO2(buf, i)     ADLER32_DO1(buf + i); ADLER32_DO1(buf + i + 1);
     789             : #define ADLER32_DO4(buf, i)     ADLER32_DO2(buf, i); ADLER32_DO2(buf, i + 2);
     790             : #define ADLER32_DO8(buf, i)     ADLER32_DO4(buf, i); ADLER32_DO4(buf, i + 4);
     791             : #define ADLER32_DO16(buf)       ADLER32_DO8(buf, 0); ADLER32_DO8(buf, 8);
     792             : 
     793         860 : unsigned int zend_adler32(unsigned int checksum, signed char *buf, uint len)
     794             : {
     795         860 :         unsigned int s1 = checksum & 0xffff;
     796         860 :         unsigned int s2 = (checksum >> 16) & 0xffff;
     797             :         signed char *end;
     798             : 
     799        1726 :         while (len >= ADLER32_NMAX) {
     800           6 :                 len -= ADLER32_NMAX;
     801           6 :                 end = buf + ADLER32_NMAX;
     802             :                 do {
     803        2082 :                         ADLER32_DO16(buf);
     804        2082 :                         buf += 16;
     805        2082 :                 } while (buf != end);
     806           6 :                 s1 %= ADLER32_BASE;
     807           6 :                 s2 %= ADLER32_BASE;
     808             :         }
     809             : 
     810         860 :         if (len) {
     811         860 :                 if (len >= 16) {
     812         860 :                         end = buf + (len & 0xfff0);
     813         860 :                         len &= 0xf;
     814             :                         do {
     815       29481 :                                 ADLER32_DO16(buf);
     816       29481 :                                 buf += 16;
     817       29481 :                         } while (buf != end);
     818             :                 }
     819         860 :                 if (len) {
     820         824 :                         end = buf + len;
     821             :                         do {
     822        6592 :                                 ADLER32_DO1(buf);
     823        6592 :                                 buf++;
     824        6592 :                         } while (buf != end);
     825             :                 }
     826         860 :                 s1 %= ADLER32_BASE;
     827         860 :                 s2 %= ADLER32_BASE;
     828             :         }
     829             : 
     830         860 :         return (s2 << 16) | s1;
     831             : }
     832             : 
     833         430 : unsigned int zend_accel_script_checksum(zend_persistent_script *persistent_script)
     834             : {
     835         430 :         signed char *mem = (signed char*)persistent_script->mem;
     836         430 :         size_t size = persistent_script->size;
     837         430 :         size_t persistent_script_check_block_size = ((char *)&(persistent_script->dynamic_members)) - (char *)persistent_script;
     838         430 :         unsigned int checksum = ADLER32_INIT;
     839             : 
     840         430 :         if (mem < (signed char*)persistent_script) {
     841           0 :                 checksum = zend_adler32(checksum, mem, (signed char*)persistent_script - mem);
     842           0 :                 size -= (signed char*)persistent_script - mem;
     843           0 :                 mem  += (signed char*)persistent_script - mem;
     844             :         }
     845             : 
     846         430 :         zend_adler32(checksum, mem, persistent_script_check_block_size);
     847         430 :         mem  += sizeof(*persistent_script);
     848         430 :         size -= sizeof(*persistent_script);
     849             : 
     850         430 :         if (size > 0) {
     851         430 :                 checksum = zend_adler32(checksum, mem, size);
     852             :         }
     853         430 :         return checksum;
     854             : }

Generated by: LCOV version 1.10

Generated at Sun, 29 May 2016 00:48:27 +0000 (20 hours ago)

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