Coverage Report

Created: 2026-07-25 06:39

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