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