winzik/docker/ext/cryo/cryo.c

489 lines
17 KiB
C

/*
* ext-cryo — snapshot, restore, analysis and sandboxing for PHP.
*
* Milestone 1 implements: cryo_snapshot(), allocation-site tracking,
* CryoReader (info/top/roots/delta), metrics, cryo_can_freeze(),
* cryo_freeze_vars()/cryo_thaw_vars(). Every other public function exists
* with its final signature and throws CryoException until its milestone.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_var.h"
#include "zend_exceptions.h"
#include "zend_smart_str.h"
#include "zend_gc.h"
#include "SAPI.h"
#include "php_cryo.h"
#include "cryo_metrics_arginfo.h"
zend_class_entry *cryo_ce_exception;
zend_class_entry *cryo_ce_freeze_exception;
zend_class_entry *cryo_ce_thaw_exception;
zend_class_entry *cryo_ce_signature_exception;
/* ------------------------------------------------------------------------ */
/* INI */
/* ------------------------------------------------------------------------ */
PHP_INI_BEGIN()
PHP_INI_ENTRY("cryo.track_alloc", "off", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.key", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("cryo.allow_unsigned", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.metrics", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.auto_snapshot_every", "0", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("cryo.otlp_endpoint", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("cryo.engine", "1", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.engine_internal_override", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.thaw", "", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.torture", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("cryo.torture_file", "", PHP_INI_SYSTEM, NULL)
PHP_INI_END()
ZEND_COLD void cryo_throw_not_implemented(const char *what, int milestone)
{
zend_throw_exception_ex(cryo_ce_exception, 0,
"%s is not implemented yet (milestone %d of the Cryo roadmap)", what, milestone);
}
/* ------------------------------------------------------------------------ */
/* Snapshot and tracking */
/* ------------------------------------------------------------------------ */
PHP_FUNCTION(cryo_snapshot)
{
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_write_snapshot(file, opts) == FAILURE) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot write snapshot to %s", ZSTR_VAL(file));
RETURN_THROWS();
}
}
PHP_FUNCTION(cryo_track)
{
bool enable;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_BOOL(enable)
ZEND_PARSE_PARAMETERS_END();
if (enable && !cryo_track.installed) {
php_error_docref(NULL, E_WARNING,
"Allocation tracking needs cryo.track_alloc=site in php.ini (set at startup)");
return;
}
cryo_track.enabled = enable;
}
/* ------------------------------------------------------------------------ */
/* Freeze safety */
/* ------------------------------------------------------------------------ */
static bool cryo_ini_on(const char *name, size_t len)
{
const char *v = zend_ini_string_ex(name, len, 0, NULL);
return v && v[0] && strcmp(v, "0") != 0 && strcasecmp(v, "off") != 0 && strcasecmp(v, "false") != 0;
}
/* The JIT runs only if OPcache is loaded and enabled for this SAPI, the JIT
* mode is set and a JIT buffer is reserved. */
bool cryo_jit_active(void)
{
if (!zend_hash_str_exists(&module_registry, "zend opcache", sizeof("zend opcache") - 1)) {
return false;
}
bool cli = sapi_module.name && (strcmp(sapi_module.name, "cli") == 0 || strcmp(sapi_module.name, "phpdbg") == 0);
if (!cryo_ini_on("opcache.enable", sizeof("opcache.enable") - 1)) {
return false;
}
if (cli && !cryo_ini_on("opcache.enable_cli", sizeof("opcache.enable_cli") - 1)) {
return false;
}
const char *jit = zend_ini_string_ex("opcache.jit", sizeof("opcache.jit") - 1, 0, NULL);
if (!jit || !jit[0] || strcasecmp(jit, "disable") == 0 || strcasecmp(jit, "off") == 0
|| strcmp(jit, "0") == 0 || strcasecmp(jit, "false") == 0 || strcasecmp(jit, "no") == 0) {
return false;
}
const char *buf = zend_ini_string_ex("opcache.jit_buffer_size", sizeof("opcache.jit_buffer_size") - 1, 0, NULL);
return buf && strtoll(buf, NULL, 10) > 0; /* "64M" -> 64, "0" -> 0 */
}
PHP_FUNCTION(cryo_can_freeze)
{
ZEND_PARSE_PARAMETERS_NONE();
if (cryo_jit_active()) {
RETURN_STRING("JIT is enabled (set opcache.jit=0)");
}
if (EG(active_fiber)) {
RETURN_STRING("called inside a Fiber");
}
if (EG(exception)) {
RETURN_STRING("an exception is in flight");
}
/* skip our own frame, then every frame up to {main} must be user code */
zend_execute_data *ex = EG(current_execute_data);
ex = ex ? ex->prev_execute_data : NULL;
for (; ex; ex = ex->prev_execute_data) {
if (!ex->func) {
continue;
}
if (!ZEND_USER_CODE(ex->func->type)) {
RETURN_STR(zend_strpprintf(0, "internal frame in the stack: %s()",
ex->func->common.function_name ? ZSTR_VAL(ex->func->common.function_name) : "?"));
}
if (ex->func->common.fn_flags & ZEND_ACC_GENERATOR) {
RETURN_STR(zend_strpprintf(0, "running generator in the stack: %s()",
ex->func->common.function_name ? ZSTR_VAL(ex->func->common.function_name) : "?"));
}
if (!ex->func->common.function_name && (ZEND_CALL_INFO(ex) & ZEND_CALL_TOP)) {
break; /* top-level code: the bottom (a script resumed by cryo_thaw() sits on its caller) */
}
}
RETURN_TRUE;
}
/* ------------------------------------------------------------------------ */
/* Light checkpoint (values only, no stack). v0 uses the PHP serializer; */
/* milestone 4 switches to the Cryo graph serializer + signature. */
/* ------------------------------------------------------------------------ */
#define CRYV_MAGIC "CRYV"
PHP_FUNCTION(cryo_freeze_vars)
{
zend_string *file;
zval *vars;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_PATH_STR(file)
Z_PARAM_ARRAY(vars)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
smart_str buf = {0};
php_serialize_data_t data;
smart_str_appendl(&buf, CRYV_MAGIC, 4);
PHP_VAR_SERIALIZE_INIT(data);
php_var_serialize(&buf, vars, &data);
PHP_VAR_SERIALIZE_DESTROY(data);
smart_str_0(&buf);
if (EG(exception)) {
smart_str_free(&buf);
RETURN_THROWS();
}
/* write to a temp file, then rename: a crash never leaves a half file */
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(buf.s), ZSTR_LEN(buf.s)) == (ssize_t) ZSTR_LEN(buf.s);
php_stream_close(s);
}
if (ok) {
ok = VCWD_RENAME(ZSTR_VAL(tmp), ZSTR_VAL(file)) == 0;
}
zend_string_release(tmp);
smart_str_free(&buf);
if (!ok) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot write %s", ZSTR_VAL(file));
RETURN_THROWS();
}
}
PHP_FUNCTION(cryo_thaw_vars)
{
zend_string *file;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH_STR(file)
ZEND_PARSE_PARAMETERS_END();
php_stream *s = php_stream_open_wrapper(ZSTR_VAL(file), "rb", REPORT_ERRORS, NULL);
if (!s) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot open %s", ZSTR_VAL(file));
RETURN_THROWS();
}
zend_string *data = php_stream_copy_to_mem(s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(s);
if (!data || ZSTR_LEN(data) < 4 || memcmp(ZSTR_VAL(data), CRYV_MAGIC, 4) != 0) {
if (data) zend_string_release(data);
zend_throw_exception_ex(cryo_ce_exception, 0, "%s is not a cryo_freeze_vars() file", ZSTR_VAL(file));
RETURN_THROWS();
}
const unsigned char *p = (const unsigned char *) ZSTR_VAL(data) + 4;
const unsigned char *end = (const unsigned char *) ZSTR_VAL(data) + ZSTR_LEN(data);
php_unserialize_data_t ud;
PHP_VAR_UNSERIALIZE_INIT(ud);
/* a corrupted file throws: no warning from the parser on top of it
* (delayed __wakeup / __unserialize run in DESTROY, with the user's level) */
int er = EG(error_reporting);
EG(error_reporting) &= E_FATAL_ERRORS;
bool ok = php_var_unserialize(return_value, &p, end, &ud);
EG(error_reporting) = er;
PHP_VAR_UNSERIALIZE_DESTROY(ud);
zend_string_release(data);
if (!ok || Z_TYPE_P(return_value) != IS_ARRAY) {
zval_ptr_dtor(return_value);
ZVAL_UNDEF(return_value); /* the VM would release it a second time */
zend_throw_exception_ex(cryo_ce_exception, 0, "Corrupted file %s", ZSTR_VAL(file));
RETURN_THROWS();
}
}
PHP_FUNCTION(cryo_info_runtime)
{
ZEND_PARSE_PARAMETERS_NONE();
array_init(return_value);
add_assoc_string(return_value, "version", PHP_CRYO_VERSION);
add_assoc_string(return_value, "backend", (char *) cryo_platform_name());
add_assoc_long(return_value, "format", 1);
add_assoc_bool(return_value, "track_alloc", cryo_track.installed);
#ifdef ZTS
add_assoc_bool(return_value, "zts", 1);
#else
add_assoc_bool(return_value, "zts", 0);
#endif
}
/* ------------------------------------------------------------------------ */
/* Arginfo */
/* ------------------------------------------------------------------------ */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_file_opts_void, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_freeze, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_file_opts_never, 0, 1, IS_NEVER, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_track, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, enable, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_cryo_can_freeze, 0, 0, MAY_BE_TRUE | MAY_BE_STRING)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_freeze_vars, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, vars, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_thaw_vars, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_array_noargs, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_memory_watch, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, bytes, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_fork_pool, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, job, IS_CALLABLE, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_fork, 0, 0, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, variant, IS_STRING, 0, "\"child\"")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_fork_send, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, data, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, worker, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_fork_recv, 0, 0, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_thaw_fork, 0, 1, IS_NEVER, 0)
ZEND_ARG_TYPE_INFO(0, pid, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_fork_job_done, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_hook, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_register_resource, 0, 3, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, describe, IS_CALLABLE, 0)
ZEND_ARG_TYPE_INFO(0, reopen, IS_CALLABLE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_bool_noargs, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_long_noargs, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_info, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cryo_verify, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, file, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()
/* ---- M2 live analysis (cryo_analysis.h): cryo_roots_of, cryo_orphans, cryo_diff ---- */
#define CRYO_ANALYSIS_ARGINFO
#include "cryo_analysis.h"
/* ---- end M2 ---- */
static const zend_function_entry cryo_functions[] = {
PHP_FE(cryo_on_freeze, arginfo_cryo_hook)
PHP_FE(cryo_on_thaw, arginfo_cryo_hook)
PHP_FE(cryo_register_resource, arginfo_cryo_register_resource)
PHP_FE(cryo_is_thawed, arginfo_cryo_bool_noargs)
PHP_FE(cryo_thaw_count, arginfo_cryo_long_noargs)
PHP_FE(cryo_origin, arginfo_cryo_array_noargs)
PHP_FE(cryo_info, arginfo_cryo_info)
PHP_FE(cryo_verify, arginfo_cryo_verify)
PHP_FE(cryo_snapshot, arginfo_cryo_file_opts_void)
PHP_FE(cryo_track, arginfo_cryo_track)
PHP_FE(cryo_can_freeze, arginfo_cryo_can_freeze)
PHP_FE(cryo_freeze_vars, arginfo_cryo_freeze_vars)
PHP_FE(cryo_thaw_vars, arginfo_cryo_thaw_vars)
CRYO_METRICS_FE
PHP_FE(cryo_info_runtime, arginfo_cryo_array_noargs)
PHP_FE(cryo_freeze, arginfo_cryo_freeze)
PHP_FE(cryo_thaw, arginfo_cryo_file_opts_never)
PHP_FE(cryo_restart, arginfo_cryo_file_opts_never)
PHP_FE(cryo_memory_watch, arginfo_cryo_memory_watch)
PHP_FE(cryo_fork_pool, arginfo_cryo_fork_pool)
PHP_FE(cryo_fork, arginfo_cryo_fork)
PHP_FE(cryo_fork_send, arginfo_cryo_fork_send)
PHP_FE(cryo_fork_recv, arginfo_cryo_fork_recv)
PHP_FE(cryo_thaw_fork, arginfo_cryo_thaw_fork)
PHP_FE(cryo_fork_job_done, arginfo_cryo_fork_job_done)
PHP_FE(cryo_fork_info, arginfo_cryo_array_noargs)
CRYO_ANALYSIS_FE /* M2 */
PHP_FE_END
};
/* ------------------------------------------------------------------------ */
/* Module */
/* ------------------------------------------------------------------------ */
PHP_MINIT_FUNCTION(cryo)
{
REGISTER_INI_ENTRIES();
REGISTER_LONG_CONSTANT("CRYO_FROZEN", CRYO_FROZEN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("CRYO_THAWED", CRYO_THAWED, CONST_CS | CONST_PERSISTENT);
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "CryoException", NULL);
cryo_ce_exception = zend_register_internal_class_ex(&ce, zend_ce_exception);
INIT_CLASS_ENTRY(ce, "CryoFreezeException", NULL);
cryo_ce_freeze_exception = zend_register_internal_class_ex(&ce, cryo_ce_exception);
INIT_CLASS_ENTRY(ce, "CryoThawException", NULL);
cryo_ce_thaw_exception = zend_register_internal_class_ex(&ce, cryo_ce_exception);
INIT_CLASS_ENTRY(ce, "CryoSignatureException", NULL);
cryo_ce_signature_exception = zend_register_internal_class_ex(&ce, cryo_ce_thaw_exception);
cryo_engine_minit();
cryo_sandbox_minit();
cryo_reader_minit();
cryo_walk_minit();
cryo_process_minit();
cryo_freeze_minit();
const char *track = INI_STR("cryo.track_alloc");
cryo_track_minit(track && (strcmp(track, "site") == 0 || strcmp(track, "stack") == 0));
cryo_metrics_minit(); /* after cryo_track_minit: chains its ZEND_NEW handler */
return SUCCESS;
}
PHP_RINIT_FUNCTION(cryo)
{
#if defined(ZTS) && defined(COMPILE_DL_CRYO)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
cryo_walk_rinit();
cryo_freeze_rinit();
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(cryo)
{
cryo_sandbox_rshutdown();
cryo_engine_rshutdown(); /* before the function tables go */
cryo_process_rshutdown();
cryo_freeze_rshutdown();
cryo_walk_rshutdown();
cryo_metrics_rshutdown();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(cryo)
{
cryo_freeze_mshutdown();
cryo_engine_mshutdown();
cryo_metrics_mshutdown(); /* before cryo_process (interrupt chain) and cryo_track (ZEND_NEW chain) */
cryo_process_mshutdown();
cryo_track_mshutdown();
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MINFO_FUNCTION(cryo)
{
php_info_print_table_start();
php_info_print_table_row(2, "Cryo support", "enabled");
php_info_print_table_row(2, "Version", PHP_CRYO_VERSION);
php_info_print_table_row(2, "Backend", cryo_platform_name());
php_info_print_table_row(2, "Allocation tracking", cryo_track.installed ? "installed" : "off");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
zend_module_entry cryo_module_entry = {
STANDARD_MODULE_HEADER,
"cryo",
cryo_functions,
PHP_MINIT(cryo),
PHP_MSHUTDOWN(cryo),
PHP_RINIT(cryo),
PHP_RSHUTDOWN(cryo),
PHP_MINFO(cryo),
PHP_CRYO_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_CRYO
ZEND_GET_MODULE(cryo)
#endif