1766 lines
61 KiB
C
1766 lines
61 KiB
C
/*
|
|
* Cryo\Engine — hot code redefinition in the running process (milestone 6).
|
|
*
|
|
* Every change is journaled and undone in reverse order by revert() and,
|
|
* at the latest, in RSHUTDOWN (before the executor destroys the function
|
|
* and class tables).
|
|
*
|
|
* User functions and methods are redefined IN PLACE: call sites keep the
|
|
* zend_function* in their run-time cache (INIT_FCALL, method caches), so the
|
|
* op_array keeps its address and identity (name, scope, prototype, access
|
|
* flags, attributes) and receives the body of the closure (opcodes, CVs,
|
|
* literals, argument info). The closure object is kept alive while its body
|
|
* is installed. Two consequences are handled here:
|
|
* - INIT_FCALL sizes the call frame from a value computed when the CALLER
|
|
* was compiled; a ZEND_INIT_FCALL user opcode handler recomputes the size
|
|
* for user functions once anything has been redefined in this process.
|
|
* - the redefined body gets its own run-time cache and static variables.
|
|
*
|
|
* Internal functions (cryo.engine_internal_override=1) get a trampoline
|
|
* handler that calls the closure; inside the closure, calls to the same
|
|
* function reach the original implementation.
|
|
*
|
|
* NTS only, like the rest of v0.1 (state in statics).
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
|
|
#include "php.h"
|
|
#include "php_ini.h"
|
|
#include "zend_exceptions.h"
|
|
#include "zend_closures.h"
|
|
#include "zend_generators.h"
|
|
#include "zend_fibers.h"
|
|
#include "zend_constants.h"
|
|
#include "zend_interfaces.h"
|
|
#include "zend_smart_str.h"
|
|
#include "php_cryo.h"
|
|
|
|
zend_class_entry *cryo_ce_engine;
|
|
static zend_object_handlers cryo_engine_handlers;
|
|
|
|
typedef enum {
|
|
CRYO_CH_FUNCTION = 1, /* user function body replaced in place */
|
|
CRYO_CH_METHOD, /* user method body replaced in place */
|
|
CRYO_CH_INTERNAL, /* internal function handler -> trampoline */
|
|
CRYO_CH_CONSTANT, /* global user constant value */
|
|
CRYO_CH_CLASS_CONSTANT, /* user class constant value */
|
|
CRYO_CH_ADD_FUNCTION,
|
|
CRYO_CH_REMOVE_FUNCTION,
|
|
CRYO_CH_RENAME_FUNCTION,
|
|
CRYO_CH_ADD_METHOD,
|
|
CRYO_CH_REMOVE_METHOD,
|
|
CRYO_CH_SUPERGLOBAL
|
|
} cryo_change_kind;
|
|
|
|
static const char *cryo_kind_names[] = {
|
|
"", "function", "method", "internal", "constant", "class_constant",
|
|
"add_function", "remove_function", "rename_function", "add_method",
|
|
"remove_method", "superglobal"
|
|
};
|
|
|
|
typedef struct _cryo_change {
|
|
zend_long id;
|
|
cryo_change_kind kind;
|
|
zend_string *target; /* "name", "Class::name" */
|
|
zend_string *extra; /* rename: new name */
|
|
double time;
|
|
zend_string *file; /* caller */
|
|
uint32_t line;
|
|
zval closure; /* body (UNDEF if none) */
|
|
zval value; /* constants: new value (for export) */
|
|
zend_long flags; /* add_method */
|
|
|
|
zend_function *func; /* target function */
|
|
zend_op_array saved; /* user: previous content */
|
|
HashTable *templ; /* user: static variables template (owned) */
|
|
bool in_place; /* user body installed in place */
|
|
|
|
zif_handler orig_handler; /* internal: original handler */
|
|
#if PHP_VERSION_ID >= 80400
|
|
const zend_frameless_function_info *orig_flf;
|
|
#endif
|
|
|
|
HashTable *table; /* function table the entry lives in */
|
|
zend_string *key; /* lowercase key in table */
|
|
zend_string *saved_name; /* rename: previous function_name */
|
|
|
|
zval *slot; /* constants: value slot */
|
|
zval saved_value; /* constants: previous value */
|
|
zend_string *sg_name; /* superglobal (persistent) */
|
|
} cryo_change;
|
|
|
|
static struct {
|
|
zval object;
|
|
cryo_change **list;
|
|
uint32_t count, cap;
|
|
zend_long next_id;
|
|
bool dirty; /* frame sizes must be recomputed */
|
|
bool installed; /* INIT_FCALL handler installed */
|
|
bool compiler_opts_saved;
|
|
uint32_t compiler_opts;
|
|
} cryo_engine;
|
|
|
|
static user_opcode_handler_t cryo_prev_init_fcall = NULL;
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Helpers */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static bool cryo_engine_ini(const char *name)
|
|
{
|
|
const char *v = zend_ini_string_ex((char *) name, strlen(name), 0, NULL);
|
|
return v && v[0] && strcmp(v, "0") != 0 && strcasecmp(v, "off") != 0
|
|
&& strcasecmp(v, "false") != 0 && strcasecmp(v, "no") != 0;
|
|
}
|
|
|
|
bool cryo_engine_enabled(void)
|
|
{
|
|
return cryo_engine_ini("cryo.engine");
|
|
}
|
|
|
|
static void *cryo_new_rtc(int size)
|
|
{
|
|
void *p = zend_arena_alloc(&CG(arena), size > 0 ? (size_t) size : sizeof(void *));
|
|
memset(p, 0, size > 0 ? (size_t) size : sizeof(void *));
|
|
return p;
|
|
}
|
|
|
|
static void cryo_ensure_rtc(zend_op_array *op)
|
|
{
|
|
if (!RUN_TIME_CACHE(op)) {
|
|
ZEND_MAP_PTR_SET(op->run_time_cache, cryo_new_rtc(op->cache_size));
|
|
}
|
|
}
|
|
|
|
static double cryo_now(void)
|
|
{
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
return (double) tv.tv_sec + (double) tv.tv_usec / 1e6;
|
|
}
|
|
|
|
static void cryo_compile_no_builtins(uint32_t extra)
|
|
{
|
|
if (!cryo_engine.compiler_opts_saved) {
|
|
cryo_engine.compiler_opts = CG(compiler_options);
|
|
cryo_engine.compiler_opts_saved = true;
|
|
}
|
|
CG(compiler_options) |= extra;
|
|
}
|
|
|
|
/* ZEND_INIT_FCALL pushes a frame sized when the caller was compiled. Once a
|
|
* body has been swapped, recompute it from the callee for user functions. */
|
|
static int cryo_init_fcall_handler(zend_execute_data *execute_data)
|
|
{
|
|
if (EXPECTED(!cryo_engine.dirty)) {
|
|
return cryo_prev_init_fcall ? cryo_prev_init_fcall(execute_data) : ZEND_USER_OPCODE_DISPATCH;
|
|
}
|
|
const zend_op *opline = EX(opline);
|
|
zend_function *fbc = CACHED_PTR(opline->result.num);
|
|
if (!fbc) {
|
|
zval *zf = zend_hash_find(EG(function_table), Z_STR_P(RT_CONSTANT(opline, opline->op2)));
|
|
if (!zf) {
|
|
return cryo_prev_init_fcall ? cryo_prev_init_fcall(execute_data) : ZEND_USER_OPCODE_DISPATCH;
|
|
}
|
|
fbc = Z_FUNC_P(zf);
|
|
if (fbc->type == ZEND_USER_FUNCTION) {
|
|
cryo_ensure_rtc(&fbc->op_array);
|
|
}
|
|
CACHE_PTR(opline->result.num, fbc);
|
|
}
|
|
if (fbc->type != ZEND_USER_FUNCTION) {
|
|
return cryo_prev_init_fcall ? cryo_prev_init_fcall(execute_data) : ZEND_USER_OPCODE_DISPATCH;
|
|
}
|
|
zend_execute_data *call = zend_vm_stack_push_call_frame_ex(
|
|
zend_vm_calc_used_stack(opline->extended_value, fbc),
|
|
ZEND_CALL_NESTED_FUNCTION, fbc, opline->extended_value, NULL);
|
|
call->prev_execute_data = EX(call);
|
|
EX(call) = call;
|
|
EX(opline) = opline + 1;
|
|
return ZEND_USER_OPCODE_CONTINUE;
|
|
}
|
|
|
|
|
|
/* INIT_FCALL reserves the frame size computed when its caller was compiled
|
|
* (opline->op1.num). A body swapped in place may need more: grow that size in
|
|
* every compiled call site of `f` (functions, methods, their closures, and the
|
|
* code frames still running). No hook on INIT_FCALL: no cost on other calls. */
|
|
static void cryo_patch_op_array(zend_op_array *op, zend_string *lcname, zend_function *f, uint32_t depth)
|
|
{
|
|
if (!op || op->type != ZEND_USER_FUNCTION || depth > 64 || (op->fn_flags & ZEND_ACC_IMMUTABLE)) return;
|
|
for (uint32_t i = 0; i < op->last; i++) {
|
|
zend_op *opline = &op->opcodes[i];
|
|
if (opline->opcode != ZEND_INIT_FCALL || opline->op2_type != IS_CONST) continue;
|
|
zval *name = RT_CONSTANT(opline, opline->op2);
|
|
if (Z_TYPE_P(name) != IS_STRING || !zend_string_equals(Z_STR_P(name), lcname)) continue;
|
|
uint32_t need = zend_vm_calc_used_stack(opline->extended_value, f);
|
|
if (opline->op1.num < need) opline->op1.num = need;
|
|
}
|
|
for (uint32_t i = 0; i < op->num_dynamic_func_defs; i++) {
|
|
cryo_patch_op_array(op->dynamic_func_defs[i], lcname, f, depth + 1);
|
|
}
|
|
}
|
|
|
|
static void cryo_patch_call_sites(zend_function *f)
|
|
{
|
|
if (!f->common.function_name || f->common.scope) return; /* INIT_FCALL calls plain functions only */
|
|
zend_string *lc = zend_string_tolower(f->common.function_name);
|
|
zend_function *fn;
|
|
ZEND_HASH_MAP_FOREACH_PTR(EG(function_table), fn) {
|
|
if (fn->type == ZEND_USER_FUNCTION) cryo_patch_op_array(&fn->op_array, lc, f, 0);
|
|
} ZEND_HASH_FOREACH_END();
|
|
zend_class_entry *ce;
|
|
ZEND_HASH_MAP_FOREACH_PTR(EG(class_table), ce) {
|
|
if (ce->type != ZEND_USER_CLASS) continue;
|
|
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) {
|
|
if (fn->type == ZEND_USER_FUNCTION && fn->common.scope == ce) cryo_patch_op_array(&fn->op_array, lc, f, 0);
|
|
} ZEND_HASH_FOREACH_END();
|
|
} ZEND_HASH_FOREACH_END();
|
|
for (zend_execute_data *ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
|
|
if (ex->func && ZEND_USER_CODE(ex->func->type)) cryo_patch_op_array(&ex->func->op_array, lc, f, 0);
|
|
}
|
|
zend_string_release(lc);
|
|
}
|
|
|
|
/* Is f running or about to be called (pending frame) somewhere? */
|
|
static bool cryo_frames_use(zend_execute_data *ex, const zend_function *f, const zend_execute_data *stop)
|
|
{
|
|
for (; ex; ex = ex->prev_execute_data) {
|
|
if (ex->func == f) {
|
|
return true;
|
|
}
|
|
if (ex->func && ZEND_USER_CODE(ex->func->type)) {
|
|
for (zend_execute_data *call = ex->call; call; call = call->prev_execute_data) {
|
|
if (call->func == f) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
if (ex == stop) {
|
|
break;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static const char *cryo_func_busy(const zend_function *f)
|
|
{
|
|
if (cryo_frames_use(EG(current_execute_data), f, NULL)) {
|
|
return "it is running or being called";
|
|
}
|
|
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
|
|
zend_object *o = EG(objects_store).object_buckets[h];
|
|
if (!IS_OBJ_VALID(o)) {
|
|
continue;
|
|
}
|
|
if (o->ce == zend_ce_generator) {
|
|
zend_generator *g = (zend_generator *) o;
|
|
if (g->execute_data && cryo_frames_use(g->execute_data, f, g->execute_data)) {
|
|
return "a suspended generator runs it";
|
|
}
|
|
} else if (o->ce == zend_ce_fiber) {
|
|
zend_fiber *fb = (zend_fiber *) o;
|
|
if (fb->context.status == ZEND_FIBER_STATUS_SUSPENDED && fb->execute_data
|
|
&& cryo_frames_use(fb->execute_data, f, fb->stack_bottom)) {
|
|
return "a suspended fiber runs it";
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static cryo_change *cryo_change_new(cryo_change_kind kind, zend_string *target)
|
|
{
|
|
cryo_change *ch = ecalloc(1, sizeof(cryo_change));
|
|
ch->id = cryo_engine.next_id++;
|
|
ch->kind = kind;
|
|
ch->target = zend_string_copy(target);
|
|
ch->time = cryo_now();
|
|
ZVAL_UNDEF(&ch->closure);
|
|
ZVAL_UNDEF(&ch->value);
|
|
ZVAL_UNDEF(&ch->saved_value);
|
|
zend_string *file = zend_get_executed_filename_ex();
|
|
ch->file = file ? zend_string_copy(file) : NULL;
|
|
ch->line = file ? zend_get_executed_lineno() : 0;
|
|
return ch;
|
|
}
|
|
|
|
static void cryo_change_push(cryo_change *ch)
|
|
{
|
|
if (cryo_engine.count == cryo_engine.cap) {
|
|
cryo_engine.cap = cryo_engine.cap ? cryo_engine.cap * 2 : 8;
|
|
cryo_engine.list = erealloc(cryo_engine.list, cryo_engine.cap * sizeof(cryo_change *));
|
|
}
|
|
cryo_engine.list[cryo_engine.count++] = ch;
|
|
}
|
|
|
|
static void cryo_change_free(cryo_change *ch)
|
|
{
|
|
zend_string_release(ch->target);
|
|
if (ch->extra) zend_string_release(ch->extra);
|
|
if (ch->file) zend_string_release(ch->file);
|
|
if (ch->key) zend_string_release(ch->key);
|
|
if (ch->saved_name) zend_string_release(ch->saved_name);
|
|
zval_ptr_dtor(&ch->value);
|
|
zval_ptr_dtor(&ch->closure);
|
|
efree(ch);
|
|
}
|
|
|
|
static bool cryo_check_engine_state(void)
|
|
{
|
|
if (EG(active_fiber)) {
|
|
zend_throw_exception(cryo_ce_exception, "Cryo\\Engine cannot change code from inside a Fiber", 0);
|
|
return false;
|
|
}
|
|
if (cryo_jit_active()) {
|
|
zend_throw_exception(cryo_ce_exception, "Cryo\\Engine cannot change code while the JIT is enabled", 0);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* In-place body swap (user functions and methods) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
#define CRYO_TAKE_FLAGS (ZEND_ACC_VARIADIC | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_HAS_RETURN_TYPE \
|
|
| ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_GENERATOR | ZEND_ACC_HAS_FINALLY_BLOCK \
|
|
| ZEND_ACC_USES_THIS | ZEND_ACC_STRICT_TYPES | ZEND_ACC_DONE_PASS_TWO)
|
|
|
|
static HashTable *cryo_closure_statics(zend_function *cf)
|
|
{
|
|
HashTable *ht = ZEND_MAP_PTR(cf->op_array.static_variables_ptr)
|
|
? ZEND_MAP_PTR_GET(cf->op_array.static_variables_ptr) : NULL;
|
|
return ht ? ht : cf->op_array.static_variables;
|
|
}
|
|
|
|
static void cryo_swap_body(cryo_change *ch, zend_function *target, zend_function *cf)
|
|
{
|
|
zend_op_array repl = cf->op_array;
|
|
zend_op_array *old = &target->op_array;
|
|
|
|
ch->func = target;
|
|
ch->saved = *old;
|
|
ch->in_place = true;
|
|
|
|
repl.type = ZEND_USER_FUNCTION;
|
|
repl.function_name = old->function_name;
|
|
repl.scope = old->scope;
|
|
repl.prototype = old->prototype;
|
|
repl.attributes = old->attributes;
|
|
repl.doc_comment = old->doc_comment;
|
|
#if PHP_VERSION_ID >= 80400
|
|
repl.prop_info = old->prop_info;
|
|
#endif
|
|
memcpy(repl.reserved, old->reserved, sizeof(repl.reserved));
|
|
repl.fn_flags = (old->fn_flags & ~CRYO_TAKE_FLAGS) | (cf->op_array.fn_flags & CRYO_TAKE_FLAGS);
|
|
|
|
ZEND_MAP_PTR_INIT(repl.run_time_cache, cryo_new_rtc(repl.cache_size));
|
|
HashTable *src = cryo_closure_statics(cf);
|
|
ch->templ = src ? zend_array_dup(src) : NULL;
|
|
repl.static_variables = ch->templ;
|
|
ZEND_MAP_PTR_INIT(repl.static_variables_ptr, NULL);
|
|
|
|
*old = repl;
|
|
cryo_engine.dirty = true;
|
|
cryo_patch_call_sites(target);
|
|
}
|
|
|
|
/* Closures created from the redefined function (Closure::fromCallable, f(...))
|
|
* share its static variables: give them their own copy before we free ours. */
|
|
static void cryo_detach_fake_closures(cryo_change *ch, zend_op_array *cur, HashTable *rt)
|
|
{
|
|
if (!ch->templ) {
|
|
return;
|
|
}
|
|
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
|
|
zend_object *o = EG(objects_store).object_buckets[h];
|
|
if (!IS_OBJ_VALID(o) || o->ce != zend_ce_closure) {
|
|
continue;
|
|
}
|
|
zend_function *f = (zend_function *) zend_get_closure_method_def(o);
|
|
if (f->type != ZEND_USER_FUNCTION || !(f->common.fn_flags & ZEND_ACC_FAKE_CLOSURE)
|
|
|| f->op_array.opcodes != cur->opcodes || f->op_array.static_variables != ch->templ) {
|
|
continue;
|
|
}
|
|
HashTable *own = zend_array_dup(rt ? rt : ch->templ);
|
|
f->op_array.static_variables = own;
|
|
ZEND_MAP_PTR_INIT(f->op_array.static_variables_ptr, own);
|
|
f->common.fn_flags &= ~ZEND_ACC_FAKE_CLOSURE;
|
|
}
|
|
}
|
|
|
|
static void cryo_unswap_body(cryo_change *ch)
|
|
{
|
|
zend_op_array *cur = &ch->func->op_array;
|
|
HashTable *rt = ZEND_MAP_PTR(cur->static_variables_ptr) ? ZEND_MAP_PTR_GET(cur->static_variables_ptr) : NULL;
|
|
|
|
cryo_detach_fake_closures(ch, cur, rt);
|
|
*cur = ch->saved;
|
|
cryo_patch_call_sites(ch->func);
|
|
/* call sites cached the function while the new body was installed */
|
|
cryo_ensure_rtc(cur);
|
|
if (rt) {
|
|
zend_array_destroy(rt);
|
|
}
|
|
if (ch->templ) {
|
|
zend_array_destroy(ch->templ);
|
|
ch->templ = NULL;
|
|
}
|
|
ch->in_place = false;
|
|
}
|
|
|
|
static zend_function *cryo_closure_func(zval *closure)
|
|
{
|
|
zend_function *cf = (zend_function *) zend_get_closure_method_def(Z_OBJ_P(closure));
|
|
if (cf->type != ZEND_USER_FUNCTION) {
|
|
zend_throw_exception(cryo_ce_exception, "The body must be a closure written in PHP", 0);
|
|
return NULL;
|
|
}
|
|
return cf;
|
|
}
|
|
|
|
static bool cryo_check_user_target(zend_function *f, const char *label)
|
|
{
|
|
if (f->common.fn_flags & ZEND_ACC_IMMUTABLE) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"%s is immutable (cached by OPcache); disable OPcache for this script to redefine it", label);
|
|
return false;
|
|
}
|
|
if (f->common.fn_flags & ZEND_ACC_ABSTRACT) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "%s is abstract", label);
|
|
return false;
|
|
}
|
|
const char *busy = cryo_func_busy(f);
|
|
if (busy) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot change %s: %s", label, busy);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* New function struct for add_function / add_method (arena, like the compiler) */
|
|
static zend_function *cryo_new_user_function(cryo_change *ch, zend_function *cf, zend_string *name,
|
|
zend_class_entry *scope, uint32_t acc)
|
|
{
|
|
zend_op_array *op = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
|
|
*op = cf->op_array;
|
|
op->function_name = zend_string_copy(name);
|
|
op->scope = scope;
|
|
op->prototype = NULL;
|
|
op->attributes = NULL;
|
|
op->doc_comment = NULL;
|
|
#if PHP_VERSION_ID >= 80400
|
|
op->prop_info = NULL;
|
|
#endif
|
|
memset(op->reserved, 0, sizeof(op->reserved));
|
|
op->fn_flags = (cf->op_array.fn_flags & CRYO_TAKE_FLAGS) | acc;
|
|
if (op->refcount) {
|
|
(*op->refcount)++;
|
|
}
|
|
ZEND_MAP_PTR_INIT(op->run_time_cache, cryo_new_rtc(op->cache_size));
|
|
HashTable *src = cryo_closure_statics(cf);
|
|
ch->templ = src ? zend_array_dup(src) : NULL;
|
|
op->static_variables = ch->templ;
|
|
ZEND_MAP_PTR_INIT(op->static_variables_ptr, NULL);
|
|
cryo_engine.dirty = true;
|
|
return (zend_function *) op;
|
|
}
|
|
|
|
static void cryo_free_user_function(cryo_change *ch, zend_function *f)
|
|
{
|
|
zend_op_array *op = &f->op_array;
|
|
HashTable *rt = ZEND_MAP_PTR(op->static_variables_ptr) ? ZEND_MAP_PTR_GET(op->static_variables_ptr) : NULL;
|
|
cryo_detach_fake_closures(ch, op, rt);
|
|
if (rt) {
|
|
zend_array_destroy(rt);
|
|
}
|
|
ZEND_MAP_PTR_INIT(op->static_variables_ptr, NULL);
|
|
if (ch->templ) {
|
|
zend_array_destroy(ch->templ);
|
|
ch->templ = NULL;
|
|
}
|
|
op->static_variables = NULL;
|
|
/* releases the name and our reference on the shared opcodes; the struct is
|
|
* arena memory and stays valid for call sites that cached it */
|
|
destroy_op_array(op);
|
|
}
|
|
|
|
/* Delete a key without running the table destructor (cached pointers stay valid) */
|
|
static void cryo_hash_del_keep(HashTable *ht, zend_string *key)
|
|
{
|
|
dtor_func_t d = ht->pDestructor;
|
|
ht->pDestructor = NULL;
|
|
zend_hash_del(ht, key);
|
|
ht->pDestructor = d;
|
|
}
|
|
|
|
/* A closure that throws "Call to undefined ..." to install in removed functions */
|
|
static bool cryo_make_thrower(zval *out, const char *what, zend_string *name)
|
|
{
|
|
smart_str code = {0};
|
|
smart_str_appends(&code, "static function () { throw new \\Error('Call to undefined ");
|
|
smart_str_appends(&code, what);
|
|
smart_str_appendc(&code, ' ');
|
|
for (size_t i = 0; i < ZSTR_LEN(name); i++) {
|
|
char c = ZSTR_VAL(name)[i];
|
|
if (c == '\'' || c == '\\') smart_str_appendc(&code, '\\');
|
|
smart_str_appendc(&code, c);
|
|
}
|
|
smart_str_appends(&code, "()'); }");
|
|
smart_str_0(&code);
|
|
zend_result r = zend_eval_stringl(ZSTR_VAL(code.s), ZSTR_LEN(code.s), out, "Cryo\\Engine");
|
|
smart_str_free(&code);
|
|
if (r != SUCCESS || Z_TYPE_P(out) != IS_OBJECT) {
|
|
zval_ptr_dtor(out);
|
|
ZVAL_UNDEF(out);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Internal functions */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static cryo_change *cryo_find_internal(const zend_function *f)
|
|
{
|
|
for (uint32_t i = cryo_engine.count; i > 0; i--) {
|
|
cryo_change *ch = cryo_engine.list[i - 1];
|
|
if (ch->kind == CRYO_CH_INTERNAL && ch->func == f) {
|
|
return ch;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static ZEND_NAMED_FUNCTION(cryo_engine_trampoline)
|
|
{
|
|
zend_function *f = EX(func);
|
|
cryo_change *ch = cryo_find_internal(f);
|
|
if (!ch) {
|
|
zend_throw_error(NULL, "Cryo\\Engine: no body for %s()", ZSTR_VAL(f->common.function_name));
|
|
return;
|
|
}
|
|
zif_handler orig = ch->orig_handler;
|
|
zval closure;
|
|
ZVAL_COPY(&closure, &ch->closure);
|
|
|
|
zend_fcall_info fci;
|
|
fci.size = sizeof(fci);
|
|
ZVAL_COPY_VALUE(&fci.function_name, &closure);
|
|
fci.object = NULL;
|
|
fci.retval = return_value;
|
|
fci.params = ZEND_CALL_NUM_ARGS(execute_data) ? ZEND_CALL_ARG(execute_data, 1) : NULL;
|
|
fci.param_count = ZEND_CALL_NUM_ARGS(execute_data);
|
|
fci.named_params = (ZEND_CALL_INFO(execute_data) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)
|
|
? execute_data->extra_named_params : NULL;
|
|
|
|
/* inside the body, the function itself is the original one */
|
|
f->internal_function.handler = orig;
|
|
zend_call_function(&fci, NULL);
|
|
if (cryo_find_internal(f)) {
|
|
f->internal_function.handler = cryo_engine_trampoline;
|
|
}
|
|
zval_ptr_dtor(&closure);
|
|
if (Z_ISREF_P(return_value) && !(f->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
|
|
zend_unwrap_reference(return_value);
|
|
}
|
|
}
|
|
|
|
static ZEND_NAMED_FUNCTION(cryo_engine_removed_handler)
|
|
{
|
|
zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(EX(func)->common.function_name));
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Undo */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static void cryo_change_undo(cryo_change *ch)
|
|
{
|
|
switch (ch->kind) {
|
|
case CRYO_CH_FUNCTION:
|
|
case CRYO_CH_METHOD:
|
|
cryo_unswap_body(ch);
|
|
break;
|
|
case CRYO_CH_INTERNAL: {
|
|
/* earlier changes of the same function stay active */
|
|
bool other = false;
|
|
for (uint32_t i = 0; i < cryo_engine.count; i++) {
|
|
if (cryo_engine.list[i] != ch && cryo_engine.list[i]->kind == CRYO_CH_INTERNAL
|
|
&& cryo_engine.list[i]->func == ch->func) {
|
|
other = true;
|
|
}
|
|
}
|
|
if (!other) {
|
|
ch->func->internal_function.handler = ch->orig_handler;
|
|
#if PHP_VERSION_ID >= 80400
|
|
ch->func->internal_function.frameless_function_infos = ch->orig_flf;
|
|
#endif
|
|
}
|
|
break;
|
|
}
|
|
case CRYO_CH_CONSTANT:
|
|
case CRYO_CH_CLASS_CONSTANT: {
|
|
uint32_t flags = Z_CONSTANT_FLAGS_P(ch->slot);
|
|
zval_ptr_dtor(ch->slot);
|
|
ZVAL_COPY_VALUE(ch->slot, &ch->saved_value);
|
|
Z_CONSTANT_FLAGS_P(ch->slot) = flags;
|
|
ZVAL_UNDEF(&ch->saved_value);
|
|
break;
|
|
}
|
|
case CRYO_CH_ADD_FUNCTION:
|
|
case CRYO_CH_ADD_METHOD:
|
|
cryo_hash_del_keep(ch->table, ch->key);
|
|
cryo_free_user_function(ch, ch->func);
|
|
break;
|
|
case CRYO_CH_REMOVE_FUNCTION:
|
|
case CRYO_CH_REMOVE_METHOD:
|
|
if (ch->in_place) {
|
|
cryo_unswap_body(ch);
|
|
}
|
|
if (ch->func->type == ZEND_INTERNAL_FUNCTION) {
|
|
ch->func->internal_function.handler = ch->orig_handler;
|
|
}
|
|
zend_hash_add_new_ptr(ch->table, ch->key, ch->func);
|
|
break;
|
|
case CRYO_CH_RENAME_FUNCTION: {
|
|
zend_string *lc = zend_string_tolower(ch->extra);
|
|
cryo_hash_del_keep(ch->table, lc);
|
|
zend_string_release(lc);
|
|
if (ch->func->type == ZEND_USER_FUNCTION) {
|
|
zend_string_release(ch->func->common.function_name);
|
|
ch->func->common.function_name = ch->saved_name;
|
|
ch->saved_name = NULL;
|
|
}
|
|
zend_hash_add_new_ptr(ch->table, ch->key, ch->func);
|
|
break;
|
|
}
|
|
case CRYO_CH_SUPERGLOBAL:
|
|
zend_hash_del(CG(auto_globals), ch->sg_name);
|
|
zend_string_release(ch->sg_name);
|
|
ch->sg_name = NULL;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static uint32_t cryo_engine_revert_from(uint32_t index)
|
|
{
|
|
uint32_t n = 0;
|
|
while (cryo_engine.count > index) {
|
|
cryo_change *ch = cryo_engine.list[cryo_engine.count - 1];
|
|
cryo_change_undo(ch);
|
|
cryo_engine.count--;
|
|
cryo_change_free(ch);
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
/* Revert change #id and every later change on the same target. */
|
|
static int cryo_engine_revert_id(zend_long id)
|
|
{
|
|
uint32_t idx = UINT32_MAX;
|
|
for (uint32_t i = 0; i < cryo_engine.count; i++) {
|
|
if (cryo_engine.list[i]->id == id) {
|
|
idx = i;
|
|
break;
|
|
}
|
|
}
|
|
if (idx == UINT32_MAX) {
|
|
return -1;
|
|
}
|
|
cryo_change *base = cryo_engine.list[idx];
|
|
/* collect the later changes on the same target, newest first */
|
|
uint32_t n = 0;
|
|
for (uint32_t i = cryo_engine.count; i > idx; i--) {
|
|
cryo_change *ch = cryo_engine.list[i - 1];
|
|
bool same = ch == base || zend_string_equals_ci(ch->target, base->target)
|
|
|| (ch->func && ch->func == base->func);
|
|
if (!same) {
|
|
continue;
|
|
}
|
|
if (ch->func && (ch->kind == CRYO_CH_FUNCTION || ch->kind == CRYO_CH_METHOD || ch->in_place)) {
|
|
const char *busy = cryo_func_busy(ch->func);
|
|
if (busy) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot revert %s: %s", ZSTR_VAL(ch->target), busy);
|
|
return (int) n;
|
|
}
|
|
}
|
|
cryo_change_undo(ch);
|
|
memmove(&cryo_engine.list[i - 1], &cryo_engine.list[i], (cryo_engine.count - i) * sizeof(cryo_change *));
|
|
cryo_engine.count--;
|
|
cryo_change_free(ch);
|
|
n++;
|
|
}
|
|
return (int) n;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Operations (shared by the PHP methods and the journal replay) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static zend_function *cryo_lookup_function(zend_string *name, zend_string **lc_out)
|
|
{
|
|
zend_string *lc = zend_string_tolower(name);
|
|
if (ZSTR_LEN(lc) && ZSTR_VAL(lc)[0] == '\\') {
|
|
zend_string *t = zend_string_init(ZSTR_VAL(lc) + 1, ZSTR_LEN(lc) - 1, 0);
|
|
zend_string_release(lc);
|
|
lc = t;
|
|
}
|
|
zend_function *f = zend_hash_find_ptr(EG(function_table), lc);
|
|
if (lc_out) {
|
|
*lc_out = lc;
|
|
} else {
|
|
zend_string_release(lc);
|
|
}
|
|
return f;
|
|
}
|
|
|
|
static zend_class_entry *cryo_lookup_class(zend_string *name)
|
|
{
|
|
zend_class_entry *ce = zend_lookup_class(name);
|
|
if (!ce && !EG(exception)) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Class %s does not exist", ZSTR_VAL(name));
|
|
}
|
|
if (ce && (ce->type != ZEND_USER_CLASS)) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Class %s is internal", ZSTR_VAL(ce->name));
|
|
return NULL;
|
|
}
|
|
if (ce && (ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"Class %s is immutable (cached by OPcache); disable OPcache for this script to change it", ZSTR_VAL(ce->name));
|
|
return NULL;
|
|
}
|
|
return ce;
|
|
}
|
|
|
|
static zend_long cryo_op_redefine_function(zend_string *name, zval *closure)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_function *f = cryo_lookup_function(name, NULL);
|
|
if (!f) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Function %s() does not exist", ZSTR_VAL(name));
|
|
return 0;
|
|
}
|
|
if (f->type == ZEND_INTERNAL_FUNCTION) {
|
|
if (!cryo_engine_ini("cryo.engine_internal_override")) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"%s() is an internal function; set cryo.engine_internal_override=1 to redefine it",
|
|
ZSTR_VAL(f->common.function_name));
|
|
return 0;
|
|
}
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_INTERNAL, f->common.function_name);
|
|
cryo_change *prev = cryo_find_internal(f);
|
|
ch->func = f;
|
|
ch->orig_handler = prev ? prev->orig_handler : f->internal_function.handler;
|
|
#if PHP_VERSION_ID >= 80400
|
|
ch->orig_flf = prev ? prev->orig_flf : f->internal_function.frameless_function_infos;
|
|
f->internal_function.frameless_function_infos = NULL;
|
|
#endif
|
|
ZVAL_COPY(&ch->closure, closure);
|
|
f->internal_function.handler = cryo_engine_trampoline;
|
|
/* code compiled from now on must call the handler (no VM builtin) */
|
|
cryo_compile_no_builtins(ZEND_COMPILE_NO_BUILTINS);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
zend_function *cf = cryo_closure_func(closure);
|
|
if (!cf) return 0;
|
|
char label[256];
|
|
snprintf(label, sizeof(label), "%s()", ZSTR_VAL(f->common.function_name));
|
|
if (!cryo_check_user_target(f, label)) return 0;
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_FUNCTION, f->common.function_name);
|
|
ZVAL_COPY(&ch->closure, closure);
|
|
cryo_swap_body(ch, f, cf);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_function *cryo_lookup_method(zend_class_entry *ce, zend_string *method, zend_string **lc_out)
|
|
{
|
|
zend_string *lc = zend_string_tolower(method);
|
|
zend_function *f = zend_hash_find_ptr(&ce->function_table, lc);
|
|
if (lc_out) *lc_out = lc; else zend_string_release(lc);
|
|
return f;
|
|
}
|
|
|
|
static zend_string *cryo_method_label(zend_class_entry *ce, zend_string *m)
|
|
{
|
|
return zend_strpprintf(0, "%s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(m));
|
|
}
|
|
|
|
static zend_long cryo_op_redefine_method(zend_string *cls, zend_string *method, zval *closure)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_class_entry *ce = cryo_lookup_class(cls);
|
|
if (!ce) return 0;
|
|
zend_function *f = cryo_lookup_method(ce, method, NULL);
|
|
if (!f) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(method));
|
|
return 0;
|
|
}
|
|
if (f->common.scope != ce) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"Method %s::%s() is inherited from %s; redefine it there", ZSTR_VAL(ce->name),
|
|
ZSTR_VAL(method), ZSTR_VAL(f->common.scope->name));
|
|
return 0;
|
|
}
|
|
if (f->type != ZEND_USER_FUNCTION) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Method %s::%s() is internal", ZSTR_VAL(ce->name), ZSTR_VAL(method));
|
|
return 0;
|
|
}
|
|
zend_function *cf = cryo_closure_func(closure);
|
|
if (!cf) return 0;
|
|
zend_string *label = cryo_method_label(ce, f->common.function_name);
|
|
char buf[300];
|
|
snprintf(buf, sizeof(buf), "%s()", ZSTR_VAL(label));
|
|
if (!cryo_check_user_target(f, buf)) {
|
|
zend_string_release(label);
|
|
return 0;
|
|
}
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_METHOD, label);
|
|
zend_string_release(label);
|
|
ZVAL_COPY(&ch->closure, closure);
|
|
cryo_swap_body(ch, f, cf);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_long cryo_op_add_function(zend_string *name, zval *closure)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_string *lc;
|
|
zend_function *f = cryo_lookup_function(name, &lc);
|
|
if (f) {
|
|
zend_string_release(lc);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Function %s() already exists", ZSTR_VAL(name));
|
|
return 0;
|
|
}
|
|
zend_function *cf = cryo_closure_func(closure);
|
|
if (!cf) { zend_string_release(lc); return 0; }
|
|
zend_string *display = (ZSTR_VAL(name)[0] == '\\')
|
|
? zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0) : zend_string_copy(name);
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_ADD_FUNCTION, display);
|
|
ZVAL_COPY(&ch->closure, closure);
|
|
ch->func = cryo_new_user_function(ch, cf, display, NULL, 0);
|
|
zend_string_release(display);
|
|
ch->table = EG(function_table);
|
|
ch->key = lc;
|
|
zend_hash_add_new_ptr(ch->table, lc, ch->func);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_long cryo_op_add_method(zend_string *cls, zend_string *method, zval *closure, zend_long flags)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_class_entry *ce = cryo_lookup_class(cls);
|
|
if (!ce) return 0;
|
|
zend_string *lc;
|
|
if (cryo_lookup_method(ce, method, &lc)) {
|
|
zend_string_release(lc);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Method %s::%s() already exists", ZSTR_VAL(ce->name), ZSTR_VAL(method));
|
|
return 0;
|
|
}
|
|
if (ZSTR_LEN(lc) > 2 && ZSTR_VAL(lc)[0] == '_' && ZSTR_VAL(lc)[1] == '_') {
|
|
zend_string_release(lc);
|
|
zend_throw_exception(cryo_ce_exception, "Magic methods cannot be added", 0);
|
|
return 0;
|
|
}
|
|
uint32_t acc = (uint32_t) flags & (ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC | ZEND_ACC_FINAL);
|
|
if (!(acc & ZEND_ACC_PPP_MASK)) acc |= ZEND_ACC_PUBLIC;
|
|
zend_function *cf = cryo_closure_func(closure);
|
|
if (!cf) { zend_string_release(lc); return 0; }
|
|
zend_string *label = cryo_method_label(ce, method);
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_ADD_METHOD, label);
|
|
zend_string_release(label);
|
|
ZVAL_COPY(&ch->closure, closure);
|
|
ch->flags = (zend_long) acc;
|
|
ch->func = cryo_new_user_function(ch, cf, method, ce, acc);
|
|
ch->table = &ce->function_table;
|
|
ch->key = lc;
|
|
zend_hash_add_new_ptr(ch->table, lc, ch->func);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_long cryo_op_remove(zend_string *cls, zend_string *name)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_class_entry *ce = NULL;
|
|
zend_string *lc;
|
|
zend_function *f;
|
|
if (cls) {
|
|
ce = cryo_lookup_class(cls);
|
|
if (!ce) return 0;
|
|
f = cryo_lookup_method(ce, name, &lc);
|
|
if (f && f->common.scope != ce) {
|
|
zend_string_release(lc);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Method %s::%s() is inherited from %s",
|
|
ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(f->common.scope->name));
|
|
return 0;
|
|
}
|
|
} else {
|
|
f = cryo_lookup_function(name, &lc);
|
|
}
|
|
if (!f) {
|
|
zend_string_release(lc);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "%s %s%s%s() does not exist", ce ? "Method" : "Function",
|
|
ce ? ZSTR_VAL(ce->name) : "", ce ? "::" : "", ZSTR_VAL(name));
|
|
return 0;
|
|
}
|
|
zend_string *label = ce ? cryo_method_label(ce, f->common.function_name) : zend_string_copy(f->common.function_name);
|
|
if (f->type == ZEND_INTERNAL_FUNCTION) {
|
|
if (ce || !cryo_engine_ini("cryo.engine_internal_override")) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"%s() is internal; set cryo.engine_internal_override=1 to remove it", ZSTR_VAL(label));
|
|
zend_string_release(label);
|
|
zend_string_release(lc);
|
|
return 0;
|
|
}
|
|
} else {
|
|
char buf[300];
|
|
snprintf(buf, sizeof(buf), "%s()", ZSTR_VAL(label));
|
|
if (!cryo_check_user_target(f, buf)) {
|
|
zend_string_release(label);
|
|
zend_string_release(lc);
|
|
return 0;
|
|
}
|
|
}
|
|
zval thrower;
|
|
if (!cryo_make_thrower(&thrower, ce ? "method" : "function", label)) {
|
|
zend_string_release(label);
|
|
zend_string_release(lc);
|
|
if (!EG(exception)) zend_throw_exception(cryo_ce_exception, "Cannot build the removal stub", 0);
|
|
return 0;
|
|
}
|
|
cryo_change *ch = cryo_change_new(ce ? CRYO_CH_REMOVE_METHOD : CRYO_CH_REMOVE_FUNCTION, label);
|
|
zend_string_release(label);
|
|
ch->func = f;
|
|
ch->table = ce ? &ce->function_table : EG(function_table);
|
|
ch->key = lc;
|
|
/* call sites that cached the function now reach a stub that throws */
|
|
if (f->type == ZEND_USER_FUNCTION) {
|
|
ZVAL_COPY_VALUE(&ch->closure, &thrower);
|
|
cryo_swap_body(ch, f, (zend_function *) zend_get_closure_method_def(Z_OBJ(thrower)));
|
|
} else {
|
|
zval_ptr_dtor(&thrower);
|
|
ch->orig_handler = f->internal_function.handler;
|
|
f->internal_function.handler = cryo_engine_removed_handler;
|
|
}
|
|
cryo_hash_del_keep(ch->table, lc);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_long cryo_op_rename_function(zend_string *from, zend_string *to)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_string *lc, *lc_to;
|
|
zend_function *f = cryo_lookup_function(from, &lc);
|
|
if (!f) {
|
|
zend_string_release(lc);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Function %s() does not exist", ZSTR_VAL(from));
|
|
return 0;
|
|
}
|
|
if (cryo_lookup_function(to, &lc_to)) {
|
|
zend_string_release(lc);
|
|
zend_string_release(lc_to);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Function %s() already exists", ZSTR_VAL(to));
|
|
return 0;
|
|
}
|
|
if (f->type == ZEND_INTERNAL_FUNCTION && !cryo_engine_ini("cryo.engine_internal_override")) {
|
|
zend_string_release(lc);
|
|
zend_string_release(lc_to);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0,
|
|
"%s() is internal; set cryo.engine_internal_override=1 to rename it", ZSTR_VAL(f->common.function_name));
|
|
return 0;
|
|
}
|
|
if (f->common.fn_flags & ZEND_ACC_IMMUTABLE) {
|
|
zend_string_release(lc);
|
|
zend_string_release(lc_to);
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "%s() is immutable (cached by OPcache)", ZSTR_VAL(f->common.function_name));
|
|
return 0;
|
|
}
|
|
zend_string *display = (ZSTR_VAL(to)[0] == '\\') ? zend_string_init(ZSTR_VAL(to) + 1, ZSTR_LEN(to) - 1, 0) : zend_string_copy(to);
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_RENAME_FUNCTION, f->common.function_name);
|
|
ch->extra = display;
|
|
ch->func = f;
|
|
ch->table = EG(function_table);
|
|
ch->key = lc;
|
|
cryo_hash_del_keep(ch->table, lc);
|
|
if (f->type == ZEND_USER_FUNCTION) {
|
|
ch->saved_name = f->common.function_name;
|
|
f->common.function_name = zend_string_copy(display);
|
|
}
|
|
zend_hash_add_new_ptr(ch->table, lc_to, f);
|
|
zend_string_release(lc_to);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
/* Constant values: same rules as define() (no objects, resources, references) */
|
|
static bool cryo_valid_constant_value(zval *v, int depth)
|
|
{
|
|
switch (Z_TYPE_P(v)) {
|
|
case IS_NULL: case IS_FALSE: case IS_TRUE: case IS_LONG: case IS_DOUBLE: case IS_STRING:
|
|
return true;
|
|
case IS_ARRAY: {
|
|
if (depth > 64) return false;
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(v), e) {
|
|
if (Z_TYPE_P(e) == IS_REFERENCE || !cryo_valid_constant_value(e, depth + 1)) return false;
|
|
} ZEND_HASH_FOREACH_END();
|
|
return true;
|
|
}
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static zend_long cryo_op_redefine_constant(zend_string *name, zval *value)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
ZVAL_DEREF(value);
|
|
if (!cryo_valid_constant_value(value, 0)) {
|
|
zend_throw_exception(cryo_ce_exception, "Constant values must be scalars, null or arrays of those", 0);
|
|
return 0;
|
|
}
|
|
const char *sep = zend_memnstr(ZSTR_VAL(name), "::", 2, ZSTR_VAL(name) + ZSTR_LEN(name));
|
|
cryo_change *ch;
|
|
zval *slot;
|
|
if (sep) {
|
|
zend_string *cls = zend_string_init(ZSTR_VAL(name), sep - ZSTR_VAL(name), 0);
|
|
zend_string *cn = zend_string_init(sep + 2, ZSTR_LEN(name) - (sep + 2 - ZSTR_VAL(name)), 0);
|
|
zend_class_entry *ce = cryo_lookup_class(cls);
|
|
zend_string_release(cls);
|
|
if (!ce) { zend_string_release(cn); return 0; }
|
|
zend_class_constant *c = zend_hash_find_ptr(&ce->constants_table, cn);
|
|
if (!c) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Constant %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(cn));
|
|
zend_string_release(cn);
|
|
return 0;
|
|
}
|
|
if (c->ce != ce) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Constant %s::%s is declared in %s", ZSTR_VAL(ce->name),
|
|
ZSTR_VAL(cn), ZSTR_VAL(c->ce->name));
|
|
zend_string_release(cn);
|
|
return 0;
|
|
}
|
|
if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "%s::%s is an enum case", ZSTR_VAL(ce->name), ZSTR_VAL(cn));
|
|
zend_string_release(cn);
|
|
return 0;
|
|
}
|
|
/* evaluate a pending constant expression first: call sites cache the
|
|
* slot and expect a value, never an AST */
|
|
if (Z_TYPE(c->value) == IS_CONSTANT_AST && zend_update_class_constant(c, cn, c->ce) != SUCCESS) {
|
|
zend_string_release(cn);
|
|
return 0;
|
|
}
|
|
if (ZEND_TYPE_IS_SET(c->type)) {
|
|
zval tmp;
|
|
ZVAL_COPY(&tmp, value);
|
|
bool ok = zend_verify_class_constant_type(c, cn, &tmp);
|
|
zval_ptr_dtor(&tmp);
|
|
if (!ok) { zend_string_release(cn); return 0; }
|
|
}
|
|
zend_string *label = zend_strpprintf(0, "%s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(cn));
|
|
zend_string_release(cn);
|
|
ch = cryo_change_new(CRYO_CH_CLASS_CONSTANT, label);
|
|
zend_string_release(label);
|
|
slot = &c->value;
|
|
} else {
|
|
zend_string *n = (ZSTR_VAL(name)[0] == '\\') ? zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0) : zend_string_copy(name);
|
|
zend_constant *c = zend_get_constant_ptr(n);
|
|
if (!c) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Constant %s does not exist", ZSTR_VAL(n));
|
|
zend_string_release(n);
|
|
return 0;
|
|
}
|
|
if (ZEND_CONSTANT_MODULE_NUMBER(c) != PHP_USER_CONSTANT || (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Constant %s is internal", ZSTR_VAL(n));
|
|
zend_string_release(n);
|
|
return 0;
|
|
}
|
|
ch = cryo_change_new(CRYO_CH_CONSTANT, c->name ? c->name : n);
|
|
zend_string_release(n);
|
|
slot = &c->value;
|
|
/* code compiled from now on must fetch constants at run time */
|
|
cryo_compile_no_builtins(ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION);
|
|
}
|
|
if (sep) {
|
|
cryo_compile_no_builtins(ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION);
|
|
}
|
|
uint32_t flags = Z_CONSTANT_FLAGS_P(slot);
|
|
ch->slot = slot;
|
|
ZVAL_COPY_VALUE(&ch->saved_value, slot);
|
|
ZVAL_COPY(slot, value);
|
|
Z_CONSTANT_FLAGS_P(slot) = flags;
|
|
ZVAL_COPY(&ch->value, value);
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
static zend_long cryo_op_superglobal(zend_string *name)
|
|
{
|
|
if (!cryo_check_engine_state()) return 0;
|
|
zend_string *n = (ZSTR_LEN(name) && ZSTR_VAL(name)[0] == '$') ? zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0) : zend_string_copy(name);
|
|
bool valid = ZSTR_LEN(n) > 0;
|
|
for (size_t i = 0; i < ZSTR_LEN(n); i++) {
|
|
unsigned char c = (unsigned char) ZSTR_VAL(n)[i];
|
|
if (!(c == '_' || c >= 0x80 || isalpha(c) || (i > 0 && isdigit(c)))) valid = false;
|
|
}
|
|
if (!valid) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Invalid variable name \"%s\"", ZSTR_VAL(n));
|
|
zend_string_release(n);
|
|
return 0;
|
|
}
|
|
if (zend_hash_exists(CG(auto_globals), n)) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "$%s is already a superglobal", ZSTR_VAL(n));
|
|
zend_string_release(n);
|
|
return 0;
|
|
}
|
|
cryo_change *ch = cryo_change_new(CRYO_CH_SUPERGLOBAL, n);
|
|
ch->sg_name = zend_string_init(ZSTR_VAL(n), ZSTR_LEN(n), 1);
|
|
zend_string_release(n);
|
|
zend_register_auto_global(ch->sg_name, 0, NULL);
|
|
zend_auto_global *ag = zend_hash_find_ptr(CG(auto_globals), ch->sg_name);
|
|
if (ag) {
|
|
ag->armed = 0;
|
|
}
|
|
cryo_change_push(ch);
|
|
return ch->id;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Closure export / import (by source: file + lines + scope + bound vars) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
bool cryo_closure_export(zval *closure, zval *out)
|
|
{
|
|
zend_function *f = (zend_function *) zend_get_closure_method_def(Z_OBJ_P(closure));
|
|
if (f->type != ZEND_USER_FUNCTION || !f->op_array.filename
|
|
|| strstr(ZSTR_VAL(f->op_array.filename), "eval()'d code")
|
|
|| strcmp(ZSTR_VAL(f->op_array.filename), "Cryo\\Engine") == 0) {
|
|
return false;
|
|
}
|
|
array_init(out);
|
|
add_assoc_str(out, "file", zend_string_copy(f->op_array.filename));
|
|
add_assoc_long(out, "start", f->op_array.line_start);
|
|
add_assoc_long(out, "end", f->op_array.line_end);
|
|
add_assoc_long(out, "args", f->op_array.num_args);
|
|
zval cvs;
|
|
array_init(&cvs);
|
|
for (int i = 0; i < f->op_array.last_var; i++) {
|
|
add_next_index_str(&cvs, zend_string_copy(f->op_array.vars[i]));
|
|
}
|
|
add_assoc_zval(out, "cvs", &cvs);
|
|
if (f->common.scope) {
|
|
add_assoc_str(out, "scope", zend_string_copy(f->common.scope->name));
|
|
} else {
|
|
add_assoc_null(out, "scope");
|
|
}
|
|
zval vars;
|
|
HashTable *ht = cryo_closure_statics(f);
|
|
if (ht) {
|
|
array_init(&vars);
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, k, v) {
|
|
if (!k) continue;
|
|
ZVAL_DEREF(v);
|
|
if (Z_TYPE_P(v) == IS_UNDEF) continue;
|
|
Z_TRY_ADDREF_P(v);
|
|
zend_hash_update(Z_ARRVAL(vars), k, v);
|
|
} ZEND_HASH_FOREACH_END();
|
|
} else {
|
|
array_init(&vars);
|
|
}
|
|
add_assoc_zval(out, "vars", &vars);
|
|
return true;
|
|
}
|
|
|
|
static bool cryo_is_ident(char c)
|
|
{
|
|
return c == '_' || isalnum((unsigned char) c) || (unsigned char) c >= 0x80;
|
|
}
|
|
|
|
/* Same parameters count and compiled variables as the exported closure
|
|
* (tells apart closures that share their lines). */
|
|
static bool cryo_closure_matches(zend_op_array *op, zval *args, zval *cvs)
|
|
{
|
|
if (args && Z_TYPE_P(args) == IS_LONG && (zend_long) op->num_args != Z_LVAL_P(args)) {
|
|
return false;
|
|
}
|
|
if (cvs && Z_TYPE_P(cvs) == IS_ARRAY) {
|
|
if (zend_hash_num_elements(Z_ARRVAL_P(cvs)) != (uint32_t) op->last_var) return false;
|
|
int i = 0;
|
|
zval *v;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(cvs), v) {
|
|
if (Z_TYPE_P(v) != IS_STRING || !zend_string_equals(Z_STR_P(v), op->vars[i])) return false;
|
|
i++;
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* Recompile the closure that spans [start, end] of file. */
|
|
bool cryo_closure_import(zval *desc, zval *out)
|
|
{
|
|
if (Z_TYPE_P(desc) != IS_ARRAY) return false;
|
|
zval *zfile = zend_hash_str_find(Z_ARRVAL_P(desc), "file", 4);
|
|
zval *zs = zend_hash_str_find(Z_ARRVAL_P(desc), "start", 5);
|
|
zval *ze = zend_hash_str_find(Z_ARRVAL_P(desc), "end", 3);
|
|
zval *zscope = zend_hash_str_find(Z_ARRVAL_P(desc), "scope", 5);
|
|
zval *zvars = zend_hash_str_find(Z_ARRVAL_P(desc), "vars", 4);
|
|
zval *zargs = zend_hash_str_find(Z_ARRVAL_P(desc), "args", 4);
|
|
zval *zcvs = zend_hash_str_find(Z_ARRVAL_P(desc), "cvs", 3);
|
|
if (!zfile || Z_TYPE_P(zfile) != IS_STRING || !zs || Z_TYPE_P(zs) != IS_LONG || !ze || Z_TYPE_P(ze) != IS_LONG
|
|
|| Z_LVAL_P(zs) < 1 || Z_LVAL_P(ze) < Z_LVAL_P(zs)) {
|
|
zend_throw_exception(cryo_ce_exception, "Invalid closure descriptor", 0);
|
|
return false;
|
|
}
|
|
zend_class_entry *scope = NULL;
|
|
if (zscope && Z_TYPE_P(zscope) == IS_STRING) {
|
|
scope = zend_lookup_class(Z_STR_P(zscope));
|
|
if (!scope) {
|
|
if (!EG(exception)) zend_throw_exception_ex(cryo_ce_exception, 0, "Class %s does not exist", Z_STRVAL_P(zscope));
|
|
return false;
|
|
}
|
|
}
|
|
php_stream *s = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb", REPORT_ERRORS, NULL);
|
|
if (!s) {
|
|
if (!EG(exception)) zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot read %s", Z_STRVAL_P(zfile));
|
|
return false;
|
|
}
|
|
zend_string *src = php_stream_copy_to_mem(s, PHP_STREAM_COPY_ALL, 0);
|
|
php_stream_close(s);
|
|
if (!src) src = ZSTR_EMPTY_ALLOC();
|
|
|
|
zend_long start = Z_LVAL_P(zs), end = Z_LVAL_P(ze), line = 1;
|
|
const char *p = ZSTR_VAL(src), *e = p + ZSTR_LEN(src), *rs = NULL, *re = NULL, *last = NULL;
|
|
if (start == 1) rs = p;
|
|
for (const char *c = p; c < e; c++) {
|
|
if (*c == '\n') {
|
|
line++;
|
|
if (line == start) rs = c + 1;
|
|
if (line == end) last = c + 1;
|
|
if (line == end + 1) { re = c; break; }
|
|
}
|
|
}
|
|
if (end == 1) last = p;
|
|
if (!re) re = e;
|
|
bool ok = false;
|
|
if (rs && last && rs <= last) {
|
|
int saved_er = EG(error_reporting);
|
|
EG(error_reporting) = 0;
|
|
for (const char *c = rs; c < re && !ok; c++) {
|
|
size_t kw = 0;
|
|
if ((size_t)(re - c) >= 8 && memcmp(c, "function", 8) == 0) kw = 8;
|
|
else if ((size_t)(re - c) >= 2 && memcmp(c, "fn", 2) == 0) kw = 2;
|
|
if (!kw || (c > rs && cryo_is_ident(c[-1])) || (c + kw < re && cryo_is_ident(c[kw]))) continue;
|
|
const char *from = c;
|
|
/* include a preceding "static" */
|
|
const char *b = c;
|
|
while (b > rs && (b[-1] == ' ' || b[-1] == '\t')) b--;
|
|
if (b - rs >= 6 && memcmp(b - 6, "static", 6) == 0 && (b - 6 == rs || !cryo_is_ident(b[-7]))) from = b - 6;
|
|
for (const char *q = re; q > (last > c ? last : c + kw) && !ok; q--) {
|
|
smart_str code = {0};
|
|
smart_str_appends(&code, "return ");
|
|
for (zend_long i = 1; i < start; i++) smart_str_appendc(&code, '\n');
|
|
smart_str_appendl(&code, from, q - from);
|
|
smart_str_appends(&code, ";");
|
|
smart_str_0(&code);
|
|
zend_op_array *op = zend_compile_string(code.s, Z_STRVAL_P(zfile), ZEND_COMPILE_POSITION_AFTER_OPEN_TAG);
|
|
smart_str_free(&code);
|
|
if (EG(exception)) {
|
|
zend_clear_exception();
|
|
}
|
|
if (!op) continue;
|
|
if (op->num_dynamic_func_defs >= 1 && (op->dynamic_func_defs[0]->fn_flags & ZEND_ACC_CLOSURE)
|
|
&& op->dynamic_func_defs[0]->line_start == (uint32_t) start
|
|
&& op->dynamic_func_defs[0]->line_end == (uint32_t) end
|
|
&& cryo_closure_matches(op->dynamic_func_defs[0], zargs, zcvs)) {
|
|
zend_create_closure(out, (zend_function *) op->dynamic_func_defs[0], scope, scope, NULL);
|
|
ok = true;
|
|
}
|
|
destroy_op_array(op);
|
|
efree(op);
|
|
}
|
|
}
|
|
EG(error_reporting) = saved_er;
|
|
}
|
|
zend_string_release(src);
|
|
if (!ok) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot find the closure of %s:%ld-%ld (source changed?)",
|
|
Z_STRVAL_P(zfile), (long) start, (long) end);
|
|
return false;
|
|
}
|
|
if (zvars && Z_TYPE_P(zvars) == IS_ARRAY) {
|
|
zend_function *cf = (zend_function *) zend_get_closure_method_def(Z_OBJ_P(out));
|
|
HashTable *ht = ZEND_MAP_PTR(cf->op_array.static_variables_ptr) ? ZEND_MAP_PTR_GET(cf->op_array.static_variables_ptr) : NULL;
|
|
if (ht) {
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zvars), k, v) {
|
|
zval *slot;
|
|
if (k && (slot = zend_hash_find(ht, k))) {
|
|
ZVAL_DEREF(slot);
|
|
zval_ptr_dtor(slot);
|
|
ZVAL_COPY(slot, v);
|
|
}
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Journal export / replay (also used by cryo_freeze/cryo_thaw and the */
|
|
/* sandbox engine proxy) */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static void cryo_change_describe(cryo_change *ch, zval *e, bool full)
|
|
{
|
|
array_init(e);
|
|
add_assoc_long(e, "id", ch->id);
|
|
add_assoc_string(e, "kind", (char *) cryo_kind_names[ch->kind]);
|
|
add_assoc_str(e, "target", zend_string_copy(ch->target));
|
|
add_assoc_double(e, "time", ch->time);
|
|
if (ch->file) add_assoc_str(e, "file", zend_string_copy(ch->file)); else add_assoc_null(e, "file");
|
|
add_assoc_long(e, "line", ch->line);
|
|
if (ch->kind == CRYO_CH_RENAME_FUNCTION) add_assoc_str(e, "to", zend_string_copy(ch->extra));
|
|
if (ch->kind == CRYO_CH_ADD_METHOD) add_assoc_long(e, "flags", ch->flags);
|
|
bool replayable = true;
|
|
if (ch->kind == CRYO_CH_FUNCTION || ch->kind == CRYO_CH_METHOD || ch->kind == CRYO_CH_INTERNAL
|
|
|| ch->kind == CRYO_CH_ADD_FUNCTION || ch->kind == CRYO_CH_ADD_METHOD) {
|
|
zval cd;
|
|
if (cryo_closure_export(&ch->closure, &cd)) {
|
|
if (full) add_assoc_zval(e, "closure", &cd); else zval_ptr_dtor(&cd);
|
|
} else {
|
|
replayable = false;
|
|
}
|
|
}
|
|
if (full && (ch->kind == CRYO_CH_CONSTANT || ch->kind == CRYO_CH_CLASS_CONSTANT)) {
|
|
Z_TRY_ADDREF(ch->value);
|
|
add_assoc_zval(e, "value", &ch->value);
|
|
}
|
|
add_assoc_bool(e, "replayable", replayable);
|
|
}
|
|
|
|
void cryo_engine_journal_export(zval *out)
|
|
{
|
|
array_init(out);
|
|
for (uint32_t i = 0; i < cryo_engine.count; i++) {
|
|
zval e;
|
|
cryo_change_describe(cryo_engine.list[i], &e, true);
|
|
add_next_index_zval(out, &e);
|
|
}
|
|
}
|
|
|
|
static zend_string *cryo_entry_str(HashTable *h, const char *k)
|
|
{
|
|
zval *v = zend_hash_str_find(h, k, strlen(k));
|
|
return v && Z_TYPE_P(v) == IS_STRING ? Z_STR_P(v) : NULL;
|
|
}
|
|
|
|
/* Apply one exported entry; returns the new change id, 0 on failure (exception). */
|
|
zend_long cryo_engine_apply_entry(zval *entry)
|
|
{
|
|
if (Z_TYPE_P(entry) != IS_ARRAY) {
|
|
zend_throw_exception(cryo_ce_exception, "Invalid journal entry", 0);
|
|
return 0;
|
|
}
|
|
HashTable *h = Z_ARRVAL_P(entry);
|
|
zend_string *kind = cryo_entry_str(h, "kind"), *target = cryo_entry_str(h, "target");
|
|
if (!kind || !target) {
|
|
zend_throw_exception(cryo_ce_exception, "Invalid journal entry", 0);
|
|
return 0;
|
|
}
|
|
zval *rp = zend_hash_str_find(h, "replayable", sizeof("replayable") - 1);
|
|
if (rp && Z_TYPE_P(rp) == IS_FALSE) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Change on %s cannot be replayed (closure without source)", ZSTR_VAL(target));
|
|
return 0;
|
|
}
|
|
zval closure;
|
|
ZVAL_UNDEF(&closure);
|
|
zval *cd = zend_hash_str_find(h, "closure", sizeof("closure") - 1);
|
|
if (cd && !cryo_closure_import(cd, &closure)) {
|
|
return 0;
|
|
}
|
|
zend_long id = 0;
|
|
const char *sep = zend_memnstr(ZSTR_VAL(target), "::", 2, ZSTR_VAL(target) + ZSTR_LEN(target));
|
|
zend_string *cls = NULL, *mname = NULL;
|
|
if (sep) {
|
|
cls = zend_string_init(ZSTR_VAL(target), sep - ZSTR_VAL(target), 0);
|
|
mname = zend_string_init(sep + 2, ZSTR_LEN(target) - (sep + 2 - ZSTR_VAL(target)), 0);
|
|
}
|
|
bool need_closure = zend_string_equals_literal(kind, "function") || zend_string_equals_literal(kind, "internal")
|
|
|| zend_string_equals_literal(kind, "method") || zend_string_equals_literal(kind, "add_function")
|
|
|| zend_string_equals_literal(kind, "add_method");
|
|
if (need_closure && Z_TYPE(closure) != IS_OBJECT) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Journal entry for %s has no closure", ZSTR_VAL(target));
|
|
} else if (zend_string_equals_literal(kind, "function") || zend_string_equals_literal(kind, "internal")) {
|
|
id = cryo_op_redefine_function(target, &closure);
|
|
} else if (zend_string_equals_literal(kind, "method") && sep) {
|
|
id = cryo_op_redefine_method(cls, mname, &closure);
|
|
} else if (zend_string_equals_literal(kind, "add_function")) {
|
|
id = cryo_op_add_function(target, &closure);
|
|
} else if (zend_string_equals_literal(kind, "add_method") && sep) {
|
|
zval *fl = zend_hash_str_find(h, "flags", 5);
|
|
id = cryo_op_add_method(cls, mname, &closure, fl && Z_TYPE_P(fl) == IS_LONG ? Z_LVAL_P(fl) : 0);
|
|
} else if (zend_string_equals_literal(kind, "remove_function")) {
|
|
id = cryo_op_remove(NULL, target);
|
|
} else if (zend_string_equals_literal(kind, "remove_method") && sep) {
|
|
id = cryo_op_remove(cls, mname);
|
|
} else if (zend_string_equals_literal(kind, "rename_function")) {
|
|
zend_string *to = cryo_entry_str(h, "to");
|
|
if (to) id = cryo_op_rename_function(target, to);
|
|
else zend_throw_exception(cryo_ce_exception, "rename_function entry without \"to\"", 0);
|
|
} else if (zend_string_equals_literal(kind, "constant") || zend_string_equals_literal(kind, "class_constant")) {
|
|
zval *v = zend_hash_str_find(h, "value", 5);
|
|
zval nul;
|
|
ZVAL_NULL(&nul);
|
|
id = cryo_op_redefine_constant(target, v ? v : &nul);
|
|
} else if (zend_string_equals_literal(kind, "superglobal")) {
|
|
id = cryo_op_superglobal(target);
|
|
} else {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Unknown journal entry kind \"%s\"", ZSTR_VAL(kind));
|
|
}
|
|
if (cls) zend_string_release(cls);
|
|
if (mname) zend_string_release(mname);
|
|
zval_ptr_dtor(&closure);
|
|
return id;
|
|
}
|
|
|
|
zend_long cryo_engine_journal_replay(zval *in)
|
|
{
|
|
if (Z_TYPE_P(in) != IS_ARRAY) {
|
|
zend_throw_exception(cryo_ce_exception, "The journal must be an array", 0);
|
|
return 0;
|
|
}
|
|
zend_long n = 0;
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in), e) {
|
|
if (!cryo_engine_apply_entry(e)) {
|
|
break;
|
|
}
|
|
n++;
|
|
} ZEND_HASH_FOREACH_END();
|
|
return n;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* PHP class */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static bool cryo_engine_guard(void)
|
|
{
|
|
if (!cryo_engine_enabled()) {
|
|
zend_throw_exception(cryo_ce_exception, "Cryo\\Engine is disabled (cryo.engine=0)", 0);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, __construct)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, current)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
if (!cryo_engine_guard()) RETURN_THROWS();
|
|
if (Z_TYPE(cryo_engine.object) != IS_OBJECT) {
|
|
object_init_ex(&cryo_engine.object, cryo_ce_engine);
|
|
}
|
|
RETURN_COPY(&cryo_engine.object);
|
|
}
|
|
|
|
#define CRYO_ENGINE_RETURN_ID(expr) do { \
|
|
if (!cryo_engine_guard()) RETURN_THROWS(); \
|
|
zend_long _id = (expr); \
|
|
if (!_id) RETURN_THROWS(); \
|
|
RETURN_LONG(_id); \
|
|
} while (0)
|
|
|
|
ZEND_METHOD(Cryo_Engine, redefineFunction)
|
|
{
|
|
zend_string *name; zval *body;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_STR(name)
|
|
Z_PARAM_OBJECT_OF_CLASS(body, zend_ce_closure)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_redefine_function(name, body));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, addFunction)
|
|
{
|
|
zend_string *name; zval *body;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_STR(name)
|
|
Z_PARAM_OBJECT_OF_CLASS(body, zend_ce_closure)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_add_function(name, body));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, removeFunction)
|
|
{
|
|
zend_string *name;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_STR(name)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_remove(NULL, name));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, renameFunction)
|
|
{
|
|
zend_string *from, *to;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_STR(from)
|
|
Z_PARAM_STR(to)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_rename_function(from, to));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, redefineMethod)
|
|
{
|
|
zend_string *cls, *method; zval *body;
|
|
ZEND_PARSE_PARAMETERS_START(3, 3)
|
|
Z_PARAM_STR(cls)
|
|
Z_PARAM_STR(method)
|
|
Z_PARAM_OBJECT_OF_CLASS(body, zend_ce_closure)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_redefine_method(cls, method, body));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, addMethod)
|
|
{
|
|
zend_string *cls, *method; zval *body; zend_long flags = 1;
|
|
ZEND_PARSE_PARAMETERS_START(3, 4)
|
|
Z_PARAM_STR(cls)
|
|
Z_PARAM_STR(method)
|
|
Z_PARAM_OBJECT_OF_CLASS(body, zend_ce_closure)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG(flags)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_add_method(cls, method, body, flags));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, removeMethod)
|
|
{
|
|
zend_string *cls, *method;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_STR(cls)
|
|
Z_PARAM_STR(method)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_remove(cls, method));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, redefineConstant)
|
|
{
|
|
zend_string *name; zval *value;
|
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
|
Z_PARAM_STR(name)
|
|
Z_PARAM_ZVAL(value)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_redefine_constant(name, value));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, registerSuperglobal)
|
|
{
|
|
zend_string *name;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_STR(name)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
CRYO_ENGINE_RETURN_ID(cryo_op_superglobal(name));
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, journal)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
array_init(return_value);
|
|
for (uint32_t i = 0; i < cryo_engine.count; i++) {
|
|
zval e;
|
|
cryo_change_describe(cryo_engine.list[i], &e, false);
|
|
add_next_index_zval(return_value, &e);
|
|
}
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, revert)
|
|
{
|
|
zend_long id = 0; bool id_null = true;
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
Z_PARAM_OPTIONAL
|
|
Z_PARAM_LONG_OR_NULL(id, id_null)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
if (id_null) {
|
|
for (uint32_t i = 0; i < cryo_engine.count; i++) {
|
|
cryo_change *ch = cryo_engine.list[i];
|
|
if (ch->func && ch->in_place) {
|
|
const char *busy = cryo_func_busy(ch->func);
|
|
if (busy) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot revert %s: %s", ZSTR_VAL(ch->target), busy);
|
|
RETURN_THROWS();
|
|
}
|
|
}
|
|
}
|
|
RETURN_LONG(cryo_engine_revert_from(0));
|
|
}
|
|
int n = cryo_engine_revert_id(id);
|
|
if (EG(exception)) RETURN_THROWS();
|
|
if (n < 0) {
|
|
zend_throw_exception_ex(cryo_ce_exception, 0, "No active change #" ZEND_LONG_FMT, id);
|
|
RETURN_THROWS();
|
|
}
|
|
RETURN_LONG(n);
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, exportJournal)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
cryo_engine_journal_export(return_value);
|
|
}
|
|
|
|
ZEND_METHOD(Cryo_Engine, replayJournal)
|
|
{
|
|
zval *journal;
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
Z_PARAM_ARRAY(journal)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
if (!cryo_engine_guard()) RETURN_THROWS();
|
|
zend_long n = cryo_engine_journal_replay(journal);
|
|
if (EG(exception)) RETURN_THROWS();
|
|
RETURN_LONG(n);
|
|
}
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_engine_construct, 0, 0, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_current, 0, 0, IS_STATIC, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_name_body, 0, 2, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
|
|
ZEND_ARG_OBJ_INFO(0, body, Closure, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_name, 0, 1, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_rename, 0, 2, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
|
|
ZEND_ARG_TYPE_INFO(0, newName, IS_STRING, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_method_body, 0, 3, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, class, IS_STRING, 0)
|
|
ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0)
|
|
ZEND_ARG_OBJ_INFO(0, body, Closure, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_add_method, 0, 3, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, class, IS_STRING, 0)
|
|
ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0)
|
|
ZEND_ARG_OBJ_INFO(0, body, Closure, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "ReflectionMethod::IS_PUBLIC")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_method, 0, 2, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, class, IS_STRING, 0)
|
|
ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_constant, 0, 2, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
|
|
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_array, 0, 0, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_revert, 0, 0, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, id, IS_LONG, 1, "null")
|
|
ZEND_END_ARG_INFO()
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_engine_replay, 0, 1, IS_LONG, 0)
|
|
ZEND_ARG_TYPE_INFO(0, journal, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
static const zend_function_entry cryo_engine_methods[] = {
|
|
ZEND_ME(Cryo_Engine, __construct, arginfo_engine_construct, ZEND_ACC_PRIVATE)
|
|
ZEND_ME(Cryo_Engine, current, arginfo_engine_current, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
|
|
ZEND_ME(Cryo_Engine, redefineFunction, arginfo_engine_name_body, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, addFunction, arginfo_engine_name_body, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, removeFunction, arginfo_engine_name, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, renameFunction, arginfo_engine_rename, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, redefineMethod, arginfo_engine_method_body, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, addMethod, arginfo_engine_add_method, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, removeMethod, arginfo_engine_method, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, redefineConstant, arginfo_engine_constant, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, registerSuperglobal, arginfo_engine_name, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, journal, arginfo_engine_array, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, revert, arginfo_engine_revert, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, exportJournal, arginfo_engine_array, ZEND_ACC_PUBLIC)
|
|
ZEND_ME(Cryo_Engine, replayJournal, arginfo_engine_replay, ZEND_ACC_PUBLIC)
|
|
ZEND_FE_END
|
|
};
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Module hooks */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
void cryo_engine_minit(void)
|
|
{
|
|
zend_class_entry ce;
|
|
INIT_CLASS_ENTRY(ce, "Cryo\\Engine", cryo_engine_methods);
|
|
cryo_ce_engine = zend_register_internal_class(&ce);
|
|
cryo_ce_engine->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NOT_SERIALIZABLE;
|
|
memcpy(&cryo_engine_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
cryo_engine_handlers.clone_obj = NULL;
|
|
cryo_ce_engine->default_object_handlers = &cryo_engine_handlers;
|
|
|
|
memset(&cryo_engine, 0, sizeof(cryo_engine));
|
|
ZVAL_UNDEF(&cryo_engine.object);
|
|
cryo_engine.next_id = 1;
|
|
|
|
/* no INIT_FCALL handler: call sites are patched when a body is swapped (cryo_patch_call_sites) */
|
|
(void) cryo_init_fcall_handler;
|
|
}
|
|
|
|
void cryo_engine_mshutdown(void)
|
|
{
|
|
if (cryo_engine.installed) {
|
|
zend_set_user_opcode_handler(ZEND_INIT_FCALL, cryo_prev_init_fcall);
|
|
cryo_engine.installed = false;
|
|
}
|
|
}
|
|
|
|
/* Undo everything before the executor destroys functions, classes, constants. */
|
|
void cryo_engine_rshutdown(void)
|
|
{
|
|
cryo_engine_revert_from(0);
|
|
if (cryo_engine.list) {
|
|
efree(cryo_engine.list);
|
|
cryo_engine.list = NULL;
|
|
}
|
|
cryo_engine.cap = 0;
|
|
zval_ptr_dtor(&cryo_engine.object);
|
|
ZVAL_UNDEF(&cryo_engine.object);
|
|
if (cryo_engine.compiler_opts_saved) {
|
|
CG(compiler_options) = cryo_engine.compiler_opts;
|
|
cryo_engine.compiler_opts_saved = false;
|
|
}
|
|
/* cryo_engine.dirty stays set: call sites compiled while a body was
|
|
* swapped may still run (session handlers, other RSHUTDOWNs). */
|
|
}
|
|
|
|
uint32_t cryo_engine_change_count(void)
|
|
{
|
|
return cryo_engine.count;
|
|
}
|