Skip to content

Commit

Permalink
Add known strings for jit autoglobals
Browse files Browse the repository at this point in the history
We always create interned strings for all autoglobals anyway, so
we might as well add known strings to make them more widely usable.
  • Loading branch information
nikic committed Apr 9, 2021
1 parent ce4afd5 commit 4ce5d2e
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 75 deletions.
3 changes: 3 additions & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_FROM, "from") \
_(ZEND_STR_TRYFROM, "tryFrom") \
_(ZEND_STR_TRYFROM_LOWERCASE, "tryfrom") \
_(ZEND_STR_AUTOGLOBAL_SERVER, "_SERVER") \
_(ZEND_STR_AUTOGLOBAL_ENV, "_ENV") \
_(ZEND_STR_AUTOGLOBAL_REQUEST, "_REQUEST") \


typedef enum _zend_known_string_id {
Expand Down
4 changes: 2 additions & 2 deletions ext/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ static zval *php_filter_get_storage(zend_long arg)/* {{{ */
break;
case PARSE_SERVER:
if (PG(auto_globals_jit)) {
zend_is_auto_global_str(ZEND_STRL("_SERVER"));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));
}
array_ptr = &IF_G(server_array);
break;
case PARSE_ENV:
if (PG(auto_globals_jit)) {
zend_is_auto_global_str(ZEND_STRL("_ENV"));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV));
}
array_ptr = !Z_ISUNDEF(IF_G(env_array)) ? &IF_G(env_array) : &PG(http_globals)[TRACK_VARS_ENV];
break;
Expand Down
64 changes: 19 additions & 45 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1653,56 +1653,37 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
return new_persistent_script;
}

static const struct jit_auto_global_info
{
const char *name;
size_t len;
} jit_auto_globals_info[] = {
{ "_SERVER", sizeof("_SERVER")-1},
{ "_ENV", sizeof("_ENV")-1},
{ "_REQUEST", sizeof("_REQUEST")-1},
};

static zend_string *jit_auto_globals_str[4];
#define ZEND_AUTOGLOBAL_MASK_SERVER (1 << 0)
#define ZEND_AUTOGLOBAL_MASK_ENV (1 << 1)
#define ZEND_AUTOGLOBAL_MASK_REQUEST (1 << 2)

static int zend_accel_get_auto_globals(void)
{
int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
int n = 1;
int mask = 0;

for (i = 0; i < ag_size ; i++) {
if (zend_hash_exists(&EG(symbol_table), jit_auto_globals_str[i])) {
mask |= n;
}
n += n;
if (zend_hash_exists(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) {
mask |= ZEND_AUTOGLOBAL_MASK_SERVER;
}
if (zend_hash_exists(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV))) {
mask |= ZEND_AUTOGLOBAL_MASK_ENV;
}
if (zend_hash_exists(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST))) {
mask |= ZEND_AUTOGLOBAL_MASK_REQUEST;
}
return mask;
}

static void zend_accel_set_auto_globals(int mask)
{
int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
int n = 1;

for (i = 0; i < ag_size ; i++) {
if (mask & n) {
ZCG(auto_globals_mask) |= n;
zend_is_auto_global(jit_auto_globals_str[i]);
}
n += n;
if (mask & ZEND_AUTOGLOBAL_MASK_SERVER) {
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));
}
}

static void zend_accel_init_auto_globals(void)
{
int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));

for (i = 0; i < ag_size ; i++) {
jit_auto_globals_str[i] = zend_string_init(jit_auto_globals_info[i].name, jit_auto_globals_info[i].len, 1);
zend_string_hash_val(jit_auto_globals_str[i]);
jit_auto_globals_str[i] = accel_new_interned_string(jit_auto_globals_str[i]);
if (mask & ZEND_AUTOGLOBAL_MASK_ENV) {
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV));
}
if (mask & ZEND_AUTOGLOBAL_MASK_REQUEST) {
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST));
}
ZCG(auto_globals_mask) |= mask;
}

static void persistent_error_cb(int type, const char *error_filename, const uint32_t error_lineno, zend_string *message) {
Expand Down Expand Up @@ -3266,7 +3247,6 @@ static zend_result accel_post_startup(void)
zend_shared_alloc_lock();
file_cache_only = 1;
fallback_process = 1;
zend_accel_init_auto_globals();
zend_shared_alloc_unlock();
goto file_cache_fallback;
break;
Expand All @@ -3278,9 +3258,6 @@ static zend_result accel_post_startup(void)
/* remember the last restart time in the process memory */
ZCG(last_restart_time) = ZCSG(last_restart_time);

/* Init auto-global strings */
zend_accel_init_auto_globals();

zend_shared_alloc_lock();
#ifdef HAVE_JIT
if (JIT_G(enabled)) {
Expand All @@ -3306,9 +3283,6 @@ static zend_result accel_post_startup(void)
JIT_G(on) = 0;
#endif
accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));

/* Init auto-global strings */
zend_accel_init_auto_globals();
}
#if ENABLE_FILE_CACHE_FALLBACK
file_cache_fallback:
Expand Down
2 changes: 1 addition & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ PHPAPI int php_session_start(void) /* {{{ */
/* Check the REQUEST_URI symbol for a string of the form
* '<session-name>=<session-id>' to allow URLs of the form
* http://yoursite/<session-name>=<session-id>/script.php */
if (!PS(id) && zend_is_auto_global_str("_SERVER", sizeof("_SERVER") - 1) == SUCCESS &&
if (!PS(id) && zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER)) == SUCCESS &&
(data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI") - 1)) &&
Z_TYPE_P(data) == IS_STRING &&
(p = strstr(Z_STRVAL_P(data), PS(session_name))) &&
Expand Down
7 changes: 2 additions & 5 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ PHP_METHOD(SoapServer, handle)
if (SG(request_info).request_body && 0 == php_stream_rewind(SG(request_info).request_body)) {
zval *server_vars, *encoding;
php_stream_filter *zf = NULL;
zend_string *server = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0);
zend_string *server = ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER);

zend_is_auto_global(server);
if ((server_vars = zend_hash_find(&EG(symbol_table), server)) != NULL &&
Expand All @@ -1291,16 +1291,13 @@ PHP_METHOD(SoapServer, handle)
php_stream_filter_append(&SG(request_info).request_body->readfilters, zf);
} else {
php_error_docref(NULL, E_WARNING,"Can't uncompress compressed request");
zend_string_release_ex(server, 0);
return;
}
} else {
php_error_docref(NULL, E_WARNING,"Request is compressed with unknown compression '%s'",Z_STRVAL_P(encoding));
zend_string_release_ex(server, 0);
return;
}
}
zend_string_release_ex(server, 0);

doc_request = soap_xmlParseFile("php://input");

Expand Down Expand Up @@ -1756,7 +1753,7 @@ static void soap_server_fault_ex(sdlFunctionPtr function, zval* fault, soapHeade

xmlDocDumpMemory(doc_return, &buf, &size);

if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) &&
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
(agent_name = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT")-1)) != NULL &&
Z_TYPE_P(agent_name) == IS_STRING) {
if (strncmp(Z_STRVAL_P(agent_name), "Shockwave Flash", sizeof("Shockwave Flash")-1) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ PHP_FUNCTION(getopt)
/* Get argv from the global symbol table. We calculate argc ourselves
* in order to be on the safe side, even though it is also available
* from the symbol table. */
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) &&
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
((args = zend_hash_find_ex_ind(Z_ARRVAL_P(&PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL ||
(args = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL)
) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/browscap.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ PHP_FUNCTION(get_browser)
if (agent_name == NULL) {
zval *http_user_agent = NULL;
if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY
|| zend_is_auto_global_str(ZEND_STRL("_SERVER"))) {
|| zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) {
http_user_agent = zend_hash_str_find(
Z_ARRVAL_P(&PG(http_globals)[TRACK_VARS_SERVER]),
"HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT")-1);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/url_scanner_ex.re
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ static int check_http_host(char *target)
zend_string *host_tmp;
char *colon;

if ((tmp = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"))) &&
if ((tmp = zend_hash_find(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
Z_TYPE_P(tmp) == IS_ARRAY &&
(host = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("HTTP_HOST"))) &&
Z_TYPE_P(host) == IS_STRING) {
Expand Down
2 changes: 1 addition & 1 deletion ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static int php_zlib_output_encoding(void)
zval *enc;

if (!ZLIBG(compression_coding)) {
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) &&
if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
(enc = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING") - 1))) {
convert_to_string(enc);
if (strstr(Z_STRVAL_P(enc), "gzip")) {
Expand Down
6 changes: 3 additions & 3 deletions main/php_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,8 @@ void php_startup_auto_globals(void)
zend_register_auto_global(zend_string_init_interned("_GET", sizeof("_GET")-1, 1), 0, php_auto_globals_create_get);
zend_register_auto_global(zend_string_init_interned("_POST", sizeof("_POST")-1, 1), 0, php_auto_globals_create_post);
zend_register_auto_global(zend_string_init_interned("_COOKIE", sizeof("_COOKIE")-1, 1), 0, php_auto_globals_create_cookie);
zend_register_auto_global(zend_string_init_interned("_SERVER", sizeof("_SERVER")-1, 1), PG(auto_globals_jit), php_auto_globals_create_server);
zend_register_auto_global(zend_string_init_interned("_ENV", sizeof("_ENV")-1, 1), PG(auto_globals_jit), php_auto_globals_create_env);
zend_register_auto_global(zend_string_init_interned("_REQUEST", sizeof("_REQUEST")-1, 1), PG(auto_globals_jit), php_auto_globals_create_request);
zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER), PG(auto_globals_jit), php_auto_globals_create_server);
zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV), PG(auto_globals_jit), php_auto_globals_create_env);
zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST), PG(auto_globals_jit), php_auto_globals_create_request);
zend_register_auto_global(zend_string_init_interned("_FILES", sizeof("_FILES")-1, 1), 0, php_auto_globals_create_files);
}
2 changes: 1 addition & 1 deletion sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ static void cgi_php_import_environment_variables(zval *array_ptr)
{
if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) != IS_ARRAY) {
zend_is_auto_global_str("_ENV", sizeof("_ENV")-1);
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV));
}

if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
Expand Down
2 changes: 1 addition & 1 deletion sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
}
}

zend_is_auto_global_str(ZEND_STRL("_SERVER"));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));

PG(during_request_startup) = 0;
switch (behavior) {
Expand Down
2 changes: 1 addition & 1 deletion sapi/phpdbg/phpdbg_wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void phpdbg_webdata_decompress(char *msg, int len) {
if ((zvp = zend_hash_str_find(ht, ZEND_STRL("GLOBALS"))) && Z_TYPE_P(zvp) == IS_ARRAY) {
{
zval *srv;
if ((srv = zend_hash_str_find(Z_ARRVAL_P(zvp), ZEND_STRL("_SERVER"))) && Z_TYPE_P(srv) == IS_ARRAY) {
if ((srv = zend_hash_find(Z_ARRVAL_P(zvp), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) && Z_TYPE_P(srv) == IS_ARRAY) {
zval *script;
if ((script = zend_hash_str_find(Z_ARRVAL_P(srv), ZEND_STRL("SCRIPT_FILENAME"))) && Z_TYPE_P(script) == IS_STRING) {
phpdbg_param_t param;
Expand Down
15 changes: 3 additions & 12 deletions sapi/phpdbg/phpdbg_webdata_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
#include "phpdbg_webdata_transfer.h"
#include "ext/standard/php_var.h"

static int phpdbg_is_auto_global(char *name, int len) {
int ret;
zend_string *str = zend_string_init(name, len, 0);
ret = zend_is_auto_global(str);
zend_string_free(str);
return ret;
}

PHPDBG_API void phpdbg_webdata_compress(char **msg, size_t *len) {
zval array;
HashTable *ht;
Expand All @@ -35,11 +27,10 @@ PHPDBG_API void phpdbg_webdata_compress(char **msg, size_t *len) {

/* fetch superglobals */
{
phpdbg_is_auto_global(ZEND_STRL("GLOBALS"));
/* might be JIT */
phpdbg_is_auto_global(ZEND_STRL("_ENV"));
phpdbg_is_auto_global(ZEND_STRL("_SERVER"));
phpdbg_is_auto_global(ZEND_STRL("_REQUEST"));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST));
array_init(&zv[1]);
zend_hash_copy(Z_ARRVAL(zv[1]), &EG(symbol_table), NULL);
Z_ARRVAL(zv[1])->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */
Expand Down

0 comments on commit 4ce5d2e

Please sign in to comment.