Coverage Report

Created: 2025-07-23 06:33

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