2013-02-13 20:26:47 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2013-03-19 14:32:24 +08:00
|
|
|
| Zend OPcache |
|
2013-02-13 20:26:47 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1998-2014 The PHP Group |
|
2013-02-13 20:26:47 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_01.txt |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
| Stanislav Malyshev <stas@zend.com> |
|
|
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zend_API.h"
|
|
|
|
#include "zend_constants.h"
|
|
|
|
#include "zend_accelerator_util_funcs.h"
|
|
|
|
#include "zend_persist.h"
|
|
|
|
#include "zend_shared_alloc.h"
|
|
|
|
|
|
|
|
#define ZEND_PROTECTED_REFCOUNT (1<<30)
|
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
static uint32_t zend_accel_refcount = ZEND_PROTECTED_REFCOUNT;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-08-26 03:20:44 +08:00
|
|
|
#if SIZEOF_SIZE_T <= SIZEOF_ZEND_LONG
|
2013-02-13 20:26:47 +08:00
|
|
|
/* If sizeof(void*) == sizeof(ulong) we can use zend_hash index functions */
|
2014-08-26 01:24:55 +08:00
|
|
|
# define accel_xlat_set(old, new) zend_hash_index_update_ptr(&ZCG(bind_hash), (zend_ulong)(zend_uintptr_t)(old), (new))
|
|
|
|
# define accel_xlat_get(old) zend_hash_index_find_ptr(&ZCG(bind_hash), (zend_ulong)(zend_uintptr_t)(old))
|
2013-02-13 20:26:47 +08:00
|
|
|
#else
|
2014-08-26 01:24:55 +08:00
|
|
|
# define accel_xlat_set(old, new) (zend_hash_str_add_ptr(&ZCG(bind_hash), (char*)&(old), sizeof(void*), (zend_ulong)(zend_uintptr_t)(old), (void**)&(new))
|
|
|
|
# define accel_xlat_get(old, new) ((new) = zend_hash_str_find_ptr(&ZCG(bind_hash), (char*)&(old), sizeof(void*), (zend_ulong)(zend_uintptr_t)(old), (void**)&(new)))
|
2013-02-13 20:26:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef int (*id_function_t)(void *, void *);
|
|
|
|
typedef void (*unique_copy_ctor_func_t)(void *pElement);
|
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
static const uint32_t uninitialized_bucket = {INVALID_IDX};
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
static int zend_prepare_function_for_execution(zend_op_array *op_array);
|
|
|
|
static void zend_hash_clone_zval(HashTable *ht, HashTable *source, int bind);
|
2014-03-29 03:34:49 +08:00
|
|
|
static zend_ast *zend_ast_clone(zend_ast *ast TSRMLS_DC);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-03-31 20:09:45 +08:00
|
|
|
static void zend_accel_destroy_zend_function(zval *zv)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-31 20:09:45 +08:00
|
|
|
zend_function *function = Z_PTR_P(zv);
|
2013-02-13 20:26:47 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
if (function->type == ZEND_USER_FUNCTION) {
|
|
|
|
if (function->op_array.static_variables) {
|
|
|
|
|
2014-04-25 15:29:35 +08:00
|
|
|
FREE_HASHTABLE(function->op_array.static_variables);
|
2013-02-13 20:26:47 +08:00
|
|
|
function->op_array.static_variables = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_zend_function(function TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
static void zend_accel_destroy_zend_class(zval *zv)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_class_entry *ce = Z_PTR_P(zv);
|
2014-03-31 20:09:45 +08:00
|
|
|
ce->function_table.pDestructor = zend_accel_destroy_zend_function;
|
2014-03-29 03:34:49 +08:00
|
|
|
destroy_zend_class(zv);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_persistent_script* create_persistent_script(void)
|
|
|
|
{
|
|
|
|
zend_persistent_script *persistent_script = (zend_persistent_script *) emalloc(sizeof(zend_persistent_script));
|
|
|
|
memset(persistent_script, 0, sizeof(zend_persistent_script));
|
|
|
|
|
2014-04-21 22:25:34 +08:00
|
|
|
zend_hash_init(&persistent_script->function_table, 128, NULL, (dtor_func_t) zend_accel_destroy_zend_function, 0);
|
2013-03-12 02:49:05 +08:00
|
|
|
/* class_table is usually destroyed by free_persistent_script() that
|
2013-02-13 20:26:47 +08:00
|
|
|
* overrides destructor. ZEND_CLASS_DTOR may be used by standard
|
|
|
|
* PHP compiler
|
|
|
|
*/
|
2014-04-21 22:25:34 +08:00
|
|
|
zend_hash_init(&persistent_script->class_table, 16, NULL, ZEND_CLASS_DTOR, 0);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
return persistent_script;
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:22:19 +08:00
|
|
|
static int compact_hash_table(HashTable *ht)
|
|
|
|
{
|
|
|
|
uint i = 3;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint j;
|
2013-06-10 15:22:19 +08:00
|
|
|
uint nSize;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *d;
|
|
|
|
Bucket *p;
|
2013-06-10 15:22:19 +08:00
|
|
|
|
2014-04-21 22:25:34 +08:00
|
|
|
if (!ht->nNumOfElements || (ht->u.flags & HASH_FLAG_PACKED)) {
|
2013-06-10 15:22:19 +08:00
|
|
|
/* Empty tables don't allocate space for Buckets */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ht->nNumOfElements >= 0x80000000) {
|
|
|
|
/* prevent overflow */
|
|
|
|
nSize = 0x80000000;
|
|
|
|
} else {
|
|
|
|
while ((1U << i) < ht->nNumOfElements) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
nSize = 1 << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nSize >= ht->nTableSize) {
|
|
|
|
/* Keep the size */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
d = (Bucket *)pemalloc(nSize * (sizeof(Bucket) + sizeof(uint32_t)), ht->u.flags & HASH_FLAG_PERSISTENT);
|
2014-04-07 22:22:03 +08:00
|
|
|
if (!d) {
|
2013-06-10 15:22:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (i = 0, j = 0; i < ht->nNumUsed; i++) {
|
|
|
|
p = ht->arData + i;
|
2014-03-29 03:34:49 +08:00
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
2014-02-10 14:04:30 +08:00
|
|
|
d[j++] = *p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ht->nNumUsed = j;
|
|
|
|
|
2014-04-21 22:25:34 +08:00
|
|
|
pefree(ht->arData, ht->u.flags & HASH_FLAG_PERSISTENT);
|
2013-06-10 15:22:19 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arData = d;
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arHash = (uint32_t *)(d + nSize);
|
2013-06-10 15:22:19 +08:00
|
|
|
ht->nTableSize = nSize;
|
|
|
|
ht->nTableMask = ht->nTableSize - 1;
|
|
|
|
zend_hash_rehash(ht);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compact_persistent_script(zend_persistent_script *persistent_script)
|
|
|
|
{
|
|
|
|
return compact_hash_table(&persistent_script->function_table) &&
|
|
|
|
compact_hash_table(&persistent_script->class_table);
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:26:47 +08:00
|
|
|
void free_persistent_script(zend_persistent_script *persistent_script, int destroy_elements)
|
|
|
|
{
|
|
|
|
if (destroy_elements) {
|
2014-03-31 20:09:45 +08:00
|
|
|
persistent_script->function_table.pDestructor = zend_accel_destroy_zend_function;
|
|
|
|
persistent_script->class_table.pDestructor = zend_accel_destroy_zend_class;
|
2013-02-13 20:26:47 +08:00
|
|
|
} else {
|
|
|
|
persistent_script->function_table.pDestructor = NULL;
|
|
|
|
persistent_script->class_table.pDestructor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_hash_destroy(&persistent_script->function_table);
|
|
|
|
zend_hash_destroy(&persistent_script->class_table);
|
|
|
|
|
|
|
|
if (persistent_script->full_path) {
|
|
|
|
efree(persistent_script->full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(persistent_script);
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
static int is_not_internal_function(zval *zv)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_function *function = Z_PTR_P(zv);
|
2013-02-13 20:26:47 +08:00
|
|
|
return(function->type != ZEND_INTERNAL_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zend_accel_free_user_functions(HashTable *ht TSRMLS_DC)
|
|
|
|
{
|
|
|
|
dtor_func_t orig_dtor = ht->pDestructor;
|
|
|
|
|
|
|
|
ht->pDestructor = NULL;
|
|
|
|
zend_hash_apply(ht, (apply_func_t) is_not_internal_function TSRMLS_CC);
|
|
|
|
ht->pDestructor = orig_dtor;
|
|
|
|
}
|
|
|
|
|
2014-08-15 16:40:07 +08:00
|
|
|
static int move_user_function(zval *zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_function *function = Z_PTR_P(zv);
|
2013-02-13 20:26:47 +08:00
|
|
|
HashTable *function_table = va_arg(args, HashTable *);
|
|
|
|
(void)num_args; /* keep the compiler happy */
|
|
|
|
|
2013-02-22 14:56:05 +08:00
|
|
|
if (function->type == ZEND_USER_FUNCTION) {
|
2014-03-31 20:09:45 +08:00
|
|
|
zend_hash_update_ptr(function_table, hash_key->key, function);
|
2013-02-13 20:26:47 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void zend_accel_move_user_functions(HashTable *src, HashTable *dst TSRMLS_DC)
|
|
|
|
{
|
|
|
|
dtor_func_t orig_dtor = src->pDestructor;
|
|
|
|
|
|
|
|
src->pDestructor = NULL;
|
|
|
|
zend_hash_apply_with_arguments(src TSRMLS_CC, (apply_func_args_t)move_user_function, 1, dst);
|
|
|
|
src->pDestructor = orig_dtor;
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
static int copy_internal_function(zval *zv, HashTable *function_table TSRMLS_DC)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_internal_function *function = Z_PTR_P(zv);
|
2013-02-22 14:56:05 +08:00
|
|
|
if (function->type == ZEND_INTERNAL_FUNCTION) {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_hash_update_mem(function_table, function->function_name, function, sizeof(zend_internal_function));
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zend_accel_copy_internal_functions(TSRMLS_D)
|
|
|
|
{
|
|
|
|
zend_hash_apply_with_argument(CG(function_table), (apply_func_arg_t)copy_internal_function, &ZCG(function_table) TSRMLS_CC);
|
|
|
|
ZCG(internal_functions_count) = zend_hash_num_elements(&ZCG(function_table));
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:09:45 +08:00
|
|
|
static void zend_destroy_property_info(zval *zv)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-03-31 20:09:45 +08:00
|
|
|
zend_property_info *property_info = Z_PTR_P(zv);
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_release(property_info->name);
|
2013-02-22 14:56:05 +08:00
|
|
|
if (property_info->doc_comment) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_release(property_info->doc_comment);
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:28:27 +08:00
|
|
|
static inline zend_string *zend_clone_str(zend_string *str TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_string *ret;
|
|
|
|
|
|
|
|
if (IS_INTERNED(str)) {
|
|
|
|
ret = str;
|
2014-09-19 19:41:01 +08:00
|
|
|
} else if (GC_REFCOUNT(str) <= 1 || (ret = accel_xlat_get(str)) == NULL) {
|
2014-08-26 01:24:55 +08:00
|
|
|
ret = zend_string_dup(str, 0);
|
2014-04-16 20:28:27 +08:00
|
|
|
GC_FLAGS(ret) = GC_FLAGS(str);
|
2014-09-19 19:41:01 +08:00
|
|
|
if (GC_REFCOUNT(str) > 1) {
|
2014-04-16 20:28:27 +08:00
|
|
|
accel_xlat_set(str, ret);
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-19 19:41:01 +08:00
|
|
|
GC_REFCOUNT(ret)++;
|
2014-04-16 20:28:27 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
static inline void zend_clone_zval(zval *src, int bind TSRMLS_DC)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2014-05-29 22:21:56 +08:00
|
|
|
if (Z_IMMUTABLE_P(src)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:26:23 +08:00
|
|
|
switch (Z_TYPE_P(src)) {
|
2014-03-29 03:34:49 +08:00
|
|
|
case IS_STRING:
|
|
|
|
case IS_CONSTANT:
|
2014-04-16 20:28:27 +08:00
|
|
|
Z_STR_P(src) = zend_clone_str(Z_STR_P(src) TSRMLS_CC);
|
2014-03-29 03:34:49 +08:00
|
|
|
break;
|
|
|
|
case IS_ARRAY:
|
|
|
|
if (Z_ARR_P(src) != &EG(symbol_table)) {
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1 && (ptr = accel_xlat_get(Z_ARR_P(src))) != NULL) {
|
|
|
|
Z_ARR_P(src) = ptr;
|
|
|
|
} else {
|
|
|
|
zend_array *old = Z_ARR_P(src);
|
|
|
|
|
|
|
|
Z_ARR_P(src) = emalloc(sizeof(zend_array));
|
|
|
|
Z_ARR_P(src)->gc = old->gc;
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1) {
|
|
|
|
accel_xlat_set(old, Z_ARR_P(src));
|
|
|
|
}
|
|
|
|
zend_hash_clone_zval(Z_ARRVAL_P(src), &old->ht, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IS_REFERENCE:
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1 && (ptr = accel_xlat_get(Z_REF_P(src))) != NULL) {
|
|
|
|
Z_REF_P(src) = ptr;
|
|
|
|
} else {
|
|
|
|
zend_reference *old = Z_REF_P(src);
|
|
|
|
ZVAL_NEW_REF(src, &old->val);
|
|
|
|
Z_REF_P(src)->gc = old->gc;
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1) {
|
|
|
|
accel_xlat_set(old, Z_REF_P(src));
|
|
|
|
}
|
2014-04-24 00:30:24 +08:00
|
|
|
zend_clone_zval(Z_REFVAL_P(src), bind TSRMLS_CC);
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IS_CONSTANT_AST:
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1 && (ptr = accel_xlat_get(Z_AST_P(src))) != NULL) {
|
|
|
|
Z_AST_P(src) = ptr;
|
|
|
|
} else {
|
|
|
|
zend_ast_ref *old = Z_AST_P(src);
|
|
|
|
|
|
|
|
ZVAL_NEW_AST(src, old->ast);
|
|
|
|
Z_AST_P(src)->gc = old->gc;
|
|
|
|
if (bind && Z_REFCOUNT_P(src) > 1) {
|
|
|
|
accel_xlat_set(old, Z_AST_P(src));
|
|
|
|
}
|
|
|
|
Z_ASTVAL_P(src) = zend_ast_clone(Z_ASTVAL_P(src) TSRMLS_CC);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 02:21:07 +08:00
|
|
|
static zend_ast *zend_ast_clone(zend_ast *ast TSRMLS_DC)
|
|
|
|
{
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-08-16 04:02:53 +08:00
|
|
|
|
|
|
|
if (ast->kind == ZEND_AST_ZVAL) {
|
|
|
|
zend_ast_zval *copy = emalloc(sizeof(zend_ast_zval));
|
|
|
|
copy->kind = ZEND_AST_ZVAL;
|
|
|
|
copy->attr = ast->attr;
|
|
|
|
ZVAL_COPY_VALUE(©->val, zend_ast_get_zval(ast));
|
|
|
|
zend_clone_zval(©->val, 0 TSRMLS_CC);
|
|
|
|
return (zend_ast *) copy;
|
|
|
|
} else if (zend_ast_is_list(ast)) {
|
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
|
|
|
zend_ast_list *copy = emalloc(
|
2014-08-26 19:57:19 +08:00
|
|
|
sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
|
2014-08-16 04:02:53 +08:00
|
|
|
copy->kind = list->kind;
|
|
|
|
copy->attr = list->attr;
|
|
|
|
copy->children = list->children;
|
|
|
|
for (i = 0; i < list->children; i++) {
|
|
|
|
if (list->child[i]) {
|
|
|
|
copy->child[i] = zend_ast_clone(list->child[i] TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
copy->child[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (zend_ast *) copy;
|
2013-11-07 02:21:07 +08:00
|
|
|
} else {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t children = zend_ast_get_num_children(ast);
|
2014-08-26 19:57:19 +08:00
|
|
|
zend_ast *copy = emalloc(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
|
2014-08-16 04:02:53 +08:00
|
|
|
copy->kind = ast->kind;
|
|
|
|
copy->attr = ast->attr;
|
|
|
|
for (i = 0; i < children; i++) {
|
|
|
|
if (ast->child[i]) {
|
|
|
|
copy->child[i] = zend_ast_clone(ast->child[i] TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
} else {
|
2014-08-16 04:02:53 +08:00
|
|
|
copy->child[i] = NULL;
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-16 04:02:53 +08:00
|
|
|
return copy;
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:26:47 +08:00
|
|
|
static void zend_hash_clone_zval(HashTable *ht, HashTable *source, int bind)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2014-06-17 01:49:46 +08:00
|
|
|
Bucket *p, *q, *r;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong nIndex;
|
2013-02-13 20:26:47 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
ht->nTableSize = source->nTableSize;
|
|
|
|
ht->nTableMask = source->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = 0;
|
2013-02-13 20:26:47 +08:00
|
|
|
ht->nNumOfElements = source->nNumOfElements;
|
|
|
|
ht->nNextFreeElement = source->nNextFreeElement;
|
|
|
|
ht->pDestructor = ZVAL_PTR_DTOR;
|
2014-04-21 22:25:34 +08:00
|
|
|
ht->u.flags = HASH_FLAG_APPLY_PROTECTION;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arData = NULL;
|
|
|
|
ht->arHash = NULL;
|
|
|
|
ht->nInternalPointer = source->nNumOfElements ? 0 : INVALID_IDX;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
if (!ht->nTableMask) {
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arHash = (uint32_t*)&uninitialized_bucket;
|
2013-02-13 20:26:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:25:34 +08:00
|
|
|
if (source->u.flags & HASH_FLAG_PACKED) {
|
|
|
|
ht->u.flags |= HASH_FLAG_PACKED;
|
2014-04-07 22:22:03 +08:00
|
|
|
ht->arData = (Bucket *) emalloc(ht->nTableSize * sizeof(Bucket));
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arHash = (uint32_t*)&uninitialized_bucket;
|
2014-06-17 01:49:46 +08:00
|
|
|
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
nIndex = p->h & ht->nTableMask;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-06-17 01:49:46 +08:00
|
|
|
r = ht->arData + ht->nNumUsed;
|
2014-02-10 14:04:30 +08:00
|
|
|
q = ht->arData + p->h;
|
2014-04-07 22:22:03 +08:00
|
|
|
while (r != q) {
|
|
|
|
ZVAL_UNDEF(&r->val);
|
|
|
|
r++;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = p->h + 1;
|
2014-06-17 01:49:46 +08:00
|
|
|
|
|
|
|
/* Initialize key */
|
|
|
|
q->h = p->h;
|
|
|
|
q->key = NULL;
|
|
|
|
|
|
|
|
/* Copy data */
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &p->val);
|
|
|
|
zend_clone_zval(&q->val, bind TSRMLS_CC);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arData = (Bucket *) emalloc(ht->nTableSize * (sizeof(Bucket) + sizeof(uint32_t)));
|
|
|
|
ht->arHash = (uint32_t*)(ht->arData + ht->nTableSize);
|
|
|
|
memset(ht->arHash, INVALID_IDX, sizeof(uint32_t) * ht->nTableSize);
|
2014-06-17 01:49:46 +08:00
|
|
|
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
nIndex = p->h & ht->nTableMask;
|
|
|
|
|
|
|
|
/* Insert into hash collision list */
|
2014-02-10 14:04:30 +08:00
|
|
|
q = ht->arData + ht->nNumUsed;
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(q->val) = ht->arHash[nIndex];
|
2014-03-29 03:34:49 +08:00
|
|
|
ht->arHash[nIndex] = ht->nNumUsed++;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2014-06-17 01:49:46 +08:00
|
|
|
/* Initialize key */
|
|
|
|
q->h = p->h;
|
|
|
|
if (!p->key) {
|
|
|
|
q->key = NULL;
|
|
|
|
} else {
|
|
|
|
q->key = zend_clone_str(p->key TSRMLS_CC);
|
|
|
|
}
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-06-17 01:49:46 +08:00
|
|
|
/* Copy data */
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &p->val);
|
|
|
|
zend_clone_zval(&q->val, bind TSRMLS_CC);
|
|
|
|
}
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zend_hash_clone_methods(HashTable *ht, HashTable *source, zend_class_entry *old_ce, zend_class_entry *ce TSRMLS_DC)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p, *q;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong nIndex;
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_class_entry *new_ce;
|
|
|
|
zend_function *new_prototype;
|
2013-02-13 20:26:47 +08:00
|
|
|
zend_op_array *new_entry;
|
|
|
|
|
|
|
|
ht->nTableSize = source->nTableSize;
|
|
|
|
ht->nTableMask = source->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = 0;
|
2013-02-13 20:26:47 +08:00
|
|
|
ht->nNumOfElements = source->nNumOfElements;
|
|
|
|
ht->nNextFreeElement = source->nNextFreeElement;
|
|
|
|
ht->pDestructor = ZEND_FUNCTION_DTOR;
|
2014-06-17 17:53:23 +08:00
|
|
|
ht->u.flags = 0;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nInternalPointer = source->nNumOfElements ? 0 : INVALID_IDX;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
if (!ht->nTableMask) {
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arHash = (uint32_t*)&uninitialized_bucket;
|
2013-02-13 20:26:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-17 01:49:46 +08:00
|
|
|
ZEND_ASSERT(!(source->u.flags & HASH_FLAG_PACKED));
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arData = (Bucket *) emalloc(ht->nTableSize * (sizeof(Bucket) + sizeof(uint32_t)));
|
|
|
|
ht->arHash = (uint32_t *)(ht->arData + ht->nTableSize);
|
|
|
|
memset(ht->arHash, INVALID_IDX, sizeof(uint32_t) * ht->nTableSize);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
2014-03-29 03:34:49 +08:00
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
nIndex = p->h & ht->nTableMask;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
/* Insert into hash collision list */
|
2014-06-17 01:49:46 +08:00
|
|
|
q = ht->arData + ht->nNumUsed;
|
|
|
|
Z_NEXT(q->val) = ht->arHash[nIndex];
|
|
|
|
ht->arHash[nIndex] = ht->nNumUsed++;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
/* Initialize key */
|
|
|
|
q->h = p->h;
|
2014-06-17 01:49:46 +08:00
|
|
|
ZEND_ASSERT(p->key != NULL);
|
|
|
|
q->key = zend_clone_str(p->key TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* Copy data */
|
2014-06-18 06:47:01 +08:00
|
|
|
ZVAL_PTR(&q->val, (void *) zend_arena_alloc(&CG(arena), sizeof(zend_op_array)));
|
2014-03-29 03:34:49 +08:00
|
|
|
new_entry = (zend_op_array*)Z_PTR(q->val);
|
|
|
|
*new_entry = *(zend_op_array*)Z_PTR(p->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* Copy constructor */
|
|
|
|
/* we use refcount to show that op_array is referenced from several places */
|
|
|
|
if (new_entry->refcount != NULL) {
|
2014-03-29 03:34:49 +08:00
|
|
|
accel_xlat_set(Z_PTR(p->val), new_entry);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_prepare_function_for_execution(new_entry);
|
|
|
|
|
2013-02-22 14:56:05 +08:00
|
|
|
if (old_ce == new_entry->scope) {
|
2013-02-13 20:26:47 +08:00
|
|
|
new_entry->scope = ce;
|
|
|
|
} else {
|
2014-03-29 03:34:49 +08:00
|
|
|
if ((new_ce = accel_xlat_get(new_entry->scope)) != NULL) {
|
|
|
|
new_entry->scope = new_ce;
|
2013-02-13 20:26:47 +08:00
|
|
|
} else {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_error(E_ERROR, ACCELERATOR_PRODUCT_NAME " class loading error, class %s, function %s", ce->name->val, new_entry->function_name->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update prototype */
|
2013-02-22 14:56:05 +08:00
|
|
|
if (new_entry->prototype) {
|
2014-03-29 03:34:49 +08:00
|
|
|
if ((new_prototype = accel_xlat_get(new_entry->prototype)) != NULL) {
|
|
|
|
new_entry->prototype = new_prototype;
|
2013-02-13 20:26:47 +08:00
|
|
|
} else {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_error(E_ERROR, ACCELERATOR_PRODUCT_NAME " class loading error, class %s, function %s", ce->name->val, new_entry->function_name->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zend_hash_clone_prop_info(HashTable *ht, HashTable *source, zend_class_entry *old_ce, zend_class_entry *ce TSRMLS_DC)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p, *q;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong nIndex;
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_class_entry *new_ce;
|
2013-02-13 20:26:47 +08:00
|
|
|
zend_property_info *prop_info;
|
|
|
|
|
|
|
|
ht->nTableSize = source->nTableSize;
|
|
|
|
ht->nTableMask = source->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = 0;
|
2013-02-13 20:26:47 +08:00
|
|
|
ht->nNumOfElements = source->nNumOfElements;
|
|
|
|
ht->nNextFreeElement = source->nNextFreeElement;
|
2014-03-31 20:09:45 +08:00
|
|
|
ht->pDestructor = zend_destroy_property_info;
|
2014-06-17 17:53:23 +08:00
|
|
|
ht->u.flags = 0;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nInternalPointer = source->nNumOfElements ? 0 : INVALID_IDX;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
if (!ht->nTableMask) {
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arHash = (uint32_t*)&uninitialized_bucket;
|
2013-02-13 20:26:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-17 01:49:46 +08:00
|
|
|
ZEND_ASSERT(!(source->u.flags & HASH_FLAG_PACKED));
|
2014-08-26 01:28:33 +08:00
|
|
|
ht->arData = (Bucket *) emalloc(ht->nTableSize * (sizeof(Bucket) + sizeof(uint32_t)));
|
|
|
|
ht->arHash = (uint32_t*)(ht->arData + ht->nTableSize);
|
|
|
|
memset(ht->arHash, INVALID_IDX, sizeof(uint32_t) * ht->nTableSize);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
2014-03-29 03:34:49 +08:00
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
nIndex = p->h & ht->nTableMask;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
/* Insert into hash collision list */
|
2014-06-17 01:49:46 +08:00
|
|
|
q = ht->arData + ht->nNumUsed;
|
|
|
|
Z_NEXT(q->val) = ht->arHash[nIndex];
|
|
|
|
ht->arHash[nIndex] = ht->nNumUsed++;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
/* Initialize key */
|
|
|
|
q->h = p->h;
|
2014-06-17 01:49:46 +08:00
|
|
|
ZEND_ASSERT(p->key != NULL);
|
|
|
|
q->key = zend_clone_str(p->key TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* Copy data */
|
2014-06-18 06:47:01 +08:00
|
|
|
ZVAL_PTR(&q->val, (void *) zend_arena_alloc(&CG(arena), sizeof(zend_property_info)));
|
2014-03-29 03:34:49 +08:00
|
|
|
prop_info = Z_PTR(q->val);
|
|
|
|
*prop_info = *(zend_property_info*)Z_PTR(p->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* Copy constructor */
|
2014-04-16 20:28:27 +08:00
|
|
|
prop_info->name = zend_clone_str(prop_info->name TSRMLS_CC);
|
2013-02-22 14:56:05 +08:00
|
|
|
if (prop_info->doc_comment) {
|
2013-02-14 15:59:23 +08:00
|
|
|
if (ZCG(accel_directives).load_comments) {
|
2014-08-26 01:24:55 +08:00
|
|
|
prop_info->doc_comment = zend_string_dup(prop_info->doc_comment, 0);
|
2013-02-14 15:59:23 +08:00
|
|
|
} else {
|
|
|
|
prop_info->doc_comment = NULL;
|
|
|
|
}
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
2013-02-22 14:56:05 +08:00
|
|
|
if (prop_info->ce == old_ce) {
|
2013-02-13 20:26:47 +08:00
|
|
|
prop_info->ce = ce;
|
2014-03-29 03:34:49 +08:00
|
|
|
} else if ((new_ce = accel_xlat_get(prop_info->ce)) != NULL) {
|
|
|
|
prop_info->ce = new_ce;
|
2013-02-13 20:26:47 +08:00
|
|
|
} else {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_error(E_ERROR, ACCELERATOR_PRODUCT_NAME" class loading error, class %s, property %s", ce->name->val, prop_info->name->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects reference count, creates copy of statics */
|
|
|
|
static int zend_prepare_function_for_execution(zend_op_array *op_array)
|
|
|
|
{
|
|
|
|
HashTable *shared_statics = op_array->static_variables;
|
|
|
|
|
|
|
|
/* protect reference count */
|
|
|
|
op_array->refcount = &zend_accel_refcount;
|
|
|
|
(*op_array->refcount) = ZEND_PROTECTED_REFCOUNT;
|
|
|
|
|
|
|
|
/* copy statics */
|
|
|
|
if (shared_statics) {
|
|
|
|
ALLOC_HASHTABLE(op_array->static_variables);
|
|
|
|
zend_hash_clone_zval(op_array->static_variables, shared_statics, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define zend_update_inherited_handler(handler) \
|
|
|
|
{ \
|
2013-02-22 14:56:05 +08:00
|
|
|
if (ce->handler != NULL) { \
|
2014-03-29 03:34:49 +08:00
|
|
|
if ((new_func = accel_xlat_get(ce->handler)) != NULL) { \
|
|
|
|
ce->handler = new_func; \
|
2013-02-13 20:26:47 +08:00
|
|
|
} else { \
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_error(E_ERROR, ACCELERATOR_PRODUCT_NAME " class loading error, class %s", ce->name->val); \
|
2013-02-13 20:26:47 +08:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Protects class' refcount, copies default properties, functions and class name */
|
|
|
|
static void zend_class_copy_ctor(zend_class_entry **pce)
|
|
|
|
{
|
|
|
|
zend_class_entry *ce = *pce;
|
|
|
|
zend_class_entry *old_ce = ce;
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_class_entry *new_ce;
|
|
|
|
zend_function *new_func;
|
2013-02-13 20:26:47 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
2014-06-18 06:47:01 +08:00
|
|
|
*pce = ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
|
2013-02-13 20:26:47 +08:00
|
|
|
*ce = *old_ce;
|
|
|
|
ce->refcount = 1;
|
|
|
|
|
|
|
|
if (old_ce->refcount != 1) {
|
|
|
|
/* this class is not used as a parent for any other classes */
|
|
|
|
accel_xlat_set(old_ce, ce);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_ce->default_properties_table) {
|
|
|
|
int i;
|
|
|
|
|
2014-03-31 17:11:58 +08:00
|
|
|
ce->default_properties_table = emalloc(sizeof(zval) * old_ce->default_properties_count);
|
2013-02-13 20:26:47 +08:00
|
|
|
for (i = 0; i < old_ce->default_properties_count; i++) {
|
2014-03-31 17:11:58 +08:00
|
|
|
ZVAL_COPY_VALUE(&ce->default_properties_table[i], &old_ce->default_properties_table[i]);
|
2014-04-01 20:30:13 +08:00
|
|
|
zend_clone_zval(&ce->default_properties_table[i], 1 TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_hash_clone_methods(&ce->function_table, &old_ce->function_table, old_ce, ce TSRMLS_CC);
|
|
|
|
|
|
|
|
/* static members */
|
|
|
|
if (old_ce->default_static_members_table) {
|
|
|
|
int i;
|
|
|
|
|
2014-03-31 17:11:58 +08:00
|
|
|
ce->default_static_members_table = emalloc(sizeof(zval) * old_ce->default_static_members_count);
|
2013-02-13 20:26:47 +08:00
|
|
|
for (i = 0; i < old_ce->default_static_members_count; i++) {
|
2014-03-31 17:11:58 +08:00
|
|
|
ZVAL_COPY_VALUE(&ce->default_static_members_table[i], &old_ce->default_static_members_table[i]);
|
|
|
|
zend_clone_zval(&ce->default_static_members_table[i], 1 TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ce->static_members_table = ce->default_static_members_table;
|
|
|
|
|
|
|
|
/* properties_info */
|
|
|
|
zend_hash_clone_prop_info(&ce->properties_info, &old_ce->properties_info, old_ce, ce TSRMLS_CC);
|
|
|
|
|
|
|
|
/* constants table */
|
2014-04-14 17:24:43 +08:00
|
|
|
zend_hash_clone_zval(&ce->constants_table, &old_ce->constants_table, 1);
|
2014-06-17 17:53:23 +08:00
|
|
|
ce->constants_table.u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-04-16 20:28:27 +08:00
|
|
|
ce->name = zend_clone_str(ce->name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* interfaces aren't really implemented, so we create a new table */
|
2013-02-22 14:56:05 +08:00
|
|
|
if (ce->num_interfaces) {
|
2013-02-13 20:26:47 +08:00
|
|
|
ce->interfaces = emalloc(sizeof(zend_class_entry *) * ce->num_interfaces);
|
|
|
|
memset(ce->interfaces, 0, sizeof(zend_class_entry *) * ce->num_interfaces);
|
|
|
|
} else {
|
|
|
|
ce->interfaces = NULL;
|
|
|
|
}
|
|
|
|
if (ZEND_CE_DOC_COMMENT(ce)) {
|
2013-02-14 15:59:23 +08:00
|
|
|
if (ZCG(accel_directives).load_comments) {
|
2014-08-26 01:24:55 +08:00
|
|
|
ZEND_CE_DOC_COMMENT(ce) = zend_string_dup(ZEND_CE_DOC_COMMENT(ce), 0);
|
2013-02-14 15:59:23 +08:00
|
|
|
} else {
|
|
|
|
ZEND_CE_DOC_COMMENT(ce) = NULL;
|
|
|
|
}
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 14:56:05 +08:00
|
|
|
if (ce->parent) {
|
2014-03-29 03:34:49 +08:00
|
|
|
if ((new_ce = accel_xlat_get(ce->parent)) != NULL) {
|
|
|
|
ce->parent = new_ce;
|
2013-02-13 20:26:47 +08:00
|
|
|
} else {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_error(E_ERROR, ACCELERATOR_PRODUCT_NAME" class loading error, class %s", ce->name->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_update_inherited_handler(constructor);
|
|
|
|
zend_update_inherited_handler(destructor);
|
|
|
|
zend_update_inherited_handler(clone);
|
|
|
|
zend_update_inherited_handler(__get);
|
|
|
|
zend_update_inherited_handler(__set);
|
|
|
|
zend_update_inherited_handler(__call);
|
|
|
|
/* 5.1 stuff */
|
|
|
|
zend_update_inherited_handler(serialize_func);
|
|
|
|
zend_update_inherited_handler(unserialize_func);
|
|
|
|
zend_update_inherited_handler(__isset);
|
|
|
|
zend_update_inherited_handler(__unset);
|
|
|
|
/* 5.2 stuff */
|
|
|
|
zend_update_inherited_handler(__tostring);
|
|
|
|
|
|
|
|
/* 5.3 stuff */
|
|
|
|
zend_update_inherited_handler(__callstatic);
|
2014-04-26 04:32:51 +08:00
|
|
|
zend_update_inherited_handler(__debugInfo);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
/* 5.4 traits */
|
|
|
|
if (ce->trait_aliases) {
|
|
|
|
zend_trait_alias **trait_aliases;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (ce->trait_aliases[i]) {
|
|
|
|
i++;
|
|
|
|
}
|
2013-02-22 14:56:05 +08:00
|
|
|
trait_aliases = emalloc(sizeof(zend_trait_alias*) * (i + 1));
|
2013-02-13 20:26:47 +08:00
|
|
|
i = 0;
|
|
|
|
while (ce->trait_aliases[i]) {
|
|
|
|
trait_aliases[i] = emalloc(sizeof(zend_trait_alias));
|
|
|
|
memcpy(trait_aliases[i], ce->trait_aliases[i], sizeof(zend_trait_alias));
|
|
|
|
trait_aliases[i]->trait_method = emalloc(sizeof(zend_trait_method_reference));
|
|
|
|
memcpy(trait_aliases[i]->trait_method, ce->trait_aliases[i]->trait_method, sizeof(zend_trait_method_reference));
|
|
|
|
if (trait_aliases[i]->trait_method) {
|
|
|
|
if (trait_aliases[i]->trait_method->method_name) {
|
|
|
|
trait_aliases[i]->trait_method->method_name =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_aliases[i]->trait_method->method_name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
if (trait_aliases[i]->trait_method->class_name) {
|
|
|
|
trait_aliases[i]->trait_method->class_name =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_aliases[i]->trait_method->class_name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trait_aliases[i]->alias) {
|
|
|
|
trait_aliases[i]->alias =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_aliases[i]->alias TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
trait_aliases[i] = NULL;
|
|
|
|
ce->trait_aliases = trait_aliases;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ce->trait_precedences) {
|
|
|
|
zend_trait_precedence **trait_precedences;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (ce->trait_precedences[i]) {
|
|
|
|
i++;
|
|
|
|
}
|
2013-02-22 14:56:05 +08:00
|
|
|
trait_precedences = emalloc(sizeof(zend_trait_precedence*) * (i + 1));
|
2013-02-13 20:26:47 +08:00
|
|
|
i = 0;
|
|
|
|
while (ce->trait_precedences[i]) {
|
|
|
|
trait_precedences[i] = emalloc(sizeof(zend_trait_precedence));
|
|
|
|
memcpy(trait_precedences[i], ce->trait_precedences[i], sizeof(zend_trait_precedence));
|
|
|
|
trait_precedences[i]->trait_method = emalloc(sizeof(zend_trait_method_reference));
|
|
|
|
memcpy(trait_precedences[i]->trait_method, ce->trait_precedences[i]->trait_method, sizeof(zend_trait_method_reference));
|
|
|
|
|
|
|
|
trait_precedences[i]->trait_method->method_name =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_precedences[i]->trait_method->method_name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
trait_precedences[i]->trait_method->class_name =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_precedences[i]->trait_method->class_name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
if (trait_precedences[i]->exclude_from_classes) {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_string **exclude_from_classes;
|
2013-02-13 20:26:47 +08:00
|
|
|
int j = 0;
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
while (trait_precedences[i]->exclude_from_classes[j].class_name) {
|
2013-02-13 20:26:47 +08:00
|
|
|
j++;
|
|
|
|
}
|
2014-03-29 03:34:49 +08:00
|
|
|
exclude_from_classes = emalloc(sizeof(zend_string*) * (j + 1));
|
2013-02-13 20:26:47 +08:00
|
|
|
j = 0;
|
2014-03-29 03:34:49 +08:00
|
|
|
while (trait_precedences[i]->exclude_from_classes[j].class_name) {
|
|
|
|
exclude_from_classes[j] =
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_clone_str(trait_precedences[i]->exclude_from_classes[j].class_name TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
exclude_from_classes[j] = NULL;
|
2014-03-29 03:34:49 +08:00
|
|
|
trait_precedences[i]->exclude_from_classes = (void*)exclude_from_classes;
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
trait_precedences[i] = NULL;
|
|
|
|
ce->trait_precedences = trait_precedences;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 20:50:41 +08:00
|
|
|
static void zend_accel_function_hash_copy(HashTable *target, HashTable *source, unique_copy_ctor_func_t pCopyConstructor TSRMLS_DC)
|
2014-03-29 03:34:49 +08:00
|
|
|
{
|
2014-04-07 20:50:41 +08:00
|
|
|
zend_function *function1, *function2;
|
2014-03-29 03:34:49 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
2014-03-31 20:09:45 +08:00
|
|
|
zval *t;
|
2014-03-29 03:34:49 +08:00
|
|
|
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (p->key) {
|
2014-04-07 20:50:41 +08:00
|
|
|
t = zend_hash_add(target, p->key, &p->val);
|
|
|
|
if (UNEXPECTED(t == NULL)) {
|
2014-03-29 03:34:49 +08:00
|
|
|
if (p->key->len > 0 && p->key->val[0] == 0) {
|
|
|
|
/* Mangled key */
|
2014-07-07 23:21:23 +08:00
|
|
|
t = zend_hash_update(target, p->key, &p->val);
|
2014-04-07 20:50:41 +08:00
|
|
|
} else {
|
|
|
|
t = zend_hash_find(target, p->key);
|
|
|
|
goto failure;
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-07 20:50:41 +08:00
|
|
|
t = zend_hash_index_add(target, p->h, &p->val);
|
|
|
|
if (UNEXPECTED(t == NULL)) {
|
|
|
|
t = zend_hash_index_find(target, p->h);
|
|
|
|
goto failure;
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 20:50:41 +08:00
|
|
|
if (pCopyConstructor) {
|
2014-06-18 06:47:01 +08:00
|
|
|
Z_PTR_P(t) = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
|
|
|
|
memcpy(Z_PTR_P(t), Z_PTR(p->val), sizeof(zend_op_array));
|
2014-04-07 20:50:41 +08:00
|
|
|
pCopyConstructor(Z_PTR_P(t));
|
|
|
|
}
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
target->nInternalPointer = target->nNumOfElements ? 0 : INVALID_IDX;
|
2014-04-07 20:50:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
function1 = Z_PTR(p->val);
|
|
|
|
function2 = Z_PTR_P(t);
|
|
|
|
CG(in_compilation) = 1;
|
|
|
|
zend_set_compiled_filename(function1->op_array.filename TSRMLS_CC);
|
|
|
|
CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
|
|
|
|
if (function2->type == ZEND_USER_FUNCTION
|
|
|
|
&& function2->op_array.last > 0) {
|
|
|
|
zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
|
|
|
|
function1->common.function_name->val,
|
|
|
|
function2->op_array.filename->val,
|
|
|
|
(int)function2->op_array.opcodes[0].lineno);
|
|
|
|
} else {
|
|
|
|
zend_error(E_ERROR, "Cannot redeclare %s()", function1->common.function_name->val);
|
|
|
|
}
|
2014-03-29 03:34:49 +08:00
|
|
|
}
|
|
|
|
|
2014-04-07 20:50:41 +08:00
|
|
|
static void zend_accel_class_hash_copy(HashTable *target, HashTable *source, unique_copy_ctor_func_t pCopyConstructor TSRMLS_DC)
|
2013-02-13 20:26:47 +08:00
|
|
|
{
|
2014-04-07 22:22:03 +08:00
|
|
|
zend_class_entry *ce1;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2013-02-13 20:26:47 +08:00
|
|
|
Bucket *p;
|
2014-03-31 17:11:58 +08:00
|
|
|
zval *t;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
2014-03-29 03:34:49 +08:00
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (p->key) {
|
2014-04-07 20:50:41 +08:00
|
|
|
t = zend_hash_add(target, p->key, &p->val);
|
|
|
|
if (UNEXPECTED(t == NULL)) {
|
2014-03-29 03:34:49 +08:00
|
|
|
if (p->key->len > 0 && p->key->val[0] == 0) {
|
2014-04-07 20:50:41 +08:00
|
|
|
/* Mangled key - ignore and wait for runtime */
|
|
|
|
continue;
|
|
|
|
} else if (!ZCG(accel_directives).ignore_dups) {
|
|
|
|
t = zend_hash_find(target, p->key);
|
|
|
|
goto failure;
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-07 20:50:41 +08:00
|
|
|
t = zend_hash_index_add(target, p->h, &p->val);
|
|
|
|
if (UNEXPECTED(t == NULL)) {
|
|
|
|
if (!ZCG(accel_directives).ignore_dups) {
|
|
|
|
t = zend_hash_index_find(target,p->h);
|
|
|
|
goto failure;
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 20:50:41 +08:00
|
|
|
if (pCopyConstructor) {
|
|
|
|
pCopyConstructor(&Z_PTR_P(t));
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 20:50:41 +08:00
|
|
|
target->nInternalPointer = target->nNumOfElements ? 0 : INVALID_IDX;
|
|
|
|
return;
|
2013-02-13 20:26:47 +08:00
|
|
|
|
2014-04-07 20:50:41 +08:00
|
|
|
failure:
|
|
|
|
ce1 = Z_PTR(p->val);
|
|
|
|
CG(in_compilation) = 1;
|
|
|
|
zend_set_compiled_filename(ce1->info.user.filename TSRMLS_CC);
|
|
|
|
CG(zend_lineno) = ce1->info.user.line_start;
|
|
|
|
zend_error(E_ERROR, "Cannot redeclare class %s", ce1->name->val);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script, int from_shared_memory TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_op_array *op_array;
|
|
|
|
|
|
|
|
op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
|
|
|
|
*op_array = persistent_script->main_op_array;
|
|
|
|
|
|
|
|
if (from_shared_memory) {
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_hash_init(&ZCG(bind_hash), 10, NULL, NULL, 0);
|
|
|
|
|
2013-02-13 20:26:47 +08:00
|
|
|
/* Copy all the necessary stuff from shared memory to regular memory, and protect the shared script */
|
|
|
|
if (zend_hash_num_elements(&persistent_script->class_table) > 0) {
|
|
|
|
zend_accel_class_hash_copy(CG(class_table), &persistent_script->class_table, (unique_copy_ctor_func_t) zend_class_copy_ctor TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* we must first to copy all classes and then prepare functions, since functions may try to bind
|
2013-07-13 20:37:04 +08:00
|
|
|
classes - which depend on pre-bind class entries existent in the class table */
|
2013-02-13 20:26:47 +08:00
|
|
|
if (zend_hash_num_elements(&persistent_script->function_table) > 0) {
|
2014-04-07 20:50:41 +08:00
|
|
|
zend_accel_function_hash_copy(CG(function_table), &persistent_script->function_table, (unique_copy_ctor_func_t)zend_prepare_function_for_execution TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_prepare_function_for_execution(op_array);
|
|
|
|
|
|
|
|
/* Register __COMPILER_HALT_OFFSET__ constant */
|
|
|
|
if (persistent_script->compiler_halt_offset != 0 &&
|
|
|
|
persistent_script->full_path) {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_string *name;
|
2013-02-13 20:26:47 +08:00
|
|
|
char haltoff[] = "__COMPILER_HALT_OFFSET__";
|
|
|
|
|
2014-03-29 03:34:49 +08:00
|
|
|
name = zend_mangle_property_name(haltoff, sizeof(haltoff) - 1, persistent_script->full_path->val, persistent_script->full_path->len, 0);
|
|
|
|
if (!zend_hash_exists(EG(zend_constants), name)) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_register_long_constant(name->val, name->len, persistent_script->compiler_halt_offset, CONST_CS, 0 TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_release(name);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
|
2014-04-16 20:28:27 +08:00
|
|
|
zend_hash_destroy(&ZCG(bind_hash));
|
2013-02-13 20:26:47 +08:00
|
|
|
} else /* if (!from_shared_memory) */ {
|
|
|
|
if (zend_hash_num_elements(&persistent_script->function_table) > 0) {
|
2014-04-07 20:50:41 +08:00
|
|
|
zend_accel_function_hash_copy(CG(function_table), &persistent_script->function_table, NULL TSRMLS_CC);
|
2013-02-13 20:26:47 +08:00
|
|
|
}
|
|
|
|
if (zend_hash_num_elements(&persistent_script->class_table) > 0) {
|
|
|
|
zend_accel_class_hash_copy(CG(class_table), &persistent_script->class_table, NULL TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
if (op_array->early_binding != (uint32_t)-1) {
|
2014-03-29 03:34:49 +08:00
|
|
|
zend_string *orig_compiled_filename = CG(compiled_filename);
|
2013-02-13 20:26:47 +08:00
|
|
|
CG(compiled_filename) = persistent_script->full_path;
|
|
|
|
zend_do_delayed_early_binding(op_array TSRMLS_CC);
|
|
|
|
CG(compiled_filename) = orig_compiled_filename;
|
|
|
|
}
|
|
|
|
|
2014-07-30 14:39:25 +08:00
|
|
|
if (!from_shared_memory) {
|
|
|
|
free_persistent_script(persistent_script, 0); /* free only hashes */
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:26:47 +08:00
|
|
|
return op_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* zend_adler32() is based on zlib implementation
|
|
|
|
* Computes the Adler-32 checksum of a data stream
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995-2005 Mark Adler
|
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
|
|
|
|
#define ADLER32_NMAX 5552
|
|
|
|
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
|
|
|
|
|
2013-02-22 14:56:05 +08:00
|
|
|
#define ADLER32_DO1(buf) {s1 += *(buf); s2 += s1;}
|
|
|
|
#define ADLER32_DO2(buf, i) ADLER32_DO1(buf + i); ADLER32_DO1(buf + i + 1);
|
|
|
|
#define ADLER32_DO4(buf, i) ADLER32_DO2(buf, i); ADLER32_DO2(buf, i + 2);
|
|
|
|
#define ADLER32_DO8(buf, i) ADLER32_DO4(buf, i); ADLER32_DO4(buf, i + 4);
|
|
|
|
#define ADLER32_DO16(buf) ADLER32_DO8(buf, 0); ADLER32_DO8(buf, 8);
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
unsigned int zend_adler32(unsigned int checksum, signed char *buf, uint len)
|
|
|
|
{
|
|
|
|
unsigned int s1 = checksum & 0xffff;
|
|
|
|
unsigned int s2 = (checksum >> 16) & 0xffff;
|
|
|
|
signed char *end;
|
|
|
|
|
|
|
|
while (len >= ADLER32_NMAX) {
|
|
|
|
len -= ADLER32_NMAX;
|
|
|
|
end = buf + ADLER32_NMAX;
|
|
|
|
do {
|
|
|
|
ADLER32_DO16(buf);
|
|
|
|
buf += 16;
|
|
|
|
} while (buf != end);
|
|
|
|
s1 %= ADLER32_BASE;
|
|
|
|
s2 %= ADLER32_BASE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
if (len >= 16) {
|
|
|
|
end = buf + (len & 0xfff0);
|
|
|
|
len &= 0xf;
|
|
|
|
do {
|
|
|
|
ADLER32_DO16(buf);
|
|
|
|
buf += 16;
|
|
|
|
} while (buf != end);
|
|
|
|
}
|
|
|
|
if (len) {
|
|
|
|
end = buf + len;
|
|
|
|
do {
|
|
|
|
ADLER32_DO1(buf);
|
|
|
|
buf++;
|
|
|
|
} while (buf != end);
|
|
|
|
}
|
|
|
|
s1 %= ADLER32_BASE;
|
|
|
|
s2 %= ADLER32_BASE;
|
2013-02-22 14:56:05 +08:00
|
|
|
}
|
2013-02-13 20:26:47 +08:00
|
|
|
|
|
|
|
return (s2 << 16) | s1;
|
|
|
|
}
|