xref: /PHP-7.4/Zend/zend_exceptions.c (revision fc6f53d4)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Marcus Boerger <helly@php.net>                              |
17    |          Sterling Hughes <sterling@php.net>                          |
18    |          Zeev Suraski <zeev@php.net>                                 |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend.h"
23 #include "zend_API.h"
24 #include "zend_builtin_functions.h"
25 #include "zend_interfaces.h"
26 #include "zend_exceptions.h"
27 #include "zend_vm.h"
28 #include "zend_dtrace.h"
29 #include "zend_smart_str.h"
30 
31 ZEND_API zend_class_entry *zend_ce_throwable;
32 ZEND_API zend_class_entry *zend_ce_exception;
33 ZEND_API zend_class_entry *zend_ce_error_exception;
34 ZEND_API zend_class_entry *zend_ce_error;
35 ZEND_API zend_class_entry *zend_ce_compile_error;
36 ZEND_API zend_class_entry *zend_ce_parse_error;
37 ZEND_API zend_class_entry *zend_ce_type_error;
38 ZEND_API zend_class_entry *zend_ce_argument_count_error;
39 ZEND_API zend_class_entry *zend_ce_arithmetic_error;
40 ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
41 
42 ZEND_API void (*zend_throw_exception_hook)(zval *ex);
43 
44 static zend_object_handlers default_exception_handlers;
45 
46 /* {{{ zend_implement_throwable */
zend_implement_throwable(zend_class_entry * interface,zend_class_entry * class_type)47 static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
48 {
49 	if (instanceof_function(class_type, zend_ce_exception) || instanceof_function(class_type, zend_ce_error)) {
50 		return SUCCESS;
51 	}
52 	zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead",
53 		ZSTR_VAL(class_type->name),
54 		ZSTR_VAL(interface->name),
55 		ZSTR_VAL(zend_ce_exception->name),
56 		ZSTR_VAL(zend_ce_error->name));
57 	return FAILURE;
58 }
59 /* }}} */
60 
i_get_exception_base(zval * object)61 static inline zend_class_entry *i_get_exception_base(zval *object) /* {{{ */
62 {
63 	return instanceof_function(Z_OBJCE_P(object), zend_ce_exception) ? zend_ce_exception : zend_ce_error;
64 }
65 /* }}} */
66 
zend_get_exception_base(zval * object)67 ZEND_API zend_class_entry *zend_get_exception_base(zval *object) /* {{{ */
68 {
69 	return i_get_exception_base(object);
70 }
71 /* }}} */
72 
zend_exception_set_previous(zend_object * exception,zend_object * add_previous)73 void zend_exception_set_previous(zend_object *exception, zend_object *add_previous) /* {{{ */
74 {
75     zval *previous, *ancestor, *ex;
76 	zval  pv, zv, rv;
77 	zend_class_entry *base_ce;
78 
79 	if (!exception || !add_previous) {
80 		return;
81 	}
82 
83 	if (exception == add_previous) {
84 		OBJ_RELEASE(add_previous);
85 		return;
86 	}
87 
88 	ZVAL_OBJ(&pv, add_previous);
89 	if (!instanceof_function(Z_OBJCE(pv), zend_ce_throwable)) {
90 		zend_error_noreturn(E_CORE_ERROR, "Previous exception must implement Throwable");
91 		return;
92 	}
93 	ZVAL_OBJ(&zv, exception);
94 	ex = &zv;
95 	do {
96 		ancestor = zend_read_property_ex(i_get_exception_base(&pv), &pv, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
97 		while (Z_TYPE_P(ancestor) == IS_OBJECT) {
98 			if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
99 				OBJ_RELEASE(add_previous);
100 				return;
101 			}
102 			ancestor = zend_read_property_ex(i_get_exception_base(ancestor), ancestor, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
103 		}
104 		base_ce = i_get_exception_base(ex);
105 		previous = zend_read_property_ex(base_ce, ex, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
106 		if (Z_TYPE_P(previous) == IS_NULL) {
107 			zend_update_property_ex(base_ce, ex, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
108 			GC_DELREF(add_previous);
109 			return;
110 		}
111 		ex = previous;
112 	} while (Z_OBJ_P(ex) != add_previous);
113 }
114 /* }}} */
115 
zend_exception_save(void)116 void zend_exception_save(void) /* {{{ */
117 {
118 	if (EG(prev_exception)) {
119 		zend_exception_set_previous(EG(exception), EG(prev_exception));
120 	}
121 	if (EG(exception)) {
122 		EG(prev_exception) = EG(exception);
123 	}
124 	EG(exception) = NULL;
125 }
126 /* }}} */
127 
zend_exception_restore(void)128 void zend_exception_restore(void) /* {{{ */
129 {
130 	if (EG(prev_exception)) {
131 		if (EG(exception)) {
132 			zend_exception_set_previous(EG(exception), EG(prev_exception));
133 		} else {
134 			EG(exception) = EG(prev_exception);
135 		}
136 		EG(prev_exception) = NULL;
137 	}
138 }
139 /* }}} */
140 
zend_throw_exception_internal(zval * exception)141 ZEND_API ZEND_COLD void zend_throw_exception_internal(zval *exception) /* {{{ */
142 {
143 #ifdef HAVE_DTRACE
144 	if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
145 		if (exception != NULL) {
146 			DTRACE_EXCEPTION_THROWN(ZSTR_VAL(Z_OBJ_P(exception)->ce->name));
147 		} else {
148 			DTRACE_EXCEPTION_THROWN(NULL);
149 		}
150 	}
151 #endif /* HAVE_DTRACE */
152 
153 	if (exception != NULL) {
154 		zend_object *previous = EG(exception);
155 		zend_exception_set_previous(Z_OBJ_P(exception), EG(exception));
156 		EG(exception) = Z_OBJ_P(exception);
157 		if (previous) {
158 			return;
159 		}
160 	}
161 	if (!EG(current_execute_data)) {
162 		if (exception && (Z_OBJCE_P(exception) == zend_ce_parse_error || Z_OBJCE_P(exception) == zend_ce_compile_error)) {
163 			return;
164 		}
165 		if(EG(exception)) {
166 			zend_exception_error(EG(exception), E_ERROR);
167 		}
168 		zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
169 	}
170 
171 	if (zend_throw_exception_hook) {
172 		zend_throw_exception_hook(exception);
173 	}
174 
175 	if (!EG(current_execute_data)->func ||
176 	    !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
177 	    EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {
178 		/* no need to rethrow the exception */
179 		return;
180 	}
181 	EG(opline_before_exception) = EG(current_execute_data)->opline;
182 	EG(current_execute_data)->opline = EG(exception_op);
183 }
184 /* }}} */
185 
zend_clear_exception(void)186 ZEND_API void zend_clear_exception(void) /* {{{ */
187 {
188 	zend_object *exception;
189 	if (EG(prev_exception)) {
190 		OBJ_RELEASE(EG(prev_exception));
191 		EG(prev_exception) = NULL;
192 	}
193 	if (!EG(exception)) {
194 		return;
195 	}
196 	/* exception may have destructor */
197 	exception = EG(exception);
198 	EG(exception) = NULL;
199 	OBJ_RELEASE(exception);
200 	if (EG(current_execute_data)) {
201 		EG(current_execute_data)->opline = EG(opline_before_exception);
202 	}
203 #if ZEND_DEBUG
204 	EG(opline_before_exception) = NULL;
205 #endif
206 }
207 /* }}} */
208 
zend_default_exception_new_ex(zend_class_entry * class_type,int skip_top_traces)209 static zend_object *zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces) /* {{{ */
210 {
211 	zval obj, tmp;
212 	zend_object *object;
213 	zval trace;
214 	zend_class_entry *base_ce;
215 	zend_string *filename;
216 
217 	Z_OBJ(obj) = object = zend_objects_new(class_type);
218 	Z_OBJ_HT(obj) = &default_exception_handlers;
219 
220 	object_properties_init(object, class_type);
221 
222 	if (EG(current_execute_data)) {
223 		zend_fetch_debug_backtrace(&trace,
224 			skip_top_traces,
225 			EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
226 	} else {
227 		array_init(&trace);
228 	}
229 	Z_SET_REFCOUNT(trace, 0);
230 
231 	base_ce = i_get_exception_base(&obj);
232 
233 	if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
234 			|| !(filename = zend_get_compiled_filename()))) {
235 		ZVAL_STRING(&tmp, zend_get_executed_filename());
236 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
237 		zval_ptr_dtor(&tmp);
238 		ZVAL_LONG(&tmp, zend_get_executed_lineno());
239 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
240 	} else {
241 		ZVAL_STR(&tmp, filename);
242 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
243 		ZVAL_LONG(&tmp, zend_get_compiled_lineno());
244 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
245 	}
246 	zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
247 
248 	return object;
249 }
250 /* }}} */
251 
zend_default_exception_new(zend_class_entry * class_type)252 static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
253 {
254 	return zend_default_exception_new_ex(class_type, 0);
255 }
256 /* }}} */
257 
zend_error_exception_new(zend_class_entry * class_type)258 static zend_object *zend_error_exception_new(zend_class_entry *class_type) /* {{{ */
259 {
260 	return zend_default_exception_new_ex(class_type, 2);
261 }
262 /* }}} */
263 
264 /* {{{ proto Exception|Error Exception|Error::__clone()
265    Clone the exception object */
ZEND_METHOD(exception,__clone)266 ZEND_COLD ZEND_METHOD(exception, __clone)
267 {
268 	/* Should never be executable */
269 	zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
270 }
271 /* }}} */
272 
273 /* {{{ proto Exception|Error::__construct(string message, int code [, Throwable previous])
274    Exception constructor */
ZEND_METHOD(exception,__construct)275 ZEND_METHOD(exception, __construct)
276 {
277 	zend_string *message = NULL;
278 	zend_long   code = 0;
279 	zval  tmp, *object, *previous = NULL;
280 	zend_class_entry *base_ce;
281 	int    argc = ZEND_NUM_ARGS();
282 
283 	object = ZEND_THIS;
284 	base_ce = i_get_exception_base(object);
285 
286 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
287 		zend_class_entry *ce;
288 
289 		if (Z_TYPE(EX(This)) == IS_OBJECT) {
290 			ce = Z_OBJCE(EX(This));
291 		} else if (Z_CE(EX(This))) {
292 			ce = Z_CE(EX(This));
293 		} else {
294 			ce = base_ce;
295 		}
296 		zend_throw_error(NULL, "Wrong parameters for %s([string $message [, long $code [, Throwable $previous = NULL]]])", ZSTR_VAL(ce->name));
297 		return;
298 	}
299 
300 	if (message) {
301 		ZVAL_STR(&tmp, message);
302 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
303 	}
304 
305 	if (code) {
306 		ZVAL_LONG(&tmp, code);
307 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
308 	}
309 
310 	if (previous) {
311 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
312 	}
313 }
314 /* }}} */
315 
316 /* {{{ proto Exception::__wakeup()
317    Exception unserialize checks */
318 #define CHECK_EXC_TYPE(id, type) \
319 	pvalue = zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 1, &value); \
320 	if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
321 		zend_unset_property(i_get_exception_base(object), object, ZSTR_VAL(ZSTR_KNOWN(id)), ZSTR_LEN(ZSTR_KNOWN(id))); \
322 	}
323 
ZEND_METHOD(exception,__wakeup)324 ZEND_METHOD(exception, __wakeup)
325 {
326 	zval value, *pvalue;
327 	zval *object = ZEND_THIS;
328 	CHECK_EXC_TYPE(ZEND_STR_MESSAGE,  IS_STRING);
329 	CHECK_EXC_TYPE(ZEND_STR_STRING,   IS_STRING);
330 	CHECK_EXC_TYPE(ZEND_STR_CODE,     IS_LONG);
331 	CHECK_EXC_TYPE(ZEND_STR_FILE,     IS_STRING);
332 	CHECK_EXC_TYPE(ZEND_STR_LINE,     IS_LONG);
333 	CHECK_EXC_TYPE(ZEND_STR_TRACE,    IS_ARRAY);
334 	pvalue = zend_read_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1, 1, &value);
335 	if (pvalue && Z_TYPE_P(pvalue) != IS_NULL && (Z_TYPE_P(pvalue) != IS_OBJECT ||
336 			!instanceof_function(Z_OBJCE_P(pvalue), zend_ce_throwable) ||
337 			pvalue == object)) {
338 		zend_unset_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1);
339 	}
340 }
341 /* }}} */
342 
343 /* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Throwable previous]]])
344    ErrorException constructor */
ZEND_METHOD(error_exception,__construct)345 ZEND_METHOD(error_exception, __construct)
346 {
347 	zend_string *message = NULL, *filename = NULL;
348 	zend_long   code = 0, severity = E_ERROR, lineno;
349 	zval   tmp, *object, *previous = NULL;
350 	int    argc = ZEND_NUM_ARGS();
351 
352 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|SllSlO!", &message, &code, &severity, &filename, &lineno, &previous, zend_ce_throwable) == FAILURE) {
353 		zend_class_entry *ce;
354 
355 		if (Z_TYPE(EX(This)) == IS_OBJECT) {
356 			ce = Z_OBJCE(EX(This));
357 		} else if (Z_CE(EX(This))) {
358 			ce = Z_CE(EX(This));
359 		} else {
360 			ce = zend_ce_error_exception;
361 		}
362 		zend_throw_error(NULL, "Wrong parameters for %s([string $message [, long $code, [ long $severity, [ string $filename, [ long $lineno  [, Throwable $previous = NULL]]]]]])", ZSTR_VAL(ce->name));
363 		return;
364 	}
365 
366 	object = ZEND_THIS;
367 
368 	if (message) {
369 		ZVAL_STR_COPY(&tmp, message);
370 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
371 		zval_ptr_dtor(&tmp);
372 	}
373 
374 	if (code) {
375 		ZVAL_LONG(&tmp, code);
376 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
377 	}
378 
379 	if (previous) {
380 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
381 	}
382 
383 	ZVAL_LONG(&tmp, severity);
384 	zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
385 
386 	if (argc >= 4) {
387 		ZVAL_STR_COPY(&tmp, filename);
388 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
389 		zval_ptr_dtor(&tmp);
390     	if (argc < 5) {
391     	    lineno = 0; /* invalidate lineno */
392     	}
393 		ZVAL_LONG(&tmp, lineno);
394 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
395 	}
396 }
397 /* }}} */
398 
399 #define DEFAULT_0_PARAMS \
400 	if (zend_parse_parameters_none() == FAILURE) { \
401 		return; \
402 	}
403 
404 #define GET_PROPERTY(object, id) \
405 	zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 0, &rv)
406 #define GET_PROPERTY_SILENT(object, id) \
407 	zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 1, &rv)
408 
409 /* {{{ proto string Exception|Error::getFile()
410    Get the file in which the exception occurred */
ZEND_METHOD(exception,getFile)411 ZEND_METHOD(exception, getFile)
412 {
413 	zval *prop, rv;
414 
415 	DEFAULT_0_PARAMS;
416 
417 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
418 	ZVAL_DEREF(prop);
419 	ZVAL_COPY(return_value, prop);
420 }
421 /* }}} */
422 
423 /* {{{ proto int Exception|Error::getLine()
424    Get the line in which the exception occurred */
ZEND_METHOD(exception,getLine)425 ZEND_METHOD(exception, getLine)
426 {
427 	zval *prop, rv;
428 
429 	DEFAULT_0_PARAMS;
430 
431 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
432 	ZVAL_DEREF(prop);
433 	ZVAL_COPY(return_value, prop);
434 }
435 /* }}} */
436 
437 /* {{{ proto string Exception|Error::getMessage()
438    Get the exception message */
ZEND_METHOD(exception,getMessage)439 ZEND_METHOD(exception, getMessage)
440 {
441 	zval *prop, rv;
442 
443 	DEFAULT_0_PARAMS;
444 
445 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
446 	ZVAL_DEREF(prop);
447 	ZVAL_COPY(return_value, prop);
448 }
449 /* }}} */
450 
451 /* {{{ proto int Exception|Error::getCode()
452    Get the exception code */
ZEND_METHOD(exception,getCode)453 ZEND_METHOD(exception, getCode)
454 {
455 	zval *prop, rv;
456 
457 	DEFAULT_0_PARAMS;
458 
459 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
460 	ZVAL_DEREF(prop);
461 	ZVAL_COPY(return_value, prop);
462 }
463 /* }}} */
464 
465 /* {{{ proto array Exception|Error::getTrace()
466    Get the stack trace for the location in which the exception occurred */
ZEND_METHOD(exception,getTrace)467 ZEND_METHOD(exception, getTrace)
468 {
469 	zval *prop, rv;
470 
471 	DEFAULT_0_PARAMS;
472 
473 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
474 	ZVAL_DEREF(prop);
475 	ZVAL_COPY(return_value, prop);
476 }
477 /* }}} */
478 
479 /* {{{ proto int ErrorException::getSeverity()
480    Get the exception severity */
ZEND_METHOD(error_exception,getSeverity)481 ZEND_METHOD(error_exception, getSeverity)
482 {
483 	zval *prop, rv;
484 
485 	DEFAULT_0_PARAMS;
486 
487 	prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_SEVERITY);
488 	ZVAL_DEREF(prop);
489 	ZVAL_COPY(return_value, prop);
490 }
491 /* }}} */
492 
493 #define TRACE_APPEND_KEY(key) do {                                          \
494 		tmp = zend_hash_find(ht, key);                                      \
495 		if (tmp) {                                                          \
496 			if (Z_TYPE_P(tmp) != IS_STRING) {                               \
497 				zend_error(E_WARNING, "Value for %s is no string",          \
498 					ZSTR_VAL(key));                                         \
499 				smart_str_appends(str, "[unknown]");                        \
500 			} else {                                                        \
501 				smart_str_appends(str, Z_STRVAL_P(tmp));                    \
502 			}                                                               \
503 		} \
504 	} while (0)
505 
_build_trace_args(zval * arg,smart_str * str)506 static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
507 {
508 	/* the trivial way would be to do
509 	 * convert_to_string_ex(arg);
510 	 * append it and kill the now tmp arg.
511 	 * but that could cause some E_NOTICE and also damn long lines.
512 	 */
513 
514 	ZVAL_DEREF(arg);
515 	switch (Z_TYPE_P(arg)) {
516 		case IS_NULL:
517 			smart_str_appends(str, "NULL, ");
518 			break;
519 		case IS_STRING:
520 			smart_str_appendc(str, '\'');
521 			smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15));
522 			if (Z_STRLEN_P(arg) > 15) {
523 				smart_str_appends(str, "...', ");
524 			} else {
525 				smart_str_appends(str, "', ");
526 			}
527 			break;
528 		case IS_FALSE:
529 			smart_str_appends(str, "false, ");
530 			break;
531 		case IS_TRUE:
532 			smart_str_appends(str, "true, ");
533 			break;
534 		case IS_RESOURCE:
535 			smart_str_appends(str, "Resource id #");
536 			smart_str_append_long(str, Z_RES_HANDLE_P(arg));
537 			smart_str_appends(str, ", ");
538 			break;
539 		case IS_LONG:
540 			smart_str_append_long(str, Z_LVAL_P(arg));
541 			smart_str_appends(str, ", ");
542 			break;
543 		case IS_DOUBLE: {
544 			smart_str_append_printf(str, "%.*G", (int) EG(precision), Z_DVAL_P(arg));
545 			smart_str_appends(str, ", ");
546 			break;
547 		}
548 		case IS_ARRAY:
549 			smart_str_appends(str, "Array, ");
550 			break;
551 		case IS_OBJECT: {
552 			zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
553 			smart_str_appends(str, "Object(");
554 			smart_str_appends(str, ZSTR_VAL(class_name));
555 			smart_str_appends(str, "), ");
556 			zend_string_release_ex(class_name, 0);
557 			break;
558 		}
559 	}
560 }
561 /* }}} */
562 
_build_trace_string(smart_str * str,HashTable * ht,uint32_t num)563 static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
564 {
565 	zval *file, *tmp;
566 
567 	smart_str_appendc(str, '#');
568 	smart_str_append_long(str, num);
569 	smart_str_appendc(str, ' ');
570 
571 	file = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_FILE), 1);
572 	if (file) {
573 		if (Z_TYPE_P(file) != IS_STRING) {
574 			zend_error(E_WARNING, "Function name is no string");
575 			smart_str_appends(str, "[unknown function]");
576 		} else{
577 			zend_long line;
578 			tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_LINE), 1);
579 			if (tmp) {
580 				if (Z_TYPE_P(tmp) == IS_LONG) {
581 					line = Z_LVAL_P(tmp);
582 				} else {
583 					zend_error(E_WARNING, "Line is no long");
584 					line = 0;
585 				}
586 			} else {
587 				line = 0;
588 			}
589 			smart_str_append(str, Z_STR_P(file));
590 			smart_str_appendc(str, '(');
591 			smart_str_append_long(str, line);
592 			smart_str_appends(str, "): ");
593 		}
594 	} else {
595 		smart_str_appends(str, "[internal function]: ");
596 	}
597 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
598 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
599 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
600 	smart_str_appendc(str, '(');
601 	tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_ARGS), 1);
602 	if (tmp) {
603 		if (Z_TYPE_P(tmp) == IS_ARRAY) {
604 			size_t last_len = ZSTR_LEN(str->s);
605 			zval *arg;
606 
607 			ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmp), arg) {
608 				_build_trace_args(arg, str);
609 			} ZEND_HASH_FOREACH_END();
610 
611 			if (last_len != ZSTR_LEN(str->s)) {
612 				ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
613 			}
614 		} else {
615 			zend_error(E_WARNING, "args element is no array");
616 		}
617 	}
618 	smart_str_appends(str, ")\n");
619 }
620 /* }}} */
621 
622 /* {{{ proto string Exception|Error::getTraceAsString()
623    Obtain the backtrace for the exception as a string (instead of an array) */
ZEND_METHOD(exception,getTraceAsString)624 ZEND_METHOD(exception, getTraceAsString)
625 {
626 	zval *trace, *frame, rv;
627 	zend_ulong index;
628 	zval *object;
629 	zend_class_entry *base_ce;
630 	smart_str str = {0};
631 	uint32_t num = 0;
632 
633 	DEFAULT_0_PARAMS;
634 
635 	object = ZEND_THIS;
636 	base_ce = i_get_exception_base(object);
637 
638 	trace = zend_read_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
639 	if (Z_TYPE_P(trace) != IS_ARRAY) {
640 		RETURN_FALSE;
641 	}
642 	ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(trace), index, frame) {
643 		if (Z_TYPE_P(frame) != IS_ARRAY) {
644 			zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
645 			continue;
646 		}
647 
648 		_build_trace_string(&str, Z_ARRVAL_P(frame), num++);
649 	} ZEND_HASH_FOREACH_END();
650 
651 	smart_str_appendc(&str, '#');
652 	smart_str_append_long(&str, num);
653 	smart_str_appends(&str, " {main}");
654 	smart_str_0(&str);
655 
656 	RETURN_NEW_STR(str.s);
657 }
658 /* }}} */
659 
660 /* {{{ proto Throwable Exception|Error::getPrevious()
661    Return previous Throwable or NULL. */
ZEND_METHOD(exception,getPrevious)662 ZEND_METHOD(exception, getPrevious)
663 {
664 	zval rv;
665 
666 	DEFAULT_0_PARAMS;
667 
668 	ZVAL_COPY(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
669 } /* }}} */
670 
671 /* {{{ proto string Exception|Error::__toString()
672    Obtain the string representation of the Exception object */
ZEND_METHOD(exception,__toString)673 ZEND_METHOD(exception, __toString)
674 {
675 	zval trace, *exception;
676 	zend_class_entry *base_ce;
677 	zend_string *str;
678 	zend_fcall_info fci;
679 	zval rv, tmp;
680 	zend_string *fname;
681 
682 	DEFAULT_0_PARAMS;
683 
684 	str = ZSTR_EMPTY_ALLOC();
685 
686 	exception = ZEND_THIS;
687 	fname = zend_string_init("gettraceasstring", sizeof("gettraceasstring")-1, 0);
688 
689 	while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
690 		zend_string *prev_str = str;
691 		zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
692 		zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
693 		zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
694 
695 		fci.size = sizeof(fci);
696 		ZVAL_STR(&fci.function_name, fname);
697 		fci.object = Z_OBJ_P(exception);
698 		fci.retval = &trace;
699 		fci.param_count = 0;
700 		fci.params = NULL;
701 		fci.no_separation = 1;
702 
703 		zend_call_function(&fci, NULL);
704 
705 		if (Z_TYPE(trace) != IS_STRING) {
706 			zval_ptr_dtor(&trace);
707 			ZVAL_UNDEF(&trace);
708 		}
709 
710 		if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
711 			zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
712 			zend_string_release_ex(message, 0);
713 			message = real_message;
714 		}
715 
716 		if (ZSTR_LEN(message) > 0) {
717 			str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
718 					"\nStack trace:\n%s%s%s",
719 					ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
720 					(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
721 					ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
722 		} else {
723 			str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
724 					"\nStack trace:\n%s%s%s",
725 					ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
726 					(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
727 					ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
728 		}
729 
730 		zend_string_release_ex(prev_str, 0);
731 		zend_string_release_ex(message, 0);
732 		zend_string_release_ex(file, 0);
733 		zval_ptr_dtor(&trace);
734 
735 		Z_PROTECT_RECURSION_P(exception);
736 		exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
737 		if (exception && Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
738 			break;
739 		}
740 	}
741 	zend_string_release_ex(fname, 0);
742 
743 	exception = ZEND_THIS;
744 	/* Reset apply counts */
745 	while (exception && Z_TYPE_P(exception) == IS_OBJECT && (base_ce = i_get_exception_base(exception)) && instanceof_function(Z_OBJCE_P(exception), base_ce)) {
746 		if (Z_IS_RECURSIVE_P(exception)) {
747 			Z_UNPROTECT_RECURSION_P(exception);
748 		} else {
749 			break;
750 		}
751 		exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
752 	}
753 
754 	exception = ZEND_THIS;
755 	base_ce = i_get_exception_base(exception);
756 
757 	/* We store the result in the private property string so we can access
758 	 * the result in uncaught exception handlers without memleaks. */
759 	ZVAL_STR(&tmp, str);
760 	zend_update_property_ex(base_ce, exception, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
761 
762 	RETURN_STR(str);
763 }
764 /* }}} */
765 
766 /** {{{ Throwable method definition */
767 static const zend_function_entry zend_funcs_throwable[] = {
768 	ZEND_ABSTRACT_ME(throwable, getMessage,       NULL)
769 	ZEND_ABSTRACT_ME(throwable, getCode,          NULL)
770 	ZEND_ABSTRACT_ME(throwable, getFile,          NULL)
771 	ZEND_ABSTRACT_ME(throwable, getLine,          NULL)
772 	ZEND_ABSTRACT_ME(throwable, getTrace,         NULL)
773 	ZEND_ABSTRACT_ME(throwable, getPrevious,      NULL)
774 	ZEND_ABSTRACT_ME(throwable, getTraceAsString, NULL)
775 	ZEND_ABSTRACT_ME(throwable, __toString,       NULL)
776 	ZEND_FE_END
777 };
778 /* }}} */
779 
780 /* {{{ internal structs */
781 /* All functions that may be used in uncaught exception handlers must be final
782  * and must not throw exceptions. Otherwise we would need a facility to handle
783  * such exceptions in that handler.
784  * Also all getXY() methods are final because thy serve as read only access to
785  * their corresponding properties, no more, no less. If after all you need to
786  * override something then it is method __toString().
787  * And never try to change the state of exceptions and never implement anything
788  * that gives the user anything to accomplish this.
789  */
790 ZEND_BEGIN_ARG_INFO_EX(arginfo_exception___construct, 0, 0, 0)
791 	ZEND_ARG_INFO(0, message)
792 	ZEND_ARG_INFO(0, code)
793 	ZEND_ARG_INFO(0, previous)
794 ZEND_END_ARG_INFO()
795 
796 static const zend_function_entry default_exception_functions[] = {
797 	ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
798 	ZEND_ME(exception, __construct, arginfo_exception___construct, ZEND_ACC_PUBLIC)
799 	ZEND_ME(exception, __wakeup, NULL, ZEND_ACC_PUBLIC)
800 	ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
801 	ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
802 	ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
803 	ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
804 	ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
805 	ZEND_ME(exception, getPrevious, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
806 	ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
807 	ZEND_ME(exception, __toString, NULL, 0)
808 	ZEND_FE_END
809 };
810 
811 ZEND_BEGIN_ARG_INFO_EX(arginfo_error_exception___construct, 0, 0, 0)
812 	ZEND_ARG_INFO(0, message)
813 	ZEND_ARG_INFO(0, code)
814 	ZEND_ARG_INFO(0, severity)
815 	ZEND_ARG_INFO(0, filename)
816 	ZEND_ARG_INFO(0, lineno)
817 	ZEND_ARG_INFO(0, previous)
818 ZEND_END_ARG_INFO()
819 
820 static const zend_function_entry error_exception_functions[] = {
821 	ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, ZEND_ACC_PUBLIC)
822 	ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
823 	ZEND_FE_END
824 };
825 /* }}} */
826 
zend_register_default_exception(void)827 void zend_register_default_exception(void) /* {{{ */
828 {
829 	zend_class_entry ce;
830 
831 	REGISTER_MAGIC_INTERFACE(throwable, Throwable);
832 
833 	memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
834 	default_exception_handlers.clone_obj = NULL;
835 
836 	INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
837 	zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
838 	zend_ce_exception->create_object = zend_default_exception_new;
839 	zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
840 
841 	zend_declare_property_string(zend_ce_exception, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
842 	zend_declare_property_string(zend_ce_exception, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
843 	zend_declare_property_long(zend_ce_exception, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
844 	zend_declare_property_null(zend_ce_exception, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
845 	zend_declare_property_null(zend_ce_exception, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
846 	zend_declare_property_null(zend_ce_exception, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
847 	zend_declare_property_null(zend_ce_exception, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
848 
849 	INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
850 	zend_ce_error_exception = zend_register_internal_class_ex(&ce, zend_ce_exception);
851 	zend_ce_error_exception->create_object = zend_error_exception_new;
852 	zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
853 
854 	INIT_CLASS_ENTRY(ce, "Error", default_exception_functions);
855 	zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
856 	zend_ce_error->create_object = zend_default_exception_new;
857 	zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
858 
859 	zend_declare_property_string(zend_ce_error, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
860 	zend_declare_property_string(zend_ce_error, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
861 	zend_declare_property_long(zend_ce_error, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
862 	zend_declare_property_null(zend_ce_error, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
863 	zend_declare_property_null(zend_ce_error, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
864 	zend_declare_property_null(zend_ce_error, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
865 	zend_declare_property_null(zend_ce_error, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
866 
867 	INIT_CLASS_ENTRY(ce, "CompileError", NULL);
868 	zend_ce_compile_error = zend_register_internal_class_ex(&ce, zend_ce_error);
869 	zend_ce_compile_error->create_object = zend_default_exception_new;
870 
871 	INIT_CLASS_ENTRY(ce, "ParseError", NULL);
872 	zend_ce_parse_error = zend_register_internal_class_ex(&ce, zend_ce_compile_error);
873 	zend_ce_parse_error->create_object = zend_default_exception_new;
874 
875 	INIT_CLASS_ENTRY(ce, "TypeError", NULL);
876 	zend_ce_type_error = zend_register_internal_class_ex(&ce, zend_ce_error);
877 	zend_ce_type_error->create_object = zend_default_exception_new;
878 
879 	INIT_CLASS_ENTRY(ce, "ArgumentCountError", NULL);
880 	zend_ce_argument_count_error = zend_register_internal_class_ex(&ce, zend_ce_type_error);
881 	zend_ce_argument_count_error->create_object = zend_default_exception_new;
882 
883 	INIT_CLASS_ENTRY(ce, "ArithmeticError", NULL);
884 	zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_ce_error);
885 	zend_ce_arithmetic_error->create_object = zend_default_exception_new;
886 
887 	INIT_CLASS_ENTRY(ce, "DivisionByZeroError", NULL);
888 	zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error);
889 	zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
890 }
891 /* }}} */
892 
893 /* {{{ Deprecated - Use zend_ce_exception directly instead */
zend_exception_get_default(void)894 ZEND_API zend_class_entry *zend_exception_get_default(void)
895 {
896 	return zend_ce_exception;
897 }
898 /* }}} */
899 
900 /* {{{ Deprecated - Use zend_ce_error_exception directly instead */
zend_get_error_exception(void)901 ZEND_API zend_class_entry *zend_get_error_exception(void)
902 {
903 	return zend_ce_error_exception;
904 }
905 /* }}} */
906 
zend_throw_exception(zend_class_entry * exception_ce,const char * message,zend_long code)907 ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
908 {
909 	zval ex, tmp;
910 
911 	if (exception_ce) {
912 		if (!instanceof_function(exception_ce, zend_ce_throwable)) {
913 			zend_error(E_NOTICE, "Exceptions must implement Throwable");
914 			exception_ce = zend_ce_exception;
915 		}
916 	} else {
917 		exception_ce = zend_ce_exception;
918 	}
919 	object_init_ex(&ex, exception_ce);
920 
921 
922 	if (message) {
923 		ZVAL_STRING(&tmp, message);
924 		zend_update_property_ex(exception_ce, &ex, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
925 		zval_ptr_dtor(&tmp);
926 	}
927 	if (code) {
928 		ZVAL_LONG(&tmp, code);
929 		zend_update_property_ex(exception_ce, &ex, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
930 	}
931 
932 	zend_throw_exception_internal(&ex);
933 	return Z_OBJ(ex);
934 }
935 /* }}} */
936 
zend_throw_exception_ex(zend_class_entry * exception_ce,zend_long code,const char * format,...)937 ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
938 {
939 	va_list arg;
940 	char *message;
941 	zend_object *obj;
942 
943 	va_start(arg, format);
944 	zend_vspprintf(&message, 0, format, arg);
945 	va_end(arg);
946 	obj = zend_throw_exception(exception_ce, message, code);
947 	efree(message);
948 	return obj;
949 }
950 /* }}} */
951 
zend_throw_error_exception(zend_class_entry * exception_ce,const char * message,zend_long code,int severity)952 ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, const char *message, zend_long code, int severity) /* {{{ */
953 {
954 	zval ex, tmp;
955 	zend_object *obj = zend_throw_exception(exception_ce, message, code);
956 	ZVAL_OBJ(&ex, obj);
957 	ZVAL_LONG(&tmp, severity);
958 	zend_update_property_ex(zend_ce_error_exception, &ex, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
959 	return obj;
960 }
961 /* }}} */
962 
zend_error_va(int type,const char * file,uint32_t lineno,const char * format,...)963 static void zend_error_va(int type, const char *file, uint32_t lineno, const char *format, ...) /* {{{ */
964 {
965 	va_list args;
966 
967 	va_start(args, format);
968 	zend_error_cb(type, file, lineno, format, args);
969 	va_end(args);
970 }
971 /* }}} */
972 
zend_error_helper(int type,const char * filename,const uint32_t lineno,const char * format,...)973 static void zend_error_helper(int type, const char *filename, const uint32_t lineno, const char *format, ...) /* {{{ */
974 {
975 	va_list va;
976 
977 	va_start(va, format);
978 	zend_error_cb(type, filename, lineno, format, va);
979 	va_end(va);
980 }
981 /* }}} */
982 
983 /* This function doesn't return if it uses E_ERROR */
zend_exception_error(zend_object * ex,int severity)984 ZEND_API ZEND_COLD void zend_exception_error(zend_object *ex, int severity) /* {{{ */
985 {
986 	zval exception, rv;
987 	zend_class_entry *ce_exception;
988 
989 	ZVAL_OBJ(&exception, ex);
990 	ce_exception = ex->ce;
991 	EG(exception) = NULL;
992 	if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
993 		zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
994 		zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
995 		zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
996 
997 		zend_error_helper(ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR,
998 			ZSTR_VAL(file), line, "%s", ZSTR_VAL(message));
999 
1000 		zend_string_release_ex(file, 0);
1001 		zend_string_release_ex(message, 0);
1002 	} else if (instanceof_function(ce_exception, zend_ce_throwable)) {
1003 		zval tmp;
1004 		zend_string *str, *file = NULL;
1005 		zend_long line = 0;
1006 
1007 		zend_call_method_with_0_params(&exception, ce_exception, &ex->ce->__tostring, "__tostring", &tmp);
1008 		if (!EG(exception)) {
1009 			if (Z_TYPE(tmp) != IS_STRING) {
1010 				zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
1011 			} else {
1012 				zend_update_property_ex(i_get_exception_base(&exception), &exception, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
1013 			}
1014 		}
1015 		zval_ptr_dtor(&tmp);
1016 
1017 		if (EG(exception)) {
1018 			zval zv;
1019 
1020 			ZVAL_OBJ(&zv, EG(exception));
1021 			/* do the best we can to inform about the inner exception */
1022 			if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
1023 				file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
1024 				line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
1025 			}
1026 
1027 			zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
1028 				"Uncaught %s in exception handling during call to %s::__tostring()",
1029 				ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
1030 
1031 			if (file) {
1032 				zend_string_release_ex(file, 0);
1033 			}
1034 		}
1035 
1036 		str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
1037 		file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
1038 		line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
1039 
1040 		zend_error_va(severity, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
1041 			"Uncaught %s\n  thrown", ZSTR_VAL(str));
1042 
1043 		zend_string_release_ex(str, 0);
1044 		zend_string_release_ex(file, 0);
1045 	} else {
1046 		zend_error(severity, "Uncaught exception '%s'", ZSTR_VAL(ce_exception->name));
1047 	}
1048 
1049 	OBJ_RELEASE(ex);
1050 }
1051 /* }}} */
1052 
zend_throw_exception_object(zval * exception)1053 ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
1054 {
1055 	zend_class_entry *exception_ce;
1056 
1057 	if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
1058 		zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
1059 	}
1060 
1061 	exception_ce = Z_OBJCE_P(exception);
1062 
1063 	if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
1064 		zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1065 		zval_ptr_dtor(exception);
1066 		return;
1067 	}
1068 	zend_throw_exception_internal(exception);
1069 }
1070 /* }}} */
1071