Coverage Report

Created: 2025-11-16 06:23

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