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