winzik/docker/ext/cryo/cryo_freeze.c
David Nurdin 1e0a9fad24 Cryo save points freeze the whole program, whatever the game (no code of it): JumPHP\Cryo\ProcessFreeze
- "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]>
2026-11-15 11:18:43 +01:00

2237 lines
85 KiB
C

/*
* cryo_freeze.c — milestones 5 and 7: cryo_freeze() / cryo_thaw() with stack
* capture, cryo_restart(), hooks, signed (and optionally compressed and
* encrypted) files, warm boot.
*
* cryo_freeze($file) writes the state of the running script: globals, static
* properties, function static variables, user constants, error / exception /
* shutdown handlers, cryo hooks, the Cryo\Engine journal, and every user frame
* of the call stack ({main} plus the functions and methods being executed),
* with their compiled variables, $this, extra arguments, live temporaries
* (foreach over arrays, @-silence) and the position of the call they wait on.
* Values the serializer cannot carry (closures, resources, CryoAware objects,
* #[CryoTransient]) go through cryo_walk.c.
*
* cryo_thaw($file), in a fresh process (php -r 'cryo_thaw("f");', after
* cryo_restart(), or at startup with CRYO_THAW=<file> / -d cryo.thaw=<file>),
* checks the signature and the sources, includes again the files the frozen
* process had included, recompiles the main script, restores the values, runs
* the cryo_on_thaw hooks, rebuilds the frames on the VM stack and resumes:
* cryo_freeze() then returns CRYO_THAWED in the innermost frame.
*
* File: "CRYF" u16 version u16 flags u32 0, then the body (zlib and/or
* libsodium when flagged) = serialize([meta, serialize(data)]), then
* "SIGN" + HMAC-SHA256(key, everything before) when signed.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_cryo.h"
#include "cryo_host.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_smart_str.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_constants.h"
#include "ext/standard/php_var.h"
#include "ext/standard/sha1.h"
#include "ext/standard/basic_functions.h"
#include "ext/hash/php_hash.h"
#include "ext/hash/php_hash_sha.h"
#include "zend_attributes.h"
#include "zend_generators.h"
#include "main/php_output.h"
#include "ext/random/php_random.h"
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#ifdef HAVE_CRYO_ZLIB
# include <zlib.h>
#endif
#ifdef HAVE_CRYO_ZSTD
# include <zstd.h>
#endif
#ifdef HAVE_CRYO_SODIUM
# include <sodium.h>
#endif
#define CRYF_MAGIC "CRYF"
#define CRYF_VERSION 2
#define CRYF_ZLIB 1
#define CRYF_CRYPT 2
#define CRYF_SIGNED 4
#define CRYF_ZSTD 8
zend_class_entry *cryo_ce_thaw_context;
/* weak: provided by cryo_engine.c / cryo_fork.c when they are built in */
extern zend_long cryo_fork_checkpoint(zend_string *variant, HashTable *opts) __attribute__((weak));
static zend_string *cryo_pending_thaw = NULL; /* startup thaw (persistent) */
static bool cryo_pending_soft = false; /* warm boot: a bad file means a normal start */
static void (*cryo_orig_execute_ex)(zend_execute_data *) = NULL;
/* request state */
static struct {
bool thawed;
zend_long thaw_count;
zval origin; /* array */
zval on_freeze; /* list of [prio, callable] */
zval on_thaw;
bool restart_pending;
zend_string *restart_file;
zval restart_opts;
uint32_t restart_tries;
zval *hook_data; /* thaw data while the cryo_on_thaw hooks run */
} cryo_fz;
/* ------------------------------------------------------------------------ */
/* Small helpers */
/* ------------------------------------------------------------------------ */
static double cryo_host_clock_ms(void)
{
#ifdef CRYO_BACKEND_WASM
return cryo_host_now();
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6;
#endif
}
static bool cryo_is_call_op(uint8_t op)
{
return op == ZEND_DO_FCALL || op == ZEND_DO_ICALL || op == ZEND_DO_UCALL || op == ZEND_DO_FCALL_BY_NAME;
}
static zend_string *cryo_sha1_of_file(const char *path)
{
php_stream *s = php_stream_open_wrapper((char *) path, "rb", 0, NULL);
if (!s) return NULL;
zend_string *data = php_stream_copy_to_mem(s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(s);
PHP_SHA1_CTX ctx;
unsigned char digest[20];
char hex[41];
PHP_SHA1Init(&ctx);
if (data) {
PHP_SHA1Update(&ctx, (const unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
zend_string_release(data);
}
PHP_SHA1Final(digest, &ctx);
make_sha1_digest(hex, digest);
return zend_string_init(hex, 40, 0);
}
static void cryo_hmac_sha256(const unsigned char *key, size_t klen, const unsigned char *msg, size_t mlen, unsigned char out[32])
{
unsigned char k[64], ipad[64], opad[64], inner[32];
PHP_SHA256_CTX c;
memset(k, 0, sizeof(k));
if (klen > 64) {
PHP_SHA256Init(&c); PHP_SHA256Update(&c, key, klen); PHP_SHA256Final(k, &c);
} else {
memcpy(k, key, klen);
}
for (int i = 0; i < 64; i++) { ipad[i] = k[i] ^ 0x36; opad[i] = k[i] ^ 0x5c; }
PHP_SHA256Init(&c); PHP_SHA256Update(&c, ipad, 64); PHP_SHA256Update(&c, msg, mlen); PHP_SHA256Final(inner, &c);
PHP_SHA256Init(&c); PHP_SHA256Update(&c, opad, 64); PHP_SHA256Update(&c, inner, 32); PHP_SHA256Final(out, &c);
}
static bool cryo_is_superglobal(zend_string *key)
{
static const char *skip[] = { "GLOBALS", "_GET", "_POST", "_COOKIE", "_SERVER", "_ENV", "_REQUEST",
"_FILES", "_SESSION", "argv", "argc", NULL };
for (int i = 0; skip[i]; i++) {
if (zend_string_equals_cstr(key, skip[i], strlen(skip[i]))) return true;
}
return false;
}
static uint32_t cryo_pos_to_ordinal(HashTable *ht, HashPosition pos)
{
uint32_t n = 0;
for (uint32_t i = 0; i < pos && i < ht->nNumUsed; i++) {
zval *v = HT_IS_PACKED(ht) ? &ht->arPacked[i] : &ht->arData[i].val;
if (Z_TYPE_P(v) != IS_UNDEF) n++;
}
return n;
}
static HashPosition cryo_ordinal_to_pos(HashTable *ht, uint32_t ordinal)
{
uint32_t n = 0, i;
for (i = 0; i < ht->nNumUsed; i++) {
zval *v = HT_IS_PACKED(ht) ? &ht->arPacked[i] : &ht->arData[i].val;
if (Z_TYPE_P(v) == IS_UNDEF) continue;
if (n++ == ordinal) return i;
}
return i;
}
static zend_string *cryo_frame_name(zend_execute_data *ex)
{
const zend_function *f = ex->func;
if (!f->common.function_name) return zend_string_init("{main}", 6, 0);
if (f->common.scope) {
return zend_strpprintf(0, "%s::%s", ZSTR_VAL(f->common.scope->name), ZSTR_VAL(f->common.function_name));
}
return zend_string_copy(f->common.function_name);
}
static zval *cryo_get(zval *arr, const char *key)
{
return arr && Z_TYPE_P(arr) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(arr), key, strlen(key)) : NULL;
}
static zend_string *cryo_key_of(HashTable *opts)
{
zval *k = opts ? zend_hash_str_find(opts, "key", 3) : NULL;
if (k && Z_TYPE_P(k) == IS_STRING && Z_STRLEN_P(k)) return zend_string_copy(Z_STR_P(k));
const char *ini = INI_STR("cryo.key");
if (!ini || !*ini) ini = getenv("CRYO_KEY"); /* a spawned sandbox gets the key of its parent this way */
return ini && *ini ? zend_string_init(ini, strlen(ini), 0) : NULL;
}
/* ------------------------------------------------------------------------ */
/* Hooks */
/* ------------------------------------------------------------------------ */
static void cryo_hook_add(zval *list, zval *cb, zend_long prio)
{
zval entry;
array_init(&entry);
add_next_index_long(&entry, prio);
Z_TRY_ADDREF_P(cb);
add_next_index_zval(&entry, cb);
add_next_index_zval(list, &entry);
}
static int cryo_hook_cmp(Bucket *a, Bucket *b)
{
zend_long pa = Z_LVAL_P(zend_hash_index_find(Z_ARRVAL(a->val), 0));
zend_long pb = Z_LVAL_P(zend_hash_index_find(Z_ARRVAL(b->val), 0));
return pa < pb ? 1 : (pa > pb ? -1 : 0); /* higher priority first, stable */
}
static void cryo_hooks_run(zval *list, zval *arg)
{
if (Z_TYPE_P(list) != IS_ARRAY || !zend_hash_num_elements(Z_ARRVAL_P(list))) return;
zval copy;
ZVAL_ARR(&copy, zend_array_dup(Z_ARRVAL_P(list)));
zend_hash_sort(Z_ARRVAL(copy), cryo_hook_cmp, 0);
zval *e;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(copy), e) {
zval *cb = zend_hash_index_find(Z_ARRVAL_P(e), 1), ret;
if (!cb) continue;
call_user_function(NULL, NULL, cb, &ret, arg ? 1 : 0, arg);
zval_ptr_dtor(&ret);
if (EG(exception)) break;
} ZEND_HASH_FOREACH_END();
zval_ptr_dtor(&copy);
}
PHP_FUNCTION(cryo_on_freeze)
{
zval *cb; zend_long prio = 0;
ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(cb) Z_PARAM_OPTIONAL Z_PARAM_LONG(prio) ZEND_PARSE_PARAMETERS_END();
if (!zend_is_callable(cb, 0, NULL)) { zend_argument_type_error(1, "must be a valid callback"); RETURN_THROWS(); }
cryo_hook_add(&cryo_fz.on_freeze, cb, prio);
}
PHP_FUNCTION(cryo_on_thaw)
{
zval *cb; zend_long prio = 0;
ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(cb) Z_PARAM_OPTIONAL Z_PARAM_LONG(prio) ZEND_PARSE_PARAMETERS_END();
if (!zend_is_callable(cb, 0, NULL)) { zend_argument_type_error(1, "must be a valid callback"); RETURN_THROWS(); }
cryo_hook_add(&cryo_fz.on_thaw, cb, prio);
}
/* also called in every child of the fork modes */
void cryo_hooks_run_thaw(void)
{
zval ctx;
object_init_ex(&ctx, cryo_ce_thaw_context);
cryo_hooks_run(&cryo_fz.on_thaw, &ctx);
zval_ptr_dtor(&ctx);
}
PHP_FUNCTION(cryo_is_thawed) { ZEND_PARSE_PARAMETERS_NONE(); RETURN_BOOL(cryo_fz.thawed); }
PHP_FUNCTION(cryo_thaw_count) { ZEND_PARSE_PARAMETERS_NONE(); RETURN_LONG(cryo_fz.thaw_count); }
PHP_FUNCTION(cryo_origin)
{
ZEND_PARSE_PARAMETERS_NONE();
if (Z_TYPE(cryo_fz.origin) == IS_ARRAY) RETURN_COPY(&cryo_fz.origin);
RETURN_EMPTY_ARRAY();
}
/* ------------------------------------------------------------------------ */
/* File container: compression, encryption, signature */
/* ------------------------------------------------------------------------ */
static void cryo_enc_key(zend_string *key, unsigned char k[32])
{
PHP_SHA256_CTX c;
PHP_SHA256Init(&c); PHP_SHA256Update(&c, (unsigned char *) "cryo-enc", 8);
PHP_SHA256Update(&c, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key)); PHP_SHA256Final(k, &c);
}
/* option crypto => "portable" forces the built-in code (tests compare it with libsodium) */
static ZEND_ATTRIBUTE_UNUSED bool cryo_use_sodium(HashTable *opts)
{
#ifdef HAVE_CRYO_SODIUM
zval *ci = opts ? zend_hash_str_find(opts, "crypto", 6) : NULL;
return !(ci && Z_TYPE_P(ci) == IS_STRING && zend_string_equals_literal(Z_STR_P(ci), "portable"));
#else
(void) opts;
return false;
#endif
}
static void cryo_aead_encrypt(unsigned char *c, const unsigned char *m, size_t mlen, const unsigned char nonce[24], const unsigned char k[32], HashTable *opts)
{
#ifdef HAVE_CRYO_SODIUM
if (cryo_use_sodium(opts)) {
unsigned long long clen;
crypto_aead_xchacha20poly1305_ietf_encrypt(c, &clen, m, mlen, NULL, 0, NULL, nonce, k);
return;
}
#endif
(void) opts;
cryo_xchacha_encrypt(c, m, mlen, nonce, k);
}
static int cryo_aead_decrypt(unsigned char *m, const unsigned char *c, size_t clen, const unsigned char nonce[24], const unsigned char k[32], HashTable *opts)
{
#ifdef HAVE_CRYO_SODIUM
if (cryo_use_sodium(opts)) {
unsigned long long mlen;
return crypto_aead_xchacha20poly1305_ietf_decrypt(m, &mlen, NULL, c, clen, NULL, 0, nonce, k);
}
#endif
(void) opts;
return cryo_xchacha_decrypt(m, c, clen, nonce, k);
}
static zend_result cryo_write_container(zend_string *file, zend_string *body, HashTable *opts)
{
uint16_t flags = 0;
zend_string *payload = zend_string_copy(body);
zval *zc = opts ? zend_hash_str_find(opts, "compress", 8) : NULL;
bool compress = !zc || (Z_TYPE_P(zc) == IS_STRING ? !zend_string_equals_literal(Z_STR_P(zc), "none") : zend_is_true(zc));
bool want_zlib = zc && Z_TYPE_P(zc) == IS_STRING && zend_string_equals_literal(Z_STR_P(zc), "zlib");
#ifdef HAVE_CRYO_ZSTD
/* zstd (the default), lz4 = zstd at its fastest level; zlib when asked or without libzstd */
if (compress && !want_zlib) {
int level = zc && Z_TYPE_P(zc) == IS_STRING && zend_string_equals_literal(Z_STR_P(zc), "lz4") ? 1 : 3;
size_t bound = ZSTD_compressBound(ZSTR_LEN(payload));
zend_string *c = zend_string_alloc(bound + 4, 0);
uint32_t raw = (uint32_t) ZSTR_LEN(payload);
memcpy(ZSTR_VAL(c), &raw, 4);
size_t n = ZSTD_compress(ZSTR_VAL(c) + 4, bound, ZSTR_VAL(payload), ZSTR_LEN(payload), level);
if (!ZSTD_isError(n)) {
ZSTR_LEN(c) = n + 4;
zend_string_release(payload);
payload = c;
flags |= CRYF_ZSTD;
compress = false;
} else {
zend_string_release(c);
}
}
#endif
(void) want_zlib;
#ifdef HAVE_CRYO_ZLIB
if (compress) {
uLongf clen = compressBound((uLong) ZSTR_LEN(payload));
zend_string *c = zend_string_alloc(clen + 4, 0);
uint32_t raw = (uint32_t) ZSTR_LEN(payload);
memcpy(ZSTR_VAL(c), &raw, 4);
if (compress2((Bytef *) ZSTR_VAL(c) + 4, &clen, (const Bytef *) ZSTR_VAL(payload), (uLong) ZSTR_LEN(payload), 6) == Z_OK) {
ZSTR_LEN(c) = clen + 4;
zend_string_release(payload);
payload = c;
flags |= CRYF_ZLIB;
} else {
zend_string_release(c);
}
}
#else
(void) compress;
#endif
zend_string *key = cryo_key_of(opts);
zval *ze = opts ? zend_hash_str_find(opts, "encrypt", 7) : NULL;
if (ze && zend_is_true(ze)) {
if (!key) {
zend_string_release(payload);
zend_throw_exception(cryo_ce_freeze_exception, "encrypt needs a key (option 'key' or ini cryo.key)", 0);
return FAILURE;
}
/* XChaCha20-Poly1305: libsodium when built with it, else the portable code (same bytes) */
unsigned char k[32];
cryo_enc_key(key, k);
zend_string *enc = zend_string_alloc(24 + ZSTR_LEN(payload) + 16, 0);
if (php_random_bytes_silent(ZSTR_VAL(enc), 24) == FAILURE) {
zend_string_release(enc);
zend_string_release(payload);
zend_string_release(key);
zend_throw_exception(cryo_ce_freeze_exception, "no randomness for the nonce", 0);
return FAILURE;
}
cryo_aead_encrypt((unsigned char *) ZSTR_VAL(enc) + 24, (unsigned char *) ZSTR_VAL(payload), ZSTR_LEN(payload),
(unsigned char *) ZSTR_VAL(enc), k, opts);
ZEND_SECURE_ZERO(k, sizeof(k));
zend_string_release(payload);
payload = enc;
flags |= CRYF_CRYPT;
}
if (key) flags |= CRYF_SIGNED;
smart_str out = {0};
uint16_t ver = CRYF_VERSION;
uint32_t zero = 0;
smart_str_appendl(&out, CRYF_MAGIC, 4);
smart_str_appendl(&out, (const char *) &ver, 2);
smart_str_appendl(&out, (const char *) &flags, 2);
smart_str_appendl(&out, (const char *) &zero, 4);
smart_str_append(&out, payload);
zend_string_release(payload);
if (key) {
unsigned char mac[32];
cryo_hmac_sha256((unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key), (unsigned char *) ZSTR_VAL(out.s), ZSTR_LEN(out.s), mac);
smart_str_appendl(&out, "SIGN", 4);
smart_str_appendl(&out, (const char *) mac, 32);
zend_string_release(key);
}
smart_str_0(&out);
zend_string *tmp = zend_strpprintf(0, "%s.tmp", ZSTR_VAL(file));
php_stream *s = php_stream_open_wrapper(ZSTR_VAL(tmp), "wb", REPORT_ERRORS, NULL);
bool ok = false;
if (s) {
ok = php_stream_write(s, ZSTR_VAL(out.s), ZSTR_LEN(out.s)) == (ssize_t) ZSTR_LEN(out.s);
php_stream_close(s);
}
if (ok) {
VCWD_CHMOD(ZSTR_VAL(tmp), 0600);
ok = VCWD_RENAME(ZSTR_VAL(tmp), ZSTR_VAL(file)) == 0;
}
zend_string_release(tmp);
smart_str_free(&out);
if (!ok) {
zend_throw_exception_ex(cryo_ce_freeze_exception, 0, "Cannot write %s", ZSTR_VAL(file));
return FAILURE;
}
return SUCCESS;
}
/* Reads and checks a file; returns the body (serialize([meta, sdata])).
* need_sig: refuse unsigned files unless cryo.allow_unsigned=1. */
static zend_string *cryo_read_container(zend_string *file, HashTable *opts, bool need_sig, uint16_t *flags_out)
{
zend_stat_t st;
if (VCWD_STAT(ZSTR_VAL(file), &st) != 0) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Cannot open %s", ZSTR_VAL(file));
return NULL;
}
#ifndef CRYO_BACKEND_WASM
if (need_sig && (st.st_mode & 022) && !INI_BOOL("cryo.allow_unsigned")) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "%s is writable by other users: refused", ZSTR_VAL(file));
return NULL;
}
#endif
php_stream *s = php_stream_open_wrapper(ZSTR_VAL(file), "rb", REPORT_ERRORS, NULL);
zend_string *raw = s ? php_stream_copy_to_mem(s, PHP_STREAM_COPY_ALL, 0) : NULL;
if (s) php_stream_close(s);
if (!raw || ZSTR_LEN(raw) < 12 || memcmp(ZSTR_VAL(raw), CRYF_MAGIC, 4) != 0) {
if (raw) zend_string_release(raw);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "%s is not a cryo_freeze() file", ZSTR_VAL(file));
return NULL;
}
uint16_t ver, flags;
memcpy(&ver, ZSTR_VAL(raw) + 4, 2);
memcpy(&flags, ZSTR_VAL(raw) + 6, 2);
if (flags_out) *flags_out = flags;
if (ver != CRYF_VERSION) {
zend_string_release(raw);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Unsupported freeze format %u in %s", ver, ZSTR_VAL(file));
return NULL;
}
size_t end = ZSTR_LEN(raw);
zend_string *key = cryo_key_of(opts);
if (flags & CRYF_SIGNED) {
if (end < 12 + 36 || memcmp(ZSTR_VAL(raw) + end - 36, "SIGN", 4) != 0) {
zend_string_release(raw);
if (key) zend_string_release(key);
zend_throw_exception_ex(cryo_ce_signature_exception, 0, "%s: signature missing", ZSTR_VAL(file));
return NULL;
}
end -= 36;
if (need_sig || key) {
unsigned char mac[32];
if (!key) {
zend_string_release(raw);
zend_throw_exception_ex(cryo_ce_signature_exception, 0, "%s is signed: set cryo.key (or the 'key' option) to thaw it", ZSTR_VAL(file));
return NULL;
}
cryo_hmac_sha256((unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key), (unsigned char *) ZSTR_VAL(raw), end, mac);
unsigned char diff = 0;
for (int i = 0; i < 32; i++) diff |= mac[i] ^ (unsigned char) ZSTR_VAL(raw)[end + 4 + i];
if (diff != 0) {
zend_string_release(raw);
zend_string_release(key);
zend_throw_exception_ex(cryo_ce_signature_exception, 0, "%s: bad signature (wrong key or modified file)", ZSTR_VAL(file));
return NULL;
}
}
} else if (need_sig && !INI_BOOL("cryo.allow_unsigned")) {
zend_string_release(raw);
if (key) zend_string_release(key);
zend_throw_exception_ex(cryo_ce_signature_exception, 0,
"%s is not signed: freeze with cryo.key set, or allow it with cryo.allow_unsigned=1 (development only)", ZSTR_VAL(file));
return NULL;
}
zend_string *body = zend_string_init(ZSTR_VAL(raw) + 12, end - 12, 0);
zend_string_release(raw);
if (flags & CRYF_CRYPT) {
if (!key || ZSTR_LEN(body) < 24 + 16) {
zend_string_release(body);
if (key) zend_string_release(key);
zend_throw_exception_ex(cryo_ce_signature_exception, 0, "%s is encrypted: a key is needed", ZSTR_VAL(file));
return NULL;
}
unsigned char k[32];
cryo_enc_key(key, k);
size_t dlen = ZSTR_LEN(body) - 24 - 16;
zend_string *dec = zend_string_alloc(dlen, 0);
int ok = cryo_aead_decrypt((unsigned char *) ZSTR_VAL(dec), (unsigned char *) ZSTR_VAL(body) + 24, ZSTR_LEN(body) - 24,
(unsigned char *) ZSTR_VAL(body), k, opts);
ZEND_SECURE_ZERO(k, sizeof(k));
zend_string_release(body);
if (ok != 0) {
zend_string_release(dec);
zend_string_release(key);
zend_throw_exception_ex(cryo_ce_signature_exception, 0, "%s: cannot decrypt (wrong key)", ZSTR_VAL(file));
return NULL;
}
ZSTR_VAL(dec)[dlen] = '\0';
body = dec;
}
if (key) zend_string_release(key);
if (flags & CRYF_ZSTD) {
#ifdef HAVE_CRYO_ZSTD
uint32_t rawlen;
if (ZSTR_LEN(body) < 4) { zend_string_release(body); goto corrupt; }
memcpy(&rawlen, ZSTR_VAL(body), 4);
unsigned long long fsize = ZSTD_getFrameContentSize(ZSTR_VAL(body) + 4, ZSTR_LEN(body) - 4);
if (fsize == ZSTD_CONTENTSIZE_ERROR || fsize == ZSTD_CONTENTSIZE_UNKNOWN || fsize != rawlen) {
zend_string_release(body);
goto corrupt;
}
zend_string *d = zend_string_alloc(rawlen, 0);
size_t n = ZSTD_decompress(ZSTR_VAL(d), rawlen, ZSTR_VAL(body) + 4, ZSTR_LEN(body) - 4);
zend_string_release(body);
if (ZSTD_isError(n) || n != rawlen) { zend_string_release(d); goto corrupt; }
ZSTR_LEN(d) = n;
ZSTR_VAL(d)[n] = '\0';
body = d;
#else
zend_string_release(body);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "%s is compressed with zstd: ext-cryo was built without libzstd", ZSTR_VAL(file));
return NULL;
#endif
}
if (flags & CRYF_ZLIB) {
#ifdef HAVE_CRYO_ZLIB
uint32_t rawlen;
if (ZSTR_LEN(body) < 4) { zend_string_release(body); goto corrupt; }
memcpy(&rawlen, ZSTR_VAL(body), 4);
/* deflate expands 1032:1 at most: a larger announced size is a lie
* (and would allocate up to 4 GB before uncompress() fails) */
if ((uint64_t) rawlen > (uint64_t) (ZSTR_LEN(body) - 4) * 1032 + 64) { zend_string_release(body); goto corrupt; }
zend_string *d = zend_string_alloc(rawlen, 0);
uLongf dl = rawlen;
if (uncompress((Bytef *) ZSTR_VAL(d), &dl, (const Bytef *) ZSTR_VAL(body) + 4, (uLong) ZSTR_LEN(body) - 4) != Z_OK) {
zend_string_release(d);
zend_string_release(body);
goto corrupt;
}
ZSTR_LEN(d) = dl;
ZSTR_VAL(d)[dl] = '\0';
zend_string_release(body);
body = d;
#else
zend_string_release(body);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "%s is compressed: ext-cryo was built without zlib", ZSTR_VAL(file));
return NULL;
#endif
}
return body;
corrupt:
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Corrupted file %s", ZSTR_VAL(file));
return NULL;
}
/* Unserializes an array. no_objects (the meta of a file, read by cryo_info()
* before any signature check): no class is instantiated (no __wakeup /
* __destruct of the file's choosing), no parser warning on top of the
* exception, and a partial value is released. */
static zend_result cryo_unserialize(const char *buf, size_t len, zval *out, bool no_objects)
{
const unsigned char *p = (const unsigned char *) buf, *end = p + len;
php_unserialize_data_t ud;
HashTable none;
ZVAL_NULL(out);
PHP_VAR_UNSERIALIZE_INIT(ud);
if (no_objects) {
zend_hash_init(&none, 0, NULL, NULL, 0);
php_var_unserialize_set_allowed_classes(ud, &none);
}
int er = EG(error_reporting);
if (no_objects) EG(error_reporting) &= E_FATAL_ERRORS;
bool ok = php_var_unserialize(out, &p, end, &ud);
EG(error_reporting) = er;
PHP_VAR_UNSERIALIZE_DESTROY(ud);
if (no_objects) {
zend_hash_destroy(&none);
}
if (!ok || Z_TYPE_P(out) != IS_ARRAY) {
if (ok || no_objects) zval_ptr_dtor(out);
ZVAL_UNDEF(out);
return FAILURE;
}
return SUCCESS;
}
/* meta (array) + serialized data from a file */
static zend_result cryo_load(zend_string *file, HashTable *opts, bool need_sig, zval *meta, zend_string **sdata, uint16_t *flags)
{
ZVAL_UNDEF(meta);
zend_string *body = cryo_read_container(file, opts, need_sig, flags);
if (!body) return FAILURE;
zval outer;
zend_result rc = cryo_unserialize(ZSTR_VAL(body), ZSTR_LEN(body), &outer, true); /* meta: arrays and scalars only */
zend_string_release(body);
zval *m = rc == SUCCESS ? zend_hash_index_find(Z_ARRVAL(outer), 0) : NULL;
zval *d = rc == SUCCESS ? zend_hash_index_find(Z_ARRVAL(outer), 1) : NULL;
if (!m || Z_TYPE_P(m) != IS_ARRAY || !d || Z_TYPE_P(d) != IS_STRING) {
if (rc == SUCCESS) zval_ptr_dtor(&outer);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Corrupted file %s", ZSTR_VAL(file));
return FAILURE;
}
ZVAL_COPY(meta, m);
if (sdata) *sdata = zend_string_copy(Z_STR_P(d));
zval_ptr_dtor(&outer);
return SUCCESS;
}
/* Sources and watched files unchanged, same PHP: NULL or the reason. */
static zend_string *cryo_compat_problem(zval *meta)
{
zval *php = cryo_get(meta, "php");
if (!php || Z_TYPE_P(php) != IS_LONG || Z_LVAL_P(php) != PHP_VERSION_ID) {
return zend_strpprintf(0, "frozen by PHP %ld, this is %d", php && Z_TYPE_P(php) == IS_LONG ? (long) Z_LVAL_P(php) : 0L, PHP_VERSION_ID);
}
const char *lists[] = { "sources", "watch" };
for (int l = 0; l < 2; l++) {
zval *src = cryo_get(meta, lists[l]);
if (!src || Z_TYPE_P(src) != IS_ARRAY) continue;
zend_string *path; zval *sha;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(src), path, sha) {
if (!path) continue;
zend_string *now = cryo_sha1_of_file(ZSTR_VAL(path));
bool same = (Z_TYPE_P(sha) == IS_STRING && now && zend_string_equals(now, Z_STR_P(sha)))
|| (Z_TYPE_P(sha) == IS_NULL && !now);
if (now) zend_string_release(now);
if (!same) return zend_strpprintf(0, "source changed: %s", ZSTR_VAL(path));
} ZEND_HASH_FOREACH_END();
}
return NULL;
}
PHP_FUNCTION(cryo_info)
{
zend_string *file;
ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_PATH_STR(file) ZEND_PARSE_PARAMETERS_END();
zval meta; uint16_t flags = 0;
if (cryo_load(file, NULL, false, &meta, NULL, &flags) == FAILURE) RETURN_THROWS();
array_init(return_value);
zval *v;
const char *keys[] = { "php", "script", "time", "pid", "host", "thaw_count", "resume", "mode", NULL };
for (int i = 0; keys[i]; i++) {
if ((v = cryo_get(&meta, keys[i]))) { Z_TRY_ADDREF_P(v); add_assoc_zval(return_value, keys[i], v); }
}
add_assoc_long(return_value, "format", CRYF_VERSION);
zval *frames = cryo_get(&meta, "frames"), *src = cryo_get(&meta, "sources");
add_assoc_long(return_value, "frames", frames && Z_TYPE_P(frames) == IS_ARRAY ? zend_hash_num_elements(Z_ARRVAL_P(frames)) : 0);
add_assoc_long(return_value, "sources", src && Z_TYPE_P(src) == IS_ARRAY ? zend_hash_num_elements(Z_ARRVAL_P(src)) : 0);
add_assoc_bool(return_value, "signed", flags & CRYF_SIGNED);
add_assoc_bool(return_value, "compressed", flags & (CRYF_ZLIB | CRYF_ZSTD));
add_assoc_string(return_value, "compression", (flags & CRYF_ZSTD) ? "zstd" : ((flags & CRYF_ZLIB) ? "zlib" : "none"));
add_assoc_bool(return_value, "encrypted", flags & CRYF_CRYPT);
zend_stat_t st;
if (VCWD_STAT(ZSTR_VAL(file), &st) == 0) add_assoc_long(return_value, "size", (zend_long) st.st_size);
zval_ptr_dtor(&meta);
}
PHP_FUNCTION(cryo_verify)
{
zend_string *file;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_PATH_STR(file) Z_PARAM_OPTIONAL Z_PARAM_ARRAY_HT(opts) ZEND_PARSE_PARAMETERS_END();
zval meta;
if (cryo_load(file, opts, true, &meta, NULL, NULL) == FAILURE) {
zend_clear_exception();
RETURN_FALSE;
}
zend_string *why = cryo_compat_problem(&meta);
zval_ptr_dtor(&meta);
if (why) { zend_string_release(why); RETURN_FALSE; }
RETURN_TRUE;
}
/* ------------------------------------------------------------------------ */
/* Freeze */
/* ------------------------------------------------------------------------ */
static zend_string *cryo_save_tmps(zend_execute_data *ex, uint32_t n, zval *tmps)
{
const zend_op_array *op = &ex->func->op_array;
for (uint32_t i = 0; i < op->last_live_range; i++) {
const zend_live_range *r = &op->live_range[i];
if (r->start > n) break;
if (n >= r->end) continue;
uint32_t kind = r->var & ZEND_LIVE_MASK, var = r->var & ~ZEND_LIVE_MASK;
zval *v = ZEND_CALL_VAR(ex, var);
if (kind == ZEND_LIVE_ROPE || kind == ZEND_LIVE_NEW || (kind == ZEND_LIVE_LOOP && Z_TYPE_P(v) != IS_ARRAY)) {
zend_string *fn = cryo_frame_name(ex);
zend_string *why = kind == ZEND_LIVE_LOOP
? zend_strpprintf(0, "a foreach in %s iterates %s (only arrays by value can be frozen)",
ZSTR_VAL(fn), Z_TYPE_P(v) == IS_REFERENCE ? "by reference" : "an object")
: zend_strpprintf(0, "%s is in the middle of a %s", ZSTR_VAL(fn),
kind == ZEND_LIVE_ROPE ? "string interpolation" : "new expression");
zend_string_release(fn);
return why;
}
if (Z_TYPE_P(v) == IS_UNDEF) continue;
zval entry, copy;
array_init(&entry);
add_assoc_long(&entry, "var", var);
add_assoc_long(&entry, "kind", kind);
ZVAL_COPY(&copy, v);
add_assoc_zval(&entry, "value", &copy);
if (kind == ZEND_LIVE_LOOP) add_assoc_long(&entry, "pos", cryo_pos_to_ordinal(Z_ARRVAL_P(v), Z_FE_POS_P(v)));
add_next_index_zval(tmps, &entry);
}
return NULL;
}
static void cryo_restore_tmps(zend_execute_data *ex, zval *tmps)
{
zval *e;
if (!tmps || Z_TYPE_P(tmps) != IS_ARRAY) return;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmps), e) {
zval *var = cryo_get(e, "var"), *kind = cryo_get(e, "kind"), *value = cryo_get(e, "value");
if (!var || !kind || !value) continue;
zval *slot = ZEND_CALL_VAR(ex, (uint32_t) Z_LVAL_P(var));
ZVAL_COPY(slot, value);
if (Z_LVAL_P(kind) == ZEND_LIVE_LOOP && Z_TYPE_P(slot) == IS_ARRAY) {
zval *pos = cryo_get(e, "pos");
Z_FE_POS_P(slot) = cryo_ordinal_to_pos(Z_ARRVAL_P(slot), pos ? (uint32_t) Z_LVAL_P(pos) : 0);
}
} ZEND_HASH_FOREACH_END();
}
#define CRYO_MAX_FRAMES 512
#define WHY(s) zend_string_init(s, sizeof(s) - 1, 0)
/* User frames from `start` down to {main}, innermost first. resume_at: the
* innermost frame resumes AT its opline (interrupt), else after its call. */
static zend_string *cryo_collect_frames(zend_execute_data *start, bool resume_at, zend_execute_data **frames, uint32_t *count)
{
*count = 0;
if (cryo_jit_active()) return WHY("JIT is enabled (set opcache.jit=0)");
if (EG(active_fiber)) return WHY("called inside a Fiber");
for (zend_execute_data *ex = start; ex; ex = ex->prev_execute_data) {
if (!ex->func) continue;
const zend_function *f = ex->func;
if (!ZEND_USER_CODE(f->type)) {
return zend_strpprintf(0, "internal frame in the stack: %s()", f->common.function_name ? ZSTR_VAL(f->common.function_name) : "?");
}
if (f->common.fn_flags & ZEND_ACC_GENERATOR) {
return zend_strpprintf(0, "running generator in the stack: %s()", ZSTR_VAL(f->common.function_name));
}
if (f->common.fn_flags & ZEND_ACC_CLOSURE) return WHY("closure in the stack");
bool bottom = false;
if (!f->common.function_name) {
/* top-level code: the main script, or a script resumed by cryo_thaw() above the code that called it
* (php -r 'cryo_thaw(...)'): the bottom of what is frozen. Included / eval'd code: refused. */
if (!(ZEND_CALL_INFO(ex) & ZEND_CALL_TOP)) {
return WHY("include/eval frame in the stack (freeze from the main script or its functions)");
}
bottom = true;
}
if (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
return zend_strpprintf(0, "%s() received extra named arguments", ZSTR_VAL(f->common.function_name));
}
if (ex->call) return WHY("cryo_freeze() is called inside the arguments of another call");
bool innermost = *count == 0;
if (!ex->opline || (!(innermost && resume_at) && !cryo_is_call_op(ex->opline->opcode))) {
return WHY("unexpected frame state");
}
if (*count == CRYO_MAX_FRAMES) return WHY("call stack too deep");
frames[(*count)++] = ex;
if (bottom) break;
}
if (*count == 0) return WHY("no user frame");
return NULL;
}
static void cryo_capture_fstatics(zval *out)
{
array_init(out);
zend_function *f;
ZEND_HASH_MAP_FOREACH_PTR(EG(function_table), f) {
if (f->type != ZEND_USER_FUNCTION || !f->op_array.static_variables) continue;
HashTable *ht = ZEND_MAP_PTR_GET(f->op_array.static_variables_ptr);
if (!ht) continue;
zval copy;
ZVAL_ARR(&copy, zend_array_dup(ht));
zend_hash_update(Z_ARRVAL_P(out), f->common.function_name, &copy);
} 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, f) {
if (f->type != ZEND_USER_FUNCTION || f->common.scope != ce || !f->op_array.static_variables) continue;
HashTable *ht = ZEND_MAP_PTR_GET(f->op_array.static_variables_ptr);
if (!ht) continue;
zval copy;
ZVAL_ARR(&copy, zend_array_dup(ht));
zend_string *k = zend_strpprintf(0, "%s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(f->common.function_name));
zend_hash_update(Z_ARRVAL_P(out), k, &copy);
zend_string_release(k);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
}
static void cryo_capture_handlers(zval *out)
{
array_init(out);
if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF && Z_TYPE(EG(user_error_handler)) != IS_NULL) {
Z_TRY_ADDREF(EG(user_error_handler));
add_assoc_zval(out, "error", &EG(user_error_handler));
add_assoc_long(out, "error_types", EG(user_error_handler_error_reporting));
}
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF && Z_TYPE(EG(user_exception_handler)) != IS_NULL) {
Z_TRY_ADDREF(EG(user_exception_handler));
add_assoc_zval(out, "exception", &EG(user_exception_handler));
}
if (BG(user_shutdown_function_names)) {
zval list;
array_init(&list);
php_shutdown_function_entry *e;
ZEND_HASH_FOREACH_PTR(BG(user_shutdown_function_names), e) {
zval item, args;
array_init(&item);
#if PHP_VERSION_ID >= 80500
/* 8.5 keeps only the call cache: rebuild a callable from it */
zval cb;
zend_fcall_info_cache *fcc = &e->fci_cache;
if (fcc->closure) {
ZVAL_OBJ_COPY(&cb, fcc->closure);
} else if (fcc->object) {
array_init(&cb);
GC_ADDREF(fcc->object);
add_next_index_object(&cb, fcc->object);
add_next_index_str(&cb, zend_string_copy(fcc->function_handler->common.function_name));
} else if (fcc->called_scope && fcc->function_handler->common.scope) {
ZVAL_STR(&cb, zend_strpprintf(0, "%s::%s", ZSTR_VAL(fcc->called_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name)));
} else {
ZVAL_STR_COPY(&cb, fcc->function_handler->common.function_name);
}
add_next_index_zval(&item, &cb);
zval *params = e->params;
uint32_t param_count = e->param_count;
#else
Z_TRY_ADDREF(e->fci.function_name);
add_next_index_zval(&item, &e->fci.function_name);
zval *params = e->fci.params;
uint32_t param_count = e->fci.param_count;
#endif
array_init(&args);
for (uint32_t i = 0; i < param_count; i++) {
Z_TRY_ADDREF(params[i]);
add_next_index_zval(&args, &params[i]);
}
add_next_index_zval(&item, &args);
add_next_index_zval(&list, &item);
} ZEND_HASH_FOREACH_END();
add_assoc_zval(out, "shutdown", &list);
}
}
static void cryo_add_watch(zval *watch, const char *path)
{
char full[MAXPATHLEN];
if (!expand_filepath(path, full)) return;
zend_string *sha = cryo_sha1_of_file(full);
if (sha) add_assoc_str(watch, full, sha);
}
/* The core: returns CRYO_FROZEN (file written), or -1 with an exception, or
* -2 when the stack cannot be frozen and quiet (deferred restart retries). */
static bool cryo_startup_thaw = false; /* the thaw runs at startup (restart / warm boot) */
static zend_long cryo_do_freeze(zend_string *file, HashTable *opts, zend_execute_data *start, bool resume_at, bool quiet)
{
double t0 = cryo_host_clock_ms();
zend_execute_data *frames[CRYO_MAX_FRAMES];
uint32_t nf;
/* include without "stack": the values only (a sandbox, a checkpoint read back by cryo_thaw() which then returns) */
bool want_stack = true;
{
zval *inc0 = opts ? zend_hash_str_find(opts, "include", 7) : NULL, *e0;
if (inc0 && Z_TYPE_P(inc0) == IS_ARRAY) {
want_stack = false;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(inc0), e0) {
if (Z_TYPE_P(e0) == IS_STRING && zend_string_equals_literal(Z_STR_P(e0), "stack")) want_stack = true;
} ZEND_HASH_FOREACH_END();
}
}
nf = 0;
zend_string *why = want_stack ? cryo_collect_frames(start, resume_at, frames, &nf) : NULL;
if (why) {
if (quiet) { zend_string_release(why); return -2; }
zend_throw_exception_ex(cryo_ce_freeze_exception, 0, "Cannot freeze here: %s", ZSTR_VAL(why));
zend_string_release(why);
return -1;
}
for (uint32_t k = 0; k < nf; k++) {
zend_op_array *op = &frames[k]->func->op_array;
zval tmps;
array_init(&tmps);
why = cryo_save_tmps(frames[k], (uint32_t) (frames[k]->opline - op->opcodes), &tmps);
zval_ptr_dtor(&tmps);
if (why) {
if (quiet) { zend_string_release(why); return -2; }
zend_throw_exception_ex(cryo_ce_freeze_exception, 0, "Cannot freeze here: %s", ZSTR_VAL(why));
zend_string_release(why);
return -1;
}
}
cryo_hooks_run(&cryo_fz.on_freeze, NULL);
if (EG(exception)) return -1;
cryo_walk w;
cryo_walk_init(&w, opts);
zval *inc = opts ? zend_hash_str_find(opts, "include", 7) : NULL;
bool want_globals = true, want_statics = true;
if (inc && Z_TYPE_P(inc) == IS_ARRAY) {
want_globals = want_statics = false;
zval *e;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(inc), e) {
if (Z_TYPE_P(e) != IS_STRING) continue;
if (zend_string_equals_literal(Z_STR_P(e), "globals")) want_globals = true;
if (zend_string_equals_literal(Z_STR_P(e), "statics")) want_statics = true;
} ZEND_HASH_FOREACH_END();
}
zval data, meta, mframes, dframes;
array_init(&data);
array_init(&meta);
array_init(&mframes);
array_init(&dframes);
for (uint32_t k = nf; k-- > 0;) {
zend_execute_data *ex = frames[k];
zend_op_array *op = &ex->func->op_array;
uint32_t n = (uint32_t) (ex->opline - op->opcodes);
zval fm, fd, cvs, tmps;
array_init(&fm); array_init(&fd); array_init(&cvs); array_init(&tmps);
if (op->function_name) {
add_assoc_str(&fm, "func", zend_string_copy(op->function_name));
if (op->scope) add_assoc_str(&fm, "class", zend_string_copy(op->scope->name));
if (Z_TYPE(ex->This) != IS_OBJECT && Z_CE(ex->This)) add_assoc_str(&fm, "called", zend_string_copy(Z_CE(ex->This)->name));
}
add_assoc_str(&fm, "file", zend_string_copy(op->filename));
add_assoc_long(&fm, "opline", n);
add_assoc_long(&fm, "last", op->last);
add_assoc_long(&fm, "line", ex->opline->lineno);
add_assoc_long(&fm, "num_args", ZEND_CALL_NUM_ARGS(ex));
cryo_save_tmps(ex, n, &tmps);
if (op->function_name) {
for (int i = 0; i < op->last_var; i++) {
zval *cv = ZEND_CALL_VAR_NUM(ex, i);
if (Z_TYPE_P(cv) == IS_UNDEF) continue;
Z_TRY_ADDREF_P(cv);
zend_hash_update(Z_ARRVAL(cvs), op->vars[i], cv);
}
if (Z_TYPE(ex->This) == IS_OBJECT) {
zval t;
ZVAL_OBJ_COPY(&t, Z_OBJ(ex->This));
add_assoc_zval(&fd, "this", &t);
}
uint32_t num_args = ZEND_CALL_NUM_ARGS(ex);
if (num_args > op->num_args && (ZEND_CALL_INFO(ex) & ZEND_CALL_FREE_EXTRA_ARGS)) {
zval extra;
array_init(&extra);
zval *p = ZEND_CALL_VAR_NUM(ex, op->last_var + op->T);
for (uint32_t j = 0; j < num_args - op->num_args; j++) {
Z_TRY_ADDREF(p[j]);
add_next_index_zval(&extra, &p[j]);
}
add_assoc_zval(&fd, "extra", &extra);
}
}
add_assoc_zval(&fd, "cvs", &cvs);
add_assoc_zval(&fd, "tmps", &tmps);
add_next_index_zval(&mframes, &fm);
add_next_index_zval(&dframes, &fd);
}
{
zval g;
array_init(&g);
zend_string *key; zval *val;
/* the globals of {main} are always needed: its CVs live there */
ZEND_HASH_FOREACH_STR_KEY_VAL_IND(&EG(symbol_table), key, val) {
if (!key || Z_TYPE_P(val) == IS_UNDEF || cryo_is_superglobal(key) || cryo_walk_var_excluded(&w, key)) continue;
if (!want_globals) {
bool is_cv = false;
if (!nf) continue;
zend_op_array *mop = &frames[nf - 1]->func->op_array;
for (int i = 0; i < mop->last_var; i++) if (zend_string_equals(mop->vars[i], key)) is_cv = true;
if (!is_cv) continue;
}
Z_TRY_ADDREF_P(val);
zend_hash_update(Z_ARRVAL(g), key, val);
} ZEND_HASH_FOREACH_END();
add_assoc_zval(&data, "globals", &g);
}
if (want_statics) {
zval statics;
array_init(&statics);
zend_class_entry *ce;
ZEND_HASH_MAP_FOREACH_PTR(EG(class_table), ce) {
if (ce->type != ZEND_USER_CLASS || !ce->default_static_members_count) continue;
zval *members = CE_STATIC_MEMBERS(ce);
if (!members) continue;
zval props;
array_init(&props);
zend_property_info *pi;
zend_string *pkey; /* the key is the bare name (pi->name is mangled for private / protected) */
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, pkey, pi) {
if (!(pi->flags & ZEND_ACC_STATIC) || pi->ce != ce) continue;
zval *v = &members[pi->offset];
ZVAL_DEINDIRECT(v);
if (Z_TYPE_P(v) == IS_UNDEF || (pi->attributes && zend_get_attribute_str(pi->attributes, "cryotransient", 13))) continue;
Z_TRY_ADDREF_P(v);
zend_hash_update(Z_ARRVAL(props), pkey, v);
} ZEND_HASH_FOREACH_END();
if (zend_hash_num_elements(Z_ARRVAL(props))) zend_hash_update(Z_ARRVAL(statics), ce->name, &props);
else zval_ptr_dtor(&props);
} ZEND_HASH_FOREACH_END();
add_assoc_zval(&data, "statics", &statics);
zval fst;
cryo_capture_fstatics(&fst);
add_assoc_zval(&data, "fstatics", &fst);
}
{
zval consts;
array_init(&consts);
zend_constant *c;
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), c) {
if (ZEND_CONSTANT_MODULE_NUMBER(c) != PHP_USER_CONSTANT || !c->name) continue;
zval v;
ZVAL_COPY(&v, &c->value);
zend_hash_update(Z_ARRVAL(consts), c->name, &v);
} ZEND_HASH_FOREACH_END();
add_assoc_zval(&data, "constants", &consts);
}
{
zval h, hooks;
cryo_capture_handlers(&h);
add_assoc_zval(&data, "handlers", &h);
array_init(&hooks);
Z_TRY_ADDREF(cryo_fz.on_thaw);
add_assoc_zval(&hooks, "on_thaw", &cryo_fz.on_thaw);
Z_TRY_ADDREF(cryo_fz.on_freeze);
add_assoc_zval(&hooks, "on_freeze", &cryo_fz.on_freeze);
zval watch;
ZVAL_UNDEF(&watch);
cryo_watch_export(&watch);
if (Z_TYPE(watch) != IS_UNDEF) add_assoc_zval(&hooks, "memory_watch", &watch);
add_assoc_zval(&data, "hooks", &hooks);
zval res;
ZVAL_ARR(&res, zend_array_dup(&cryo_resource_handlers));
add_assoc_zval(&data, "resource_handlers", &res);
}
{
zval j;
ZVAL_UNDEF(&j);
cryo_engine_journal_export(&j);
if (Z_TYPE(j) != IS_UNDEF) add_assoc_zval(&data, "engine", &j);
}
add_assoc_zval(&data, "frames", &dframes);
zval *user = opts ? zend_hash_str_find(opts, "data", 4) : NULL;
if (user) { Z_TRY_ADDREF_P(user); add_assoc_zval(&data, "data", user); }
/* closures, resources, CryoAware, transient, exclude: swapped for markers */
cryo_walk_freeze(&w, &data);
if (Z_TYPE(w.typed) == IS_ARRAY) { /* markers of typed properties, put back at the thaw */
add_assoc_zval(&data, "typed", &w.typed);
ZVAL_UNDEF(&w.typed);
}
smart_str sdata = {0};
if (!EG(exception)) {
php_serialize_data_t sd;
PHP_VAR_SERIALIZE_INIT(sd);
php_var_serialize(&sdata, &data, &sd);
PHP_VAR_SERIALIZE_DESTROY(sd);
}
cryo_walk_undo(&w);
zval_ptr_dtor(&data);
if (EG(exception)) {
smart_str_free(&sdata);
zval_ptr_dtor(&meta);
zval_ptr_dtor(&mframes);
return -1;
}
smart_str_0(&sdata);
zend_op_array *main_op = nf ? &frames[nf - 1]->func->op_array : NULL;
zend_string *main_file = main_op ? main_op->filename : ZSTR_EMPTY_ALLOC();
add_assoc_long(&meta, "php", PHP_VERSION_ID);
add_assoc_str(&meta, "script", zend_string_copy(main_file));
add_assoc_string(&meta, "resume", !nf ? "none" : (resume_at ? "at" : "after"));
{
char cwd[MAXPATHLEN];
if (VCWD_GETCWD(cwd, sizeof(cwd))) add_assoc_string(&meta, "cwd", cwd);
}
zval inc_list, sources, watch;
array_init(&inc_list);
array_init(&sources);
array_init(&watch);
{
if (main_op) {
zend_string *s = cryo_sha1_of_file(ZSTR_VAL(main_file));
if (s) add_assoc_str(&sources, ZSTR_VAL(main_file), s); else add_assoc_null(&sources, ZSTR_VAL(main_file));
}
zend_string *key;
bool first = true;
ZEND_HASH_MAP_FOREACH_STR_KEY(&EG(included_files), key) {
bool primary = first;
first = false;
/* without the stack, the first file (the primary script) is not included again: it would run again */
if (!key || (main_op && zend_string_equals(key, main_file)) || (!main_op && primary)) continue;
add_next_index_str(&inc_list, zend_string_copy(key));
zend_string *h = cryo_sha1_of_file(ZSTR_VAL(key));
if (h) add_assoc_str(&sources, ZSTR_VAL(key), h); else add_assoc_null(&sources, ZSTR_VAL(key));
} ZEND_HASH_FOREACH_END();
/* warm boot invalidation: composer.lock and .env next to the script and in the cwd */
char dir[MAXPATHLEN];
strlcpy(dir, main_op ? ZSTR_VAL(main_file) : ".", sizeof(dir));
zend_dirname(dir, strlen(dir));
const char *names[] = { "composer.lock", ".env" };
for (int i = 0; i < 2; i++) {
char p[MAXPATHLEN];
snprintf(p, sizeof(p), "%s/%s", dir, names[i]);
cryo_add_watch(&watch, p);
cryo_add_watch(&watch, names[i]);
}
zval *w2 = opts ? zend_hash_str_find(opts, "watch", 5) : NULL, *e;
if (w2 && Z_TYPE_P(w2) == IS_ARRAY) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(w2), e) {
if (Z_TYPE_P(e) == IS_STRING) cryo_add_watch(&watch, Z_STRVAL_P(e));
} ZEND_HASH_FOREACH_END();
}
}
add_assoc_zval(&meta, "included", &inc_list);
add_assoc_zval(&meta, "sources", &sources);
add_assoc_zval(&meta, "watch", &watch);
add_assoc_zval(&meta, "frames", &mframes);
add_assoc_double(&meta, "time", (double) time(NULL));
add_assoc_long(&meta, "pid", (zend_long) getpid());
{
char host[256] = "";
gethostname(host, sizeof(host) - 1);
add_assoc_string(&meta, "host", host);
}
add_assoc_long(&meta, "thaw_count", cryo_fz.thaw_count);
add_assoc_long(&meta, "memory", (zend_long) zend_memory_usage(0));
cryo_metrics_sample_now();
zval outer, zs;
array_init(&outer);
add_next_index_zval(&outer, &meta);
ZVAL_STR(&zs, sdata.s ? sdata.s : ZSTR_EMPTY_ALLOC());
add_next_index_zval(&outer, &zs);
smart_str body = {0};
php_serialize_data_t sd;
PHP_VAR_SERIALIZE_INIT(sd);
php_var_serialize(&body, &outer, &sd);
PHP_VAR_SERIALIZE_DESTROY(sd);
zval_ptr_dtor(&outer);
smart_str_0(&body);
zend_result rc = cryo_write_container(file, body.s, opts);
if (rc == SUCCESS) {
zend_stat_t st;
cryo_stats.freezes++;
cryo_stats.last_freeze_ms = cryo_host_clock_ms() - t0;
cryo_stats.last_freeze_bytes = VCWD_STAT(ZSTR_VAL(file), &st) == 0 ? (uint64_t) st.st_size : ZSTR_LEN(body.s);
}
smart_str_free(&body);
return rc == SUCCESS ? CRYO_FROZEN : -1;
}
PHP_FUNCTION(cryo_freeze)
{
zend_string *file;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH_STR(file)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
zval *zmode = opts ? zend_hash_str_find(opts, "mode", 4) : NULL;
const char *mode = zmode && Z_TYPE_P(zmode) == IS_STRING ? Z_STRVAL_P(zmode) : "exit";
if (strcmp(mode, "exit") && strcmp(mode, "continue") && strcmp(mode, "fork") && strcmp(mode, "memory_image")) {
zend_argument_value_error(2, "option 'mode' must be exit, continue, fork or memory_image");
RETURN_THROWS();
}
if (strcmp(mode, "memory_image") == 0) {
#ifdef CRYO_BACKEND_WASM
RETURN_LONG(cryo_host_clone(1) >= 0 ? CRYO_FROZEN : CRYO_FROZEN);
#else
zend_throw_exception(cryo_ce_freeze_exception, "mode memory_image is WebAssembly only (the host copies the linear memory)", 0);
RETURN_THROWS();
#endif
}
if (strcmp(mode, "fork") == 0) {
if (ZSTR_LEN(file)) {
if (cryo_do_freeze(file, opts, EG(current_execute_data)->prev_execute_data, false, false) < 0) RETURN_THROWS();
}
if (!cryo_fork_checkpoint) {
zend_throw_exception(cryo_ce_freeze_exception, "mode fork is not available in this build", 0);
RETURN_THROWS();
}
zval *variant = opts ? zend_hash_str_find(opts, "fork", 4) : NULL;
zend_string *v = variant && Z_TYPE_P(variant) == IS_STRING ? zend_string_copy(Z_STR_P(variant)) : zend_string_init("child", 5, 0);
zend_long r = cryo_fork_checkpoint(v, opts);
zend_string_release(v);
if (EG(exception)) RETURN_THROWS();
RETURN_LONG(r);
}
if (cryo_do_freeze(file, opts, EG(current_execute_data)->prev_execute_data, false, false) < 0) RETURN_THROWS();
if (strcmp(mode, "exit") == 0) {
php_output_end_all();
zend_throw_unwind_exit();
RETURN_THROWS();
}
RETURN_LONG(CRYO_FROZEN);
}
/* cryo_restart(): freeze, then start PHP again on the same command line, which
* thaws the file before {main} runs. From a cryo_memory_watch() callback the
* freeze waits for the next safe point of the interrupted code. */
PHP_FUNCTION(cryo_restart)
{
zend_string *file;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH_STR(file)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
if (cryo_watch_in_callback) {
if (cryo_fz.restart_file) zend_string_release(cryo_fz.restart_file);
zval_ptr_dtor(&cryo_fz.restart_opts);
cryo_fz.restart_file = zend_string_copy(file);
if (opts) ZVAL_ARR(&cryo_fz.restart_opts, zend_array_dup(opts)); else ZVAL_UNDEF(&cryo_fz.restart_opts);
cryo_fz.restart_pending = true;
cryo_fz.restart_tries = 0;
return;
}
zend_long r = cryo_do_freeze(file, opts, EG(current_execute_data)->prev_execute_data, false, false);
if (r < 0) RETURN_THROWS();
char full[MAXPATHLEN];
cryo_exec_self(expand_filepath(ZSTR_VAL(file), full) ? full : ZSTR_VAL(file));
RETURN_THROWS(); /* only reached when exec failed (an exception is set) */
}
/* ------------------------------------------------------------------------ */
/* Torture test (cryo.torture=K): freeze at the K-th safe point and exit */
/* (bin/cryo-torture runs K = 1, 2, 3... and compares with a normal run) */
/* ------------------------------------------------------------------------ */
static zend_long cryo_torture_k = 0, cryo_torture_seen = 0;
void cryo_torture_tick(zend_execute_data *ex)
{
if (!cryo_torture_k || EG(exception)) return;
zend_atomic_bool_store(&EG(vm_interrupt), true); /* wake up again at the next safe point */
if (!ex || !ex->func || !ZEND_USER_CODE(ex->func->type)) return;
if (++cryo_torture_seen < cryo_torture_k) return;
const char *f = INI_STR("cryo.torture_file");
if (!f || !*f) return;
zend_string *file = zend_string_init(f, strlen(f), 0);
zend_long r = cryo_do_freeze(file, NULL, ex, true, true);
zend_string_release(file);
if (r == -2) return; /* not freezable here: try the next safe point */
cryo_torture_k = 0;
if (r < 0) return;
fprintf(stderr, "[cryo-torture] frozen at safe point %ld\n", (long) cryo_torture_seen);
php_output_end_all();
zend_throw_unwind_exit();
}
bool cryo_restart_requested(void)
{
return cryo_fz.restart_pending;
}
void cryo_freeze_deferred(zend_execute_data *ex)
{
if (!cryo_fz.restart_pending || !ex || !ex->func || !ZEND_USER_CODE(ex->func->type)) return;
HashTable *opts = Z_TYPE(cryo_fz.restart_opts) == IS_ARRAY ? Z_ARRVAL(cryo_fz.restart_opts) : NULL;
zend_long r = cryo_do_freeze(cryo_fz.restart_file, opts, ex, true, true);
if (r == -2) {
if (++cryo_fz.restart_tries == 100000) {
php_error_docref(NULL, E_WARNING, "cryo_restart(): no safe point to freeze was reached, restart abandoned");
cryo_fz.restart_pending = false;
}
return;
}
cryo_fz.restart_pending = false;
if (r < 0) return;
char full[MAXPATHLEN];
cryo_exec_self(expand_filepath(ZSTR_VAL(cryo_fz.restart_file), full) ? full : ZSTR_VAL(cryo_fz.restart_file));
}
/* ------------------------------------------------------------------------ */
/* Thaw */
/* ------------------------------------------------------------------------ */
static zend_result cryo_include_again(zval *meta)
{
zval *cwd = cryo_get(meta, "cwd");
if (cwd && Z_TYPE_P(cwd) == IS_STRING && VCWD_CHDIR(Z_STRVAL_P(cwd)) != 0) {
php_error_docref(NULL, E_WARNING, "Cannot change to the frozen working directory %s", Z_STRVAL_P(cwd));
}
zval *inc = cryo_get(meta, "included"), *f;
if (!inc || Z_TYPE_P(inc) != IS_ARRAY) return SUCCESS;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(inc), f) {
if (Z_TYPE_P(f) != IS_STRING || zend_hash_exists(&EG(included_files), Z_STR_P(f))) continue;
smart_str code = {0};
smart_str_appends(&code, "require_once '");
for (size_t i = 0; i < Z_STRLEN_P(f); i++) {
char c = Z_STRVAL_P(f)[i];
if (c == '\'' || c == '\\') smart_str_appendc(&code, '\\');
smart_str_appendc(&code, c);
}
smart_str_appends(&code, "';");
smart_str_0(&code);
zend_result rc = zend_eval_stringl(ZSTR_VAL(code.s), ZSTR_LEN(code.s), NULL, "cryo_thaw");
smart_str_free(&code);
if (rc == FAILURE || EG(exception)) return FAILURE;
} ZEND_HASH_FOREACH_END();
return SUCCESS;
}
static void cryo_bind_declarations(zend_op_array *op, uint32_t upto)
{
for (uint32_t i = 0; i < upto && i < op->last; i++) {
zend_op *opline = &op->opcodes[i];
if (opline->opcode == ZEND_DECLARE_CLASS || opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
zval *lcname = RT_CONSTANT(opline, opline->op1);
zval *rtd = lcname + 1;
if (zend_hash_exists(EG(class_table), Z_STR_P(lcname)) || !zend_hash_exists(EG(class_table), Z_STR_P(rtd))) continue;
zend_string *parent = opline->op2_type == IS_CONST ? Z_STR_P(RT_CONSTANT(opline, opline->op2)) : NULL;
if (parent && !zend_hash_exists(EG(class_table), parent)) continue;
do_bind_class(lcname, parent);
if (EG(exception)) return;
} else if (opline->opcode == ZEND_DECLARE_FUNCTION) {
zval *lcname = RT_CONSTANT(opline, opline->op1);
if (zend_hash_exists(EG(function_table), Z_STR_P(lcname)) || !op->dynamic_func_defs
|| opline->op2.num >= op->num_dynamic_func_defs) continue;
do_bind_function((zend_function *) op->dynamic_func_defs[opline->op2.num], lcname);
}
}
}
static zend_function *cryo_lookup_func(zend_string *name, zend_string *cls)
{
HashTable *table = EG(function_table);
if (cls) {
zend_class_entry *ce = zend_lookup_class(cls);
if (!ce) return NULL;
table = &ce->function_table;
}
return zend_hash_find_ptr_lc(table, name);
}
/* ------------------------------------------------------------------------ */
/* Suspended generators (cryo_walk.c calls these) */
/* ------------------------------------------------------------------------ */
/* A generator suspended at a yield (or not started): its function, position,
* variables, temporaries, current key/value. SUCCESS, or FAILURE when it
* cannot be carried (running, finished, inside a yield from, a closure). */
zend_result cryo_generator_describe(zend_object *obj, zval *data)
{
zend_generator *g = (zend_generator *) obj;
zend_execute_data *ex = g->execute_data;
if (!ex || (g->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) || g->frozen_call_stack
|| Z_TYPE(g->values) != IS_UNDEF || g->node.parent || g->node.children) {
return FAILURE;
}
zend_op_array *op = &ex->func->op_array;
if (!op->function_name || (op->fn_flags & ZEND_ACC_CLOSURE)) return FAILURE;
uint32_t num_args = ZEND_CALL_NUM_ARGS(ex);
if (num_args > op->num_args) return FAILURE;
uint32_t n = (uint32_t) (ex->opline - op->opcodes);
zval tmps, cvs;
array_init(&tmps);
zend_string *why = cryo_save_tmps(ex, n, &tmps);
if (why) { zend_string_release(why); zval_ptr_dtor(&tmps); return FAILURE; }
array_init(data);
add_assoc_str(data, "func", zend_string_copy(op->function_name));
if (op->scope) add_assoc_str(data, "class", zend_string_copy(op->scope->name));
if (Z_TYPE(ex->This) != IS_OBJECT && Z_CE(ex->This)) add_assoc_str(data, "called", zend_string_copy(Z_CE(ex->This)->name));
add_assoc_str(data, "file", zend_string_copy(op->filename));
add_assoc_long(data, "last", op->last);
add_assoc_long(data, "opline", n);
add_assoc_long(data, "num_args", num_args);
add_assoc_long(data, "flags", g->flags & (ZEND_GENERATOR_AT_FIRST_YIELD | ZEND_GENERATOR_DO_INIT));
add_assoc_long(data, "largest_key", g->largest_used_integer_key);
if (g->send_target) {
add_assoc_long(data, "send", (zend_long) ((char *) g->send_target - (char *) ex));
}
array_init(&cvs);
for (int i = 0; i < op->last_var; i++) {
zval *cv = ZEND_CALL_VAR_NUM(ex, i);
if (Z_TYPE_P(cv) == IS_UNDEF) continue;
Z_TRY_ADDREF_P(cv);
zend_hash_update(Z_ARRVAL(cvs), op->vars[i], cv);
}
add_assoc_zval(data, "cvs", &cvs);
add_assoc_zval(data, "tmps", &tmps);
if (Z_TYPE(ex->This) == IS_OBJECT) {
zval t;
ZVAL_OBJ_COPY(&t, Z_OBJ(ex->This));
add_assoc_zval(data, "this", &t);
}
zval v;
if (Z_TYPE(g->value) != IS_UNDEF) { ZVAL_COPY(&v, &g->value); add_assoc_zval(data, "value", &v); }
if (Z_TYPE(g->key) != IS_UNDEF) { ZVAL_COPY(&v, &g->key); add_assoc_zval(data, "key", &v); }
return SUCCESS;
}
/* Rebuilds the generator: a frame made like ZEND_GENERATOR_CREATE does (on the
* VM stack, then copied to the heap), resumed at the saved position. */
zend_result cryo_generator_rebuild(zval *data, zval *out)
{
zval *name = cryo_get(data, "func"), *cls = cryo_get(data, "class"), *file = cryo_get(data, "file");
zval *last = cryo_get(data, "last"), *zn = cryo_get(data, "opline"), *zna = cryo_get(data, "num_args");
if (!name || !file || !last || !zn || Z_TYPE_P(name) != IS_STRING || Z_TYPE_P(file) != IS_STRING) return FAILURE;
zend_function *fbc = cryo_lookup_func(Z_STR_P(name), cls && Z_TYPE_P(cls) == IS_STRING ? Z_STR_P(cls) : NULL);
if (!fbc || fbc->type != ZEND_USER_FUNCTION || !(fbc->common.fn_flags & ZEND_ACC_GENERATOR)
|| !zend_string_equals(fbc->op_array.filename, Z_STR_P(file)) || fbc->op_array.last != (uint32_t) Z_LVAL_P(last)
|| (uint32_t) Z_LVAL_P(zn) >= fbc->op_array.last) {
return FAILURE;
}
zend_op_array *op = &fbc->op_array;
uint32_t num_args = zna ? (uint32_t) Z_LVAL_P(zna) : 0;
if (num_args > op->num_args) return FAILURE;
zval *this_zv = cryo_get(data, "this"), *called = cryo_get(data, "called");
uint32_t info = ZEND_CALL_NESTED_FUNCTION;
void *obj_or_scope;
if (this_zv && Z_TYPE_P(this_zv) == IS_OBJECT) {
info |= ZEND_CALL_HAS_THIS;
obj_or_scope = Z_OBJ_P(this_zv);
} else if (called && Z_TYPE_P(called) == IS_STRING) {
obj_or_scope = zend_lookup_class(Z_STR_P(called));
} else {
obj_or_scope = fbc->common.scope;
}
zend_execute_data *saved_current = EG(current_execute_data);
zend_execute_data *call = zend_vm_stack_push_call_frame(info, fbc, num_args, obj_or_scope);
for (uint32_t a = 0; a < num_args; a++) ZVAL_UNDEF(ZEND_CALL_ARG(call, a + 1));
call->prev_execute_data = NULL;
zend_init_func_execute_data(call, op, NULL);
EG(current_execute_data) = saved_current;
zval *cvs = cryo_get(data, "cvs");
for (int v = 0; v < op->last_var; v++) {
zval *slot = ZEND_CALL_VAR_NUM(call, v);
zval_ptr_dtor(slot);
ZVAL_UNDEF(slot);
zval *sv = cvs && Z_TYPE_P(cvs) == IS_ARRAY ? zend_hash_find(Z_ARRVAL_P(cvs), op->vars[v]) : NULL;
if (sv) ZVAL_COPY(slot, sv);
}
for (uint32_t t = 0; t < op->T; t++) ZVAL_UNDEF(ZEND_CALL_VAR_NUM(call, op->last_var + t));
cryo_restore_tmps(call, cryo_get(data, "tmps"));
uint32_t used_stack = (ZEND_CALL_FRAME_SLOT + op->last_var + op->T) * sizeof(zval);
zend_execute_data *gex = (zend_execute_data *) emalloc(used_stack);
memcpy(gex, call, used_stack);
zend_vm_stack_free_call_frame(call); /* the values moved to the heap copy */
object_init_ex(out, zend_ce_generator);
zend_generator *g = (zend_generator *) Z_OBJ_P(out);
g->func = fbc;
g->execute_data = gex;
g->frozen_call_stack = NULL;
g->execute_fake.opline = NULL;
g->execute_fake.func = NULL;
g->execute_fake.prev_execute_data = NULL;
ZVAL_OBJ(&g->execute_fake.This, (zend_object *) g);
gex->opline = op->opcodes + Z_LVAL_P(zn);
gex->return_value = (zval *) g;
gex->prev_execute_data = NULL;
gex->call = NULL;
uint32_t call_info = Z_TYPE_INFO(gex->This);
if (info & ZEND_CALL_HAS_THIS) {
ZEND_ADD_CALL_FLAG_EX(call_info, ZEND_CALL_RELEASE_THIS);
Z_ADDREF(gex->This);
}
ZEND_ADD_CALL_FLAG_EX(call_info, (ZEND_CALL_TOP_FUNCTION | ZEND_CALL_ALLOCATED | ZEND_CALL_GENERATOR));
Z_TYPE_INFO(gex->This) = call_info;
zval *v = cryo_get(data, "value"), *k = cryo_get(data, "key"), *fl = cryo_get(data, "flags"),
*lk = cryo_get(data, "largest_key"), *send = cryo_get(data, "send");
if (v) ZVAL_COPY(&g->value, v);
if (k) ZVAL_COPY(&g->key, k);
if (lk) g->largest_used_integer_key = Z_LVAL_P(lk);
if (fl) g->flags = (uint8_t) Z_LVAL_P(fl);
g->send_target = send && Z_TYPE_P(send) == IS_LONG ? (zval *) ((char *) gex + Z_LVAL_P(send)) : NULL;
if (g->send_target) ZVAL_NULL(g->send_target);
return SUCCESS;
}
static void cryo_restore_handlers(zval *h)
{
zval *e;
if ((e = cryo_get(h, "error"))) {
zval *types = cryo_get(h, "error_types");
zval_ptr_dtor(&EG(user_error_handler));
ZVAL_COPY(&EG(user_error_handler), e);
EG(user_error_handler_error_reporting) = types ? (int) Z_LVAL_P(types) : E_ALL;
}
if ((e = cryo_get(h, "exception"))) {
zval_ptr_dtor(&EG(user_exception_handler));
ZVAL_COPY(&EG(user_exception_handler), e);
}
zval *list = cryo_get(h, "shutdown"), *item;
if (list && Z_TYPE_P(list) == IS_ARRAY) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(list), item) {
zval *cb = zend_hash_index_find(Z_ARRVAL_P(item), 0), *args = zend_hash_index_find(Z_ARRVAL_P(item), 1);
php_shutdown_function_entry entry;
memset(&entry, 0, sizeof(entry));
uint32_t n = args && Z_TYPE_P(args) == IS_ARRAY ? zend_hash_num_elements(Z_ARRVAL_P(args)) : 0;
zval *argv = n ? safe_emalloc(n, sizeof(zval), 0) : NULL, *a;
uint32_t i = 0;
if (n) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), a) { ZVAL_COPY(&argv[i++], a); } ZEND_HASH_FOREACH_END();
}
#if PHP_VERSION_ID >= 80500
if (!cb || !zend_is_callable_ex(cb, NULL, 0, NULL, &entry.fci_cache, NULL)) {
for (i = 0; i < n; i++) zval_ptr_dtor(&argv[i]);
if (argv) efree(argv);
continue;
}
zend_fcc_addref(&entry.fci_cache);
entry.params = argv;
entry.param_count = n;
#else
if (!cb || zend_fcall_info_init(cb, 0, &entry.fci, &entry.fci_cache, NULL, NULL) == FAILURE) {
for (i = 0; i < n; i++) zval_ptr_dtor(&argv[i]);
if (argv) efree(argv);
continue;
}
Z_TRY_ADDREF(entry.fci.function_name);
zend_fcall_info_argp(&entry.fci, n, argv);
for (i = 0; i < n; i++) zval_ptr_dtor(&argv[i]);
if (argv) efree(argv);
#endif
append_user_shutdown_function(&entry);
} ZEND_HASH_FOREACH_END();
}
}
static zend_result cryo_restore_values(zval *data)
{
zend_string *key; zval *val;
zval *consts = cryo_get(data, "constants"), *globals = cryo_get(data, "globals"), *statics = cryo_get(data, "statics");
zval *fst = cryo_get(data, "fstatics"), *handlers = cryo_get(data, "handlers"), *hooks = cryo_get(data, "hooks");
zval *res = cryo_get(data, "resource_handlers"), *engine = cryo_get(data, "engine");
if (engine) {
cryo_engine_journal_replay(engine);
if (EG(exception)) return FAILURE;
}
if (consts && Z_TYPE_P(consts) == IS_ARRAY) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(consts), key, val) {
if (!key || zend_get_constant_ptr(key)) continue;
zend_constant c;
memset(&c, 0, sizeof(c)); /* later PHP versions add fields (filename, attributes) */
ZVAL_COPY(&c.value, val);
ZEND_CONSTANT_SET_FLAGS(&c, CONST_CS, PHP_USER_CONSTANT);
c.name = zend_string_copy(key);
zend_register_constant(&c);
} ZEND_HASH_FOREACH_END();
}
if (globals && Z_TYPE_P(globals) == IS_ARRAY) {
/* the running frame bound to the global symbol table (if any) sees the new values */
zend_execute_data *bound = NULL;
for (zend_execute_data *ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
if (ex->func && ZEND_USER_CODE(ex->func->type) && (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
if (ex->symbol_table == &EG(symbol_table) && ex->func->op_array.last_var) bound = ex;
break;
}
}
if (bound) zend_detach_symbol_table(bound);
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(globals), key, val) {
if (!key) continue;
Z_TRY_ADDREF_P(val);
zend_hash_update(&EG(symbol_table), key, val);
} ZEND_HASH_FOREACH_END();
if (bound) zend_attach_symbol_table(bound);
}
if (statics && Z_TYPE_P(statics) == IS_ARRAY) {
zval *props;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(statics), key, props) {
zend_class_entry *ce = key ? zend_lookup_class(key) : NULL;
if (!ce || Z_TYPE_P(props) != IS_ARRAY) continue;
zend_string *pname;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(props), pname, val) {
if (!pname) continue;
zend_update_static_property_ex(ce, pname, val);
if (EG(exception)) return FAILURE;
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
}
if (fst && Z_TYPE_P(fst) == IS_ARRAY) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(fst), key, val) {
if (!key || Z_TYPE_P(val) != IS_ARRAY) continue;
const char *sep = strstr(ZSTR_VAL(key), "::");
zend_function *f;
if (sep) {
zend_string *cls = zend_string_init(ZSTR_VAL(key), sep - ZSTR_VAL(key), 0);
zend_string *fn = zend_string_init(sep + 2, strlen(sep + 2), 0);
f = cryo_lookup_func(fn, cls);
zend_string_release(cls); zend_string_release(fn);
} else {
f = cryo_lookup_func(key, NULL);
}
if (!f || f->type != ZEND_USER_FUNCTION || !f->op_array.static_variables) continue;
HashTable *ht = ZEND_MAP_PTR_GET(f->op_array.static_variables_ptr);
if (!ht) {
ht = zend_array_dup(f->op_array.static_variables);
ZEND_MAP_PTR_SET(f->op_array.static_variables_ptr, ht);
}
zend_string *vk; zval *vv;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(val), vk, vv) {
if (!vk) continue;
Z_TRY_ADDREF_P(vv);
zend_hash_update(ht, vk, vv);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
}
if (handlers) cryo_restore_handlers(handlers);
if (res && Z_TYPE_P(res) == IS_ARRAY) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(res), key, val) {
if (key) { Z_TRY_ADDREF_P(val); zend_hash_update(&cryo_resource_handlers, key, val); }
} ZEND_HASH_FOREACH_END();
}
if (hooks) {
zval *ot = cryo_get(hooks, "on_thaw"), *of = cryo_get(hooks, "on_freeze");
if (ot && Z_TYPE_P(ot) == IS_ARRAY) { zval_ptr_dtor(&cryo_fz.on_thaw); ZVAL_COPY(&cryo_fz.on_thaw, ot); }
if (of && Z_TYPE_P(of) == IS_ARRAY) { zval_ptr_dtor(&cryo_fz.on_freeze); ZVAL_COPY(&cryo_fz.on_freeze, of); }
cryo_watch_import(cryo_get(hooks, "memory_watch"));
}
return EG(exception) ? FAILURE : SUCCESS;
}
/* --- CryoThawContext: paths "GLOBALS.a.b", "Class::$p.x", "frame.N.$v" --- */
typedef void (*cryo_scan_cb)(zval *slot, const char *path, void *arg);
static void cryo_scan(zval *slot, smart_str *path, HashTable *seen, cryo_scan_cb cb, void *arg, uint32_t depth)
{
if (depth > 256) return;
smart_str_0(path);
cb(slot, ZSTR_VAL(path->s), arg);
zval *v = slot;
ZVAL_DEREF(v);
size_t mark = ZSTR_LEN(path->s);
if (Z_TYPE_P(v) == IS_ARRAY) {
if (zend_hash_index_exists(seen, (zend_ulong)(uintptr_t) Z_ARR_P(v))) return;
SEPARATE_ARRAY(v);
zend_hash_index_add_new(seen, (zend_ulong)(uintptr_t) Z_ARR_P(v), &EG(uninitialized_zval));
zend_string *k; zend_ulong i; zval *e;
ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(v), i, k, e) {
smart_str_appendc(path, '.');
if (k) smart_str_append(path, k); else smart_str_append_unsigned(path, i);
cryo_scan(e, path, seen, cb, arg, depth + 1);
ZSTR_LEN(path->s) = mark;
} ZEND_HASH_FOREACH_END();
} else if (Z_TYPE_P(v) == IS_OBJECT && (Z_OBJCE_P(v)->type == ZEND_USER_CLASS || Z_OBJCE_P(v) == zend_standard_class_def)) {
zend_object *o = Z_OBJ_P(v);
if (zend_hash_index_exists(seen, (zend_ulong)(uintptr_t) o)) return;
zend_hash_index_add_new(seen, (zend_ulong)(uintptr_t) o, &EG(uninitialized_zval));
zend_property_info *pi;
ZEND_HASH_MAP_FOREACH_PTR(&o->ce->properties_info, pi) {
if (pi->flags & ZEND_ACC_STATIC) continue;
smart_str_appendc(path, '.');
smart_str_appends(path, zend_get_unmangled_property_name(pi->name));
cryo_scan(OBJ_PROP(o, pi->offset), path, seen, cb, arg, depth + 1);
ZSTR_LEN(path->s) = mark;
} ZEND_HASH_FOREACH_END();
if (o->properties) {
zend_string *k; zval *e;
ZEND_HASH_FOREACH_STR_KEY_VAL(o->properties, k, e) {
if (!k || Z_TYPE_P(e) == IS_INDIRECT) continue;
smart_str_appendc(path, '.');
smart_str_append(path, k);
cryo_scan(e, path, seen, cb, arg, depth + 1);
ZSTR_LEN(path->s) = mark;
} ZEND_HASH_FOREACH_END();
}
}
}
/* every live root: globals, static properties, frame variables of the thaw */
static void cryo_scan_roots(cryo_scan_cb cb, void *arg)
{
HashTable seen;
zend_hash_init(&seen, 64, NULL, NULL, 0);
smart_str path = {0};
zend_string *k; zval *v;
ZEND_HASH_FOREACH_STR_KEY_VAL_IND(&EG(symbol_table), k, v) {
if (!k || cryo_is_superglobal(k)) continue;
smart_str_free(&path);
smart_str_appends(&path, "GLOBALS.");
smart_str_append(&path, k);
cryo_scan(v, &path, &seen, cb, arg, 0);
} ZEND_HASH_FOREACH_END();
zend_class_entry *ce;
ZEND_HASH_MAP_FOREACH_PTR(EG(class_table), ce) {
if (ce->type != ZEND_USER_CLASS || !CE_STATIC_MEMBERS(ce)) continue;
zend_property_info *pi;
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, pi) {
if (!(pi->flags & ZEND_ACC_STATIC) || pi->ce != ce) continue;
zval *sv = &CE_STATIC_MEMBERS(ce)[pi->offset];
ZVAL_DEINDIRECT(sv);
smart_str_free(&path);
smart_str_append(&path, ce->name);
smart_str_appends(&path, "::$");
smart_str_appends(&path, zend_get_unmangled_property_name(pi->name));
cryo_scan(sv, &path, &seen, cb, arg, 0);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
zval *frames = cryo_fz.hook_data ? cryo_get(cryo_fz.hook_data, "frames") : NULL, *fd;
zend_ulong idx;
if (frames && Z_TYPE_P(frames) == IS_ARRAY) {
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(frames), idx, fd) {
zval *cvs = cryo_get(fd, "cvs");
if (!cvs || Z_TYPE_P(cvs) != IS_ARRAY) continue;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(cvs), k, v) {
if (!k) continue;
smart_str_free(&path);
smart_str_appends(&path, "frame.");
smart_str_append_unsigned(&path, idx);
smart_str_appends(&path, ".$");
smart_str_append(&path, k);
cryo_scan(v, &path, &seen, cb, arg, 0);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
}
smart_str_free(&path);
zend_hash_destroy(&seen);
}
typedef struct { const char *path; zval *value; bool unset; bool done; } cryo_set_arg;
typedef struct { zend_class_entry *ce; zval *factory; HashTable map; } cryo_replace_arg;
static void cryo_cb_pending(zval *slot, const char *path, void *arg)
{
zval *v = slot; ZVAL_DEREF(v);
if (Z_TYPE_P(v) == IS_OBJECT && Z_OBJCE_P(v) == cryo_ce_pending) {
zval rv, *d = zend_read_property(cryo_ce_pending, Z_OBJ_P(v), "desc", 4, 1, &rv), *t = zend_read_property(cryo_ce_pending, Z_OBJ_P(v), "type", 4, 1, &rv);
zval e;
array_init(&e);
Z_TRY_ADDREF_P(t); add_assoc_zval(&e, "type", t);
Z_TRY_ADDREF_P(d); add_assoc_zval(&e, "desc", d);
add_assoc_zval((zval *) arg, path, &e);
}
}
static void cryo_cb_set(zval *slot, const char *path, void *arg)
{
cryo_set_arg *a = arg;
if (a->done || strcmp(path, a->path) != 0) return;
zval *v = slot; ZVAL_DEREF(v);
zval_ptr_dtor(v);
if (a->unset) ZVAL_NULL(v); else ZVAL_COPY(v, a->value);
a->done = true;
}
static void cryo_cb_replace(zval *slot, const char *path, void *arg)
{
cryo_replace_arg *a = arg;
zval *v = slot; ZVAL_DEREF(v);
if (Z_TYPE_P(v) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(v), a->ce)) return;
zval *done = zend_hash_index_find(&a->map, (zend_ulong)(uintptr_t) Z_OBJ_P(v)), nv;
if (done) {
ZVAL_COPY(&nv, done);
} else {
zval old;
ZVAL_COPY(&old, v);
call_user_function(NULL, NULL, a->factory, &nv, 1, &old);
zend_hash_index_add(&a->map, (zend_ulong)(uintptr_t) Z_OBJ(old), &nv);
Z_TRY_ADDREF(nv);
zval_ptr_dtor(&old);
}
zval_ptr_dtor(v);
ZVAL_COPY_VALUE(v, &nv);
(void) path;
}
ZEND_METHOD(CryoThawContext, pending)
{
ZEND_PARSE_PARAMETERS_NONE();
array_init(return_value);
cryo_scan_roots(cryo_cb_pending, return_value);
}
ZEND_METHOD(CryoThawContext, resolve)
{
zend_string *path; zval *value;
ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(path) Z_PARAM_ZVAL(value) ZEND_PARSE_PARAMETERS_END();
cryo_set_arg a = { ZSTR_VAL(path), value, false, false };
cryo_scan_roots(cryo_cb_set, &a);
RETURN_BOOL(a.done);
}
ZEND_METHOD(CryoThawContext, unset)
{
zend_string *path;
ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(path) ZEND_PARSE_PARAMETERS_END();
/* a whole global goes away; anything deeper becomes null */
if (strncmp(ZSTR_VAL(path), "GLOBALS.", 8) == 0 && !strchr(ZSTR_VAL(path) + 8, '.')) {
RETURN_BOOL(zend_hash_str_del(&EG(symbol_table), ZSTR_VAL(path) + 8, ZSTR_LEN(path) - 8) == SUCCESS);
}
cryo_set_arg a = { ZSTR_VAL(path), NULL, true, false };
cryo_scan_roots(cryo_cb_set, &a);
RETURN_BOOL(a.done);
}
ZEND_METHOD(CryoThawContext, replaceClass)
{
zend_string *cls; zval *factory;
ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(cls) Z_PARAM_ZVAL(factory) ZEND_PARSE_PARAMETERS_END();
zend_class_entry *ce = zend_lookup_class(cls);
if (!ce) RETURN_LONG(0);
cryo_replace_arg a;
a.ce = ce; a.factory = factory;
zend_hash_init(&a.map, 8, NULL, ZVAL_PTR_DTOR, 0);
cryo_scan_roots(cryo_cb_replace, &a);
RETVAL_LONG(zend_hash_num_elements(&a.map));
zend_hash_destroy(&a.map);
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ctx_pending, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ctx_resolve, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, path, 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_ctx_unset, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ctx_replace, 0, 2, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, class, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, factory, IS_CALLABLE, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry cryo_ctx_methods[] = {
ZEND_ME(CryoThawContext, pending, arginfo_ctx_pending, ZEND_ACC_PUBLIC)
ZEND_ME(CryoThawContext, resolve, arginfo_ctx_resolve, ZEND_ACC_PUBLIC)
ZEND_ME(CryoThawContext, unset, arginfo_ctx_unset, ZEND_ACC_PUBLIC)
ZEND_ME(CryoThawContext, replaceClass, arginfo_ctx_replace, ZEND_ACC_PUBLIC)
ZEND_FE_END
};
/* After the values are back: hooks, then no CryoPending may be left. */
static zend_result cryo_after_restore(zval *meta, zend_string *file, zval *data)
{
cryo_fz.thawed = true;
zval *tc = cryo_get(meta, "thaw_count");
cryo_fz.thaw_count = (tc && Z_TYPE_P(tc) == IS_LONG ? Z_LVAL_P(tc) : 0) + 1;
zval_ptr_dtor(&cryo_fz.origin);
array_init(&cryo_fz.origin);
add_assoc_str(&cryo_fz.origin, "file", zend_string_copy(file));
zval *v;
const char *keys[] = { "time", "pid", "host", "script", NULL };
for (int i = 0; keys[i]; i++) {
if ((v = cryo_get(meta, keys[i]))) { Z_TRY_ADDREF_P(v); add_assoc_zval(&cryo_fz.origin, keys[i], v); }
}
cryo_stats.thaws++;
if (cryo_startup_thaw) cryo_stats.restarts++;
zval *mem = cryo_get(meta, "memory");
if (mem && Z_TYPE_P(mem) == IS_LONG && (size_t) Z_LVAL_P(mem) > zend_memory_usage(0)) {
cryo_stats.bytes_reclaimed += (uint64_t) Z_LVAL_P(mem) - zend_memory_usage(0);
}
cryo_fz.hook_data = data;
cryo_hooks_run_thaw();
if (!EG(exception)) {
zval left;
array_init(&left);
cryo_scan_roots(cryo_cb_pending, &left);
if (zend_hash_num_elements(Z_ARRVAL(left))) {
smart_str paths = {0};
zend_string *k;
ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL(left), k) {
if (paths.s) smart_str_appends(&paths, ", ");
smart_str_append(&paths, k);
} ZEND_HASH_FOREACH_END();
smart_str_0(&paths);
zend_throw_exception_ex(cryo_ce_thaw_exception, 0,
"Values could not be restored (register a handler with cryo_register_resource() or resolve them in cryo_on_thaw): %s",
ZSTR_VAL(paths.s));
smart_str_free(&paths);
}
zval_ptr_dtor(&left);
}
cryo_fz.hook_data = NULL;
return EG(exception) ? FAILURE : SUCCESS;
}
/* Rebuilds the frames above main_ex and runs them to the end of {main}. */
static zend_result cryo_resume(zend_execute_data *main_ex, zval *meta, zval *data)
{
zval *mframes = cryo_get(meta, "frames"), *dframes = cryo_get(data, "frames");
if (!mframes || !dframes || Z_TYPE_P(mframes) != IS_ARRAY || Z_TYPE_P(dframes) != IS_ARRAY
|| !zend_hash_num_elements(Z_ARRVAL_P(mframes))
|| zend_hash_num_elements(Z_ARRVAL_P(mframes)) != zend_hash_num_elements(Z_ARRVAL_P(dframes))) {
zend_throw_exception(cryo_ce_thaw_exception, "Corrupted frames", 0);
return FAILURE;
}
zval *zresume = cryo_get(meta, "resume");
bool resume_at = zresume && Z_TYPE_P(zresume) == IS_STRING && zend_string_equals_literal(Z_STR_P(zresume), "at");
uint32_t nf = zend_hash_num_elements(Z_ARRVAL_P(mframes));
zend_op_array *op = &main_ex->func->op_array;
zval *fm0 = zend_hash_index_find(Z_ARRVAL_P(mframes), 0);
zval *last0 = cryo_get(fm0, "last"), *n0 = cryo_get(fm0, "opline");
if (!last0 || !n0 || op->last != (uint32_t) Z_LVAL_P(last0) || (uint32_t) Z_LVAL_P(n0) >= op->last) {
zend_throw_exception(cryo_ce_thaw_exception, "The main script does not match the frozen one", 0);
return FAILURE;
}
uint32_t n = (uint32_t) Z_LVAL_P(n0);
cryo_bind_declarations(op, n);
if (EG(exception)) return FAILURE;
cryo_restore_tmps(main_ex, cryo_get(zend_hash_index_find(Z_ARRVAL_P(dframes), 0), "tmps"));
zend_execute_data *prev = main_ex;
for (uint32_t i = 1; i < nf; i++) {
zval *fm = zend_hash_index_find(Z_ARRVAL_P(mframes), i), *fd = zend_hash_index_find(Z_ARRVAL_P(dframes), i);
zval *name = cryo_get(fm, "func"), *cls = cryo_get(fm, "class"), *file = cryo_get(fm, "file"), *last = cryo_get(fm, "last");
zend_function *fbc = name && Z_TYPE_P(name) == IS_STRING
? cryo_lookup_func(Z_STR_P(name), cls && Z_TYPE_P(cls) == IS_STRING ? Z_STR_P(cls) : NULL) : NULL;
if (!fbc || fbc->type != ZEND_USER_FUNCTION || !file || !last || !zend_string_equals(fbc->op_array.filename, Z_STR_P(file))
|| fbc->op_array.last != (uint32_t) Z_LVAL_P(last)) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Function %s changed or is missing since the freeze",
name && Z_TYPE_P(name) == IS_STRING ? Z_STRVAL_P(name) : "?");
return FAILURE;
}
if (!cryo_is_call_op(prev->func->op_array.opcodes[n].opcode)) {
zend_throw_exception(cryo_ce_thaw_exception, "Corrupted frame position", 0);
return FAILURE;
}
zend_op *call_op = &prev->func->op_array.opcodes[n];
prev->opline = call_op;
prev->call = NULL;
zval *ret = NULL;
if (call_op->result_type != IS_UNUSED) {
ret = ZEND_CALL_VAR(prev, call_op->result.var);
ZVAL_NULL(ret);
}
zval *this_zv = cryo_get(fd, "this"), *called = cryo_get(fm, "called");
uint32_t info = ZEND_CALL_NESTED_FUNCTION;
void *obj_or_scope;
if (this_zv && Z_TYPE_P(this_zv) == IS_OBJECT) {
info |= ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS;
obj_or_scope = Z_OBJ_P(this_zv);
GC_ADDREF(Z_OBJ_P(this_zv));
} else if (called && Z_TYPE_P(called) == IS_STRING) {
obj_or_scope = zend_lookup_class(Z_STR_P(called));
} else {
obj_or_scope = fbc->common.scope;
}
zval *zna = cryo_get(fm, "num_args"), *extra = cryo_get(fd, "extra");
uint32_t num_args = zna ? (uint32_t) Z_LVAL_P(zna) : 0;
uint32_t nextra = extra && Z_TYPE_P(extra) == IS_ARRAY ? zend_hash_num_elements(Z_ARRVAL_P(extra)) : 0;
if (num_args > fbc->op_array.num_args && !nextra) num_args = fbc->op_array.num_args;
zend_execute_data *call = zend_vm_stack_push_call_frame(info, fbc, num_args, obj_or_scope);
for (uint32_t a = 0; a < num_args; a++) ZVAL_UNDEF(ZEND_CALL_ARG(call, a + 1));
call->prev_execute_data = prev;
zend_init_func_execute_data(call, &fbc->op_array, ret);
zend_op_array *fop = &fbc->op_array;
zval *cvs = cryo_get(fd, "cvs");
for (int v = 0; v < fop->last_var; v++) {
zval *slot = ZEND_CALL_VAR_NUM(call, v);
zval_ptr_dtor(slot);
ZVAL_UNDEF(slot);
zval *saved = cvs && Z_TYPE_P(cvs) == IS_ARRAY ? zend_hash_find(Z_ARRVAL_P(cvs), fop->vars[v]) : NULL;
if (saved) ZVAL_COPY(slot, saved);
}
if (nextra && (ZEND_CALL_INFO(call) & ZEND_CALL_FREE_EXTRA_ARGS)) {
zval *dst = ZEND_CALL_VAR_NUM(call, fop->last_var + fop->T), *e;
uint32_t j = 0;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(extra), e) {
if (j < num_args - fop->num_args) { zval_ptr_dtor(&dst[j]); ZVAL_COPY(&dst[j], e); }
j++;
} ZEND_HASH_FOREACH_END();
}
cryo_restore_tmps(call, cryo_get(fd, "tmps"));
zval *zn = cryo_get(fm, "opline");
n = zn ? (uint32_t) Z_LVAL_P(zn) : 0;
if (n >= fop->last) {
zend_throw_exception(cryo_ce_thaw_exception, "Corrupted frame position", 0);
return FAILURE;
}
prev = call;
}
zend_op *at = &prev->func->op_array.opcodes[n];
if (resume_at) {
prev->opline = at; /* the interrupted instruction runs now */
} else {
if (!cryo_is_call_op(at->opcode)) {
zend_throw_exception(cryo_ce_thaw_exception, "Corrupted frame position", 0);
return FAILURE;
}
if (at->result_type != IS_UNUSED) ZVAL_LONG(ZEND_CALL_VAR(prev, at->result.var), CRYO_THAWED);
prev->opline = at + 1; /* cryo_freeze() returns CRYO_THAWED */
}
prev->call = NULL;
EG(current_execute_data) = prev;
zend_execute_ex(prev);
return SUCCESS;
}
/* include again, unserialize, rebuild markers, restore values and hooks */
static zend_result cryo_prepare(zend_string *file, zval *meta, zend_string *sdata, zend_op_array *main, zval *data)
{
ZVAL_UNDEF(data);
if (cryo_include_again(meta) == FAILURE) return FAILURE;
/* classes and functions {main} declared at run time before the freeze
* point must exist before the values are unserialized */
zval *frames = cryo_get(meta, "frames");
zval *fm0 = frames && Z_TYPE_P(frames) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(frames), 0) : NULL;
zval *n0 = cryo_get(fm0, "opline");
if (main && n0 && Z_TYPE_P(n0) == IS_LONG) {
cryo_bind_declarations(main, (uint32_t) Z_LVAL_P(n0));
if (EG(exception)) return FAILURE;
}
if (cryo_unserialize(ZSTR_VAL(sdata), ZSTR_LEN(sdata), data, false) == FAILURE) {
if (!EG(exception)) zend_throw_exception(cryo_ce_thaw_exception, "Cannot restore the frozen values (a class may be missing)", 0);
return FAILURE;
}
cryo_thaw_ctx t;
cryo_thaw_ctx_init(&t, main);
cryo_walk_thaw(&t, data, "data");
cryo_thaw_ctx_free(&t);
if (EG(exception)) return FAILURE;
cryo_walk_typed_restore(data);
if (cryo_restore_values(data) == FAILURE) return FAILURE;
return cryo_after_restore(meta, file, data);
}
PHP_FUNCTION(cryo_thaw)
{
zend_string *file;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH_STR(file)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
zval meta, data;
zend_string *sdata = NULL;
ZVAL_UNDEF(&data);
if (cryo_load(file, opts, true, &meta, &sdata, NULL) == FAILURE) RETURN_THROWS();
zval *script = cryo_get(&meta, "script");
zend_op_array *op = NULL;
zend_string *why = NULL;
zval *zres = cryo_get(&meta, "resume");
if (zres && Z_TYPE_P(zres) == IS_STRING && zend_string_equals_literal(Z_STR_P(zres), "none")) {
/* values only: restore them here and give the hand back */
if ((why = cryo_compat_problem(&meta))) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Cannot thaw %s: %s", ZSTR_VAL(file), ZSTR_VAL(why));
zend_string_release(why);
} else {
cryo_prepare(file, &meta, sdata, NULL, &data);
}
zend_string_release(sdata);
zval_ptr_dtor(&meta);
zval_ptr_dtor(&data);
if (EG(exception)) RETURN_THROWS();
return;
}
if (!script || Z_TYPE_P(script) != IS_STRING) {
zend_throw_exception(cryo_ce_thaw_exception, "Corrupted file: no script", 0);
goto done;
}
if (zend_hash_exists(&EG(included_files), Z_STR_P(script))) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0,
"cryo_thaw() must run in a fresh process: %s is already loaded here (use php -r 'cryo_thaw(...);')", Z_STRVAL_P(script));
goto done;
}
if ((why = cryo_compat_problem(&meta))) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Cannot thaw %s: %s", ZSTR_VAL(file), ZSTR_VAL(why));
zend_string_release(why);
goto done;
}
if (cryo_include_again(&meta) == FAILURE) goto done;
op = compile_filename(ZEND_REQUIRE, Z_STR_P(script));
if (!op) {
if (!EG(exception)) zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Cannot compile %s", Z_STRVAL_P(script));
goto done;
}
if (cryo_prepare(file, &meta, sdata, op, &data) == FAILURE) goto done;
{
zend_execute_data *mex = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_CODE | ZEND_CALL_HAS_SYMBOL_TABLE,
(zend_function *) op, 0, NULL);
mex->symbol_table = &EG(symbol_table);
mex->prev_execute_data = EG(current_execute_data);
zend_init_code_execute_data(mex, op, NULL);
if (cryo_resume(mex, &meta, &data) == FAILURE) {
EG(current_execute_data) = mex->prev_execute_data;
zend_detach_symbol_table(mex);
if (mex->prev_execute_data) zend_attach_symbol_table(mex->prev_execute_data);
}
zend_vm_stack_free_call_frame(mex);
}
done:
if (op) { destroy_op_array(op); efree_size(op, sizeof(zend_op_array)); }
if (sdata) zend_string_release(sdata);
zval_ptr_dtor(&meta);
zval_ptr_dtor(&data);
if (!EG(exception)) zend_throw_unwind_exit(); /* the thawed script has ended */
RETURN_THROWS();
}
/* ------------------------------------------------------------------------ */
/* Thaw at startup: CRYO_THAW=<file> (after cryo_restart) or cryo.thaw=<file> */
/* ------------------------------------------------------------------------ */
static void cryo_thaw_execute_ex(zend_execute_data *ex)
{
if (cryo_pending_thaw && (ZEND_CALL_INFO(ex) & ZEND_CALL_TOP_CODE) && !ex->prev_execute_data) {
zend_string *file = zend_string_init(ZSTR_VAL(cryo_pending_thaw), ZSTR_LEN(cryo_pending_thaw), 0);
bool soft = cryo_pending_soft;
zend_execute_ex = cryo_orig_execute_ex;
cryo_startup_thaw = true;
zval meta, data;
zend_string *sdata = NULL;
ZVAL_UNDEF(&data);
zend_result rc = FAILURE;
bool started = false;
if (!(soft && VCWD_ACCESS(ZSTR_VAL(file), R_OK) != 0)
&& cryo_load(file, NULL, true, &meta, &sdata, NULL) == SUCCESS) {
zval *script = cryo_get(&meta, "script");
zend_string *why = NULL;
if (!script || Z_TYPE_P(script) != IS_STRING || !zend_string_equals(Z_STR_P(script), ex->func->op_array.filename)) {
zend_throw_exception(cryo_ce_thaw_exception, "the file was frozen by another script", 0);
} else if ((why = cryo_compat_problem(&meta))) {
zend_throw_exception_ex(cryo_ce_thaw_exception, 0, "Cannot thaw %s: %s", ZSTR_VAL(file), ZSTR_VAL(why));
zend_string_release(why);
} else {
started = true;
rc = cryo_prepare(file, &meta, sdata, &ex->func->op_array, &data);
if (rc == SUCCESS) {
zend_attach_symbol_table(ex); /* the globals came back after the frame was made */
rc = cryo_resume(ex, &meta, &data);
}
}
zend_string_release(sdata);
zval_ptr_dtor(&meta);
zval_ptr_dtor(&data);
}
zend_string_release(file);
if (rc == SUCCESS) return;
if (EG(exception) && soft && !started) {
zend_clear_exception(); /* warm boot: a missing or stale file = a normal start */
} else if (EG(exception)) {
zend_detach_symbol_table(ex);
EG(current_execute_data) = ex->prev_execute_data;
return;
}
}
cryo_orig_execute_ex(ex);
}
void cryo_freeze_minit(void)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "CryoThawContext", cryo_ctx_methods);
cryo_ce_thaw_context = zend_register_internal_class(&ce);
cryo_ce_thaw_context->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES | ZEND_ACC_NOT_SERIALIZABLE;
const char *f = getenv("CRYO_THAW");
bool soft = false;
if (!f || !*f) {
f = INI_STR("cryo.thaw");
soft = true;
}
if (f && *f) {
cryo_pending_thaw = zend_string_init(f, strlen(f), 1);
cryo_pending_soft = soft;
if (!soft) unsetenv("CRYO_THAW");
cryo_orig_execute_ex = zend_execute_ex;
zend_execute_ex = cryo_thaw_execute_ex;
}
}
void cryo_freeze_rinit(void)
{
cryo_torture_k = INI_INT("cryo.torture");
cryo_torture_seen = 0;
if (cryo_torture_k > 0) zend_atomic_bool_store(&EG(vm_interrupt), true);
memset(&cryo_fz, 0, sizeof(cryo_fz));
ZVAL_UNDEF(&cryo_fz.origin);
array_init(&cryo_fz.on_freeze);
array_init(&cryo_fz.on_thaw);
ZVAL_UNDEF(&cryo_fz.restart_opts);
}
void cryo_freeze_rshutdown(void)
{
zval_ptr_dtor(&cryo_fz.origin);
zval_ptr_dtor(&cryo_fz.on_freeze);
zval_ptr_dtor(&cryo_fz.on_thaw);
zval_ptr_dtor(&cryo_fz.restart_opts);
if (cryo_fz.restart_file) zend_string_release(cryo_fz.restart_file);
memset(&cryo_fz, 0, sizeof(cryo_fz));
}
void cryo_freeze_mshutdown(void)
{
if (cryo_pending_thaw) {
zend_string_release(cryo_pending_thaw);
cryo_pending_thaw = NULL;
}
if (cryo_orig_execute_ex && zend_execute_ex == cryo_thaw_execute_ex) zend_execute_ex = cryo_orig_execute_ex;
}