Coverage Report

Created: 2025-12-14 06:05

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