xref: /php-src/ext/spl/php_spl.c (revision 11accb5c)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Marcus Boerger <helly@php.net>                              |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 
21 #include "php.h"
22 #include "php_main.h"
23 #include "ext/standard/info.h"
24 #include "php_spl.h"
25 #include "php_spl_arginfo.h"
26 #include "spl_functions.h"
27 #include "spl_array.h"
28 #include "spl_directory.h"
29 #include "spl_iterators.h"
30 #include "spl_exceptions.h"
31 #include "spl_observer.h"
32 #include "spl_dllist.h"
33 #include "spl_fixedarray.h"
34 #include "spl_heap.h"
35 #include "zend_exceptions.h"
36 #include "zend_interfaces.h"
37 #include "main/snprintf.h"
38 
39 ZEND_TLS zend_string *spl_autoload_extensions;
40 ZEND_TLS HashTable *spl_autoload_functions;
41 
42 #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
43 
spl_find_ce_by_name(zend_string * name,bool autoload)44 static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload)
45 {
46 	zend_class_entry *ce;
47 
48 	if (!autoload) {
49 		zend_string *lc_name = zend_string_tolower(name);
50 
51 		ce = zend_hash_find_ptr(EG(class_table), lc_name);
52 		zend_string_release(lc_name);
53 	} else {
54 		ce = zend_lookup_class(name);
55 	}
56 	if (ce == NULL) {
57 		php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : "");
58 		return NULL;
59 	}
60 
61 	return ce;
62 }
63 
64 /* {{{ Return an array containing the names of all parent classes */
PHP_FUNCTION(class_parents)65 PHP_FUNCTION(class_parents)
66 {
67 	zval *obj;
68 	zend_class_entry *parent_class, *ce;
69 	bool autoload = 1;
70 
71 	/* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
72 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
73 		RETURN_THROWS();
74 	}
75 
76 	if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
77 		zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
78 		RETURN_THROWS();
79 	}
80 
81 	if (Z_TYPE_P(obj) == IS_STRING) {
82 		if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
83 			RETURN_FALSE;
84 		}
85 	} else {
86 		ce = Z_OBJCE_P(obj);
87 	}
88 
89 	array_init(return_value);
90 	parent_class = ce->parent;
91 	while (parent_class) {
92 		spl_add_class_name(return_value, parent_class, 0, 0);
93 		parent_class = parent_class->parent;
94 	}
95 }
96 /* }}} */
97 
98 /* {{{ Return all classes and interfaces implemented by SPL */
PHP_FUNCTION(class_implements)99 PHP_FUNCTION(class_implements)
100 {
101 	zval *obj;
102 	bool autoload = 1;
103 	zend_class_entry *ce;
104 
105 	/* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
106 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
107 		RETURN_THROWS();
108 	}
109 	if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
110 		zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
111 		RETURN_THROWS();
112 	}
113 
114 	if (Z_TYPE_P(obj) == IS_STRING) {
115 		if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
116 			RETURN_FALSE;
117 		}
118 	} else {
119 		ce = Z_OBJCE_P(obj);
120 	}
121 
122 	array_init(return_value);
123 	spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE);
124 }
125 /* }}} */
126 
127 /* {{{ Return all traits used by a class. */
PHP_FUNCTION(class_uses)128 PHP_FUNCTION(class_uses)
129 {
130 	zval *obj;
131 	bool autoload = 1;
132 	zend_class_entry *ce;
133 
134 	/* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
135 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
136 		RETURN_THROWS();
137 	}
138 	if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
139 		zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
140 		RETURN_THROWS();
141 	}
142 
143 	if (Z_TYPE_P(obj) == IS_STRING) {
144 		if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
145 			RETURN_FALSE;
146 		}
147 	} else {
148 		ce = Z_OBJCE_P(obj);
149 	}
150 
151 	array_init(return_value);
152 	spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT);
153 }
154 /* }}} */
155 
156 #define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
157 	spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags)
158 
159 #define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
160 	SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
161 	SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
162 	SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
163 	SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
164 	SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
165 	SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
166 	SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \
167 	SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
168 	SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
169 	SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
170 	SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
171 	SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
172 	SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
173 	SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
174 	SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
175 	SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
176 	SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
177 	SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
178 	SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
179 	SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
180 	SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
181 	SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
182 	SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
183 	SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
184 	SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
185 	SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
186 	SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
187 	SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
188 	SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
189 	SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \
190 	SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
191 	SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
192 	SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
193 	SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
194 	SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
195 	SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
196 	SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
197 	SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
198 	SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
199 	SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
200 	SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
201 	SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
202 	SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
203 	SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
204 	SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
205 	SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
206 	SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
207 	SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
208 	SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
209 	SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
210 	SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
211 	SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
212 	SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
213 	SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
214 	SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
215 
216 /* {{{ Return an array containing the names of all clsses and interfaces defined in SPL */
PHP_FUNCTION(spl_classes)217 PHP_FUNCTION(spl_classes)
218 {
219 	if (zend_parse_parameters_none() == FAILURE) {
220 		RETURN_THROWS();
221 	}
222 
223 	array_init(return_value);
224 
225 	SPL_LIST_CLASSES(return_value, 0, 0, 0)
226 }
227 /* }}} */
228 
spl_autoload(zend_string * class_name,zend_string * lc_name,const char * ext,int ext_len)229 static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
230 {
231 	zend_string *class_file;
232 	zval dummy;
233 	zend_file_handle file_handle;
234 	zend_op_array *new_op_array;
235 	zval result;
236 	int ret;
237 
238 	class_file = zend_strpprintf(0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext);
239 
240 #if DEFAULT_SLASH != '\\'
241 	{
242 		char *ptr = ZSTR_VAL(class_file);
243 		char *end = ptr + ZSTR_LEN(class_file);
244 
245 		while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
246 			*ptr = DEFAULT_SLASH;
247 		}
248 	}
249 #endif
250 
251 	zend_stream_init_filename_ex(&file_handle, class_file);
252 	ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
253 
254 	if (ret == SUCCESS) {
255 		zend_string *opened_path;
256 		if (!file_handle.opened_path) {
257 			file_handle.opened_path = zend_string_copy(class_file);
258 		}
259 		opened_path = zend_string_copy(file_handle.opened_path);
260 		ZVAL_NULL(&dummy);
261 		if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
262 			new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
263 		} else {
264 			new_op_array = NULL;
265 		}
266 		zend_string_release_ex(opened_path, 0);
267 		if (new_op_array) {
268 			uint32_t orig_jit_trace_num = EG(jit_trace_num);
269 
270 			ZVAL_UNDEF(&result);
271 			zend_execute(new_op_array, &result);
272 			EG(jit_trace_num) = orig_jit_trace_num;
273 
274 			destroy_op_array(new_op_array);
275 			efree(new_op_array);
276 			if (!EG(exception)) {
277 				zval_ptr_dtor(&result);
278 			}
279 
280 			zend_destroy_file_handle(&file_handle);
281 			zend_string_release(class_file);
282 			return zend_hash_exists(EG(class_table), lc_name);
283 		}
284 	}
285 	zend_destroy_file_handle(&file_handle);
286 	zend_string_release(class_file);
287 	return 0;
288 } /* }}} */
289 
290 /* {{{ Default autoloader implementation */
PHP_FUNCTION(spl_autoload)291 PHP_FUNCTION(spl_autoload)
292 {
293 	int pos_len, pos1_len;
294 	char *pos, *pos1;
295 	zend_string *class_name, *lc_name, *file_exts = NULL;
296 
297 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
298 		RETURN_THROWS();
299 	}
300 
301 	if (!file_exts) {
302 		file_exts = spl_autoload_extensions;
303 	}
304 
305 	if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
306 		pos = SPL_DEFAULT_FILE_EXTENSIONS;
307 		pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
308 	} else {
309 		pos = ZSTR_VAL(file_exts);
310 		pos_len = (int)ZSTR_LEN(file_exts);
311 	}
312 
313 	lc_name = zend_string_tolower(class_name);
314 	while (pos && *pos && !EG(exception)) {
315 		pos1 = strchr(pos, ',');
316 		if (pos1) {
317 			pos1_len = (int)(pos1 - pos);
318 		} else {
319 			pos1_len = pos_len;
320 		}
321 		if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
322 			break; /* loaded */
323 		}
324 		pos = pos1 ? pos1 + 1 : NULL;
325 		pos_len = pos1? pos_len - pos1_len - 1 : 0;
326 	}
327 	zend_string_release(lc_name);
328 } /* }}} */
329 
330 /* {{{ Register and return default file extensions for spl_autoload */
PHP_FUNCTION(spl_autoload_extensions)331 PHP_FUNCTION(spl_autoload_extensions)
332 {
333 	zend_string *file_exts = NULL;
334 
335 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
336 		RETURN_THROWS();
337 	}
338 
339 	if (file_exts) {
340 		if (spl_autoload_extensions) {
341 			zend_string_release_ex(spl_autoload_extensions, 0);
342 		}
343 		spl_autoload_extensions = zend_string_copy(file_exts);
344 	}
345 
346 	if (spl_autoload_extensions == NULL) {
347 		RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
348 	} else {
349 		zend_string_addref(spl_autoload_extensions);
350 		RETURN_STR(spl_autoload_extensions);
351 	}
352 } /* }}} */
353 
354 typedef struct {
355 	zend_function *func_ptr;
356 	zend_object *obj;
357 	zend_object *closure;
358 	zend_class_entry *ce;
359 } autoload_func_info;
360 
autoload_func_info_destroy(autoload_func_info * alfi)361 static void autoload_func_info_destroy(autoload_func_info *alfi) {
362 	if (alfi->obj) {
363 		zend_object_release(alfi->obj);
364 	}
365 	if (alfi->func_ptr &&
366 		UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
367 		zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
368 		zend_free_trampoline(alfi->func_ptr);
369 	}
370 	if (alfi->closure) {
371 		zend_object_release(alfi->closure);
372 	}
373 	efree(alfi);
374 }
375 
autoload_func_info_zval_dtor(zval * element)376 static void autoload_func_info_zval_dtor(zval *element)
377 {
378 	autoload_func_info_destroy(Z_PTR_P(element));
379 }
380 
autoload_func_info_from_fci(zend_fcall_info * fci,zend_fcall_info_cache * fcc)381 static autoload_func_info *autoload_func_info_from_fci(
382 		zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
383 	autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
384 	alfi->ce = fcc->calling_scope;
385 	alfi->func_ptr = fcc->function_handler;
386 	alfi->obj = fcc->object;
387 	if (alfi->obj) {
388 		GC_ADDREF(alfi->obj);
389 	}
390 	if (Z_TYPE(fci->function_name) == IS_OBJECT) {
391 		alfi->closure = Z_OBJ(fci->function_name);
392 		GC_ADDREF(alfi->closure);
393 	} else {
394 		alfi->closure = NULL;
395 	}
396 	return alfi;
397 }
398 
autoload_func_info_equals(const autoload_func_info * alfi1,const autoload_func_info * alfi2)399 static bool autoload_func_info_equals(
400 		const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
401 	if (UNEXPECTED(
402 		(alfi1->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) &&
403 		(alfi2->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)
404 	)) {
405 		return alfi1->obj == alfi2->obj
406 			&& alfi1->ce == alfi2->ce
407 			&& alfi1->closure == alfi2->closure
408 			&& zend_string_equals(alfi1->func_ptr->common.function_name, alfi2->func_ptr->common.function_name)
409 		;
410 	}
411 	return alfi1->func_ptr == alfi2->func_ptr
412 		&& alfi1->obj == alfi2->obj
413 		&& alfi1->ce == alfi2->ce
414 		&& alfi1->closure == alfi2->closure;
415 }
416 
spl_perform_autoload(zend_string * class_name,zend_string * lc_name)417 static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
418 	if (!spl_autoload_functions) {
419 		return NULL;
420 	}
421 
422 	/* We don't use ZEND_HASH_MAP_FOREACH here,
423 	 * because autoloaders may be added/removed during autoloading. */
424 	HashPosition pos;
425 	zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
426 	while (1) {
427 		autoload_func_info *alfi =
428 			zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
429 		if (!alfi) {
430 			break;
431 		}
432 
433 		zend_function *func = alfi->func_ptr;
434 		if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
435 			func = emalloc(sizeof(zend_op_array));
436 			memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
437 			zend_string_addref(func->op_array.function_name);
438 		}
439 
440 		zval param;
441 		ZVAL_STR(&param, class_name);
442 		zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, &param, NULL);
443 		if (EG(exception)) {
444 			break;
445 		}
446 
447 		if (ZSTR_HAS_CE_CACHE(class_name) &&  ZSTR_GET_CE_CACHE(class_name)) {
448 			return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
449 		} else {
450 			zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
451 			if (ce) {
452 				return ce;
453 			}
454 		}
455 
456 		zend_hash_move_forward_ex(spl_autoload_functions, &pos);
457 	}
458 	return NULL;
459 }
460 
461 /* {{{ Try all registered autoload function to load the requested class */
PHP_FUNCTION(spl_autoload_call)462 PHP_FUNCTION(spl_autoload_call)
463 {
464 	zend_string *class_name;
465 
466 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
467 		RETURN_THROWS();
468 	}
469 
470 	zend_string *lc_name = zend_string_tolower(class_name);
471 	spl_perform_autoload(class_name, lc_name);
472 	zend_string_release(lc_name);
473 } /* }}} */
474 
475 #define HT_MOVE_TAIL_TO_HEAD(ht)						        \
476 	ZEND_ASSERT(!HT_IS_PACKED(ht));						        \
477 	do {												        \
478 		Bucket tmp = (ht)->arData[(ht)->nNumUsed-1];				\
479 		memmove((ht)->arData + 1, (ht)->arData,					\
480 			sizeof(Bucket) * ((ht)->nNumUsed - 1));				\
481 		(ht)->arData[0] = tmp;									\
482 		zend_hash_rehash(ht);						        	\
483 	} while (0)
484 
spl_find_registered_function(autoload_func_info * find_alfi)485 static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
486 	if (!spl_autoload_functions) {
487 		return NULL;
488 	}
489 
490 	autoload_func_info *alfi;
491 	ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
492 		if (autoload_func_info_equals(alfi, find_alfi)) {
493 			return _p;
494 		}
495 	} ZEND_HASH_FOREACH_END();
496 	return NULL;
497 }
498 
499 /* {{{ Register given function as autoloader */
PHP_FUNCTION(spl_autoload_register)500 PHP_FUNCTION(spl_autoload_register)
501 {
502 	bool do_throw = 1;
503 	bool prepend  = 0;
504 	zend_fcall_info fci = {0};
505 	zend_fcall_info_cache fcc;
506 	autoload_func_info *alfi;
507 
508 	ZEND_PARSE_PARAMETERS_START(0, 3)
509 		Z_PARAM_OPTIONAL
510 		Z_PARAM_FUNC_OR_NULL(fci, fcc)
511 		Z_PARAM_BOOL(do_throw)
512 		Z_PARAM_BOOL(prepend)
513 	ZEND_PARSE_PARAMETERS_END();
514 
515 	if (!do_throw) {
516 		php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
517 			"spl_autoload_register() will always throw");
518 	}
519 
520 	if (!spl_autoload_functions) {
521 		ALLOC_HASHTABLE(spl_autoload_functions);
522 		zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
523 		/* Initialize as non-packed hash table for prepend functionality. */
524 		zend_hash_real_init_mixed(spl_autoload_functions);
525 	}
526 
527 	/* If first arg is not null */
528 	if (ZEND_FCI_INITIALIZED(fci)) {
529 		if (!fcc.function_handler) {
530 			/* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
531 			 * with it outselves. It is important that it is not refetched on every call,
532 			 * because calls may occur from different scopes. */
533 			zend_is_callable_ex(&fci.function_name, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL, &fcc, NULL);
534 		}
535 
536 		if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
537 			fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
538 			zend_argument_value_error(1, "must not be the spl_autoload_call() function");
539 			RETURN_THROWS();
540 		}
541 
542 		alfi = autoload_func_info_from_fci(&fci, &fcc);
543 		if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
544 			zend_function *copy = emalloc(sizeof(zend_op_array));
545 
546 			memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
547 			alfi->func_ptr->common.function_name = NULL;
548 			alfi->func_ptr = copy;
549 		}
550 	} else {
551 		alfi = emalloc(sizeof(autoload_func_info));
552 		alfi->func_ptr = zend_hash_str_find_ptr(
553 			CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
554 		alfi->obj = NULL;
555 		alfi->ce = NULL;
556 		alfi->closure = NULL;
557 	}
558 
559 	if (spl_find_registered_function(alfi)) {
560 		autoload_func_info_destroy(alfi);
561 		RETURN_TRUE;
562 	}
563 
564 	zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
565 	if (prepend && spl_autoload_functions->nNumOfElements > 1) {
566 		/* Move the newly created element to the head of the hashtable */
567 		HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
568 	}
569 
570 	RETURN_TRUE;
571 } /* }}} */
572 
573 /* {{{ Unregister given function as autoloader */
PHP_FUNCTION(spl_autoload_unregister)574 PHP_FUNCTION(spl_autoload_unregister)
575 {
576 	zend_fcall_info fci;
577 	zend_fcall_info_cache fcc;
578 
579 	ZEND_PARSE_PARAMETERS_START(1, 1)
580 		Z_PARAM_FUNC(fci, fcc)
581 	ZEND_PARSE_PARAMETERS_END();
582 
583 	if (fcc.function_handler && zend_string_equals_literal(
584 			fcc.function_handler->common.function_name, "spl_autoload_call")) {
585 		if (spl_autoload_functions) {
586 			/* Don't destroy the hash table, as we might be iterating over it right now. */
587 			zend_hash_clean(spl_autoload_functions);
588 		}
589 		RETURN_TRUE;
590 	}
591 
592 	if (!fcc.function_handler) {
593 		/* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
594 		 * with it outselves. It is important that it is not refetched on every call,
595 		 * because calls may occur from different scopes. */
596 		zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
597 	}
598 
599 	autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
600 	Bucket *p = spl_find_registered_function(alfi);
601 	autoload_func_info_destroy(alfi);
602 	if (p) {
603 		zend_hash_del_bucket(spl_autoload_functions, p);
604 		RETURN_TRUE;
605 	}
606 
607 	RETURN_FALSE;
608 } /* }}} */
609 
610 /* {{{ Return all registered autoloader functions */
PHP_FUNCTION(spl_autoload_functions)611 PHP_FUNCTION(spl_autoload_functions)
612 {
613 	autoload_func_info *alfi;
614 
615 	if (zend_parse_parameters_none() == FAILURE) {
616 		RETURN_THROWS();
617 	}
618 
619 	array_init(return_value);
620 	if (spl_autoload_functions) {
621 		ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
622 			if (alfi->closure) {
623 				GC_ADDREF(alfi->closure);
624 				add_next_index_object(return_value, alfi->closure);
625 			} else if (alfi->func_ptr->common.scope) {
626 				zval tmp;
627 
628 				array_init(&tmp);
629 				if (alfi->obj) {
630 					GC_ADDREF(alfi->obj);
631 					add_next_index_object(&tmp, alfi->obj);
632 				} else {
633 					add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
634 				}
635 				add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
636 				add_next_index_zval(return_value, &tmp);
637 			} else {
638 				add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
639 			}
640 		} ZEND_HASH_FOREACH_END();
641 	}
642 } /* }}} */
643 
644 /* {{{ Return hash id for given object */
PHP_FUNCTION(spl_object_hash)645 PHP_FUNCTION(spl_object_hash)
646 {
647 	zend_object *obj;
648 
649 	ZEND_PARSE_PARAMETERS_START(1, 1)
650 		Z_PARAM_OBJ(obj)
651 	ZEND_PARSE_PARAMETERS_END();
652 
653 	RETURN_NEW_STR(php_spl_object_hash(obj));
654 }
655 /* }}} */
656 
657 /* {{{ Returns the integer object handle for the given object */
PHP_FUNCTION(spl_object_id)658 PHP_FUNCTION(spl_object_id)
659 {
660 	zend_object *obj;
661 
662 	ZEND_PARSE_PARAMETERS_START(1, 1)
663 		Z_PARAM_OBJ(obj)
664 	ZEND_PARSE_PARAMETERS_END();
665 
666 	RETURN_LONG((zend_long)obj->handle);
667 }
668 /* }}} */
669 
php_spl_object_hash(zend_object * obj)670 PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/
671 {
672 	return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle);
673 }
674 /* }}} */
675 
spl_build_class_list_string(zval * entry,char ** list)676 static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
677 {
678 	char *res;
679 
680 	spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
681 	efree(*list);
682 	*list = res;
683 } /* }}} */
684 
685 /* {{{ PHP_MINFO(spl) */
PHP_MINFO_FUNCTION(spl)686 PHP_MINFO_FUNCTION(spl)
687 {
688 	zval list, *zv;
689 	char *strg;
690 
691 	php_info_print_table_start();
692 	php_info_print_table_row(2, "SPL support", "enabled");
693 
694 	array_init(&list);
695 	SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
696 	strg = estrdup("");
697 	ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
698 		spl_build_class_list_string(zv, &strg);
699 	} ZEND_HASH_FOREACH_END();
700 	zend_array_destroy(Z_ARR(list));
701 	php_info_print_table_row(2, "Interfaces", strg + 2);
702 	efree(strg);
703 
704 	array_init(&list);
705 	SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
706 	strg = estrdup("");
707 	ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
708 		spl_build_class_list_string(zv, &strg);
709 	} ZEND_HASH_FOREACH_END();
710 	zend_array_destroy(Z_ARR(list));
711 	php_info_print_table_row(2, "Classes", strg + 2);
712 	efree(strg);
713 
714 	php_info_print_table_end();
715 }
716 /* }}} */
717 
718 /* {{{ PHP_MINIT_FUNCTION(spl) */
PHP_MINIT_FUNCTION(spl)719 PHP_MINIT_FUNCTION(spl)
720 {
721 	zend_autoload = spl_perform_autoload;
722 
723 	PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
724 	PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
725 	PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
726 	PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
727 	PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
728 	PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
729 	PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
730 	PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
731 
732 	return SUCCESS;
733 }
734 /* }}} */
735 
PHP_RINIT_FUNCTION(spl)736 PHP_RINIT_FUNCTION(spl) /* {{{ */
737 {
738 	spl_autoload_extensions = NULL;
739 	spl_autoload_functions = NULL;
740 	return SUCCESS;
741 } /* }}} */
742 
PHP_RSHUTDOWN_FUNCTION(spl)743 PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
744 {
745 	if (spl_autoload_extensions) {
746 		zend_string_release_ex(spl_autoload_extensions, 0);
747 		spl_autoload_extensions = NULL;
748 	}
749 	if (spl_autoload_functions) {
750 		zend_hash_destroy(spl_autoload_functions);
751 		FREE_HASHTABLE(spl_autoload_functions);
752 		spl_autoload_functions = NULL;
753 	}
754 	return SUCCESS;
755 } /* }}} */
756 
757 static const zend_module_dep spl_deps[] = {
758 	ZEND_MOD_REQUIRED("json")
759 	ZEND_MOD_END
760 };
761 
762 zend_module_entry spl_module_entry = {
763 	STANDARD_MODULE_HEADER_EX, NULL,
764 	spl_deps,
765 	"SPL",
766 	ext_functions,
767 	PHP_MINIT(spl),
768 	NULL,
769 	PHP_RINIT(spl),
770 	PHP_RSHUTDOWN(spl),
771 	PHP_MINFO(spl),
772 	PHP_SPL_VERSION,
773 	STANDARD_MODULE_PROPERTIES
774 };
775