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