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