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