Coverage Report

Created: 2025-09-27 06:26

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