winzik/docker/ext/cryo/cryo_graph.c

865 lines
29 KiB
C

/*
* cryo_graph.c — allocation-site tracking and snapshot writer.
*
* Walk every root (globals, static properties, static variables of user
* functions and methods, frame CVs, $this), then every object still in the
* store but not reached (orphans), and write the graph to a .cryo file,
* with a content hash per string / flat array (section HASH).
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php_cryo.h"
#include "cryo_format.h"
#include "cryo_analysis.h"
#include "zend_smart_str.h"
#include "zend_objects_API.h"
#include "zend_gc.h"
#include "ext/standard/info.h"
cryo_track_state cryo_track;
static user_opcode_handler_t cryo_prev_new_handler = NULL;
/*
* Allocation stacks (cryo.track_alloc=stack) and write sites. Kept here
* (not in cryo_track_state) so php_cryo.h does not change; same per-handle
* layout as site_of / epoch_of / ce_of, same capacity (cryo_track.cap).
*/
#define CRYO_STACK_DEPTH 8
static bool cryo_stack_mode; /* cryo.track_alloc=stack */
static uint32_t *cryo_stack_of; /* handle -> stack id + 1 (0 = unknown) */
static uint32_t *cryo_write_of; /* handle -> site id + 1 of the last write */
static zend_class_entry **cryo_write_ce; /* handle -> class of the object written */
static HashTable cryo_stacks; /* binary key (u32 site ids) -> stack id */
static uint32_t cryo_next_stack;
/* Opcodes that store a value somewhere: their write site is recorded for
* objects (value in op2 for ASSIGN / ASSIGN_OP / ASSIGN_REF, else in the
* OP_DATA that follows). */
static const uint8_t cryo_write_opcodes[] = {
ZEND_ASSIGN, ZEND_ASSIGN_DIM, ZEND_ASSIGN_OBJ, ZEND_ASSIGN_STATIC_PROP,
ZEND_ASSIGN_OP, ZEND_ASSIGN_DIM_OP, ZEND_ASSIGN_OBJ_OP, ZEND_ASSIGN_STATIC_PROP_OP,
ZEND_ASSIGN_REF, ZEND_ASSIGN_OBJ_REF, ZEND_ASSIGN_STATIC_PROP_REF,
};
#define CRYO_NWRITE_OPS (sizeof(cryo_write_opcodes) / sizeof(cryo_write_opcodes[0]))
static user_opcode_handler_t cryo_prev_write_handler[CRYO_NWRITE_OPS];
/* ------------------------------------------------------------------------ */
/* Allocation-site tracking */
/* ------------------------------------------------------------------------ */
static void cryo_track_grow(uint32_t need)
{
uint32_t cap = cryo_track.cap ? cryo_track.cap : 1024;
while (cap <= need) {
cap *= 2;
}
uint32_t old = cryo_track.cap;
cryo_track.site_of = perealloc(cryo_track.site_of, cap * sizeof(uint32_t), 1);
cryo_track.epoch_of = perealloc(cryo_track.epoch_of, cap * sizeof(uint32_t), 1);
cryo_track.ce_of = perealloc(cryo_track.ce_of, cap * sizeof(zend_class_entry *), 1);
cryo_stack_of = perealloc(cryo_stack_of, cap * sizeof(uint32_t), 1);
cryo_write_of = perealloc(cryo_write_of, cap * sizeof(uint32_t), 1);
cryo_write_ce = perealloc(cryo_write_ce, cap * sizeof(zend_class_entry *), 1);
memset(cryo_track.site_of + old, 0, (cap - old) * sizeof(uint32_t));
memset(cryo_track.epoch_of + old, 0, (cap - old) * sizeof(uint32_t));
memset(cryo_track.ce_of + old, 0, (cap - old) * sizeof(zend_class_entry *));
memset(cryo_stack_of + old, 0, (cap - old) * sizeof(uint32_t));
memset(cryo_write_of + old, 0, (cap - old) * sizeof(uint32_t));
memset(cryo_write_ce + old, 0, (cap - old) * sizeof(zend_class_entry *));
cryo_track.cap = cap;
}
static uint32_t cryo_site_id(zend_execute_data *ex)
{
const zend_function *fn = ex->func;
smart_str key = {0};
smart_str_append(&key, fn->op_array.filename);
smart_str_appendc(&key, ':');
smart_str_append_long(&key, (zend_long) ex->opline->lineno);
smart_str_appendc(&key, ' ');
if (fn->common.scope) {
smart_str_append(&key, fn->common.scope->name);
smart_str_appends(&key, "::");
}
if (fn->common.function_name) {
smart_str_append(&key, fn->common.function_name);
} else {
smart_str_appends(&key, "{main}");
}
smart_str_0(&key);
zval *found = zend_hash_find(&cryo_track.sites, key.s);
if (found) {
smart_str_free(&key);
return (uint32_t) Z_LVAL_P(found);
}
zval id;
ZVAL_LONG(&id, cryo_track.next_site);
/* persistent copy: the table lives across requests */
zend_string *pkey = zend_string_init(ZSTR_VAL(key.s), ZSTR_LEN(key.s), 1);
zend_hash_add_new(&cryo_track.sites, pkey, &id);
zend_string_release(pkey);
smart_str_free(&key);
return cryo_track.next_site++;
}
/* Call stack of user frames (innermost first, up to CRYO_STACK_DEPTH), as
* a deduplicated stack id. Each frame is a site id ("file:line function"). */
static uint32_t cryo_stack_id(zend_execute_data *execute_data)
{
uint32_t frames[CRYO_STACK_DEPTH], n = 0;
for (zend_execute_data *ex = execute_data; ex && n < CRYO_STACK_DEPTH; ex = ex->prev_execute_data) {
if (ex->func && ZEND_USER_CODE(ex->func->type) && ex->opline) {
frames[n++] = cryo_site_id(ex);
}
}
const char *key = (const char *) frames;
size_t len = n * sizeof(uint32_t);
zval *found = zend_hash_str_find(&cryo_stacks, key, len);
if (found) {
return (uint32_t) Z_LVAL_P(found);
}
zval id;
ZVAL_LONG(&id, cryo_next_stack);
zend_hash_str_add_new(&cryo_stacks, key, len, &id); /* persistent key copy */
return cryo_next_stack++;
}
/*
* ZEND_NEW: the object about to be created will take the head of the free
* list, or the top of the store. We record the site against that handle and
* the class, and check the class again at snapshot time to drop stale slots.
*/
static int cryo_new_handler(zend_execute_data *execute_data)
{
if (cryo_track.enabled && EX(func) && ZEND_USER_CODE(EX(func)->type)) {
uint32_t handle = EG(objects_store).free_list_head != -1
? (uint32_t) EG(objects_store).free_list_head
: EG(objects_store).top;
const zend_op *opline = EX(opline);
zend_class_entry *ce = NULL;
if (opline->op1_type == IS_CONST) {
zval *name = RT_CONSTANT(opline, opline->op1);
ce = zend_lookup_class_ex(Z_STR_P(name), Z_STR_P(name + 1), ZEND_FETCH_CLASS_NO_AUTOLOAD);
}
if (handle >= cryo_track.cap) {
cryo_track_grow(handle);
}
cryo_track.site_of[handle] = cryo_site_id(execute_data) + 1;
cryo_track.epoch_of[handle] = cryo_track.epoch;
cryo_track.ce_of[handle] = ce; /* NULL = class not known yet (autoload) */
cryo_stack_of[handle] = cryo_stack_mode ? cryo_stack_id(execute_data) + 1 : 0;
cryo_write_of[handle] = 0; /* the slot belongs to a new object now */
cryo_write_ce[handle] = NULL;
}
if (cryo_prev_new_handler) {
return cryo_prev_new_handler(execute_data);
}
return ZEND_USER_OPCODE_DISPATCH;
}
/* Value of an operand, dereferenced (NULL for IS_UNUSED / IS_CONST: a
* constant is never an object). */
static zval *cryo_operand(zend_execute_data *execute_data, uint8_t type, znode_op op)
{
if (!(type & (IS_TMP_VAR | IS_VAR | IS_CV))) {
return NULL;
}
zval *zv = EX_VAR(op.var);
if (Z_TYPE_P(zv) == IS_INDIRECT) {
zv = Z_INDIRECT_P(zv);
}
ZVAL_DEREF(zv);
return zv;
}
/*
* Write sites: before ASSIGN* runs, if the value stored is an object, record
* this line as its last write site. A plain ASSIGN only counts when it
* stores into something that outlives the frame: top-level code (globals),
* a non-CV target, or a CV bound by reference (static / global / &).
*/
static int cryo_write_handler(zend_execute_data *execute_data)
{
const zend_op *opline = EX(opline);
uint32_t at = 0;
while (at < CRYO_NWRITE_OPS && cryo_write_opcodes[at] != opline->opcode) {
at++;
}
if (cryo_track.enabled && EX(func) && ZEND_USER_CODE(EX(func)->type)) {
zval *val;
switch (opline->opcode) {
case ZEND_ASSIGN:
case ZEND_ASSIGN_OP:
case ZEND_ASSIGN_REF:
val = cryo_operand(execute_data, opline->op2_type, opline->op2);
if (val && opline->opcode == ZEND_ASSIGN && opline->op1_type == IS_CV
&& EX(func)->op_array.function_name
&& Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_REFERENCE) {
val = NULL; /* local variable of a function */
}
break;
default: /* value in the OP_DATA that follows */
val = cryo_operand(execute_data, (opline + 1)->op1_type, (opline + 1)->op1);
break;
}
if (val && Z_TYPE_P(val) == IS_OBJECT) {
zend_object *obj = Z_OBJ_P(val);
if (obj->handle >= cryo_track.cap) {
cryo_track_grow(obj->handle);
}
cryo_write_of[obj->handle] = cryo_site_id(execute_data) + 1;
cryo_write_ce[obj->handle] = obj->ce;
}
}
if (at < CRYO_NWRITE_OPS && cryo_prev_write_handler[at]) {
return cryo_prev_write_handler[at](execute_data);
}
return ZEND_USER_OPCODE_DISPATCH;
}
void cryo_track_minit(bool install_handler)
{
memset(&cryo_track, 0, sizeof(cryo_track));
zend_hash_init(&cryo_track.sites, 64, NULL, NULL, 1);
zend_hash_init(&cryo_stacks, 64, NULL, NULL, 1);
cryo_next_stack = 0;
if (install_handler) {
const char *mode = INI_STR("cryo.track_alloc");
cryo_stack_mode = mode && strcmp(mode, "stack") == 0;
cryo_prev_new_handler = zend_get_user_opcode_handler(ZEND_NEW);
zend_set_user_opcode_handler(ZEND_NEW, cryo_new_handler);
for (uint32_t i = 0; i < CRYO_NWRITE_OPS; i++) {
cryo_prev_write_handler[i] = zend_get_user_opcode_handler(cryo_write_opcodes[i]);
zend_set_user_opcode_handler(cryo_write_opcodes[i], cryo_write_handler);
}
cryo_track.installed = true;
cryo_track.enabled = true;
}
}
void cryo_track_mshutdown(void)
{
if (cryo_track.installed) {
zend_set_user_opcode_handler(ZEND_NEW, cryo_prev_new_handler);
for (uint32_t i = 0; i < CRYO_NWRITE_OPS; i++) {
zend_set_user_opcode_handler(cryo_write_opcodes[i], cryo_prev_write_handler[i]);
}
}
zend_hash_destroy(&cryo_track.sites);
zend_hash_destroy(&cryo_stacks);
if (cryo_track.site_of) {
pefree(cryo_track.site_of, 1);
pefree(cryo_track.epoch_of, 1);
pefree(cryo_track.ce_of, 1);
pefree(cryo_stack_of, 1);
pefree(cryo_write_of, 1);
pefree(cryo_write_ce, 1);
}
}
/* ------------------------------------------------------------------------ */
/* Snapshot writer */
/* ------------------------------------------------------------------------ */
typedef struct {
HashTable ids; /* (uintptr_t) ptr -> node id */
HashTable strtab; /* string -> index */
uint32_t nstr;
smart_str strs, nodes, edges, roots, hashes, nstk, writ;
uint32_t next_id;
uint32_t nnodes, nedges, nroots;
/* explicit work stack, no C recursion */
struct cryo_work { zval *zv; uint32_t parent; uint32_t name; bool red; } *stack;
uint32_t sp, scap;
HashTable *redact; /* option "redact": name patterns (`*` wildcard, case-insensitive) */
zend_object *skip; /* live queries: frame variables holding this object are not roots */
} cryo_writer;
static uint32_t w_str(cryo_writer *w, const char *s, size_t len)
{
zval *found = zend_hash_str_find(&w->strtab, s, len);
if (found) {
return (uint32_t) Z_LVAL_P(found);
}
zval idx;
ZVAL_LONG(&idx, w->nstr);
zend_hash_str_add_new(&w->strtab, s, len, &idx);
uint32_t l = (uint32_t) len;
smart_str_appendl(&w->strs, (const char *) &l, 4);
smart_str_appendl(&w->strs, s, len);
return w->nstr++;
}
static void w_edge(cryo_writer *w, uint32_t from, uint32_t to, uint32_t name)
{
cryo_edge_rec e = { from, to, name };
smart_str_appendl(&w->edges, (const char *) &e, sizeof(e));
w->nedges++;
}
static void w_root(cryo_writer *w, uint8_t kind, uint32_t node, uint32_t name)
{
cryo_root_rec r = { kind, node, name };
smart_str_appendl(&w->roots, (const char *) &r, sizeof(r));
w->nroots++;
}
static void w_push(cryo_writer *w, zval *zv, uint32_t parent, uint32_t name, bool red)
{
if (w->sp == w->scap) {
w->scap = w->scap ? w->scap * 2 : 256;
w->stack = erealloc(w->stack, w->scap * sizeof(*w->stack));
}
w->stack[w->sp].zv = zv;
w->stack[w->sp].parent = parent;
w->stack[w->sp].name = name;
w->stack[w->sp].red = red;
w->sp++;
}
/* Property / key name without the mangling of private and protected names */
static void w_plain_key(zend_string *key, const char **s, size_t *len)
{
const char *cls;
if (ZSTR_LEN(key) && ZSTR_VAL(key)[0] == '\0'
&& zend_unmangle_property_name_ex(key, &cls, s, len) == SUCCESS) {
return;
}
*s = ZSTR_VAL(key);
*len = ZSTR_LEN(key);
}
/* `*` wildcard match, iterative (shared with the reader's query language) */
bool cryo_glob(const char *p, size_t plen, const char *s, size_t slen, bool nocase)
{
size_t pi = 0, si = 0, star = (size_t) -1, mark = 0;
while (si < slen) {
char pc = pi < plen ? p[pi] : 0, sc = s[si];
if (nocase) {
if (pc >= 'A' && pc <= 'Z') pc = (char) (pc + 32);
if (sc >= 'A' && sc <= 'Z') sc = (char) (sc + 32);
}
if (pi < plen && p[pi] == '*') {
star = pi++;
mark = si;
} else if (pi < plen && pc == sc) {
pi++; si++;
} else if (star != (size_t) -1) {
pi = star + 1;
si = ++mark;
} else {
return false;
}
}
while (pi < plen && p[pi] == '*') pi++;
return pi == plen;
}
/* Does this key / variable name match a "redact" pattern? */
static bool w_redacted(cryo_writer *w, const char *name, size_t len)
{
if (!w->redact) {
return false;
}
zval *pat;
ZEND_HASH_FOREACH_VAL(w->redact, pat) {
if (Z_TYPE_P(pat) == IS_STRING && cryo_glob(Z_STRVAL_P(pat), Z_STRLEN_P(pat), name, len, true)) {
return true;
}
} ZEND_HASH_FOREACH_END();
return false;
}
static bool w_key_redacted(cryo_writer *w, zend_string *key)
{
const char *s; size_t len;
if (!key || !w->redact) {
return false;
}
w_plain_key(key, &s, &len);
return w_redacted(w, s, len);
}
/* Edge name of a key; under a redacted value, keys become "[redacted N]" */
static uint32_t w_key_name(cryo_writer *w, zend_string *key, zend_ulong idx, bool red)
{
if (key && red) {
char buf[48];
int n = snprintf(buf, sizeof(buf), "[redacted %zu]", ZSTR_LEN(key));
return w_str(w, buf, (size_t) n);
}
if (key) {
const char *cls, *prop;
size_t plen;
if (ZSTR_LEN(key) && ZSTR_VAL(key)[0] == '\0'
&& zend_unmangle_property_name_ex(key, &cls, &prop, &plen) == SUCCESS) {
return w_str(w, prop, plen);
}
return w_str(w, ZSTR_VAL(key), ZSTR_LEN(key));
}
char buf[32];
int n = snprintf(buf, sizeof(buf), "[" ZEND_ULONG_FMT "]", idx);
return w_str(w, buf, (size_t) n);
}
/* FNV-1a 32 (section HASH) */
#define CRYO_FNV_INIT 0x811c9dc5u
static uint32_t fnv1a(uint32_t h, const void *data, size_t len)
{
const unsigned char *p = data;
for (size_t i = 0; i < len; i++) {
h ^= p[i];
h *= 0x01000193u;
}
return h;
}
static inline uint32_t fnv_tag(uint32_t h, char tag)
{
return fnv1a(h, &tag, 1);
}
static inline uint32_t fnv_bytes(uint32_t h, const char *s, size_t len)
{
uint32_t l = (uint32_t) len;
h = fnv1a(h, &l, 4);
return fnv1a(h, s, len);
}
/* Hashes one array element; returns false if the value is not a scalar or
* a string (the array is then not "flat" and gets no hash). */
static bool hash_flat_elem(uint32_t *h, zend_string *key, zend_ulong idx, zval *val)
{
if (key) {
*h = fnv_tag(*h, 's');
*h = fnv_bytes(*h, ZSTR_VAL(key), ZSTR_LEN(key));
} else {
int64_t i = (int64_t) (zend_long) idx;
*h = fnv_tag(*h, 'i');
*h = fnv1a(*h, &i, 8);
}
switch (Z_TYPE_P(val)) {
case IS_NULL: *h = fnv_tag(*h, 'n'); return true;
case IS_FALSE: *h = fnv_tag(*h, 'f'); return true;
case IS_TRUE: *h = fnv_tag(*h, 't'); return true;
case IS_LONG: {
int64_t l = (int64_t) Z_LVAL_P(val);
*h = fnv_tag(*h, 'l');
*h = fnv1a(*h, &l, 8);
return true;
}
case IS_DOUBLE: {
double d = Z_DVAL_P(val);
*h = fnv_tag(*h, 'd');
*h = fnv1a(*h, &d, 8);
return true;
}
case IS_STRING:
*h = fnv_tag(*h, 'S');
*h = fnv_bytes(*h, Z_STRVAL_P(val), Z_STRLEN_P(val));
return true;
default:
return false;
}
}
/* Returns the node id for zv, creating it (and queuing its children) if new.
* Scalars produce no node: CRYO_NONE. */
static uint32_t w_node(cryo_writer *w, zval *zv, bool red)
{
void *ptr;
uint8_t type;
switch (Z_TYPE_P(zv)) {
case IS_STRING: ptr = Z_STR_P(zv); type = CRYO_NODE_STRING; break;
case IS_ARRAY: ptr = Z_ARR_P(zv); type = CRYO_NODE_ARRAY; break;
case IS_OBJECT: ptr = Z_OBJ_P(zv); type = CRYO_NODE_OBJECT; break;
case IS_REFERENCE: ptr = Z_REF_P(zv); type = CRYO_NODE_REF; break;
default: return CRYO_NONE;
}
zval *seen = zend_hash_index_find(&w->ids, (zend_ulong)(uintptr_t) ptr);
if (seen) {
return (uint32_t) Z_LVAL_P(seen);
}
uint32_t id = w->next_id++;
zval zid;
ZVAL_LONG(&zid, id);
zend_hash_index_add_new(&w->ids, (zend_ulong)(uintptr_t) ptr, &zid);
cryo_node_rec n = { id, type, CRYO_NONE, 0, CRYO_NONE, CRYO_NONE, CRYO_NONE };
cryo_hash_rec hr = { 0, 0 };
uint32_t stack = CRYO_NONE, write = CRYO_NONE;
if (type == CRYO_NODE_STRING) {
n.size = ZSTR_IS_INTERNED(Z_STR_P(zv)) ? 0 : _ZSTR_STRUCT_SIZE(Z_STRLEN_P(zv));
hr.hash = red ? 0 : fnv1a(CRYO_FNV_INIT, Z_STRVAL_P(zv), Z_STRLEN_P(zv)); /* redacted: length only */
hr.len = (uint32_t) MIN(Z_STRLEN_P(zv), (size_t) UINT32_MAX);
} else if (type == CRYO_NODE_ARRAY) {
HashTable *ht = Z_ARR_P(zv);
n.size = (HT_IS_PACKED(ht) ? ht->nTableSize * sizeof(zval)
: ht->nTableSize * sizeof(Bucket)) + sizeof(HashTable);
if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
n.size = 0;
}
zend_string *key; zend_ulong idx; zval *val;
uint32_t h = CRYO_FNV_INIT;
bool flat = !red;
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, val) {
bool kred = red || w_key_redacted(w, key);
if (flat) {
flat = !kred && hash_flat_elem(&h, key, idx, val);
}
w_push(w, val, id, w_key_name(w, key, idx, red), kred);
} ZEND_HASH_FOREACH_END();
if (flat) {
hr.hash = h;
hr.len = zend_hash_num_elements(ht);
}
} else if (type == CRYO_NODE_OBJECT) {
zend_object *obj = Z_OBJ_P(zv);
n.class_str = w_str(w, ZSTR_VAL(obj->ce->name), ZSTR_LEN(obj->ce->name));
n.size = sizeof(zend_object) + zend_object_properties_size(obj->ce);
n.handle = obj->handle;
if (obj->handle < cryo_track.cap && cryo_track.site_of[obj->handle]
&& (cryo_track.ce_of[obj->handle] == NULL || cryo_track.ce_of[obj->handle] == obj->ce)) {
n.site = cryo_track.site_of[obj->handle] - 1;
n.epoch = cryo_track.epoch_of[obj->handle];
if (cryo_stack_of[obj->handle]) {
stack = cryo_stack_of[obj->handle] - 1;
}
}
if (obj->handle < cryo_track.cap && cryo_write_of[obj->handle] && cryo_write_ce[obj->handle] == obj->ce) {
write = cryo_write_of[obj->handle] - 1;
}
HashTable *props = obj->handlers->get_properties(obj);
if (props) {
zend_string *key; zend_ulong idx; zval *val;
ZEND_HASH_FOREACH_KEY_VAL_IND(props, idx, key, val) {
if (Z_TYPE_P(val) != IS_UNDEF) {
w_push(w, val, id, w_key_name(w, key, idx, red), red || w_key_redacted(w, key));
}
} ZEND_HASH_FOREACH_END();
}
} else { /* reference */
w_push(w, Z_REFVAL_P(zv), id, w_str(w, "&", 1), red);
}
if (hr.hash == 0 && !red && (type == CRYO_NODE_STRING || (type == CRYO_NODE_ARRAY && hr.len))) {
hr.hash = 1;
}
smart_str_appendl(&w->nodes, (const char *) &n, sizeof(n));
smart_str_appendl(&w->hashes, (const char *) &hr, sizeof(hr));
smart_str_appendl(&w->nstk, (const char *) &stack, 4);
smart_str_appendl(&w->writ, (const char *) &write, 4);
w->nnodes++;
return id;
}
/* Visit a root value and everything reachable from it. */
static void w_walk_root(cryo_writer *w, uint8_t kind, zval *zv, const char *name, size_t name_len)
{
/* redaction of a root: on the variable / property name after the last '$' */
const char *var = name;
for (size_t i = 0; i < name_len; i++) {
if (name[i] == '$') var = name + i + 1;
}
uint32_t id = w_node(w, zv, w_redacted(w, var, name_len - (size_t) (var - name)));
if (id == CRYO_NONE) {
return;
}
w_root(w, kind, id, w_str(w, name, name_len));
while (w->sp) {
struct cryo_work it = w->stack[--w->sp];
uint32_t child = w_node(w, it.zv, it.red);
if (child != CRYO_NONE) {
w_edge(w, it.parent, child, it.name);
}
}
}
/* Static variables of one user function or method (roots "f::$v" or
* "C::m::$v"). `seen` avoids walking a table twice (class aliases). */
static void w_walk_statics(cryo_writer *w, HashTable *seen, const zend_op_array *op, zend_string *scope)
{
if (op->type != ZEND_USER_FUNCTION || !op->static_variables || !op->function_name) {
return;
}
HashTable *ht = ZEND_MAP_PTR_GET(op->static_variables_ptr);
if (!ht) {
ht = op->static_variables; /* not called yet: initial values */
}
if (zend_hash_index_exists(seen, (zend_ulong)(uintptr_t) ht)) {
return;
}
zend_hash_index_add_empty_element(seen, (zend_ulong)(uintptr_t) ht);
zend_string *key; zval *val;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
if (!key || Z_TYPE_P(val) == IS_CONSTANT_AST) {
continue;
}
smart_str name = {0};
if (scope) {
smart_str_append(&name, scope);
smart_str_appends(&name, "::");
}
smart_str_append(&name, op->function_name);
smart_str_appends(&name, "::$");
smart_str_append(&name, key);
smart_str_0(&name);
w_walk_root(w, CRYO_ROOT_STATIC_VAR, val, ZSTR_VAL(name.s), ZSTR_LEN(name.s));
smart_str_free(&name);
} ZEND_HASH_FOREACH_END();
}
static void w_section(smart_str *out, const char *tag, smart_str *payload, uint32_t count_prefix, bool with_count)
{
uint64_t len = (payload->s ? ZSTR_LEN(payload->s) : 0) + (with_count ? 4 : 0);
smart_str_appendl(out, tag, 4);
smart_str_appendl(out, (const char *) &len, 8);
if (with_count) {
smart_str_appendl(out, (const char *) &count_prefix, 4);
}
if (payload->s) {
smart_str_append(out, payload->s);
}
}
/*
* Build a whole .cryo file in memory. Options: "gc" (collect cycles first,
* default gc_default), "redact" (list of key / property / variable name
* patterns: matching strings keep only their length, keys below them become
* "[redacted N]"). `find`: frame variables holding this object are not
* roots, and *found_id receives its node id (CRYO_NONE if absent).
*/
static zend_string *cryo_build_snapshot(HashTable *options, bool gc_default, zend_object *find, uint32_t *found_id)
{
cryo_writer w;
memset(&w, 0, sizeof(w));
zend_hash_init(&w.ids, 1024, NULL, NULL, 0);
zend_hash_init(&w.strtab, 256, NULL, NULL, 0);
w.skip = find;
zval *gc_opt = options ? zend_hash_str_find(options, "gc", 2) : NULL;
if (gc_opt ? zend_is_true(gc_opt) : gc_default) {
zend_gc_collect_cycles();
}
zval *redact = options ? zend_hash_str_find(options, "redact", 6) : NULL;
if (redact && Z_TYPE_P(redact) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(redact))) {
w.redact = Z_ARRVAL_P(redact);
}
/* 1. globals */
{
zend_string *key; zval *val;
ZEND_HASH_FOREACH_STR_KEY_VAL_IND(&EG(symbol_table), key, val) {
if (!key || zend_string_equals_literal(key, "GLOBALS")) {
continue;
}
w_walk_root(&w, CRYO_ROOT_GLOBAL, val, ZSTR_VAL(key), ZSTR_LEN(key));
} ZEND_HASH_FOREACH_END();
}
/* 2. static properties of user classes */
{
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 *statics = CE_STATIC_MEMBERS(ce);
if (!statics) {
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 *val = &statics[pi->offset];
ZVAL_DEINDIRECT(val);
smart_str name = {0};
smart_str_append(&name, ce->name);
smart_str_appends(&name, "::$");
smart_str_append(&name, pi->name);
smart_str_0(&name);
w_walk_root(&w, CRYO_ROOT_STATIC_PROP, val, ZSTR_VAL(name.s), ZSTR_LEN(name.s));
smart_str_free(&name);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
}
/* 2b. static variables of user functions and methods */
{
HashTable seen;
zend_hash_init(&seen, 64, NULL, NULL, 0);
zend_function *fn;
ZEND_HASH_MAP_FOREACH_PTR(EG(function_table), fn) {
w_walk_statics(&w, &seen, &fn->op_array, NULL);
} ZEND_HASH_FOREACH_END();
zend_class_entry *ce;
ZEND_HASH_MAP_FOREACH_PTR(EG(class_table), ce) {
if (ce->type != ZEND_USER_CLASS) {
continue;
}
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) {
if (fn->common.scope == ce) { /* inherited methods share the parent's statics */
w_walk_statics(&w, &seen, &fn->op_array, ce->name);
}
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&seen);
}
/* 3. live frames: CVs and $this */
/* down to the first top-level code frame: below it is not this script (php -r 'cryo_thaw(...)') */
for (zend_execute_data *ex = EG(current_execute_data); ex;
ex = (ex->func && ZEND_USER_CODE(ex->func->type) && !ex->func->op_array.function_name
&& (ZEND_CALL_INFO(ex) & ZEND_CALL_TOP)) ? NULL : ex->prev_execute_data) {
if (!ex->func || !ZEND_USER_CODE(ex->func->type)) {
continue;
}
const zend_op_array *op = &ex->func->op_array;
const char *fname = op->function_name ? ZSTR_VAL(op->function_name) : "{main}";
for (int i = 0; i < op->last_var; i++) {
zval *cv = ZEND_CALL_VAR_NUM(ex, i);
if (w.skip) {
zval *d = cv;
ZVAL_DEREF(d);
if (Z_TYPE_P(d) == IS_OBJECT && Z_OBJ_P(d) == w.skip) {
continue; /* the variable passed to the live query */
}
}
char buf[512];
int n = snprintf(buf, sizeof(buf), "%s:$%s", fname, ZSTR_VAL(op->vars[i]));
w_walk_root(&w, CRYO_ROOT_FRAME, cv, buf, (size_t) MIN(n, (int) sizeof(buf) - 1));
}
if (Z_TYPE(ex->This) == IS_OBJECT) {
char buf[512];
int n = snprintf(buf, sizeof(buf), "%s:$this", fname);
w_walk_root(&w, CRYO_ROOT_FRAME, &ex->This, buf, (size_t) MIN(n, (int) sizeof(buf) - 1));
}
}
/* 4. orphans: alive in the store, unreachable from every root */
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
zend_object *obj = EG(objects_store).object_buckets[h];
if (!IS_OBJ_VALID(obj)) {
continue;
}
if (zend_hash_index_exists(&w.ids, (zend_ulong)(uintptr_t) obj)) {
continue;
}
zval tmp;
ZVAL_OBJ(&tmp, obj);
w_walk_root(&w, CRYO_ROOT_ORPHAN, &tmp, "(orphan)", 8);
}
/* 5. sites table */
smart_str sites = {0};
{
uint32_t *by_id = ecalloc(cryo_track.next_site ? cryo_track.next_site : 1, sizeof(uint32_t));
zend_string *key; zval *val;
ZEND_HASH_FOREACH_STR_KEY_VAL(&cryo_track.sites, key, val) {
by_id[Z_LVAL_P(val)] = w_str(&w, ZSTR_VAL(key), ZSTR_LEN(key));
} ZEND_HASH_FOREACH_END();
smart_str_appendl(&sites, (const char *) by_id, cryo_track.next_site * sizeof(uint32_t));
efree(by_id);
}
/* 5b. allocation stacks: u32 count, then { u32 nframes, u32 site[nframes] } */
smart_str stks = {0};
if (cryo_stack_mode) {
zend_string *key; zval *val;
ZEND_HASH_FOREACH_STR_KEY_VAL(&cryo_stacks, key, val) { /* insertion order = id order */
(void) val;
uint32_t nf = (uint32_t) (ZSTR_LEN(key) / sizeof(uint32_t));
smart_str_appendl(&stks, (const char *) &nf, 4);
smart_str_appendl(&stks, ZSTR_VAL(key), nf * sizeof(uint32_t));
} ZEND_HASH_FOREACH_END();
}
/* 6. assemble */
smart_str out = {0};
uint16_t ver = CRYO_FORMAT_VERSION, flags = 0;
uint32_t phpv = PHP_VERSION_ID, epoch = cryo_track.epoch;
smart_str_appendl(&out, CRYO_MAGIC, 4);
smart_str_appendl(&out, (const char *) &ver, 2);
smart_str_appendl(&out, (const char *) &flags, 2);
smart_str_appendl(&out, (const char *) &phpv, 4);
smart_str_appendl(&out, (const char *) &epoch, 4);
w_section(&out, CRYO_SEC_STRS, &w.strs, w.nstr, true);
w_section(&out, CRYO_SEC_NODE, &w.nodes, 0, false);
w_section(&out, CRYO_SEC_EDGE, &w.edges, 0, false);
w_section(&out, CRYO_SEC_ROOT, &w.roots, 0, false);
w_section(&out, CRYO_SEC_SITE, &sites, 0, false);
w_section(&out, CRYO_SEC_HASH, &w.hashes, 0, false);
if (cryo_track.installed) {
w_section(&out, CRYO_SEC_WRIT, &w.writ, 0, false);
}
if (cryo_stack_mode) {
w_section(&out, CRYO_SEC_STKS, &stks, cryo_next_stack, true);
w_section(&out, CRYO_SEC_NSTK, &w.nstk, 0, false);
}
smart_str_0(&out);
if (found_id) {
zval *id = find ? zend_hash_index_find(&w.ids, (zend_ulong)(uintptr_t) find) : NULL;
*found_id = id ? (uint32_t) Z_LVAL_P(id) : CRYO_NONE;
}
smart_str_free(&sites);
smart_str_free(&w.strs);
smart_str_free(&w.nodes);
smart_str_free(&w.edges);
smart_str_free(&w.roots);
smart_str_free(&w.hashes);
smart_str_free(&w.nstk);
smart_str_free(&w.writ);
smart_str_free(&stks);
if (w.stack) {
efree(w.stack);
}
zend_hash_destroy(&w.ids);
zend_hash_destroy(&w.strtab);
return out.s;
}
zend_result cryo_write_snapshot(zend_string *file, HashTable *options)
{
zend_string *data = cryo_build_snapshot(options, true, NULL, NULL);
zend_result rc = FAILURE;
php_stream *s = php_stream_open_wrapper(ZSTR_VAL(file), "wb", REPORT_ERRORS, NULL);
if (s) {
if (php_stream_write(s, ZSTR_VAL(data), ZSTR_LEN(data)) == (ssize_t) ZSTR_LEN(data)) {
rc = SUCCESS;
}
php_stream_close(s);
}
zend_string_release(data);
if (rc == SUCCESS) {
cryo_track.epoch++; /* objects born from now on belong to the next interval */
}
return rc;
}
/* Snapshot of the running process kept in memory (live queries): no cycle
* collection by default, the epoch does not move. */
zend_string *cryo_snapshot_mem(HashTable *options, zend_object *find, uint32_t *found_id)
{
return cryo_build_snapshot(options, false, find, found_id);
}