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 #include "zend_exceptions_arginfo.h"
31 #include "zend_observer.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_compile_error;
38 ZEND_API zend_class_entry *zend_ce_parse_error;
39 ZEND_API zend_class_entry *zend_ce_type_error;
40 ZEND_API zend_class_entry *zend_ce_argument_count_error;
41 ZEND_API zend_class_entry *zend_ce_value_error;
42 ZEND_API zend_class_entry *zend_ce_arithmetic_error;
43 ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
44 ZEND_API zend_class_entry *zend_ce_unhandled_match_error;
45 ZEND_API zend_class_entry *zend_ce_request_parse_body_exception;
46
47 /* Internal pseudo-exception that is not exposed to userland. Throwing this exception *does not* execute finally blocks. */
48 static zend_class_entry zend_ce_unwind_exit;
49
50 /* Internal pseudo-exception that is not exposed to userland. Throwing this exception *does* execute finally blocks. */
51 static zend_class_entry zend_ce_graceful_exit;
52
53 ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
54
55 static zend_object_handlers default_exception_handlers;
56
57 /* {{{ zend_implement_throwable */
zend_implement_throwable(zend_class_entry * interface,zend_class_entry * class_type)58 static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
59 {
60 /* zend_ce_exception and zend_ce_error may not be initialized yet when this is called (e.g when
61 * implementing Throwable for Exception itself). Perform a manual inheritance check. */
62 zend_class_entry *root = class_type;
63 while (root->parent) {
64 root = root->parent;
65 }
66 if (zend_string_equals_literal(root->name, "Exception")
67 || zend_string_equals_literal(root->name, "Error")) {
68 return SUCCESS;
69 }
70
71 bool can_extend = (class_type->ce_flags & ZEND_ACC_ENUM) == 0;
72
73 zend_error_noreturn(E_ERROR,
74 can_extend
75 ? "%s %s cannot implement interface %s, extend Exception or Error instead"
76 : "%s %s cannot implement interface %s",
77 zend_get_object_type_uc(class_type),
78 ZSTR_VAL(class_type->name),
79 ZSTR_VAL(interface->name));
80 return FAILURE;
81 }
82 /* }}} */
83
i_get_exception_base(zend_object * object)84 static inline zend_class_entry *i_get_exception_base(zend_object *object) /* {{{ */
85 {
86 return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
87 }
88 /* }}} */
89
zend_get_exception_base(zend_object * object)90 ZEND_API zend_class_entry *zend_get_exception_base(zend_object *object) /* {{{ */
91 {
92 return i_get_exception_base(object);
93 }
94 /* }}} */
95
zend_exception_set_previous(zend_object * exception,zend_object * add_previous)96 void zend_exception_set_previous(zend_object *exception, zend_object *add_previous) /* {{{ */
97 {
98 zval *previous, *ancestor, *ex;
99 zval pv, zv, rv;
100 zend_class_entry *base_ce;
101
102 if (!exception || !add_previous) {
103 return;
104 }
105
106 if (exception == add_previous || zend_is_unwind_exit(add_previous) || zend_is_graceful_exit(add_previous)) {
107 OBJ_RELEASE(add_previous);
108 return;
109 }
110
111 ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
112 && "Previous exception must implement Throwable");
113
114 ZVAL_OBJ(&pv, add_previous);
115 ZVAL_OBJ(&zv, exception);
116 ex = &zv;
117 do {
118 ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
119 ZVAL_DEREF(ancestor);
120 while (Z_TYPE_P(ancestor) == IS_OBJECT) {
121 if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
122 OBJ_RELEASE(add_previous);
123 return;
124 }
125 ancestor = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(ancestor)), Z_OBJ_P(ancestor), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
126 ZVAL_DEREF(ancestor);
127 }
128 base_ce = i_get_exception_base(Z_OBJ_P(ex));
129 previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
130 ZVAL_DEREF(previous);
131 if (Z_TYPE_P(previous) == IS_NULL) {
132 zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
133 GC_DELREF(add_previous);
134 return;
135 }
136 ex = previous;
137 } while (Z_OBJ_P(ex) != add_previous);
138 }
139 /* }}} */
140
zend_exception_save(void)141 void zend_exception_save(void) /* {{{ */
142 {
143 if (EG(prev_exception)) {
144 zend_exception_set_previous(EG(exception), EG(prev_exception));
145 }
146 if (EG(exception)) {
147 EG(prev_exception) = EG(exception);
148 }
149 EG(exception) = NULL;
150 }
151 /* }}} */
152
zend_exception_restore(void)153 void zend_exception_restore(void) /* {{{ */
154 {
155 if (EG(prev_exception)) {
156 if (EG(exception)) {
157 zend_exception_set_previous(EG(exception), EG(prev_exception));
158 } else {
159 EG(exception) = EG(prev_exception);
160 }
161 EG(prev_exception) = NULL;
162 }
163 }
164 /* }}} */
165
is_handle_exception_set(void)166 static zend_always_inline bool is_handle_exception_set(void) {
167 zend_execute_data *execute_data = EG(current_execute_data);
168 return !execute_data
169 || !execute_data->func
170 || !ZEND_USER_CODE(execute_data->func->common.type)
171 || execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
172 }
173
zend_throw_exception_internal(zend_object * exception)174 ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
175 {
176 #ifdef HAVE_DTRACE
177 if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
178 if (exception != NULL) {
179 DTRACE_EXCEPTION_THROWN(ZSTR_VAL(exception->ce->name));
180 } else {
181 DTRACE_EXCEPTION_THROWN(NULL);
182 }
183 }
184 #endif /* HAVE_DTRACE */
185
186 if (exception != NULL) {
187 zend_object *previous = EG(exception);
188 if (previous && zend_is_unwind_exit(previous)) {
189 /* Don't replace unwinding exception with different exception. */
190 OBJ_RELEASE(exception);
191 return;
192 }
193
194 zend_exception_set_previous(exception, EG(exception));
195 EG(exception) = exception;
196 if (previous) {
197 ZEND_ASSERT(is_handle_exception_set() && "HANDLE_EXCEPTION not set?");
198 return;
199 }
200 }
201 if (!EG(current_execute_data)) {
202 if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
203 return;
204 }
205 if (EG(exception)) {
206 if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF
207 && !zend_is_unwind_exit(EG(exception))
208 && !zend_is_graceful_exit(EG(exception))) {
209 zend_user_exception_handler();
210 if (EG(exception)) {
211 zend_exception_error(EG(exception), E_ERROR);
212 }
213 return;
214 } else {
215 zend_exception_error(EG(exception), E_ERROR);
216 }
217 zend_bailout();
218 }
219 zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
220 }
221
222 if (zend_throw_exception_hook) {
223 zend_throw_exception_hook(exception);
224 }
225
226 if (is_handle_exception_set()) {
227 /* no need to rethrow the exception */
228 return;
229 }
230 EG(opline_before_exception) = EG(current_execute_data)->opline;
231 EG(current_execute_data)->opline = EG(exception_op);
232 }
233 /* }}} */
234
zend_clear_exception(void)235 ZEND_API void zend_clear_exception(void) /* {{{ */
236 {
237 zend_object *exception;
238 if (EG(prev_exception)) {
239 OBJ_RELEASE(EG(prev_exception));
240 EG(prev_exception) = NULL;
241 }
242 if (!EG(exception)) {
243 return;
244 }
245 /* exception may have destructor */
246 exception = EG(exception);
247 EG(exception) = NULL;
248 OBJ_RELEASE(exception);
249 if (EG(current_execute_data)) {
250 EG(current_execute_data)->opline = EG(opline_before_exception);
251 }
252 #if ZEND_DEBUG
253 EG(opline_before_exception) = NULL;
254 #endif
255 }
256 /* }}} */
257
zend_default_exception_new(zend_class_entry * class_type)258 static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
259 {
260 zval tmp;
261 zval trace;
262 zend_class_entry *base_ce;
263 zend_string *filename;
264
265 zend_object *object = zend_objects_new(class_type);
266 object_properties_init(object, class_type);
267
268 if (EG(current_execute_data)) {
269 zend_fetch_debug_backtrace(&trace,
270 0,
271 EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
272 } else {
273 array_init(&trace);
274 }
275 Z_SET_REFCOUNT(trace, 0);
276
277 base_ce = i_get_exception_base(object);
278
279 if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
280 || !(filename = zend_get_compiled_filename()))) {
281 ZVAL_STRING(&tmp, zend_get_executed_filename());
282 zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
283 zval_ptr_dtor(&tmp);
284 ZVAL_LONG(&tmp, zend_get_executed_lineno());
285 zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
286 } else {
287 ZVAL_STR(&tmp, filename);
288 zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
289 ZVAL_LONG(&tmp, zend_get_compiled_lineno());
290 zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
291 }
292 zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
293
294 return object;
295 }
296 /* }}} */
297
298 /* {{{ Clone the exception object */
ZEND_METHOD(Exception,__clone)299 ZEND_COLD ZEND_METHOD(Exception, __clone)
300 {
301 /* __clone() is private but this is reachable with reflection */
302 zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
303 }
304 /* }}} */
305
306 /* {{{ Exception constructor */
ZEND_METHOD(Exception,__construct)307 ZEND_METHOD(Exception, __construct)
308 {
309 zend_string *message = NULL;
310 zend_long code = 0;
311 zval tmp, *object, *previous = NULL;
312 zend_class_entry *base_ce;
313
314 object = ZEND_THIS;
315 base_ce = i_get_exception_base(Z_OBJ_P(object));
316
317 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
318 RETURN_THROWS();
319 }
320
321 if (message) {
322 ZVAL_STR(&tmp, message);
323 zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
324 }
325
326 if (code) {
327 ZVAL_LONG(&tmp, code);
328 zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
329 }
330
331 if (previous) {
332 zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
333 }
334 }
335 /* }}} */
336
337 /* {{{ Exception unserialize checks */
338 #define CHECK_EXC_TYPE(id, type) \
339 pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
340 if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
341 zend_unset_property(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_VAL(ZSTR_KNOWN(id)), ZSTR_LEN(ZSTR_KNOWN(id))); \
342 }
343
ZEND_METHOD(Exception,__wakeup)344 ZEND_METHOD(Exception, __wakeup)
345 {
346 ZEND_PARSE_PARAMETERS_NONE();
347
348 zval value, *pvalue;
349 zval *object = ZEND_THIS;
350 CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
351 CHECK_EXC_TYPE(ZEND_STR_CODE, IS_LONG);
352 /* The type of all other properties is enforced through typed properties. */
353 }
354 /* }}} */
355
356 /* {{{ ErrorException constructor */
ZEND_METHOD(ErrorException,__construct)357 ZEND_METHOD(ErrorException, __construct)
358 {
359 zend_string *message = NULL, *filename = NULL;
360 zend_long code = 0, severity = E_ERROR, lineno;
361 bool lineno_is_null = 1;
362 zval tmp, *object, *previous = NULL;
363
364 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
365 RETURN_THROWS();
366 }
367
368 object = ZEND_THIS;
369
370 if (message) {
371 ZVAL_STR_COPY(&tmp, message);
372 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
373 zval_ptr_dtor(&tmp);
374 }
375
376 if (code) {
377 ZVAL_LONG(&tmp, code);
378 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
379 }
380
381 if (previous) {
382 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
383 }
384
385 ZVAL_LONG(&tmp, severity);
386 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
387
388 if (filename) {
389 ZVAL_STR_COPY(&tmp, filename);
390 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
391 zval_ptr_dtor(&tmp);
392 }
393
394 if (!lineno_is_null) {
395 ZVAL_LONG(&tmp, lineno);
396 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
397 } else if (filename) {
398 ZVAL_LONG(&tmp, 0);
399 zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
400 }
401 }
402 /* }}} */
403
404 #define GET_PROPERTY(object, id) \
405 zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 0, &rv)
406 #define GET_PROPERTY_SILENT(object, id) \
407 zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &rv)
408
409 /* {{{ Get the file in which the exception occurred */
ZEND_METHOD(Exception,getFile)410 ZEND_METHOD(Exception, getFile)
411 {
412 zval *prop, rv;
413
414 ZEND_PARSE_PARAMETERS_NONE();
415
416 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
417 RETURN_STR(zval_get_string(prop));
418 }
419 /* }}} */
420
421 /* {{{ Get the line in which the exception occurred */
ZEND_METHOD(Exception,getLine)422 ZEND_METHOD(Exception, getLine)
423 {
424 zval *prop, rv;
425
426 ZEND_PARSE_PARAMETERS_NONE();
427
428 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
429 RETURN_LONG(zval_get_long(prop));
430 }
431 /* }}} */
432
433 /* {{{ Get the exception message */
ZEND_METHOD(Exception,getMessage)434 ZEND_METHOD(Exception, getMessage)
435 {
436 zval *prop, rv;
437
438 ZEND_PARSE_PARAMETERS_NONE();
439
440 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
441 RETURN_STR(zval_get_string(prop));
442 }
443 /* }}} */
444
445 /* {{{ Get the exception code */
ZEND_METHOD(Exception,getCode)446 ZEND_METHOD(Exception, getCode)
447 {
448 zval *prop, rv;
449
450 ZEND_PARSE_PARAMETERS_NONE();
451
452 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
453 ZVAL_DEREF(prop);
454 ZVAL_COPY(return_value, prop);
455 }
456 /* }}} */
457
458 /* {{{ Get the stack trace for the location in which the exception occurred */
ZEND_METHOD(Exception,getTrace)459 ZEND_METHOD(Exception, getTrace)
460 {
461 zval *prop, rv;
462
463 ZEND_PARSE_PARAMETERS_NONE();
464
465 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
466 ZVAL_DEREF(prop);
467 ZVAL_COPY(return_value, prop);
468 }
469 /* }}} */
470
471 /* {{{ Get the exception severity */
ZEND_METHOD(ErrorException,getSeverity)472 ZEND_METHOD(ErrorException, getSeverity)
473 {
474 zval *prop, rv;
475
476 ZEND_PARSE_PARAMETERS_NONE();
477
478 prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_SEVERITY);
479 ZVAL_DEREF(prop);
480 ZVAL_COPY(return_value, prop);
481 }
482 /* }}} */
483
484 #define TRACE_APPEND_KEY(key) do { \
485 tmp = zend_hash_find(ht, key); \
486 if (tmp) { \
487 if (Z_TYPE_P(tmp) != IS_STRING) { \
488 zend_error(E_WARNING, "Value for %s is not a string", \
489 ZSTR_VAL(key)); \
490 smart_str_appends(str, "[unknown]"); \
491 } else { \
492 smart_str_appends(str, Z_STRVAL_P(tmp)); \
493 } \
494 } \
495 } while (0)
496
_build_trace_args(zval * arg,smart_str * str)497 static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
498 {
499 /* the trivial way would be to do
500 * convert_to_string(arg);
501 * append it and kill the now tmp arg.
502 * but that could cause some E_NOTICE and also damn long lines.
503 */
504
505 ZVAL_DEREF(arg);
506
507 if (smart_str_append_zval(str, arg, EG(exception_string_param_max_len)) == SUCCESS) {
508 smart_str_appends(str, ", ");
509 } else {
510 switch (Z_TYPE_P(arg)) {
511 case IS_RESOURCE:
512 smart_str_appends(str, "Resource id #");
513 smart_str_append_long(str, Z_RES_HANDLE_P(arg));
514 smart_str_appends(str, ", ");
515 break;
516 case IS_ARRAY:
517 smart_str_appends(str, "Array, ");
518 break;
519 case IS_OBJECT: {
520 zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
521 smart_str_appends(str, "Object(");
522 smart_str_appends(str, ZSTR_VAL(class_name));
523 smart_str_appends(str, "), ");
524 zend_string_release_ex(class_name, 0);
525 break;
526 }
527 }
528 }
529 }
530 /* }}} */
531
_build_trace_string(smart_str * str,const HashTable * ht,uint32_t num)532 static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
533 {
534 zval *file, *tmp;
535
536 smart_str_appendc(str, '#');
537 smart_str_append_long(str, num);
538 smart_str_appendc(str, ' ');
539
540 file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
541 if (file) {
542 if (UNEXPECTED(Z_TYPE_P(file) != IS_STRING)) {
543 zend_error(E_WARNING, "File name is not a string");
544 smart_str_appends(str, "[unknown file]: ");
545 } else{
546 zend_long line = 0;
547 tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
548 if (tmp) {
549 if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
550 line = Z_LVAL_P(tmp);
551 } else {
552 zend_error(E_WARNING, "Line is not an int");
553 }
554 }
555 smart_str_append(str, Z_STR_P(file));
556 smart_str_appendc(str, '(');
557 smart_str_append_long(str, line);
558 smart_str_appends(str, "): ");
559 }
560 } else {
561 smart_str_appends(str, "[internal function]: ");
562 }
563 TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
564 TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
565 TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
566 smart_str_appendc(str, '(');
567 tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
568 if (tmp) {
569 if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
570 size_t last_len = ZSTR_LEN(str->s);
571 zend_string *name;
572 zval *arg;
573
574 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
575 if (name) {
576 smart_str_append(str, name);
577 smart_str_appends(str, ": ");
578 }
579 _build_trace_args(arg, str);
580 } ZEND_HASH_FOREACH_END();
581
582 if (last_len != ZSTR_LEN(str->s)) {
583 ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
584 }
585 } else {
586 zend_error(E_WARNING, "args element is not an array");
587 }
588 }
589 smart_str_appends(str, ")\n");
590 }
591 /* }}} */
592
zend_trace_to_string(const HashTable * trace,bool include_main)593 ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
594 zend_ulong index;
595 zval *frame;
596 uint32_t num = 0;
597 smart_str str = {0};
598
599 ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
600 if (UNEXPECTED(Z_TYPE_P(frame) != IS_ARRAY)) {
601 zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
602 continue;
603 }
604
605 _build_trace_string(&str, Z_ARRVAL_P(frame), num++);
606 } ZEND_HASH_FOREACH_END();
607
608 if (include_main) {
609 smart_str_appendc(&str, '#');
610 smart_str_append_long(&str, num);
611 smart_str_appends(&str, " {main}");
612 }
613
614 smart_str_0(&str);
615 return str.s ? str.s : ZSTR_EMPTY_ALLOC();
616 }
617
618 /* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
ZEND_METHOD(Exception,getTraceAsString)619 ZEND_METHOD(Exception, getTraceAsString)
620 {
621
622 ZEND_PARSE_PARAMETERS_NONE();
623
624 zval *object = ZEND_THIS;
625 zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
626 zval rv;
627 const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
628 if (EG(exception)) {
629 RETURN_THROWS();
630 }
631
632 ZVAL_DEREF(trace);
633 /* Type should be guaranteed by property type. */
634 ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
635 RETURN_NEW_STR(zend_trace_to_string(Z_ARRVAL_P(trace), /* include_main */ true));
636 }
637 /* }}} */
638
639 /* {{{ Return previous Throwable or NULL. */
ZEND_METHOD(Exception,getPrevious)640 ZEND_METHOD(Exception, getPrevious)
641 {
642 zval rv;
643
644 ZEND_PARSE_PARAMETERS_NONE();
645
646 ZVAL_COPY_DEREF(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
647 } /* }}} */
648
649 /* {{{ Obtain the string representation of the Exception object */
ZEND_METHOD(Exception,__toString)650 ZEND_METHOD(Exception, __toString)
651 {
652 zval trace, *exception;
653 zend_class_entry *base_ce;
654 zend_string *str;
655 zend_fcall_info fci;
656 zval rv, tmp;
657 zend_string *fname;
658
659 ZEND_PARSE_PARAMETERS_NONE();
660
661 str = ZSTR_EMPTY_ALLOC();
662
663 exception = ZEND_THIS;
664 fname = ZSTR_INIT_LITERAL("gettraceasstring", 0);
665
666 while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
667 zend_string *prev_str = str;
668 zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
669 zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
670 zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
671
672 fci.size = sizeof(fci);
673 ZVAL_STR(&fci.function_name, fname);
674 fci.object = Z_OBJ_P(exception);
675 fci.retval = &trace;
676 fci.param_count = 0;
677 fci.params = NULL;
678 fci.named_params = NULL;
679
680 zend_call_function(&fci, NULL);
681
682 if (Z_TYPE(trace) != IS_STRING) {
683 zval_ptr_dtor(&trace);
684 ZVAL_UNDEF(&trace);
685 }
686
687 if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
688 zend_string *real_message = zend_strpprintf_unchecked(0, "%S and defined", message);
689 zend_string_release_ex(message, 0);
690 message = real_message;
691 }
692
693 zend_string *tmp_trace = (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace))
694 ? zend_string_copy(Z_STR(trace))
695 : ZSTR_INIT_LITERAL("#0 {main}\n", false);
696
697 zend_string *name = Z_OBJCE_P(exception)->name;
698
699 if (ZSTR_LEN(message) > 0) {
700 zval message_zv;
701 ZVAL_STR(&message_zv, message);
702
703 str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
704 name, message, file, line,
705 tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
706 } else {
707 str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
708 name, file, line,
709 tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
710 }
711 zend_string_release_ex(tmp_trace, false);
712
713 zend_string_release_ex(prev_str, 0);
714 zend_string_release_ex(message, 0);
715 zend_string_release_ex(file, 0);
716 zval_ptr_dtor(&trace);
717
718 Z_PROTECT_RECURSION_P(exception);
719 exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
720 ZVAL_DEREF(exception);
721 if (Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
722 break;
723 }
724 }
725 zend_string_release_ex(fname, 0);
726
727 exception = ZEND_THIS;
728 /* Reset apply counts */
729 while (Z_TYPE_P(exception) == IS_OBJECT && (base_ce = i_get_exception_base(Z_OBJ_P(exception))) && instanceof_function(Z_OBJCE_P(exception), base_ce)) {
730 if (Z_IS_RECURSIVE_P(exception)) {
731 Z_UNPROTECT_RECURSION_P(exception);
732 } else {
733 break;
734 }
735 exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
736 ZVAL_DEREF(exception);
737 }
738
739 exception = ZEND_THIS;
740 base_ce = i_get_exception_base(Z_OBJ_P(exception));
741
742 /* We store the result in the private property string so we can access
743 * the result in uncaught exception handlers without memleaks. */
744 ZVAL_STR(&tmp, str);
745 zend_update_property_ex(base_ce, Z_OBJ_P(exception), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
746
747 RETURN_STR(str);
748 }
749 /* }}} */
750
zend_init_exception_class_entry(zend_class_entry * ce)751 static void zend_init_exception_class_entry(zend_class_entry *ce) {
752 ce->create_object = zend_default_exception_new;
753 ce->default_object_handlers = &default_exception_handlers;
754 }
755
zend_register_default_exception(void)756 void zend_register_default_exception(void) /* {{{ */
757 {
758 zend_ce_throwable = register_class_Throwable(zend_ce_stringable);
759 zend_ce_throwable->interface_gets_implemented = zend_implement_throwable;
760
761 memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
762 default_exception_handlers.clone_obj = NULL;
763
764 zend_ce_exception = register_class_Exception(zend_ce_throwable);
765 zend_init_exception_class_entry(zend_ce_exception);
766
767 zend_ce_error_exception = register_class_ErrorException(zend_ce_exception);
768 zend_init_exception_class_entry(zend_ce_error_exception);
769
770 zend_ce_error = register_class_Error(zend_ce_throwable);
771 zend_init_exception_class_entry(zend_ce_error);
772
773 zend_ce_compile_error = register_class_CompileError(zend_ce_error);
774 zend_init_exception_class_entry(zend_ce_compile_error);
775
776 zend_ce_parse_error = register_class_ParseError(zend_ce_compile_error);
777 zend_init_exception_class_entry(zend_ce_parse_error);
778
779 zend_ce_type_error = register_class_TypeError(zend_ce_error);
780 zend_init_exception_class_entry(zend_ce_type_error);
781
782 zend_ce_argument_count_error = register_class_ArgumentCountError(zend_ce_type_error);
783 zend_init_exception_class_entry(zend_ce_argument_count_error);
784
785 zend_ce_value_error = register_class_ValueError(zend_ce_error);
786 zend_init_exception_class_entry(zend_ce_value_error);
787
788 zend_ce_arithmetic_error = register_class_ArithmeticError(zend_ce_error);
789 zend_init_exception_class_entry(zend_ce_arithmetic_error);
790
791 zend_ce_division_by_zero_error = register_class_DivisionByZeroError(zend_ce_arithmetic_error);
792 zend_init_exception_class_entry(zend_ce_division_by_zero_error);
793
794 zend_ce_unhandled_match_error = register_class_UnhandledMatchError(zend_ce_error);
795 zend_init_exception_class_entry(zend_ce_unhandled_match_error);
796
797 zend_ce_request_parse_body_exception = register_class_RequestParseBodyException(zend_ce_exception);
798 zend_init_exception_class_entry(zend_ce_request_parse_body_exception);
799
800 INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
801
802 INIT_CLASS_ENTRY(zend_ce_graceful_exit, "GracefulExit", NULL);
803 }
804 /* }}} */
805
806 /* {{{ Deprecated - Use zend_ce_exception directly instead */
zend_exception_get_default(void)807 ZEND_API zend_class_entry *zend_exception_get_default(void)
808 {
809 return zend_ce_exception;
810 }
811 /* }}} */
812
813 /* {{{ Deprecated - Use zend_ce_error_exception directly instead */
zend_get_error_exception(void)814 ZEND_API zend_class_entry *zend_get_error_exception(void)
815 {
816 return zend_ce_error_exception;
817 }
818 /* }}} */
819
zend_throw_exception_zstr(zend_class_entry * exception_ce,zend_string * message,zend_long code)820 static zend_object *zend_throw_exception_zstr(zend_class_entry *exception_ce, zend_string *message, zend_long code) /* {{{ */
821 {
822 zval ex, tmp;
823
824 if (!exception_ce) {
825 exception_ce = zend_ce_exception;
826 }
827
828 ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
829 && "Exceptions must implement Throwable");
830
831 object_init_ex(&ex, exception_ce);
832
833 if (message) {
834 ZVAL_STR(&tmp, message);
835 zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
836 }
837 if (code) {
838 ZVAL_LONG(&tmp, code);
839 zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
840 }
841
842 zend_throw_exception_internal(Z_OBJ(ex));
843
844 return Z_OBJ(ex);
845 }
846 /* }}} */
847
zend_throw_exception(zend_class_entry * exception_ce,const char * message,zend_long code)848 ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
849 {
850 zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
851 zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
852 if (msg_str) {
853 zend_string_release(msg_str);
854 }
855 return ex;
856 }
857 /* }}} */
858
zend_throw_exception_ex(zend_class_entry * exception_ce,zend_long code,const char * format,...)859 ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
860 {
861 va_list arg;
862 zend_object *obj;
863
864 va_start(arg, format);
865 zend_string *msg_str = zend_vstrpprintf(0, format, arg);
866 va_end(arg);
867 obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
868 zend_string_release(msg_str);
869 return obj;
870 }
871 /* }}} */
872
zend_throw_error_exception(zend_class_entry * exception_ce,zend_string * message,zend_long code,int severity)873 ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, zend_string *message, zend_long code, int severity) /* {{{ */
874 {
875 zend_object *obj = zend_throw_exception_zstr(exception_ce, message, code);
876 if (exception_ce && instanceof_function(exception_ce, zend_ce_error_exception)) {
877 zval tmp;
878 ZVAL_LONG(&tmp, severity);
879 zend_update_property_ex(zend_ce_error_exception, obj, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
880 }
881 return obj;
882 }
883 /* }}} */
884
zend_error_va(int type,zend_string * file,uint32_t lineno,const char * format,...)885 static void zend_error_va(int type, zend_string *file, uint32_t lineno, const char *format, ...) /* {{{ */
886 {
887 va_list args;
888 va_start(args, format);
889 zend_string *message = zend_vstrpprintf(0, format, args);
890 zend_observer_error_notify(type, file, lineno, message);
891 zend_error_cb(type, file, lineno, message);
892 zend_string_release(message);
893 va_end(args);
894 }
895 /* }}} */
896
897 /* This function doesn't return if it uses E_ERROR */
zend_exception_error(zend_object * ex,int severity)898 ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severity) /* {{{ */
899 {
900 zval exception, rv;
901 zend_class_entry *ce_exception;
902 zend_result result = FAILURE;
903
904 ZVAL_OBJ(&exception, ex);
905 ce_exception = ex->ce;
906 EG(exception) = NULL;
907 if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
908 zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
909 zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
910 zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
911 int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;
912
913 zend_observer_error_notify(type, file, line, message);
914 zend_error_cb(type, file, line, message);
915
916 zend_string_release_ex(file, 0);
917 zend_string_release_ex(message, 0);
918 } else if (instanceof_function(ce_exception, zend_ce_throwable)) {
919 zval tmp;
920 zend_string *str, *file = NULL;
921 zend_long line = 0;
922
923 zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
924 if (!EG(exception)) {
925 if (Z_TYPE(tmp) != IS_STRING) {
926 zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
927 } else {
928 zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
929 }
930 }
931 zval_ptr_dtor(&tmp);
932
933 if (EG(exception)) {
934 zval zv;
935
936 ZVAL_OBJ(&zv, EG(exception));
937 /* do the best we can to inform about the inner exception */
938 if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
939 file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
940 line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
941 }
942
943 zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
944 "Uncaught %s in exception handling during call to %s::__toString()",
945 ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
946
947 if (file) {
948 zend_string_release_ex(file, 0);
949 }
950 }
951
952 str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
953 file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
954 line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
955
956 zend_error_va(severity | E_DONT_BAIL,
957 (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
958 "Uncaught %S\n thrown", str);
959
960 zend_string_release_ex(str, 0);
961 zend_string_release_ex(file, 0);
962 } else if (ce_exception == &zend_ce_unwind_exit || ce_exception == &zend_ce_graceful_exit) {
963 /* We successfully unwound, nothing more to do.
964 * We still return FAILURE in this case, as further execution should still be aborted. */
965 } else {
966 zend_error(severity, "Uncaught exception %s", ZSTR_VAL(ce_exception->name));
967 }
968
969 OBJ_RELEASE(ex);
970 return result;
971 }
972 /* }}} */
973
zend_exception_uncaught_error(const char * format,...)974 ZEND_NORETURN void zend_exception_uncaught_error(const char *format, ...) {
975 va_list va;
976 va_start(va, format);
977 zend_string *prefix = zend_vstrpprintf(0, format, va);
978 va_end(va);
979
980 ZEND_ASSERT(EG(exception));
981 zval exception_zv;
982 ZVAL_OBJ_COPY(&exception_zv, EG(exception));
983 zend_clear_exception();
984
985 zend_string *exception_str = zval_get_string(&exception_zv);
986 zend_error_noreturn(E_ERROR,
987 "%s: Uncaught %s", ZSTR_VAL(prefix), ZSTR_VAL(exception_str));
988 }
989
zend_throw_exception_object(zval * exception)990 ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
991 {
992 if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
993 zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
994 }
995
996 zend_class_entry *exception_ce = Z_OBJCE_P(exception);
997
998 if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
999 zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1000 zval_ptr_dtor(exception);
1001 return;
1002 }
1003
1004 zend_throw_exception_internal(Z_OBJ_P(exception));
1005 }
1006 /* }}} */
1007
zend_create_unwind_exit(void)1008 ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void)
1009 {
1010 return zend_objects_new(&zend_ce_unwind_exit);
1011 }
1012
zend_create_graceful_exit(void)1013 ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void)
1014 {
1015 return zend_objects_new(&zend_ce_graceful_exit);
1016 }
1017
zend_throw_unwind_exit(void)1018 ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
1019 {
1020 ZEND_ASSERT(!EG(exception));
1021 EG(exception) = zend_create_unwind_exit();
1022 EG(opline_before_exception) = EG(current_execute_data)->opline;
1023 EG(current_execute_data)->opline = EG(exception_op);
1024 }
1025
zend_throw_graceful_exit(void)1026 ZEND_API ZEND_COLD void zend_throw_graceful_exit(void)
1027 {
1028 ZEND_ASSERT(!EG(exception));
1029 EG(exception) = zend_create_graceful_exit();
1030 EG(opline_before_exception) = EG(current_execute_data)->opline;
1031 EG(current_execute_data)->opline = EG(exception_op);
1032 }
1033
zend_is_unwind_exit(const zend_object * ex)1034 ZEND_API bool zend_is_unwind_exit(const zend_object *ex)
1035 {
1036 return ex->ce == &zend_ce_unwind_exit;
1037 }
1038
zend_is_graceful_exit(const zend_object * ex)1039 ZEND_API bool zend_is_graceful_exit(const zend_object *ex)
1040 {
1041 return ex->ce == &zend_ce_graceful_exit;
1042 }
1043