Coverage Report

Created: 2026-06-02 06:36

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