Coverage Report

Created: 2026-06-02 06:40

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