Coverage Report

Created: 2025-06-13 06:43

/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.92k
#define ZEND_EXCEPTION_MESSAGE_OFF 0
34
258
#define ZEND_EXCEPTION_CODE_OFF 2
35
1.29M
#define ZEND_EXCEPTION_FILE_OFF 3
36
1.29M
#define ZEND_EXCEPTION_LINE_OFF 4
37
1.29M
#define ZEND_EXCEPTION_TRACE_OFF 5
38
125
#define ZEND_EXCEPTION_PREVIOUS_OFF 6
39
191
#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
934
{
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
934
  zend_class_entry *root = class_type;
71
2.27k
  while (root->parent) {
72
1.33k
    root = root->parent;
73
1.33k
  }
74
934
  if (zend_string_equals_literal(root->name, "Exception")
75
934
      || zend_string_equals_literal(root->name, "Error")) {
76
924
    return SUCCESS;
77
924
  }
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
934
}
90
/* }}} */
91
92
static inline zend_class_entry *i_get_exception_base(zend_object *object) /* {{{ */
93
28.6M
{
94
28.6M
  return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
95
28.6M
}
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.14M
{
106
1.14M
  zval *previous, *ancestor, *ex;
107
1.14M
  zval  pv, zv, rv;
108
1.14M
  zend_class_entry *base_ce;
109
110
1.14M
  if (!exception || !add_previous) {
111
993k
    return;
112
993k
  }
113
114
146k
  if (exception == add_previous || zend_is_unwind_exit(add_previous) || zend_is_graceful_exit(add_previous)) {
115
45
    OBJ_RELEASE(add_previous);
116
45
    return;
117
45
  }
118
119
146k
  ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
120
146k
    && "Previous exception must implement Throwable");
121
122
146k
  ZVAL_OBJ(&pv, add_previous);
123
146k
  ZVAL_OBJ(&zv, exception);
124
146k
  ex = &zv;
125
146k
  do {
126
146k
    ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
127
146k
    ZVAL_DEREF(ancestor);
128
23.2M
    while (Z_TYPE_P(ancestor) == IS_OBJECT) {
129
23.0M
      if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
130
27
        OBJ_RELEASE(add_previous);
131
27
        return;
132
27
      }
133
23.0M
      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
23.0M
      ZVAL_DEREF(ancestor);
135
23.0M
    }
136
146k
    base_ce = i_get_exception_base(Z_OBJ_P(ex));
137
146k
    previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
138
146k
    ZVAL_DEREF(previous);
139
146k
    if (Z_TYPE_P(previous) == IS_NULL) {
140
146k
      zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
141
146k
      GC_DELREF(add_previous);
142
146k
      return;
143
146k
    }
144
58
    ex = previous;
145
58
  } while (Z_OBJ_P(ex) != add_previous);
146
146k
}
147
/* }}} */
148
149
void zend_exception_save(void) /* {{{ */
150
490k
{
151
490k
  if (EG(prev_exception)) {
152
8
    zend_exception_set_previous(EG(exception), EG(prev_exception));
153
8
  }
154
490k
  if (EG(exception)) {
155
35.6k
    EG(prev_exception) = EG(exception);
156
35.6k
  }
157
490k
  EG(exception) = NULL;
158
490k
}
159
/* }}} */
160
161
void zend_exception_restore(void) /* {{{ */
162
1.18M
{
163
1.18M
  if (EG(prev_exception)) {
164
35.2k
    if (EG(exception)) {
165
10
      zend_exception_set_previous(EG(exception), EG(prev_exception));
166
35.2k
    } else {
167
35.2k
      EG(exception) = EG(prev_exception);
168
35.2k
    }
169
35.2k
    EG(prev_exception) = NULL;
170
35.2k
  }
171
1.18M
}
172
/* }}} */
173
174
801k
static zend_always_inline bool is_handle_exception_set(void) {
175
801k
  zend_execute_data *execute_data = EG(current_execute_data);
176
801k
  return !execute_data
177
801k
    || !execute_data->func
178
801k
    || !ZEND_USER_CODE(execute_data->func->common.type)
179
801k
    || execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
180
801k
}
181
182
ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
183
1.14M
{
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.14M
  if (exception != NULL) {
195
1.14M
    zend_object *previous = EG(exception);
196
1.14M
    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.14M
    zend_exception_set_previous(exception, EG(exception));
203
1.14M
    EG(exception) = exception;
204
1.14M
    if (previous) {
205
146k
      return;
206
146k
    }
207
1.14M
  }
208
994k
  if (!EG(current_execute_data)) {
209
192k
    if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
210
192k
      return;
211
192k
    }
212
210
    if (EG(exception)) {
213
210
      if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF
214
210
       && !zend_is_unwind_exit(EG(exception))
215
210
       && !zend_is_graceful_exit(EG(exception))) {
216
15
        zend_user_exception_handler();
217
15
        if (EG(exception)) {
218
0
          zend_exception_error(EG(exception), E_ERROR);
219
0
        }
220
15
        return;
221
195
      } else {
222
195
        zend_exception_error(EG(exception), E_ERROR);
223
195
      }
224
210
      zend_bailout();
225
210
    }
226
0
    zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
227
210
  }
228
229
801k
  if (zend_throw_exception_hook) {
230
0
    zend_throw_exception_hook(exception);
231
0
  }
232
233
801k
  if (is_handle_exception_set()) {
234
    /* no need to rethrow the exception */
235
740k
    return;
236
740k
  }
237
60.7k
  EG(opline_before_exception) = EG(current_execute_data)->opline;
238
60.7k
  EG(current_execute_data)->opline = EG(exception_op);
239
60.7k
}
240
/* }}} */
241
242
ZEND_API void zend_clear_exception(void) /* {{{ */
243
180k
{
244
180k
  zend_object *exception;
245
180k
  if (EG(prev_exception)) {
246
363
    OBJ_RELEASE(EG(prev_exception));
247
363
    EG(prev_exception) = NULL;
248
363
  }
249
180k
  if (!EG(exception)) {
250
6.99k
    return;
251
6.99k
  }
252
  /* exception may have destructor */
253
173k
  exception = EG(exception);
254
173k
  EG(exception) = NULL;
255
173k
  OBJ_RELEASE(exception);
256
173k
  if (EG(current_execute_data)) {
257
24.1k
    EG(current_execute_data)->opline = EG(opline_before_exception);
258
24.1k
  }
259
173k
#if ZEND_DEBUG
260
173k
  EG(opline_before_exception) = NULL;
261
173k
#endif
262
173k
}
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.87M
{
270
3.87M
  if (UNEXPECTED(object->ce->num_hooked_props > 0)) {
271
    /* Property may have been overridden with a hook. */
272
95
    zend_update_property_ex(scope != NULL ? scope : object->ce, object, member, value);
273
95
    zval_ptr_dtor(value);
274
95
    return;
275
95
  }
276
3.87M
#if ZEND_DEBUG
277
3.87M
  zend_class_entry *old_scope = EG(fake_scope);
278
3.87M
  EG(fake_scope) = i_get_exception_base(object);
279
3.87M
  const zend_property_info *prop_info = zend_get_property_info(object->ce, member, true);
280
3.87M
  ZEND_ASSERT(OBJ_PROP_TO_NUM(prop_info->offset) == prop_num);
281
3.87M
  EG(fake_scope) = old_scope;
282
3.87M
#endif
283
3.87M
  zval *zv = OBJ_PROP_NUM(object, prop_num);
284
3.87M
  zval_ptr_safe_dtor(zv);
285
3.87M
  ZVAL_COPY_VALUE(zv, value);
286
3.87M
}
287
288
static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
289
1.29M
{
290
1.29M
  zval tmp;
291
1.29M
  zval trace;
292
1.29M
  zend_string *filename;
293
294
1.29M
  zend_object *object = zend_objects_new(class_type);
295
1.29M
  object_properties_init(object, class_type);
296
297
1.29M
  if (EG(current_execute_data)) {
298
1.09M
    zend_fetch_debug_backtrace(&trace,
299
1.09M
      0,
300
1.09M
      EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
301
1.09M
  } else {
302
192k
    ZVAL_EMPTY_ARRAY(&trace);
303
192k
  }
304
305
1.29M
  zend_update_property_num_checked(i_get_exception_base(object), object, ZEND_EXCEPTION_TRACE_OFF, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
306
307
1.29M
  if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
308
1.29M
      || !(filename = zend_get_compiled_filename()))) {
309
1.08M
    ZVAL_STRING(&tmp, zend_get_executed_filename());
310
1.08M
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
311
1.08M
    ZVAL_LONG(&tmp, zend_get_executed_lineno());
312
1.08M
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
313
1.08M
  } else {
314
205k
    ZVAL_STR_COPY(&tmp, filename);
315
205k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
316
205k
    ZVAL_LONG(&tmp, zend_get_compiled_lineno());
317
205k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
318
205k
  }
319
320
1.29M
  return object;
321
1.29M
}
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
4.21k
{
334
4.21k
  zval tmp, *object = ZEND_THIS;
335
336
4.21k
  if (message) {
337
2.92k
    ZVAL_STR_COPY(&tmp, message);
338
2.92k
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
339
2.92k
    if (UNEXPECTED(EG(exception))) {
340
0
      return FAILURE;
341
0
    }
342
2.92k
  }
343
344
4.21k
  if (code) {
345
258
    ZVAL_LONG(&tmp, code);
346
258
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
347
258
    if (UNEXPECTED(EG(exception))) {
348
10
      return FAILURE;
349
10
    }
350
258
  }
351
352
4.20k
  if (previous) {
353
125
    Z_ADDREF_P(previous);
354
125
    zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
355
125
    if (UNEXPECTED(EG(exception))) {
356
0
      return FAILURE;
357
0
    }
358
125
  }
359
360
4.20k
  return SUCCESS;
361
4.20k
}
362
363
/* {{{ Exception constructor */
364
ZEND_METHOD(Exception, __construct)
365
4.03k
{
366
4.03k
  zend_string *message = NULL;
367
4.03k
  zend_long   code = 0;
368
4.03k
  zval *previous = NULL;
369
370
4.03k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
371
19
    RETURN_THROWS();
372
19
  }
373
374
4.01k
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
375
5
    RETURN_THROWS();
376
5
  }
377
4.01k
}
378
/* }}} */
379
380
/* {{{ Exception unserialize checks */
381
#define CHECK_EXC_TYPE(id, type) \
382
4.79k
  pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
383
4.79k
  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
2.39k
{
389
2.39k
  ZEND_PARSE_PARAMETERS_NONE();
390
391
2.39k
  zval value, *pvalue;
392
2.39k
  zval *object = ZEND_THIS;
393
2.39k
  CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
394
2.39k
  CHECK_EXC_TYPE(ZEND_STR_CODE,    IS_LONG);
395
  /* The type of all other properties is enforced through typed properties. */
396
2.39k
}
397
/* }}} */
398
399
/* {{{ ErrorException constructor */
400
ZEND_METHOD(ErrorException, __construct)
401
205
{
402
205
  zend_string *message = NULL, *filename = NULL;
403
205
  zend_long   code = 0, severity = E_ERROR, lineno;
404
205
  bool lineno_is_null = 1;
405
205
  zval   tmp, *object, *previous = NULL;
406
407
205
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
408
9
    RETURN_THROWS();
409
9
  }
410
411
196
  object = ZEND_THIS;
412
413
196
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
414
5
    RETURN_THROWS();
415
5
  }
416
417
191
  ZVAL_LONG(&tmp, severity);
418
191
  zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_SEVERITY_OFF, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
419
191
  if (UNEXPECTED(EG(exception))) {
420
0
    RETURN_THROWS();
421
0
  }
422
423
191
  if (filename) {
424
119
    ZVAL_STR_COPY(&tmp, filename);
425
119
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
426
119
    if (UNEXPECTED(EG(exception))) {
427
0
      RETURN_THROWS();
428
0
    }
429
119
  }
430
431
191
  if (!lineno_is_null) {
432
111
    ZVAL_LONG(&tmp, lineno);
433
111
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
434
111
    if (UNEXPECTED(EG(exception))) {
435
0
      RETURN_THROWS();
436
0
    }
437
111
  } else if (filename) {
438
18
    ZVAL_LONG(&tmp, 0);
439
18
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
440
18
    if (UNEXPECTED(EG(exception))) {
441
0
      RETURN_THROWS();
442
0
    }
443
18
  }
444
191
}
445
/* }}} */
446
447
#define GET_PROPERTY(object, id) \
448
116k
  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
666
  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
123
{
455
123
  zval *prop, rv;
456
457
123
  ZEND_PARSE_PARAMETERS_NONE();
458
459
123
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
460
123
  RETURN_STR(zval_get_string(prop));
461
123
}
462
/* }}} */
463
464
/* {{{ Get the line in which the exception occurred */
465
ZEND_METHOD(Exception, getLine)
466
128
{
467
128
  zval *prop, rv;
468
469
128
  ZEND_PARSE_PARAMETERS_NONE();
470
471
128
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
472
128
  RETURN_LONG(zval_get_long(prop));
473
128
}
474
/* }}} */
475
476
/* {{{ Get the exception message */
477
ZEND_METHOD(Exception, getMessage)
478
89.6k
{
479
89.6k
  zval *prop, rv;
480
481
89.6k
  ZEND_PARSE_PARAMETERS_NONE();
482
483
89.6k
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
484
89.6k
  RETURN_STR(zval_get_string(prop));
485
89.6k
}
486
/* }}} */
487
488
/* {{{ Get the exception code */
489
ZEND_METHOD(Exception, getCode)
490
90
{
491
90
  zval *prop, rv;
492
493
90
  ZEND_PARSE_PARAMETERS_NONE();
494
495
90
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
496
90
  ZVAL_DEREF(prop);
497
90
  ZVAL_COPY(return_value, prop);
498
90
}
499
/* }}} */
500
501
/* {{{ Get the stack trace for the location in which the exception occurred */
502
ZEND_METHOD(Exception, getTrace)
503
113
{
504
113
  zval *prop, rv;
505
506
113
  ZEND_PARSE_PARAMETERS_NONE();
507
508
113
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
509
113
  ZVAL_DEREF(prop);
510
113
  ZVAL_COPY(return_value, prop);
511
113
}
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
91.4k
#define TRACE_APPEND_KEY(key) do {                                          \
528
91.4k
    tmp = zend_hash_find(ht, key);                                      \
529
91.4k
    if (tmp) {                                                          \
530
40.3k
      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
40.2k
      } else {                                                        \
535
40.2k
        smart_str_appends(str, Z_STRVAL_P(tmp));                    \
536
40.2k
      }                                                               \
537
40.3k
    } \
538
91.4k
  } while (0)
539
540
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
541
34.0k
{
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
34.0k
  ZVAL_DEREF(arg);
549
550
34.0k
  if (smart_str_append_zval(str, arg, EG(exception_string_param_max_len)) == SUCCESS) {
551
32.6k
    smart_str_appends(str, ", ");
552
32.6k
  } else {
553
1.38k
    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
10
      case IS_ARRAY:
560
10
        smart_str_appends(str, "Array, ");
561
10
        break;
562
1.37k
      case IS_OBJECT: {
563
1.37k
        zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
564
1.37k
        smart_str_appends(str, "Object(");
565
1.37k
        smart_str_appends(str, ZSTR_VAL(class_name));
566
1.37k
        smart_str_appends(str, "), ");
567
1.37k
        zend_string_release_ex(class_name, 0);
568
1.37k
        break;
569
0
      }
570
1.38k
    }
571
1.38k
  }
572
34.0k
}
573
/* }}} */
574
575
static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
576
30.4k
{
577
30.4k
  zval *file, *tmp;
578
579
30.4k
  smart_str_appendc(str, '#');
580
30.4k
  smart_str_append_long(str, num);
581
30.4k
  smart_str_appendc(str, ' ');
582
583
30.4k
  file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
584
30.4k
  if (file) {
585
30.1k
    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
30.1k
    } else{
589
30.1k
      zend_long line = 0;
590
30.1k
      tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
591
30.1k
      if (tmp) {
592
30.1k
        if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
593
30.1k
          line = Z_LVAL_P(tmp);
594
30.1k
        } else {
595
0
          zend_error(E_WARNING, "Line is not an int");
596
0
        }
597
30.1k
      }
598
30.1k
      smart_str_append(str, Z_STR_P(file));
599
30.1k
      smart_str_appendc(str, '(');
600
30.1k
      smart_str_append_long(str, line);
601
30.1k
      smart_str_appends(str, "): ");
602
30.1k
    }
603
30.1k
  } else {
604
372
    smart_str_appends(str, "[internal function]: ");
605
372
  }
606
30.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
607
30.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
608
30.4k
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
609
30.4k
  smart_str_appendc(str, '(');
610
30.4k
  tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
611
30.4k
  if (tmp) {
612
30.3k
    if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
613
30.3k
      size_t last_len = ZSTR_LEN(str->s);
614
30.3k
      zend_string *name;
615
30.3k
      zval *arg;
616
617
98.5k
      ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
618
98.5k
        if (name) {
619
20
          smart_str_append(str, name);
620
20
          smart_str_appends(str, ": ");
621
20
        }
622
98.5k
        _build_trace_args(arg, str);
623
98.5k
      } ZEND_HASH_FOREACH_END();
624
625
30.3k
      if (last_len != ZSTR_LEN(str->s)) {
626
29.5k
        ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
627
29.5k
      }
628
30.3k
    } else {
629
5
      zend_error(E_WARNING, "args element is not an array");
630
5
    }
631
30.3k
  }
632
30.4k
  smart_str_appends(str, ")\n");
633
30.4k
}
634
/* }}} */
635
636
13.6k
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
637
13.6k
  zend_ulong index;
638
13.6k
  zval *frame;
639
13.6k
  uint32_t num = 0;
640
13.6k
  smart_str str = {0};
641
642
74.6k
  ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
643
74.6k
    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
30.4k
    _build_trace_string(&str, Z_ARRVAL_P(frame), num++);
649
30.4k
  } ZEND_HASH_FOREACH_END();
650
651
13.6k
  if (include_main) {
652
13.4k
    smart_str_appendc(&str, '#');
653
13.4k
    smart_str_append_long(&str, num);
654
13.4k
    smart_str_appends(&str, " {main}");
655
13.4k
  }
656
657
13.6k
  smart_str_0(&str);
658
13.6k
  return str.s ? str.s : ZSTR_EMPTY_ALLOC();
659
13.6k
}
660
661
/* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
662
ZEND_METHOD(Exception, getTraceAsString)
663
5.34k
{
664
665
5.34k
  ZEND_PARSE_PARAMETERS_NONE();
666
667
5.34k
  zval *object = ZEND_THIS;
668
5.34k
  zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
669
5.34k
  zval rv;
670
5.34k
  const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
671
5.34k
  if (EG(exception)) {
672
0
    RETURN_THROWS();
673
0
  }
674
675
5.34k
  ZVAL_DEREF(trace);
676
  /* Type should be guaranteed by property type. */
677
5.34k
  ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
678
5.34k
  RETURN_NEW_STR(zend_trace_to_string(Z_ARRVAL_P(trace), /* include_main */ true));
679
5.34k
}
680
/* }}} */
681
682
/* {{{ Return previous Throwable or NULL. */
683
ZEND_METHOD(Exception, getPrevious)
684
140
{
685
140
  zval rv;
686
687
140
  ZEND_PARSE_PARAMETERS_NONE();
688
689
140
  ZVAL_COPY_DEREF(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
690
140
} /* }}} */
691
692
/* {{{ Obtain the string representation of the Exception object */
693
ZEND_METHOD(Exception, __toString)
694
5.25k
{
695
5.25k
  zval trace, *exception;
696
5.25k
  zend_class_entry *base_ce;
697
5.25k
  zend_string *str;
698
5.25k
  zend_fcall_info fci;
699
5.25k
  zval rv, tmp;
700
5.25k
  zend_string *fname;
701
702
5.25k
  ZEND_PARSE_PARAMETERS_NONE();
703
704
5.25k
  str = ZSTR_EMPTY_ALLOC();
705
706
5.25k
  exception = ZEND_THIS;
707
5.25k
  fname = ZSTR_INIT_LITERAL("gettraceasstring", 0);
708
709
10.5k
  while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
710
5.31k
    zend_string *prev_str = str;
711
5.31k
    zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
712
5.31k
    zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
713
5.31k
    zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
714
715
5.31k
    fci.size = sizeof(fci);
716
5.31k
    ZVAL_STR(&fci.function_name, fname);
717
5.31k
    fci.object = Z_OBJ_P(exception);
718
5.31k
    fci.retval = &trace;
719
5.31k
    fci.param_count = 0;
720
5.31k
    fci.params = NULL;
721
5.31k
    fci.named_params = NULL;
722
723
5.31k
    zend_call_function(&fci, NULL);
724
725
5.31k
    if (Z_TYPE(trace) != IS_STRING) {
726
0
      zval_ptr_dtor(&trace);
727
0
      ZVAL_UNDEF(&trace);
728
0
    }
729
730
5.31k
    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
62
      zend_string *real_message = zend_strpprintf_unchecked(0, "%S and defined", message);
732
62
      zend_string_release_ex(message, 0);
733
62
      message = real_message;
734
62
    }
735
736
5.31k
    zend_string *tmp_trace = (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace))
737
5.31k
      ? zend_string_copy(Z_STR(trace))
738
5.31k
      : ZSTR_INIT_LITERAL("#0 {main}\n", false);
739
740
5.31k
    zend_string *name = Z_OBJCE_P(exception)->name;
741
742
5.31k
    if (ZSTR_LEN(message) > 0) {
743
4.79k
      zval message_zv;
744
4.79k
      ZVAL_STR(&message_zv, message);
745
746
4.79k
      str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
747
4.79k
        name, message, file, line,
748
4.79k
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
749
4.79k
    } else {
750
520
      str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
751
520
        name, file, line,
752
520
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
753
520
    }
754
5.31k
    zend_string_release_ex(tmp_trace, false);
755
756
5.31k
    zend_string_release_ex(prev_str, 0);
757
5.31k
    zend_string_release_ex(message, 0);
758
5.31k
    zend_string_release_ex(file, 0);
759
5.31k
    zval_ptr_dtor(&trace);
760
761
5.31k
    Z_PROTECT_RECURSION_P(exception);
762
5.31k
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
763
5.31k
    ZVAL_DEREF(exception);
764
5.31k
    if (Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
765
0
      break;
766
0
    }
767
5.31k
  }
768
5.25k
  zend_string_release_ex(fname, 0);
769
770
5.25k
  exception = ZEND_THIS;
771
  /* Reset apply counts */
772
10.5k
  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.31k
    if (Z_IS_RECURSIVE_P(exception)) {
774
5.31k
      Z_UNPROTECT_RECURSION_P(exception);
775
5.31k
    } else {
776
0
      break;
777
0
    }
778
5.31k
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
779
5.31k
    ZVAL_DEREF(exception);
780
5.31k
  }
781
782
5.25k
  exception = ZEND_THIS;
783
5.25k
  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.25k
  ZVAL_STR(&tmp, str);
788
5.25k
  zend_update_property_ex(base_ce, Z_OBJ_P(exception), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
789
790
5.25k
  RETURN_STR(str);
791
5.25k
}
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
/* {{{ Deprecated - Use zend_ce_exception directly instead */
850
ZEND_API zend_class_entry *zend_exception_get_default(void)
851
0
{
852
0
  return zend_ce_exception;
853
0
}
854
/* }}} */
855
856
/* {{{ Deprecated - Use zend_ce_error_exception directly instead */
857
ZEND_API zend_class_entry *zend_get_error_exception(void)
858
0
{
859
0
  return zend_ce_error_exception;
860
0
}
861
/* }}} */
862
863
static zend_object *zend_throw_exception_zstr(zend_class_entry *exception_ce, zend_string *message, zend_long code) /* {{{ */
864
1.13M
{
865
1.13M
  zval ex, tmp;
866
867
1.13M
  if (!exception_ce) {
868
484
    exception_ce = zend_ce_exception;
869
484
  }
870
871
1.13M
  ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
872
1.13M
    && "Exceptions must implement Throwable");
873
874
1.13M
  object_init_ex(&ex, exception_ce);
875
876
1.13M
  if (message) {
877
1.13M
    ZVAL_STR(&tmp, message);
878
1.13M
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
879
1.13M
  }
880
1.13M
  if (code) {
881
497
    ZVAL_LONG(&tmp, code);
882
497
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
883
497
  }
884
885
1.13M
  zend_throw_exception_internal(Z_OBJ(ex));
886
887
1.13M
  return Z_OBJ(ex);
888
1.13M
}
889
/* }}} */
890
891
ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
892
473k
{
893
473k
  zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
894
473k
  zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
895
473k
  if (msg_str) {
896
473k
    zend_string_release(msg_str);
897
473k
  }
898
473k
  return ex;
899
473k
}
900
/* }}} */
901
902
ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
903
662k
{
904
662k
  va_list arg;
905
662k
  zend_object *obj;
906
907
662k
  va_start(arg, format);
908
662k
  zend_string *msg_str = zend_vstrpprintf(0, format, arg);
909
662k
  va_end(arg);
910
662k
  obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
911
662k
  zend_string_release(msg_str);
912
662k
  return obj;
913
662k
}
914
/* }}} */
915
916
ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, zend_string *message, zend_long code, int severity) /* {{{ */
917
0
{
918
0
  zend_object *obj = zend_throw_exception_zstr(exception_ce, message, code);
919
0
  if (exception_ce && instanceof_function(exception_ce, zend_ce_error_exception)) {
920
0
    zval tmp;
921
0
    ZVAL_LONG(&tmp, severity);
922
0
    zend_update_property_ex(zend_ce_error_exception, obj, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
923
0
  }
924
0
  return obj;
925
0
}
926
/* }}} */
927
928
static void zend_error_va(int type, zend_string *file, uint32_t lineno, const char *format, ...) /* {{{ */
929
216
{
930
216
  va_list args;
931
216
  va_start(args, format);
932
216
  zend_string *message = zend_vstrpprintf(0, format, args);
933
216
  zend_observer_error_notify(type, file, lineno, message);
934
216
  zend_error_cb(type, file, lineno, message);
935
216
  zend_string_release(message);
936
216
  va_end(args);
937
216
}
938
/* }}} */
939
940
/* This function doesn't return if it uses E_ERROR */
941
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severity) /* {{{ */
942
238
{
943
238
  zval exception, rv;
944
238
  zend_class_entry *ce_exception;
945
238
  zend_result result = FAILURE;
946
947
238
  ZVAL_OBJ(&exception, ex);
948
238
  ce_exception = ex->ce;
949
238
  EG(exception) = NULL;
950
951
238
  zval_ptr_dtor(&EG(last_fatal_error_backtrace));
952
238
  ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
953
954
238
  if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
955
9
    zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
956
9
    zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
957
9
    zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
958
9
    int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;
959
960
9
    zend_observer_error_notify(type, file, line, message);
961
9
    zend_error_cb(type, file, line, message);
962
963
9
    zend_string_release_ex(file, 0);
964
9
    zend_string_release_ex(message, 0);
965
229
  } else if (instanceof_function(ce_exception, zend_ce_throwable)) {
966
216
    zval tmp;
967
216
    zend_string *str, *file = NULL;
968
216
    zend_long line = 0;
969
970
216
    zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
971
216
    if (!EG(exception)) {
972
216
      if (UNEXPECTED(Z_ISREF(tmp))) {
973
0
        zend_unwrap_reference(&tmp);
974
0
      }
975
216
      if (Z_TYPE(tmp) != IS_STRING) {
976
0
        zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
977
216
      } else {
978
216
        zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
979
216
      }
980
216
    }
981
216
    zval_ptr_dtor(&tmp);
982
983
216
    if (EG(exception)) {
984
0
      zval zv;
985
986
0
      ZVAL_OBJ(&zv, EG(exception));
987
      /* do the best we can to inform about the inner exception */
988
0
      if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
989
0
        file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
990
0
        line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
991
0
      }
992
993
0
      zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
994
0
        "Uncaught %s in exception handling during call to %s::__toString()",
995
0
        ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
996
997
0
      if (file) {
998
0
        zend_string_release_ex(file, 0);
999
0
      }
1000
0
    }
1001
1002
216
    str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
1003
216
    file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
1004
216
    line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
1005
1006
216
    zend_error_va(severity | E_DONT_BAIL,
1007
216
      (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
1008
216
      "Uncaught %S\n  thrown", str);
1009
1010
216
    zend_string_release_ex(str, 0);
1011
216
    zend_string_release_ex(file, 0);
1012
216
  } else if (ce_exception == &zend_ce_unwind_exit || ce_exception == &zend_ce_graceful_exit) {
1013
    /* We successfully unwound, nothing more to do.
1014
     * We still return FAILURE in this case, as further execution should still be aborted. */
1015
13
  } else {
1016
0
    zend_error(severity, "Uncaught exception %s", ZSTR_VAL(ce_exception->name));
1017
0
  }
1018
1019
238
  OBJ_RELEASE(ex);
1020
238
  return result;
1021
238
}
1022
/* }}} */
1023
1024
38
ZEND_NORETURN void zend_exception_uncaught_error(const char *format, ...) {
1025
38
  va_list va;
1026
38
  va_start(va, format);
1027
38
  zend_string *prefix = zend_vstrpprintf(0, format, va);
1028
38
  va_end(va);
1029
1030
38
  ZEND_ASSERT(EG(exception));
1031
38
  zval exception_zv;
1032
38
  ZVAL_OBJ_COPY(&exception_zv, EG(exception));
1033
38
  zend_clear_exception();
1034
1035
38
  zend_string *exception_str = zval_get_string(&exception_zv);
1036
38
  zend_error_noreturn(E_ERROR,
1037
38
    "%s: Uncaught %s", ZSTR_VAL(prefix), ZSTR_VAL(exception_str));
1038
38
}
1039
1040
ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
1041
3.49k
{
1042
3.49k
  if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
1043
0
    zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
1044
0
  }
1045
1046
3.49k
  zend_class_entry *exception_ce = Z_OBJCE_P(exception);
1047
1048
3.49k
  if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
1049
5
    zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1050
5
    zval_ptr_dtor(exception);
1051
5
    return;
1052
5
  }
1053
1054
3.49k
  zend_throw_exception_internal(Z_OBJ_P(exception));
1055
3.49k
}
1056
/* }}} */
1057
1058
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void)
1059
133
{
1060
133
  return zend_objects_new(&zend_ce_unwind_exit);
1061
133
}
1062
1063
ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void)
1064
351
{
1065
351
  return zend_objects_new(&zend_ce_graceful_exit);
1066
351
}
1067
1068
ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
1069
133
{
1070
133
  ZEND_ASSERT(!EG(exception));
1071
133
  EG(exception) = zend_create_unwind_exit();
1072
133
  EG(opline_before_exception) = EG(current_execute_data)->opline;
1073
133
  EG(current_execute_data)->opline = EG(exception_op);
1074
133
}
1075
1076
ZEND_API ZEND_COLD void zend_throw_graceful_exit(void)
1077
0
{
1078
0
  ZEND_ASSERT(!EG(exception));
1079
0
  EG(exception) = zend_create_graceful_exit();
1080
0
  EG(opline_before_exception) = EG(current_execute_data)->opline;
1081
0
  EG(current_execute_data)->opline = EG(exception_op);
1082
0
}
1083
1084
ZEND_API bool zend_is_unwind_exit(const zend_object *ex)
1085
294k
{
1086
294k
  return ex->ce == &zend_ce_unwind_exit;
1087
294k
}
1088
1089
ZEND_API bool zend_is_graceful_exit(const zend_object *ex)
1090
147k
{
1091
147k
  return ex->ce == &zend_ce_graceful_exit;
1092
147k
}