1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "zend.h"
21 #include "zend_API.h"
22 #include "zend_gc.h"
23 #include "zend_builtin_functions.h"
24 #include "zend_constants.h"
25 #include "zend_ini.h"
26 #include "zend_exceptions.h"
27 #include "zend_extensions.h"
28 #include "zend_closures.h"
29 #include "zend_generators.h"
30 
31 static ZEND_FUNCTION(zend_version);
32 static ZEND_FUNCTION(func_num_args);
33 static ZEND_FUNCTION(func_get_arg);
34 static ZEND_FUNCTION(func_get_args);
35 static ZEND_FUNCTION(strlen);
36 static ZEND_FUNCTION(strcmp);
37 static ZEND_FUNCTION(strncmp);
38 static ZEND_FUNCTION(strcasecmp);
39 static ZEND_FUNCTION(strncasecmp);
40 static ZEND_FUNCTION(each);
41 static ZEND_FUNCTION(error_reporting);
42 static ZEND_FUNCTION(define);
43 static ZEND_FUNCTION(defined);
44 static ZEND_FUNCTION(get_class);
45 static ZEND_FUNCTION(get_called_class);
46 static ZEND_FUNCTION(get_parent_class);
47 static ZEND_FUNCTION(method_exists);
48 static ZEND_FUNCTION(property_exists);
49 static ZEND_FUNCTION(class_exists);
50 static ZEND_FUNCTION(interface_exists);
51 static ZEND_FUNCTION(trait_exists);
52 static ZEND_FUNCTION(function_exists);
53 static ZEND_FUNCTION(class_alias);
54 static ZEND_FUNCTION(get_included_files);
55 static ZEND_FUNCTION(is_subclass_of);
56 static ZEND_FUNCTION(is_a);
57 static ZEND_FUNCTION(get_class_vars);
58 static ZEND_FUNCTION(get_object_vars);
59 static ZEND_FUNCTION(get_mangled_object_vars);
60 static ZEND_FUNCTION(get_class_methods);
61 static ZEND_FUNCTION(trigger_error);
62 static ZEND_FUNCTION(set_error_handler);
63 static ZEND_FUNCTION(restore_error_handler);
64 static ZEND_FUNCTION(set_exception_handler);
65 static ZEND_FUNCTION(restore_exception_handler);
66 static ZEND_FUNCTION(get_declared_classes);
67 static ZEND_FUNCTION(get_declared_traits);
68 static ZEND_FUNCTION(get_declared_interfaces);
69 static ZEND_FUNCTION(get_defined_functions);
70 static ZEND_FUNCTION(get_defined_vars);
71 static ZEND_FUNCTION(create_function);
72 static ZEND_FUNCTION(get_resource_type);
73 static ZEND_FUNCTION(get_resources);
74 static ZEND_FUNCTION(get_loaded_extensions);
75 static ZEND_FUNCTION(extension_loaded);
76 static ZEND_FUNCTION(get_extension_funcs);
77 static ZEND_FUNCTION(get_defined_constants);
78 static ZEND_FUNCTION(debug_backtrace);
79 static ZEND_FUNCTION(debug_print_backtrace);
80 #if ZEND_DEBUG && defined(ZTS)
81 static ZEND_FUNCTION(zend_thread_id);
82 #endif
83 static ZEND_FUNCTION(gc_mem_caches);
84 static ZEND_FUNCTION(gc_collect_cycles);
85 static ZEND_FUNCTION(gc_enabled);
86 static ZEND_FUNCTION(gc_enable);
87 static ZEND_FUNCTION(gc_disable);
88 static ZEND_FUNCTION(gc_status);
89 
90 /* {{{ arginfo */
91 ZEND_BEGIN_ARG_INFO(arginfo_zend__void, 0)
92 ZEND_END_ARG_INFO()
93 
94 ZEND_BEGIN_ARG_INFO_EX(arginfo_func_get_arg, 0, 0, 1)
95 	ZEND_ARG_INFO(0, arg_num)
96 ZEND_END_ARG_INFO()
97 
98 ZEND_BEGIN_ARG_INFO_EX(arginfo_strlen, 0, 0, 1)
99 	ZEND_ARG_INFO(0, str)
100 ZEND_END_ARG_INFO()
101 
102 ZEND_BEGIN_ARG_INFO_EX(arginfo_strcmp, 0, 0, 2)
103 	ZEND_ARG_INFO(0, str1)
104 	ZEND_ARG_INFO(0, str2)
105 ZEND_END_ARG_INFO()
106 
107 ZEND_BEGIN_ARG_INFO_EX(arginfo_strncmp, 0, 0, 3)
108 	ZEND_ARG_INFO(0, str1)
109 	ZEND_ARG_INFO(0, str2)
110 	ZEND_ARG_INFO(0, len)
111 ZEND_END_ARG_INFO()
112 
113 ZEND_BEGIN_ARG_INFO_EX(arginfo_each, 0, 0, 1)
114 	ZEND_ARG_INFO(1, arr)
115 ZEND_END_ARG_INFO()
116 
117 ZEND_BEGIN_ARG_INFO_EX(arginfo_error_reporting, 0, 0, 0)
118 	ZEND_ARG_INFO(0, new_error_level)
119 ZEND_END_ARG_INFO()
120 
121 ZEND_BEGIN_ARG_INFO_EX(arginfo_define, 0, 0, 2)
122 	ZEND_ARG_INFO(0, constant_name)
123 	ZEND_ARG_INFO(0, value)
124 	ZEND_ARG_INFO(0, case_insensitive)
125 ZEND_END_ARG_INFO()
126 
127 ZEND_BEGIN_ARG_INFO_EX(arginfo_defined, 0, 0, 1)
128 	ZEND_ARG_INFO(0, constant_name)
129 ZEND_END_ARG_INFO()
130 
131 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class, 0, 0, 0)
132 	ZEND_ARG_INFO(0, object)
133 ZEND_END_ARG_INFO()
134 
135 ZEND_BEGIN_ARG_INFO_EX(arginfo_is_subclass_of, 0, 0, 2)
136 	ZEND_ARG_INFO(0, object)
137 	ZEND_ARG_INFO(0, class_name)
138 	ZEND_ARG_INFO(0, allow_string)
139 ZEND_END_ARG_INFO()
140 
141 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class_vars, 0, 0, 1)
142 	ZEND_ARG_INFO(0, class_name)
143 ZEND_END_ARG_INFO()
144 
145 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_object_vars, 0, 0, 1)
146 	ZEND_ARG_INFO(0, obj)
147 ZEND_END_ARG_INFO()
148 
149 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_mangled_object_vars, 0, 0, 1)
150 	ZEND_ARG_INFO(0, obj)
151 ZEND_END_ARG_INFO()
152 
153 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class_methods, 0, 0, 1)
154 	ZEND_ARG_INFO(0, class)
155 ZEND_END_ARG_INFO()
156 
157 ZEND_BEGIN_ARG_INFO_EX(arginfo_method_exists, 0, 0, 2)
158 	ZEND_ARG_INFO(0, object)
159 	ZEND_ARG_INFO(0, method)
160 ZEND_END_ARG_INFO()
161 
162 ZEND_BEGIN_ARG_INFO_EX(arginfo_property_exists, 0, 0, 2)
163 	ZEND_ARG_INFO(0, object_or_class)
164 	ZEND_ARG_INFO(0, property_name)
165 ZEND_END_ARG_INFO()
166 
167 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_exists, 0, 0, 1)
168 	ZEND_ARG_INFO(0, classname)
169 	ZEND_ARG_INFO(0, autoload)
170 ZEND_END_ARG_INFO()
171 
172 ZEND_BEGIN_ARG_INFO_EX(arginfo_trait_exists, 0, 0, 1)
173 	ZEND_ARG_INFO(0, traitname)
174 	ZEND_ARG_INFO(0, autoload)
175 ZEND_END_ARG_INFO()
176 
177 ZEND_BEGIN_ARG_INFO_EX(arginfo_function_exists, 0, 0, 1)
178 	ZEND_ARG_INFO(0, function_name)
179 ZEND_END_ARG_INFO()
180 
181 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_alias, 0, 0, 2)
182 	ZEND_ARG_INFO(0, user_class_name)
183 	ZEND_ARG_INFO(0, alias_name)
184 	ZEND_ARG_INFO(0, autoload)
185 ZEND_END_ARG_INFO()
186 
187 ZEND_BEGIN_ARG_INFO_EX(arginfo_trigger_error, 0, 0, 1)
188 	ZEND_ARG_INFO(0, message)
189 	ZEND_ARG_INFO(0, error_type)
190 ZEND_END_ARG_INFO()
191 
192 ZEND_BEGIN_ARG_INFO_EX(arginfo_set_error_handler, 0, 0, 1)
193 	ZEND_ARG_INFO(0, error_handler)
194 	ZEND_ARG_INFO(0, error_types)
195 ZEND_END_ARG_INFO()
196 
197 ZEND_BEGIN_ARG_INFO_EX(arginfo_set_exception_handler, 0, 0, 1)
198 	ZEND_ARG_INFO(0, exception_handler)
199 ZEND_END_ARG_INFO()
200 
201 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_defined_functions, 0, 0, 0)
202 	ZEND_ARG_INFO(0, exclude_disabled)
203 ZEND_END_ARG_INFO()
204 
205 ZEND_BEGIN_ARG_INFO_EX(arginfo_create_function, 0, 0, 2)
206 	ZEND_ARG_INFO(0, args)
207 	ZEND_ARG_INFO(0, code)
208 ZEND_END_ARG_INFO()
209 
210 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resource_type, 0, 0, 1)
211 	ZEND_ARG_INFO(0, res)
212 ZEND_END_ARG_INFO()
213 
214 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resources, 0, 0, 0)
215 	ZEND_ARG_INFO(0, type)
216 ZEND_END_ARG_INFO()
217 
218 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_loaded_extensions, 0, 0, 0)
219 	ZEND_ARG_INFO(0, zend_extensions)
220 ZEND_END_ARG_INFO()
221 
222 ZEND_BEGIN_ARG_INFO_EX(arginfo_get_defined_constants, 0, 0, 0)
223 	ZEND_ARG_INFO(0, categorize)
224 ZEND_END_ARG_INFO()
225 
226 ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_backtrace, 0, 0, 0)
227 	ZEND_ARG_INFO(0, options)
228 	ZEND_ARG_INFO(0, limit)
229 ZEND_END_ARG_INFO()
230 
231 ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_print_backtrace, 0, 0, 0)
232 	ZEND_ARG_INFO(0, options)
233 	ZEND_ARG_INFO(0, limit)
234 ZEND_END_ARG_INFO()
235 
236 ZEND_BEGIN_ARG_INFO_EX(arginfo_extension_loaded, 0, 0, 1)
237 	ZEND_ARG_INFO(0, extension_name)
238 ZEND_END_ARG_INFO()
239 
240 /* }}} */
241 
242 static const zend_function_entry builtin_functions[] = { /* {{{ */
243 	ZEND_FE(zend_version,		arginfo_zend__void)
244 	ZEND_FE(func_num_args,		arginfo_zend__void)
245 	ZEND_FE(func_get_arg,		arginfo_func_get_arg)
246 	ZEND_FE(func_get_args,		arginfo_zend__void)
247 	ZEND_FE(strlen,			arginfo_strlen)
248 	ZEND_FE(strcmp,			arginfo_strcmp)
249 	ZEND_FE(strncmp,		arginfo_strncmp)
250 	ZEND_FE(strcasecmp,		arginfo_strcmp)
251 	ZEND_FE(strncasecmp,		arginfo_strncmp)
252 	ZEND_FE(each,			arginfo_each)
253 	ZEND_FE(error_reporting,	arginfo_error_reporting)
254 	ZEND_FE(define,			arginfo_define)
255 	ZEND_FE(defined,		arginfo_defined)
256 	ZEND_FE(get_class,		arginfo_get_class)
257 	ZEND_FE(get_called_class,	arginfo_zend__void)
258 	ZEND_FE(get_parent_class,	arginfo_get_class)
259 	ZEND_FE(method_exists,		arginfo_method_exists)
260 	ZEND_FE(property_exists,	arginfo_property_exists)
261 	ZEND_FE(class_exists,		arginfo_class_exists)
262 	ZEND_FE(interface_exists,	arginfo_class_exists)
263 	ZEND_FE(trait_exists,		arginfo_trait_exists)
264 	ZEND_FE(function_exists,	arginfo_function_exists)
265 	ZEND_FE(class_alias,		arginfo_class_alias)
266 	ZEND_FE(get_included_files,	arginfo_zend__void)
267 	ZEND_FALIAS(get_required_files,	get_included_files,		arginfo_zend__void)
268 	ZEND_FE(is_subclass_of,		arginfo_is_subclass_of)
269 	ZEND_FE(is_a,			arginfo_is_subclass_of)
270 	ZEND_FE(get_class_vars,		arginfo_get_class_vars)
271 	ZEND_FE(get_object_vars,	arginfo_get_object_vars)
272 	ZEND_FE(get_mangled_object_vars,	arginfo_get_mangled_object_vars)
273 	ZEND_FE(get_class_methods,	arginfo_get_class_methods)
274 	ZEND_FE(trigger_error,		arginfo_trigger_error)
275 	ZEND_FALIAS(user_error,		trigger_error,		arginfo_trigger_error)
276 	ZEND_FE(set_error_handler,		arginfo_set_error_handler)
277 	ZEND_FE(restore_error_handler,		arginfo_zend__void)
278 	ZEND_FE(set_exception_handler,		arginfo_set_exception_handler)
279 	ZEND_FE(restore_exception_handler,	arginfo_zend__void)
280 	ZEND_FE(get_declared_classes, 		arginfo_zend__void)
281 	ZEND_FE(get_declared_traits, 		arginfo_zend__void)
282 	ZEND_FE(get_declared_interfaces, 	arginfo_zend__void)
283 	ZEND_FE(get_defined_functions, 		arginfo_get_defined_functions)
284 	ZEND_FE(get_defined_vars,		arginfo_zend__void)
285 	ZEND_DEP_FE(create_function,		arginfo_create_function)
286 	ZEND_FE(get_resource_type,		arginfo_get_resource_type)
287 	ZEND_FE(get_resources,			arginfo_get_resources)
288 	ZEND_FE(get_loaded_extensions,		arginfo_get_loaded_extensions)
289 	ZEND_FE(extension_loaded,		arginfo_extension_loaded)
290 	ZEND_FE(get_extension_funcs,		arginfo_extension_loaded)
291 	ZEND_FE(get_defined_constants,		arginfo_get_defined_constants)
292 	ZEND_FE(debug_backtrace, 		arginfo_debug_backtrace)
293 	ZEND_FE(debug_print_backtrace, 		arginfo_debug_print_backtrace)
294 #if ZEND_DEBUG && defined(ZTS)
295 	ZEND_FE(zend_thread_id,		NULL)
296 #endif
297 	ZEND_FE(gc_mem_caches,      arginfo_zend__void)
298 	ZEND_FE(gc_collect_cycles, 	arginfo_zend__void)
299 	ZEND_FE(gc_enabled, 		arginfo_zend__void)
300 	ZEND_FE(gc_enable, 		arginfo_zend__void)
301 	ZEND_FE(gc_disable, 		arginfo_zend__void)
302 	ZEND_FE(gc_status, 		arginfo_zend__void)
303 	ZEND_FE_END
304 };
305 /* }}} */
306 
ZEND_MINIT_FUNCTIONnull307 ZEND_MINIT_FUNCTION(core) { /* {{{ */
308 	zend_class_entry class_entry;
309 
310 	INIT_CLASS_ENTRY(class_entry, "stdClass", NULL);
311 	zend_standard_class_def = zend_register_internal_class(&class_entry);
312 
313 	zend_register_default_classes();
314 
315 	return SUCCESS;
316 }
317 /* }}} */
318 
319 zend_module_entry zend_builtin_module = { /* {{{ */
320 	STANDARD_MODULE_HEADER,
321 	"Core",
322 	builtin_functions,
323 	ZEND_MINIT(core),
324 	NULL,
325 	NULL,
326 	NULL,
327 	NULL,
328 	ZEND_VERSION,
329 	STANDARD_MODULE_PROPERTIES
330 };
331 /* }}} */
332 
zend_startup_builtin_functions(void)333 int zend_startup_builtin_functions(void) /* {{{ */
334 {
335 	zend_builtin_module.module_number = 0;
336 	zend_builtin_module.type = MODULE_PERSISTENT;
337 	return (EG(current_module) = zend_register_module_ex(&zend_builtin_module)) == NULL ? FAILURE : SUCCESS;
338 }
339 /* }}} */
340 
341 /* {{{ proto string zend_version(void)
342    Get the version of the Zend Engine */
ZEND_FUNCTIONnull343 ZEND_FUNCTION(zend_version)
344 {
345 	if (zend_parse_parameters_none() == FAILURE) {
346 		return;
347 	}
348 
349 	RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1);
350 }
351 /* }}} */
352 
353 /* {{{ proto int gc_mem_caches(void)
354    Reclaims memory used by MM caches.
355    Returns number of freed bytes */
ZEND_FUNCTIONnull356 ZEND_FUNCTION(gc_mem_caches)
357 {
358 	if (zend_parse_parameters_none() == FAILURE) {
359 		return;
360 	}
361 
362 	RETURN_LONG(zend_mm_gc(zend_mm_get_heap()));
363 }
364 /* }}} */
365 
366 /* {{{ proto int gc_collect_cycles(void)
367    Forces collection of any existing garbage cycles.
368    Returns number of freed zvals */
ZEND_FUNCTIONnull369 ZEND_FUNCTION(gc_collect_cycles)
370 {
371 	if (zend_parse_parameters_none() == FAILURE) {
372 		return;
373 	}
374 
375 	RETURN_LONG(gc_collect_cycles());
376 }
377 /* }}} */
378 
379 /* {{{ proto void gc_enabled(void)
380    Returns status of the circular reference collector */
ZEND_FUNCTIONnull381 ZEND_FUNCTION(gc_enabled)
382 {
383 	if (zend_parse_parameters_none() == FAILURE) {
384 		return;
385 	}
386 
387 	RETURN_BOOL(gc_enabled());
388 }
389 /* }}} */
390 
391 /* {{{ proto void gc_enable(void)
392    Activates the circular reference collector */
ZEND_FUNCTIONnull393 ZEND_FUNCTION(gc_enable)
394 {
395 	zend_string *key;
396 
397 	if (zend_parse_parameters_none() == FAILURE) {
398 		return;
399 	}
400 
401 	key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
402 	zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
403 	zend_string_release_ex(key, 0);
404 }
405 /* }}} */
406 
407 /* {{{ proto void gc_disable(void)
408    Deactivates the circular reference collector */
ZEND_FUNCTIONnull409 ZEND_FUNCTION(gc_disable)
410 {
411 	zend_string *key;
412 
413 	if (zend_parse_parameters_none() == FAILURE) {
414 		return;
415 	}
416 
417 	key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
418 	zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
419 	zend_string_release_ex(key, 0);
420 }
421 /* }}} */
422 
423 /* {{{ proto array gc_status(void)
424    Returns current GC statistics */
ZEND_FUNCTIONnull425 ZEND_FUNCTION(gc_status)
426 {
427 	zend_gc_status status;
428 
429 	if (zend_parse_parameters_none() == FAILURE) {
430 		return;
431 	}
432 
433 	zend_gc_get_status(&status);
434 
435 	array_init_size(return_value, 3);
436 
437 	add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
438 	add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
439 	add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
440 	add_assoc_long_ex(return_value, "roots", sizeof("roots")-1, (long)status.num_roots);
441 }
442 /* }}} */
443 
444 /* {{{ proto int func_num_args(void)
445    Get the number of arguments that were passed to the function */
ZEND_FUNCTIONnull446 ZEND_FUNCTION(func_num_args)
447 {
448 	zend_execute_data *ex = EX(prev_execute_data);
449 
450 	if (zend_parse_parameters_none() == FAILURE) {
451 		return;
452 	}
453 
454 	if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
455 		zend_error(E_WARNING, "func_num_args():  Called from the global scope - no function context");
456 		RETURN_LONG(-1);
457 	}
458 
459 	if (zend_forbid_dynamic_call("func_num_args()") == FAILURE) {
460 		RETURN_LONG(-1);
461 	}
462 
463 	RETURN_LONG(ZEND_CALL_NUM_ARGS(ex));
464 }
465 /* }}} */
466 
467 /* {{{ proto mixed func_get_arg(int arg_num)
468    Get the $arg_num'th argument that was passed to the function */
ZEND_FUNCTIONnull469 ZEND_FUNCTION(func_get_arg)
470 {
471 	uint32_t arg_count, first_extra_arg;
472 	zval *arg;
473 	zend_long requested_offset;
474 	zend_execute_data *ex;
475 
476 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) == FAILURE) {
477 		return;
478 	}
479 
480 	if (requested_offset < 0) {
481 		zend_error(E_WARNING, "func_get_arg():  The argument number should be >= 0");
482 		RETURN_FALSE;
483 	}
484 
485 	ex = EX(prev_execute_data);
486 	if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
487 		zend_error(E_WARNING, "func_get_arg():  Called from the global scope - no function context");
488 		RETURN_FALSE;
489 	}
490 
491 	if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
492 		RETURN_FALSE;
493 	}
494 
495 	arg_count = ZEND_CALL_NUM_ARGS(ex);
496 
497 	if ((zend_ulong)requested_offset >= arg_count) {
498 		zend_error(E_WARNING, "func_get_arg():  Argument " ZEND_LONG_FMT " not passed to function", requested_offset);
499 		RETURN_FALSE;
500 	}
501 
502 	first_extra_arg = ex->func->op_array.num_args;
503 	if ((zend_ulong)requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
504 		arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
505 	} else {
506 		arg = ZEND_CALL_ARG(ex, requested_offset + 1);
507 	}
508 	if (EXPECTED(!Z_ISUNDEF_P(arg))) {
509 		ZVAL_COPY_DEREF(return_value, arg);
510 	}
511 }
512 /* }}} */
513 
514 /* {{{ proto array func_get_args()
515    Get an array of the arguments that were passed to the function */
ZEND_FUNCTIONnull516 ZEND_FUNCTION(func_get_args)
517 {
518 	zval *p, *q;
519 	uint32_t arg_count, first_extra_arg;
520 	uint32_t i;
521 	zend_execute_data *ex = EX(prev_execute_data);
522 
523 	if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
524 		zend_error(E_WARNING, "func_get_args():  Called from the global scope - no function context");
525 		RETURN_FALSE;
526 	}
527 
528 	if (zend_forbid_dynamic_call("func_get_args()") == FAILURE) {
529 		RETURN_FALSE;
530 	}
531 
532 	arg_count = ZEND_CALL_NUM_ARGS(ex);
533 
534 	if (arg_count) {
535 		array_init_size(return_value, arg_count);
536 		first_extra_arg = ex->func->op_array.num_args;
537 		zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
538 		ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
539 			i = 0;
540 			p = ZEND_CALL_ARG(ex, 1);
541 			if (arg_count > first_extra_arg) {
542 				while (i < first_extra_arg) {
543 					q = p;
544 					if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
545 						ZVAL_DEREF(q);
546 						if (Z_OPT_REFCOUNTED_P(q)) {
547 							Z_ADDREF_P(q);
548 						}
549 						ZEND_HASH_FILL_SET(q);
550 					} else {
551 						ZEND_HASH_FILL_SET_NULL();
552 					}
553 					ZEND_HASH_FILL_NEXT();
554 					p++;
555 					i++;
556 				}
557 				p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T);
558 			}
559 			while (i < arg_count) {
560 				q = p;
561 				if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
562 					ZVAL_DEREF(q);
563 					if (Z_OPT_REFCOUNTED_P(q)) {
564 						Z_ADDREF_P(q);
565 					}
566 					ZEND_HASH_FILL_SET(q);
567 				} else {
568 					ZEND_HASH_FILL_SET_NULL();
569 				}
570 				ZEND_HASH_FILL_NEXT();
571 				p++;
572 				i++;
573 			}
574 		} ZEND_HASH_FILL_END();
575 		Z_ARRVAL_P(return_value)->nNumOfElements = arg_count;
576 	} else {
577 		RETURN_EMPTY_ARRAY();
578 	}
579 }
580 /* }}} */
581 
582 /* {{{ proto int strlen(string str)
583    Get string length
584    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
ZEND_FUNCTIONnull585 ZEND_FUNCTION(strlen)
586 {
587 	zend_string *s;
588 
589 	ZEND_PARSE_PARAMETERS_START(1, 1)
590 		Z_PARAM_STR(s)
591 	ZEND_PARSE_PARAMETERS_END();
592 
593 	RETVAL_LONG(ZSTR_LEN(s));
594 }
595 /* }}} */
596 
597 /* {{{ proto int strcmp(string str1, string str2)
598    Binary safe string comparison */
ZEND_FUNCTIONnull599 ZEND_FUNCTION(strcmp)
600 {
601 	zend_string *s1, *s2;
602 
603 	ZEND_PARSE_PARAMETERS_START(2, 2)
604 		Z_PARAM_STR(s1)
605 		Z_PARAM_STR(s2)
606 	ZEND_PARSE_PARAMETERS_END();
607 
608 	RETURN_LONG(zend_binary_strcmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
609 }
610 /* }}} */
611 
612 /* {{{ proto int strncmp(string str1, string str2, int len)
613    Binary safe string comparison */
ZEND_FUNCTIONnull614 ZEND_FUNCTION(strncmp)
615 {
616 	zend_string *s1, *s2;
617 	zend_long len;
618 
619 	ZEND_PARSE_PARAMETERS_START(3, 3)
620 		Z_PARAM_STR(s1)
621 		Z_PARAM_STR(s2)
622 		Z_PARAM_LONG(len)
623 	ZEND_PARSE_PARAMETERS_END();
624 
625 	if (len < 0) {
626 		zend_error(E_WARNING, "Length must be greater than or equal to 0");
627 		RETURN_FALSE;
628 	}
629 
630 	RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
631 }
632 /* }}} */
633 
634 /* {{{ proto int strcasecmp(string str1, string str2)
635    Binary safe case-insensitive string comparison */
ZEND_FUNCTIONnull636 ZEND_FUNCTION(strcasecmp)
637 {
638 	zend_string *s1, *s2;
639 
640 	ZEND_PARSE_PARAMETERS_START(2, 2)
641 		Z_PARAM_STR(s1)
642 		Z_PARAM_STR(s2)
643 	ZEND_PARSE_PARAMETERS_END();
644 
645 	RETURN_LONG(zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
646 }
647 /* }}} */
648 
649 /* {{{ proto int strncasecmp(string str1, string str2, int len)
650    Binary safe string comparison */
ZEND_FUNCTIONnull651 ZEND_FUNCTION(strncasecmp)
652 {
653 	zend_string *s1, *s2;
654 	zend_long len;
655 
656 	ZEND_PARSE_PARAMETERS_START(3, 3)
657 		Z_PARAM_STR(s1)
658 		Z_PARAM_STR(s2)
659 		Z_PARAM_LONG(len)
660 	ZEND_PARSE_PARAMETERS_END();
661 
662 	if (len < 0) {
663 		zend_error(E_WARNING, "Length must be greater than or equal to 0");
664 		RETURN_FALSE;
665 	}
666 
667 	RETURN_LONG(zend_binary_strncasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
668 }
669 /* }}} */
670 
671 /* {{{ proto mixed each(array &arr)
672    Return the currently pointed key..value pair in the passed array, and advance the pointer to the next element, or false if there is no element at this place */
ZEND_FUNCTIONnull673 ZEND_FUNCTION(each)
674 {
675 	zval *array, *entry, tmp;
676 	zend_ulong num_key;
677 	HashTable *target_hash;
678 	zend_string *key;
679 
680 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z/", &array) == FAILURE) {
681 		return;
682 	}
683 
684 	if (!EG(each_deprecation_thrown)) {
685 		zend_error(E_DEPRECATED, "The each() function is deprecated. This message will be suppressed on further calls");
686 		EG(each_deprecation_thrown) = 1;
687 	}
688 
689 	target_hash = HASH_OF(array);
690 	if (!target_hash) {
691 		zend_error(E_WARNING,"Variable passed to each() is not an array or object");
692 		return;
693 	}
694 	while (1) {
695 		entry = zend_hash_get_current_data(target_hash);
696 		if (!entry) {
697 			RETURN_FALSE;
698 		} else if (Z_TYPE_P(entry) == IS_INDIRECT) {
699 			entry = Z_INDIRECT_P(entry);
700 			if (Z_TYPE_P(entry) == IS_UNDEF) {
701 				zend_hash_move_forward(target_hash);
702 				continue;
703 			}
704 		}
705 		break;
706 	}
707 	array_init_size(return_value, 4);
708 	zend_hash_real_init_mixed(Z_ARRVAL_P(return_value));
709 
710 	/* add value elements */
711 	ZVAL_DEREF(entry);
712 	if (Z_REFCOUNTED_P(entry)) {
713 		GC_ADDREF_EX(Z_COUNTED_P(entry), 2);
714 	}
715 	zend_hash_index_add_new(Z_ARRVAL_P(return_value), 1, entry);
716 	zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_VALUE), entry);
717 
718 	/* add the key elements */
719 	if (zend_hash_get_current_key(target_hash, &key, &num_key) == HASH_KEY_IS_STRING) {
720 		ZVAL_STR_COPY(&tmp, key);
721 		Z_TRY_ADDREF(tmp);
722 	} else {
723 		ZVAL_LONG(&tmp, num_key);
724 	}
725 	zend_hash_index_add_new(Z_ARRVAL_P(return_value), 0, &tmp);
726 	zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_KEY), &tmp);
727 	zend_hash_move_forward(target_hash);
728 }
729 /* }}} */
730 
731 /* {{{ proto int error_reporting([int new_error_level])
732    Return the current error_reporting level, and if an argument was passed - change to the new level */
ZEND_FUNCTIONnull733 ZEND_FUNCTION(error_reporting)
734 {
735 	zval *err = NULL;
736 	int old_error_reporting;
737 
738 	ZEND_PARSE_PARAMETERS_START(0, 1)
739 		Z_PARAM_OPTIONAL
740 		Z_PARAM_ZVAL(err)
741 	ZEND_PARSE_PARAMETERS_END();
742 
743 	old_error_reporting = EG(error_reporting);
744 	if (ZEND_NUM_ARGS() != 0) {
745 		zend_string *new_val = zval_try_get_string(err);
746 
747 		if (UNEXPECTED(!new_val)) {
748 			return;
749 		}
750 
751 		do {
752 			zend_ini_entry *p = EG(error_reporting_ini_entry);
753 
754 			if (!p) {
755 				zval *zv = zend_hash_find_ex(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), 1);
756 				if (zv) {
757 					p = EG(error_reporting_ini_entry) = (zend_ini_entry*)Z_PTR_P(zv);
758 				} else {
759 					break;
760 				}
761 			}
762 			if (!p->modified) {
763 				if (!EG(modified_ini_directives)) {
764 					ALLOC_HASHTABLE(EG(modified_ini_directives));
765 					zend_hash_init(EG(modified_ini_directives), 8, NULL, NULL, 0);
766 				}
767 				if (EXPECTED(zend_hash_add_ptr(EG(modified_ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), p) != NULL)) {
768 					p->orig_value = p->value;
769 					p->orig_modifiable = p->modifiable;
770 					p->modified = 1;
771 				}
772 			} else if (p->orig_value != p->value) {
773 				zend_string_release_ex(p->value, 0);
774 			}
775 
776 			p->value = new_val;
777 			if (Z_TYPE_P(err) == IS_LONG) {
778 				EG(error_reporting) = Z_LVAL_P(err);
779 			} else {
780 				EG(error_reporting) = atoi(ZSTR_VAL(p->value));
781 			}
782 		} while (0);
783 	}
784 
785 	RETVAL_LONG(old_error_reporting);
786 }
787 /* }}} */
788 
validate_constant_array(HashTable *ht)789 static int validate_constant_array(HashTable *ht) /* {{{ */
790 {
791 	int ret = 1;
792 	zval *val;
793 
794 	GC_PROTECT_RECURSION(ht);
795 	ZEND_HASH_FOREACH_VAL_IND(ht, val) {
796 		ZVAL_DEREF(val);
797 		if (Z_REFCOUNTED_P(val)) {
798 			if (Z_TYPE_P(val) == IS_ARRAY) {
799 				if (Z_REFCOUNTED_P(val)) {
800 					if (Z_IS_RECURSIVE_P(val)) {
801 						zend_error(E_WARNING, "Constants cannot be recursive arrays");
802 						ret = 0;
803 						break;
804 					} else if (!validate_constant_array(Z_ARRVAL_P(val))) {
805 						ret = 0;
806 						break;
807 					}
808 				}
809 			} else if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_RESOURCE) {
810 				zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
811 				ret = 0;
812 				break;
813 			}
814 		}
815 	} ZEND_HASH_FOREACH_END();
816 	GC_UNPROTECT_RECURSION(ht);
817 	return ret;
818 }
819 /* }}} */
820 
copy_constant_array(zval *dst, zval *src)821 static void copy_constant_array(zval *dst, zval *src) /* {{{ */
822 {
823 	zend_string *key;
824 	zend_ulong idx;
825 	zval *new_val, *val;
826 
827 	array_init_size(dst, zend_hash_num_elements(Z_ARRVAL_P(src)));
828 	ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(src), idx, key, val) {
829 		/* constant arrays can't contain references */
830 		ZVAL_DEREF(val);
831 		if (key) {
832 			new_val = zend_hash_add_new(Z_ARRVAL_P(dst), key, val);
833 		} else {
834 			new_val = zend_hash_index_add_new(Z_ARRVAL_P(dst), idx, val);
835 		}
836 		if (Z_TYPE_P(val) == IS_ARRAY) {
837 			if (Z_REFCOUNTED_P(val)) {
838 				copy_constant_array(new_val, val);
839 			}
840 		} else {
841 			Z_TRY_ADDREF_P(val);
842 		}
843 	} ZEND_HASH_FOREACH_END();
844 }
845 /* }}} */
846 
847 /* {{{ proto bool define(string constant_name, mixed value[, bool case_insensitive])
848    Define a new constant */
ZEND_FUNCTIONnull849 ZEND_FUNCTION(define)
850 {
851 	zend_string *name;
852 	zval *val, val_free;
853 	zend_bool non_cs = 0;
854 	int case_sensitive = CONST_CS;
855 	zend_constant c;
856 
857 	ZEND_PARSE_PARAMETERS_START(2, 3)
858 		Z_PARAM_STR(name)
859 		Z_PARAM_ZVAL(val)
860 		Z_PARAM_OPTIONAL
861 		Z_PARAM_BOOL(non_cs)
862 	ZEND_PARSE_PARAMETERS_END();
863 
864 	if (non_cs) {
865 		case_sensitive = 0;
866 	}
867 
868 	if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
869 		zend_error(E_WARNING, "Class constants cannot be defined or redefined");
870 		RETURN_FALSE;
871 	}
872 
873 	ZVAL_UNDEF(&val_free);
874 
875 repeat:
876 	switch (Z_TYPE_P(val)) {
877 		case IS_LONG:
878 		case IS_DOUBLE:
879 		case IS_STRING:
880 		case IS_FALSE:
881 		case IS_TRUE:
882 		case IS_NULL:
883 		case IS_RESOURCE:
884 			break;
885 		case IS_ARRAY:
886 			if (Z_REFCOUNTED_P(val)) {
887 				if (!validate_constant_array(Z_ARRVAL_P(val))) {
888 					RETURN_FALSE;
889 				} else {
890 					copy_constant_array(&c.value, val);
891 					goto register_constant;
892 				}
893 			}
894 			break;
895 		case IS_OBJECT:
896 			if (Z_TYPE(val_free) == IS_UNDEF) {
897 				if (Z_OBJ_HT_P(val)->get) {
898 					val = Z_OBJ_HT_P(val)->get(val, &val_free);
899 					goto repeat;
900 				} else if (Z_OBJ_HT_P(val)->cast_object) {
901 					if (Z_OBJ_HT_P(val)->cast_object(val, &val_free, IS_STRING) == SUCCESS) {
902 						val = &val_free;
903 						break;
904 					}
905 				}
906 			}
907 			/* no break */
908 		default:
909 			zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
910 			zval_ptr_dtor(&val_free);
911 			RETURN_FALSE;
912 	}
913 
914 	ZVAL_COPY(&c.value, val);
915 	zval_ptr_dtor(&val_free);
916 
917 register_constant:
918 	if (non_cs) {
919 		zend_error(E_DEPRECATED,
920 			"define(): Declaration of case-insensitive constants is deprecated");
921 	}
922 
923 	/* non persistent */
924 	ZEND_CONSTANT_SET_FLAGS(&c, case_sensitive, PHP_USER_CONSTANT);
925 	c.name = zend_string_copy(name);
926 	if (zend_register_constant(&c) == SUCCESS) {
927 		RETURN_TRUE;
928 	} else {
929 		RETURN_FALSE;
930 	}
931 }
932 /* }}} */
933 
934 /* {{{ proto bool defined(string constant_name)
935    Check whether a constant exists
936    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
ZEND_FUNCTIONnull937 ZEND_FUNCTION(defined)
938 {
939 	zend_string *name;
940 
941 	ZEND_PARSE_PARAMETERS_START(1, 1)
942 		Z_PARAM_STR(name)
943 	ZEND_PARSE_PARAMETERS_END();
944 
945 	if (zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT | ZEND_GET_CONSTANT_NO_DEPRECATION_CHECK)) {
946 		RETURN_TRUE;
947 	} else {
948 		RETURN_FALSE;
949 	}
950 }
951 /* }}} */
952 
953 /* {{{ proto string get_class([object object])
954    Retrieves the class name */
ZEND_FUNCTIONnull955 ZEND_FUNCTION(get_class)
956 {
957 	zval *obj = NULL;
958 
959 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {
960 		RETURN_FALSE;
961 	}
962 
963 	if (!obj) {
964 		zend_class_entry *scope = zend_get_executed_scope();
965 
966 		if (scope) {
967 			RETURN_STR_COPY(scope->name);
968 		} else {
969 			zend_error(E_WARNING, "get_class() called without object from outside a class");
970 			RETURN_FALSE;
971 		}
972 	}
973 
974 	RETURN_STR_COPY(Z_OBJCE_P(obj)->name);
975 }
976 /* }}} */
977 
978 /* {{{ proto string get_called_class()
979    Retrieves the "Late Static Binding" class name */
ZEND_FUNCTIONnull980 ZEND_FUNCTION(get_called_class)
981 {
982 	zend_class_entry *called_scope;
983 
984 	if (zend_parse_parameters_none() == FAILURE) {
985 		return;
986 	}
987 
988 	called_scope = zend_get_called_scope(execute_data);
989 	if (called_scope) {
990 		RETURN_STR_COPY(called_scope->name);
991 	} else {
992 		zend_class_entry *scope = zend_get_executed_scope();
993 		if (!scope)  {
994 			zend_error(E_WARNING, "get_called_class() called from outside a class");
995 		}
996 	}
997 	RETURN_FALSE;
998 }
999 /* }}} */
1000 
1001 /* {{{ proto mixed get_parent_class([mixed object])
1002    Retrieves the parent class name for object or class or current scope or false if not in a scope. */
ZEND_FUNCTIONnull1003 ZEND_FUNCTION(get_parent_class)
1004 {
1005 	zval *arg;
1006 	zend_class_entry *ce = NULL;
1007 
1008 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1009 		return;
1010 	}
1011 
1012 	if (!ZEND_NUM_ARGS()) {
1013 		ce = zend_get_executed_scope();
1014 		if (ce && ce->parent) {
1015 			RETURN_STR_COPY(ce->parent->name);
1016 		} else {
1017 			RETURN_FALSE;
1018 		}
1019 	}
1020 
1021 	if (Z_TYPE_P(arg) == IS_OBJECT) {
1022 		ce = Z_OBJ_P(arg)->ce;
1023 	} else if (Z_TYPE_P(arg) == IS_STRING) {
1024 		ce = zend_lookup_class(Z_STR_P(arg));
1025 	}
1026 
1027 	if (ce && ce->parent) {
1028 		RETURN_STR_COPY(ce->parent->name);
1029 	} else {
1030 		RETURN_FALSE;
1031 	}
1032 }
1033 /* }}} */
1034 
is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass)1035 static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass) /* {{{ */
1036 {
1037 	zval *obj;
1038 	zend_string *class_name;
1039 	zend_class_entry *instance_ce;
1040 	zend_class_entry *ce;
1041 	zend_bool allow_string = only_subclass;
1042 	zend_bool retval;
1043 
1044 	ZEND_PARSE_PARAMETERS_START(2, 3)
1045 		Z_PARAM_ZVAL(obj)
1046 		Z_PARAM_STR(class_name)
1047 		Z_PARAM_OPTIONAL
1048 		Z_PARAM_BOOL(allow_string)
1049 	ZEND_PARSE_PARAMETERS_END();
1050 	/*
1051 	 * allow_string - is_a default is no, is_subclass_of is yes.
1052 	 *   if it's allowed, then the autoloader will be called if the class does not exist.
1053 	 *   default behaviour is different, as 'is_a' used to be used to test mixed return values
1054 	 *   and there is no easy way to deprecate this.
1055 	 */
1056 
1057 	if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
1058 		instance_ce = zend_lookup_class(Z_STR_P(obj));
1059 		if (!instance_ce) {
1060 			RETURN_FALSE;
1061 		}
1062 	} else if (Z_TYPE_P(obj) == IS_OBJECT) {
1063 		instance_ce = Z_OBJCE_P(obj);
1064 	} else {
1065 		RETURN_FALSE;
1066 	}
1067 
1068 	if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) {
1069 		retval = 1;
1070 	} else {
1071 		ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
1072 		if (!ce) {
1073 			retval = 0;
1074 		} else {
1075 			if (only_subclass && instance_ce == ce) {
1076 				retval = 0;
1077 			} else {
1078 				retval = instanceof_function(instance_ce, ce);
1079 			}
1080 		}
1081 	}
1082 
1083 	RETURN_BOOL(retval);
1084 }
1085 /* }}} */
1086 
1087 /* {{{ proto bool is_subclass_of(mixed object_or_string, string class_name [, bool allow_string])
1088    Returns true if the object has this class as one of its parents */
ZEND_FUNCTIONnull1089 ZEND_FUNCTION(is_subclass_of)
1090 {
1091 	is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1092 }
1093 /* }}} */
1094 
1095 /* {{{ proto bool is_a(mixed object_or_string, string class_name [, bool allow_string])
1096    Returns true if the first argument is an object and is this class or has this class as one of its parents, */
ZEND_FUNCTIONnull1097 ZEND_FUNCTION(is_a)
1098 {
1099 	is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1100 }
1101 /* }}} */
1102 
1103 /* {{{ add_class_vars */
add_class_vars(zend_class_entry *scope, zend_class_entry *ce, int statics, zval *return_value)1104 static void add_class_vars(zend_class_entry *scope, zend_class_entry *ce, int statics, zval *return_value)
1105 {
1106 	zend_property_info *prop_info;
1107 	zval *prop, prop_copy;
1108 	zend_string *key;
1109 
1110 	ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
1111 		if (((prop_info->flags & ZEND_ACC_PROTECTED) &&
1112 			 !zend_check_protected(prop_info->ce, scope)) ||
1113 			((prop_info->flags & ZEND_ACC_PRIVATE) &&
1114 			  prop_info->ce != scope)) {
1115 			continue;
1116 		}
1117 		prop = NULL;
1118 		if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
1119 			prop = &ce->default_static_members_table[prop_info->offset];
1120 			ZVAL_DEINDIRECT(prop);
1121 		} else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
1122 			prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
1123 		}
1124 		if (!prop) {
1125 			continue;
1126 		}
1127 
1128 		if (Z_ISUNDEF_P(prop)) {
1129 			/* Return uninitialized typed properties as a null value */
1130 			ZVAL_NULL(&prop_copy);
1131 		} else {
1132 			/* copy: enforce read only access */
1133 			ZVAL_COPY_OR_DUP(&prop_copy, prop);
1134 		}
1135 		prop = &prop_copy;
1136 
1137 		/* this is necessary to make it able to work with default array
1138 		 * properties, returned to user */
1139 		if (Z_OPT_TYPE_P(prop) == IS_CONSTANT_AST) {
1140 			if (UNEXPECTED(zval_update_constant_ex(prop, NULL) != SUCCESS)) {
1141 				return;
1142 			}
1143 		}
1144 
1145 		zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
1146 	} ZEND_HASH_FOREACH_END();
1147 }
1148 /* }}} */
1149 
1150 /* {{{ proto array get_class_vars(string class_name)
1151    Returns an array of default properties of the class. */
ZEND_FUNCTIONnull1152 ZEND_FUNCTION(get_class_vars)
1153 {
1154 	zend_string *class_name;
1155 	zend_class_entry *ce, *scope;
1156 
1157 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
1158 		return;
1159 	}
1160 
1161 	ce = zend_lookup_class(class_name);
1162 	if (!ce) {
1163 		RETURN_FALSE;
1164 	} else {
1165 		array_init(return_value);
1166 		if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1167 			if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
1168 				return;
1169 			}
1170 		}
1171 		scope = zend_get_executed_scope();
1172 		add_class_vars(scope, ce, 0, return_value);
1173 		add_class_vars(scope, ce, 1, return_value);
1174 	}
1175 }
1176 /* }}} */
1177 
1178 /* {{{ proto array get_object_vars(object obj)
1179    Returns an array of object properties */
ZEND_FUNCTIONnull1180 ZEND_FUNCTION(get_object_vars)
1181 {
1182 	zval *obj;
1183 	zval *value;
1184 	HashTable *properties;
1185 	zend_string *key;
1186 	zend_object *zobj;
1187 	zend_ulong num_key;
1188 
1189 	ZEND_PARSE_PARAMETERS_START(1, 1)
1190 		Z_PARAM_OBJECT(obj)
1191 	ZEND_PARSE_PARAMETERS_END();
1192 
1193 	properties = Z_OBJ_HT_P(obj)->get_properties(obj);
1194 	if (properties == NULL) {
1195 		RETURN_FALSE;
1196 	}
1197 
1198 	zobj = Z_OBJ_P(obj);
1199 
1200 	if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
1201 		/* fast copy */
1202 		if (EXPECTED(zobj->handlers == &std_object_handlers)) {
1203 			RETURN_ARR(zend_proptable_to_symtable(properties, 0));
1204 		}
1205 		RETURN_ARR(zend_proptable_to_symtable(properties, 1));
1206 	} else {
1207 		array_init_size(return_value, zend_hash_num_elements(properties));
1208 
1209 		ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, value) {
1210 			zend_bool is_dynamic = 1;
1211 			if (Z_TYPE_P(value) == IS_INDIRECT) {
1212 				value = Z_INDIRECT_P(value);
1213 				if (UNEXPECTED(Z_ISUNDEF_P(value))) {
1214 					continue;
1215 				}
1216 
1217 				is_dynamic = 0;
1218 			}
1219 
1220 			if (key && zend_check_property_access(zobj, key, is_dynamic) == FAILURE) {
1221 				continue;
1222 			}
1223 
1224 			if (Z_ISREF_P(value) && Z_REFCOUNT_P(value) == 1) {
1225 				value = Z_REFVAL_P(value);
1226 			}
1227 			Z_TRY_ADDREF_P(value);
1228 
1229 			if (UNEXPECTED(!key)) {
1230 				/* This case is only possible due to loopholes, e.g. ArrayObject */
1231 				zend_hash_index_add(Z_ARRVAL_P(return_value), num_key, value);
1232 			} else if (!is_dynamic && ZSTR_VAL(key)[0] == 0) {
1233 				const char *prop_name, *class_name;
1234 				size_t prop_len;
1235 				zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
1236 				/* We assume here that a mangled property name is never
1237 				 * numeric. This is probably a safe assumption, but
1238 				 * theoretically someone might write an extension with
1239 				 * private, numeric properties. Well, too bad.
1240 				 */
1241 				zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
1242 			} else {
1243 				zend_symtable_add_new(Z_ARRVAL_P(return_value), key, value);
1244 			}
1245 		} ZEND_HASH_FOREACH_END();
1246 	}
1247 }
1248 /* }}} */
1249 
1250 /* {{{ proto array get_mangled_object_vars(object obj)
1251    Returns an array of mangled object properties. Does not respect property visibility. */
ZEND_FUNCTIONnull1252 ZEND_FUNCTION(get_mangled_object_vars)
1253 {
1254 	zval *obj;
1255 	HashTable *properties;
1256 
1257 	ZEND_PARSE_PARAMETERS_START(1, 1)
1258 		Z_PARAM_OBJECT(obj)
1259 	ZEND_PARSE_PARAMETERS_END();
1260 
1261 	properties = Z_OBJ_HT_P(obj)->get_properties(obj);
1262 	if (!properties) {
1263 		ZVAL_EMPTY_ARRAY(return_value);
1264 		return;
1265 	}
1266 
1267 	properties = zend_proptable_to_symtable(properties,
1268 		(Z_OBJCE_P(obj)->default_properties_count ||
1269 		 Z_OBJ_P(obj)->handlers != &std_object_handlers ||
1270 		 GC_IS_RECURSIVE(properties)));
1271 	RETURN_ARR(properties);
1272 }
1273 /* }}} */
1274 
same_name(zend_string *key, zend_string *name)1275 static int same_name(zend_string *key, zend_string *name) /* {{{ */
1276 {
1277 	zend_string *lcname;
1278 	int ret;
1279 
1280 	if (key == name) {
1281 		return 1;
1282 	}
1283 	if (ZSTR_LEN(key) != ZSTR_LEN(name)) {
1284 		return 0;
1285 	}
1286 	lcname = zend_string_tolower(name);
1287 	ret = memcmp(ZSTR_VAL(lcname), ZSTR_VAL(key), ZSTR_LEN(key)) == 0;
1288 	zend_string_release_ex(lcname, 0);
1289 	return ret;
1290 }
1291 /* }}} */
1292 
1293 /* {{{ proto array get_class_methods(mixed class)
1294    Returns an array of method names for class or class instance. */
ZEND_FUNCTIONnull1295 ZEND_FUNCTION(get_class_methods)
1296 {
1297 	zval *klass;
1298 	zval method_name;
1299 	zend_class_entry *ce = NULL;
1300 	zend_class_entry *scope;
1301 	zend_function *mptr;
1302 	zend_string *key;
1303 
1304 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &klass) == FAILURE) {
1305 		return;
1306 	}
1307 
1308 	if (Z_TYPE_P(klass) == IS_OBJECT) {
1309 		ce = Z_OBJCE_P(klass);
1310 	} else if (Z_TYPE_P(klass) == IS_STRING) {
1311 		ce = zend_lookup_class(Z_STR_P(klass));
1312 	}
1313 
1314 	if (!ce) {
1315 		RETURN_NULL();
1316 	}
1317 
1318 	array_init(return_value);
1319 	scope = zend_get_executed_scope();
1320 
1321 	ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, key, mptr) {
1322 
1323 		if ((mptr->common.fn_flags & ZEND_ACC_PUBLIC)
1324 		 || (scope &&
1325 			 (((mptr->common.fn_flags & ZEND_ACC_PROTECTED) &&
1326 			   zend_check_protected(mptr->common.scope, scope))
1327 		   || ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) &&
1328 			   scope == mptr->common.scope)))
1329 		) {
1330 			if (mptr->type == ZEND_USER_FUNCTION &&
1331 				(!mptr->op_array.refcount || *mptr->op_array.refcount > 1) &&
1332 				 key && !same_name(key, mptr->common.function_name)) {
1333 				ZVAL_STR_COPY(&method_name, zend_find_alias_name(mptr->common.scope, key));
1334 				zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1335 			} else {
1336 				ZVAL_STR_COPY(&method_name, mptr->common.function_name);
1337 				zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1338 			}
1339 		}
1340 	} ZEND_HASH_FOREACH_END();
1341 }
1342 /* }}} */
1343 
1344 /* {{{ proto bool method_exists(object object, string method)
1345    Checks if the class method exists */
ZEND_FUNCTIONnull1346 ZEND_FUNCTION(method_exists)
1347 {
1348 	zval *klass;
1349 	zend_string *method_name;
1350 	zend_string *lcname;
1351 	zend_class_entry *ce;
1352 	zend_function *func;
1353 
1354 	ZEND_PARSE_PARAMETERS_START(2, 2)
1355 		Z_PARAM_ZVAL(klass)
1356 		Z_PARAM_STR(method_name)
1357 	ZEND_PARSE_PARAMETERS_END();
1358 
1359 	if (Z_TYPE_P(klass) == IS_OBJECT) {
1360 		ce = Z_OBJCE_P(klass);
1361 	} else if (Z_TYPE_P(klass) == IS_STRING) {
1362 		if ((ce = zend_lookup_class(Z_STR_P(klass))) == NULL) {
1363 			RETURN_FALSE;
1364 		}
1365 	} else {
1366 		RETURN_FALSE;
1367 	}
1368 
1369 	lcname = zend_string_tolower(method_name);
1370 	func = zend_hash_find_ptr(&ce->function_table, lcname);
1371 	zend_string_release_ex(lcname, 0);
1372 
1373 	if (func) {
1374 		/* Exclude shadow properties when checking a method on a specific class. Include
1375 		 * them when checking an object, as method_exists() generally ignores visibility.
1376 		 * TODO: Should we use EG(scope) for the object case instead? */
1377 		RETURN_BOOL(Z_TYPE_P(klass) == IS_OBJECT
1378 			|| !(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
1379 	}
1380 
1381 	if (Z_TYPE_P(klass) == IS_OBJECT) {
1382 		zend_object *obj = Z_OBJ_P(klass);
1383 		func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
1384 		if (func != NULL) {
1385 			if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
1386 				/* Returns true to the fake Closure's __invoke */
1387 				RETVAL_BOOL(func->common.scope == zend_ce_closure
1388 					&& zend_string_equals_literal(method_name, ZEND_INVOKE_FUNC_NAME));
1389 
1390 				zend_string_release_ex(func->common.function_name, 0);
1391 				zend_free_trampoline(func);
1392 				return;
1393 			}
1394 			RETURN_TRUE;
1395 		}
1396 	}
1397 	RETURN_FALSE;
1398 }
1399 /* }}} */
1400 
1401 /* {{{ proto bool property_exists(mixed object_or_class, string property_name)
1402    Checks if the object or class has a property */
ZEND_FUNCTIONnull1403 ZEND_FUNCTION(property_exists)
1404 {
1405 	zval *object;
1406 	zend_string *property;
1407 	zend_class_entry *ce;
1408 	zend_property_info *property_info;
1409 	zval property_z;
1410 
1411 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
1412 		return;
1413 	}
1414 
1415 	if (property == NULL) {
1416 		RETURN_FALSE;
1417 	}
1418 
1419 	if (Z_TYPE_P(object) == IS_STRING) {
1420 		ce = zend_lookup_class(Z_STR_P(object));
1421 		if (!ce) {
1422 			RETURN_FALSE;
1423 		}
1424 	} else if (Z_TYPE_P(object) == IS_OBJECT) {
1425 		ce = Z_OBJCE_P(object);
1426 	} else {
1427 		zend_error(E_WARNING, "First parameter must either be an object or the name of an existing class");
1428 		RETURN_NULL();
1429 	}
1430 
1431 	property_info = zend_hash_find_ptr(&ce->properties_info, property);
1432 	if (property_info != NULL
1433 	 && (!(property_info->flags & ZEND_ACC_PRIVATE)
1434 	  || property_info->ce == ce)) {
1435 		RETURN_TRUE;
1436 	}
1437 
1438 	ZVAL_STR(&property_z, property);
1439 
1440 	if (Z_TYPE_P(object) ==  IS_OBJECT &&
1441 		Z_OBJ_HANDLER_P(object, has_property)(object, &property_z, 2, NULL)) {
1442 		RETURN_TRUE;
1443 	}
1444 	RETURN_FALSE;
1445 }
1446 /* }}} */
1447 
class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags)1448 static inline void class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
1449 {
1450 	zend_string *name;
1451 	zend_string *lcname;
1452 	zend_class_entry *ce;
1453 	zend_bool autoload = 1;
1454 
1455 	ZEND_PARSE_PARAMETERS_START(1, 2)
1456 		Z_PARAM_STR(name)
1457 		Z_PARAM_OPTIONAL
1458 		Z_PARAM_BOOL(autoload)
1459 	ZEND_PARSE_PARAMETERS_END();
1460 
1461 	if (!autoload) {
1462 		if (ZSTR_VAL(name)[0] == '\\') {
1463 			/* Ignore leading "\" */
1464 			lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
1465 			zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1466 		} else {
1467 			lcname = zend_string_tolower(name);
1468 		}
1469 
1470 		ce = zend_hash_find_ptr(EG(class_table), lcname);
1471 		zend_string_release_ex(lcname, 0);
1472 	} else {
1473 		ce = zend_lookup_class(name);
1474 	}
1475 
1476  	if (ce) {
1477 		RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
1478 	} else {
1479 		RETURN_FALSE;
1480 	}
1481 }
1482 /* {{{ */
1483 
1484 /* {{{ proto bool class_exists(string classname [, bool autoload])
1485    Checks if the class exists */
ZEND_FUNCTIONnull1486 ZEND_FUNCTION(class_exists)
1487 {
1488 	class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1489 }
1490 /* }}} */
1491 
1492 /* {{{ proto bool interface_exists(string classname [, bool autoload])
1493    Checks if the class exists */
ZEND_FUNCTIONnull1494 ZEND_FUNCTION(interface_exists)
1495 {
1496 	class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED|ZEND_ACC_INTERFACE, 0);
1497 }
1498 /* }}} */
1499 
1500 /* {{{ proto bool trait_exists(string traitname [, bool autoload])
1501  Checks if the trait exists */
ZEND_FUNCTIONnull1502 ZEND_FUNCTION(trait_exists)
1503 {
1504 	class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
1505 }
1506 /* }}} */
1507 
1508 /* {{{ proto bool function_exists(string function_name)
1509    Checks if the function exists */
ZEND_FUNCTIONnull1510 ZEND_FUNCTION(function_exists)
1511 {
1512 	zend_string *name;
1513 	zend_function *func;
1514 	zend_string *lcname;
1515 
1516 	ZEND_PARSE_PARAMETERS_START(1, 1)
1517 		Z_PARAM_STR(name)
1518 	ZEND_PARSE_PARAMETERS_END();
1519 
1520 	if (ZSTR_VAL(name)[0] == '\\') {
1521 		/* Ignore leading "\" */
1522 		lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
1523 		zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1524 	} else {
1525 		lcname = zend_string_tolower(name);
1526 	}
1527 
1528 	func = zend_hash_find_ptr(EG(function_table), lcname);
1529 	zend_string_release_ex(lcname, 0);
1530 
1531 	/*
1532 	 * A bit of a hack, but not a bad one: we see if the handler of the function
1533 	 * is actually one that displays "function is disabled" message.
1534 	 */
1535 	RETURN_BOOL(func && (func->type != ZEND_INTERNAL_FUNCTION ||
1536 		func->internal_function.handler != zif_display_disabled_function));
1537 }
1538 /* }}} */
1539 
1540 /* {{{ proto bool class_alias(string user_class_name , string alias_name [, bool autoload])
1541    Creates an alias for user defined class */
ZEND_FUNCTIONnull1542 ZEND_FUNCTION(class_alias)
1543 {
1544 	zend_string *class_name;
1545 	char *alias_name;
1546 	zend_class_entry *ce;
1547 	size_t alias_name_len;
1548 	zend_bool autoload = 1;
1549 
1550 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ss|b", &class_name, &alias_name, &alias_name_len, &autoload) == FAILURE) {
1551 		return;
1552 	}
1553 
1554 	ce = zend_lookup_class_ex(class_name, NULL, !autoload ? ZEND_FETCH_CLASS_NO_AUTOLOAD : 0);
1555 
1556 	if (ce) {
1557 		if (ce->type == ZEND_USER_CLASS) {
1558 			if (zend_register_class_alias_ex(alias_name, alias_name_len, ce, 0) == SUCCESS) {
1559 				RETURN_TRUE;
1560 			} else {
1561 				zend_error(E_WARNING, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), alias_name);
1562 				RETURN_FALSE;
1563 			}
1564 		} else {
1565 			zend_error(E_WARNING, "First argument of class_alias() must be a name of user defined class");
1566 			RETURN_FALSE;
1567 		}
1568 	} else {
1569 		zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
1570 		RETURN_FALSE;
1571 	}
1572 }
1573 /* }}} */
1574 
1575 /* {{{ proto array get_included_files(void)
1576    Returns an array with the file names that were include_once()'d */
ZEND_FUNCTIONnull1577 ZEND_FUNCTION(get_included_files)
1578 {
1579 	zend_string *entry;
1580 
1581 	if (zend_parse_parameters_none() == FAILURE) {
1582 		return;
1583 	}
1584 
1585 	array_init(return_value);
1586 	ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), entry) {
1587 		if (entry) {
1588 			add_next_index_str(return_value, zend_string_copy(entry));
1589 		}
1590 	} ZEND_HASH_FOREACH_END();
1591 }
1592 /* }}} */
1593 
1594 /* {{{ proto void trigger_error(string message [, int error_type])
1595    Generates a user-level error/warning/notice message */
ZEND_FUNCTIONnull1596 ZEND_FUNCTION(trigger_error)
1597 {
1598 	zend_long error_type = E_USER_NOTICE;
1599 	char *message;
1600 	size_t message_len;
1601 
1602 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
1603 		return;
1604 	}
1605 
1606 	switch (error_type) {
1607 		case E_USER_ERROR:
1608 		case E_USER_WARNING:
1609 		case E_USER_NOTICE:
1610 		case E_USER_DEPRECATED:
1611 			break;
1612 		default:
1613 			zend_error(E_WARNING, "Invalid error type specified");
1614 			RETURN_FALSE;
1615 			break;
1616 	}
1617 
1618 	zend_error((int)error_type, "%s", message);
1619 	RETURN_TRUE;
1620 }
1621 /* }}} */
1622 
1623 /* {{{ proto string set_error_handler(callable error_handler [, int error_types])
1624    Sets a user-defined error handler function.  Returns the previously defined error handler, or false on error */
ZEND_FUNCTIONnull1625 ZEND_FUNCTION(set_error_handler)
1626 {
1627 	zval *error_handler;
1628 	zend_long error_type = E_ALL;
1629 
1630 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &error_handler, &error_type) == FAILURE) {
1631 		return;
1632 	}
1633 
1634 	if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
1635 		if (!zend_is_callable(error_handler, 0, NULL)) {
1636 			zend_string *error_handler_name = zend_get_callable_name(error_handler);
1637 			zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1638 					   get_active_function_name(), error_handler_name?ZSTR_VAL(error_handler_name):"unknown");
1639 			zend_string_release_ex(error_handler_name, 0);
1640 			return;
1641 		}
1642 	}
1643 
1644 	if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1645 		ZVAL_COPY(return_value, &EG(user_error_handler));
1646 	}
1647 
1648 	zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
1649 	zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
1650 
1651 	if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
1652 		ZVAL_UNDEF(&EG(user_error_handler));
1653 		return;
1654 	}
1655 
1656 	ZVAL_COPY(&EG(user_error_handler), error_handler);
1657 	EG(user_error_handler_error_reporting) = (int)error_type;
1658 }
1659 /* }}} */
1660 
1661 /* {{{ proto void restore_error_handler(void)
1662    Restores the previously defined error handler function */
ZEND_FUNCTIONnull1663 ZEND_FUNCTION(restore_error_handler)
1664 {
1665 	if (zend_parse_parameters_none() == FAILURE) {
1666 		return;
1667 	}
1668 
1669 	if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1670 		zval zeh;
1671 
1672 		ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
1673 		ZVAL_UNDEF(&EG(user_error_handler));
1674 		zval_ptr_dtor(&zeh);
1675 	}
1676 
1677 	if (zend_stack_is_empty(&EG(user_error_handlers))) {
1678 		ZVAL_UNDEF(&EG(user_error_handler));
1679 	} else {
1680 		zval *tmp;
1681 		EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
1682 		zend_stack_del_top(&EG(user_error_handlers_error_reporting));
1683 		tmp = zend_stack_top(&EG(user_error_handlers));
1684 		ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
1685 		zend_stack_del_top(&EG(user_error_handlers));
1686 	}
1687 	RETURN_TRUE;
1688 }
1689 /* }}} */
1690 
1691 /* {{{ proto mixed set_exception_handler(callable exception_handler)
1692    Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
ZEND_FUNCTIONnull1693 ZEND_FUNCTION(set_exception_handler)
1694 {
1695 	zval *exception_handler;
1696 
1697 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception_handler) == FAILURE) {
1698 		return;
1699 	}
1700 
1701 	if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
1702 		if (!zend_is_callable(exception_handler, 0, NULL)) {
1703 		zend_string *exception_handler_name = zend_get_callable_name(exception_handler);
1704 			zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1705 					   get_active_function_name(), exception_handler_name?ZSTR_VAL(exception_handler_name):"unknown");
1706 			zend_string_release_ex(exception_handler_name, 0);
1707 			return;
1708 		}
1709 	}
1710 
1711 	if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1712 		ZVAL_COPY(return_value, &EG(user_exception_handler));
1713 	}
1714 
1715 	zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
1716 
1717 	if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
1718 		ZVAL_UNDEF(&EG(user_exception_handler));
1719 		return;
1720 	}
1721 
1722 	ZVAL_COPY(&EG(user_exception_handler), exception_handler);
1723 }
1724 /* }}} */
1725 
1726 /* {{{ proto void restore_exception_handler(void)
1727    Restores the previously defined exception handler function */
ZEND_FUNCTIONnull1728 ZEND_FUNCTION(restore_exception_handler)
1729 {
1730 	if (zend_parse_parameters_none() == FAILURE) {
1731 		return;
1732 	}
1733 
1734 	if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1735 		zval_ptr_dtor(&EG(user_exception_handler));
1736 	}
1737 	if (zend_stack_is_empty(&EG(user_exception_handlers))) {
1738 		ZVAL_UNDEF(&EG(user_exception_handler));
1739 	} else {
1740 		zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1741 		ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1742 		zend_stack_del_top(&EG(user_exception_handlers));
1743 	}
1744 	RETURN_TRUE;
1745 }
1746 /* }}} */
1747 
copy_class_or_interface_name(zval *array, zend_string *key, zend_class_entry *ce)1748 static void copy_class_or_interface_name(zval *array, zend_string *key, zend_class_entry *ce) /* {{{ */
1749 {
1750 	if ((ce->refcount == 1 && !(ce->ce_flags & ZEND_ACC_IMMUTABLE)) ||
1751 		same_name(key, ce->name)) {
1752 		key = ce->name;
1753 	}
1754 	add_next_index_str(array, zend_string_copy(key));
1755 }
1756 /* }}} */
1757 
get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags)1758 static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
1759 {
1760 	zend_string *key;
1761 	zend_class_entry *ce;
1762 
1763 	if (zend_parse_parameters_none() == FAILURE) {
1764 		return;
1765 	}
1766 
1767 	array_init(return_value);
1768 	ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
1769 		if (key
1770 		 && ZSTR_VAL(key)[0] != 0
1771 		 && (ce->ce_flags & flags)
1772 		 && !(ce->ce_flags & skip_flags)) {
1773 			copy_class_or_interface_name(return_value, key, ce);
1774 		}
1775 	} ZEND_HASH_FOREACH_END();
1776 }
1777 /* {{{ */
1778 
1779 /* {{{ proto array get_declared_traits()
1780    Returns an array of all declared traits. */
ZEND_FUNCTIONnull1781 ZEND_FUNCTION(get_declared_traits)
1782 {
1783 	get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
1784 }
1785 /* }}} */
1786 
1787 /* {{{ proto array get_declared_classes()
1788    Returns an array of all declared classes. */
ZEND_FUNCTIONnull1789 ZEND_FUNCTION(get_declared_classes)
1790 {
1791 	get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1792 }
1793 /* }}} */
1794 
1795 /* {{{ proto array get_declared_interfaces()
1796    Returns an array of all declared interfaces. */
ZEND_FUNCTIONnull1797 ZEND_FUNCTION(get_declared_interfaces)
1798 {
1799 	get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE, 0);
1800 }
1801 /* }}} */
1802 
1803 /* {{{ proto array get_defined_functions(void)
1804    Returns an array of all defined functions */
ZEND_FUNCTIONnull1805 ZEND_FUNCTION(get_defined_functions)
1806 {
1807 	zval internal, user;
1808 	zend_string *key;
1809 	zend_function *func;
1810 	zend_bool exclude_disabled = 0;
1811 
1812 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
1813 		return;
1814 	}
1815 
1816 	array_init(&internal);
1817 	array_init(&user);
1818 	array_init(return_value);
1819 
1820 	ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
1821 		if (key && ZSTR_VAL(key)[0] != 0) {
1822 			if (func->type == ZEND_INTERNAL_FUNCTION
1823 				&& (!exclude_disabled || func->internal_function.handler != ZEND_FN(display_disabled_function))) {
1824 				add_next_index_str(&internal, zend_string_copy(key));
1825 			} else if (func->type == ZEND_USER_FUNCTION) {
1826 				add_next_index_str(&user, zend_string_copy(key));
1827 			}
1828 		}
1829 	} ZEND_HASH_FOREACH_END();
1830 
1831 	zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
1832 	zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
1833 }
1834 /* }}} */
1835 
1836 /* {{{ proto array get_defined_vars(void)
1837    Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
ZEND_FUNCTIONnull1838 ZEND_FUNCTION(get_defined_vars)
1839 {
1840 	zend_array *symbol_table;
1841 	if (zend_forbid_dynamic_call("get_defined_vars()") == FAILURE) {
1842 		return;
1843 	}
1844 
1845 	symbol_table = zend_rebuild_symbol_table();
1846 	if (UNEXPECTED(symbol_table == NULL)) {
1847 		return;
1848 	}
1849 
1850 	RETURN_ARR(zend_array_dup(symbol_table));
1851 }
1852 /* }}} */
1853 
1854 #define LAMBDA_TEMP_FUNCNAME	"__lambda_func"
1855 /* {{{ proto string create_function(string args, string code)
1856    Creates an anonymous function, and returns its name (funny, eh?) */
ZEND_FUNCTIONnull1857 ZEND_FUNCTION(create_function)
1858 {
1859     zend_string *function_name;
1860 	char *eval_code, *function_args, *function_code;
1861 	size_t eval_code_length, function_args_len, function_code_len;
1862 	int retval;
1863 	char *eval_name;
1864 
1865 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &function_args, &function_args_len, &function_code, &function_code_len) == FAILURE) {
1866 		return;
1867 	}
1868 
1869 	eval_code = (char *) emalloc(sizeof("function " LAMBDA_TEMP_FUNCNAME)
1870 			+function_args_len
1871 			+2	/* for the args parentheses */
1872 			+2	/* for the curly braces */
1873 			+function_code_len);
1874 
1875 	eval_code_length = sizeof("function " LAMBDA_TEMP_FUNCNAME "(") - 1;
1876 	memcpy(eval_code, "function " LAMBDA_TEMP_FUNCNAME "(", eval_code_length);
1877 
1878 	memcpy(eval_code + eval_code_length, function_args, function_args_len);
1879 	eval_code_length += function_args_len;
1880 
1881 	eval_code[eval_code_length++] = ')';
1882 	eval_code[eval_code_length++] = '{';
1883 
1884 	memcpy(eval_code + eval_code_length, function_code, function_code_len);
1885 	eval_code_length += function_code_len;
1886 
1887 	eval_code[eval_code_length++] = '}';
1888 	eval_code[eval_code_length] = '\0';
1889 
1890 	eval_name = zend_make_compiled_string_description("runtime-created function");
1891 	retval = zend_eval_stringl(eval_code, eval_code_length, NULL, eval_name);
1892 	efree(eval_code);
1893 	efree(eval_name);
1894 
1895 	if (retval==SUCCESS) {
1896 		zend_op_array *func;
1897 		HashTable *static_variables;
1898 
1899 		func = zend_hash_str_find_ptr(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1900 		if (!func) {
1901 			zend_error_noreturn(E_CORE_ERROR, "Unexpected inconsistency in create_function()");
1902 			RETURN_FALSE;
1903 		}
1904 		if (func->refcount) {
1905 			(*func->refcount)++;
1906 		}
1907 		static_variables = func->static_variables;
1908 		func->static_variables = NULL;
1909 		zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1910 		func->static_variables = static_variables;
1911 
1912 		function_name = zend_string_alloc(sizeof("0lambda_")+MAX_LENGTH_OF_LONG, 0);
1913 		ZSTR_VAL(function_name)[0] = '\0';
1914 
1915 		do {
1916 			ZSTR_LEN(function_name) = snprintf(ZSTR_VAL(function_name) + 1, sizeof("lambda_")+MAX_LENGTH_OF_LONG, "lambda_%d", ++EG(lambda_count)) + 1;
1917 		} while (zend_hash_add_ptr(EG(function_table), function_name, func) == NULL);
1918 		RETURN_NEW_STR(function_name);
1919 	} else {
1920 		zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1921 		RETURN_FALSE;
1922 	}
1923 }
1924 /* }}} */
1925 
1926 #if ZEND_DEBUG && defined(ZTS)
ZEND_FUNCTIONnull1927 ZEND_FUNCTION(zend_thread_id)
1928 {
1929 	RETURN_LONG((zend_long)tsrm_thread_id());
1930 }
1931 #endif
1932 
1933 /* {{{ proto string get_resource_type(resource res)
1934    Get the resource type name for a given resource */
ZEND_FUNCTIONnull1935 ZEND_FUNCTION(get_resource_type)
1936 {
1937 	const char *resource_type;
1938 	zval *z_resource_type;
1939 
1940 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_resource_type) == FAILURE) {
1941 		return;
1942 	}
1943 
1944 	resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type));
1945 	if (resource_type) {
1946 		RETURN_STRING(resource_type);
1947 	} else {
1948 		RETURN_STRING("Unknown");
1949 	}
1950 }
1951 /* }}} */
1952 
1953 /* {{{ proto array get_resources([string resouce_type])
1954    Get an array with all active resources */
ZEND_FUNCTIONnull1955 ZEND_FUNCTION(get_resources)
1956 {
1957 	zend_string *type = NULL;
1958 	zend_string *key;
1959 	zend_ulong index;
1960 	zval *val;
1961 
1962 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &type) == FAILURE) {
1963 		return;
1964 	}
1965 
1966 	if (!type) {
1967 		array_init(return_value);
1968 		ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1969 			if (!key) {
1970 				Z_ADDREF_P(val);
1971 				zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1972 			}
1973 		} ZEND_HASH_FOREACH_END();
1974 	} else if (zend_string_equals_literal(type, "Unknown")) {
1975 		array_init(return_value);
1976 		ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1977 			if (!key && Z_RES_TYPE_P(val) <= 0) {
1978 				Z_ADDREF_P(val);
1979 				zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1980 			}
1981 		} ZEND_HASH_FOREACH_END();
1982 	} else {
1983 		int id = zend_fetch_list_dtor_id(ZSTR_VAL(type));
1984 
1985 		if (id <= 0) {
1986 			zend_error(E_WARNING, "get_resources():  Unknown resource type '%s'", ZSTR_VAL(type));
1987 			RETURN_FALSE;
1988 		}
1989 
1990 		array_init(return_value);
1991 		ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1992 			if (!key && Z_RES_TYPE_P(val) == id) {
1993 				Z_ADDREF_P(val);
1994 				zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1995 			}
1996 		} ZEND_HASH_FOREACH_END();
1997 	}
1998 }
1999 /* }}} */
2000 
add_zendext_info(zend_extension *ext, void *arg)2001 static int add_zendext_info(zend_extension *ext, void *arg) /* {{{ */
2002 {
2003 	zval *name_array = (zval *)arg;
2004 	add_next_index_string(name_array, ext->name);
2005 	return 0;
2006 }
2007 /* }}} */
2008 
2009 /* {{{ proto array get_loaded_extensions([bool zend_extensions])
2010    Return an array containing names of loaded extensions */
ZEND_FUNCTIONnull2011 ZEND_FUNCTION(get_loaded_extensions)
2012 {
2013 	zend_bool zendext = 0;
2014 
2015 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &zendext) == FAILURE) {
2016 		return;
2017 	}
2018 
2019 	array_init(return_value);
2020 
2021 	if (zendext) {
2022 		zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t)add_zendext_info, return_value);
2023 	} else {
2024 		zend_module_entry *module;
2025 
2026 		ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2027 			add_next_index_string(return_value, module->name);
2028 		} ZEND_HASH_FOREACH_END();
2029 	}
2030 }
2031 /* }}} */
2032 
2033 /* {{{ proto array get_defined_constants([bool categorize])
2034    Return an array containing the names and values of all defined constants */
ZEND_FUNCTIONnull2035 ZEND_FUNCTION(get_defined_constants)
2036 {
2037 	zend_bool categorize = 0;
2038 
2039 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &categorize) == FAILURE) {
2040 		return;
2041 	}
2042 
2043 	array_init(return_value);
2044 
2045 	if (categorize) {
2046 		zend_constant *val;
2047 		int module_number;
2048 		zval *modules, const_val;
2049 		char **module_names;
2050 		zend_module_entry *module;
2051 		int i = 1;
2052 
2053 		modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
2054 		module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
2055 
2056 		module_names[0] = "internal";
2057 		ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2058 			module_names[module->module_number] = (char *)module->name;
2059 			i++;
2060 		} ZEND_HASH_FOREACH_END();
2061 		module_names[i] = "user";
2062 
2063 		ZEND_HASH_FOREACH_PTR(EG(zend_constants), val) {
2064 			if (!val->name) {
2065 				/* skip special constants */
2066 				continue;
2067 			}
2068 
2069 			if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
2070 				module_number = i;
2071 			} else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
2072 				/* should not happen */
2073 				continue;
2074 			} else {
2075 				module_number = ZEND_CONSTANT_MODULE_NUMBER(val);
2076 			}
2077 
2078 			if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
2079 				array_init(&modules[module_number]);
2080 				add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
2081 			}
2082 
2083 			ZVAL_COPY_OR_DUP(&const_val, &val->value);
2084 			zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
2085 		} ZEND_HASH_FOREACH_END();
2086 
2087 		efree(module_names);
2088 		efree(modules);
2089 	} else {
2090 		zend_constant *constant;
2091 		zval const_val;
2092 
2093 		ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
2094 			if (!constant->name) {
2095 				/* skip special constants */
2096 				continue;
2097 			}
2098 			ZVAL_COPY_OR_DUP(&const_val, &constant->value);
2099 			zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
2100 		} ZEND_HASH_FOREACH_END();
2101 	}
2102 }
2103 /* }}} */
2104 
debug_backtrace_get_args(zend_execute_data *call, zval *arg_array)2105 static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /* {{{ */
2106 {
2107 	uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
2108 
2109 	if (num_args) {
2110 		uint32_t i = 0;
2111 		zval *p = ZEND_CALL_ARG(call, 1);
2112 
2113 		array_init_size(arg_array, num_args);
2114 		zend_hash_real_init_packed(Z_ARRVAL_P(arg_array));
2115 		ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(arg_array)) {
2116 			if (call->func->type == ZEND_USER_FUNCTION) {
2117 				uint32_t first_extra_arg = MIN(num_args, call->func->op_array.num_args);
2118 
2119 				if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
2120 					/* In case of attached symbol_table, values on stack may be invalid
2121 					 * and we have to access them through symbol_table
2122 					 * See: https://bugs.php.net/bug.php?id=73156
2123 					 */
2124 					zend_string *arg_name;
2125 					zval *arg;
2126 
2127 					while (i < first_extra_arg) {
2128 						arg_name = call->func->op_array.vars[i];
2129 						arg = zend_hash_find_ex_ind(call->symbol_table, arg_name, 1);
2130 						if (arg) {
2131 							if (Z_OPT_REFCOUNTED_P(arg)) {
2132 								Z_ADDREF_P(arg);
2133 							}
2134 							ZEND_HASH_FILL_SET(arg);
2135 						} else {
2136 							ZEND_HASH_FILL_SET_NULL();
2137 						}
2138 						ZEND_HASH_FILL_NEXT();
2139 						i++;
2140 					}
2141 				} else {
2142 					while (i < first_extra_arg) {
2143 						if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
2144 							if (Z_OPT_REFCOUNTED_P(p)) {
2145 								Z_ADDREF_P(p);
2146 							}
2147 							ZEND_HASH_FILL_SET(p);
2148 						} else {
2149 							ZEND_HASH_FILL_SET_NULL();
2150 						}
2151 						ZEND_HASH_FILL_NEXT();
2152 						p++;
2153 						i++;
2154 					}
2155 				}
2156 				p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
2157 			}
2158 
2159 			while (i < num_args) {
2160 				if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
2161 					if (Z_OPT_REFCOUNTED_P(p)) {
2162 						Z_ADDREF_P(p);
2163 					}
2164 					ZEND_HASH_FILL_SET(p);
2165 				} else {
2166 					ZEND_HASH_FILL_SET_NULL();
2167 				}
2168 				ZEND_HASH_FILL_NEXT();
2169 				p++;
2170 				i++;
2171 			}
2172 		} ZEND_HASH_FILL_END();
2173 		Z_ARRVAL_P(arg_array)->nNumOfElements = num_args;
2174 	} else {
2175 		ZVAL_EMPTY_ARRAY(arg_array);
2176 	}
2177 }
2178 /* }}} */
2179 
debug_print_backtrace_args(zval *arg_array)2180 void debug_print_backtrace_args(zval *arg_array) /* {{{ */
2181 {
2182 	zval *tmp;
2183 	int i = 0;
2184 
2185 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg_array), tmp) {
2186 		if (i++) {
2187 			ZEND_PUTS(", ");
2188 		}
2189 		zend_print_flat_zval_r(tmp);
2190 	} ZEND_HASH_FOREACH_END();
2191 }
2192 /* }}} */
2193 
skip_internal_handler(zend_execute_data *skip)2194 static inline zend_bool skip_internal_handler(zend_execute_data *skip) /* {{{ */
2195 {
2196 	return !(skip->func && ZEND_USER_CODE(skip->func->common.type))
2197 			&& skip->prev_execute_data
2198 			&& skip->prev_execute_data->func
2199 			&& ZEND_USER_CODE(skip->prev_execute_data->func->common.type)
2200 			&& skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL
2201 			&& skip->prev_execute_data->opline->opcode != ZEND_DO_ICALL
2202 			&& skip->prev_execute_data->opline->opcode != ZEND_DO_UCALL
2203 			&& skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL_BY_NAME
2204 			&& skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL;
2205 }
2206 /* {{{ */
2207 
2208 /* {{{ proto void debug_print_backtrace([int options[, int limit]]) */
ZEND_FUNCTIONnull2209 ZEND_FUNCTION(debug_print_backtrace)
2210 {
2211 	zend_execute_data *call, *ptr, *skip;
2212 	zend_object *object;
2213 	int lineno, frameno = 0;
2214 	zend_function *func;
2215 	const char *function_name;
2216 	const char *filename;
2217 	zend_string *class_name = NULL;
2218 	char *call_type;
2219 	const char *include_filename = NULL;
2220 	zval arg_array;
2221 	int indent = 0;
2222 	zend_long options = 0;
2223 	zend_long limit = 0;
2224 
2225 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
2226 		return;
2227 	}
2228 
2229 	ZVAL_UNDEF(&arg_array);
2230 	ptr = EX(prev_execute_data);
2231 
2232 	/* skip debug_backtrace() */
2233 	call = ptr;
2234 	ptr = ptr->prev_execute_data;
2235 
2236 	while (ptr && (limit == 0 || frameno < limit)) {
2237 		frameno++;
2238 		class_name = NULL;
2239 		call_type = NULL;
2240 		ZVAL_UNDEF(&arg_array);
2241 
2242 		ptr = zend_generator_check_placeholder_frame(ptr);
2243 
2244 		skip = ptr;
2245 		/* skip internal handler */
2246 		if (skip_internal_handler(skip)) {
2247 			skip = skip->prev_execute_data;
2248 		}
2249 
2250 		if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2251 			filename = ZSTR_VAL(skip->func->op_array.filename);
2252 			if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2253 				if (EG(opline_before_exception)) {
2254 					lineno = EG(opline_before_exception)->lineno;
2255 				} else {
2256 					lineno = skip->func->op_array.line_end;
2257 				}
2258 			} else {
2259 				lineno = skip->opline->lineno;
2260 			}
2261 		} else {
2262 			filename = NULL;
2263 			lineno = 0;
2264 		}
2265 
2266 		/* $this may be passed into regular internal functions */
2267 		object = (Z_TYPE(call->This) == IS_OBJECT) ? Z_OBJ(call->This) : NULL;
2268 
2269 		if (call->func) {
2270 			zend_string *zend_function_name;
2271 
2272 			func = call->func;
2273 			if (func->common.scope && func->common.scope->trait_aliases) {
2274 				zend_function_name = zend_resolve_method_name(object ? object->ce : func->common.scope, func);
2275 			} else {
2276 				zend_function_name = func->common.function_name;
2277 			}
2278 			if (zend_function_name != NULL) {
2279 				function_name = ZSTR_VAL(zend_function_name);
2280 			} else {
2281 				function_name = NULL;
2282 			}
2283 		} else {
2284 			func = NULL;
2285 			function_name = NULL;
2286 		}
2287 
2288 		if (function_name) {
2289 			if (object) {
2290 				if (func->common.scope) {
2291 					class_name = func->common.scope->name;
2292 				} else if (object->handlers->get_class_name == zend_std_get_class_name) {
2293 					class_name = object->ce->name;
2294 				} else {
2295 					class_name = object->handlers->get_class_name(object);
2296 				}
2297 
2298 				call_type = "->";
2299 			} else if (func->common.scope) {
2300 				class_name = func->common.scope->name;
2301 				call_type = "::";
2302 			} else {
2303 				class_name = NULL;
2304 				call_type = NULL;
2305 			}
2306 			if (func->type != ZEND_EVAL_CODE) {
2307 				if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0) {
2308 					debug_backtrace_get_args(call, &arg_array);
2309 				}
2310 			}
2311 		} else {
2312 			/* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2313 			zend_bool build_filename_arg = 1;
2314 
2315 			if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2316 				/* can happen when calling eval from a custom sapi */
2317 				function_name = "unknown";
2318 				build_filename_arg = 0;
2319 			} else
2320 			switch (ptr->opline->extended_value) {
2321 				case ZEND_EVAL:
2322 					function_name = "eval";
2323 					build_filename_arg = 0;
2324 					break;
2325 				case ZEND_INCLUDE:
2326 					function_name = "include";
2327 					break;
2328 				case ZEND_REQUIRE:
2329 					function_name = "require";
2330 					break;
2331 				case ZEND_INCLUDE_ONCE:
2332 					function_name = "include_once";
2333 					break;
2334 				case ZEND_REQUIRE_ONCE:
2335 					function_name = "require_once";
2336 					break;
2337 				default:
2338 					/* this can actually happen if you use debug_backtrace() in your error_handler and
2339 					 * you're in the top-scope */
2340 					function_name = "unknown";
2341 					build_filename_arg = 0;
2342 					break;
2343 			}
2344 
2345 			if (build_filename_arg && include_filename) {
2346 				array_init(&arg_array);
2347 				add_next_index_string(&arg_array, (char*)include_filename);
2348 			}
2349 			call_type = NULL;
2350 		}
2351 		zend_printf("#%-2d ", indent);
2352 		if (class_name) {
2353 			ZEND_PUTS(ZSTR_VAL(class_name));
2354 			ZEND_PUTS(call_type);
2355 			if (object
2356 			  && !func->common.scope
2357 			  && object->handlers->get_class_name != zend_std_get_class_name) {
2358 				zend_string_release_ex(class_name, 0);
2359 			}
2360 		}
2361 		zend_printf("%s(", function_name);
2362 		if (Z_TYPE(arg_array) != IS_UNDEF) {
2363 			debug_print_backtrace_args(&arg_array);
2364 			zval_ptr_dtor(&arg_array);
2365 		}
2366 		if (filename) {
2367 			zend_printf(") called at [%s:%d]\n", filename, lineno);
2368 		} else {
2369 			zend_execute_data *prev_call = skip;
2370 			zend_execute_data *prev = skip->prev_execute_data;
2371 
2372 			while (prev) {
2373 				if (prev_call &&
2374 					prev_call->func &&
2375 					!ZEND_USER_CODE(prev_call->func->common.type)) {
2376 					prev = NULL;
2377 					break;
2378 				}
2379 				if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2380 					zend_printf(") called at [%s:%d]\n", ZSTR_VAL(prev->func->op_array.filename), prev->opline->lineno);
2381 					break;
2382 				}
2383 				prev_call = prev;
2384 				prev = prev->prev_execute_data;
2385 			}
2386 			if (!prev) {
2387 				ZEND_PUTS(")\n");
2388 			}
2389 		}
2390 		include_filename = filename;
2391 		call = skip;
2392 		ptr = skip->prev_execute_data;
2393 		++indent;
2394 	}
2395 }
2396 
2397 /* }}} */
2398 
zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit)2399 ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
2400 {
2401 	zend_execute_data *ptr, *skip, *call = NULL;
2402 	zend_object *object;
2403 	int lineno, frameno = 0;
2404 	zend_function *func;
2405 	zend_string *function_name;
2406 	zend_string *filename;
2407 	zend_string *include_filename = NULL;
2408 	zval stack_frame, tmp;
2409 
2410 	array_init(return_value);
2411 
2412 	if (!(ptr = EG(current_execute_data))) {
2413 		return;
2414 	}
2415 
2416 	if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type)) {
2417 		call = ptr;
2418 		ptr = ptr->prev_execute_data;
2419 	}
2420 
2421 	if (ptr) {
2422 		if (skip_last) {
2423 			/* skip debug_backtrace() */
2424 			call = ptr;
2425 			ptr = ptr->prev_execute_data;
2426 		} else {
2427 			/* skip "new Exception()" */
2428 			if (ptr->func && ZEND_USER_CODE(ptr->func->common.type) && (ptr->opline->opcode == ZEND_NEW)) {
2429 				call = ptr;
2430 				ptr = ptr->prev_execute_data;
2431 			}
2432 		}
2433 		if (!call) {
2434 			call = ptr;
2435 			ptr = ptr->prev_execute_data;
2436 		}
2437 	}
2438 
2439 	while (ptr && (limit == 0 || frameno < limit)) {
2440 		frameno++;
2441 		array_init(&stack_frame);
2442 
2443 		ptr = zend_generator_check_placeholder_frame(ptr);
2444 
2445 		skip = ptr;
2446 		/* skip internal handler */
2447 		if (skip_internal_handler(skip)) {
2448 			skip = skip->prev_execute_data;
2449 		}
2450 
2451 		if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2452 			filename = skip->func->op_array.filename;
2453 			if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2454 				if (EG(opline_before_exception)) {
2455 					lineno = EG(opline_before_exception)->lineno;
2456 				} else {
2457 					lineno = skip->func->op_array.line_end;
2458 				}
2459 			} else {
2460 				lineno = skip->opline->lineno;
2461 			}
2462 			ZVAL_STR_COPY(&tmp, filename);
2463 			zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
2464 			ZVAL_LONG(&tmp, lineno);
2465 			zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
2466 
2467 			/* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
2468 			 * and debug_baktrace() might have been called by the error_handler. in this case we don't
2469 			 * want to pop anything of the argument-stack */
2470 		} else {
2471 			zend_execute_data *prev_call = skip;
2472 			zend_execute_data *prev = skip->prev_execute_data;
2473 
2474 			while (prev) {
2475 				if (prev_call &&
2476 					prev_call->func &&
2477 					!ZEND_USER_CODE(prev_call->func->common.type) &&
2478 					!(prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2479 					break;
2480 				}
2481 				if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2482 					ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
2483 					zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
2484 					ZVAL_LONG(&tmp, prev->opline->lineno);
2485 					zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
2486 					break;
2487 				}
2488 				prev_call = prev;
2489 				prev = prev->prev_execute_data;
2490 			}
2491 			filename = NULL;
2492 		}
2493 
2494 		/* $this may be passed into regular internal functions */
2495 		object = (call && (Z_TYPE(call->This) == IS_OBJECT)) ? Z_OBJ(call->This) : NULL;
2496 
2497 		if (call && call->func) {
2498 			func = call->func;
2499 			function_name = (func->common.scope &&
2500 							 func->common.scope->trait_aliases) ?
2501 				zend_resolve_method_name(
2502 					(object ? object->ce : func->common.scope), func) :
2503 				func->common.function_name;
2504 		} else {
2505 			func = NULL;
2506 			function_name = NULL;
2507 		}
2508 
2509 		if (function_name) {
2510 			ZVAL_STR_COPY(&tmp, function_name);
2511 			zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
2512 
2513 			if (object) {
2514 				if (func->common.scope) {
2515 					ZVAL_STR_COPY(&tmp, func->common.scope->name);
2516 				} else if (object->handlers->get_class_name == zend_std_get_class_name) {
2517 					ZVAL_STR_COPY(&tmp, object->ce->name);
2518 				} else {
2519 					ZVAL_STR(&tmp, object->handlers->get_class_name(object));
2520 				}
2521 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
2522 				if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
2523 					ZVAL_OBJ(&tmp, object);
2524 					zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp);
2525 					Z_ADDREF(tmp);
2526 				}
2527 
2528 				ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
2529 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
2530 			} else if (func->common.scope) {
2531 				ZVAL_STR_COPY(&tmp, func->common.scope->name);
2532 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
2533 				ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
2534 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
2535 			}
2536 
2537 			if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
2538 				func->type != ZEND_EVAL_CODE) {
2539 
2540 				debug_backtrace_get_args(call, &tmp);
2541 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
2542 			}
2543 		} else {
2544 			/* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2545 			zend_bool build_filename_arg = 1;
2546 			zend_string *pseudo_function_name;
2547 
2548 			if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2549 				/* can happen when calling eval from a custom sapi */
2550 				pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
2551 				build_filename_arg = 0;
2552 			} else
2553 			switch (ptr->opline->extended_value) {
2554 				case ZEND_EVAL:
2555 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_EVAL);
2556 					build_filename_arg = 0;
2557 					break;
2558 				case ZEND_INCLUDE:
2559 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE);
2560 					break;
2561 				case ZEND_REQUIRE:
2562 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE);
2563 					break;
2564 				case ZEND_INCLUDE_ONCE:
2565 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE_ONCE);
2566 					break;
2567 				case ZEND_REQUIRE_ONCE:
2568 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE_ONCE);
2569 					break;
2570 				default:
2571 					/* this can actually happen if you use debug_backtrace() in your error_handler and
2572 					 * you're in the top-scope */
2573 					pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
2574 					build_filename_arg = 0;
2575 					break;
2576 			}
2577 
2578 			if (build_filename_arg && include_filename) {
2579 				zval arg_array;
2580 
2581 				array_init(&arg_array);
2582 
2583 				/* include_filename always points to the last filename of the last last called-function.
2584 				   if we have called include in the frame above - this is the file we have included.
2585 				 */
2586 
2587 				ZVAL_STR_COPY(&tmp, include_filename);
2588 				zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
2589 				zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array);
2590 			}
2591 
2592 			ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
2593 			zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
2594 		}
2595 
2596 		zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &stack_frame);
2597 
2598 		include_filename = filename;
2599 
2600 		call = skip;
2601 		ptr = skip->prev_execute_data;
2602 	}
2603 }
2604 /* }}} */
2605 
2606 /* {{{ proto array debug_backtrace([int options[, int limit]])
2607    Return backtrace as array */
ZEND_FUNCTIONnull2608 ZEND_FUNCTION(debug_backtrace)
2609 {
2610 	zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2611 	zend_long limit = 0;
2612 
2613 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
2614 		return;
2615 	}
2616 
2617 	zend_fetch_debug_backtrace(return_value, 1, options, limit);
2618 }
2619 /* }}} */
2620 
2621 /* {{{ proto bool extension_loaded(string extension_name)
2622    Returns true if the named extension is loaded */
ZEND_FUNCTIONnull2623 ZEND_FUNCTION(extension_loaded)
2624 {
2625 	zend_string *extension_name;
2626 	zend_string *lcname;
2627 
2628 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
2629 		return;
2630 	}
2631 
2632 	lcname = zend_string_tolower(extension_name);
2633 	if (zend_hash_exists(&module_registry, lcname)) {
2634 		RETVAL_TRUE;
2635 	} else {
2636 		RETVAL_FALSE;
2637 	}
2638 	zend_string_release_ex(lcname, 0);
2639 }
2640 /* }}} */
2641 
2642 /* {{{ proto array get_extension_funcs(string extension_name)
2643    Returns an array with the names of functions belonging to the named extension */
ZEND_FUNCTIONnull2644 ZEND_FUNCTION(get_extension_funcs)
2645 {
2646 	zend_string *extension_name;
2647 	zend_string *lcname;
2648 	int array;
2649 	zend_module_entry *module;
2650 	zend_function *zif;
2651 
2652 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
2653 		return;
2654 	}
2655 	if (strncasecmp(ZSTR_VAL(extension_name), "zend", sizeof("zend"))) {
2656 		lcname = zend_string_tolower(extension_name);
2657 		module = zend_hash_find_ptr(&module_registry, lcname);
2658 		zend_string_release_ex(lcname, 0);
2659 	} else {
2660 		module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
2661 	}
2662 
2663 	if (!module) {
2664 		RETURN_FALSE;
2665 	}
2666 
2667 	if (module->functions) {
2668 		/* avoid BC break, if functions list is empty, will return an empty array */
2669 		array_init(return_value);
2670 		array = 1;
2671 	} else {
2672 		array = 0;
2673 	}
2674 
2675 	ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
2676 		if (zif->common.type == ZEND_INTERNAL_FUNCTION
2677 			&& zif->internal_function.module == module) {
2678 			if (!array) {
2679 				array_init(return_value);
2680 				array = 1;
2681 			}
2682 			add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
2683 		}
2684 	} ZEND_HASH_FOREACH_END();
2685 
2686 	if (!array) {
2687 		RETURN_FALSE;
2688 	}
2689 }
2690 /* }}} */
2691