/* Copyright 2025 Sebastian Peters */ #include #include #include "../opcodes/src/pwphpbytecode_opcodes.h" #include "Zend/zend_exceptions.h" #include #include "pwphpbytecode.h" static void ini_enable_settings(HashTable *configuration_hash) { zval ini_value1; ZVAL_NEW_STR(&ini_value1, zend_string_init(ZEND_STRL("PW-Embed SAPI error:"), /* persistent */ 1)); zend_hash_str_update(configuration_hash, ZEND_STRL("error_prepend_string"), &ini_value1); zval ini_value2; ZVAL_NEW_STR(&ini_value2, zend_string_init(ZEND_STRL("On"), /* persistent */ 1)); zend_hash_str_update(configuration_hash, ZEND_STRL("opcache.enable_cli"), &ini_value2); zval ini_value3; ZVAL_NEW_STR(&ini_value3, zend_string_init(ZEND_STRL("256M"), /* persistent */ 1)); zend_hash_str_update(configuration_hash, ZEND_STRL("opcache.jit_buffer_size"), &ini_value3); zval ini_value5; ZVAL_NEW_STR(&ini_value5, zend_string_init(ZEND_STRL("function"), /* persistent */ 1)); zend_hash_str_update(configuration_hash, ZEND_STRL("opcache.jit"), &ini_value5); zval ini_value7; ZVAL_NEW_STR(&ini_value7, zend_string_init(ZEND_STRL("0"), /* persistent */ 1)); zend_hash_str_update(configuration_hash, ZEND_STRL("opcache.save_comments"), &ini_value7); } extern void pwphpbytecode_engine_start(int argc, char **argv){ PG(display_startup_errors)=1; PG(during_request_startup)=1; php_embed_module.ini_defaults = ini_enable_settings; php_embed_module.name = "cli"; php_embed_init(argc, argv); } extern void pwphpbytecode_engine_stop(){ php_embed_shutdown(); } extern void pwphpbytecode_load_compiled_file(char* debugname, char * filename, bool verbose, bool with_jit){ int ret = 0; zend_try { pw_main_opcache_load_compiled_file(filename, verbose, with_jit); // If there was an uncaught error/exception, then report it. zend_object *ex = EG(exception); if (ex != NULL) { zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""4"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(254); } } } zend_catch { zend_object *ex = EG(exception); if (ex != NULL) { EG(exception) = NULL; zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""5"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(253); } EG(exception) = ex; } } zend_end_try(); if(ret == FAILURE){ php_printf("\r\n""ERROR2""\r\n"); php_embed_shutdown(); exit(252); } } extern void pwphpbytecode_load_compiled_chararr(char* debugname, unsigned char *chararr, int charlen, bool verbose, bool with_jit){ int ret = 0; if(verbose){ if(with_jit) php_printf("DEBUG - WITH JIT pwphpbytecode_load_compiled_chararr (%s)\r\n",debugname); else php_printf("DEBUG - W/O JIT pwphpbytecode_load_compiled_chararr (%s)\r\n",debugname); } zend_try { pw_main_opcache_load_compiled_chararr(chararr, charlen, verbose, with_jit); // If there was an uncaught error/exception, then report it. zend_object *ex = EG(exception); if (ex != NULL) { zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""6"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(254); } } } zend_catch { zend_object *ex = EG(exception); if (ex != NULL) { EG(exception) = NULL; zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""7"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(253); } EG(exception) = ex; } } zend_end_try(); if(ret == FAILURE){ php_printf("\r\n""ERROR3""\r\n"); php_embed_shutdown(); exit(252); } } extern void pwphpbytecode_compile_file(char *script_txt, char *target_txt, bool verbose){ pw_internal_opcache_compile_file(script_txt, target_txt, verbose); } extern void pwphpbytecode_exec(char* debugname, char *code){ int ret = 0; zend_try { ret = zend_eval_string(code, NULL, "" TSRMLS_CACHE); // If there was an uncaught error/exception, then report it. zend_object *ex = EG(exception); if (ex != NULL) { zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""1"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(254); } } } zend_catch { zend_object *ex = EG(exception); if (ex != NULL) { EG(exception) = NULL; zval exception; ZVAL_OBJ(&exception, ex); zend_class_entry *ce_exception = Z_OBJCE(exception); if (ce_exception) { zval rv; zend_string *message = zval_get_string(zend_read_property_ex(instanceof_function(Z_OBJCE_P(&exception), zend_ce_exception) ? zend_ce_exception : zend_ce_error, (Z_OBJ_P(&exception)), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv)); char exception_str[256]=""; sprintf(exception_str,"%s",ZSTR_VAL(ce_exception->name)); if(strcmp(exception_str,"UnwindExit")==0){ php_embed_shutdown(); exit(EG(exit_status)); } fprintf(stderr, "PWPHP Uncaught (""2"") (%s) Exception '%s': %s""\r\n",debugname, ZSTR_VAL(ce_exception->name), ZSTR_VAL(message)); php_embed_shutdown(); exit(253); } EG(exception) = ex; } } zend_end_try(); if(ret == FAILURE){ php_printf("\r\n""ERROR""\r\n"); php_embed_shutdown(); exit(252); } } extern bool pwphpbytecode_check_if_in_raw_mode(){ return false; } // This is only a wrapper - only used in _raw-mode extern bool pwphpbytecode_is_raw_loaded(char *classname){ // In non -raw-mode (the library must self ensure if its loaded) return true; } // This is only a wrapper - only used in _raw-mode extern void pwphpbytecode_set_raw_loaded(char *classname){ return; }