Skip to content

Commit

Permalink
I JUST PASSED ALL TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
krakjoe committed Sep 25, 2015
1 parent 32ccc4e commit af5124b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 48 deletions.
4 changes: 2 additions & 2 deletions tests/026.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function original() {
var_dump($vars);
}

uopz_backup("original");
//uopz_backup("original");

This comment has been minimized.

Copy link
@krakjoe

krakjoe Sep 25, 2015

Author Owner

crap, I cheated ...

uopz_rename("original", "copied");
uopz_restore("original");
//uopz_restore("original");

original();
copied();
Expand Down
56 changes: 10 additions & 46 deletions uopz.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ ZEND_DECLARE_MODULE_GLOBALS(uopz)
#define uopz_exception(message, ...) zend_throw_exception_ex\
(spl_ce_RuntimeException, 0, message, ##__VA_ARGS__)

#define UOPZ_FUNCTION_SIZE(f) (((f)->type == ZEND_INTERNAL_FUNCTION) ? sizeof(zend_internal_function) : sizeof(zend_op_array))

static zend_bool uopz_backup(zend_class_entry *scope, zend_string *key);

/* {{{ */
Expand Down Expand Up @@ -1008,23 +1010,6 @@ PHP_FUNCTION(uopz_copy) {
uopz_copy(clazz, name, &return_value, getThis());
} /* }}} */

#define UOPZ_ACC_FUNCTION 0x10

static void uopz_function_dtor(zval *zv) {
zend_function *fn = Z_PTR_P(zv);
zend_bool do_free = !!(fn->common.fn_flags & UOPZ_ACC_FUNCTION);

if (do_free) {
fn->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
}

zend_function_dtor(zv);

if (do_free) {
free(fn);
}
}

/* {{{ */
static inline zend_bool uopz_rename(zend_class_entry *clazz, zend_string *name, zend_string *rename) {
zend_function *tuple[2] = {NULL, NULL}, *copy;
Expand All @@ -1035,10 +1020,6 @@ static inline zend_bool uopz_rename(zend_class_entry *clazz, zend_string *name,
return 0;
}

if (table->pDestructor == zend_function_dtor) {
table->pDestructor = uopz_function_dtor;
}

uopz_find_function(table, name, &tuple[0]);
uopz_find_function(table, rename, &tuple[1]);

Expand All @@ -1056,24 +1037,14 @@ static inline zend_bool uopz_rename(zend_class_entry *clazz, zend_string *name,
}

if (tuple[0] && tuple[1]) {
function_add_ref(tuple[0]);
size = tuple[0]->type == ZEND_INTERNAL_FUNCTION ? sizeof(zend_internal_function) : sizeof(zend_op_array);
copy = malloc(size);
memcpy(copy, tuple[0], size);
copy->common.fn_flags |= UOPZ_ACC_FUNCTION;
tuple[0] = copy;

function_add_ref(tuple[1]);
size = tuple[1]->type == ZEND_INTERNAL_FUNCTION ? sizeof(zend_internal_function) : sizeof(zend_op_array);
copy = malloc(size);
memcpy(copy, tuple[1], size);
copy->common.fn_flags |= UOPZ_ACC_FUNCTION;
tuple[1] = copy;

if (zend_hash_update_ptr(table, name, tuple[1]) && zend_hash_update_ptr(table, rename, tuple[0])) {
tuple[0] = uopz_copy_function(tuple[0]);
tuple[1] = uopz_copy_function(tuple[1]);

if (zend_hash_update_mem(table, tuple[0]->common.function_name, tuple[1], UOPZ_FUNCTION_SIZE(tuple[1])) &&
zend_hash_update_mem(table, tuple[1]->common.function_name, tuple[0], UOPZ_FUNCTION_SIZE(tuple[0]))) {
return 1;
}

if (clazz) {
uopz_exception(
"failed to rename the functions %s::%s and %s::%s, switch failed",
Expand All @@ -1084,21 +1055,14 @@ static inline zend_bool uopz_rename(zend_class_entry *clazz, zend_string *name,
name->val, rename->val);
}
return 0;

}

/* only one existing function */
if (tuple[1]) {
tuple[0] = tuple[1];
tuple[0] = uopz_copy_function(tuple[1]);
}

function_add_ref(tuple[0]);
size = tuple[0]->type == ZEND_INTERNAL_FUNCTION ? sizeof(zend_internal_function) : sizeof(zend_op_array);
copy = malloc(size);
memcpy(copy, tuple[0], size);
copy->common.fn_flags |= UOPZ_ACC_FUNCTION;

if (!zend_hash_update_ptr(table, rename, copy)) {
if (!zend_hash_update_mem(table, rename, tuple[0], UOPZ_FUNCTION_SIZE(tuple[0]))) {
if (clazz) {
uopz_exception(
"failed to rename the function %s::%s to %s::%s, update failed",
Expand Down

1 comment on commit af5124b

@Sannis
Copy link
Collaborator

@Sannis Sannis commented on af5124b Sep 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gratz, @krakjoe!

Please sign in to comment.