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