/src/php-src/ext/standard/assert.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Thies C. Arntzen <thies@thieso.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | /* {{{ includes */ |
16 | | #include "php.h" |
17 | | #include "php_assert.h" |
18 | | #include "php_ini.h" |
19 | | #include "zend_exceptions.h" |
20 | | /* }}} */ |
21 | | |
22 | | ZEND_BEGIN_MODULE_GLOBALS(assert) |
23 | | zval callback; |
24 | | char *cb; |
25 | | bool active; |
26 | | bool bail; |
27 | | bool warning; |
28 | | bool exception; |
29 | | ZEND_END_MODULE_GLOBALS(assert) |
30 | | |
31 | | ZEND_DECLARE_MODULE_GLOBALS(assert) |
32 | | |
33 | 4 | #define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v) |
34 | | |
35 | | PHPAPI zend_class_entry *assertion_error_ce; |
36 | | |
37 | | /* Hack to pass a custom stage for the our OnModify handler so that a deprecation warning does not get emitted |
38 | | * when an option is modified via assert_option() function */ |
39 | 16 | #define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6) |
40 | | |
41 | | static inline bool php_must_emit_ini_deprecation(int stage) |
42 | 8 | { |
43 | 8 | return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS; |
44 | 8 | } |
45 | | |
46 | | static PHP_INI_MH(OnChangeCallback) /* {{{ */ |
47 | 2 | { |
48 | 2 | if (EG(current_execute_data)) { |
49 | 0 | if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) { |
50 | 0 | zval_ptr_dtor(&ASSERTG(callback)); |
51 | 0 | ZVAL_UNDEF(&ASSERTG(callback)); |
52 | 0 | } |
53 | 0 | if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) { |
54 | 0 | if (php_must_emit_ini_deprecation(stage)) { |
55 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated"); |
56 | 0 | } |
57 | 0 | ZVAL_STR_COPY(&ASSERTG(callback), new_value); |
58 | 0 | } |
59 | 2 | } else { |
60 | 2 | if (ASSERTG(cb)) { |
61 | 0 | pefree(ASSERTG(cb), 1); |
62 | 0 | } |
63 | 2 | if (new_value && ZSTR_LEN(new_value)) { |
64 | 0 | if (php_must_emit_ini_deprecation(stage)) { |
65 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated"); |
66 | 0 | } |
67 | 0 | ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1); |
68 | 0 | memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value)); |
69 | 0 | ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0'; |
70 | 2 | } else { |
71 | 2 | ASSERTG(cb) = NULL; |
72 | 2 | } |
73 | 2 | } |
74 | 2 | return SUCCESS; |
75 | 2 | } |
76 | | /* }}} */ |
77 | | |
78 | | static PHP_INI_MH(OnUpdateActiveBool) |
79 | 2 | { |
80 | 2 | bool *p = ZEND_INI_GET_ADDR(); |
81 | 2 | *p = zend_ini_parse_bool(new_value); |
82 | 2 | if (php_must_emit_ini_deprecation(stage) && !*p) { |
83 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.active INI setting is deprecated"); |
84 | 0 | } |
85 | 2 | return SUCCESS; |
86 | 2 | } |
87 | | |
88 | | static PHP_INI_MH(OnUpdateBailBool) |
89 | 2 | { |
90 | 2 | bool *p = ZEND_INI_GET_ADDR(); |
91 | 2 | *p = zend_ini_parse_bool(new_value); |
92 | 2 | if (php_must_emit_ini_deprecation(stage) && *p) { |
93 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.bail INI setting is deprecated"); |
94 | 0 | } |
95 | 2 | return SUCCESS; |
96 | 2 | } |
97 | | |
98 | | static PHP_INI_MH(OnUpdateExceptionBool) |
99 | 2 | { |
100 | 2 | bool *p = ZEND_INI_GET_ADDR(); |
101 | 2 | *p = zend_ini_parse_bool(new_value); |
102 | 2 | if (php_must_emit_ini_deprecation(stage) && !*p) { |
103 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.exception INI setting is deprecated"); |
104 | 0 | } |
105 | 2 | return SUCCESS; |
106 | 2 | } |
107 | | |
108 | | |
109 | | static PHP_INI_MH(OnUpdateWarningBool) |
110 | 2 | { |
111 | 2 | bool *p = ZEND_INI_GET_ADDR(); |
112 | 2 | *p = zend_ini_parse_bool(new_value); |
113 | 2 | if (php_must_emit_ini_deprecation(stage) && !*p) { |
114 | 0 | php_error_docref(NULL, E_DEPRECATED, "assert.warning INI setting is deprecated"); |
115 | 0 | } |
116 | 2 | return SUCCESS; |
117 | 2 | } |
118 | | |
119 | | |
120 | | PHP_INI_BEGIN() |
121 | | STD_PHP_INI_BOOLEAN("assert.active", "1", PHP_INI_ALL, OnUpdateActiveBool, active, zend_assert_globals, assert_globals) |
122 | | STD_PHP_INI_BOOLEAN("assert.bail", "0", PHP_INI_ALL, OnUpdateBailBool, bail, zend_assert_globals, assert_globals) |
123 | | STD_PHP_INI_BOOLEAN("assert.warning", "1", PHP_INI_ALL, OnUpdateWarningBool, warning, zend_assert_globals, assert_globals) |
124 | | PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback) |
125 | | STD_PHP_INI_BOOLEAN("assert.exception", "1", PHP_INI_ALL, OnUpdateExceptionBool, exception, zend_assert_globals, assert_globals) |
126 | | PHP_INI_END() |
127 | | |
128 | | static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */ |
129 | 2 | { |
130 | 2 | ZVAL_UNDEF(&assert_globals_p->callback); |
131 | 2 | assert_globals_p->cb = NULL; |
132 | 2 | } |
133 | | /* }}} */ |
134 | | |
135 | | PHP_MINIT_FUNCTION(assert) /* {{{ */ |
136 | 2 | { |
137 | 2 | ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL); |
138 | | |
139 | 2 | REGISTER_INI_ENTRIES(); |
140 | | |
141 | 2 | return SUCCESS; |
142 | 2 | } |
143 | | /* }}} */ |
144 | | |
145 | | PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */ |
146 | 0 | { |
147 | 0 | if (ASSERTG(cb)) { |
148 | 0 | pefree(ASSERTG(cb), 1); |
149 | 0 | ASSERTG(cb) = NULL; |
150 | 0 | } |
151 | 0 | return SUCCESS; |
152 | 0 | } |
153 | | /* }}} */ |
154 | | |
155 | | PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */ |
156 | 6.77k | { |
157 | 6.77k | if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) { |
158 | 0 | zval_ptr_dtor(&ASSERTG(callback)); |
159 | 0 | ZVAL_UNDEF(&ASSERTG(callback)); |
160 | 0 | } |
161 | | |
162 | 6.77k | return SUCCESS; |
163 | 6.77k | } |
164 | | /* }}} */ |
165 | | |
166 | | PHP_MINFO_FUNCTION(assert) /* {{{ */ |
167 | 0 | { |
168 | 0 | DISPLAY_INI_ENTRIES(); |
169 | 0 | } |
170 | | /* }}} */ |
171 | | |
172 | | /* {{{ Checks if assertion is false */ |
173 | | PHP_FUNCTION(assert) |
174 | 0 | { |
175 | 0 | zval *assertion; |
176 | 0 | zend_string *description_str = NULL; |
177 | 0 | zend_object *description_obj = NULL; |
178 | | |
179 | | /* EG(assertions) <= 0 is only reachable by dynamic calls to assert(), |
180 | | * since calls known at compile time will skip the entire call when |
181 | | * assertions are disabled. |
182 | | */ |
183 | 0 | if (!ASSERTG(active) || EG(assertions) <= 0) { |
184 | 0 | RETURN_TRUE; |
185 | 0 | } |
186 | | |
187 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
188 | 0 | Z_PARAM_ZVAL(assertion) |
189 | 0 | Z_PARAM_OPTIONAL |
190 | 0 | Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str) |
191 | 0 | ZEND_PARSE_PARAMETERS_END(); |
192 | | |
193 | 0 | if (zend_is_true(assertion)) { |
194 | 0 | RETURN_TRUE; |
195 | 0 | } |
196 | | |
197 | 0 | if (description_obj) { |
198 | 0 | GC_ADDREF(description_obj); |
199 | 0 | zend_throw_exception_internal(description_obj); |
200 | 0 | RETURN_THROWS(); |
201 | 0 | } |
202 | | |
203 | 0 | if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) { |
204 | 0 | ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb)); |
205 | 0 | } |
206 | |
|
207 | 0 | if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) { |
208 | 0 | zval args[4]; |
209 | 0 | zval retval; |
210 | 0 | uint32_t lineno = zend_get_executed_lineno(); |
211 | 0 | zend_string *filename = zend_get_executed_filename_ex(); |
212 | 0 | if (UNEXPECTED(!filename)) { |
213 | 0 | filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED); |
214 | 0 | } |
215 | |
|
216 | 0 | ZVAL_STR(&args[0], filename); |
217 | 0 | ZVAL_LONG(&args[1], lineno); |
218 | 0 | ZVAL_NULL(&args[2]); |
219 | |
|
220 | 0 | ZVAL_FALSE(&retval); |
221 | |
|
222 | 0 | if (description_str) { |
223 | 0 | ZVAL_STR(&args[3], description_str); |
224 | 0 | call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args); |
225 | 0 | } else { |
226 | 0 | call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args); |
227 | 0 | } |
228 | |
|
229 | 0 | zval_ptr_dtor(&retval); |
230 | 0 | } |
231 | |
|
232 | 0 | if (ASSERTG(exception)) { |
233 | 0 | zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR); |
234 | 0 | if (ASSERTG(bail)) { |
235 | | /* When bail is turned on, the exception will not be caught. */ |
236 | 0 | zend_exception_error(EG(exception), E_ERROR); |
237 | 0 | } |
238 | 0 | } else if (ASSERTG(warning)) { |
239 | 0 | php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion"); |
240 | 0 | } |
241 | |
|
242 | 0 | if (ASSERTG(bail)) { |
243 | 0 | if (EG(exception)) { |
244 | | /* The callback might have thrown. Use E_WARNING to print the |
245 | | * exception so we can avoid bailout and use unwind_exit. */ |
246 | 0 | zend_exception_error(EG(exception), E_WARNING); |
247 | 0 | } |
248 | 0 | zend_throw_unwind_exit(); |
249 | 0 | RETURN_THROWS(); |
250 | 0 | } else { |
251 | 0 | RETURN_FALSE; |
252 | 0 | } |
253 | 0 | } |
254 | | /* }}} */ |
255 | | |
256 | | /* {{{ Set/get the various assert flags */ |
257 | | PHP_FUNCTION(assert_options) |
258 | 0 | { |
259 | 0 | zval *value = NULL; |
260 | 0 | zend_long what; |
261 | 0 | bool oldint; |
262 | 0 | uint32_t ac = ZEND_NUM_ARGS(); |
263 | 0 | zend_string *key; |
264 | |
|
265 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
266 | 0 | Z_PARAM_LONG(what) |
267 | 0 | Z_PARAM_OPTIONAL |
268 | 0 | Z_PARAM_ZVAL(value) |
269 | 0 | ZEND_PARSE_PARAMETERS_END(); |
270 | | |
271 | 0 | switch (what) { |
272 | 0 | case PHP_ASSERT_ACTIVE: |
273 | 0 | oldint = ASSERTG(active); |
274 | 0 | if (ac == 2) { |
275 | 0 | zend_string *value_str = zval_try_get_string(value); |
276 | 0 | if (UNEXPECTED(!value_str)) { |
277 | 0 | RETURN_THROWS(); |
278 | 0 | } |
279 | | |
280 | 0 | key = ZSTR_INIT_LITERAL("assert.active", 0); |
281 | 0 | zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0); |
282 | 0 | zend_string_release_ex(key, 0); |
283 | 0 | zend_string_release_ex(value_str, 0); |
284 | 0 | } |
285 | 0 | RETURN_LONG(oldint); |
286 | | |
287 | 0 | case PHP_ASSERT_BAIL: |
288 | 0 | oldint = ASSERTG(bail); |
289 | 0 | if (ac == 2) { |
290 | 0 | zend_string *value_str = zval_try_get_string(value); |
291 | 0 | if (UNEXPECTED(!value_str)) { |
292 | 0 | RETURN_THROWS(); |
293 | 0 | } |
294 | | |
295 | 0 | key = ZSTR_INIT_LITERAL("assert.bail", 0); |
296 | 0 | zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0); |
297 | 0 | zend_string_release_ex(key, 0); |
298 | 0 | zend_string_release_ex(value_str, 0); |
299 | 0 | } |
300 | 0 | RETURN_LONG(oldint); |
301 | | |
302 | 0 | case PHP_ASSERT_WARNING: |
303 | 0 | oldint = ASSERTG(warning); |
304 | 0 | if (ac == 2) { |
305 | 0 | zend_string *value_str = zval_try_get_string(value); |
306 | 0 | if (UNEXPECTED(!value_str)) { |
307 | 0 | RETURN_THROWS(); |
308 | 0 | } |
309 | | |
310 | 0 | key = ZSTR_INIT_LITERAL("assert.warning", 0); |
311 | 0 | zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0); |
312 | 0 | zend_string_release_ex(key, 0); |
313 | 0 | zend_string_release_ex(value_str, 0); |
314 | 0 | } |
315 | 0 | RETURN_LONG(oldint); |
316 | | |
317 | 0 | case PHP_ASSERT_CALLBACK: |
318 | 0 | if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) { |
319 | 0 | ZVAL_COPY(return_value, &ASSERTG(callback)); |
320 | 0 | } else if (ASSERTG(cb)) { |
321 | 0 | RETVAL_STRING(ASSERTG(cb)); |
322 | 0 | } else { |
323 | 0 | RETVAL_NULL(); |
324 | 0 | } |
325 | |
|
326 | 0 | if (ac == 2) { |
327 | 0 | zval_ptr_dtor(&ASSERTG(callback)); |
328 | 0 | if (Z_TYPE_P(value) == IS_NULL) { |
329 | 0 | ZVAL_UNDEF(&ASSERTG(callback)); |
330 | 0 | } else { |
331 | 0 | ZVAL_COPY(&ASSERTG(callback), value); |
332 | 0 | } |
333 | 0 | } |
334 | 0 | return; |
335 | | |
336 | 0 | case PHP_ASSERT_EXCEPTION: |
337 | 0 | oldint = ASSERTG(exception); |
338 | 0 | if (ac == 2) { |
339 | 0 | zend_string *val = zval_try_get_string(value); |
340 | 0 | if (UNEXPECTED(!val)) { |
341 | 0 | RETURN_THROWS(); |
342 | 0 | } |
343 | | |
344 | 0 | key = ZSTR_INIT_LITERAL("assert.exception", 0); |
345 | 0 | zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0); |
346 | 0 | zend_string_release_ex(val, 0); |
347 | 0 | zend_string_release_ex(key, 0); |
348 | 0 | } |
349 | 0 | RETURN_LONG(oldint); |
350 | | |
351 | 0 | default: |
352 | 0 | zend_argument_value_error(1, "must be an ASSERT_* constant"); |
353 | | RETURN_THROWS(); |
354 | 0 | } |
355 | 0 | } |
356 | | /* }}} */ |