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