]> granicus.if.org Git - php/commitdiff
Optimized require_once() and include_once() by eliminationg open() syscall on se
authorDmitry Stogov <dmitry@php.net>
Wed, 5 Mar 2008 13:35:02 +0000 (13:35 +0000)
committerDmitry Stogov <dmitry@php.net>
Wed, 5 Mar 2008 13:35:02 +0000 (13:35 +0000)
cond usage.

Zend/zend.c
Zend/zend.h
Zend/zend_vm_def.h
Zend/zend_vm_execute.h
main/fopen_wrappers.c
main/fopen_wrappers.h
main/main.c

index 75b36fc3fcfc4bf5786ef952c0d24f67be6e758a..fbeafe1a90dd02239879256655ed086e0b6f5485 100644 (file)
@@ -65,6 +65,7 @@ ZEND_API void (*zend_ticks_function)(int ticks);
 ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
 int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
 ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
+ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
 
 void (*zend_on_timeout)(int seconds TSRMLS_DC);
 
@@ -1067,6 +1068,7 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions, i
        zend_on_timeout = utility_functions->on_timeout;
        zend_vspprintf = utility_functions->vspprintf_function;
        zend_getenv = utility_functions->getenv_function;
+       zend_resolve_path = utility_functions->resolve_path_function;
 
        zend_compile_file = compile_file;
        zend_compile_string = compile_string;
index 2d3be029aef18d05a947467e5d681a645ec97575..a9318a054db737dda155ce5db17dd4bb067b995e 100644 (file)
@@ -503,6 +503,7 @@ typedef struct _zend_utility_functions {
        int (*stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
        int (*vspprintf_function)(char **pbuf, size_t max_len, const char *format, va_list ap);
        char *(*getenv_function)(char *name, size_t name_len TSRMLS_DC);
+       char *(*resolve_path_function)(const char *filename, int filename_len TSRMLS_DC);
 } zend_utility_functions;
 
 typedef struct _zend_utility_values {
@@ -640,6 +641,7 @@ extern void (*zend_on_timeout)(int seconds TSRMLS_DC);
 extern ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
 extern int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
 extern ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
+extern ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
 
 ZEND_API void zend_error(int type, const char *format, ...);
 
index d96f81c1b54a743283447e1a6ea0a0b1766046b5..51db4a763adc0796224500fc62946d9ac4654132 100644 (file)
@@ -3155,27 +3155,23 @@ ZEND_VM_HANDLER(73, ZEND_INCLUDE_OR_EVAL, CONST|TMP|VAR|CV, ANY)
                case ZEND_INCLUDE_ONCE:
                case ZEND_REQUIRE_ONCE: {
                                zend_file_handle file_handle;
-
-                               if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) {
-                                       cwd_state state;
-
-                                       state.cwd_length = 0;
-                                       state.cwd = malloc(1);
-                                       state.cwd[0] = 0;
-
-                                       failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) &&
-                                               zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1));
-
-                                       free(state.cwd);
+                               char *resolved_path;
+                               resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC);
+                               if (resolved_path) {
+                                       failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1);
+                               } else {
+                                       resolved_path = Z_STRVAL_P(inc_filename);
                                }
+                               if (failure_retval) {
+                               /* do nothing, file already included */
+                               } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) {
 
-                               if (failure_retval) {
-                                       /* do nothing */
-                               } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) {
                                        if (!file_handle.opened_path) {
-                                               file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
+                                               file_handle.opened_path = estrdup(resolved_path);
                                        }
-
+                                       
                                        if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) {
                                                new_op_array = zend_compile_file(&file_handle, (Z_LVAL(opline->op2.u.constant)==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE) TSRMLS_CC);
                                                zend_destroy_file_handle(&file_handle TSRMLS_CC);
@@ -3190,6 +3186,9 @@ ZEND_VM_HANDLER(73, ZEND_INCLUDE_OR_EVAL, CONST|TMP|VAR|CV, ANY)
                                                zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename));
                                        }
                                }
+                               if (resolved_path != Z_STRVAL_P(inc_filename)) {
+                                       efree(resolved_path);
+                               }
                        }
                        break;
                case ZEND_INCLUDE:
index 7f4ce1e4b77763cd9fb9b31c7dfc59adf4283f95..a9bbcb7f2834980ba2e54c20efed7984424fadc3 100644 (file)
@@ -1696,25 +1696,21 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                case ZEND_INCLUDE_ONCE:
                case ZEND_REQUIRE_ONCE: {
                                zend_file_handle file_handle;
+                               char *resolved_path;
 
-                               if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) {
-                                       cwd_state state;
-
-                                       state.cwd_length = 0;
-                                       state.cwd = malloc(1);
-                                       state.cwd[0] = 0;
-
-                                       failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) &&
-                                               zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1));
-
-                                       free(state.cwd);
+                               resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC);
+                               if (resolved_path) {
+                                       failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1);
+                               } else {
+                                       resolved_path = Z_STRVAL_P(inc_filename);
                                }
 
-                               if (failure_retval) {
-                                       /* do nothing */
-                               } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) {
+                               if (failure_retval) {
+                               /* do nothing, file already included */
+                               } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) {
+
                                        if (!file_handle.opened_path) {
-                                               file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
+                                               file_handle.opened_path = estrdup(resolved_path);
                                        }
 
                                        if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) {
@@ -1731,6 +1727,9 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                                zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename));
                                        }
                                }
+                               if (resolved_path != Z_STRVAL_P(inc_filename)) {
+                                       efree(resolved_path);
+                               }
                        }
                        break;
                case ZEND_INCLUDE:
@@ -5004,25 +5003,21 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                case ZEND_INCLUDE_ONCE:
                case ZEND_REQUIRE_ONCE: {
                                zend_file_handle file_handle;
+                               char *resolved_path;
 
-                               if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) {
-                                       cwd_state state;
-
-                                       state.cwd_length = 0;
-                                       state.cwd = malloc(1);
-                                       state.cwd[0] = 0;
-
-                                       failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) &&
-                                               zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1));
-
-                                       free(state.cwd);
+                               resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC);
+                               if (resolved_path) {
+                                       failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1);
+                               } else {
+                                       resolved_path = Z_STRVAL_P(inc_filename);
                                }
 
-                               if (failure_retval) {
-                                       /* do nothing */
-                               } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) {
+                               if (failure_retval) {
+                               /* do nothing, file already included */
+                               } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) {
+
                                        if (!file_handle.opened_path) {
-                                               file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
+                                               file_handle.opened_path = estrdup(resolved_path);
                                        }
 
                                        if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) {
@@ -5039,6 +5034,9 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                                zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename));
                                        }
                                }
+                               if (resolved_path != Z_STRVAL_P(inc_filename)) {
+                                       efree(resolved_path);
+                               }
                        }
                        break;
                case ZEND_INCLUDE:
@@ -8346,25 +8344,21 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                case ZEND_INCLUDE_ONCE:
                case ZEND_REQUIRE_ONCE: {
                                zend_file_handle file_handle;
+                               char *resolved_path;
 
-                               if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) {
-                                       cwd_state state;
-
-                                       state.cwd_length = 0;
-                                       state.cwd = malloc(1);
-                                       state.cwd[0] = 0;
-
-                                       failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) &&
-                                               zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1));
-
-                                       free(state.cwd);
+                               resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC);
+                               if (resolved_path) {
+                                       failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1);
+                               } else {
+                                       resolved_path = Z_STRVAL_P(inc_filename);
                                }
 
-                               if (failure_retval) {
-                                       /* do nothing */
-                               } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) {
+                               if (failure_retval) {
+                               /* do nothing, file already included */
+                               } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) {
+
                                        if (!file_handle.opened_path) {
-                                               file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
+                                               file_handle.opened_path = estrdup(resolved_path);
                                        }
 
                                        if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) {
@@ -8381,6 +8375,9 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                                zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename));
                                        }
                                }
+                               if (resolved_path != Z_STRVAL_P(inc_filename)) {
+                                       efree(resolved_path);
+                               }
                        }
                        break;
                case ZEND_INCLUDE:
@@ -22603,25 +22600,21 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                case ZEND_INCLUDE_ONCE:
                case ZEND_REQUIRE_ONCE: {
                                zend_file_handle file_handle;
+                               char *resolved_path;
 
-                               if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) {
-                                       cwd_state state;
-
-                                       state.cwd_length = 0;
-                                       state.cwd = malloc(1);
-                                       state.cwd[0] = 0;
-
-                                       failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) &&
-                                               zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1));
-
-                                       free(state.cwd);
+                               resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC);
+                               if (resolved_path) {
+                                       failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1);
+                               } else {
+                                       resolved_path = Z_STRVAL_P(inc_filename);
                                }
 
-                               if (failure_retval) {
-                                       /* do nothing */
-                               } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) {
+                               if (failure_retval) {
+                               /* do nothing, file already included */
+                               } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) {
+
                                        if (!file_handle.opened_path) {
-                                               file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
+                                               file_handle.opened_path = estrdup(resolved_path);
                                        }
 
                                        if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) {
@@ -22638,6 +22631,9 @@ static int ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                                                zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename));
                                        }
                                }
+                               if (resolved_path != Z_STRVAL_P(inc_filename)) {
+                                       efree(resolved_path);
+                               }
                        }
                        break;
                case ZEND_INCLUDE:
index 690573c0fed11e64fa572c1c2ec7a64f1054ef64..d54e26aa72ba5e265ef24968f0acdcc5221e6b27 100644 (file)
@@ -447,6 +447,80 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC)
 }
 /* }}} */
 
+/* {{{ php_resolve_path
+ * Returns the realpath for given filename according to include path
+ */
+PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC)
+{
+       char resolved_path[MAXPATHLEN];
+       char trypath[MAXPATHLEN];
+       char *ptr, *end;
+
+       if (!filename) {
+               return NULL;
+       }
+
+       if (*filename == '.' ||
+           IS_ABSOLUTE_PATH(filename, filename_length) ||
+           !path ||
+           !*path) {
+               if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
+                       return estrdup(resolved_path);
+               } else {
+                       return NULL;
+               }
+       }
+
+       ptr = path;
+       while (ptr && *ptr) {
+               end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
+               if (end) {
+                       if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
+                               ptr = end + 1;
+                               continue;
+                       }
+                       memcpy(trypath, ptr, end-ptr);
+                       trypath[end-ptr] = '/';
+                       memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
+                       ptr = end+1;
+               } else {
+                       int len = strlen(ptr);
+
+                       if (len + 1 + filename_length + 1 >= MAXPATHLEN) {
+                               break;
+                       }
+                       memcpy(trypath, ptr, len);
+                       trypath[len] = '/';
+                       memcpy(trypath+len+1, filename, filename_length+1);
+                       ptr = NULL;
+               }
+               if (tsrm_realpath(trypath, resolved_path TSRMLS_CC)) {
+                       return estrdup(resolved_path);
+               }
+       } /* end provided path */
+
+       /* check in calling scripts' current working directory as a fall back case
+        */
+       if (zend_is_executing(TSRMLS_C)) {
+               char *exec_fname = zend_get_executed_filename(TSRMLS_C);
+               int exec_fname_length = strlen(exec_fname);
+
+               while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
+               if (exec_fname && exec_fname[0] != '[' &&
+                   exec_fname_length > 0 &&
+                   exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
+                       memcpy(trypath, exec_fname, exec_fname_length + 1);
+                       memcpy(trypath+exec_fname_length + 1, filename, filename_length+1);
+                       if (tsrm_realpath(trypath, resolved_path TSRMLS_CC)) {
+                               return estrdup(resolved_path);
+                       }
+               }
+       }
+
+       return NULL;
+}
+/* }}} */
+
 /* {{{ php_fopen_with_path
  * Tries to open a file with a PATH-style list of directories.
  * If the filename starts with "." or "/", the path is ignored.
index d16015987f52be10efa50be9fb2ef60f6e14c689..7c2f33fd1ca4a67733c1d12b5c6a1fac3d365e1e 100644 (file)
@@ -32,6 +32,8 @@ PHPAPI int php_check_open_basedir(const char *path TSRMLS_DC);
 PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC);
 PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path TSRMLS_DC);
 
+PHPAPI char *php_resolve_path(const char *filename, int filename_len, const char *path TSRMLS_DC);
+
 PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC);
 
 PHPAPI char *php_strip_url_passwd(char *path);
index 1abb8d09934d8b911d59c907b23c7372eb9de7be..6996863aae79f10c8c6861e303ddab1cb951217f 100644 (file)
@@ -1202,6 +1202,12 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h
 }
 /* }}} */
 
+static char *php_resolve_path_for_zend(const char *filename, int filename_len TSRMLS_DC) /* {{{ */
+{
+       return php_resolve_path(filename, filename_len, PG(include_path) TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ php_get_configuration_directive_for_zend
  */
 static int php_get_configuration_directive_for_zend(char *name, uint name_length, zval *contents)
@@ -1816,6 +1822,7 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
        zuf.stream_open_function = php_stream_open_for_zend;
        zuf.vspprintf_function = vspprintf;
        zuf.getenv_function = sapi_getenv;
+       zuf.resolve_path_function = php_resolve_path_for_zend;
        zend_startup(&zuf, NULL, 1);
 
 #ifdef ZTS