Coverage Report

Created: 2025-12-31 07:28

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
559
#define ZEND_EXCEPTION_MESSAGE_OFF 0
34
2
#define ZEND_EXCEPTION_CODE_OFF 2
35
562k
#define ZEND_EXCEPTION_FILE_OFF 3
36
562k
#define ZEND_EXCEPTION_LINE_OFF 4
37
562k
#define ZEND_EXCEPTION_TRACE_OFF 5
38
2
#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
771
{
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
771
  const zend_class_entry *root = class_type;
70
1.90k
  while (root->parent) {
71
1.13k
    root = root->parent;
72
1.13k
  }
73
771
  if (zend_string_equals_literal(root->name, "Exception")
74
769
      || zend_string_equals_literal(root->name, "Error")) {
75
769
    return SUCCESS;
76
769
  }
77
78
771
  bool can_extend = (class_type->ce_flags & ZEND_ACC_ENUM) == 0;
79
80
2
  zend_error_noreturn(E_ERROR,
81
2
    can_extend
82
2
      ? "%s %s cannot implement interface %s, extend Exception or Error instead"
83
2
      : "%s %s cannot implement interface %s",
84
2
    zend_get_object_type_uc(class_type),
85
2
    ZSTR_VAL(class_type->name),
86
2
    ZSTR_VAL(interface->name));
87
0
  return FAILURE;
88
771
}
89
/* }}} */
90
91
static inline zend_class_entry *i_get_exception_base(const zend_object *object) /* {{{ */
92
2.56M
{
93
2.56M
  return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
94
2.56M
}
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
460k
{
105
460k
  zval *previous, *ancestor, *ex;
106
460k
  zval  pv, zv, rv;
107
460k
  zend_class_entry *base_ce;
108
109
460k
  if (!exception || !add_previous) {
110
451k
    return;
111
451k
  }
112
113
9.75k
  if (exception == add_previous || zend_is_unwind_exit(add_previous) || zend_is_graceful_exit(add_previous)) {
114
4
    OBJ_RELEASE(add_previous);
115
4
    return;
116
4
  }
117
118
9.75k
  ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
119
9.75k
    && "Previous exception must implement Throwable");
120
121
9.75k
  ZVAL_OBJ(&pv, add_previous);
122
9.75k
  ZVAL_OBJ(&zv, exception);
123
9.75k
  ex = &zv;
124
9.82k
  do {
125
9.82k
    ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
126
9.82k
    ZVAL_DEREF(ancestor);
127
234k
    while (Z_TYPE_P(ancestor) == IS_OBJECT) {
128
225k
      if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
129
1
        OBJ_RELEASE(add_previous);
130
1
        return;
131
1
      }
132
225k
      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
225k
      ZVAL_DEREF(ancestor);
134
225k
    }
135
9.81k
    base_ce = i_get_exception_base(Z_OBJ_P(ex));
136
9.81k
    previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
137
9.81k
    ZVAL_DEREF(previous);
138
9.81k
    if (Z_TYPE_P(previous) == IS_NULL) {
139
9.75k
      zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
140
9.75k
      GC_DELREF(add_previous);
141
9.75k
      return;
142
9.75k
    }
143
66
    ex = previous;
144
66
  } while (Z_OBJ_P(ex) != add_previous);
145
9.75k
}
146
/* }}} */
147
148
void zend_exception_save(void) /* {{{ */
149
348k
{
150
348k
  if (EG(prev_exception)) {
151
1
    zend_exception_set_previous(EG(exception), EG(prev_exception));
152
1
  }
153
348k
  if (EG(exception)) {
154
3.96k
    EG(prev_exception) = EG(exception);
155
3.96k
  }
156
348k
  EG(exception) = NULL;
157
348k
}
158
/* }}} */
159
160
void zend_exception_restore(void) /* {{{ */
161
571k
{
162
571k
  if (EG(prev_exception)) {
163
3.72k
    if (EG(exception)) {
164
1
      zend_exception_set_previous(EG(exception), EG(prev_exception));
165
3.72k
    } else {
166
3.72k
      EG(exception) = EG(prev_exception);
167
3.72k
    }
168
3.72k
    EG(prev_exception) = NULL;
169
3.72k
  }
170
571k
}
171
/* }}} */
172
173
290k
static zend_always_inline bool is_handle_exception_set(void) {
174
290k
  zend_execute_data *execute_data = EG(current_execute_data);
175
290k
  return !execute_data
176
290k
    || !execute_data->func
177
290k
    || !ZEND_USER_CODE(execute_data->func->common.type)
178
7.14k
    || execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
179
290k
}
180
181
ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
182
461k
{
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
461k
  if (exception != NULL) {
194
460k
    const zend_object *previous = EG(exception);
195
460k
    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
460k
    zend_exception_set_previous(exception, EG(exception));
202
460k
    EG(exception) = exception;
203
460k
    if (previous) {
204
9.71k
      return;
205
9.71k
    }
206
460k
  }
207
451k
  if (!EG(current_execute_data)) {
208
160k
    if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
209
160k
      return;
210
160k
    }
211
684
    if (EG(exception)) {
212
684
      if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF
213
1
       && !zend_is_unwind_exit(EG(exception))
214
1
       && !zend_is_graceful_exit(EG(exception))) {
215
1
        zend_user_exception_handler();
216
1
        if (EG(exception)) {
217
0
          zend_exception_error(EG(exception), E_ERROR);
218
0
        }
219
1
        return;
220
683
      } else {
221
683
        zend_exception_error(EG(exception), E_ERROR);
222
683
      }
223
684
      zend_bailout();
224
684
    }
225
0
    zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
226
684
  }
227
228
290k
  if (zend_throw_exception_hook) {
229
0
    zend_throw_exception_hook(exception);
230
0
  }
231
232
290k
  if (is_handle_exception_set()) {
233
    /* no need to rethrow the exception */
234
283k
    return;
235
283k
  }
236
7.14k
  EG(opline_before_exception) = EG(current_execute_data)->opline;
237
7.14k
  EG(current_execute_data)->opline = EG(exception_op);
238
7.14k
}
239
/* }}} */
240
241
ZEND_API void zend_clear_exception(void) /* {{{ */
242
144k
{
243
144k
  zend_object *exception;
244
144k
  if (EG(prev_exception)) {
245
238
    OBJ_RELEASE(EG(prev_exception));
246
238
    EG(prev_exception) = NULL;
247
238
  }
248
144k
  if (!EG(exception)) {
249
1.40k
    return;
250
1.40k
  }
251
  /* exception may have destructor */
252
142k
  exception = EG(exception);
253
142k
  EG(exception) = NULL;
254
142k
  OBJ_RELEASE(exception);
255
142k
  if (EG(current_execute_data)) {
256
12.4k
    EG(current_execute_data)->opline = EG(opline_before_exception);
257
12.4k
  }
258
142k
#if ZEND_DEBUG
259
142k
  EG(opline_before_exception) = NULL;
260
142k
#endif
261
142k
}
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
1.68M
{
269
1.68M
  if (UNEXPECTED(object->ce->num_hooked_props > 0)) {
270
    /* Property may have been overridden with a hook. */
271
0
    zend_update_property_ex(scope != NULL ? scope : object->ce, object, member, value);
272
0
    zval_ptr_dtor(value);
273
0
    return;
274
0
  }
275
1.68M
#if ZEND_DEBUG
276
1.68M
  const zend_class_entry *old_scope = EG(fake_scope);
277
1.68M
  EG(fake_scope) = i_get_exception_base(object);
278
1.68M
  const zend_property_info *prop_info = zend_get_property_info(object->ce, member, true);
279
1.68M
  ZEND_ASSERT(OBJ_PROP_TO_NUM(prop_info->offset) == prop_num);
280
1.68M
  EG(fake_scope) = old_scope;
281
1.68M
#endif
282
1.68M
  zval *zv = OBJ_PROP_NUM(object, prop_num);
283
1.68M
  zval_ptr_safe_dtor(zv);
284
1.68M
  ZVAL_COPY_VALUE(zv, value);
285
1.68M
}
286
287
static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
288
562k
{
289
562k
  zval tmp;
290
562k
  zval trace;
291
562k
  zend_string *filename;
292
293
562k
  zend_object *object = zend_objects_new(class_type);
294
562k
  object_properties_init(object, class_type);
295
296
562k
  if (EG(current_execute_data)) {
297
402k
    zend_fetch_debug_backtrace(&trace,
298
402k
      0,
299
402k
      EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
300
402k
  } else {
301
160k
    ZVAL_EMPTY_ARRAY(&trace);
302
160k
  }
303
304
562k
  zend_update_property_num_checked(i_get_exception_base(object), object, ZEND_EXCEPTION_TRACE_OFF, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
305
306
562k
  if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
307
562k
      || !(filename = zend_get_compiled_filename()))) {
308
520k
    ZVAL_STRING(&tmp, zend_get_executed_filename());
309
520k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
310
520k
    ZVAL_LONG(&tmp, zend_get_executed_lineno());
311
520k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
312
520k
  } else {
313
41.9k
    ZVAL_STR_COPY(&tmp, filename);
314
41.9k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
315
41.9k
    ZVAL_LONG(&tmp, zend_get_compiled_lineno());
316
41.9k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
317
41.9k
  }
318
319
562k
  return object;
320
562k
}
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
845
{
333
845
  zval tmp, *object = ZEND_THIS;
334
335
845
  if (message) {
336
559
    ZVAL_STR_COPY(&tmp, message);
337
559
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
338
559
    if (UNEXPECTED(EG(exception))) {
339
0
      return FAILURE;
340
0
    }
341
559
  }
342
343
845
  if (code) {
344
2
    ZVAL_LONG(&tmp, code);
345
2
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
346
2
    if (UNEXPECTED(EG(exception))) {
347
0
      return FAILURE;
348
0
    }
349
2
  }
350
351
845
  if (previous) {
352
2
    Z_ADDREF_P(previous);
353
2
    zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
354
2
    if (UNEXPECTED(EG(exception))) {
355
0
      return FAILURE;
356
0
    }
357
2
  }
358
359
845
  return SUCCESS;
360
845
}
361
362
/* {{{ Exception constructor */
363
ZEND_METHOD(Exception, __construct)
364
842
{
365
842
  zend_string *message = NULL;
366
842
  zend_long   code = 0;
367
842
  zval *previous = NULL;
368
369
842
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
370
3
    RETURN_THROWS();
371
3
  }
372
373
839
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
374
0
    RETURN_THROWS();
375
0
  }
376
839
}
377
/* }}} */
378
379
/* {{{ Exception unserialize checks */
380
#define CHECK_EXC_TYPE(id, type) \
381
5.58k
  pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
382
5.58k
  if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
383
3
    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
3
  }
385
386
ZEND_METHOD(Exception, __wakeup)
387
2.79k
{
388
2.79k
  ZEND_PARSE_PARAMETERS_NONE();
389
390
2.79k
  zval value, *pvalue;
391
2.79k
  zval *object = ZEND_THIS;
392
2.79k
  CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
393
2.79k
  CHECK_EXC_TYPE(ZEND_STR_CODE,    IS_LONG);
394
  /* The type of all other properties is enforced through typed properties. */
395
2.79k
}
396
/* }}} */
397
398
/* {{{ ErrorException constructor */
399
ZEND_METHOD(ErrorException, __construct)
400
4
{
401
4
  zend_string *message = NULL, *filename = NULL;
402
4
  zend_long   code = 0, severity = E_ERROR, lineno;
403
4
  bool lineno_is_null = true;
404
4
  zval   tmp, *object, *previous = NULL;
405
406
4
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
407
1
    RETURN_THROWS();
408
1
  }
409
410
3
  object = ZEND_THIS;
411
412
3
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
413
0
    RETURN_THROWS();
414
0
  }
415
416
3
  ZVAL_LONG(&tmp, severity);
417
3
  zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
418
3
  if (UNEXPECTED(EG(exception))) {
419
0
    RETURN_THROWS();
420
0
  }
421
422
3
  if (filename) {
423
0
    ZVAL_STR_COPY(&tmp, filename);
424
0
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
425
0
    if (UNEXPECTED(EG(exception))) {
426
0
      RETURN_THROWS();
427
0
    }
428
0
  }
429
430
3
  if (!lineno_is_null) {
431
0
    ZVAL_LONG(&tmp, lineno);
432
0
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
433
0
    if (UNEXPECTED(EG(exception))) {
434
0
      RETURN_THROWS();
435
0
    }
436
3
  } else if (filename) {
437
0
    ZVAL_LONG(&tmp, 0);
438
0
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
439
0
    if (UNEXPECTED(EG(exception))) {
440
0
      RETURN_THROWS();
441
0
    }
442
0
  }
443
3
}
444
/* }}} */
445
446
#define GET_PROPERTY(object, id) \
447
56.8k
  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
2.05k
  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
2
{
454
2
  zval *prop, rv;
455
456
2
  ZEND_PARSE_PARAMETERS_NONE();
457
458
2
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
459
2
  RETURN_STR(zval_get_string(prop));
460
2
}
461
/* }}} */
462
463
/* {{{ Get the line in which the exception occurred */
464
ZEND_METHOD(Exception, getLine)
465
2
{
466
2
  zval *prop, rv;
467
468
2
  ZEND_PARSE_PARAMETERS_NONE();
469
470
2
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
471
2
  RETURN_LONG(zval_get_long(prop));
472
2
}
473
/* }}} */
474
475
/* {{{ Get the exception message */
476
ZEND_METHOD(Exception, getMessage)
477
35.3k
{
478
35.3k
  zval *prop, rv;
479
480
35.3k
  ZEND_PARSE_PARAMETERS_NONE();
481
482
35.3k
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
483
35.3k
  RETURN_STR(zval_get_string(prop));
484
35.3k
}
485
/* }}} */
486
487
/* {{{ Get the exception code */
488
ZEND_METHOD(Exception, getCode)
489
0
{
490
0
  zval *prop, rv;
491
492
0
  ZEND_PARSE_PARAMETERS_NONE();
493
494
0
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
495
0
  ZVAL_DEREF(prop);
496
0
  ZVAL_COPY(return_value, prop);
497
0
}
498
/* }}} */
499
500
/* {{{ Get the stack trace for the location in which the exception occurred */
501
ZEND_METHOD(Exception, getTrace)
502
9
{
503
9
  zval *prop, rv;
504
505
9
  ZEND_PARSE_PARAMETERS_NONE();
506
507
9
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
508
9
  ZVAL_DEREF(prop);
509
9
  ZVAL_COPY(return_value, prop);
510
9
}
511
/* }}} */
512
513
/* {{{ Get the exception severity */
514
ZEND_METHOD(ErrorException, getSeverity)
515
0
{
516
0
  zval *prop, rv;
517
518
0
  ZEND_PARSE_PARAMETERS_NONE();
519
520
0
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_SEVERITY);
521
0
  ZVAL_DEREF(prop);
522
0
  ZVAL_COPY(return_value, prop);
523
0
}
524
/* }}} */
525
526
49.2k
#define TRACE_APPEND_KEY(key) do {                                          \
527
49.2k
    tmp = zend_hash_find(ht, key);                                      \
528
49.2k
    if (tmp) {                                                          \
529
21.6k
      if (Z_TYPE_P(tmp) != IS_STRING) {                               \
530
0
        zend_error(E_WARNING, "Value for %s is not a string",       \
531
0
          ZSTR_VAL(key));                                         \
532
0
        smart_str_appends(str, "[unknown]");                        \
533
21.6k
      } else {                                                        \
534
21.6k
        smart_str_appends(str, Z_STRVAL_P(tmp));                    \
535
21.6k
      }                                                               \
536
21.6k
    } \
537
49.2k
  } while (0)
538
539
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
540
18.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
18.9k
  ZVAL_DEREF(arg);
548
549
18.9k
  if (smart_str_append_zval(str, arg, EG(exception_string_param_max_len)) == SUCCESS) {
550
17.5k
    smart_str_appends(str, ", ");
551
17.5k
  } else {
552
1.37k
    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
661
      case IS_ARRAY:
559
661
        smart_str_appends(str, "Array, ");
560
661
        break;
561
717
      case IS_OBJECT: {
562
717
        zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
563
717
        smart_str_appends(str, "Object(");
564
717
        smart_str_appends(str, ZSTR_VAL(class_name));
565
717
        smart_str_appends(str, "), ");
566
717
        zend_string_release_ex(class_name, 0);
567
717
        break;
568
0
      }
569
1.37k
    }
570
1.37k
  }
571
18.9k
}
572
/* }}} */
573
574
static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
575
16.4k
{
576
16.4k
  zval *file, *tmp;
577
578
16.4k
  smart_str_appendc(str, '#');
579
16.4k
  smart_str_append_long(str, num);
580
16.4k
  smart_str_appendc(str, ' ');
581
582
16.4k
  file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
583
16.4k
  if (file) {
584
15.7k
    if (UNEXPECTED(Z_TYPE_P(file) != IS_STRING)) {
585
0
      zend_error(E_WARNING, "File name is not a string");
586
0
      smart_str_appends(str, "[unknown file]: ");
587
15.7k
    } else{
588
15.7k
      zend_long line = 0;
589
15.7k
      tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
590
15.7k
      if (tmp) {
591
15.7k
        if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
592
15.7k
          line = Z_LVAL_P(tmp);
593
15.7k
        } else {
594
0
          zend_error(E_WARNING, "Line is not an int");
595
0
        }
596
15.7k
      }
597
15.7k
      smart_str_append(str, Z_STR_P(file));
598
15.7k
      smart_str_appendc(str, '(');
599
15.7k
      smart_str_append_long(str, line);
600
15.7k
      smart_str_appends(str, "): ");
601
15.7k
    }
602
15.7k
  } else {
603
683
    smart_str_appends(str, "[internal function]: ");
604
683
  }
605
16.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
606
16.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
607
16.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
608
16.4k
  smart_str_appendc(str, '(');
609
16.4k
  tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
610
16.4k
  if (tmp) {
611
16.4k
    if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
612
16.4k
      size_t last_len = ZSTR_LEN(str->s);
613
16.4k
      zend_string *name;
614
16.4k
      zval *arg;
615
616
54.3k
      ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
617
54.3k
        if (name) {
618
2
          smart_str_append(str, name);
619
2
          smart_str_appends(str, ": ");
620
2
        }
621
54.3k
        _build_trace_args(arg, str);
622
54.3k
      } ZEND_HASH_FOREACH_END();
623
624
16.4k
      if (last_len != ZSTR_LEN(str->s)) {
625
16.3k
        ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
626
16.3k
      }
627
16.4k
    } else {
628
0
      zend_error(E_WARNING, "args element is not an array");
629
0
    }
630
16.4k
  }
631
16.4k
  smart_str_appends(str, ")\n");
632
16.4k
}
633
/* }}} */
634
635
6.57k
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
636
6.57k
  zend_ulong index;
637
6.57k
  zval *frame;
638
6.57k
  uint32_t num = 0;
639
6.57k
  smart_str str = {0};
640
641
39.4k
  ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
642
39.4k
    if (UNEXPECTED(Z_TYPE_P(frame) != IS_ARRAY)) {
643
0
      zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
644
0
      continue;
645
0
    }
646
647
16.4k
    _build_trace_string(&str, Z_ARRVAL_P(frame), num++);
648
16.4k
  } ZEND_HASH_FOREACH_END();
649
650
6.57k
  if (include_main) {
651
6.56k
    smart_str_appendc(&str, '#');
652
6.56k
    smart_str_append_long(&str, num);
653
6.56k
    smart_str_appends(&str, " {main}");
654
6.56k
  }
655
656
6.57k
  smart_str_0(&str);
657
6.57k
  return str.s ? str.s : ZSTR_EMPTY_ALLOC();
658
6.57k
}
659
660
/* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
661
ZEND_METHOD(Exception, getTraceAsString)
662
2.72k
{
663
664
2.72k
  ZEND_PARSE_PARAMETERS_NONE();
665
666
2.72k
  zval *object = ZEND_THIS;
667
2.72k
  zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
668
2.72k
  zval rv;
669
2.72k
  const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
670
2.72k
  if (EG(exception)) {
671
0
    RETURN_THROWS();
672
0
  }
673
674
2.72k
  ZVAL_DEREF(trace);
675
  /* Type should be guaranteed by property type. */
676
2.72k
  ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
677
2.72k
  RETURN_NEW_STR(zend_trace_to_string(Z_ARRVAL_P(trace), /* include_main */ true));
678
2.72k
}
679
/* }}} */
680
681
/* {{{ Return previous Throwable or NULL. */
682
ZEND_METHOD(Exception, getPrevious)
683
12
{
684
12
  zval rv;
685
686
12
  ZEND_PARSE_PARAMETERS_NONE();
687
688
12
  ZVAL_COPY_DEREF(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
689
12
} /* }}} */
690
691
/* {{{ Obtain the string representation of the Exception object */
692
ZEND_METHOD(Exception, __toString)
693
4.70k
{
694
4.70k
  zval trace, *exception;
695
4.70k
  zend_class_entry *base_ce;
696
4.70k
  zend_string *str;
697
4.70k
  zval rv, tmp;
698
699
4.70k
  ZEND_PARSE_PARAMETERS_NONE();
700
701
4.70k
  str = ZSTR_EMPTY_ALLOC();
702
703
4.70k
  exception = ZEND_THIS;
704
4.70k
  base_ce = i_get_exception_base(Z_OBJ_P(exception));
705
706
  /* As getTraceAsString method is final we can grab it once */
707
4.70k
  zend_function *getTraceAsString = zend_hash_str_find_ptr(&base_ce->function_table, ZEND_STRL("gettraceasstring"));
708
4.70k
  ZEND_ASSERT(getTraceAsString && "Method getTraceAsString must exist");
709
710
711
4.70k
  zend_fcall_info fci;
712
4.70k
  fci.size = sizeof(fci);
713
4.70k
  ZVAL_UNDEF(&fci.function_name);
714
4.70k
  fci.retval = &trace;
715
4.70k
  fci.param_count = 0;
716
4.70k
  fci.params = NULL;
717
4.70k
  fci.object = NULL;
718
4.70k
  fci.named_params = NULL;
719
720
4.70k
  zend_fcall_info_cache fcc;
721
4.70k
  fcc.function_handler = getTraceAsString;
722
4.70k
  fcc.called_scope = base_ce;
723
4.70k
  fcc.closure = NULL;
724
725
9.40k
  while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
726
4.70k
    zend_string *prev_str = str;
727
4.70k
    zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
728
4.70k
    zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
729
4.70k
    zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
730
731
4.70k
    fcc.object = Z_OBJ_P(exception);
732
4.70k
    fcc.calling_scope = Z_OBJCE_P(exception);
733
4.70k
    zend_call_function(&fci, &fcc);
734
735
4.70k
    if (Z_TYPE(trace) != IS_STRING) {
736
0
      zval_ptr_dtor(&trace);
737
0
      ZVAL_UNDEF(&trace);
738
0
    }
739
740
4.70k
    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
24
      zend_string *real_message = zend_strpprintf_unchecked(0, "%S and defined", message);
742
24
      zend_string_release_ex(message, 0);
743
24
      message = real_message;
744
24
    }
745
746
4.70k
    zend_string *tmp_trace = (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace))
747
4.70k
      ? zend_string_copy(Z_STR(trace))
748
4.70k
      : ZSTR_INIT_LITERAL("#0 {main}\n", false);
749
750
4.70k
    zend_string *name = Z_OBJCE_P(exception)->name;
751
752
4.70k
    if (ZSTR_LEN(message) > 0) {
753
2.65k
      zval message_zv;
754
2.65k
      ZVAL_STR(&message_zv, message);
755
756
2.65k
      str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
757
2.65k
        name, message, file, line,
758
2.65k
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
759
2.65k
    } else {
760
2.04k
      str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
761
2.04k
        name, file, line,
762
2.04k
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
763
2.04k
    }
764
4.70k
    zend_string_release_ex(tmp_trace, false);
765
766
4.70k
    zend_string_release_ex(prev_str, 0);
767
4.70k
    zend_string_release_ex(message, 0);
768
4.70k
    zend_string_release_ex(file, 0);
769
4.70k
    zval_ptr_dtor(&trace);
770
771
4.70k
    Z_PROTECT_RECURSION_P(exception);
772
4.70k
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
773
4.70k
    ZVAL_DEREF(exception);
774
4.70k
    if (Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
775
0
      break;
776
0
    }
777
4.70k
  }
778
779
4.70k
  exception = ZEND_THIS;
780
  /* Reset apply counts */
781
4.70k
  zend_class_entry *previous_base_ce;
782
7.42k
  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.72k
    if (Z_IS_RECURSIVE_P(exception)) {
784
2.72k
      Z_UNPROTECT_RECURSION_P(exception);
785
2.72k
    } else {
786
0
      break;
787
0
    }
788
2.72k
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
789
2.72k
    ZVAL_DEREF(exception);
790
2.72k
  }
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
4.70k
  ZVAL_STR(&tmp, str);
795
4.70k
  zend_update_property_ex(base_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
796
797
4.70k
  RETURN_STR(str);
798
4.70k
}
799
/* }}} */
800
801
168
static void zend_init_exception_class_entry(zend_class_entry *ce) {
802
168
  ce->create_object = zend_default_exception_new;
803
168
  ce->default_object_handlers = &default_exception_handlers;
804
168
}
805
806
void zend_register_default_exception(void) /* {{{ */
807
14
{
808
14
  zend_ce_throwable = register_class_Throwable(zend_ce_stringable);
809
14
  zend_ce_throwable->interface_gets_implemented = zend_implement_throwable;
810
811
14
  memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
812
14
  default_exception_handlers.clone_obj = NULL;
813
814
14
  zend_ce_exception = register_class_Exception(zend_ce_throwable);
815
14
  zend_init_exception_class_entry(zend_ce_exception);
816
817
14
  zend_ce_error_exception = register_class_ErrorException(zend_ce_exception);
818
14
  zend_init_exception_class_entry(zend_ce_error_exception);
819
820
14
  zend_ce_error = register_class_Error(zend_ce_throwable);
821
14
  zend_init_exception_class_entry(zend_ce_error);
822
823
14
  zend_ce_compile_error = register_class_CompileError(zend_ce_error);
824
14
  zend_init_exception_class_entry(zend_ce_compile_error);
825
826
14
  zend_ce_parse_error = register_class_ParseError(zend_ce_compile_error);
827
14
  zend_init_exception_class_entry(zend_ce_parse_error);
828
829
14
  zend_ce_type_error = register_class_TypeError(zend_ce_error);
830
14
  zend_init_exception_class_entry(zend_ce_type_error);
831
832
14
  zend_ce_argument_count_error = register_class_ArgumentCountError(zend_ce_type_error);
833
14
  zend_init_exception_class_entry(zend_ce_argument_count_error);
834
835
14
  zend_ce_value_error = register_class_ValueError(zend_ce_error);
836
14
  zend_init_exception_class_entry(zend_ce_value_error);
837
838
14
  zend_ce_arithmetic_error = register_class_ArithmeticError(zend_ce_error);
839
14
  zend_init_exception_class_entry(zend_ce_arithmetic_error);
840
841
14
  zend_ce_division_by_zero_error = register_class_DivisionByZeroError(zend_ce_arithmetic_error);
842
14
  zend_init_exception_class_entry(zend_ce_division_by_zero_error);
843
844
14
  zend_ce_unhandled_match_error = register_class_UnhandledMatchError(zend_ce_error);
845
14
  zend_init_exception_class_entry(zend_ce_unhandled_match_error);
846
847
14
  zend_ce_request_parse_body_exception = register_class_RequestParseBodyException(zend_ce_exception);
848
14
  zend_init_exception_class_entry(zend_ce_request_parse_body_exception);
849
850
14
  INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
851
852
14
  INIT_CLASS_ENTRY(zend_ce_graceful_exit, "GracefulExit", NULL);
853
14
}
854
/* }}} */
855
856
static zend_object *zend_throw_exception_zstr(zend_class_entry *exception_ce, zend_string *message, zend_long code) /* {{{ */
857
459k
{
858
459k
  zval ex, tmp;
859
860
459k
  if (!exception_ce) {
861
425
    exception_ce = zend_ce_exception;
862
425
  }
863
864
459k
  ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
865
459k
    && "Exceptions must implement Throwable");
866
867
459k
  object_init_ex(&ex, exception_ce);
868
869
459k
  if (message) {
870
459k
    ZVAL_STR(&tmp, message);
871
459k
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
872
459k
  }
873
459k
  if (code) {
874
132
    ZVAL_LONG(&tmp, code);
875
132
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
876
132
  }
877
878
459k
  zend_throw_exception_internal(Z_OBJ(ex));
879
880
459k
  return Z_OBJ(ex);
881
459k
}
882
/* }}} */
883
884
ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
885
246k
{
886
246k
  zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
887
246k
  zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
888
246k
  if (msg_str) {
889
246k
    zend_string_release(msg_str);
890
246k
  }
891
246k
  return ex;
892
246k
}
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
213k
{
897
213k
  va_list arg;
898
213k
  zend_object *obj;
899
900
213k
  va_start(arg, format);
901
213k
  zend_string *msg_str = zend_vstrpprintf(0, format, arg);
902
213k
  va_end(arg);
903
213k
  obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
904
213k
  zend_string_release(msg_str);
905
213k
  return obj;
906
213k
}
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
0
{
911
0
  zend_object *obj = zend_throw_exception_zstr(exception_ce, message, code);
912
0
  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
0
  return obj;
918
0
}
919
/* }}} */
920
921
static void zend_error_va(int type, zend_string *file, uint32_t lineno, const char *format, ...) /* {{{ */
922
683
{
923
683
  va_list args;
924
683
  va_start(args, format);
925
683
  zend_string *message = zend_vstrpprintf(0, format, args);
926
683
  zend_observer_error_notify(type, file, lineno, message);
927
683
  zend_error_cb(type, file, lineno, message);
928
683
  zend_string_release(message);
929
683
  va_end(args);
930
683
}
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
686
{
936
686
  zval exception, rv;
937
686
  zend_class_entry *ce_exception;
938
686
  zend_result result = FAILURE;
939
940
686
  ZVAL_OBJ(&exception, ex);
941
686
  ce_exception = ex->ce;
942
686
  EG(exception) = NULL;
943
944
686
  zval_ptr_dtor(&EG(last_fatal_error_backtrace));
945
686
  ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
946
947
686
  if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
948
1
    zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
949
1
    zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
950
1
    zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
951
1
    int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;
952
953
1
    zend_observer_error_notify(type, file, line, message);
954
1
    zend_error_cb(type, file, line, message);
955
956
1
    zend_string_release_ex(file, 0);
957
1
    zend_string_release_ex(message, 0);
958
685
  } else if (instanceof_function(ce_exception, zend_ce_throwable)) {
959
685
    zval tmp;
960
685
    zend_string *str, *file = NULL;
961
685
    zend_long line = 0;
962
963
685
    zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
964
685
    if (!EG(exception)) {
965
683
      if (UNEXPECTED(Z_ISREF(tmp))) {
966
0
        zend_unwrap_reference(&tmp);
967
0
      }
968
683
      if (Z_TYPE(tmp) != IS_STRING) {
969
0
        zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
970
683
      } else {
971
683
        zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
972
683
      }
973
683
    }
974
685
    zval_ptr_dtor(&tmp);
975
976
685
    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
685
    str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
996
685
    file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
997
685
    line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
998
999
685
    zend_error_va(severity | E_DONT_BAIL,
1000
685
      (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
1001
685
      "Uncaught %S\n  thrown", str);
1002
1003
685
    zend_string_release_ex(str, 0);
1004
685
    zend_string_release_ex(file, 0);
1005
685
  } 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
0
  } else {
1009
0
    zend_error(severity, "Uncaught exception %s", ZSTR_VAL(ce_exception->name));
1010
0
  }
1011
1012
686
  OBJ_RELEASE(ex);
1013
686
  return result;
1014
686
}
1015
/* }}} */
1016
1017
1
ZEND_NORETURN void zend_exception_uncaught_error(const char *format, ...) {
1018
1
  va_list va;
1019
1
  va_start(va, format);
1020
1
  zend_string *prefix = zend_vstrpprintf(0, format, va);
1021
1
  va_end(va);
1022
1023
1
  ZEND_ASSERT(EG(exception));
1024
1
  zval exception_zv;
1025
1
  ZVAL_OBJ_COPY(&exception_zv, EG(exception));
1026
1
  zend_clear_exception();
1027
1028
1
  zend_string *exception_str = zval_get_string(&exception_zv);
1029
1
  zend_error_noreturn(E_ERROR,
1030
1
    "%s: Uncaught %s", ZSTR_VAL(prefix), ZSTR_VAL(exception_str));
1031
1
}
1032
1033
ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
1034
628
{
1035
628
  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
628
  zend_class_entry *exception_ce = Z_OBJCE_P(exception);
1040
1041
628
  if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
1042
1
    zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1043
1
    zval_ptr_dtor(exception);
1044
1
    return;
1045
1
  }
1046
1047
627
  zend_throw_exception_internal(Z_OBJ_P(exception));
1048
627
}
1049
/* }}} */
1050
1051
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void)
1052
19
{
1053
19
  return zend_objects_new(&zend_ce_unwind_exit);
1054
19
}
1055
1056
ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void)
1057
290
{
1058
290
  return zend_objects_new(&zend_ce_graceful_exit);
1059
290
}
1060
1061
ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
1062
19
{
1063
19
  ZEND_ASSERT(!EG(exception));
1064
19
  EG(exception) = zend_create_unwind_exit();
1065
19
  EG(opline_before_exception) = EG(current_execute_data)->opline;
1066
19
  EG(current_execute_data)->opline = EG(exception_op);
1067
19
}
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
19.9k
{
1079
19.9k
  return ex->ce == &zend_ce_unwind_exit;
1080
19.9k
}
1081
1082
ZEND_API bool zend_is_graceful_exit(const zend_object *ex)
1083
10.3k
{
1084
10.3k
  return ex->ce == &zend_ce_graceful_exit;
1085
10.3k
}