- "Freeze here" stops the game loop, Application::run() freezes every value reachable from the globals and statics (cryo_freeze include globals + statics: the application, engine, scenes, loop and timers, the game's services) and runs the loop again; JUMPHP_CRYO_THAW thaws it in Application::boot() before anything is built, which returns the thawed application: the game goes on exactly where it was - native parts made again at the thaw: window and SDL renderer opened by the new process, textures from their recipe (Sdl::$recipes: file or picture; render targets blank, redrawn), fonts, sounds and the music playing, gamepads (cryo_register_resource + cryo_on_thaw / replaceClass); files signed with a key of the project - the scene save point stays next to it, used when the whole program can not be resumed (code changed, OpenGL) - SceneManager params by class (no WeakMap), the container's compiler and private-method closures out of the freeze - ext-cryo a9c891d (php-cryo): typed properties holding a frozen value (closure, resource, __wakeup object, excluded object) come back; DESKTOP_REV 11 - IDE: each save point says "whole program" / "scene only" (why); docs/ide.md, docs/cryo.md Co-Authored-By: Claude Opus 5.5 <[email protected]>
962 lines
37 KiB
C
962 lines
37 KiB
C
/*
|
|
* cryo_walk.c — what the PHP serializer cannot carry, around cryo_freeze():
|
|
*
|
|
* freeze: before serializing, walk the captured values and swap IN PLACE
|
|
* closures, resources, internal non-serializable objects, CryoAware
|
|
* objects, #[CryoTransient] properties/classes and excluded classes for
|
|
* Cryo\Frozen markers (or null). Every swap is undone right after the
|
|
* serialization, so the running script never sees them.
|
|
* thaw: after unserializing, walk again and turn markers back into closures
|
|
* (found by file and lines, variables rebound), objects (__cryoThaw),
|
|
* resources (registered or built-in handlers) or CryoPending values.
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
|
|
#include "php.h"
|
|
#include "php_cryo.h"
|
|
#include "zend_exceptions.h"
|
|
#include "zend_closures.h"
|
|
#include "zend_generators.h"
|
|
#include "zend_attributes.h"
|
|
#include "zend_interfaces.h"
|
|
#include "zend_smart_str.h"
|
|
|
|
zend_class_entry *cryo_ce_frozen; /* Cryo\Frozen: marker (kind, data) */
|
|
zend_class_entry *cryo_ce_pending; /* CryoPending: a value nobody reopened */
|
|
zend_class_entry *cryo_ce_aware; /* interface CryoAware */
|
|
zend_class_entry *cryo_ce_transient; /* #[CryoTransient] */
|
|
|
|
HashTable cryo_resource_handlers; /* lower type => [describe, reopen] */
|
|
|
|
#define CRYO_WALK_MAX_DEPTH 256
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Markers */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static void cryo_make_marker(zval *out, const char *kind, zval *data)
|
|
{
|
|
object_init_ex(out, cryo_ce_frozen);
|
|
zend_update_property_string(cryo_ce_frozen, Z_OBJ_P(out), "kind", 4, kind);
|
|
zend_update_property(cryo_ce_frozen, Z_OBJ_P(out), "data", 4, data);
|
|
}
|
|
|
|
static zval *cryo_marker_data(zval *marker)
|
|
{
|
|
zval rv;
|
|
return zend_read_property(cryo_ce_frozen, Z_OBJ_P(marker), "data", 4, 1, &rv);
|
|
}
|
|
|
|
static const char *cryo_marker_kind(zval *marker)
|
|
{
|
|
zval rv, *k = zend_read_property(cryo_ce_frozen, Z_OBJ_P(marker), "kind", 4, 1, &rv);
|
|
return Z_TYPE_P(k) == IS_STRING ? Z_STRVAL_P(k) : "";
|
|
}
|
|
|
|
static bool cryo_has_attr(HashTable *attrs, const char *lcname)
|
|
{
|
|
return attrs && zend_get_attribute_str(attrs, lcname, strlen(lcname)) != NULL;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Freeze side */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
void cryo_walk_init(cryo_walk *w, HashTable *opts)
|
|
{
|
|
memset(w, 0, sizeof(*w));
|
|
ZVAL_UNDEF(&w->typed);
|
|
zend_hash_init(&w->seen, 64, NULL, NULL, 0);
|
|
zend_hash_init(&w->done, 16, NULL, ZVAL_PTR_DTOR, 0);
|
|
w->on_resource = CRYO_RES_DESCRIPTOR;
|
|
zval *v;
|
|
if (opts && (v = zend_hash_str_find(opts, "on_resource", 11)) && Z_TYPE_P(v) == IS_STRING) {
|
|
if (zend_string_equals_literal(Z_STR_P(v), "null")) w->on_resource = CRYO_RES_NULL;
|
|
else if (zend_string_equals_literal(Z_STR_P(v), "fail")) w->on_resource = CRYO_RES_FAIL;
|
|
}
|
|
if (opts && (v = zend_hash_str_find(opts, "strict", 6))) {
|
|
w->strict = zend_is_true(v);
|
|
if (w->strict) w->on_resource = CRYO_RES_FAIL;
|
|
}
|
|
if (opts && (v = zend_hash_str_find(opts, "exclude", 7)) && Z_TYPE_P(v) == IS_ARRAY) {
|
|
w->exclude = Z_ARRVAL_P(v);
|
|
}
|
|
}
|
|
|
|
/* record a swap: the slot gets `with`, its old value is kept for cryo_walk_undo() */
|
|
static void cryo_swap(cryo_walk *w, zval *slot, zval *with)
|
|
{
|
|
if (w->nswaps == w->capswaps) {
|
|
w->capswaps = w->capswaps ? w->capswaps * 2 : 32;
|
|
w->swaps = erealloc(w->swaps, w->capswaps * sizeof(*w->swaps));
|
|
}
|
|
w->swaps[w->nswaps].slot = slot;
|
|
ZVAL_COPY_VALUE(&w->swaps[w->nswaps].old, slot);
|
|
w->nswaps++;
|
|
ZVAL_COPY_VALUE(slot, with);
|
|
}
|
|
|
|
void cryo_walk_undo(cryo_walk *w)
|
|
{
|
|
for (uint32_t i = w->nswaps; i-- > 0;) {
|
|
zval_ptr_dtor(w->swaps[i].slot);
|
|
ZVAL_COPY_VALUE(w->swaps[i].slot, &w->swaps[i].old);
|
|
}
|
|
if (Z_TYPE(w->typed) == IS_ARRAY) zval_ptr_dtor(&w->typed); /* a freeze that failed before using them */
|
|
ZVAL_UNDEF(&w->typed);
|
|
if (w->swaps) efree(w->swaps);
|
|
w->swaps = NULL;
|
|
w->nswaps = w->capswaps = 0;
|
|
zend_hash_destroy(&w->seen);
|
|
zend_hash_destroy(&w->done);
|
|
}
|
|
|
|
static bool cryo_class_excluded(cryo_walk *w, zend_class_entry *ce)
|
|
{
|
|
if (cryo_has_attr(ce->attributes, "cryotransient")) return true;
|
|
if (!w->exclude) return false;
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(w->exclude, e) {
|
|
if (Z_TYPE_P(e) != IS_STRING) continue;
|
|
for (zend_class_entry *c = ce; c; c = c->parent) {
|
|
if (zend_string_equals_ci(c->name, Z_STR_P(e))) return true;
|
|
}
|
|
} ZEND_HASH_FOREACH_END();
|
|
return false;
|
|
}
|
|
|
|
bool cryo_walk_var_excluded(cryo_walk *w, zend_string *name)
|
|
{
|
|
if (!w->exclude) return false;
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(w->exclude, e) {
|
|
if (Z_TYPE_P(e) == IS_STRING && zend_string_equals(Z_STR_P(e), name)) return true;
|
|
} ZEND_HASH_FOREACH_END();
|
|
return false;
|
|
}
|
|
|
|
static void cryo_walk_zval(cryo_walk *w, zval *slot, uint32_t depth);
|
|
|
|
/* user classes whose (de)serialization runs user code */
|
|
static bool cryo_needs_raw(zend_class_entry *ce)
|
|
{
|
|
if (ce->type != ZEND_USER_CLASS || ce->create_object || (ce->ce_flags & ZEND_ACC_ENUM)) return false;
|
|
if (cryo_ce_aware && instanceof_function(ce, cryo_ce_aware)) return false;
|
|
return ce->__serialize || ce->__unserialize || ce->serialize || ce->unserialize
|
|
|| zend_hash_str_exists(&ce->function_table, "__wakeup", 8)
|
|
|| zend_hash_str_exists(&ce->function_table, "__sleep", 7);
|
|
}
|
|
|
|
/* Closure → marker. Returns FAILURE if it cannot be described. */
|
|
static zend_result cryo_closure_marker(cryo_walk *w, zval *zv, zval *out)
|
|
{
|
|
zend_object *obj = Z_OBJ_P(zv);
|
|
const zend_function *f = zend_get_closure_method_def(obj);
|
|
zval data, *this_ptr = zend_get_closure_this_ptr(zv);
|
|
array_init(&data);
|
|
if (f->type != ZEND_USER_FUNCTION || (f->common.fn_flags & ZEND_ACC_FAKE_CLOSURE)) {
|
|
/* strlen(...), $obj->m(...), Closure::fromCallable('f') */
|
|
add_assoc_str(&data, "callable", zend_string_copy(f->common.function_name));
|
|
if (f->common.scope) add_assoc_str(&data, "class", zend_string_copy(f->common.scope->name));
|
|
} else {
|
|
add_assoc_str(&data, "file", zend_string_copy(f->op_array.filename));
|
|
add_assoc_long(&data, "line", f->op_array.line_start);
|
|
add_assoc_long(&data, "end", f->op_array.line_end);
|
|
add_assoc_long(&data, "ops", f->op_array.last); /* tells apart closures sharing the same lines */
|
|
if (f->common.scope) add_assoc_str(&data, "class", zend_string_copy(f->common.scope->name));
|
|
HashTable *ht = ZEND_MAP_PTR_GET(f->op_array.static_variables_ptr);
|
|
if (!ht) ht = f->op_array.static_variables;
|
|
zval vars;
|
|
array_init(&vars);
|
|
if (ht) {
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, k, v) {
|
|
if (!k || Z_TYPE_P(v) == IS_UNDEF) continue;
|
|
Z_TRY_ADDREF_P(v);
|
|
zend_hash_update(Z_ARRVAL(vars), k, v); /* references stay references */
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
add_assoc_zval(&data, "vars", &vars);
|
|
}
|
|
if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT) {
|
|
Z_ADDREF_P(this_ptr);
|
|
add_assoc_zval(&data, "this", this_ptr);
|
|
}
|
|
cryo_make_marker(out, "closure", &data);
|
|
zval_ptr_dtor(&data);
|
|
(void) w;
|
|
return SUCCESS;
|
|
}
|
|
|
|
/* Resource or non-serializable internal object → marker, null, or failure. */
|
|
static zend_result cryo_resource_marker(cryo_walk *w, zval *zv, zval *out, uint32_t depth)
|
|
{
|
|
zend_string *type;
|
|
if (Z_TYPE_P(zv) == IS_RESOURCE) {
|
|
const char *t = zend_rsrc_list_get_rsrc_type(Z_RES_P(zv));
|
|
type = zend_string_init(t ? t : "unknown", strlen(t ? t : "unknown"), 0);
|
|
} else {
|
|
type = zend_string_copy(Z_OBJCE_P(zv)->name);
|
|
}
|
|
zend_string *lc = zend_string_tolower(type);
|
|
zval *h = zend_hash_find(&cryo_resource_handlers, lc);
|
|
zend_string_release(lc);
|
|
zval desc;
|
|
ZVAL_UNDEF(&desc);
|
|
|
|
if (h && Z_TYPE_P(h) == IS_ARRAY) {
|
|
zval *describe = zend_hash_index_find(Z_ARRVAL_P(h), 0);
|
|
if (describe && call_user_function(NULL, NULL, describe, &desc, 1, zv) == FAILURE) {
|
|
ZVAL_UNDEF(&desc);
|
|
}
|
|
if (EG(exception)) { zend_string_release(type); return FAILURE; }
|
|
} else if (Z_TYPE_P(zv) == IS_RESOURCE && cryo_stream_describe(zv, &desc) == SUCCESS) {
|
|
/* built-in: plain files, php://std*, sockets */
|
|
}
|
|
|
|
if (Z_TYPE(desc) == IS_UNDEF) {
|
|
if (w->on_resource == CRYO_RES_FAIL) {
|
|
zend_throw_exception_ex(cryo_ce_freeze_exception, 0,
|
|
"Cannot freeze a value of type %s (register a handler with cryo_register_resource())", ZSTR_VAL(type));
|
|
zend_string_release(type);
|
|
return FAILURE;
|
|
}
|
|
if (w->on_resource == CRYO_RES_NULL) {
|
|
ZVAL_NULL(out);
|
|
zend_string_release(type);
|
|
return SUCCESS;
|
|
}
|
|
array_init(&desc);
|
|
}
|
|
cryo_walk_zval(w, &desc, depth + 1); /* a description may hold closures */
|
|
zval data;
|
|
array_init(&data);
|
|
add_assoc_str(&data, "type", type);
|
|
add_assoc_zval(&data, "desc", &desc);
|
|
cryo_make_marker(out, "resource", &data);
|
|
zval_ptr_dtor(&data);
|
|
return SUCCESS;
|
|
}
|
|
|
|
static void cryo_walk_object(cryo_walk *w, zval *slot, uint32_t depth)
|
|
{
|
|
zend_object *obj = Z_OBJ_P(slot);
|
|
zend_class_entry *ce = obj->ce;
|
|
zval *done = zend_hash_index_find(&w->done, (zend_ulong)(uintptr_t) obj);
|
|
if (done) { /* same object seen before: same marker */
|
|
zval m;
|
|
ZVAL_COPY(&m, done);
|
|
cryo_swap(w, slot, &m);
|
|
return;
|
|
}
|
|
zval marker;
|
|
ZVAL_UNDEF(&marker);
|
|
|
|
if (cryo_class_excluded(w, ce)) {
|
|
ZVAL_NULL(&marker);
|
|
} else if (ce == zend_ce_closure) {
|
|
if (cryo_closure_marker(w, slot, &marker) == FAILURE) return;
|
|
} else if (ce == zend_ce_generator) {
|
|
zval gd;
|
|
if (cryo_generator_describe(obj, &gd) == SUCCESS) {
|
|
cryo_make_marker(&marker, "generator", &gd);
|
|
zval_ptr_dtor(&gd);
|
|
zval keep;
|
|
ZVAL_COPY(&keep, &marker);
|
|
zend_hash_index_update(&w->done, (zend_ulong)(uintptr_t) obj, &keep);
|
|
cryo_walk_zval(w, cryo_marker_data(&marker), depth + 1);
|
|
cryo_swap(w, slot, &marker);
|
|
return;
|
|
}
|
|
if (w->strict) {
|
|
zend_throw_exception(cryo_ce_freeze_exception, "Cannot freeze this Generator (running, finished, in a yield from, or a closure) in strict mode", 0);
|
|
return;
|
|
}
|
|
php_error_docref(NULL, E_WARNING, "This Generator cannot be frozen (running, finished, in a yield from, or a closure): it becomes null");
|
|
ZVAL_NULL(&marker);
|
|
} else if (cryo_needs_raw(ce)) {
|
|
/* the spec: objects come back without constructor nor __wakeup / __unserialize: their
|
|
* properties are carried as they are (and __sleep / __serialize are not called either) */
|
|
zval m, props, dyn;
|
|
array_init(&m);
|
|
add_assoc_str(&m, "class", zend_string_copy(ce->name));
|
|
array_init(&props);
|
|
array_init(&dyn);
|
|
add_assoc_zval(&m, "props", &props);
|
|
add_assoc_zval(&m, "dyn", &dyn);
|
|
cryo_make_marker(&marker, "raw", &m);
|
|
zval_ptr_dtor(&m);
|
|
zval keep;
|
|
ZVAL_COPY(&keep, &marker);
|
|
zend_hash_index_update(&w->done, (zend_ulong)(uintptr_t) obj, &keep); /* before the properties: cycles */
|
|
zval *md = cryo_marker_data(&marker);
|
|
zval *mprops = zend_hash_str_find(Z_ARRVAL_P(md), "props", 5), *mdyn = zend_hash_str_find(Z_ARRVAL_P(md), "dyn", 3);
|
|
SEPARATE_ARRAY(mprops);
|
|
SEPARATE_ARRAY(mdyn);
|
|
for (int i = 0; i < ce->default_properties_count; i++) {
|
|
zend_property_info *pi = ce->properties_info_table ? ce->properties_info_table[i] : NULL;
|
|
zval *pv = &obj->properties_table[i];
|
|
if (!pi || Z_TYPE_P(pv) == IS_UNDEF || cryo_has_attr(pi->attributes, "cryotransient")) continue;
|
|
zval entry, v;
|
|
array_init(&entry);
|
|
add_next_index_str(&entry, zend_string_copy(pi->ce->name));
|
|
add_next_index_str(&entry, zend_string_copy(pi->name));
|
|
ZVAL_COPY(&v, pv);
|
|
add_next_index_zval(&entry, &v);
|
|
add_next_index_zval(mprops, &entry);
|
|
}
|
|
if (obj->properties) {
|
|
zend_string *k; zval *pv;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(obj->properties, k, pv) {
|
|
if (!k || Z_TYPE_P(pv) == IS_INDIRECT) continue;
|
|
Z_TRY_ADDREF_P(pv);
|
|
zend_hash_update(Z_ARRVAL_P(mdyn), k, pv);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
cryo_walk_zval(w, md, depth + 1);
|
|
zval m2;
|
|
ZVAL_COPY(&m2, &marker);
|
|
zval_ptr_dtor(&marker);
|
|
cryo_swap(w, slot, &m2);
|
|
return;
|
|
} else if (cryo_ce_aware && instanceof_function(ce, cryo_ce_aware)) {
|
|
zval data, cls;
|
|
zend_call_method_with_0_params(obj, ce, NULL, "__cryofreeze", &data);
|
|
if (EG(exception)) return;
|
|
if (Z_TYPE(data) != IS_ARRAY) { zval_ptr_dtor(&data); array_init(&data); }
|
|
zval m;
|
|
array_init(&m);
|
|
ZVAL_STR_COPY(&cls, ce->name);
|
|
add_assoc_zval(&m, "class", &cls);
|
|
add_assoc_zval(&m, "data", &data);
|
|
cryo_make_marker(&marker, "aware", &m);
|
|
zval_ptr_dtor(&m);
|
|
zend_hash_index_add_new(&w->seen, (zend_ulong)(uintptr_t) obj, &EG(uninitialized_zval));
|
|
cryo_walk_zval(w, cryo_marker_data(&marker), depth + 1);
|
|
} else if (ce->type == ZEND_INTERNAL_CLASS && (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE)) {
|
|
if (cryo_resource_marker(w, slot, &marker, depth) == FAILURE) return;
|
|
}
|
|
|
|
if (Z_TYPE(marker) != IS_UNDEF) {
|
|
zval keep;
|
|
ZVAL_COPY(&keep, &marker);
|
|
zend_hash_index_update(&w->done, (zend_ulong)(uintptr_t) obj, &keep);
|
|
/* walk inside the marker (closure vars, $this...) */
|
|
if (Z_TYPE(marker) == IS_OBJECT && ce == zend_ce_closure) {
|
|
cryo_walk_zval(w, cryo_marker_data(&marker), depth + 1);
|
|
}
|
|
cryo_swap(w, slot, &marker);
|
|
return;
|
|
}
|
|
|
|
/* a plain object: walk its properties */
|
|
if (zend_hash_index_exists(&w->seen, (zend_ulong)(uintptr_t) obj)) return;
|
|
zend_hash_index_add_new(&w->seen, (zend_ulong)(uintptr_t) obj, &EG(uninitialized_zval));
|
|
if (ce->default_properties_count) {
|
|
zend_property_info *pi;
|
|
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, pi) {
|
|
if (pi->flags & ZEND_ACC_STATIC) continue;
|
|
zval *p = OBJ_PROP(obj, pi->offset);
|
|
if (Z_TYPE_P(p) == IS_UNDEF) continue;
|
|
if (cryo_has_attr(pi->attributes, "cryotransient")) {
|
|
zval n;
|
|
if (ZEND_TYPE_IS_SET(pi->type) && !ZEND_TYPE_ALLOW_NULL(pi->type)) ZVAL_UNDEF(&n); else ZVAL_NULL(&n);
|
|
cryo_swap(w, p, &n);
|
|
continue;
|
|
}
|
|
cryo_walk_zval(w, p, depth + 1);
|
|
/* a typed property can not hold a marker (nor the null of an excluded value) when the values are
|
|
* unserialized: the marker is kept aside with its object, the property left uninitialized, and
|
|
* cryo_walk_typed_restore() puts the thawed value back into it */
|
|
if (ZEND_TYPE_IS_SET(pi->type) && Z_TYPE_P(p) != IS_REFERENCE) {
|
|
if (Z_TYPE_P(p) == IS_OBJECT && Z_OBJCE_P(p) == cryo_ce_frozen && !(ZEND_TYPE_PURE_MASK(pi->type) & MAY_BE_OBJECT)) {
|
|
zval entry, o, m, undef;
|
|
array_init(&entry);
|
|
ZVAL_OBJ_COPY(&o, obj);
|
|
add_next_index_zval(&entry, &o);
|
|
add_next_index_str(&entry, zend_string_copy(pi->ce->name));
|
|
add_next_index_str(&entry, zend_string_copy(pi->name));
|
|
ZVAL_COPY(&m, p);
|
|
add_next_index_zval(&entry, &m);
|
|
if (Z_TYPE(w->typed) != IS_ARRAY) array_init(&w->typed);
|
|
add_next_index_zval(&w->typed, &entry);
|
|
ZVAL_UNDEF(&undef);
|
|
cryo_swap(w, p, &undef);
|
|
} else if (Z_TYPE_P(p) == IS_NULL && !ZEND_TYPE_ALLOW_NULL(pi->type)) {
|
|
zval undef;
|
|
ZVAL_UNDEF(&undef);
|
|
cryo_swap(w, p, &undef);
|
|
}
|
|
}
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
if (obj->properties && (ce->type == ZEND_USER_CLASS || ce == zend_standard_class_def)) { /* + stdClass */
|
|
zval *p;
|
|
ZEND_HASH_FOREACH_VAL(obj->properties, p) {
|
|
if (Z_TYPE_P(p) == IS_INDIRECT) continue; /* declared: done above */
|
|
cryo_walk_zval(w, p, depth + 1);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
}
|
|
|
|
static void cryo_walk_zval(cryo_walk *w, zval *slot, uint32_t depth)
|
|
{
|
|
if (EG(exception) || depth > CRYO_WALK_MAX_DEPTH) return;
|
|
zval *v = slot;
|
|
if (Z_TYPE_P(v) == IS_REFERENCE) {
|
|
zend_reference *ref = Z_REF_P(v);
|
|
if (zend_hash_index_exists(&w->seen, (zend_ulong)(uintptr_t) ref)) return;
|
|
zend_hash_index_add_new(&w->seen, (zend_ulong)(uintptr_t) ref, &EG(uninitialized_zval));
|
|
v = Z_REFVAL_P(v);
|
|
}
|
|
switch (Z_TYPE_P(v)) {
|
|
case IS_ARRAY: {
|
|
zend_array *ht = Z_ARR_P(v);
|
|
if (GC_FLAGS(ht) & GC_IMMUTABLE) return;
|
|
if (zend_hash_index_exists(&w->seen, (zend_ulong)(uintptr_t) ht)) return;
|
|
zend_hash_index_add_new(&w->seen, (zend_ulong)(uintptr_t) ht, &EG(uninitialized_zval));
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(ht, e) {
|
|
if (Z_TYPE_P(e) == IS_INDIRECT) e = Z_INDIRECT_P(e);
|
|
cryo_walk_zval(w, e, depth + 1);
|
|
} ZEND_HASH_FOREACH_END();
|
|
return;
|
|
}
|
|
case IS_OBJECT:
|
|
cryo_walk_object(w, v, depth);
|
|
return;
|
|
case IS_RESOURCE: {
|
|
zval marker;
|
|
ZVAL_UNDEF(&marker);
|
|
if (cryo_resource_marker(w, v, &marker, depth) == SUCCESS) cryo_swap(w, v, &marker);
|
|
return;
|
|
}
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
void cryo_walk_freeze(cryo_walk *w, zval *root)
|
|
{
|
|
cryo_walk_zval(w, root, 0);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Thaw side */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static HashTable *cryo_scratch_files = NULL; /* file => op_array compiled only to find closures */
|
|
static uint32_t cryo_want_ops = 0; /* opcode count of the closure looked for (0: any) */
|
|
|
|
static zend_op_array *cryo_find_in(zend_op_array *op, zend_string *file, uint32_t line, uint32_t end, uint32_t depth)
|
|
{
|
|
if (!op || depth > 64) return NULL;
|
|
if ((op->fn_flags & ZEND_ACC_CLOSURE) && op->line_start == line && op->line_end == end
|
|
&& (!cryo_want_ops || op->last == cryo_want_ops)
|
|
&& op->filename && zend_string_equals(op->filename, file)) {
|
|
return op;
|
|
}
|
|
for (uint32_t i = 0; i < op->num_dynamic_func_defs; i++) {
|
|
zend_op_array *r = cryo_find_in(op->dynamic_func_defs[i], file, line, end, depth + 1);
|
|
if (r) return r;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static zend_op_array *cryo_find_closure(cryo_thaw_ctx *t, zend_string *file, uint32_t line, uint32_t end)
|
|
{
|
|
zend_op_array *r = t && t->main ? cryo_find_in(t->main, file, line, end, 0) : NULL;
|
|
if (r) return r;
|
|
zend_function *f;
|
|
ZEND_HASH_MAP_FOREACH_PTR(EG(function_table), f) {
|
|
if (f->type == ZEND_USER_FUNCTION && zend_string_equals(f->op_array.filename, file)
|
|
&& (r = cryo_find_in(&f->op_array, file, line, end, 0))) {
|
|
return r;
|
|
}
|
|
} ZEND_HASH_FOREACH_END();
|
|
zend_class_entry *ce;
|
|
ZEND_HASH_MAP_FOREACH_PTR(EG(class_table), ce) {
|
|
if (ce->type != ZEND_USER_CLASS || !ce->info.user.filename || !zend_string_equals(ce->info.user.filename, file)) continue;
|
|
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, f) {
|
|
if (f->type == ZEND_USER_FUNCTION && (r = cryo_find_in(&f->op_array, file, line, end, 0))) return r;
|
|
} ZEND_HASH_FOREACH_END();
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
/* a closure declared at the top level of an included file: compile it
|
|
* again into scratch tables (nothing gets declared twice), keep it */
|
|
if (!cryo_scratch_files) {
|
|
ALLOC_HASHTABLE(cryo_scratch_files);
|
|
zend_hash_init(cryo_scratch_files, 8, NULL, NULL, 0);
|
|
}
|
|
zend_op_array *top = zend_hash_find_ptr(cryo_scratch_files, file);
|
|
if (!top) {
|
|
HashTable *ft = CG(function_table), *ct = CG(class_table);
|
|
HashTable *sft = emalloc(sizeof(HashTable)), *sct = emalloc(sizeof(HashTable));
|
|
zend_hash_init(sft, 8, NULL, NULL, 0);
|
|
zend_hash_init(sct, 8, NULL, NULL, 0);
|
|
CG(function_table) = sft;
|
|
CG(class_table) = sct;
|
|
zend_file_handle fh;
|
|
zend_stream_init_filename_ex(&fh, file);
|
|
top = zend_compile_file(&fh, ZEND_INCLUDE);
|
|
zend_destroy_file_handle(&fh);
|
|
CG(function_table) = ft;
|
|
CG(class_table) = ct;
|
|
if (EG(exception)) zend_clear_exception();
|
|
if (!top) return NULL;
|
|
zend_hash_add_ptr(cryo_scratch_files, file, top);
|
|
}
|
|
return cryo_find_in(top, file, line, end, 0);
|
|
}
|
|
|
|
static void cryo_rebuild_closure(cryo_thaw_ctx *t, zval *data, zval *out)
|
|
{
|
|
zval *callable = zend_hash_str_find(Z_ARRVAL_P(data), "callable", 8);
|
|
zval *cls = zend_hash_str_find(Z_ARRVAL_P(data), "class", 5);
|
|
zval *this_ptr = zend_hash_str_find(Z_ARRVAL_P(data), "this", 4);
|
|
ZVAL_NULL(out);
|
|
if (callable && Z_TYPE_P(callable) == IS_STRING) {
|
|
zval fn, args[1];
|
|
if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT) {
|
|
array_init(&args[0]);
|
|
Z_ADDREF_P(this_ptr);
|
|
add_next_index_zval(&args[0], this_ptr);
|
|
add_next_index_str(&args[0], zend_string_copy(Z_STR_P(callable)));
|
|
} else if (cls && Z_TYPE_P(cls) == IS_STRING) {
|
|
ZVAL_STR(&args[0], zend_strpprintf(0, "%s::%s", Z_STRVAL_P(cls), Z_STRVAL_P(callable)));
|
|
} else {
|
|
ZVAL_STR_COPY(&args[0], Z_STR_P(callable));
|
|
}
|
|
ZVAL_STRING(&fn, "Closure::fromCallable");
|
|
call_user_function(NULL, NULL, &fn, out, 1, args);
|
|
zval_ptr_dtor(&fn);
|
|
zval_ptr_dtor(&args[0]);
|
|
return;
|
|
}
|
|
zval *file = zend_hash_str_find(Z_ARRVAL_P(data), "file", 4);
|
|
zval *line = zend_hash_str_find(Z_ARRVAL_P(data), "line", 4);
|
|
zval *end = zend_hash_str_find(Z_ARRVAL_P(data), "end", 3);
|
|
if (!file || !line || !end || Z_TYPE_P(file) != IS_STRING) return;
|
|
zval *ops = zend_hash_str_find(Z_ARRVAL_P(data), "ops", 3);
|
|
/* two closures on the same lines (fn () => new A(fn () => ...)): the one with the same opcode count,
|
|
* else (code compiled differently) the first one on those lines */
|
|
cryo_want_ops = ops && Z_TYPE_P(ops) == IS_LONG ? (uint32_t) Z_LVAL_P(ops) : 0;
|
|
zend_op_array *op = cryo_find_closure(t, Z_STR_P(file), (uint32_t) Z_LVAL_P(line), (uint32_t) Z_LVAL_P(end));
|
|
if (!op && cryo_want_ops) {
|
|
cryo_want_ops = 0;
|
|
op = cryo_find_closure(t, Z_STR_P(file), (uint32_t) Z_LVAL_P(line), (uint32_t) Z_LVAL_P(end));
|
|
}
|
|
cryo_want_ops = 0;
|
|
if (!op) {
|
|
php_error_docref(NULL, E_WARNING, "The closure of %s:%ld is gone: it becomes null", Z_STRVAL_P(file), (long) Z_LVAL_P(line));
|
|
return;
|
|
}
|
|
zend_class_entry *scope = cls && Z_TYPE_P(cls) == IS_STRING ? zend_lookup_class(Z_STR_P(cls)) : NULL;
|
|
zend_create_closure(out, (zend_function *) op, scope, scope,
|
|
this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT ? this_ptr : NULL);
|
|
zval *vars = zend_hash_str_find(Z_ARRVAL_P(data), "vars", 4);
|
|
if (vars && Z_TYPE_P(vars) == IS_ARRAY) {
|
|
zend_function *cf = (zend_function *) zend_get_closure_method_def(Z_OBJ_P(out));
|
|
HashTable *ht = ZEND_MAP_PTR_GET(cf->op_array.static_variables_ptr);
|
|
if (ht) {
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(vars), k, v) {
|
|
if (!k) continue;
|
|
Z_TRY_ADDREF_P(v);
|
|
zend_hash_update(ht, k, v);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void cryo_reopen_resource(cryo_thaw_ctx *t, zval *data, zval *out, const char *path)
|
|
{
|
|
zval *type = zend_hash_str_find(Z_ARRVAL_P(data), "type", 4);
|
|
zval *desc = zend_hash_str_find(Z_ARRVAL_P(data), "desc", 4);
|
|
ZVAL_UNDEF(out);
|
|
if (type && Z_TYPE_P(type) == IS_STRING) {
|
|
zend_string *lc = zend_string_tolower(Z_STR_P(type));
|
|
zval *h = zend_hash_find(&cryo_resource_handlers, lc);
|
|
zend_string_release(lc);
|
|
zval *reopen = h && Z_TYPE_P(h) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(h), 1) : NULL;
|
|
if (reopen && desc) {
|
|
if (call_user_function(NULL, NULL, reopen, out, 1, desc) == FAILURE) ZVAL_UNDEF(out);
|
|
if (EG(exception)) { zval_ptr_dtor(out); ZVAL_NULL(out); return; }
|
|
} else if (desc && Z_TYPE_P(desc) == IS_ARRAY) {
|
|
cryo_stream_reopen(desc, out);
|
|
}
|
|
}
|
|
if (Z_TYPE_P(out) == IS_UNDEF) {
|
|
object_init_ex(out, cryo_ce_pending);
|
|
zend_update_property_string(cryo_ce_pending, Z_OBJ_P(out), "path", 4, path);
|
|
if (type) zend_update_property(cryo_ce_pending, Z_OBJ_P(out), "type", 4, type);
|
|
if (desc) zend_update_property(cryo_ce_pending, Z_OBJ_P(out), "desc", 4, desc);
|
|
t->pending++;
|
|
}
|
|
}
|
|
|
|
static void cryo_thaw_zval(cryo_thaw_ctx *t, zval *slot, uint32_t depth, smart_str *path, int pass);
|
|
|
|
static void cryo_path_push(smart_str *path, size_t *mark, const char *sep, zend_string *key, zend_ulong idx)
|
|
{
|
|
*mark = path->s ? ZSTR_LEN(path->s) : 0;
|
|
if (*mark > 400) return;
|
|
smart_str_appends(path, sep);
|
|
/* private / protected property names are mangled ("\0Class\0name"): paths use the bare name */
|
|
if (key && ZSTR_LEN(key) && ZSTR_VAL(key)[0] == '\0') smart_str_appends(path, zend_get_unmangled_property_name(key));
|
|
else if (key) smart_str_append(path, key);
|
|
else smart_str_append_unsigned(path, idx);
|
|
}
|
|
|
|
static void cryo_path_pop(smart_str *path, size_t mark)
|
|
{
|
|
if (path->s) ZSTR_LEN(path->s) = mark;
|
|
}
|
|
|
|
static void cryo_thaw_marker(cryo_thaw_ctx *t, zval *slot, uint32_t depth, smart_str *path, int pass)
|
|
{
|
|
zend_object *mobj = Z_OBJ_P(slot);
|
|
zval *done = zend_hash_index_find(&t->done, (zend_ulong)(uintptr_t) mobj);
|
|
if (done) {
|
|
zval v;
|
|
ZVAL_COPY(&v, done);
|
|
zval_ptr_dtor(slot);
|
|
ZVAL_COPY_VALUE(slot, &v);
|
|
return;
|
|
}
|
|
const char *kind = cryo_marker_kind(slot);
|
|
zval *data = cryo_marker_data(slot);
|
|
bool mine = (pass == 1) == (strcmp(kind, "closure") == 0);
|
|
if (!mine || Z_TYPE_P(data) != IS_ARRAY) {
|
|
if (Z_TYPE_P(data) == IS_ARRAY || Z_TYPE_P(data) == IS_OBJECT) cryo_thaw_zval(t, data, depth + 1, path, pass);
|
|
return;
|
|
}
|
|
if (strcmp(kind, "raw") != 0) cryo_thaw_zval(t, data, depth + 1, path, pass); /* leaves first (raw: after, for cycles) */
|
|
zval result;
|
|
ZVAL_NULL(&result);
|
|
if (strcmp(kind, "closure") == 0) {
|
|
cryo_rebuild_closure(t, data, &result);
|
|
} else if (strcmp(kind, "aware") == 0) {
|
|
zval *cls = zend_hash_str_find(Z_ARRVAL_P(data), "class", 5);
|
|
zval *d = zend_hash_str_find(Z_ARRVAL_P(data), "data", 4);
|
|
zend_class_entry *ce = cls && Z_TYPE_P(cls) == IS_STRING ? zend_lookup_class(Z_STR_P(cls)) : NULL;
|
|
if (ce && object_init_ex(&result, ce) == SUCCESS) { /* no constructor */
|
|
zval ret;
|
|
zend_call_method_with_1_params(Z_OBJ(result), ce, NULL, "__cryothaw", &ret, d ? d : &EG(uninitialized_zval));
|
|
zval_ptr_dtor(&ret);
|
|
}
|
|
} else if (strcmp(kind, "generator") == 0) {
|
|
if (cryo_generator_rebuild(data, &result) == FAILURE) {
|
|
php_error_docref(NULL, E_WARNING, "A frozen Generator could not be rebuilt (its function changed): it becomes null");
|
|
ZVAL_NULL(&result);
|
|
}
|
|
} else if (strcmp(kind, "raw") == 0) {
|
|
zval *cls = zend_hash_str_find(Z_ARRVAL_P(data), "class", 5);
|
|
zend_class_entry *ce = cls && Z_TYPE_P(cls) == IS_STRING ? zend_lookup_class(Z_STR_P(cls)) : NULL;
|
|
if (ce && object_init_ex(&result, ce) == SUCCESS) { /* no constructor, no __wakeup */
|
|
zval keep;
|
|
ZVAL_COPY(&keep, &result);
|
|
zend_hash_index_update(&t->done, (zend_ulong)(uintptr_t) mobj, &keep); /* cycles find it */
|
|
cryo_thaw_zval(t, data, depth + 1, path, pass);
|
|
zend_object *obj = Z_OBJ(result);
|
|
zval *props = zend_hash_str_find(Z_ARRVAL_P(data), "props", 5), *e;
|
|
if (props && Z_TYPE_P(props) == IS_ARRAY) {
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(props), e) {
|
|
zval *dc = zend_hash_index_find(Z_ARRVAL_P(e), 0), *pn = zend_hash_index_find(Z_ARRVAL_P(e), 1),
|
|
*pv = zend_hash_index_find(Z_ARRVAL_P(e), 2);
|
|
if (!dc || !pn || !pv || Z_TYPE_P(dc) != IS_STRING || Z_TYPE_P(pn) != IS_STRING) continue;
|
|
zend_class_entry *dce = zend_lookup_class(Z_STR_P(dc));
|
|
zend_property_info *pi = dce ? zend_hash_find_ptr(&dce->properties_info, Z_STR_P(pn)) : NULL;
|
|
if (!pi || (pi->flags & ZEND_ACC_STATIC) || !instanceof_function(ce, dce)) continue;
|
|
zval *slot2 = OBJ_PROP(obj, pi->offset);
|
|
zval_ptr_dtor(slot2);
|
|
ZVAL_COPY(slot2, pv);
|
|
if (Z_ISREF_P(slot2) && ZEND_TYPE_IS_SET(pi->type)) ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(slot2), pi);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
zval *dyn = zend_hash_str_find(Z_ARRVAL_P(data), "dyn", 3);
|
|
if (dyn && Z_TYPE_P(dyn) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(dyn))) {
|
|
HashTable *ht = zend_std_get_properties(obj);
|
|
zend_string *k; zval *v;
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(dyn), k, v) {
|
|
if (k) { Z_TRY_ADDREF_P(v); zend_hash_update(ht, k, v); }
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
zval_ptr_dtor(slot);
|
|
ZVAL_COPY_VALUE(slot, &result);
|
|
return;
|
|
}
|
|
} else if (strcmp(kind, "resource") == 0) {
|
|
cryo_reopen_resource(t, data, &result, path->s ? ZSTR_VAL(path->s) : "?");
|
|
}
|
|
zval keep;
|
|
ZVAL_COPY(&keep, &result);
|
|
zend_hash_index_update(&t->done, (zend_ulong)(uintptr_t) mobj, &keep);
|
|
zval_ptr_dtor(slot);
|
|
ZVAL_COPY_VALUE(slot, &result);
|
|
}
|
|
|
|
static void cryo_thaw_zval(cryo_thaw_ctx *t, zval *slot, uint32_t depth, smart_str *path, int pass)
|
|
{
|
|
if (EG(exception) || depth > CRYO_WALK_MAX_DEPTH) return;
|
|
zval *v = slot;
|
|
if (path->s) smart_str_0(path);
|
|
if (Z_TYPE_P(v) == IS_REFERENCE) {
|
|
if (zend_hash_index_exists(&t->seen, (zend_ulong)(uintptr_t) Z_REF_P(v))) return;
|
|
zend_hash_index_add_new(&t->seen, (zend_ulong)(uintptr_t) Z_REF_P(v), &EG(uninitialized_zval));
|
|
v = Z_REFVAL_P(v);
|
|
}
|
|
if (Z_TYPE_P(v) == IS_ARRAY) {
|
|
zend_array *ht = Z_ARR_P(v);
|
|
if (zend_hash_index_exists(&t->seen, (zend_ulong)(uintptr_t) ht)) return;
|
|
SEPARATE_ARRAY(v);
|
|
ht = Z_ARR_P(v);
|
|
zend_hash_index_add_new(&t->seen, (zend_ulong)(uintptr_t) ht, &EG(uninitialized_zval));
|
|
zend_string *k; zend_ulong i; zval *e;
|
|
ZEND_HASH_FOREACH_KEY_VAL(ht, i, k, e) {
|
|
size_t mark;
|
|
cryo_path_push(path, &mark, ".", k, i);
|
|
cryo_thaw_zval(t, e, depth + 1, path, pass);
|
|
cryo_path_pop(path, mark);
|
|
} ZEND_HASH_FOREACH_END();
|
|
} else if (Z_TYPE_P(v) == IS_OBJECT) {
|
|
if (Z_OBJCE_P(v) == cryo_ce_frozen) {
|
|
cryo_thaw_marker(t, v, depth, path, pass);
|
|
return;
|
|
}
|
|
zend_object *obj = Z_OBJ_P(v);
|
|
if (zend_hash_index_exists(&t->seen, (zend_ulong)(uintptr_t) obj)) return;
|
|
zend_hash_index_add_new(&t->seen, (zend_ulong)(uintptr_t) obj, &EG(uninitialized_zval));
|
|
if (obj->ce->type != ZEND_USER_CLASS && obj->ce != zend_standard_class_def) return;
|
|
for (int p = 0; p < obj->ce->default_properties_count; p++) {
|
|
zval *pv = &obj->properties_table[p];
|
|
if (Z_TYPE_P(pv) == IS_UNDEF) continue;
|
|
zend_property_info *pi = obj->ce->properties_info_table ? obj->ce->properties_info_table[p] : NULL;
|
|
size_t mark;
|
|
cryo_path_push(path, &mark, "->", pi ? pi->name : NULL, (zend_ulong) p);
|
|
cryo_thaw_zval(t, pv, depth + 1, path, pass);
|
|
cryo_path_pop(path, mark);
|
|
}
|
|
if (obj->properties) {
|
|
zend_string *k; zend_ulong i; zval *e;
|
|
ZEND_HASH_FOREACH_KEY_VAL(obj->properties, i, k, e) {
|
|
if (Z_TYPE_P(e) == IS_INDIRECT) continue;
|
|
size_t mark;
|
|
cryo_path_push(path, &mark, "->", k, i);
|
|
cryo_thaw_zval(t, e, depth + 1, path, pass);
|
|
cryo_path_pop(path, mark);
|
|
} ZEND_HASH_FOREACH_END();
|
|
}
|
|
}
|
|
}
|
|
|
|
void cryo_walk_thaw(cryo_thaw_ctx *t, zval *root, const char *root_path)
|
|
{
|
|
for (int pass = 1; pass <= 2; pass++) {
|
|
smart_str path = {0};
|
|
smart_str_appends(&path, root_path);
|
|
zend_hash_init(&t->seen, 64, NULL, NULL, 0);
|
|
cryo_thaw_zval(t, root, 0, &path, pass);
|
|
zend_hash_destroy(&t->seen);
|
|
smart_str_free(&path);
|
|
}
|
|
}
|
|
|
|
/* after the thaw walk: the values taken out of typed properties at the freeze, put back (thawed now) */
|
|
void cryo_walk_typed_restore(zval *data)
|
|
{
|
|
if (Z_TYPE_P(data) != IS_ARRAY) return;
|
|
zval *typed = zend_hash_str_find(Z_ARRVAL_P(data), "typed", 5);
|
|
if (!typed || Z_TYPE_P(typed) != IS_ARRAY) return;
|
|
zval *e;
|
|
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(typed), e) {
|
|
ZVAL_DEREF(e);
|
|
if (Z_TYPE_P(e) != IS_ARRAY) continue;
|
|
zval *o = zend_hash_index_find(Z_ARRVAL_P(e), 0), *cls = zend_hash_index_find(Z_ARRVAL_P(e), 1);
|
|
zval *name = zend_hash_index_find(Z_ARRVAL_P(e), 2), *val = zend_hash_index_find(Z_ARRVAL_P(e), 3);
|
|
if (!o || !cls || !name || !val) continue;
|
|
ZVAL_DEREF(o); ZVAL_DEREF(val);
|
|
if (Z_TYPE_P(o) != IS_OBJECT || Z_TYPE_P(cls) != IS_STRING || Z_TYPE_P(name) != IS_STRING) continue;
|
|
zend_class_entry *ce = zend_lookup_class(Z_STR_P(cls));
|
|
zend_property_info *pi = ce ? zend_hash_find_ptr(&ce->properties_info, Z_STR_P(name)) : NULL;
|
|
if (!pi || (pi->flags & ZEND_ACC_STATIC) || !instanceof_function(Z_OBJCE_P(o), ce)) continue;
|
|
if (Z_TYPE_P(val) == IS_NULL && !ZEND_TYPE_ALLOW_NULL(pi->type)) continue; /* nobody reopened it: left uninitialized */
|
|
zval *slot = OBJ_PROP(Z_OBJ_P(o), pi->offset);
|
|
zval_ptr_dtor(slot);
|
|
ZVAL_COPY(slot, val);
|
|
} ZEND_HASH_FOREACH_END();
|
|
zend_hash_str_del(Z_ARRVAL_P(data), "typed", 5);
|
|
}
|
|
|
|
void cryo_thaw_ctx_init(cryo_thaw_ctx *t, zend_op_array *main)
|
|
{
|
|
memset(t, 0, sizeof(*t));
|
|
t->main = main;
|
|
zend_hash_init(&t->done, 16, NULL, ZVAL_PTR_DTOR, 0);
|
|
}
|
|
|
|
void cryo_thaw_ctx_free(cryo_thaw_ctx *t)
|
|
{
|
|
zend_hash_destroy(&t->done);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Classes and cryo_register_resource() */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
PHP_FUNCTION(cryo_register_resource)
|
|
{
|
|
zend_string *type;
|
|
zval *describe, *reopen;
|
|
ZEND_PARSE_PARAMETERS_START(3, 3)
|
|
Z_PARAM_STR(type)
|
|
Z_PARAM_ZVAL(describe)
|
|
Z_PARAM_ZVAL(reopen)
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
if (!zend_is_callable(describe, 0, NULL)) { zend_argument_type_error(2, "must be a valid callback"); RETURN_THROWS(); }
|
|
if (!zend_is_callable(reopen, 0, NULL)) { zend_argument_type_error(3, "must be a valid callback"); RETURN_THROWS(); }
|
|
zval pair;
|
|
array_init(&pair);
|
|
Z_TRY_ADDREF_P(describe);
|
|
Z_TRY_ADDREF_P(reopen);
|
|
add_next_index_zval(&pair, describe);
|
|
add_next_index_zval(&pair, reopen);
|
|
zend_string *lc = zend_string_tolower(type);
|
|
zend_hash_update(&cryo_resource_handlers, lc, &pair);
|
|
zend_string_release(lc);
|
|
}
|
|
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_aware_freeze, 0, 0, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_aware_thaw, 0, 1, IS_VOID, 0)
|
|
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
static const zend_function_entry cryo_aware_methods[] = {
|
|
ZEND_ABSTRACT_ME(CryoAware, __cryoFreeze, arginfo_cryo_aware_freeze)
|
|
ZEND_ABSTRACT_ME(CryoAware, __cryoThaw, arginfo_cryo_aware_thaw)
|
|
ZEND_FE_END
|
|
};
|
|
|
|
ZEND_METHOD(CryoTransient, __construct)
|
|
{
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
}
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_cryo_transient_construct, 0, 0, 0)
|
|
ZEND_END_ARG_INFO()
|
|
|
|
static const zend_function_entry cryo_transient_methods[] = {
|
|
ZEND_ME(CryoTransient, __construct, arginfo_cryo_transient_construct, ZEND_ACC_PUBLIC)
|
|
ZEND_FE_END
|
|
};
|
|
|
|
void cryo_walk_minit(void)
|
|
{
|
|
zend_class_entry ce;
|
|
|
|
INIT_CLASS_ENTRY(ce, "Cryo\\Frozen", NULL);
|
|
cryo_ce_frozen = zend_register_internal_class(&ce);
|
|
cryo_ce_frozen->ce_flags |= ZEND_ACC_FINAL;
|
|
zend_declare_property_string(cryo_ce_frozen, "kind", 4, "", ZEND_ACC_PUBLIC);
|
|
zend_declare_property_null(cryo_ce_frozen, "data", 4, ZEND_ACC_PUBLIC);
|
|
|
|
INIT_CLASS_ENTRY(ce, "CryoPending", NULL);
|
|
cryo_ce_pending = zend_register_internal_class(&ce);
|
|
cryo_ce_pending->ce_flags |= ZEND_ACC_FINAL;
|
|
zend_declare_property_string(cryo_ce_pending, "path", 4, "", ZEND_ACC_PUBLIC);
|
|
zend_declare_property_string(cryo_ce_pending, "type", 4, "", ZEND_ACC_PUBLIC);
|
|
zend_declare_property_null(cryo_ce_pending, "desc", 4, ZEND_ACC_PUBLIC);
|
|
|
|
INIT_CLASS_ENTRY(ce, "CryoAware", cryo_aware_methods);
|
|
cryo_ce_aware = zend_register_internal_interface(&ce);
|
|
|
|
INIT_CLASS_ENTRY(ce, "CryoTransient", cryo_transient_methods);
|
|
cryo_ce_transient = zend_register_internal_class(&ce);
|
|
cryo_ce_transient->ce_flags |= ZEND_ACC_FINAL;
|
|
{
|
|
/* #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] */
|
|
zend_string *an = zend_string_init_interned("Attribute", sizeof("Attribute") - 1, 1);
|
|
zend_attribute *attr = zend_add_class_attribute(cryo_ce_transient, an, 1);
|
|
zend_string_release(an);
|
|
ZVAL_LONG(&attr->args[0].value, ZEND_ATTRIBUTE_TARGET_CLASS | ZEND_ATTRIBUTE_TARGET_PROPERTY);
|
|
zend_mark_internal_attribute(cryo_ce_transient);
|
|
}
|
|
|
|
}
|
|
|
|
void cryo_walk_rinit(void)
|
|
{
|
|
zend_hash_init(&cryo_resource_handlers, 8, NULL, ZVAL_PTR_DTOR, 0);
|
|
}
|
|
|
|
void cryo_walk_rshutdown(void)
|
|
{
|
|
zend_hash_destroy(&cryo_resource_handlers);
|
|
if (cryo_scratch_files) {
|
|
zend_hash_destroy(cryo_scratch_files);
|
|
FREE_HASHTABLE(cryo_scratch_files);
|
|
cryo_scratch_files = NULL;
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
/* Built-in stream bridge: plain files, php://std*, sockets */
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
zend_result cryo_stream_describe(zval *zv, zval *desc)
|
|
{
|
|
php_stream *s = (php_stream *) zend_fetch_resource2_ex(zv, NULL, php_file_le_stream(), php_file_le_pstream());
|
|
if (!s || !s->orig_path) return FAILURE;
|
|
const char *p = s->orig_path;
|
|
array_init(desc);
|
|
if (strncmp(p, "php://std", 9) == 0) {
|
|
add_assoc_string(desc, "std", (char *) p);
|
|
return SUCCESS;
|
|
}
|
|
if (s->wrapper == &php_plain_files_wrapper) {
|
|
char full[MAXPATHLEN];
|
|
add_assoc_string(desc, "path", expand_filepath(p, full) ? full : (char *) p);
|
|
add_assoc_string(desc, "mode", s->mode);
|
|
add_assoc_long(desc, "pos", (zend_long) php_stream_tell(s));
|
|
return SUCCESS;
|
|
}
|
|
if (strstr(p, "://") && (strncmp(p, "tcp://", 6) == 0 || strncmp(p, "udp://", 6) == 0
|
|
|| strncmp(p, "unix://", 7) == 0 || strncmp(p, "ssl://", 6) == 0 || strncmp(p, "tls://", 6) == 0)) {
|
|
add_assoc_string(desc, "socket", (char *) p);
|
|
return SUCCESS;
|
|
}
|
|
zval_ptr_dtor(desc);
|
|
ZVAL_UNDEF(desc);
|
|
return FAILURE;
|
|
}
|
|
|
|
void cryo_stream_reopen(zval *desc, zval *out)
|
|
{
|
|
zval *std = zend_hash_str_find(Z_ARRVAL_P(desc), "std", 3);
|
|
zval *path = zend_hash_str_find(Z_ARRVAL_P(desc), "path", 4);
|
|
zval *sock = zend_hash_str_find(Z_ARRVAL_P(desc), "socket", 6);
|
|
php_stream *s = NULL;
|
|
if (std && Z_TYPE_P(std) == IS_STRING) {
|
|
s = php_stream_open_wrapper(Z_STRVAL_P(std), strcmp(Z_STRVAL_P(std), "php://stdin") == 0 ? "rb" : "wb", 0, NULL);
|
|
} else if (path && Z_TYPE_P(path) == IS_STRING) {
|
|
zval *mode = zend_hash_str_find(Z_ARRVAL_P(desc), "mode", 4), *pos = zend_hash_str_find(Z_ARRVAL_P(desc), "pos", 3);
|
|
char m[16] = "rb";
|
|
if (mode && Z_TYPE_P(mode) == IS_STRING && Z_STRLEN_P(mode) < sizeof(m)) {
|
|
strcpy(m, Z_STRVAL_P(mode));
|
|
if (m[0] == 'w' || m[0] == 'x') m[0] = 'c'; /* reopen without truncating */
|
|
}
|
|
s = php_stream_open_wrapper(Z_STRVAL_P(path), m, REPORT_ERRORS, NULL);
|
|
if (s && pos && Z_TYPE_P(pos) == IS_LONG && m[0] != 'a') php_stream_seek(s, Z_LVAL_P(pos), SEEK_SET);
|
|
} else if (sock && Z_TYPE_P(sock) == IS_STRING) {
|
|
s = php_stream_xport_create(Z_STRVAL_P(sock), Z_STRLEN_P(sock), REPORT_ERRORS,
|
|
STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, NULL, NULL, NULL);
|
|
}
|
|
if (s) {
|
|
php_stream_to_zval(s, out);
|
|
} else {
|
|
ZVAL_UNDEF(out);
|
|
}
|
|
}
|