Coverage Report

Created: 2025-12-14 06:09

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