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 | | | Zeev Suraski <zeev@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "zend.h" |
21 | | #include "zend_extensions.h" |
22 | | #include "zend_modules.h" |
23 | | #include "zend_constants.h" |
24 | | #include "zend_list.h" |
25 | | #include "zend_API.h" |
26 | | #include "zend_exceptions.h" |
27 | | #include "zend_builtin_functions.h" |
28 | | #include "zend_ini.h" |
29 | | #include "zend_vm.h" |
30 | | #include "zend_dtrace.h" |
31 | | #include "zend_virtual_cwd.h" |
32 | | #include "zend_smart_str.h" |
33 | | #include "zend_smart_string.h" |
34 | | #include "zend_cpuinfo.h" |
35 | | #include "zend_attributes.h" |
36 | | #include "zend_observer.h" |
37 | | #include "zend_fibers.h" |
38 | | #include "zend_call_stack.h" |
39 | | #include "zend_max_execution_timer.h" |
40 | | #include "zend_hrtime.h" |
41 | | #include "zend_enum.h" |
42 | | #include "zend_closures.h" |
43 | | #include "Optimizer/zend_optimizer.h" |
44 | | #include "php.h" |
45 | | #include "php_globals.h" |
46 | | |
47 | | // FIXME: Breaks the declaration of the function below |
48 | | #undef zenderror |
49 | | |
50 | | static size_t global_map_ptr_last = 0; |
51 | | static bool startup_done = false; |
52 | | |
53 | | #ifdef ZTS |
54 | | ZEND_API int compiler_globals_id; |
55 | | ZEND_API int executor_globals_id; |
56 | | ZEND_API size_t compiler_globals_offset; |
57 | | ZEND_API size_t executor_globals_offset; |
58 | | static HashTable *global_function_table = NULL; |
59 | | static HashTable *global_class_table = NULL; |
60 | | static HashTable *global_constants_table = NULL; |
61 | | static HashTable *global_auto_globals_table = NULL; |
62 | | static HashTable *global_persistent_list = NULL; |
63 | | TSRMLS_MAIN_CACHE_DEFINE() |
64 | | # define GLOBAL_FUNCTION_TABLE global_function_table |
65 | | # define GLOBAL_CLASS_TABLE global_class_table |
66 | | # define GLOBAL_CONSTANTS_TABLE global_constants_table |
67 | | # define GLOBAL_AUTO_GLOBALS_TABLE global_auto_globals_table |
68 | | #else |
69 | 14 | # define GLOBAL_FUNCTION_TABLE CG(function_table) |
70 | 14 | # define GLOBAL_CLASS_TABLE CG(class_table) |
71 | 14 | # define GLOBAL_AUTO_GLOBALS_TABLE CG(auto_globals) |
72 | 14 | # define GLOBAL_CONSTANTS_TABLE EG(zend_constants) |
73 | | #endif |
74 | | |
75 | | ZEND_API zend_utility_values zend_uv; |
76 | | ZEND_API bool zend_dtrace_enabled; |
77 | | |
78 | | /* version information */ |
79 | | static char *zend_version_info; |
80 | | static uint32_t zend_version_info_length; |
81 | 28 | #define ZEND_CORE_VERSION_INFO "Zend Engine v" ZEND_VERSION ", Copyright (c) Zend Technologies\n" |
82 | 1.77k | #define PRINT_ZVAL_INDENT 4 |
83 | | |
84 | | /* true multithread-shared globals */ |
85 | | ZEND_API zend_class_entry *zend_standard_class_def = NULL; |
86 | | ZEND_API size_t (*zend_printf)(const char *format, ...); |
87 | | ZEND_API zend_write_func_t zend_write; |
88 | | ZEND_API FILE *(*zend_fopen)(zend_string *filename, zend_string **opened_path); |
89 | | ZEND_API zend_result (*zend_stream_open_function)(zend_file_handle *handle); |
90 | | ZEND_API void (*zend_ticks_function)(int ticks); |
91 | | ZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data); |
92 | | ZEND_API void (*zend_error_cb)(int type, zend_string *error_filename, const uint32_t error_lineno, zend_string *message); |
93 | | void (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap); |
94 | | void (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap); |
95 | | ZEND_API char *(*zend_getenv)(const char *name, size_t name_len); |
96 | | ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename); |
97 | | ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL; |
98 | | ZEND_API void (*zend_post_shutdown_cb)(void) = NULL; |
99 | | ZEND_API void (*zend_accel_schedule_restart_hook)(int reason) = NULL; |
100 | | ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL; |
101 | | ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(zend_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL; |
102 | | |
103 | | /* This callback must be signal handler safe! */ |
104 | | void (*zend_on_timeout)(int seconds); |
105 | | |
106 | | static void (*zend_message_dispatcher_p)(zend_long message, const void *data); |
107 | | static zval *(*zend_get_configuration_directive_p)(zend_string *name); |
108 | | |
109 | | #if ZEND_RC_DEBUG |
110 | | ZEND_API bool zend_rc_debug = 0; |
111 | | #endif |
112 | | |
113 | | static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */ |
114 | 37 | { |
115 | 37 | if (!new_value) { |
116 | 0 | EG(error_reporting) = E_ALL; |
117 | 37 | } else { |
118 | 37 | EG(error_reporting) = atoi(ZSTR_VAL(new_value)); |
119 | 37 | } |
120 | 37 | return SUCCESS; |
121 | 37 | } |
122 | | /* }}} */ |
123 | | |
124 | | static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */ |
125 | 63 | { |
126 | 63 | bool val; |
127 | | |
128 | 63 | val = zend_ini_parse_bool(new_value); |
129 | 63 | gc_enable(val); |
130 | | |
131 | 63 | return SUCCESS; |
132 | 63 | } |
133 | | /* }}} */ |
134 | | |
135 | | static ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */ |
136 | 2 | { |
137 | 2 | if (gc_enabled()) { |
138 | 2 | ZEND_PUTS("On"); |
139 | 2 | } else { |
140 | 0 | ZEND_PUTS("Off"); |
141 | 0 | } |
142 | 2 | } |
143 | | /* }}} */ |
144 | | |
145 | | |
146 | | static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */ |
147 | 14 | { |
148 | 14 | if (!CG(multibyte)) { |
149 | 14 | return FAILURE; |
150 | 14 | } |
151 | 0 | if (!zend_multibyte_get_functions()) { |
152 | 0 | return SUCCESS; |
153 | 0 | } |
154 | 0 | return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0); |
155 | 0 | } |
156 | | /* }}} */ |
157 | | |
158 | | static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */ |
159 | 20 | { |
160 | 20 | zend_long *p = (zend_long *) ZEND_INI_GET_ADDR(); |
161 | | |
162 | 20 | zend_long val = zend_ini_parse_quantity_warn(new_value, entry->name); |
163 | | |
164 | 20 | if (stage != ZEND_INI_STAGE_STARTUP && |
165 | 6 | stage != ZEND_INI_STAGE_SHUTDOWN && |
166 | 6 | *p != val && |
167 | 4 | (*p < 0 || val < 0)) { |
168 | 0 | zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini"); |
169 | 0 | return FAILURE; |
170 | 0 | } |
171 | | |
172 | 20 | *p = val; |
173 | 20 | return SUCCESS; |
174 | 20 | } |
175 | | /* }}} */ |
176 | | |
177 | | static ZEND_INI_MH(OnSetExceptionStringParamMaxLen) /* {{{ */ |
178 | 38 | { |
179 | 38 | zend_long i = ZEND_ATOL(ZSTR_VAL(new_value)); |
180 | 38 | if (i >= 0 && i <= 1000000) { |
181 | 26 | EG(exception_string_param_max_len) = i; |
182 | 26 | return SUCCESS; |
183 | 26 | } else { |
184 | 12 | return FAILURE; |
185 | 12 | } |
186 | 38 | } |
187 | | /* }}} */ |
188 | | |
189 | | #ifdef ZEND_CHECK_STACK_LIMIT |
190 | | static ZEND_INI_MH(OnUpdateMaxAllowedStackSize) /* {{{ */ |
191 | 14 | { |
192 | 14 | zend_long size = zend_ini_parse_quantity_warn(new_value, entry->name); |
193 | | |
194 | 14 | if (size < ZEND_MAX_ALLOWED_STACK_SIZE_UNCHECKED) { |
195 | 0 | zend_error(E_WARNING, "Invalid \"%s\" setting. Value must be >= %d, but got " ZEND_LONG_FMT, |
196 | 0 | ZSTR_VAL(entry->name), ZEND_MAX_ALLOWED_STACK_SIZE_UNCHECKED, size); |
197 | 0 | return FAILURE; |
198 | 0 | } |
199 | | |
200 | 14 | EG(max_allowed_stack_size) = size; |
201 | | |
202 | 14 | return SUCCESS; |
203 | 14 | } |
204 | | /* }}} */ |
205 | | |
206 | | static ZEND_INI_MH(OnUpdateReservedStackSize) /* {{{ */ |
207 | 14 | { |
208 | 14 | zend_ulong size = zend_ini_parse_uquantity_warn(new_value, entry->name); |
209 | | |
210 | | /* Min value accounts for alloca, PCRE2 START_FRAMES_SIZE, and some buffer |
211 | | * for normal function calls. |
212 | | * We could reduce this on systems without alloca if we also add stack size |
213 | | * checks before pcre2_match(). */ |
214 | | #ifdef ZEND_ALLOCA_MAX_SIZE |
215 | | zend_ulong min = ZEND_ALLOCA_MAX_SIZE + 16*1024; |
216 | | #else |
217 | 14 | zend_ulong min = 32*1024; |
218 | 14 | #endif |
219 | | |
220 | | #if defined(__SANITIZE_ADDRESS__) || __has_feature(memory_sanitizer) |
221 | | /* AddressSanitizer and MemorySanitizer use more stack due to |
222 | | * instrumentation */ |
223 | | min *= 10; |
224 | | #endif |
225 | | |
226 | 14 | if (size == 0) { |
227 | 14 | size = min; |
228 | 14 | } else if (size < min) { |
229 | 0 | zend_error(E_WARNING, "Invalid \"%s\" setting. Value must be >= " ZEND_ULONG_FMT ", but got " ZEND_ULONG_FMT "\n", |
230 | 0 | ZSTR_VAL(entry->name), min, size); |
231 | 0 | return FAILURE; |
232 | 0 | } |
233 | | |
234 | 14 | EG(reserved_stack_size) = size; |
235 | | |
236 | 14 | return SUCCESS; |
237 | 14 | } |
238 | | /* }}} */ |
239 | | #endif /* ZEND_CHECK_STACK_LIMIT */ |
240 | | |
241 | | static ZEND_INI_MH(OnUpdateFiberStackSize) /* {{{ */ |
242 | 18 | { |
243 | 18 | if (new_value) { |
244 | 2 | zend_long tmp = zend_ini_parse_quantity_warn(new_value, entry->name); |
245 | 2 | if (tmp < 0) { |
246 | 1 | zend_error(E_WARNING, "fiber.stack_size must be a positive number"); |
247 | 1 | return FAILURE; |
248 | 1 | } |
249 | 1 | EG(fiber_stack_size) = tmp; |
250 | 16 | } else { |
251 | 16 | EG(fiber_stack_size) = ZEND_FIBER_DEFAULT_C_STACK_SIZE; |
252 | 16 | } |
253 | 17 | return SUCCESS; |
254 | 18 | } |
255 | | /* }}} */ |
256 | | |
257 | | #if ZEND_DEBUG |
258 | | # define SIGNAL_CHECK_DEFAULT "1" |
259 | | #else |
260 | | # define SIGNAL_CHECK_DEFAULT "0" |
261 | | #endif |
262 | | |
263 | | ZEND_INI_BEGIN() |
264 | | ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting) |
265 | | STD_ZEND_INI_BOOLEAN("fatal_error_backtraces", "1", ZEND_INI_ALL, OnUpdateBool, fatal_error_backtrace_on, zend_executor_globals, executor_globals) |
266 | | STD_ZEND_INI_ENTRY("zend.assertions", "1", ZEND_INI_ALL, OnUpdateAssertions, assertions, zend_executor_globals, executor_globals) |
267 | | ZEND_INI_ENTRY3_EX("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb) |
268 | | STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals) |
269 | | ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding) |
270 | | STD_ZEND_INI_BOOLEAN("zend.detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals) |
271 | | #ifdef ZEND_SIGNALS |
272 | | STD_ZEND_INI_BOOLEAN("zend.signal_check", SIGNAL_CHECK_DEFAULT, ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals) |
273 | | #endif |
274 | | STD_ZEND_INI_BOOLEAN("zend.exception_ignore_args", "0", ZEND_INI_ALL, OnUpdateBool, exception_ignore_args, zend_executor_globals, executor_globals) |
275 | | STD_ZEND_INI_ENTRY("zend.exception_string_param_max_len", "15", ZEND_INI_ALL, OnSetExceptionStringParamMaxLen, exception_string_param_max_len, zend_executor_globals, executor_globals) |
276 | | STD_ZEND_INI_ENTRY("fiber.stack_size", NULL, ZEND_INI_ALL, OnUpdateFiberStackSize, fiber_stack_size, zend_executor_globals, executor_globals) |
277 | | #ifdef ZEND_CHECK_STACK_LIMIT |
278 | | /* The maximum allowed call stack size. 0: auto detect, -1: no limit. For fibers, this is fiber.stack_size. */ |
279 | | STD_ZEND_INI_ENTRY("zend.max_allowed_stack_size", "0", ZEND_INI_SYSTEM, OnUpdateMaxAllowedStackSize, max_allowed_stack_size, zend_executor_globals, executor_globals) |
280 | | /* Subtracted from the max allowed stack size, as a buffer, when checking for overflow. 0: auto detect. */ |
281 | | STD_ZEND_INI_ENTRY("zend.reserved_stack_size", "0", ZEND_INI_SYSTEM, OnUpdateReservedStackSize, reserved_stack_size, zend_executor_globals, executor_globals) |
282 | | #endif |
283 | | |
284 | | ZEND_INI_END() |
285 | | |
286 | | ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */ |
287 | 10.5M | { |
288 | 10.5M | smart_string buf = {0}; |
289 | | |
290 | | /* since there are places where (v)spprintf called without checking for null, |
291 | | a bit of defensive coding here */ |
292 | 10.5M | if (!pbuf) { |
293 | 0 | return 0; |
294 | 0 | } |
295 | | |
296 | 10.5M | zend_printf_to_smart_string(&buf, format, ap); |
297 | | |
298 | 10.5M | if (max_len && buf.len > max_len) { |
299 | 0 | buf.len = max_len; |
300 | 0 | } |
301 | | |
302 | 10.5M | smart_string_0(&buf); |
303 | | |
304 | 10.5M | if (buf.c) { |
305 | 10.5M | *pbuf = buf.c; |
306 | 10.5M | return buf.len; |
307 | 10.5M | } else { |
308 | 0 | *pbuf = estrndup("", 0); |
309 | 0 | return 0; |
310 | 0 | } |
311 | 10.5M | } |
312 | | /* }}} */ |
313 | | |
314 | | ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */ |
315 | 7.02k | { |
316 | 7.02k | va_list arg; |
317 | 7.02k | size_t len; |
318 | | |
319 | 7.02k | va_start(arg, format); |
320 | 7.02k | len = zend_vspprintf(message, max_len, format, arg); |
321 | 7.02k | va_end(arg); |
322 | 7.02k | return len; |
323 | 7.02k | } |
324 | | /* }}} */ |
325 | | |
326 | | ZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */ |
327 | 0 | { |
328 | 0 | va_list arg; |
329 | 0 | size_t len; |
330 | |
|
331 | 0 | va_start(arg, format); |
332 | 0 | len = zend_vspprintf(message, max_len, format, arg); |
333 | 0 | va_end(arg); |
334 | 0 | return len; |
335 | 0 | } |
336 | | /* }}} */ |
337 | | |
338 | | ZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */ |
339 | 3.75M | { |
340 | 3.75M | smart_str buf = {0}; |
341 | | |
342 | 3.75M | zend_printf_to_smart_str(&buf, format, ap); |
343 | | |
344 | 3.75M | if (!buf.s) { |
345 | 0 | return ZSTR_EMPTY_ALLOC(); |
346 | 0 | } |
347 | | |
348 | 3.75M | if (max_len && ZSTR_LEN(buf.s) > max_len) { |
349 | 0 | ZSTR_LEN(buf.s) = max_len; |
350 | 0 | } |
351 | | |
352 | 3.75M | return smart_str_extract(&buf); |
353 | 3.75M | } |
354 | | /* }}} */ |
355 | | |
356 | | ZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */ |
357 | 120k | { |
358 | 120k | va_list arg; |
359 | 120k | zend_string *str; |
360 | | |
361 | 120k | va_start(arg, format); |
362 | 120k | str = zend_vstrpprintf(max_len, format, arg); |
363 | 120k | va_end(arg); |
364 | 120k | return str; |
365 | 120k | } |
366 | | /* }}} */ |
367 | | |
368 | | ZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */ |
369 | 1.73M | { |
370 | 1.73M | va_list arg; |
371 | 1.73M | zend_string *str; |
372 | | |
373 | 1.73M | va_start(arg, format); |
374 | 1.73M | str = zend_vstrpprintf(max_len, format, arg); |
375 | 1.73M | va_end(arg); |
376 | 1.73M | return str; |
377 | 1.73M | } |
378 | | /* }}} */ |
379 | | |
380 | | static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent); |
381 | | |
382 | | static void print_hash(smart_str *buf, HashTable *ht, int indent, bool is_object) /* {{{ */ |
383 | 558 | { |
384 | 558 | zval *tmp; |
385 | 558 | zend_string *string_key; |
386 | 558 | zend_ulong num_key; |
387 | 558 | int i; |
388 | | |
389 | 966 | for (i = 0; i < indent; i++) { |
390 | 408 | smart_str_appendc(buf, ' '); |
391 | 408 | } |
392 | 558 | smart_str_appends(buf, "(\n"); |
393 | 558 | indent += PRINT_ZVAL_INDENT; |
394 | 2.09k | ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) { |
395 | 4.07k | for (i = 0; i < indent; i++) { |
396 | 3.41k | smart_str_appendc(buf, ' '); |
397 | 3.41k | } |
398 | 2.09k | smart_str_appendc(buf, '['); |
399 | 2.09k | if (string_key) { |
400 | 62 | if (is_object) { |
401 | 26 | const char *prop_name, *class_name; |
402 | 26 | size_t prop_len; |
403 | 26 | int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len); |
404 | | |
405 | 26 | smart_str_appendl(buf, prop_name, prop_len); |
406 | 26 | if (class_name && mangled == SUCCESS) { |
407 | 0 | if (class_name[0] == '*') { |
408 | 0 | smart_str_appends(buf, ":protected"); |
409 | 0 | } else { |
410 | 0 | smart_str_appends(buf, ":"); |
411 | 0 | smart_str_appends(buf, class_name); |
412 | 0 | smart_str_appends(buf, ":private"); |
413 | 0 | } |
414 | 0 | } |
415 | 36 | } else { |
416 | 36 | smart_str_append(buf, string_key); |
417 | 36 | } |
418 | 598 | } else { |
419 | 598 | smart_str_append_long(buf, num_key); |
420 | 598 | } |
421 | 2.09k | smart_str_appends(buf, "] => "); |
422 | 2.09k | zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT); |
423 | 2.09k | smart_str_appends(buf, "\n"); |
424 | 2.09k | } ZEND_HASH_FOREACH_END(); |
425 | 558 | indent -= PRINT_ZVAL_INDENT; |
426 | 966 | for (i = 0; i < indent; i++) { |
427 | 408 | smart_str_appendc(buf, ' '); |
428 | 408 | } |
429 | 558 | smart_str_appends(buf, ")\n"); |
430 | 558 | } |
431 | | /* }}} */ |
432 | | |
433 | | static void print_flat_hash(smart_str *buf, HashTable *ht) /* {{{ */ |
434 | 0 | { |
435 | 0 | zval *tmp; |
436 | 0 | zend_string *string_key; |
437 | 0 | zend_ulong num_key; |
438 | 0 | int i = 0; |
439 | |
|
440 | 0 | ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) { |
441 | 0 | if (i++ > 0) { |
442 | 0 | smart_str_appendc(buf, ','); |
443 | 0 | } |
444 | 0 | smart_str_appendc(buf, '['); |
445 | 0 | if (string_key) { |
446 | 0 | smart_str_append(buf, string_key); |
447 | 0 | } else { |
448 | 0 | smart_str_append_unsigned(buf, num_key); |
449 | 0 | } |
450 | 0 | smart_str_appends(buf, "] => "); |
451 | 0 | zend_print_flat_zval_r_to_buf(buf, tmp); |
452 | 0 | } ZEND_HASH_FOREACH_END(); |
453 | 0 | } |
454 | | /* }}} */ |
455 | | |
456 | | ZEND_API bool zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */ |
457 | 0 | { |
458 | 0 | if (Z_TYPE_P(expr) == IS_STRING) { |
459 | 0 | return 0; |
460 | 0 | } else { |
461 | 0 | ZVAL_STR(expr_copy, zval_get_string_func(expr)); |
462 | 0 | return 1; |
463 | 0 | } |
464 | 0 | } |
465 | | /* }}} */ |
466 | | |
467 | | ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */ |
468 | 0 | { |
469 | 0 | zend_string *tmp_str; |
470 | 0 | zend_string *str = zval_get_tmp_string(expr, &tmp_str); |
471 | 0 | size_t len = ZSTR_LEN(str); |
472 | |
|
473 | 0 | if (len != 0) { |
474 | 0 | zend_write(ZSTR_VAL(str), len); |
475 | 0 | } |
476 | |
|
477 | 0 | zend_tmp_string_release(tmp_str); |
478 | 0 | return len; |
479 | 0 | } |
480 | | /* }}} */ |
481 | | |
482 | | void zend_print_flat_zval_r_to_buf(smart_str *buf, zval *expr) /* {{{ */ |
483 | 0 | { |
484 | 0 | switch (Z_TYPE_P(expr)) { |
485 | 0 | case IS_ARRAY: |
486 | 0 | smart_str_appends(buf, "Array ("); |
487 | 0 | if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) { |
488 | 0 | if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) { |
489 | 0 | smart_str_appends(buf, " *RECURSION*"); |
490 | 0 | return; |
491 | 0 | } |
492 | 0 | GC_PROTECT_RECURSION(Z_ARRVAL_P(expr)); |
493 | 0 | } |
494 | 0 | print_flat_hash(buf, Z_ARRVAL_P(expr)); |
495 | 0 | smart_str_appendc(buf, ')'); |
496 | 0 | GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr)); |
497 | 0 | break; |
498 | 0 | case IS_OBJECT: |
499 | 0 | { |
500 | 0 | HashTable *properties; |
501 | 0 | zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr)); |
502 | 0 | smart_str_append(buf, class_name); |
503 | 0 | smart_str_appends(buf, " Object ("); |
504 | 0 | zend_string_release_ex(class_name, 0); |
505 | |
|
506 | 0 | if (GC_IS_RECURSIVE(Z_COUNTED_P(expr))) { |
507 | 0 | smart_str_appends(buf, " *RECURSION*"); |
508 | 0 | return; |
509 | 0 | } |
510 | | |
511 | 0 | properties = Z_OBJPROP_P(expr); |
512 | 0 | if (properties) { |
513 | 0 | GC_PROTECT_RECURSION(Z_OBJ_P(expr)); |
514 | 0 | print_flat_hash(buf, properties); |
515 | 0 | GC_UNPROTECT_RECURSION(Z_OBJ_P(expr)); |
516 | 0 | } |
517 | 0 | smart_str_appendc(buf, ')'); |
518 | 0 | break; |
519 | 0 | } |
520 | 0 | case IS_REFERENCE: |
521 | 0 | zend_print_flat_zval_r_to_buf(buf, Z_REFVAL_P(expr)); |
522 | 0 | break; |
523 | 0 | case IS_STRING: |
524 | 0 | smart_str_append(buf, Z_STR_P(expr)); |
525 | 0 | break; |
526 | 0 | default: |
527 | 0 | { |
528 | 0 | zend_string *str = zval_get_string_func(expr); |
529 | 0 | smart_str_append(buf, str); |
530 | 0 | zend_string_release_ex(str, 0); |
531 | 0 | break; |
532 | 0 | } |
533 | 0 | } |
534 | 0 | } |
535 | | /* }}} */ |
536 | | |
537 | | ZEND_API void zend_print_flat_zval_r(zval *expr) |
538 | 0 | { |
539 | 0 | smart_str buf = {0}; |
540 | 0 | zend_print_flat_zval_r_to_buf(&buf, expr); |
541 | 0 | smart_str_0(&buf); |
542 | 0 | zend_write(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
543 | 0 | smart_str_free(&buf); |
544 | 0 | } |
545 | | |
546 | | static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent) /* {{{ */ |
547 | 1.64k | { |
548 | 1.64k | switch (Z_TYPE_P(expr)) { |
549 | 545 | case IS_ARRAY: |
550 | 545 | smart_str_appends(buf, "Array\n"); |
551 | 545 | if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) { |
552 | 501 | if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) { |
553 | 4 | smart_str_appends(buf, " *RECURSION*"); |
554 | 4 | return; |
555 | 4 | } |
556 | 497 | GC_PROTECT_RECURSION(Z_ARRVAL_P(expr)); |
557 | 497 | } |
558 | 541 | print_hash(buf, Z_ARRVAL_P(expr), indent, false); |
559 | 541 | GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr)); |
560 | 541 | break; |
561 | 17 | case IS_OBJECT: |
562 | 17 | { |
563 | 17 | HashTable *properties; |
564 | | |
565 | 17 | zend_object *zobj = Z_OBJ_P(expr); |
566 | 17 | uint32_t *guard = zend_get_recursion_guard(zobj); |
567 | 17 | zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(zobj); |
568 | 17 | smart_str_appends(buf, ZSTR_VAL(class_name)); |
569 | 17 | zend_string_release_ex(class_name, 0); |
570 | | |
571 | 17 | if (!(zobj->ce->ce_flags & ZEND_ACC_ENUM)) { |
572 | 13 | smart_str_appends(buf, " Object\n"); |
573 | 13 | } else { |
574 | 4 | smart_str_appends(buf, " Enum"); |
575 | 4 | if (zobj->ce->enum_backing_type != IS_UNDEF) { |
576 | 2 | smart_str_appendc(buf, ':'); |
577 | 2 | smart_str_appends(buf, zend_get_type_by_const(zobj->ce->enum_backing_type)); |
578 | 2 | } |
579 | 4 | smart_str_appendc(buf, '\n'); |
580 | 4 | } |
581 | | |
582 | 17 | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) { |
583 | 0 | smart_str_appends(buf, " *RECURSION*"); |
584 | 0 | return; |
585 | 0 | } |
586 | | |
587 | 17 | if ((properties = zend_get_properties_for(expr, ZEND_PROP_PURPOSE_DEBUG)) == NULL) { |
588 | 0 | print_hash(buf, (HashTable*) &zend_empty_array, indent, true); |
589 | 0 | break; |
590 | 0 | } |
591 | | |
592 | 17 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj); |
593 | 17 | print_hash(buf, properties, indent, true); |
594 | 17 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj); |
595 | | |
596 | 17 | zend_release_properties(properties); |
597 | 17 | break; |
598 | 17 | } |
599 | 406 | case IS_LONG: |
600 | 406 | smart_str_append_long(buf, Z_LVAL_P(expr)); |
601 | 406 | break; |
602 | 11 | case IS_REFERENCE: |
603 | 11 | zend_print_zval_r_to_buf(buf, Z_REFVAL_P(expr), indent); |
604 | 11 | break; |
605 | 218 | case IS_STRING: |
606 | 218 | smart_str_append(buf, Z_STR_P(expr)); |
607 | 218 | break; |
608 | 452 | default: |
609 | 452 | { |
610 | 452 | zend_string *str = zval_get_string_func(expr); |
611 | 452 | smart_str_append(buf, str); |
612 | 452 | zend_string_release_ex(str, 0); |
613 | 452 | } |
614 | 452 | break; |
615 | 1.64k | } |
616 | 1.64k | } |
617 | | /* }}} */ |
618 | | |
619 | | ZEND_API zend_string *zend_print_zval_r_to_str(zval *expr, int indent) /* {{{ */ |
620 | 978 | { |
621 | 978 | smart_str buf = {0}; |
622 | 978 | zend_print_zval_r_to_buf(&buf, expr, indent); |
623 | 978 | smart_str_0(&buf); |
624 | 978 | return buf.s; |
625 | 978 | } |
626 | | /* }}} */ |
627 | | |
628 | | ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */ |
629 | 972 | { |
630 | 972 | zend_string *str = zend_print_zval_r_to_str(expr, indent); |
631 | 972 | zend_write(ZSTR_VAL(str), ZSTR_LEN(str)); |
632 | 972 | zend_string_release_ex(str, 0); |
633 | 972 | } |
634 | | /* }}} */ |
635 | | |
636 | | static FILE *zend_fopen_wrapper(zend_string *filename, zend_string **opened_path) /* {{{ */ |
637 | 0 | { |
638 | 0 | if (opened_path) { |
639 | 0 | *opened_path = zend_string_copy(filename); |
640 | 0 | } |
641 | 0 | return fopen(ZSTR_VAL(filename), "rb"); |
642 | 0 | } |
643 | | /* }}} */ |
644 | | |
645 | | #ifdef ZTS |
646 | | static bool short_tags_default = true; |
647 | | static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT; |
648 | | #else |
649 | 14 | # define short_tags_default 1 |
650 | 14 | # define compiler_options_default ZEND_COMPILE_DEFAULT |
651 | | #endif |
652 | | |
653 | | static void zend_set_default_compile_time_values(void) /* {{{ */ |
654 | 14 | { |
655 | | /* default compile-time values */ |
656 | 14 | CG(short_tags) = short_tags_default; |
657 | 14 | CG(compiler_options) = compiler_options_default; |
658 | | |
659 | 14 | CG(rtd_key_counter) = 0; |
660 | 14 | } |
661 | | /* }}} */ |
662 | | |
663 | | #ifdef ZEND_WIN32 |
664 | | static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */ |
665 | | { |
666 | | ZeroMemory(osvi, sizeof(OSVERSIONINFOEX)); |
667 | | osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
668 | | if(!GetVersionEx((OSVERSIONINFO *) osvi)) { |
669 | | ZEND_UNREACHABLE(); /* Should not happen as sizeof is used. */ |
670 | | } |
671 | | } |
672 | | /* }}} */ |
673 | | #endif |
674 | | |
675 | | static void zend_init_exception_op(void) /* {{{ */ |
676 | 14 | { |
677 | 14 | memset(EG(exception_op), 0, sizeof(EG(exception_op))); |
678 | 14 | EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION; |
679 | 14 | ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)); |
680 | 14 | EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION; |
681 | 14 | ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1); |
682 | 14 | EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION; |
683 | 14 | ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2); |
684 | 14 | } |
685 | | /* }}} */ |
686 | | |
687 | | static void zend_init_call_trampoline_op(void) /* {{{ */ |
688 | 14 | { |
689 | 14 | memset(&EG(call_trampoline_op), 0, sizeof(EG(call_trampoline_op))); |
690 | 14 | EG(call_trampoline_op).opcode = ZEND_CALL_TRAMPOLINE; |
691 | 14 | ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op)); |
692 | 14 | } |
693 | | /* }}} */ |
694 | | |
695 | | static void auto_global_dtor(zval *zv) /* {{{ */ |
696 | 0 | { |
697 | 0 | free(Z_PTR_P(zv)); |
698 | 0 | } |
699 | | /* }}} */ |
700 | | |
701 | | #ifdef ZTS |
702 | | static void auto_global_copy_ctor(zval *zv) /* {{{ */ |
703 | | { |
704 | | zend_auto_global *old_ag = (zend_auto_global *) Z_PTR_P(zv); |
705 | | zend_auto_global *new_ag = pemalloc(sizeof(zend_auto_global), 1); |
706 | | |
707 | | new_ag->name = old_ag->name; |
708 | | new_ag->auto_global_callback = old_ag->auto_global_callback; |
709 | | new_ag->jit = old_ag->jit; |
710 | | |
711 | | Z_PTR_P(zv) = new_ag; |
712 | | } |
713 | | /* }}} */ |
714 | | |
715 | | static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */ |
716 | | { |
717 | | compiler_globals->compiled_filename = NULL; |
718 | | compiler_globals->zend_lineno = 0; |
719 | | |
720 | | compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable)); |
721 | | zend_hash_init(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1); |
722 | | zend_hash_copy(compiler_globals->function_table, global_function_table, NULL); |
723 | | compiler_globals->copied_functions_count = zend_hash_num_elements(compiler_globals->function_table); |
724 | | |
725 | | compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable)); |
726 | | zend_hash_init(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1); |
727 | | zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref); |
728 | | |
729 | | zend_set_default_compile_time_values(); |
730 | | |
731 | | compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable)); |
732 | | zend_hash_init(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1); |
733 | | zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor); |
734 | | |
735 | | compiler_globals->script_encoding_list = NULL; |
736 | | compiler_globals->current_linking_class = NULL; |
737 | | |
738 | | /* Map region is going to be created and resized at run-time. */ |
739 | | compiler_globals->map_ptr_real_base = NULL; |
740 | | compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); |
741 | | compiler_globals->map_ptr_size = 0; |
742 | | compiler_globals->map_ptr_last = global_map_ptr_last; |
743 | | compiler_globals->internal_run_time_cache = NULL; |
744 | | if (compiler_globals->map_ptr_last || zend_map_ptr_static_size) { |
745 | | /* Allocate map_ptr table */ |
746 | | compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096); |
747 | | void *base = pemalloc((zend_map_ptr_static_size + compiler_globals->map_ptr_size) * sizeof(void*), 1); |
748 | | compiler_globals->map_ptr_real_base = base; |
749 | | compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(base); |
750 | | memset(base, 0, (zend_map_ptr_static_size + compiler_globals->map_ptr_last) * sizeof(void*)); |
751 | | } |
752 | | zend_init_internal_run_time_cache(); |
753 | | } |
754 | | /* }}} */ |
755 | | |
756 | | static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */ |
757 | | { |
758 | | if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) { |
759 | | uint32_t n = compiler_globals->copied_functions_count; |
760 | | |
761 | | /* Prevent destruction of functions copied from the main process context */ |
762 | | if (zend_hash_num_elements(compiler_globals->function_table) <= n) { |
763 | | compiler_globals->function_table->nNumUsed = 0; |
764 | | } else { |
765 | | Bucket *p = compiler_globals->function_table->arData; |
766 | | |
767 | | compiler_globals->function_table->nNumOfElements -= n; |
768 | | while (n != 0) { |
769 | | ZVAL_UNDEF(&p->val); |
770 | | p++; |
771 | | n--; |
772 | | } |
773 | | } |
774 | | zend_hash_destroy(compiler_globals->function_table); |
775 | | free(compiler_globals->function_table); |
776 | | } |
777 | | if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) { |
778 | | /* Child classes may reuse structures from parent classes, so destroy in reverse order. */ |
779 | | zend_hash_graceful_reverse_destroy(compiler_globals->class_table); |
780 | | free(compiler_globals->class_table); |
781 | | } |
782 | | if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) { |
783 | | zend_hash_destroy(compiler_globals->auto_globals); |
784 | | free(compiler_globals->auto_globals); |
785 | | } |
786 | | if (compiler_globals->script_encoding_list) { |
787 | | pefree((char*)compiler_globals->script_encoding_list, 1); |
788 | | } |
789 | | if (compiler_globals->map_ptr_real_base) { |
790 | | free(compiler_globals->map_ptr_real_base); |
791 | | compiler_globals->map_ptr_real_base = NULL; |
792 | | compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); |
793 | | compiler_globals->map_ptr_size = 0; |
794 | | } |
795 | | if (compiler_globals->internal_run_time_cache) { |
796 | | pefree(compiler_globals->internal_run_time_cache, 1); |
797 | | compiler_globals->internal_run_time_cache = NULL; |
798 | | } |
799 | | } |
800 | | /* }}} */ |
801 | | |
802 | | static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */ |
803 | | { |
804 | | zend_startup_constants(); |
805 | | zend_copy_constants(executor_globals->zend_constants, GLOBAL_CONSTANTS_TABLE); |
806 | | zend_init_rsrc_plist(); |
807 | | zend_init_exception_op(); |
808 | | zend_init_call_trampoline_op(); |
809 | | memset(&executor_globals->trampoline, 0, sizeof(zend_op_array)); |
810 | | executor_globals->capture_warnings_during_sccp = 0; |
811 | | executor_globals->user_error_handler_error_reporting = 0; |
812 | | ZVAL_UNDEF(&executor_globals->user_error_handler); |
813 | | ZVAL_UNDEF(&executor_globals->user_exception_handler); |
814 | | executor_globals->in_autoload = NULL; |
815 | | executor_globals->current_execute_data = NULL; |
816 | | executor_globals->current_module = NULL; |
817 | | executor_globals->exit_status = 0; |
818 | | #if XPFPA_HAVE_CW |
819 | | executor_globals->saved_fpu_cw = 0; |
820 | | #endif |
821 | | executor_globals->saved_fpu_cw_ptr = NULL; |
822 | | executor_globals->active = false; |
823 | | executor_globals->bailout = NULL; |
824 | | executor_globals->error_handling = EH_NORMAL; |
825 | | executor_globals->exception_class = NULL; |
826 | | executor_globals->exception = NULL; |
827 | | executor_globals->objects_store.object_buckets = NULL; |
828 | | executor_globals->current_fiber_context = NULL; |
829 | | executor_globals->main_fiber_context = NULL; |
830 | | executor_globals->active_fiber = NULL; |
831 | | #ifdef ZEND_WIN32 |
832 | | zend_get_windows_version_info(&executor_globals->windows_version_info); |
833 | | #endif |
834 | | executor_globals->flags = EG_FLAGS_INITIAL; |
835 | | executor_globals->record_errors = false; |
836 | | executor_globals->num_errors = 0; |
837 | | executor_globals->errors = NULL; |
838 | | executor_globals->filename_override = NULL; |
839 | | executor_globals->lineno_override = -1; |
840 | | #ifdef ZEND_CHECK_STACK_LIMIT |
841 | | executor_globals->stack_limit = (void*)0; |
842 | | executor_globals->stack_base = (void*)0; |
843 | | #endif |
844 | | #ifdef ZEND_MAX_EXECUTION_TIMERS |
845 | | executor_globals->pid = 0; |
846 | | executor_globals->oldact = (struct sigaction){0}; |
847 | | #endif |
848 | | memset(executor_globals->strtod_state.freelist, 0, |
849 | | sizeof(executor_globals->strtod_state.freelist)); |
850 | | executor_globals->strtod_state.p5s = NULL; |
851 | | executor_globals->strtod_state.result = NULL; |
852 | | } |
853 | | /* }}} */ |
854 | | |
855 | | static void executor_globals_persistent_list_dtor(void *storage) |
856 | | { |
857 | | zend_executor_globals *executor_globals = storage; |
858 | | |
859 | | if (&executor_globals->persistent_list != global_persistent_list) { |
860 | | zend_destroy_rsrc_list(&executor_globals->persistent_list); |
861 | | } |
862 | | } |
863 | | |
864 | | static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */ |
865 | | { |
866 | | zend_ini_dtor(executor_globals->ini_directives); |
867 | | |
868 | | if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) { |
869 | | zend_hash_destroy(executor_globals->zend_constants); |
870 | | free(executor_globals->zend_constants); |
871 | | } |
872 | | } |
873 | | /* }}} */ |
874 | | |
875 | | static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */ |
876 | | { |
877 | | zend_copy_ini_directives(); |
878 | | zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP); |
879 | | #ifdef ZEND_CHECK_STACK_LIMIT |
880 | | zend_call_stack_init(); |
881 | | #endif |
882 | | zend_max_execution_timer_init(); |
883 | | } |
884 | | /* }}} */ |
885 | | #endif |
886 | | |
887 | | #if defined(__FreeBSD__) || defined(__DragonFly__) |
888 | | /* FreeBSD and DragonFly floating point precision fix */ |
889 | | #include <floatingpoint.h> |
890 | | #endif |
891 | | |
892 | | static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */ |
893 | 14 | { |
894 | 14 | memset(scanner_globals_p, 0, sizeof(*scanner_globals_p)); |
895 | 14 | } |
896 | | /* }}} */ |
897 | | |
898 | | static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */ |
899 | 14 | { |
900 | 14 | memset(scanner_globals_p, 0, sizeof(*scanner_globals_p)); |
901 | 14 | } |
902 | | /* }}} */ |
903 | | |
904 | | static void module_destructor_zval(zval *zv) /* {{{ */ |
905 | 0 | { |
906 | 0 | zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv); |
907 | 0 | module_destructor(module); |
908 | 0 | } |
909 | | /* }}} */ |
910 | | |
911 | | static bool php_auto_globals_create_globals(zend_string *name) /* {{{ */ |
912 | 47 | { |
913 | | /* While we keep registering $GLOBALS as an auto-global, we do not create an |
914 | | * actual variable for it. Access to it handled specially by the compiler. */ |
915 | 47 | return false; |
916 | 47 | } |
917 | | /* }}} */ |
918 | | |
919 | | void zend_startup(zend_utility_functions *utility_functions) /* {{{ */ |
920 | 14 | { |
921 | | #ifdef ZTS |
922 | | zend_compiler_globals *compiler_globals; |
923 | | zend_executor_globals *executor_globals; |
924 | | extern ZEND_API ts_rsrc_id ini_scanner_globals_id; |
925 | | extern ZEND_API ts_rsrc_id language_scanner_globals_id; |
926 | | #else |
927 | 14 | extern zend_ini_scanner_globals ini_scanner_globals; |
928 | 14 | extern zend_php_scanner_globals language_scanner_globals; |
929 | 14 | #endif |
930 | | |
931 | 14 | zend_cpu_startup(); |
932 | | |
933 | | #ifdef ZEND_WIN32 |
934 | | php_win32_cp_set_by_id(65001); |
935 | | #endif |
936 | | |
937 | | /* Set up early utility functions. zend_mm depends on |
938 | | * zend_random_bytes_insecure */ |
939 | 14 | zend_random_bytes = utility_functions->random_bytes_function; |
940 | 14 | zend_random_bytes_insecure = utility_functions->random_bytes_insecure_function; |
941 | | |
942 | 14 | start_memory_manager(); |
943 | | |
944 | 14 | virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */ |
945 | | |
946 | | #if defined(__FreeBSD__) || defined(__DragonFly__) |
947 | | /* FreeBSD and DragonFly floating point precision fix */ |
948 | | fpsetmask(0); |
949 | | #endif |
950 | | |
951 | 14 | zend_startup_hrtime(); |
952 | 14 | zend_startup_extensions_mechanism(); |
953 | | |
954 | | /* Set up utility functions and values */ |
955 | 14 | zend_error_cb = utility_functions->error_function; |
956 | 14 | zend_printf = utility_functions->printf_function; |
957 | 14 | zend_write = utility_functions->write_function; |
958 | 14 | zend_fopen = utility_functions->fopen_function; |
959 | 14 | if (!zend_fopen) { |
960 | 0 | zend_fopen = zend_fopen_wrapper; |
961 | 0 | } |
962 | 14 | zend_stream_open_function = utility_functions->stream_open_function; |
963 | 14 | zend_message_dispatcher_p = utility_functions->message_handler; |
964 | 14 | zend_get_configuration_directive_p = utility_functions->get_configuration_directive; |
965 | 14 | zend_ticks_function = utility_functions->ticks_function; |
966 | 14 | zend_on_timeout = utility_functions->on_timeout; |
967 | 14 | zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function; |
968 | 14 | zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function; |
969 | 14 | zend_getenv = utility_functions->getenv_function; |
970 | 14 | zend_resolve_path = utility_functions->resolve_path_function; |
971 | | |
972 | 14 | zend_interrupt_function = NULL; |
973 | | |
974 | | #ifdef HAVE_DTRACE |
975 | | /* build with dtrace support */ |
976 | | { |
977 | | char *tmp = getenv("USE_ZEND_DTRACE"); |
978 | | |
979 | | if (tmp && ZEND_ATOL(tmp)) { |
980 | | zend_dtrace_enabled = 1; |
981 | | zend_compile_file = dtrace_compile_file; |
982 | | zend_execute_ex = dtrace_execute_ex; |
983 | | zend_execute_internal = dtrace_execute_internal; |
984 | | |
985 | | zend_observer_error_register(dtrace_error_notify_cb); |
986 | | } else { |
987 | | zend_compile_file = compile_file; |
988 | | zend_execute_ex = execute_ex; |
989 | | zend_execute_internal = NULL; |
990 | | } |
991 | | } |
992 | | #else |
993 | 14 | zend_compile_file = compile_file; |
994 | 14 | zend_execute_ex = execute_ex; |
995 | 14 | zend_execute_internal = NULL; |
996 | 14 | #endif /* HAVE_DTRACE */ |
997 | 14 | zend_compile_string = compile_string; |
998 | 14 | zend_throw_exception_hook = NULL; |
999 | | |
1000 | | /* Set up the default garbage collection implementation. */ |
1001 | 14 | gc_collect_cycles = zend_gc_collect_cycles; |
1002 | | |
1003 | 14 | zend_vm_init(); |
1004 | | |
1005 | | /* set up version */ |
1006 | 14 | zend_version_info = strdup(ZEND_CORE_VERSION_INFO); |
1007 | 14 | zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1; |
1008 | | |
1009 | 14 | GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable)); |
1010 | 14 | GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable)); |
1011 | 14 | GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable)); |
1012 | 14 | GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable)); |
1013 | | |
1014 | 14 | zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1); |
1015 | 14 | zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1); |
1016 | 14 | zend_hash_init(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1); |
1017 | 14 | zend_hash_init(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1); |
1018 | | |
1019 | 14 | zend_hash_init(&module_registry, 32, NULL, module_destructor_zval, 1); |
1020 | 14 | zend_init_rsrc_list_dtors(); |
1021 | | |
1022 | | #ifdef ZTS |
1023 | | ts_allocate_fast_id(&compiler_globals_id, &compiler_globals_offset, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor); |
1024 | | ts_allocate_fast_id(&executor_globals_id, &executor_globals_offset, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor); |
1025 | | ts_allocate_fast_id(&language_scanner_globals_id, &language_scanner_globals_offset, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL); |
1026 | | ts_allocate_fast_id(&ini_scanner_globals_id, &ini_scanner_globals_offset, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL); |
1027 | | compiler_globals = ts_resource(compiler_globals_id); |
1028 | | executor_globals = ts_resource(executor_globals_id); |
1029 | | |
1030 | | compiler_globals_dtor(compiler_globals); |
1031 | | compiler_globals->in_compilation = false; |
1032 | | compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable)); |
1033 | | compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable)); |
1034 | | |
1035 | | *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE; |
1036 | | *compiler_globals->class_table = *GLOBAL_CLASS_TABLE; |
1037 | | compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE; |
1038 | | |
1039 | | zend_hash_destroy(executor_globals->zend_constants); |
1040 | | *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE; |
1041 | | #else |
1042 | 14 | ini_scanner_globals_ctor(&ini_scanner_globals); |
1043 | 14 | php_scanner_globals_ctor(&language_scanner_globals); |
1044 | 14 | zend_set_default_compile_time_values(); |
1045 | | #ifdef ZEND_WIN32 |
1046 | | zend_get_windows_version_info(&EG(windows_version_info)); |
1047 | | #endif |
1048 | | /* Map region is going to be created and resized at run-time. */ |
1049 | 14 | CG(map_ptr_real_base) = NULL; |
1050 | 14 | CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL); |
1051 | 14 | CG(map_ptr_size) = 0; |
1052 | 14 | CG(map_ptr_last) = 0; |
1053 | 14 | #endif /* ZTS */ |
1054 | 14 | EG(error_reporting) = E_ALL & ~E_NOTICE; |
1055 | 14 | EG(fatal_error_backtrace_on) = false; |
1056 | 14 | ZVAL_UNDEF(&EG(last_fatal_error_backtrace)); |
1057 | | |
1058 | 14 | zend_interned_strings_init(); |
1059 | 14 | zend_object_handlers_startup(); |
1060 | 14 | zend_startup_builtin_functions(); |
1061 | 14 | zend_register_standard_constants(); |
1062 | 14 | zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals); |
1063 | | |
1064 | 14 | #ifndef ZTS |
1065 | 14 | zend_init_rsrc_plist(); |
1066 | 14 | zend_init_exception_op(); |
1067 | 14 | zend_init_call_trampoline_op(); |
1068 | 14 | #endif |
1069 | | |
1070 | 14 | zend_ini_startup(); |
1071 | | |
1072 | | #ifdef ZEND_WIN32 |
1073 | | /* Uses INI settings, so needs to be run after it. */ |
1074 | | php_win32_cp_setup(); |
1075 | | #endif |
1076 | | |
1077 | 14 | zend_optimizer_startup(); |
1078 | | |
1079 | | #ifdef ZTS |
1080 | | tsrm_set_new_thread_end_handler(zend_new_thread_end_handler); |
1081 | | tsrm_set_shutdown_handler(zend_interned_strings_dtor); |
1082 | | #endif |
1083 | | |
1084 | 14 | zend_enum_startup(); |
1085 | 14 | zend_closure_startup(); |
1086 | 14 | } |
1087 | | /* }}} */ |
1088 | | |
1089 | | void zend_register_standard_ini_entries(void) /* {{{ */ |
1090 | 14 | { |
1091 | 14 | zend_register_ini_entries_ex(ini_entries, 0, MODULE_PERSISTENT); |
1092 | 14 | } |
1093 | | /* }}} */ |
1094 | | |
1095 | | |
1096 | | /* Unlink the global (r/o) copies of the class, function and constant tables, |
1097 | | * and use a fresh r/w copy for the startup thread |
1098 | | */ |
1099 | | zend_result zend_post_startup(void) /* {{{ */ |
1100 | 14 | { |
1101 | | #ifdef ZTS |
1102 | | zend_encoding **script_encoding_list; |
1103 | | |
1104 | | zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id); |
1105 | | zend_executor_globals *executor_globals = ts_resource(executor_globals_id); |
1106 | | #endif |
1107 | | |
1108 | 14 | startup_done = true; |
1109 | | |
1110 | 14 | if (zend_post_startup_cb) { |
1111 | 14 | zend_result (*cb)(void) = zend_post_startup_cb; |
1112 | | |
1113 | 14 | zend_post_startup_cb = NULL; |
1114 | 14 | if (cb() != SUCCESS) { |
1115 | 0 | return FAILURE; |
1116 | 0 | } |
1117 | 14 | } |
1118 | | |
1119 | | #ifdef ZTS |
1120 | | *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table; |
1121 | | *GLOBAL_CLASS_TABLE = *compiler_globals->class_table; |
1122 | | *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants; |
1123 | | global_map_ptr_last = compiler_globals->map_ptr_last; |
1124 | | |
1125 | | short_tags_default = CG(short_tags); |
1126 | | compiler_options_default = CG(compiler_options); |
1127 | | |
1128 | | zend_destroy_rsrc_list(&EG(persistent_list)); |
1129 | | free(compiler_globals->function_table); |
1130 | | compiler_globals->function_table = NULL; |
1131 | | free(compiler_globals->class_table); |
1132 | | compiler_globals->class_table = NULL; |
1133 | | if (compiler_globals->map_ptr_real_base) { |
1134 | | free(compiler_globals->map_ptr_real_base); |
1135 | | } |
1136 | | compiler_globals->map_ptr_real_base = NULL; |
1137 | | compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); |
1138 | | if (compiler_globals->internal_run_time_cache) { |
1139 | | pefree(compiler_globals->internal_run_time_cache, 1); |
1140 | | } |
1141 | | compiler_globals->internal_run_time_cache = NULL; |
1142 | | if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) { |
1143 | | compiler_globals_ctor(compiler_globals); |
1144 | | compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list; |
1145 | | } else { |
1146 | | compiler_globals_ctor(compiler_globals); |
1147 | | } |
1148 | | free(EG(zend_constants)); |
1149 | | EG(zend_constants) = NULL; |
1150 | | |
1151 | | executor_globals_ctor(executor_globals); |
1152 | | global_persistent_list = &EG(persistent_list); |
1153 | | zend_copy_ini_directives(); |
1154 | | #else |
1155 | 14 | global_map_ptr_last = CG(map_ptr_last); |
1156 | 14 | #endif |
1157 | | |
1158 | 14 | #ifdef ZEND_CHECK_STACK_LIMIT |
1159 | 14 | zend_call_stack_init(); |
1160 | 14 | #endif |
1161 | 14 | gc_init(); |
1162 | | |
1163 | 14 | return SUCCESS; |
1164 | 14 | } |
1165 | | /* }}} */ |
1166 | | |
1167 | | void zend_shutdown(void) /* {{{ */ |
1168 | 0 | { |
1169 | 0 | zend_vm_dtor(); |
1170 | |
|
1171 | 0 | zend_destroy_rsrc_list(&EG(persistent_list)); |
1172 | | #ifdef ZTS |
1173 | | ts_apply_for_id(executor_globals_id, executor_globals_persistent_list_dtor); |
1174 | | #endif |
1175 | 0 | zend_destroy_modules(); |
1176 | |
|
1177 | 0 | virtual_cwd_deactivate(); |
1178 | 0 | virtual_cwd_shutdown(); |
1179 | |
|
1180 | 0 | zend_hash_destroy(GLOBAL_FUNCTION_TABLE); |
1181 | | /* Child classes may reuse structures from parent classes, so destroy in reverse order. */ |
1182 | 0 | zend_hash_graceful_reverse_destroy(GLOBAL_CLASS_TABLE); |
1183 | |
|
1184 | 0 | zend_flf_capacity = 0; |
1185 | 0 | zend_flf_count = 0; |
1186 | 0 | free(zend_flf_functions); |
1187 | 0 | free(zend_flf_handlers); |
1188 | 0 | zend_flf_functions = NULL; |
1189 | 0 | zend_flf_handlers = NULL; |
1190 | |
|
1191 | 0 | zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE); |
1192 | 0 | free(GLOBAL_AUTO_GLOBALS_TABLE); |
1193 | |
|
1194 | 0 | zend_shutdown_extensions(); |
1195 | 0 | free(zend_version_info); |
1196 | |
|
1197 | 0 | free(GLOBAL_FUNCTION_TABLE); |
1198 | 0 | free(GLOBAL_CLASS_TABLE); |
1199 | |
|
1200 | 0 | zend_hash_destroy(GLOBAL_CONSTANTS_TABLE); |
1201 | 0 | free(GLOBAL_CONSTANTS_TABLE); |
1202 | 0 | zend_shutdown_strtod(); |
1203 | 0 | zend_attributes_shutdown(); |
1204 | |
|
1205 | | #ifdef ZTS |
1206 | | GLOBAL_FUNCTION_TABLE = NULL; |
1207 | | GLOBAL_CLASS_TABLE = NULL; |
1208 | | GLOBAL_AUTO_GLOBALS_TABLE = NULL; |
1209 | | GLOBAL_CONSTANTS_TABLE = NULL; |
1210 | | ts_free_id(executor_globals_id); |
1211 | | ts_free_id(compiler_globals_id); |
1212 | | #else |
1213 | 0 | if (CG(map_ptr_real_base)) { |
1214 | 0 | free(CG(map_ptr_real_base)); |
1215 | 0 | CG(map_ptr_real_base) = NULL; |
1216 | 0 | CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL); |
1217 | 0 | CG(map_ptr_size) = 0; |
1218 | 0 | } |
1219 | 0 | if (CG(script_encoding_list)) { |
1220 | 0 | free(ZEND_VOIDP(CG(script_encoding_list))); |
1221 | 0 | CG(script_encoding_list) = NULL; |
1222 | 0 | CG(script_encoding_list_size) = 0; |
1223 | 0 | } |
1224 | 0 | if (CG(internal_run_time_cache)) { |
1225 | 0 | pefree(CG(internal_run_time_cache), 1); |
1226 | 0 | CG(internal_run_time_cache) = NULL; |
1227 | 0 | } |
1228 | 0 | #endif |
1229 | 0 | zend_map_ptr_static_last = 0; |
1230 | 0 | zend_map_ptr_static_size = 0; |
1231 | |
|
1232 | 0 | zend_destroy_rsrc_list_dtors(); |
1233 | |
|
1234 | 0 | zend_unload_modules(); |
1235 | |
|
1236 | 0 | zend_optimizer_shutdown(); |
1237 | 0 | startup_done = false; |
1238 | 0 | } |
1239 | | /* }}} */ |
1240 | | |
1241 | | void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */ |
1242 | 14 | { |
1243 | 14 | zend_uv = *utility_values; |
1244 | 14 | } |
1245 | | /* }}} */ |
1246 | | |
1247 | | /* this should be compatible with the standard zenderror */ |
1248 | | ZEND_COLD void zenderror(const char *error) /* {{{ */ |
1249 | 29.9k | { |
1250 | 29.9k | CG(parse_error) = 0; |
1251 | | |
1252 | 29.9k | if (EG(exception)) { |
1253 | | /* An exception was thrown in the lexer, don't throw another in the parser. */ |
1254 | 6.45k | return; |
1255 | 6.45k | } |
1256 | | |
1257 | 23.4k | zend_throw_exception(zend_ce_parse_error, error, 0); |
1258 | 23.4k | } |
1259 | | /* }}} */ |
1260 | | |
1261 | | ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */ |
1262 | 11.4k | { |
1263 | | |
1264 | 11.4k | if (!EG(bailout)) { |
1265 | 0 | zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno); |
1266 | 0 | exit(-1); |
1267 | 0 | } |
1268 | 11.4k | gc_protect(1); |
1269 | 11.4k | CG(unclean_shutdown) = 1; |
1270 | 11.4k | CG(active_class_entry) = NULL; |
1271 | 11.4k | CG(in_compilation) = 0; |
1272 | 11.4k | CG(memoize_mode) = 0; |
1273 | 11.4k | EG(current_execute_data) = NULL; |
1274 | 11.4k | LONGJMP(*EG(bailout), FAILURE); |
1275 | 11.4k | } |
1276 | | /* }}} */ |
1277 | | |
1278 | | ZEND_API size_t zend_get_page_size(void) |
1279 | 2 | { |
1280 | | #ifdef _WIN32 |
1281 | | SYSTEM_INFO system_info; |
1282 | | GetSystemInfo(&system_info); |
1283 | | return system_info.dwPageSize; |
1284 | | #elif defined(__FreeBSD__) || defined(__APPLE__) |
1285 | | /* This returns the value obtained from |
1286 | | * the auxv vector, avoiding a |
1287 | | * syscall (on FreeBSD)/function call (on macOS). */ |
1288 | | return getpagesize(); |
1289 | | #else |
1290 | 2 | return (size_t) sysconf(_SC_PAGESIZE); |
1291 | 2 | #endif |
1292 | 2 | } |
1293 | | |
1294 | | ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */ |
1295 | 14 | { |
1296 | 14 | char *new_info; |
1297 | 14 | uint32_t new_info_length; |
1298 | | |
1299 | 14 | new_info_length = (uint32_t)(sizeof(" with v, , by \n") |
1300 | 14 | + strlen(extension->name) |
1301 | 14 | + strlen(extension->version) |
1302 | 14 | + strlen(extension->copyright) |
1303 | 14 | + strlen(extension->author)); |
1304 | | |
1305 | 14 | new_info = (char *) malloc(new_info_length + 1); |
1306 | | |
1307 | 14 | snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author); |
1308 | | |
1309 | 14 | zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1); |
1310 | 14 | strncat(zend_version_info, new_info, new_info_length); |
1311 | 14 | zend_version_info_length += new_info_length; |
1312 | 14 | free(new_info); |
1313 | 14 | } |
1314 | | /* }}} */ |
1315 | | |
1316 | | ZEND_API const char *get_zend_version(void) /* {{{ */ |
1317 | 1 | { |
1318 | 1 | return zend_version_info; |
1319 | 1 | } |
1320 | | /* }}} */ |
1321 | | |
1322 | | ZEND_API void zend_activate(void) /* {{{ */ |
1323 | 222k | { |
1324 | | #ifdef ZTS |
1325 | | virtual_cwd_activate(); |
1326 | | #endif |
1327 | 222k | gc_reset(); |
1328 | 222k | init_compiler(); |
1329 | 222k | init_executor(); |
1330 | 222k | startup_scanner(); |
1331 | 222k | if (CG(map_ptr_last)) { |
1332 | 222k | memset((void **)CG(map_ptr_real_base) + zend_map_ptr_static_size, 0, CG(map_ptr_last) * sizeof(void*)); |
1333 | 222k | } |
1334 | 222k | zend_reset_internal_run_time_cache(); |
1335 | 222k | zend_observer_activate(); |
1336 | 222k | } |
1337 | | /* }}} */ |
1338 | | |
1339 | | void zend_call_destructors(void) /* {{{ */ |
1340 | 222k | { |
1341 | 222k | zend_try { |
1342 | 222k | shutdown_destructors(); |
1343 | 222k | } zend_end_try(); |
1344 | 222k | } |
1345 | | /* }}} */ |
1346 | | |
1347 | | ZEND_API void zend_deactivate(void) /* {{{ */ |
1348 | 222k | { |
1349 | | /* we're no longer executing anything */ |
1350 | 222k | EG(current_execute_data) = NULL; |
1351 | | |
1352 | 222k | zend_try { |
1353 | 222k | shutdown_scanner(); |
1354 | 222k | } zend_end_try(); |
1355 | | |
1356 | | /* shutdown_executor() takes care of its own bailout handling */ |
1357 | 222k | shutdown_executor(); |
1358 | | |
1359 | 222k | zend_try { |
1360 | 222k | zend_ini_deactivate(); |
1361 | 222k | } zend_end_try(); |
1362 | | |
1363 | 222k | zend_try { |
1364 | 222k | shutdown_compiler(); |
1365 | 222k | } zend_end_try(); |
1366 | | |
1367 | 222k | zend_destroy_rsrc_list(&EG(regular_list)); |
1368 | | |
1369 | | /* See GH-8646: https://github.com/php/php-src/issues/8646 |
1370 | | * |
1371 | | * Interned strings that hold class entries can get a corresponding slot in map_ptr for the CE cache. |
1372 | | * map_ptr works like a bump allocator: there is a counter which increases to allocate the next slot in the map. |
1373 | | * |
1374 | | * For class name strings in non-opcache we have: |
1375 | | * - on startup: permanent + interned |
1376 | | * - on request: interned |
1377 | | * For class name strings in opcache we have: |
1378 | | * - on startup: permanent + interned |
1379 | | * - on request: either not interned at all, which we can ignore because they won't get a CE cache entry |
1380 | | * or they were already permanent + interned |
1381 | | * or we get a new permanent + interned string in the opcache persistence code |
1382 | | * |
1383 | | * Notice that the map_ptr layout always has the permanent strings first, and the request strings after. |
1384 | | * In non-opcache, a request string may get a slot in map_ptr, and that interned request string |
1385 | | * gets destroyed at the end of the request. The corresponding map_ptr slot can thereafter never be used again. |
1386 | | * This causes map_ptr to keep reallocating to larger and larger sizes. |
1387 | | * |
1388 | | * We solve it as follows: |
1389 | | * We can check whether we had any interned request strings, which only happens in non-opcache. |
1390 | | * If we have any, we reset map_ptr to the last permanent string. |
1391 | | * We can't lose any permanent strings because of map_ptr's layout. |
1392 | | */ |
1393 | 222k | if (zend_hash_num_elements(&CG(interned_strings)) > 0) { |
1394 | 0 | zend_map_ptr_reset(); |
1395 | 0 | } |
1396 | | |
1397 | | #if GC_BENCH |
1398 | | gc_bench_print(); |
1399 | | #endif |
1400 | 222k | } |
1401 | | /* }}} */ |
1402 | | |
1403 | | ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */ |
1404 | 1.42k | { |
1405 | 1.42k | if (zend_message_dispatcher_p) { |
1406 | 1.42k | zend_message_dispatcher_p(message, data); |
1407 | 1.42k | } |
1408 | 1.42k | } |
1409 | | /* }}} */ |
1410 | | |
1411 | | ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */ |
1412 | 2.49k | { |
1413 | 2.49k | if (zend_get_configuration_directive_p) { |
1414 | 2.49k | return zend_get_configuration_directive_p(name); |
1415 | 2.49k | } else { |
1416 | 0 | return NULL; |
1417 | 0 | } |
1418 | 2.49k | } |
1419 | | /* }}} */ |
1420 | | |
1421 | 0 | #define SAVE_STACK(stack) do { \ |
1422 | 0 | if (CG(stack).top) { \ |
1423 | 0 | memcpy(&stack, &CG(stack), sizeof(zend_stack)); \ |
1424 | 0 | CG(stack).top = CG(stack).max = 0; \ |
1425 | 0 | CG(stack).elements = NULL; \ |
1426 | 0 | } else { \ |
1427 | 0 | stack.top = 0; \ |
1428 | 0 | } \ |
1429 | 0 | } while (0) |
1430 | | |
1431 | 0 | #define RESTORE_STACK(stack) do { \ |
1432 | 0 | if (stack.top) { \ |
1433 | 0 | zend_stack_destroy(&CG(stack)); \ |
1434 | 0 | memcpy(&CG(stack), &stack, sizeof(zend_stack)); \ |
1435 | 0 | } \ |
1436 | 0 | } while (0) |
1437 | | |
1438 | | ZEND_API ZEND_COLD void zend_error_zstr_at( |
1439 | | int orig_type, zend_string *error_filename, uint32_t error_lineno, zend_string *message) |
1440 | 1.78M | { |
1441 | 1.78M | zval params[4]; |
1442 | 1.78M | zval retval; |
1443 | 1.78M | zval orig_user_error_handler; |
1444 | 1.78M | bool in_compilation; |
1445 | 1.78M | zend_class_entry *saved_class_entry = NULL; |
1446 | 1.78M | zend_stack loop_var_stack; |
1447 | 1.78M | zend_stack delayed_oplines_stack; |
1448 | 1.78M | int type = orig_type & E_ALL; |
1449 | 1.78M | bool orig_record_errors; |
1450 | 1.78M | uint32_t orig_num_errors; |
1451 | 1.78M | zend_error_info **orig_errors; |
1452 | 1.78M | zend_result res; |
1453 | | |
1454 | | /* If we're executing a function during SCCP, count any warnings that may be emitted, |
1455 | | * but don't perform any other error handling. */ |
1456 | 1.78M | if (EG(capture_warnings_during_sccp)) { |
1457 | 0 | ZEND_ASSERT(!(type & E_FATAL_ERRORS) && "Fatal error during SCCP"); |
1458 | 0 | EG(capture_warnings_during_sccp)++; |
1459 | 0 | return; |
1460 | 0 | } |
1461 | | |
1462 | | /* Emit any delayed error before handling fatal error */ |
1463 | 1.78M | if ((type & E_FATAL_ERRORS) && !(type & E_DONT_BAIL) && EG(num_errors)) { |
1464 | 229 | uint32_t num_errors = EG(num_errors); |
1465 | 229 | zend_error_info **errors = EG(errors); |
1466 | 229 | EG(num_errors) = 0; |
1467 | 229 | EG(errors) = NULL; |
1468 | | |
1469 | 229 | bool orig_record_errors = EG(record_errors); |
1470 | 229 | EG(record_errors) = false; |
1471 | | |
1472 | | /* Disable user error handler before emitting delayed errors, as |
1473 | | * it's unsafe to execute user code after a fatal error. */ |
1474 | 229 | int orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting); |
1475 | 229 | EG(user_error_handler_error_reporting) = 0; |
1476 | | |
1477 | 229 | zend_emit_recorded_errors_ex(num_errors, errors); |
1478 | | |
1479 | 229 | EG(user_error_handler_error_reporting) = orig_user_error_handler_error_reporting; |
1480 | 229 | EG(record_errors) = orig_record_errors; |
1481 | 229 | EG(num_errors) = num_errors; |
1482 | 229 | EG(errors) = errors; |
1483 | 229 | } |
1484 | | |
1485 | 1.78M | if (EG(record_errors)) { |
1486 | 99.8k | zend_error_info *info = emalloc(sizeof(zend_error_info)); |
1487 | 99.8k | info->type = type; |
1488 | 99.8k | info->lineno = error_lineno; |
1489 | 99.8k | info->filename = zend_string_copy(error_filename); |
1490 | 99.8k | info->message = zend_string_copy(message); |
1491 | | |
1492 | | /* This is very inefficient for a large number of errors. |
1493 | | * Use pow2 realloc if it becomes a problem. */ |
1494 | 99.8k | EG(num_errors)++; |
1495 | 99.8k | EG(errors) = erealloc(EG(errors), sizeof(zend_error_info*) * EG(num_errors)); |
1496 | 99.8k | EG(errors)[EG(num_errors)-1] = info; |
1497 | | |
1498 | | /* Do not process non-fatal recorded error */ |
1499 | 99.8k | if (!(type & E_FATAL_ERRORS) || (type & E_DONT_BAIL)) { |
1500 | 96.4k | return; |
1501 | 96.4k | } |
1502 | 99.8k | } |
1503 | | |
1504 | | // Always clear the last backtrace. |
1505 | 1.68M | zval_ptr_dtor(&EG(last_fatal_error_backtrace)); |
1506 | 1.68M | ZVAL_UNDEF(&EG(last_fatal_error_backtrace)); |
1507 | | |
1508 | | /* Report about uncaught exception in case of fatal errors */ |
1509 | 1.68M | if (EG(exception)) { |
1510 | 2.46k | if (type & E_FATAL_ERRORS) { |
1511 | 0 | zend_execute_data *ex = EG(current_execute_data); |
1512 | 0 | const zend_op *opline = NULL; |
1513 | |
|
1514 | 0 | while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) { |
1515 | 0 | ex = ex->prev_execute_data; |
1516 | 0 | } |
1517 | 0 | if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION && |
1518 | 0 | EG(opline_before_exception)) { |
1519 | 0 | opline = EG(opline_before_exception); |
1520 | 0 | } |
1521 | 0 | zend_exception_error(EG(exception), E_WARNING); |
1522 | 0 | EG(exception) = NULL; |
1523 | 0 | if (opline) { |
1524 | 0 | ex->opline = opline; |
1525 | 0 | } |
1526 | 0 | } |
1527 | 1.68M | } else if (EG(fatal_error_backtrace_on) && (type & E_FATAL_ERRORS)) { |
1528 | 3.83k | zend_fetch_debug_backtrace(&EG(last_fatal_error_backtrace), 0, EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0); |
1529 | 3.83k | } |
1530 | | |
1531 | 1.68M | zend_observer_error_notify(type, error_filename, error_lineno, message); |
1532 | | |
1533 | | /* if we don't have a user defined error handler */ |
1534 | 1.68M | if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF |
1535 | 0 | || !(EG(user_error_handler_error_reporting) & type) |
1536 | 1.68M | || EG(error_handling) != EH_NORMAL) { |
1537 | 1.68M | zend_error_cb(orig_type, error_filename, error_lineno, message); |
1538 | 1.68M | } else switch (type) { |
1539 | 0 | case E_ERROR: |
1540 | 0 | case E_PARSE: |
1541 | 0 | case E_CORE_ERROR: |
1542 | 0 | case E_CORE_WARNING: |
1543 | 0 | case E_COMPILE_ERROR: |
1544 | 0 | case E_COMPILE_WARNING: |
1545 | | /* The error may not be safe to handle in user-space */ |
1546 | 0 | zend_error_cb(orig_type, error_filename, error_lineno, message); |
1547 | 0 | break; |
1548 | 0 | default: |
1549 | | /* Handle the error in user space */ |
1550 | 0 | ZVAL_STR_COPY(¶ms[1], message); |
1551 | 0 | ZVAL_LONG(¶ms[0], type); |
1552 | |
|
1553 | 0 | if (error_filename) { |
1554 | 0 | ZVAL_STR_COPY(¶ms[2], error_filename); |
1555 | 0 | } else { |
1556 | 0 | ZVAL_NULL(¶ms[2]); |
1557 | 0 | } |
1558 | |
|
1559 | 0 | ZVAL_LONG(¶ms[3], error_lineno); |
1560 | |
|
1561 | 0 | ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler)); |
1562 | 0 | ZVAL_UNDEF(&EG(user_error_handler)); |
1563 | | |
1564 | | /* User error handler may include() additional PHP files. |
1565 | | * If an error was generated during compilation PHP will compile |
1566 | | * such scripts recursively, but some CG() variables may be |
1567 | | * inconsistent. */ |
1568 | |
|
1569 | 0 | in_compilation = CG(in_compilation); |
1570 | 0 | if (in_compilation) { |
1571 | 0 | saved_class_entry = CG(active_class_entry); |
1572 | 0 | CG(active_class_entry) = NULL; |
1573 | 0 | SAVE_STACK(loop_var_stack); |
1574 | 0 | SAVE_STACK(delayed_oplines_stack); |
1575 | 0 | CG(in_compilation) = 0; |
1576 | 0 | } |
1577 | |
|
1578 | 0 | orig_record_errors = EG(record_errors); |
1579 | 0 | orig_num_errors = EG(num_errors); |
1580 | 0 | orig_errors = EG(errors); |
1581 | 0 | EG(record_errors) = false; |
1582 | 0 | EG(num_errors) = 0; |
1583 | 0 | EG(errors) = NULL; |
1584 | |
|
1585 | 0 | res = call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 4, params); |
1586 | |
|
1587 | 0 | EG(record_errors) = orig_record_errors; |
1588 | 0 | EG(num_errors) = orig_num_errors; |
1589 | 0 | EG(errors) = orig_errors; |
1590 | |
|
1591 | 0 | if (res == SUCCESS) { |
1592 | 0 | if (Z_TYPE(retval) != IS_UNDEF) { |
1593 | 0 | if (Z_TYPE(retval) == IS_FALSE) { |
1594 | 0 | zend_error_cb(orig_type, error_filename, error_lineno, message); |
1595 | 0 | } |
1596 | 0 | zval_ptr_dtor(&retval); |
1597 | 0 | } |
1598 | 0 | } else if (!EG(exception)) { |
1599 | | /* The user error handler failed, use built-in error handler */ |
1600 | 0 | zend_error_cb(orig_type, error_filename, error_lineno, message); |
1601 | 0 | } |
1602 | |
|
1603 | 0 | if (in_compilation) { |
1604 | 0 | CG(active_class_entry) = saved_class_entry; |
1605 | 0 | RESTORE_STACK(loop_var_stack); |
1606 | 0 | RESTORE_STACK(delayed_oplines_stack); |
1607 | 0 | CG(in_compilation) = 1; |
1608 | 0 | } |
1609 | |
|
1610 | 0 | zval_ptr_dtor(¶ms[2]); |
1611 | 0 | zval_ptr_dtor(¶ms[1]); |
1612 | |
|
1613 | 0 | if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) { |
1614 | 0 | ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler); |
1615 | 0 | } else { |
1616 | 0 | zval_ptr_dtor(&orig_user_error_handler); |
1617 | 0 | } |
1618 | 0 | break; |
1619 | 4 | } |
1620 | | |
1621 | 1.68M | if (type == E_PARSE) { |
1622 | | /* eval() errors do not affect exit_status */ |
1623 | 0 | if (!(EG(current_execute_data) && |
1624 | 0 | EG(current_execute_data)->func && |
1625 | 0 | ZEND_USER_CODE(EG(current_execute_data)->func->type) && |
1626 | 0 | EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL && |
1627 | 0 | EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) { |
1628 | 0 | EG(exit_status) = 255; |
1629 | 0 | } |
1630 | 0 | } |
1631 | 1.68M | } |
1632 | | /* }}} */ |
1633 | | |
1634 | | static ZEND_COLD void zend_error_va_list( |
1635 | | int orig_type, zend_string *error_filename, uint32_t error_lineno, |
1636 | | const char *format, va_list args) |
1637 | 1.32M | { |
1638 | 1.32M | zend_string *message = zend_vstrpprintf(0, format, args); |
1639 | 1.32M | zend_error_zstr_at(orig_type, error_filename, error_lineno, message); |
1640 | 1.32M | zend_string_release(message); |
1641 | 1.32M | } |
1642 | | |
1643 | 1.68M | static ZEND_COLD void get_filename_lineno(int type, zend_string **filename, uint32_t *lineno) { |
1644 | | /* Obtain relevant filename and lineno */ |
1645 | 1.68M | switch (type) { |
1646 | 2 | case E_CORE_ERROR: |
1647 | 2 | case E_CORE_WARNING: |
1648 | 2 | *filename = NULL; |
1649 | 2 | *lineno = 0; |
1650 | 2 | break; |
1651 | 0 | case E_PARSE: |
1652 | 2.89k | case E_COMPILE_ERROR: |
1653 | 50.9k | case E_COMPILE_WARNING: |
1654 | 51.4k | case E_ERROR: |
1655 | 59.4k | case E_NOTICE: |
1656 | 831k | case E_DEPRECATED: |
1657 | 1.68M | case E_WARNING: |
1658 | 1.68M | case E_USER_ERROR: |
1659 | 1.68M | case E_USER_WARNING: |
1660 | 1.68M | case E_USER_NOTICE: |
1661 | 1.68M | case E_USER_DEPRECATED: |
1662 | 1.68M | case E_RECOVERABLE_ERROR: |
1663 | 1.68M | if (zend_is_compiling()) { |
1664 | 99.0k | *filename = zend_get_compiled_filename(); |
1665 | 99.0k | *lineno = zend_get_compiled_lineno(); |
1666 | 1.58M | } else if (zend_is_executing()) { |
1667 | 1.57M | *filename = zend_get_executed_filename_ex(); |
1668 | 1.57M | *lineno = zend_get_executed_lineno(); |
1669 | 1.57M | } else { |
1670 | 13.2k | *filename = NULL; |
1671 | 13.2k | *lineno = 0; |
1672 | 13.2k | } |
1673 | 1.68M | break; |
1674 | 0 | default: |
1675 | 0 | *filename = NULL; |
1676 | 0 | *lineno = 0; |
1677 | 0 | break; |
1678 | 1.68M | } |
1679 | 1.68M | if (!*filename) { |
1680 | 866k | *filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED); |
1681 | 866k | } |
1682 | 1.68M | } |
1683 | | |
1684 | | ZEND_API ZEND_COLD void zend_error_at( |
1685 | 761 | int type, zend_string *filename, uint32_t lineno, const char *format, ...) { |
1686 | 761 | va_list args; |
1687 | | |
1688 | 761 | if (!filename) { |
1689 | 0 | uint32_t dummy_lineno; |
1690 | 0 | get_filename_lineno(type, &filename, &dummy_lineno); |
1691 | 0 | } |
1692 | | |
1693 | 761 | va_start(args, format); |
1694 | 761 | zend_error_va_list(type, filename, lineno, format, args); |
1695 | 761 | va_end(args); |
1696 | 761 | } |
1697 | | |
1698 | 1.32M | #define zend_error_impl(type, format) do { \ |
1699 | 1.32M | zend_string *filename; \ |
1700 | 1.32M | uint32_t lineno; \ |
1701 | 1.32M | va_list args; \ |
1702 | 1.32M | get_filename_lineno(type, &filename, &lineno); \ |
1703 | 1.32M | va_start(args, format); \ |
1704 | 1.32M | zend_error_va_list(type, filename, lineno, format, args); \ |
1705 | 1.32M | va_end(args); \ |
1706 | 1.32M | } while (0) |
1707 | | |
1708 | 620k | ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) { |
1709 | 620k | zend_error_impl(type, format); |
1710 | 620k | } |
1711 | | |
1712 | 704k | ZEND_API ZEND_COLD void zend_error_unchecked(int type, const char *format, ...) { |
1713 | 704k | zend_error_impl(type, format); |
1714 | 704k | } |
1715 | | |
1716 | | ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn( |
1717 | | int type, zend_string *filename, uint32_t lineno, const char *format, ...) |
1718 | 23 | { |
1719 | 23 | va_list args; |
1720 | | |
1721 | 23 | if (!filename) { |
1722 | 0 | uint32_t dummy_lineno; |
1723 | 0 | get_filename_lineno(type, &filename, &dummy_lineno); |
1724 | 0 | } |
1725 | | |
1726 | 23 | va_start(args, format); |
1727 | 23 | zend_error_va_list(type, filename, lineno, format, args); |
1728 | 23 | va_end(args); |
1729 | | /* Should never reach this. */ |
1730 | 23 | abort(); |
1731 | 23 | } |
1732 | | |
1733 | 3.15k | #define zend_error_noreturn_impl(type, format) do { \ |
1734 | 3.15k | zend_string *filename; \ |
1735 | 3.15k | uint32_t lineno; \ |
1736 | 3.15k | va_list args; \ |
1737 | 3.15k | get_filename_lineno(type, &filename, &lineno); \ |
1738 | 3.15k | va_start(args, format); \ |
1739 | 3.15k | zend_error_va_list(type, filename, lineno, format, args); \ |
1740 | 3.15k | va_end(args); \ |
1741 | 3.15k | /* Should never reach this. */ \ |
1742 | 3.15k | abort(); \ |
1743 | 3.15k | } while (0) |
1744 | | |
1745 | | ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...) |
1746 | 3.12k | { |
1747 | 3.12k | zend_error_noreturn_impl(type, format); |
1748 | 3.12k | } |
1749 | | |
1750 | | ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn_unchecked(int type, const char *format, ...) |
1751 | 21 | { |
1752 | 21 | zend_error_noreturn_impl(type, format); |
1753 | 21 | } |
1754 | | |
1755 | | ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message) |
1756 | 0 | { |
1757 | 0 | #ifdef HAVE_STRERROR_R |
1758 | 0 | char b[1024]; |
1759 | |
|
1760 | 0 | # ifdef STRERROR_R_CHAR_P |
1761 | 0 | char *buf = strerror_r(errn, b, sizeof(b)); |
1762 | | # else |
1763 | | strerror_r(errn, b, sizeof(b)); |
1764 | | char *buf = b; |
1765 | | # endif |
1766 | | #else |
1767 | | char *buf = strerror(errn); |
1768 | | #endif |
1769 | |
|
1770 | 0 | zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn); |
1771 | 0 | } |
1772 | | |
1773 | 356k | ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) { |
1774 | 356k | zend_string *filename; |
1775 | 356k | uint32_t lineno; |
1776 | 356k | get_filename_lineno(type, &filename, &lineno); |
1777 | 356k | zend_error_zstr_at(type, filename, lineno, message); |
1778 | 356k | } |
1779 | | |
1780 | | ZEND_API void zend_begin_record_errors(void) |
1781 | 72.4k | { |
1782 | 72.4k | ZEND_ASSERT(!EG(record_errors) && "Error recording already enabled"); |
1783 | 72.4k | EG(record_errors) = true; |
1784 | 72.4k | EG(num_errors) = 0; |
1785 | 72.4k | EG(errors) = NULL; |
1786 | 72.4k | } |
1787 | | |
1788 | | ZEND_API void zend_emit_recorded_errors_ex(uint32_t num_errors, zend_error_info **errors) |
1789 | 145k | { |
1790 | 243k | for (uint32_t i = 0; i < num_errors; i++) { |
1791 | 97.7k | zend_error_info *error = errors[i]; |
1792 | 97.7k | zend_error_zstr_at(error->type, error->filename, error->lineno, error->message); |
1793 | 97.7k | } |
1794 | 145k | } |
1795 | | |
1796 | | ZEND_API void zend_emit_recorded_errors(void) |
1797 | 69.0k | { |
1798 | 69.0k | EG(record_errors) = false; |
1799 | 69.0k | zend_emit_recorded_errors_ex(EG(num_errors), EG(errors)); |
1800 | 69.0k | } |
1801 | | |
1802 | | ZEND_API void zend_free_recorded_errors(void) |
1803 | 69.2k | { |
1804 | 69.2k | if (!EG(num_errors)) { |
1805 | 67.1k | return; |
1806 | 67.1k | } |
1807 | | |
1808 | 65.0k | for (uint32_t i = 0; i < EG(num_errors); i++) { |
1809 | 62.8k | zend_error_info *info = EG(errors)[i]; |
1810 | 62.8k | zend_string_release(info->filename); |
1811 | 62.8k | zend_string_release(info->message); |
1812 | 62.8k | efree(info); |
1813 | 62.8k | } |
1814 | 2.16k | efree(EG(errors)); |
1815 | 2.16k | EG(errors) = NULL; |
1816 | 2.16k | EG(num_errors) = 0; |
1817 | 2.16k | } |
1818 | | |
1819 | | ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */ |
1820 | 89.2k | { |
1821 | 89.2k | va_list va; |
1822 | 89.2k | char *message = NULL; |
1823 | | |
1824 | 89.2k | if (!exception_ce) { |
1825 | 88.7k | exception_ce = zend_ce_error; |
1826 | 88.7k | } |
1827 | | |
1828 | | /* Marker used to disable exception generation during preloading. */ |
1829 | 89.2k | if (EG(exception) == (void*)(uintptr_t)-1) { |
1830 | 0 | return; |
1831 | 0 | } |
1832 | | |
1833 | 89.2k | va_start(va, format); |
1834 | 89.2k | zend_vspprintf(&message, 0, format, va); |
1835 | | |
1836 | | //TODO: we can't convert compile-time errors to exceptions yet??? |
1837 | 89.2k | if (EG(current_execute_data) && !CG(in_compilation)) { |
1838 | 89.2k | zend_throw_exception(exception_ce, message, 0); |
1839 | 89.2k | } else { |
1840 | 3 | zend_error_noreturn(E_ERROR, "%s", message); |
1841 | 3 | } |
1842 | | |
1843 | 89.2k | efree(message); |
1844 | 89.2k | va_end(va); |
1845 | 89.2k | } |
1846 | | /* }}} */ |
1847 | | |
1848 | | /* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */ |
1849 | | ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type) |
1850 | 94 | { |
1851 | 94 | switch (type) { |
1852 | 0 | case BP_VAR_IS: |
1853 | 0 | zend_type_error("Cannot access offset of type %s in isset or empty", |
1854 | 0 | zend_zval_type_name(offset)); |
1855 | 0 | return; |
1856 | 1 | case BP_VAR_UNSET: |
1857 | | /* Consistent error for when trying to unset a string offset */ |
1858 | 1 | if (zend_string_equals(container, ZSTR_KNOWN(ZEND_STR_STRING))) { |
1859 | 1 | zend_throw_error(NULL, "Cannot unset string offsets"); |
1860 | 1 | } else { |
1861 | 0 | zend_type_error("Cannot unset offset of type %s on %s", zend_zval_type_name(offset), ZSTR_VAL(container)); |
1862 | 0 | } |
1863 | 1 | return; |
1864 | 93 | default: |
1865 | 93 | zend_type_error("Cannot access offset of type %s on %s", |
1866 | 93 | zend_zval_type_name(offset), ZSTR_VAL(container)); |
1867 | 93 | return; |
1868 | 94 | } |
1869 | 94 | } |
1870 | | |
1871 | | ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */ |
1872 | 2.03k | { |
1873 | 2.03k | va_list va; |
1874 | 2.03k | char *message = NULL; |
1875 | | |
1876 | 2.03k | va_start(va, format); |
1877 | 2.03k | zend_vspprintf(&message, 0, format, va); |
1878 | 2.03k | zend_throw_exception(zend_ce_type_error, message, 0); |
1879 | 2.03k | efree(message); |
1880 | 2.03k | va_end(va); |
1881 | 2.03k | } /* }}} */ |
1882 | | |
1883 | | ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) /* {{{ */ |
1884 | 154 | { |
1885 | 154 | va_list va; |
1886 | 154 | char *message = NULL; |
1887 | | |
1888 | 154 | va_start(va, format); |
1889 | 154 | zend_vspprintf(&message, 0, format, va); |
1890 | 154 | zend_throw_exception(zend_ce_argument_count_error, message, 0); |
1891 | 154 | efree(message); |
1892 | | |
1893 | 154 | va_end(va); |
1894 | 154 | } /* }}} */ |
1895 | | |
1896 | | ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) /* {{{ */ |
1897 | 7 | { |
1898 | 7 | va_list va; |
1899 | 7 | char *message = NULL; |
1900 | | |
1901 | 7 | va_start(va, format); |
1902 | 7 | zend_vspprintf(&message, 0, format, va); |
1903 | 7 | zend_throw_exception(zend_ce_value_error, message, 0); |
1904 | 7 | efree(message); |
1905 | 7 | va_end(va); |
1906 | 7 | } /* }}} */ |
1907 | | |
1908 | | ZEND_API ZEND_COLD void zend_output_debug_string(bool trigger_break, const char *format, ...) /* {{{ */ |
1909 | 0 | { |
1910 | 0 | #if ZEND_DEBUG |
1911 | 0 | va_list args; |
1912 | |
|
1913 | 0 | va_start(args, format); |
1914 | | # ifdef ZEND_WIN32 |
1915 | | { |
1916 | | char output_buf[1024]; |
1917 | | |
1918 | | vsnprintf(output_buf, 1024, format, args); |
1919 | | OutputDebugString(output_buf); |
1920 | | OutputDebugString("\n"); |
1921 | | if (trigger_break && IsDebuggerPresent()) { |
1922 | | DebugBreak(); |
1923 | | } |
1924 | | } |
1925 | | # else |
1926 | 0 | vfprintf(stderr, format, args); |
1927 | 0 | fprintf(stderr, "\n"); |
1928 | 0 | # endif |
1929 | 0 | va_end(args); |
1930 | 0 | #endif |
1931 | 0 | } |
1932 | | /* }}} */ |
1933 | | |
1934 | | ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */ |
1935 | 1 | { |
1936 | 1 | zval orig_user_exception_handler; |
1937 | 1 | zval params[1], retval2; |
1938 | 1 | zend_object *old_exception; |
1939 | | |
1940 | 1 | if (zend_is_unwind_exit(EG(exception))) { |
1941 | 0 | return; |
1942 | 0 | } |
1943 | | |
1944 | 1 | old_exception = EG(exception); |
1945 | 1 | EG(exception) = NULL; |
1946 | 1 | ZVAL_OBJ(¶ms[0], old_exception); |
1947 | | |
1948 | 1 | ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler)); |
1949 | 1 | zend_stack_push(&EG(user_exception_handlers), &orig_user_exception_handler); |
1950 | 1 | ZVAL_UNDEF(&EG(user_exception_handler)); |
1951 | | |
1952 | 1 | if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) { |
1953 | 1 | zval_ptr_dtor(&retval2); |
1954 | 1 | if (EG(exception)) { |
1955 | 0 | OBJ_RELEASE(EG(exception)); |
1956 | 0 | EG(exception) = NULL; |
1957 | 0 | } |
1958 | 1 | OBJ_RELEASE(old_exception); |
1959 | 1 | } else { |
1960 | 0 | EG(exception) = old_exception; |
1961 | 0 | } |
1962 | | |
1963 | 1 | if (Z_TYPE(EG(user_exception_handler)) == IS_UNDEF) { |
1964 | 1 | zval *tmp = zend_stack_top(&EG(user_exception_handlers)); |
1965 | 1 | if (tmp) { |
1966 | 1 | ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp); |
1967 | 1 | zend_stack_del_top(&EG(user_exception_handlers)); |
1968 | 1 | } |
1969 | 1 | } |
1970 | 1 | } /* }}} */ |
1971 | | |
1972 | | ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle) |
1973 | 0 | { |
1974 | 0 | zend_op_array *op_array = zend_compile_file(file_handle, type); |
1975 | 0 | if (file_handle->opened_path) { |
1976 | 0 | zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path); |
1977 | 0 | } |
1978 | |
|
1979 | 0 | zend_result ret = SUCCESS; |
1980 | 0 | if (op_array) { |
1981 | 0 | zend_execute(op_array, retval); |
1982 | 0 | zend_exception_restore(); |
1983 | 0 | if (UNEXPECTED(EG(exception))) { |
1984 | 0 | if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) { |
1985 | 0 | zend_user_exception_handler(); |
1986 | 0 | } |
1987 | 0 | if (EG(exception)) { |
1988 | 0 | ret = zend_exception_error(EG(exception), E_ERROR); |
1989 | 0 | } |
1990 | 0 | } |
1991 | 0 | zend_destroy_static_vars(op_array); |
1992 | 0 | destroy_op_array(op_array); |
1993 | 0 | efree_size(op_array, sizeof(zend_op_array)); |
1994 | 0 | } else if (type == ZEND_REQUIRE) { |
1995 | 0 | ret = FAILURE; |
1996 | 0 | } |
1997 | |
|
1998 | 0 | return ret; |
1999 | 0 | } |
2000 | | |
2001 | | ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */ |
2002 | 0 | { |
2003 | 0 | va_list files; |
2004 | 0 | int i; |
2005 | 0 | zend_file_handle *file_handle; |
2006 | 0 | zend_result ret = SUCCESS; |
2007 | |
|
2008 | 0 | va_start(files, file_count); |
2009 | 0 | for (i = 0; i < file_count; i++) { |
2010 | 0 | file_handle = va_arg(files, zend_file_handle *); |
2011 | 0 | if (!file_handle) { |
2012 | 0 | continue; |
2013 | 0 | } |
2014 | 0 | if (ret == FAILURE) { |
2015 | 0 | continue; |
2016 | 0 | } |
2017 | 0 | ret = zend_execute_script(type, retval, file_handle); |
2018 | 0 | } |
2019 | 0 | va_end(files); |
2020 | |
|
2021 | 0 | return ret; |
2022 | 0 | } |
2023 | | /* }}} */ |
2024 | | |
2025 | 5.48k | #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s" |
2026 | | |
2027 | | ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */ |
2028 | 5.48k | { |
2029 | 5.48k | const char *cur_filename; |
2030 | 5.48k | int cur_lineno; |
2031 | 5.48k | char *compiled_string_description; |
2032 | | |
2033 | 5.48k | if (zend_is_compiling()) { |
2034 | 0 | cur_filename = ZSTR_VAL(zend_get_compiled_filename()); |
2035 | 0 | cur_lineno = zend_get_compiled_lineno(); |
2036 | 5.48k | } else if (zend_is_executing()) { |
2037 | 5.48k | cur_filename = zend_get_executed_filename(); |
2038 | 5.48k | cur_lineno = zend_get_executed_lineno(); |
2039 | 5.48k | } else { |
2040 | 0 | cur_filename = "Unknown"; |
2041 | 0 | cur_lineno = 0; |
2042 | 0 | } |
2043 | | |
2044 | 5.48k | zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name); |
2045 | 5.48k | return compiled_string_description; |
2046 | 5.48k | } |
2047 | | /* }}} */ |
2048 | | |
2049 | | void free_estring(char **str_p) /* {{{ */ |
2050 | 0 | { |
2051 | 0 | efree(*str_p); |
2052 | 0 | } |
2053 | | /* }}} */ |
2054 | | |
2055 | | ZEND_API size_t zend_map_ptr_static_size; |
2056 | | ZEND_API size_t zend_map_ptr_static_last; |
2057 | | |
2058 | | ZEND_API void zend_map_ptr_reset(void) |
2059 | 0 | { |
2060 | 0 | CG(map_ptr_last) = global_map_ptr_last; |
2061 | 0 | } |
2062 | | |
2063 | | ZEND_API void *zend_map_ptr_new(void) |
2064 | 8.42k | { |
2065 | 8.42k | void **ptr; |
2066 | | |
2067 | 8.42k | if (CG(map_ptr_last) >= CG(map_ptr_size)) { |
2068 | | /* Grow map_ptr table */ |
2069 | 15 | CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096); |
2070 | 15 | CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); |
2071 | 15 | CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base)); |
2072 | 15 | } |
2073 | 8.42k | ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last); |
2074 | 8.42k | *ptr = NULL; |
2075 | 8.42k | CG(map_ptr_last)++; |
2076 | 8.42k | return ZEND_MAP_PTR_PTR2OFFSET(ptr); |
2077 | 8.42k | } |
2078 | | |
2079 | | ZEND_API void *zend_map_ptr_new_static(void) |
2080 | 0 | { |
2081 | 0 | void **ptr; |
2082 | |
|
2083 | 0 | if (zend_map_ptr_static_last >= zend_map_ptr_static_size) { |
2084 | 0 | zend_map_ptr_static_size += 4096; |
2085 | | /* Grow map_ptr table */ |
2086 | 0 | void *new_base = pemalloc((zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); |
2087 | 0 | if (CG(map_ptr_real_base)) { |
2088 | 0 | memcpy((void **)new_base + 4096, CG(map_ptr_real_base), (CG(map_ptr_last) + zend_map_ptr_static_size - 4096) * sizeof(void *)); |
2089 | 0 | pefree(CG(map_ptr_real_base), 1); |
2090 | 0 | } |
2091 | 0 | CG(map_ptr_real_base) = new_base; |
2092 | 0 | CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(new_base); |
2093 | 0 | } |
2094 | 0 | ptr = (void**)CG(map_ptr_real_base) + (zend_map_ptr_static_last & 4095); |
2095 | 0 | *ptr = NULL; |
2096 | 0 | zend_map_ptr_static_last++; |
2097 | 0 | return ZEND_MAP_PTR_PTR2OFFSET(ptr); |
2098 | 0 | } |
2099 | | |
2100 | | ZEND_API void zend_map_ptr_extend(size_t last) |
2101 | 12.6k | { |
2102 | 12.6k | if (last > CG(map_ptr_last)) { |
2103 | 0 | void **ptr; |
2104 | |
|
2105 | 0 | if (last >= CG(map_ptr_size)) { |
2106 | | /* Grow map_ptr table */ |
2107 | 0 | CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096); |
2108 | 0 | CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); |
2109 | 0 | CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base)); |
2110 | 0 | } |
2111 | 0 | ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last); |
2112 | 0 | memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*)); |
2113 | 0 | CG(map_ptr_last) = last; |
2114 | 0 | } |
2115 | 12.6k | } |
2116 | | |
2117 | | ZEND_API void zend_alloc_ce_cache(zend_string *type_name) |
2118 | 463k | { |
2119 | 463k | if (ZSTR_HAS_CE_CACHE(type_name) || !ZSTR_IS_INTERNED(type_name)) { |
2120 | 436k | return; |
2121 | 436k | } |
2122 | | |
2123 | 26.4k | if ((GC_FLAGS(type_name) & IS_STR_PERMANENT) && startup_done) { |
2124 | | /* Don't allocate slot on permanent interned string outside module startup. |
2125 | | * The cache slot would no longer be valid on the next request. */ |
2126 | 24.1k | return; |
2127 | 24.1k | } |
2128 | | |
2129 | 2.31k | if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF)) |
2130 | 2.31k | || zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
2131 | 0 | return; |
2132 | 0 | } |
2133 | | |
2134 | | /* We use the refcount to keep map_ptr of corresponding type */ |
2135 | 2.31k | uint32_t ret; |
2136 | 2.32k | do { |
2137 | 2.32k | ret = ZEND_MAP_PTR_NEW_OFFSET(); |
2138 | 2.32k | } while (ret <= 2); |
2139 | 2.31k | GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR); |
2140 | 2.31k | GC_SET_REFCOUNT(type_name, ret); |
2141 | 2.31k | } |