Coverage Report

Created: 2026-04-01 06:49

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