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

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:25 +0000 (3 days ago)

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