From 4e55747a2b0fd481018bfd390e2aa50569f73c41 Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Sun, 2 Mar 2003 10:19:15 +0000 Subject: [PATCH] Add JIT initialization for _SERVER and _ENV (it's less important for the others, even though it should be fairly easy now too) --- ext/filepro/filepro.c | 4 - ext/session/session.c | 2 +- main/main.c | 325 +++++++--------------------------------- main/php_globals.h | 8 +- main/php_variables.c | 338 ++++++++++++++++++++++++++++++++++++++++++ main/php_variables.h | 4 + php.ini-dist | 5 + php.ini-recommended | 5 + 8 files changed, 413 insertions(+), 278 deletions(-) diff --git a/ext/filepro/filepro.c b/ext/filepro/filepro.c index 712d4d4653..935f996c29 100644 --- a/ext/filepro/filepro.c +++ b/ext/filepro/filepro.c @@ -34,11 +34,7 @@ #include "safe_mode.h" #include "fopen_wrappers.h" #include -#ifdef PHP_WIN32 -#include -#else #include -#endif #include #include "php_globals.h" diff --git a/ext/session/session.c b/ext/session/session.c index d74d787029..495dfa07fc 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -1714,7 +1714,7 @@ PHP_MINIT_FUNCTION(session) ps_globals = ts_resource(ps_globals_id); #endif - zend_register_auto_global("_SESSION", sizeof("_SESSION")-1 TSRMLS_CC); + zend_register_auto_global("_SESSION", sizeof("_SESSION")-1, NULL TSRMLS_CC); PS(module_number) = module_number; /* if we really need this var we need to init it in zts mode as well! */ diff --git a/main/main.c b/main/main.c index 94e8d8df12..277eba516d 100644 --- a/main/main.c +++ b/main/main.c @@ -25,8 +25,8 @@ #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS -#include #include "php.h" +#include #ifdef PHP_WIN32 #include "win32/time.h" #include "win32/signal.h" @@ -116,28 +116,6 @@ static last_error_type last_error; static void php_build_argv(char *s, zval *track_vars_array TSRMLS_DC); -static char *short_track_vars_names[] = { - "_POST", - "_GET", - "_COOKIE", - "_SERVER", - "_ENV", - "_FILES", - NULL -}; - -static int short_track_vars_names_length[] = { - sizeof("_POST"), - sizeof("_GET"), - sizeof("_COOKIE"), - sizeof("_SERVER"), - sizeof("_ENV"), - sizeof("_FILES") -}; - -#define NUM_TRACK_VARS (sizeof(short_track_vars_names_length)/sizeof(int)) - - #define SAFE_FILENAME(f) ((f)?(f):"-") /* {{{ PHP_INI_MH @@ -278,6 +256,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("output_handler", NULL, PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateString, output_handler, php_core_globals, core_globals) STD_PHP_INI_BOOLEAN("register_argc_argv", "1", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, register_argc_argv, php_core_globals, core_globals) STD_PHP_INI_BOOLEAN("register_globals", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, register_globals, php_core_globals, core_globals) + STD_PHP_INI_BOOLEAN("register_long_arrays", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, register_long_arrays, php_core_globals, core_globals) #if PHP_SAFE_MODE STD_PHP_INI_BOOLEAN("safe_mode", "1", PHP_INI_SYSTEM, OnUpdateBool, safe_mode, php_core_globals, core_globals) #else @@ -864,8 +843,6 @@ static void sigchld_handler(int apar) /* }}} */ #endif -static int php_hash_environment(TSRMLS_D); - /* {{{ php_start_sapi() */ static int php_start_sapi(TSRMLS_D) @@ -1039,7 +1016,9 @@ void php_request_shutdown_for_hook(void *dummy) int i; for (i = 0; i < NUM_TRACK_VARS; i++) { - zval_ptr_dtor(&PG(http_globals)[i]); + if (PG(http_globals)[i]) { + zval_ptr_dtor(&PG(http_globals)[i]); + } } } zend_end_try(); @@ -1164,7 +1143,6 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod zend_utility_values zuv; int module_number=0; /* for REGISTER_INI_ENTRIES() */ char *php_os; - int i; #ifdef ZTS zend_executor_globals *executor_globals; void ***tsrm_ls; @@ -1277,10 +1255,7 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod } zuv.import_use_extension = ".php"; - for (i=0; irefcount++; - } - PG(http_globals)[i] = dummy_track_vars_array; - } - zend_hash_update(&EG(symbol_table), track_vars_names[i], track_vars_names_length[i], &PG(http_globals)[i], sizeof(zval *), NULL); - PG(http_globals)[i]->refcount++; - zend_hash_update(&EG(symbol_table), short_track_vars_names[i], short_track_vars_names_length[i], &PG(http_globals)[i], sizeof(zval *), NULL); - PG(http_globals)[i]->refcount++; - } - - { - zval *form_variables; - - ALLOC_ZVAL(form_variables); - array_init(form_variables); - INIT_PZVAL(form_variables); - - for (p=variables_order; p && *p; p++) { - switch (*p) { - case 'g': - case 'G': - zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); - break; - case 'p': - case 'P': - zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); - break; - case 'c': - case 'C': - zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); - break; - } - } - - zend_hash_update(&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"), &form_variables, sizeof(zval *), NULL); - } - - return SUCCESS; + zend_hash_update(&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"), &form_variables, sizeof(zval *), NULL); } -/* }}} */ - -/* {{{ php_build_argv - */ -static void php_build_argv(char *s, zval *track_vars_array TSRMLS_DC) -{ - pval *arr, *argc, *tmp; - int count = 0; - char *ss, *space; - - ALLOC_ZVAL(arr); - array_init(arr); - INIT_PZVAL(arr); - - /* Prepare argv */ - if (SG(request_info).argc) { /* are we in cli sapi? */ - int i; - for (i=0; irefcount++; - argc->refcount++; - zend_hash_update(&EG(symbol_table), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL); - zend_hash_add(&EG(symbol_table), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL); - } - zend_hash_update(Z_ARRVAL_P(track_vars_array), "argv", sizeof("argv"), &arr, sizeof(pval *), NULL); - zend_hash_update(Z_ARRVAL_P(track_vars_array), "argc", sizeof("argc"), &argc, sizeof(pval *), NULL); -} -/* }}} */ -/* {{{ php_handle_special_queries - */ -PHPAPI int php_handle_special_queries(TSRMLS_D) -{ - if (SG(request_info).query_string && SG(request_info).query_string[0]=='=' - && PG(expose_php)) { - if (php_info_logos(SG(request_info).query_string+1 TSRMLS_CC)) { - return 1; - } else if (!strcmp(SG(request_info).query_string+1, PHP_CREDITS_GUID)) { - php_print_credits(PHP_CREDITS_ALL); - return 1; - } - } - return 0; -} -/* }}} */ /* {{{ php_execute_script */ diff --git a/main/php_globals.h b/main/php_globals.h index 122fea904b..ddb940aa62 100644 --- a/main/php_globals.h +++ b/main/php_globals.h @@ -40,6 +40,7 @@ extern ZEND_API struct _php_core_globals core_globals; #define TRACK_VARS_SERVER 3 #define TRACK_VARS_ENV 4 #define TRACK_VARS_FILES 5 +#define TRACK_VARS_REQUEST 6 struct _php_tick_function_entry; @@ -118,6 +119,7 @@ struct _php_core_globals { zend_bool expose_php; zend_bool register_globals; + zend_bool register_long_arrays; zend_bool register_argc_argv; zend_bool y2k_compliance; @@ -130,17 +132,13 @@ struct _php_core_globals { long xmlrpc_error_number; + zend_bool activated_auto_globals[8]; zend_bool modules_activated; - zend_bool file_uploads; - zend_bool during_request_startup; - zend_bool allow_url_fopen; - zend_bool always_populate_raw_post_data; - zend_bool report_zend_debug; }; diff --git a/main/php_variables.c b/main/php_variables.c index cb354c7dcd..8296a6d24f 100644 --- a/main/php_variables.c +++ b/main/php_variables.c @@ -29,6 +29,7 @@ #include "zend_globals.h" + /* for systems that need to override reading of environment variables */ void _php_import_environment_variables(zval *array_ptr TSRMLS_DC); PHPAPI void (*php_import_environment_variables)(zval *array_ptr TSRMLS_DC) = _php_import_environment_variables; @@ -355,6 +356,343 @@ void _php_import_environment_variables(zval *array_ptr TSRMLS_DC) } +zend_bool php_std_auto_global_callback(char *name, uint name_len TSRMLS_DC) +{ + zend_printf("%s\n", name); + return 0; /* don't rearm */ +} + +/* {{{ php_build_argv + */ +static void php_build_argv(char *s, zval *track_vars_array TSRMLS_DC) +{ + pval *arr, *argc, *tmp; + int count = 0; + char *ss, *space; + + ALLOC_ZVAL(arr); + array_init(arr); + INIT_PZVAL(arr); + + /* Prepare argv */ + if (SG(request_info).argc) { /* are we in cli sapi? */ + int i; + for (i=0; irefcount++; + argc->refcount++; + zend_hash_update(&EG(symbol_table), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL); + zend_hash_add(&EG(symbol_table), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL); + } + + zend_hash_update(Z_ARRVAL_P(track_vars_array), "argv", sizeof("argv"), &arr, sizeof(pval *), NULL); + zend_hash_update(Z_ARRVAL_P(track_vars_array), "argc", sizeof("argc"), &argc, sizeof(pval *), NULL); +} +/* }}} */ + +/* {{{ php_handle_special_queries + */ +PHPAPI int php_handle_special_queries(TSRMLS_D) +{ + if (SG(request_info).query_string && SG(request_info).query_string[0]=='=' + && PG(expose_php)) { + if (php_info_logos(SG(request_info).query_string+1 TSRMLS_CC)) { + return 1; + } else if (!strcmp(SG(request_info).query_string+1, PHP_CREDITS_GUID)) { + php_print_credits(PHP_CREDITS_ALL); + return 1; + } + } + return 0; +} +/* }}} */ + + + +/* {{{ php_register_server_variables + */ +static inline void php_register_server_variables(TSRMLS_D) +{ + zval *array_ptr=NULL; + + ALLOC_ZVAL(array_ptr); + array_init(array_ptr); + INIT_PZVAL(array_ptr); + PG(http_globals)[TRACK_VARS_SERVER] = array_ptr; + + /* Server variables */ + if (sapi_module.register_server_variables) { + sapi_module.register_server_variables(array_ptr TSRMLS_CC); + } + + /* argv/argc support */ + if (PG(register_argc_argv)) { + php_build_argv(SG(request_info).query_string, array_ptr TSRMLS_CC); + } + + /* PHP Authentication support */ + if (SG(request_info).auth_user) { + php_register_variable("PHP_AUTH_USER", SG(request_info).auth_user, array_ptr TSRMLS_CC); + } + if (SG(request_info).auth_password) { + php_register_variable("PHP_AUTH_PW", SG(request_info).auth_password, array_ptr TSRMLS_CC); + } +} +/* }}} */ + + +static zend_bool php_auto_globals_create_server(char *name, uint name_len TSRMLS_DC); +static zend_bool php_auto_globals_create_env(char *name, uint name_len TSRMLS_DC); + + +/* {{{ php_hash_environment + */ +int php_hash_environment(TSRMLS_D) +{ + char *p; + unsigned char _gpc_flags[3] = {0, 0, 0}; + zend_bool have_variables_order; + zval *dummy_track_vars_array = NULL; + zend_bool initialized_dummy_track_vars_array=0; + int i; + zend_bool jit_initialization = (!PG(register_globals) && !PG(register_long_arrays)); + char *variables_order; + struct auto_global_record { + char *name; + uint name_len; + char *long_name; + uint long_name_len; + zend_bool jit_initialization; + } auto_global_records[] = { + { "_POST", sizeof("_POST"), "HTTP_POST_GLOBALS", sizeof("HTTP_POST_GLOBALS"), 0 }, + { "_GET", sizeof("_GET"), "HTTP_GET_GLOBALS", sizeof("HTTP_GET_GLOBALS"), 0 }, + { "_COOKIE", sizeof("_COOKIE"), "HTTP_COOKIE_GLOBALS", sizeof("HTTP_COOKIE_GLOBALS"), 0 }, + { "_SERVER", sizeof("_SERVER"), "HTTP_SERVER_GLOBALS", sizeof("HTTP_SERVER_GLOBALS"), 1 }, + { "_ENV", sizeof("_ENV"), "HTTP_ENV_GLOBALS", sizeof("HTTP_ENV_GLOBALS"), 1 }, + { "_FILES", sizeof("_FILES"), "HTTP_FILES_GLOBALS", sizeof("HTTP_FILES_GLOBALS"), 0 }, + }; + size_t num_track_vars = sizeof(auto_global_records)/sizeof(struct auto_global_record); + + for (i=0; irefcount++; + } + PG(http_globals)[i] = dummy_track_vars_array; + } + + zend_hash_update(&EG(symbol_table), auto_global_records[i].name, auto_global_records[i].name_len, &PG(http_globals)[i], sizeof(zval *), NULL); + PG(http_globals)[i]->refcount++; + if (PG(register_long_arrays)) { + zend_hash_update(&EG(symbol_table), auto_global_records[i].long_name, auto_global_records[i].long_name_len, &PG(http_globals)[i], sizeof(zval *), NULL); + PG(http_globals)[i]->refcount++; + } + } + + /* Create _REQUEST */ + { + zval *form_variables; + + ALLOC_ZVAL(form_variables); + array_init(form_variables); + INIT_PZVAL(form_variables); + + for (p=variables_order; p && *p; p++) { + switch (*p) { + case 'g': + case 'G': + zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); + break; + case 'p': + case 'P': + zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); + break; + case 'c': + case 'C': + zend_hash_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]), (void (*)(void *pData)) zval_add_ref, NULL, sizeof(zval *), 1); + break; + } + } + + zend_hash_update(&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"), &form_variables, sizeof(zval *), NULL); + } + + return SUCCESS; +} +/* }}} */ + + +static zend_bool php_auto_globals_create_server(char *name, uint name_len TSRMLS_DC) +{ + php_register_server_variables(TSRMLS_C); + + zend_hash_update(&EG(symbol_table), name, name_len+1, &PG(http_globals)[TRACK_VARS_SERVER], sizeof(zval *), NULL); + PG(http_globals)[TRACK_VARS_SERVER]->refcount++; + + if (PG(register_long_arrays)) { + zend_hash_update(&EG(symbol_table), "HTTP_SERVER_GLOBALS", sizeof("HTTP_SERVER_GLOBALS"), &PG(http_globals)[TRACK_VARS_SERVER], sizeof(zval *), NULL); + PG(http_globals)[TRACK_VARS_SERVER]->refcount++; + } + + return 0; /* don't rearm */ +} + + +static zend_bool php_auto_globals_create_env(char *name, uint name_len TSRMLS_DC) +{ + ALLOC_ZVAL(PG(http_globals)[TRACK_VARS_ENV]); + array_init(PG(http_globals)[TRACK_VARS_ENV]); + INIT_PZVAL(PG(http_globals)[TRACK_VARS_ENV]); + php_import_environment_variables(PG(http_globals)[TRACK_VARS_ENV] TSRMLS_CC); + + zend_hash_update(&EG(symbol_table), name, name_len+1, &PG(http_globals)[TRACK_VARS_ENV], sizeof(zval *), NULL); + PG(http_globals)[TRACK_VARS_ENV]->refcount++; + + if (PG(register_long_arrays)) { + zend_hash_update(&EG(symbol_table), "HTTP_ENV_GLOBALS", sizeof("HTTP_ENV_GLOBALS"), &PG(http_globals)[TRACK_VARS_ENV], sizeof(zval *), NULL); + PG(http_globals)[TRACK_VARS_ENV]->refcount++; + } + + return 0; /* don't rearm */ +} + + +void php_startup_auto_globals(TSRMLS_D) +{ + zend_bool cb = (!PG(register_globals) && !PG(register_long_arrays)); + + zend_register_auto_global("_GET", sizeof("_GET")-1, NULL TSRMLS_CC); + zend_register_auto_global("_POST", sizeof("_POST")-1, NULL TSRMLS_CC); + zend_register_auto_global("_COOKIE", sizeof("_COOKIE")-1, NULL TSRMLS_CC); + zend_register_auto_global("_SERVER", sizeof("_SERVER")-1, cb?php_auto_globals_create_server:NULL TSRMLS_CC); + zend_register_auto_global("_ENV", sizeof("_ENV")-1, cb?php_auto_globals_create_env:NULL TSRMLS_CC); + zend_register_auto_global("_REQUEST", sizeof("_REQUEST")-1, NULL TSRMLS_CC); +} + /* * Local variables: * tab-width: 4 diff --git a/main/php_variables.h b/main/php_variables.h index 77fed233c2..748b71a658 100644 --- a/main/php_variables.h +++ b/main/php_variables.h @@ -37,5 +37,9 @@ PHPAPI void php_register_variable(char *var, char *val, pval *track_vars_array T PHPAPI void php_register_variable_safe(char *var, char *val, int val_len, pval *track_vars_array TSRMLS_DC); PHPAPI void php_register_variable_ex(char *var, zval *val, pval *track_vars_array TSRMLS_DC); +int php_hash_environment(TSRMLS_D); + + +#define NUM_TRACK_VARS 6 #endif /* PHP_VARIABLES_H */ diff --git a/php.ini-dist b/php.ini-dist index d54d04a1a1..1b00fb4593 100644 --- a/php.ini-dist +++ b/php.ini-dist @@ -354,6 +354,11 @@ variables_order = "EGPCS" ; to possible security problems, if the code is not very well thought of. register_globals = Off +; Whether or not to register the old-style input arrays, HTTP_GET_GLOBALS +; and friends. If you're not using them, it's recommended to turn them off, +; for performance reasons. +register_long_arrays = Off + ; This directive tells PHP whether to declare the argv&argc variables (that ; would contain the GET information). If you don't use these variables, you ; should turn it off for increased performance. diff --git a/php.ini-recommended b/php.ini-recommended index e19b0a01f2..99f4e140c4 100644 --- a/php.ini-recommended +++ b/php.ini-recommended @@ -369,6 +369,11 @@ variables_order = "GPCS" ; to possible security problems, if the code is not very well thought of. register_globals = Off +; Whether or not to register the old-style input arrays, HTTP_GET_GLOBALS +; and friends. If you're not using them, it's recommended to turn them off, +; for performance reasons. +register_long_arrays = Off + ; This directive tells PHP whether to declare the argv&argc variables (that ; would contain the GET information). If you don't use these variables, you ; should turn it off for increased performance. -- 2.50.1