Coverage Report

Created: 2026-10-28 06:20

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