Skip to content

Cleanup SPL autoload implementation #5696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Zend/tests/bug49908.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ string(3) "Bar"

Fatal error: Uncaught Exception: Bar in %s:%d
Stack trace:
#0 [internal function]: {closure}('Bar')
#1 %s(%d): spl_autoload_call('Bar')
#2 [internal function]: {closure}('Foo')
#3 %s(%d): spl_autoload_call('Foo')
#4 {main}
#0 %s(%d): {closure}('Bar')
#1 %s(%d): {closure}('Foo')
#2 {main}
thrown in %s on line %d
7 changes: 3 additions & 4 deletions Zend/tests/generators/bug65161.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ foreach (testGenerator() as $i);
--EXPECTF--
Fatal error: Uncaught Error: Call to undefined function foo() in %s:%d
Stack trace:
#0 [internal function]: autoload('SyntaxError')
#1 %s(%d): spl_autoload_call('SyntaxError')
#2 %s(%d): testGenerator()
#3 {main}
#0 %s(%d): autoload('SyntaxError')
#1 %s(%d): testGenerator()
#2 {main}
thrown in %s on line %d
5 changes: 2 additions & 3 deletions Zend/tests/type_declarations/variance/loading_exception1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ Class A does not exist

Fatal error: During inheritance of B with variance dependencies: Uncaught Exception: Class A does not exist in %s:%d
Stack trace:
#0 [internal function]: {closure}('A')
#1 %s(%d): spl_autoload_call('A')
#2 {main} in %s on line %d
#0 %s(%d): {closure}('A')
#1 {main} in %s on line %d
5 changes: 2 additions & 3 deletions Zend/tests/type_declarations/variance/loading_exception2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ Class I does not exist

Fatal error: During inheritance of B with variance dependencies: Uncaught Exception: Class I does not exist in %s:%d
Stack trace:
#0 [internal function]: {closure}('I')
#1 %s(%d): spl_autoload_call('I')
#2 {main} in %s on line %d
#0 %s(%d): {closure}('I')
#1 {main} in %s on line %d
5 changes: 2 additions & 3 deletions Zend/tests/use_unlinked_class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class A implements I {
Fatal error: Uncaught ReflectionException: Class A does not exist in %s:%d
Stack trace:
#0 %s(%d): ReflectionClass->__construct('A')
#1 [internal function]: {closure}('I')
#2 %s(%d): spl_autoload_call('I')
#3 {main}
#1 %s(%d): {closure}('I')
#2 {main}
thrown in %s on line %d
3 changes: 3 additions & 0 deletions Zend/zend_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct _zend_fcall_info;
ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data);
ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);

/* The lc_name may be stack allocated! */
ZEND_API extern zend_class_entry *(*zend_autoload)(zend_string *name, zend_string *lc_name);

void init_executor(void);
void shutdown_executor(void);
void shutdown_destructors(void);
Expand Down
45 changes: 10 additions & 35 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data);
ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
ZEND_API zend_class_entry *(*zend_autoload)(zend_string *name, zend_string *lc_name);

/* true globals */
ZEND_API const zend_fcall_info empty_fcall_info = {0};
Expand Down Expand Up @@ -140,7 +141,6 @@ void init_executor(void) /* {{{ */
EG(class_table) = CG(class_table);

EG(in_autoload) = NULL;
EG(autoload_func) = NULL;
EG(error_handling) = EH_NORMAL;
EG(flags) = EG_FLAGS_INITIAL;

Expand Down Expand Up @@ -913,11 +913,9 @@ ZEND_API void zend_call_known_instance_method_with_2_params(
ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *key, uint32_t flags) /* {{{ */
{
zend_class_entry *ce = NULL;
zval args[1], *zv;
zval local_retval;
zval *zv;
zend_string *lc_name;
zend_fcall_info fcall_info;
zend_fcall_info_cache fcall_cache;
zend_string *autoload_name;

if (key) {
lc_name = key;
Expand Down Expand Up @@ -952,25 +950,22 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
return ce;
}

/* The compiler is not-reentrant. Make sure we __autoload() only during run-time
* (doesn't impact functionality of __autoload()
*/
/* The compiler is not-reentrant. Make sure we autoload only during run-time. */
if ((flags & ZEND_FETCH_CLASS_NO_AUTOLOAD) || zend_is_compiling()) {
if (!key) {
zend_string_release_ex(lc_name, 0);
}
return NULL;
}

if (!EG(autoload_func)) {
if (!zend_autoload) {
if (!key) {
zend_string_release_ex(lc_name, 0);
}
return NULL;

}

/* Verify class name before passing it to __autoload() */
/* Verify class name before passing it to the autoloader. */
if (!key && strspn(ZSTR_VAL(name), "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\") != ZSTR_LEN(name)) {
zend_string_release_ex(lc_name, 0);
return NULL;
Expand All @@ -988,39 +983,19 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
return NULL;
}

ZVAL_UNDEF(&local_retval);

if (ZSTR_VAL(name)[0] == '\\') {
ZVAL_STRINGL(&args[0], ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
autoload_name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
} else {
ZVAL_STR_COPY(&args[0], name);
autoload_name = zend_string_copy(name);
}

fcall_info.size = sizeof(fcall_info);
ZVAL_STR_COPY(&fcall_info.function_name, EG(autoload_func)->common.function_name);
fcall_info.retval = &local_retval;
fcall_info.param_count = 1;
fcall_info.params = args;
fcall_info.object = NULL;
fcall_info.no_separation = 1;

fcall_cache.function_handler = EG(autoload_func);
fcall_cache.called_scope = NULL;
fcall_cache.object = NULL;

zend_exception_save();
if ((zend_call_function(&fcall_info, &fcall_cache) == SUCCESS) && !EG(exception)) {
ce = zend_hash_find_ptr(EG(class_table), lc_name);
}
ce = zend_autoload(autoload_name, lc_name);
zend_exception_restore();

zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&fcall_info.function_name);

zend_string_release_ex(autoload_name, 0);
zend_hash_del(EG(in_autoload), lc_name);

zval_ptr_dtor(&local_retval);

if (!key) {
zend_string_release_ex(lc_name, 0);
}
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ struct _zend_executor_globals {
uint32_t persistent_classes_count;

HashTable *in_autoload;
zend_function *autoload_func;
zend_bool full_tables_cleanup;

/* for extended information support */
Expand Down
Loading