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
36
#define ZEND_EXCEPTION_MESSAGE_OFF 0
33
3
#define ZEND_EXCEPTION_CODE_OFF 2
34
18.9k
#define ZEND_EXCEPTION_FILE_OFF 3
35
18.9k
#define ZEND_EXCEPTION_LINE_OFF 4
36
18.9k
#define ZEND_EXCEPTION_TRACE_OFF 5
37
2
#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
108
{
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
108
  const zend_class_entry *root = class_type;
69
263
  while (root->parent) {
70
155
    root = root->parent;
71
155
  }
72
108
  if (zend_string_equals_literal(root->name, "Exception")
73
106
      || zend_string_equals_literal(root->name, "Error")) {
74
106
    return SUCCESS;
75
106
  }
76
77
108
  bool can_extend = (class_type->ce_flags & ZEND_ACC_ENUM) == 0;
78
79
2
  zend_error_noreturn(E_ERROR,
80
2
    can_extend
81
2
      ? "%s %s cannot implement interface %s, extend Exception or Error instead"
82
2
      : "%s %s cannot implement interface %s",
83
2
    zend_get_object_type_uc(class_type),
84
2
    ZSTR_VAL(class_type->name),
85
2
    ZSTR_VAL(interface->name));
86
0
  return FAILURE;
87
108
}
88
/* }}} */
89
90
static inline zend_class_entry *i_get_exception_base(const zend_object *object) /* {{{ */
91
245k
{
92
245k
  return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
93
245k
}
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
19.1k
{
104
19.1k
  zval *previous, *ancestor, *ex;
105
19.1k
  zval  pv, zv, rv;
106
19.1k
  zend_class_entry *base_ce;
107
108
19.1k
  if (!exception || !add_previous) {
109
12.3k
    return;
110
12.3k
  }
111
112
6.85k
  if (exception == add_previous || zend_is_unwind_exit(add_previous) || zend_is_graceful_exit(add_previous)) {
113
3
    OBJ_RELEASE(add_previous);
114
3
    return;
115
3
  }
116
117
6.85k
  ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
118
6.85k
    && "Previous exception must implement Throwable");
119
120
6.85k
  ZVAL_OBJ(&pv, add_previous);
121
6.85k
  ZVAL_OBJ(&zv, exception);
122
6.85k
  ex = &zv;
123
6.91k
  do {
124
6.91k
    ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
125
6.91k
    ZVAL_DEREF(ancestor);
126
149k
    while (Z_TYPE_P(ancestor) == IS_OBJECT) {
127
142k
      if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
128
1
        OBJ_RELEASE(add_previous);
129
1
        return;
130
1
      }
131
142k
      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
142k
      ZVAL_DEREF(ancestor);
133
142k
    }
134
6.91k
    base_ce = i_get_exception_base(Z_OBJ_P(ex));
135
6.91k
    previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
136
6.91k
    ZVAL_DEREF(previous);
137
6.91k
    if (Z_TYPE_P(previous) == IS_NULL) {
138
6.85k
      zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
139
6.85k
      GC_DELREF(add_previous);
140
6.85k
      return;
141
6.85k
    }
142
59
    ex = previous;
143
59
  } while (Z_OBJ_P(ex) != add_previous);
144
6.85k
}
145
/* }}} */
146
147
6.43k
static zend_always_inline bool is_handle_exception_set(void) {
148
6.43k
  zend_execute_data *execute_data = EG(current_execute_data);
149
6.43k
  return !execute_data
150
6.43k
    || !execute_data->func
151
6.43k
    || !ZEND_USER_CODE(execute_data->func->common.type)
152
2.51k
    || execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
153
6.43k
}
154
155
ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
156
19.8k
{
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
19.8k
  if (exception != NULL) {
168
19.1k
    const zend_object *previous = EG(exception);
169
19.1k
    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
19.1k
    zend_exception_set_previous(exception, EG(exception));
176
19.1k
    EG(exception) = exception;
177
19.1k
    if (previous) {
178
6.82k
      return;
179
6.82k
    }
180
19.1k
  }
181
12.9k
  if (!EG(current_execute_data)) {
182
6.55k
    if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
183
5.88k
      return;
184
5.88k
    }
185
672
    if (EG(exception)) {
186
672
      if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF
187
2
       && !zend_is_unwind_exit(EG(exception))
188
2
       && !zend_is_graceful_exit(EG(exception))) {
189
2
        zend_user_exception_handler();
190
2
        if (EG(exception)) {
191
0
          zend_exception_error(EG(exception), E_ERROR);
192
0
        }
193
2
        return;
194
670
      } else {
195
670
        zend_exception_error(EG(exception), E_ERROR);
196
670
      }
197
672
      zend_bailout();
198
672
    }
199
0
    zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
200
672
  }
201
202
6.43k
  if (zend_throw_exception_hook) {
203
0
    zend_throw_exception_hook(exception);
204
0
  }
205
206
6.43k
  if (is_handle_exception_set()) {
207
    /* no need to rethrow the exception */
208
3.92k
    return;
209
3.92k
  }
210
2.51k
  EG(opline_before_exception) = EG(current_execute_data)->opline;
211
2.51k
  EG(current_execute_data)->opline = EG(exception_op);
212
2.51k
}
213
/* }}} */
214
215
ZEND_API void zend_clear_exception(void) /* {{{ */
216
7.49k
{
217
7.49k
  zend_object *exception;
218
7.49k
  if (!EG(exception)) {
219
659
    return;
220
659
  }
221
  /* exception may have destructor */
222
6.83k
  exception = EG(exception);
223
6.83k
  EG(exception) = NULL;
224
6.83k
  OBJ_RELEASE(exception);
225
6.83k
  if (EG(current_execute_data)) {
226
3.07k
    EG(current_execute_data)->opline = EG(opline_before_exception);
227
3.07k
  }
228
6.83k
#if ZEND_DEBUG
229
6.83k
  EG(opline_before_exception) = NULL;
230
6.83k
#endif
231
6.83k
}
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
56.9k
{
239
56.9k
  if (UNEXPECTED(object->ce->num_hooked_props > 0)) {
240
    /* Property may have been overridden with a hook. */
241
10
    zend_update_property_ex(scope != NULL ? scope : object->ce, object, member, value);
242
10
    zval_ptr_dtor(value);
243
10
    return;
244
10
  }
245
56.9k
#if ZEND_DEBUG
246
56.9k
  const zend_class_entry *old_scope = EG(fake_scope);
247
56.9k
  EG(fake_scope) = i_get_exception_base(object);
248
56.9k
  const zend_property_info *prop_info = zend_get_property_info(object->ce, member, true);
249
56.9k
  ZEND_ASSERT(OBJ_PROP_TO_NUM(prop_info->offset) == prop_num);
250
56.9k
  EG(fake_scope) = old_scope;
251
56.9k
#endif
252
56.9k
  zval *zv = OBJ_PROP_NUM(object, prop_num);
253
56.9k
  zval_ptr_safe_dtor(zv);
254
56.9k
  ZVAL_COPY_VALUE(zv, value);
255
56.9k
}
256
257
static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
258
18.9k
{
259
18.9k
  zval tmp;
260
18.9k
  zval trace;
261
18.9k
  zend_string *filename;
262
263
18.9k
  zend_object *object = zend_objects_new(class_type);
264
18.9k
  object_properties_init(object, class_type);
265
266
18.9k
  if (EG(current_execute_data)) {
267
13.1k
    zend_fetch_debug_backtrace(&trace,
268
13.1k
      0,
269
13.1k
      EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
270
13.1k
  } else {
271
5.88k
    ZVAL_EMPTY_ARRAY(&trace);
272
5.88k
  }
273
274
18.9k
  zend_update_property_num_checked(i_get_exception_base(object), object, ZEND_EXCEPTION_TRACE_OFF, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
275
276
18.9k
  if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
277
18.9k
      || !(filename = zend_get_compiled_filename()))) {
278
7.25k
    ZVAL_STRING(&tmp, zend_get_executed_filename());
279
7.25k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
280
7.25k
    ZVAL_LONG(&tmp, zend_get_executed_lineno());
281
7.25k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
282
11.7k
  } else {
283
11.7k
    ZVAL_STR_COPY(&tmp, filename);
284
11.7k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_FILE_OFF, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
285
11.7k
    ZVAL_LONG(&tmp, zend_get_compiled_lineno());
286
11.7k
    zend_update_property_num_checked(NULL, object, ZEND_EXCEPTION_LINE_OFF, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
287
11.7k
  }
288
289
18.9k
  return object;
290
18.9k
}
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
162
{
303
162
  zval tmp, *object = ZEND_THIS;
304
305
162
  if (message) {
306
36
    ZVAL_STR_COPY(&tmp, message);
307
36
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
308
36
    if (UNEXPECTED(EG(exception))) {
309
0
      return FAILURE;
310
0
    }
311
36
  }
312
313
162
  if (code) {
314
3
    ZVAL_LONG(&tmp, code);
315
3
    zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
316
3
    if (UNEXPECTED(EG(exception))) {
317
0
      return FAILURE;
318
0
    }
319
3
  }
320
321
162
  if (previous) {
322
2
    Z_ADDREF_P(previous);
323
2
    zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
324
2
    if (UNEXPECTED(EG(exception))) {
325
0
      return FAILURE;
326
0
    }
327
2
  }
328
329
162
  return SUCCESS;
330
162
}
331
332
/* {{{ Exception constructor */
333
ZEND_METHOD(Exception, __construct)
334
161
{
335
161
  zend_string *message = NULL;
336
161
  zend_long   code = 0;
337
161
  zval *previous = NULL;
338
339
161
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
340
3
    RETURN_THROWS();
341
3
  }
342
343
158
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
344
0
    RETURN_THROWS();
345
0
  }
346
158
}
347
/* }}} */
348
349
/* {{{ Exception unserialize checks */
350
#define CHECK_EXC_TYPE(id, type) \
351
2
  pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
352
2
  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
1
{
358
1
  ZEND_PARSE_PARAMETERS_NONE();
359
360
1
  zval value, *pvalue;
361
1
  zval *object = ZEND_THIS;
362
1
  CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
363
1
  CHECK_EXC_TYPE(ZEND_STR_CODE,    IS_LONG);
364
  /* The type of all other properties is enforced through typed properties. */
365
1
}
366
/* }}} */
367
368
/* {{{ ErrorException constructor */
369
ZEND_METHOD(ErrorException, __construct)
370
3
{
371
3
  zend_string *message = NULL, *filename = NULL;
372
3
  zend_long   code = 0, severity = E_ERROR, lineno;
373
3
  bool lineno_is_null = true;
374
3
  zval   tmp, *object, *previous = NULL;
375
376
3
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
377
1
    RETURN_THROWS();
378
1
  }
379
380
2
  object = ZEND_THIS;
381
382
2
  if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
383
0
    RETURN_THROWS();
384
0
  }
385
386
2
  ZVAL_LONG(&tmp, severity);
387
2
  zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
388
2
  if (UNEXPECTED(EG(exception))) {
389
0
    RETURN_THROWS();
390
0
  }
391
392
2
  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
2
  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
2
  } 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
2
}
414
/* }}} */
415
416
#define GET_PROPERTY(object, id) \
417
11.5k
  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
2.02k
  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
2
{
424
2
  zval *prop, rv;
425
426
2
  ZEND_PARSE_PARAMETERS_NONE();
427
428
2
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
429
2
  RETURN_STR(zval_get_string(prop));
430
2
}
431
/* }}} */
432
433
/* {{{ Get the line in which the exception occurred */
434
ZEND_METHOD(Exception, getLine)
435
2
{
436
2
  zval *prop, rv;
437
438
2
  ZEND_PARSE_PARAMETERS_NONE();
439
440
2
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
441
2
  RETURN_LONG(zval_get_long(prop));
442
2
}
443
/* }}} */
444
445
/* {{{ Get the exception message */
446
ZEND_METHOD(Exception, getMessage)
447
112
{
448
112
  zval *prop, rv;
449
450
112
  ZEND_PARSE_PARAMETERS_NONE();
451
452
112
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
453
112
  RETURN_STR(zval_get_string(prop));
454
112
}
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
3
{
473
3
  zval *prop, rv;
474
475
3
  ZEND_PARSE_PARAMETERS_NONE();
476
477
3
  prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
478
3
  ZVAL_DEREF(prop);
479
3
  ZVAL_COPY(return_value, prop);
480
3
}
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
1.47k
#define TRACE_APPEND_KEY(key) do {                                          \
497
1.47k
    tmp = zend_hash_find(ht, key);                                      \
498
1.47k
    if (tmp) {                                                          \
499
1.42k
      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
1.42k
      } else {                                                        \
504
1.42k
        smart_str_append(str, Z_STR_P(tmp));                        \
505
1.42k
      }                                                               \
506
1.42k
    } \
507
1.47k
  } while (0)
508
509
static void build_trace_args(zval *arg, smart_str *str) /* {{{ */
510
1.42k
{
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
1.42k
  ZVAL_DEREF(arg);
518
519
1.42k
  if (smart_str_append_zval(str, arg, EG(exception_string_param_max_len)) != SUCCESS) {
520
675
    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
661
      case IS_ARRAY:
526
661
        smart_str_appends(str, "Array");
527
661
        break;
528
14
      case IS_OBJECT: {
529
14
        zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
530
14
        smart_str_appends(str, "Object(");
531
        /* cut off on NULL byte ... class@anonymous */
532
14
        smart_str_appends(str, ZSTR_VAL(class_name));
533
14
        smart_str_appends(str, ")");
534
14
        zend_string_release_ex(class_name, 0);
535
14
        break;
536
0
      }
537
675
    }
538
675
  }
539
1.42k
}
540
/* }}} */
541
542
static void build_trace_args_list(zval *tmp, smart_str *str) /* {{{ */
543
733
{
544
733
  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
733
  bool first = true;
551
3.57k
  ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), zend_string *name, zval *arg) {
552
3.57k
    if (!first) {
553
705
      smart_str_appends(str, ", ");
554
705
    }
555
3.57k
    first = false;
556
3.57k
    if (name) {
557
2
      smart_str_append(str, name);
558
2
      smart_str_appends(str, ": ");
559
2
    }
560
3.57k
    build_trace_args(arg, str);
561
3.57k
  } ZEND_HASH_FOREACH_END();
562
733
}
563
/* }}} */
564
565
static void build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
566
737
{
567
737
  zval *file, *tmp;
568
569
737
  smart_str_appendc(str, '#');
570
737
  smart_str_append_long(str, num);
571
737
  smart_str_appendc(str, ' ');
572
573
737
  file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
574
737
  if (file) {
575
64
    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
64
    } else{
579
64
      zend_long line = 0;
580
64
      tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
581
64
      if (tmp) {
582
64
        if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
583
64
          line = Z_LVAL_P(tmp);
584
64
        } else {
585
0
          zend_error(E_WARNING, "Line is not an int");
586
0
        }
587
64
      }
588
64
      smart_str_append(str, Z_STR_P(file));
589
64
      smart_str_appendc(str, '(');
590
64
      smart_str_append_long(str, line);
591
64
      smart_str_appends(str, "): ");
592
64
    }
593
673
  } else {
594
673
    smart_str_appends(str, "[internal function]: ");
595
673
  }
596
737
  const zval *class_name = zend_hash_find(ht, ZSTR_KNOWN(ZEND_STR_CLASS));
597
737
  if (class_name) {
598
690
    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
690
    } else {
602
      /* cut off on NULL byte ... class@anonymous */
603
690
      smart_str_appends(str, Z_STRVAL_P(class_name));
604
690
    }
605
690
  }
606
737
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
607
737
  TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
608
737
  smart_str_appendc(str, '(');
609
737
  tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
610
737
  if (tmp) {
611
733
    build_trace_args_list(tmp, str);
612
733
  }
613
737
  smart_str_appends(str, ")\n");
614
737
}
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
1.39k
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
649
1.39k
  zend_ulong index;
650
1.39k
  zval *frame;
651
1.39k
  uint32_t num = 0;
652
1.39k
  smart_str str = {0};
653
654
2.87k
  ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
655
2.87k
    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
737
    build_trace_string(&str, Z_ARRVAL_P(frame), num++);
661
737
  } ZEND_HASH_FOREACH_END();
662
663
1.39k
  if (include_main) {
664
1.38k
    smart_str_appendc(&str, '#');
665
1.38k
    smart_str_append_long(&str, num);
666
1.38k
    smart_str_appends(&str, " {main}");
667
1.38k
  }
668
669
1.39k
  smart_str_0(&str);
670
1.39k
  return str.s ? str.s : ZSTR_EMPTY_ALLOC();
671
1.39k
}
672
673
/* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
674
ZEND_METHOD(Exception, getTraceAsString)
675
702
{
676
677
702
  ZEND_PARSE_PARAMETERS_NONE();
678
679
702
  zval *object = ZEND_THIS;
680
702
  zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
681
702
  zval rv;
682
702
  const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
683
702
  if (EG(exception)) {
684
0
    RETURN_THROWS();
685
0
  }
686
687
702
  ZVAL_DEREF(trace);
688
  /* Type should be guaranteed by property type. */
689
702
  ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
690
702
  RETURN_NEW_STR(zend_trace_to_string(Z_ARRVAL_P(trace), /* include_main */ true));
691
702
}
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
2.67k
{
707
2.67k
  zval trace, *exception;
708
2.67k
  zend_class_entry *base_ce;
709
2.67k
  zend_string *str;
710
2.67k
  zval rv, tmp;
711
712
2.67k
  ZEND_PARSE_PARAMETERS_NONE();
713
714
2.67k
  str = ZSTR_EMPTY_ALLOC();
715
716
2.67k
  exception = ZEND_THIS;
717
2.67k
  base_ce = i_get_exception_base(Z_OBJ_P(exception));
718
719
  /* As getTraceAsString method is final we can grab it once */
720
2.67k
  zend_function *getTraceAsString = zend_hash_str_find_ptr(&base_ce->function_table, ZEND_STRL("gettraceasstring"));
721
2.67k
  ZEND_ASSERT(getTraceAsString && "Method getTraceAsString must exist");
722
723
724
2.67k
  zend_fcall_info fci;
725
2.67k
  fci.size = sizeof(fci);
726
2.67k
  ZVAL_UNDEF(&fci.function_name);
727
2.67k
  fci.retval = &trace;
728
2.67k
  fci.param_count = 0;
729
2.67k
  fci.params = NULL;
730
2.67k
  fci.object = NULL;
731
2.67k
  fci.named_params = NULL;
732
733
2.67k
  zend_fcall_info_cache fcc;
734
2.67k
  fcc.function_handler = getTraceAsString;
735
2.67k
  fcc.called_scope = base_ce;
736
2.67k
  fcc.closure = NULL;
737
738
5.35k
  while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
739
2.68k
    zend_string *prev_str = str;
740
2.68k
    zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
741
2.68k
    zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
742
2.68k
    zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
743
744
2.68k
    fcc.object = Z_OBJ_P(exception);
745
2.68k
    fcc.calling_scope = Z_OBJCE_P(exception);
746
2.68k
    zend_call_function(&fci, &fcc);
747
748
2.68k
    if (Z_TYPE(trace) != IS_STRING) {
749
0
      zval_ptr_dtor(&trace);
750
0
      ZVAL_UNDEF(&trace);
751
0
    }
752
753
2.68k
    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
2.68k
    zend_string *tmp_trace = (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace))
760
2.68k
      ? zend_string_copy(Z_STR(trace))
761
2.68k
      : ZSTR_INIT_LITERAL("#0 {main}\n", false);
762
763
2.68k
    zend_string *name = Z_OBJCE_P(exception)->name;
764
765
2.68k
    if (ZSTR_LEN(message) > 0) {
766
697
      zval message_zv;
767
697
      ZVAL_STR(&message_zv, message);
768
769
697
      str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
770
697
        name, message, file, line,
771
697
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
772
1.98k
    } else {
773
1.98k
      str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
774
1.98k
        name, file, line,
775
1.98k
        tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
776
1.98k
    }
777
2.68k
    zend_string_release_ex(tmp_trace, false);
778
779
2.68k
    zend_string_release_ex(prev_str, 0);
780
2.68k
    zend_string_release_ex(message, 0);
781
2.68k
    zend_string_release_ex(file, 0);
782
2.68k
    zval_ptr_dtor(&trace);
783
784
2.68k
    Z_PROTECT_RECURSION_P(exception);
785
2.68k
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
786
2.68k
    ZVAL_DEREF(exception);
787
2.68k
    if (Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
788
0
      break;
789
0
    }
790
2.68k
  }
791
792
2.67k
  exception = ZEND_THIS;
793
  /* Reset apply counts */
794
2.67k
  zend_class_entry *previous_base_ce;
795
3.37k
  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
700
    if (Z_IS_RECURSIVE_P(exception)) {
797
700
      Z_UNPROTECT_RECURSION_P(exception);
798
700
    } else {
799
0
      break;
800
0
    }
801
700
    exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
802
700
    ZVAL_DEREF(exception);
803
700
  }
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
2.67k
  ZVAL_STR(&tmp, str);
808
2.67k
  zend_update_property_ex(base_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
809
810
2.67k
  RETURN_STR(str);
811
2.67k
}
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
18.8k
{
871
18.8k
  zval ex, tmp;
872
873
18.8k
  if (!exception_ce) {
874
4
    exception_ce = zend_ce_exception;
875
4
  }
876
877
18.8k
  ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
878
18.8k
    && "Exceptions must implement Throwable");
879
880
18.8k
  object_init_ex(&ex, exception_ce);
881
882
18.8k
  if (message) {
883
18.8k
    ZVAL_STR(&tmp, message);
884
18.8k
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
885
18.8k
  }
886
18.8k
  if (code) {
887
23
    ZVAL_LONG(&tmp, code);
888
23
    zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
889
23
  }
890
891
18.8k
  zend_throw_exception_internal(Z_OBJ(ex));
892
893
18.8k
  return Z_OBJ(ex);
894
18.8k
}
895
/* }}} */
896
897
ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
898
14.9k
{
899
14.9k
  zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
900
14.9k
  zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
901
14.9k
  if (msg_str) {
902
14.9k
    zend_string_release(msg_str);
903
14.9k
  }
904
14.9k
  return ex;
905
14.9k
}
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
3.87k
{
910
3.87k
  va_list arg;
911
3.87k
  zend_object *obj;
912
913
3.87k
  va_start(arg, format);
914
3.87k
  zend_string *msg_str = zend_vstrpprintf(0, format, arg);
915
3.87k
  va_end(arg);
916
3.87k
  obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
917
3.87k
  zend_string_release(msg_str);
918
3.87k
  return obj;
919
3.87k
}
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
673
{
936
673
  va_list args;
937
673
  va_start(args, format);
938
673
  zend_string *message = zend_vstrpprintf(0, format, args);
939
673
  zend_observer_error_notify(type, file, lineno, message);
940
673
  zend_error_cb(type, file, lineno, message);
941
673
  zend_string_release(message);
942
673
  va_end(args);
943
673
}
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
675
{
949
675
  zval exception, rv;
950
675
  zend_class_entry *ce_exception;
951
675
  zend_result result = FAILURE;
952
953
675
  ZVAL_OBJ(&exception, ex);
954
675
  ce_exception = ex->ce;
955
675
  EG(exception) = NULL;
956
957
675
  zval_ptr_dtor(&EG(last_fatal_error_backtrace));
958
675
  ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
959
960
675
  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
675
  } else if (instanceof_function(ce_exception, zend_ce_throwable)) {
972
675
    zval tmp;
973
675
    zend_string *str, *file = NULL;
974
675
    zend_long line = 0;
975
976
675
    zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
977
675
    if (!EG(exception)) {
978
673
      if (UNEXPECTED(Z_ISREF(tmp))) {
979
0
        zend_unwrap_reference(&tmp);
980
0
      }
981
673
      if (Z_TYPE(tmp) != IS_STRING) {
982
0
        zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
983
673
      } else {
984
673
        zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
985
673
      }
986
673
    }
987
675
    zval_ptr_dtor(&tmp);
988
989
675
    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
675
    str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
1009
675
    file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
1010
675
    line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
1011
1012
675
    zend_error_va(severity | E_DONT_BAIL,
1013
675
      (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
1014
675
      "Uncaught %S\n  thrown", str);
1015
1016
675
    zend_string_release_ex(str, 0);
1017
675
    zend_string_release_ex(file, 0);
1018
675
  } 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
675
  OBJ_RELEASE(ex);
1026
675
  return result;
1027
675
}
1028
/* }}} */
1029
1030
1
ZEND_NORETURN void zend_exception_uncaught_error(const char *format, ...) {
1031
1
  va_list va;
1032
1
  va_start(va, format);
1033
1
  zend_string *prefix = zend_vstrpprintf(0, format, va);
1034
1
  va_end(va);
1035
1036
1
  ZEND_ASSERT(EG(exception));
1037
1
  zval exception_zv;
1038
1
  ZVAL_OBJ_COPY(&exception_zv, EG(exception));
1039
1
  zend_clear_exception();
1040
1041
1
  zend_string *exception_str = zval_get_string(&exception_zv);
1042
1
  zend_error_noreturn(E_ERROR,
1043
1
    "%s: Uncaught %s", ZSTR_VAL(prefix), ZSTR_VAL(exception_str));
1044
1
}
1045
1046
ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
1047
53
{
1048
53
  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
53
  zend_class_entry *exception_ce = Z_OBJCE_P(exception);
1053
1054
53
  if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
1055
1
    zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1056
1
    zval_ptr_dtor(exception);
1057
1
    return;
1058
1
  }
1059
1060
52
  zend_throw_exception_internal(Z_OBJ_P(exception));
1061
52
}
1062
/* }}} */
1063
1064
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void)
1065
14
{
1066
14
  return zend_objects_new(&zend_ce_unwind_exit);
1067
14
}
1068
1069
ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void)
1070
275
{
1071
275
  return zend_objects_new(&zend_ce_graceful_exit);
1072
275
}
1073
1074
ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
1075
14
{
1076
14
  ZEND_ASSERT(!EG(exception));
1077
14
  EG(exception) = zend_create_unwind_exit();
1078
14
  EG(opline_before_exception) = EG(current_execute_data)->opline;
1079
14
  EG(current_execute_data)->opline = EG(exception_op);
1080
14
}
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
14.1k
{
1092
14.1k
  return ex->ce == &zend_ce_unwind_exit;
1093
14.1k
}
1094
1095
ZEND_API bool zend_is_graceful_exit(const zend_object *ex)
1096
7.42k
{
1097
7.42k
  return ex->ce == &zend_ce_graceful_exit;
1098
7.42k
}