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
38 static size_t global_map_ptr_last = 0;
39
40 #ifdef ZTS
41 ZEND_API int compiler_globals_id;
42 ZEND_API int executor_globals_id;
43 ZEND_API size_t compiler_globals_offset;
44 ZEND_API size_t executor_globals_offset;
45 static HashTable *global_function_table = NULL;
46 static HashTable *global_class_table = NULL;
47 static HashTable *global_constants_table = NULL;
48 static HashTable *global_auto_globals_table = NULL;
49 static HashTable *global_persistent_list = NULL;
50 ZEND_TSRMLS_CACHE_DEFINE()
51 # define GLOBAL_FUNCTION_TABLE global_function_table
52 # define GLOBAL_CLASS_TABLE global_class_table
53 # define GLOBAL_CONSTANTS_TABLE global_constants_table
54 # define GLOBAL_AUTO_GLOBALS_TABLE global_auto_globals_table
55 #else
56 # define GLOBAL_FUNCTION_TABLE CG(function_table)
57 # define GLOBAL_CLASS_TABLE CG(class_table)
58 # define GLOBAL_AUTO_GLOBALS_TABLE CG(auto_globals)
59 # define GLOBAL_CONSTANTS_TABLE EG(zend_constants)
60 #endif
61
62 ZEND_API zend_utility_values zend_uv;
63 ZEND_API zend_bool zend_dtrace_enabled;
64
65 /* version information */
66 static char *zend_version_info;
67 static uint32_t zend_version_info_length;
68 #define ZEND_CORE_VERSION_INFO "Zend Engine v" ZEND_VERSION ", Copyright (c) Zend Technologies\n"
69 #define PRINT_ZVAL_INDENT 4
70
71 /* true multithread-shared globals */
72 ZEND_API zend_class_entry *zend_standard_class_def = NULL;
73 ZEND_API size_t (*zend_printf)(const char *format, ...);
74 ZEND_API zend_write_func_t zend_write;
75 ZEND_API FILE *(*zend_fopen)(const char *filename, zend_string **opened_path);
76 ZEND_API zend_result (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
77 ZEND_API void (*zend_ticks_function)(int ticks);
78 ZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data);
79 ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint32_t error_lineno, zend_string *message);
80 void (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap);
81 void (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap);
82 ZEND_API char *(*zend_getenv)(const char *name, size_t name_len);
83 ZEND_API zend_string *(*zend_resolve_path)(const char *filename, size_t filename_len);
84 ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL;
85 ZEND_API void (*zend_post_shutdown_cb)(void) = NULL;
86 ZEND_API zend_result (*zend_preload_autoload)(zend_string *filename) = NULL;
87
88 /* This callback must be signal handler safe! */
89 void (*zend_on_timeout)(int seconds);
90
91 static void (*zend_message_dispatcher_p)(zend_long message, const void *data);
92 static zval *(*zend_get_configuration_directive_p)(zend_string *name);
93
94 #if ZEND_RC_DEBUG
95 ZEND_API zend_bool zend_rc_debug = 0;
96 #endif
97
ZEND_INI_MH(OnUpdateErrorReporting)98 static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
99 {
100 if (!new_value) {
101 EG(error_reporting) = E_ALL;
102 } else {
103 EG(error_reporting) = atoi(ZSTR_VAL(new_value));
104 }
105 return SUCCESS;
106 }
107 /* }}} */
108
ZEND_INI_MH(OnUpdateGCEnabled)109 static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
110 {
111 zend_bool val;
112
113 val = zend_ini_parse_bool(new_value);
114 gc_enable(val);
115
116 return SUCCESS;
117 }
118 /* }}} */
119
ZEND_INI_DISP(zend_gc_enabled_displayer_cb)120 static ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */
121 {
122 if (gc_enabled()) {
123 ZEND_PUTS("On");
124 } else {
125 ZEND_PUTS("Off");
126 }
127 }
128 /* }}} */
129
130
ZEND_INI_MH(OnUpdateScriptEncoding)131 static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
132 {
133 if (!CG(multibyte)) {
134 return FAILURE;
135 }
136 if (!zend_multibyte_get_functions()) {
137 return SUCCESS;
138 }
139 return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
140 }
141 /* }}} */
142
ZEND_INI_MH(OnUpdateAssertions)143 static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
144 {
145 zend_long *p = (zend_long *) ZEND_INI_GET_ADDR();
146
147 zend_long val = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
148
149 if (stage != ZEND_INI_STAGE_STARTUP &&
150 stage != ZEND_INI_STAGE_SHUTDOWN &&
151 *p != val &&
152 (*p < 0 || val < 0)) {
153 zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
154 return FAILURE;
155 }
156
157 *p = val;
158 return SUCCESS;
159 }
160 /* }}} */
161
ZEND_INI_MH(OnSetExceptionStringParamMaxLen)162 static ZEND_INI_MH(OnSetExceptionStringParamMaxLen) /* {{{ */
163 {
164 zend_long i;
165
166 ZEND_ATOL(i, ZSTR_VAL(new_value));
167 if (i >= 0 && i <= 1000000) {
168 EG(exception_string_param_max_len) = i;
169 return SUCCESS;
170 } else {
171 return FAILURE;
172 }
173 }
174 /* }}} */
175
176 #if ZEND_DEBUG
177 # define SIGNAL_CHECK_DEFAULT "1"
178 #else
179 # define SIGNAL_CHECK_DEFAULT "0"
180 #endif
181
182 ZEND_INI_BEGIN()
183 ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting)
184 STD_ZEND_INI_ENTRY("zend.assertions", "1", ZEND_INI_ALL, OnUpdateAssertions, assertions, zend_executor_globals, executor_globals)
185 ZEND_INI_ENTRY3_EX("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb)
186 STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals)
187 ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding)
188 STD_ZEND_INI_BOOLEAN("zend.detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
189 #ifdef ZEND_SIGNALS
190 STD_ZEND_INI_BOOLEAN("zend.signal_check", SIGNAL_CHECK_DEFAULT, ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
191 #endif
192 STD_ZEND_INI_BOOLEAN("zend.exception_ignore_args", "0", ZEND_INI_ALL, OnUpdateBool, exception_ignore_args, zend_executor_globals, executor_globals)
193 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)
ZEND_INI_END()194 ZEND_INI_END()
195
196 ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
197 {
198 smart_string buf = {0};
199
200 /* since there are places where (v)spprintf called without checking for null,
201 a bit of defensive coding here */
202 if (!pbuf) {
203 return 0;
204 }
205
206 zend_printf_to_smart_string(&buf, format, ap);
207
208 if (max_len && buf.len > max_len) {
209 buf.len = max_len;
210 }
211
212 smart_string_0(&buf);
213
214 if (buf.c) {
215 *pbuf = buf.c;
216 return buf.len;
217 } else {
218 *pbuf = estrndup("", 0);
219 return 0;
220 }
221 }
222 /* }}} */
223
zend_spprintf(char ** message,size_t max_len,const char * format,...)224 ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
225 {
226 va_list arg;
227 size_t len;
228
229 va_start(arg, format);
230 len = zend_vspprintf(message, max_len, format, arg);
231 va_end(arg);
232 return len;
233 }
234 /* }}} */
235
zend_spprintf_unchecked(char ** message,size_t max_len,const char * format,...)236 ZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */
237 {
238 va_list arg;
239 size_t len;
240
241 va_start(arg, format);
242 len = zend_vspprintf(message, max_len, format, arg);
243 va_end(arg);
244 return len;
245 }
246 /* }}} */
247
zend_vstrpprintf(size_t max_len,const char * format,va_list ap)248 ZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */
249 {
250 smart_str buf = {0};
251
252 zend_printf_to_smart_str(&buf, format, ap);
253
254 if (!buf.s) {
255 return ZSTR_EMPTY_ALLOC();
256 }
257
258 if (max_len && ZSTR_LEN(buf.s) > max_len) {
259 ZSTR_LEN(buf.s) = max_len;
260 }
261
262 smart_str_0(&buf);
263 return buf.s;
264 }
265 /* }}} */
266
zend_strpprintf(size_t max_len,const char * format,...)267 ZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
268 {
269 va_list arg;
270 zend_string *str;
271
272 va_start(arg, format);
273 str = zend_vstrpprintf(max_len, format, arg);
274 va_end(arg);
275 return str;
276 }
277 /* }}} */
278
zend_strpprintf_unchecked(size_t max_len,const char * format,...)279 ZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */
280 {
281 va_list arg;
282 zend_string *str;
283
284 va_start(arg, format);
285 str = zend_vstrpprintf(max_len, format, arg);
286 va_end(arg);
287 return str;
288 }
289 /* }}} */
290
291 static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent);
292
print_hash(smart_str * buf,HashTable * ht,int indent,zend_bool is_object)293 static void print_hash(smart_str *buf, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
294 {
295 zval *tmp;
296 zend_string *string_key;
297 zend_ulong num_key;
298 int i;
299
300 for (i = 0; i < indent; i++) {
301 smart_str_appendc(buf, ' ');
302 }
303 smart_str_appends(buf, "(\n");
304 indent += PRINT_ZVAL_INDENT;
305 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
306 for (i = 0; i < indent; i++) {
307 smart_str_appendc(buf, ' ');
308 }
309 smart_str_appendc(buf, '[');
310 if (string_key) {
311 if (is_object) {
312 const char *prop_name, *class_name;
313 size_t prop_len;
314 int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
315
316 smart_str_appendl(buf, prop_name, prop_len);
317 if (class_name && mangled == SUCCESS) {
318 if (class_name[0] == '*') {
319 smart_str_appends(buf, ":protected");
320 } else {
321 smart_str_appends(buf, ":");
322 smart_str_appends(buf, class_name);
323 smart_str_appends(buf, ":private");
324 }
325 }
326 } else {
327 smart_str_append(buf, string_key);
328 }
329 } else {
330 smart_str_append_long(buf, num_key);
331 }
332 smart_str_appends(buf, "] => ");
333 zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT);
334 smart_str_appends(buf, "\n");
335 } ZEND_HASH_FOREACH_END();
336 indent -= PRINT_ZVAL_INDENT;
337 for (i = 0; i < indent; i++) {
338 smart_str_appendc(buf, ' ');
339 }
340 smart_str_appends(buf, ")\n");
341 }
342 /* }}} */
343
print_flat_hash(HashTable * ht)344 static void print_flat_hash(HashTable *ht) /* {{{ */
345 {
346 zval *tmp;
347 zend_string *string_key;
348 zend_ulong num_key;
349 int i = 0;
350
351 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
352 if (i++ > 0) {
353 ZEND_PUTS(",");
354 }
355 ZEND_PUTS("[");
356 if (string_key) {
357 ZEND_WRITE(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
358 } else {
359 zend_printf(ZEND_ULONG_FMT, num_key);
360 }
361 ZEND_PUTS("] => ");
362 zend_print_flat_zval_r(tmp);
363 } ZEND_HASH_FOREACH_END();
364 }
365 /* }}} */
366
zend_make_printable_zval(zval * expr,zval * expr_copy)367 ZEND_API bool zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
368 {
369 if (Z_TYPE_P(expr) == IS_STRING) {
370 return 0;
371 } else {
372 ZVAL_STR(expr_copy, zval_get_string_func(expr));
373 return 1;
374 }
375 }
376 /* }}} */
377
zend_print_zval(zval * expr,int indent)378 ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
379 {
380 zend_string *tmp_str;
381 zend_string *str = zval_get_tmp_string(expr, &tmp_str);
382 size_t len = ZSTR_LEN(str);
383
384 if (len != 0) {
385 zend_write(ZSTR_VAL(str), len);
386 }
387
388 zend_tmp_string_release(tmp_str);
389 return len;
390 }
391 /* }}} */
392
zend_print_flat_zval_r(zval * expr)393 ZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
394 {
395 switch (Z_TYPE_P(expr)) {
396 case IS_ARRAY:
397 ZEND_PUTS("Array (");
398 if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
399 if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
400 ZEND_PUTS(" *RECURSION*");
401 return;
402 }
403 GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
404 }
405 print_flat_hash(Z_ARRVAL_P(expr));
406 ZEND_PUTS(")");
407 GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
408 break;
409 case IS_OBJECT:
410 {
411 HashTable *properties;
412 zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
413 zend_printf("%s Object (", ZSTR_VAL(class_name));
414 zend_string_release_ex(class_name, 0);
415
416 if (GC_IS_RECURSIVE(Z_COUNTED_P(expr))) {
417 ZEND_PUTS(" *RECURSION*");
418 return;
419 }
420
421 properties = Z_OBJPROP_P(expr);
422 if (properties) {
423 GC_PROTECT_RECURSION(Z_OBJ_P(expr));
424 print_flat_hash(properties);
425 GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
426 }
427 ZEND_PUTS(")");
428 break;
429 }
430 case IS_REFERENCE:
431 zend_print_flat_zval_r(Z_REFVAL_P(expr));
432 break;
433 default:
434 zend_print_zval(expr, 0);
435 break;
436 }
437 }
438 /* }}} */
439
zend_print_zval_r_to_buf(smart_str * buf,zval * expr,int indent)440 static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent) /* {{{ */
441 {
442 switch (Z_TYPE_P(expr)) {
443 case IS_ARRAY:
444 smart_str_appends(buf, "Array\n");
445 if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
446 if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
447 smart_str_appends(buf, " *RECURSION*");
448 return;
449 }
450 GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
451 }
452 print_hash(buf, Z_ARRVAL_P(expr), indent, 0);
453 GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
454 break;
455 case IS_OBJECT:
456 {
457 HashTable *properties;
458
459 zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
460 smart_str_appends(buf, ZSTR_VAL(class_name));
461 zend_string_release_ex(class_name, 0);
462
463 smart_str_appends(buf, " Object\n");
464 if (GC_IS_RECURSIVE(Z_OBJ_P(expr))) {
465 smart_str_appends(buf, " *RECURSION*");
466 return;
467 }
468
469 if ((properties = zend_get_properties_for(expr, ZEND_PROP_PURPOSE_DEBUG)) == NULL) {
470 break;
471 }
472
473 GC_PROTECT_RECURSION(Z_OBJ_P(expr));
474 print_hash(buf, properties, indent, 1);
475 GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
476
477 zend_release_properties(properties);
478 break;
479 }
480 case IS_LONG:
481 smart_str_append_long(buf, Z_LVAL_P(expr));
482 break;
483 case IS_REFERENCE:
484 zend_print_zval_r_to_buf(buf, Z_REFVAL_P(expr), indent);
485 break;
486 case IS_STRING:
487 smart_str_append(buf, Z_STR_P(expr));
488 break;
489 default:
490 {
491 zend_string *str = zval_get_string_func(expr);
492 smart_str_append(buf, str);
493 zend_string_release_ex(str, 0);
494 }
495 break;
496 }
497 }
498 /* }}} */
499
zend_print_zval_r_to_str(zval * expr,int indent)500 ZEND_API zend_string *zend_print_zval_r_to_str(zval *expr, int indent) /* {{{ */
501 {
502 smart_str buf = {0};
503 zend_print_zval_r_to_buf(&buf, expr, indent);
504 smart_str_0(&buf);
505 return buf.s;
506 }
507 /* }}} */
508
zend_print_zval_r(zval * expr,int indent)509 ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */
510 {
511 zend_string *str = zend_print_zval_r_to_str(expr, indent);
512 zend_write(ZSTR_VAL(str), ZSTR_LEN(str));
513 zend_string_release_ex(str, 0);
514 }
515 /* }}} */
516
zend_fopen_wrapper(const char * filename,zend_string ** opened_path)517 static FILE *zend_fopen_wrapper(const char *filename, zend_string **opened_path) /* {{{ */
518 {
519 if (opened_path) {
520 *opened_path = zend_string_init(filename, strlen(filename), 0);
521 }
522 return fopen(filename, "rb");
523 }
524 /* }}} */
525
526 #ifdef ZTS
527 static zend_bool short_tags_default = 1;
528 static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
529 #else
530 # define short_tags_default 1
531 # define compiler_options_default ZEND_COMPILE_DEFAULT
532 #endif
533
zend_set_default_compile_time_values(void)534 static void zend_set_default_compile_time_values(void) /* {{{ */
535 {
536 /* default compile-time values */
537 CG(short_tags) = short_tags_default;
538 CG(compiler_options) = compiler_options_default;
539
540 CG(rtd_key_counter) = 0;
541 }
542 /* }}} */
543
544 #ifdef ZEND_WIN32
zend_get_windows_version_info(OSVERSIONINFOEX * osvi)545 static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */
546 {
547 ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
548 osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
549 if(!GetVersionEx((OSVERSIONINFO *) osvi)) {
550 ZEND_UNREACHABLE(); /* Should not happen as sizeof is used. */
551 }
552 }
553 /* }}} */
554 #endif
555
zend_init_exception_op(void)556 static void zend_init_exception_op(void) /* {{{ */
557 {
558 memset(EG(exception_op), 0, sizeof(EG(exception_op)));
559 EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
560 ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
561 EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
562 ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
563 EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
564 ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
565 }
566 /* }}} */
567
zend_init_call_trampoline_op(void)568 static void zend_init_call_trampoline_op(void) /* {{{ */
569 {
570 memset(&EG(call_trampoline_op), 0, sizeof(EG(call_trampoline_op)));
571 EG(call_trampoline_op).opcode = ZEND_CALL_TRAMPOLINE;
572 ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
573 }
574 /* }}} */
575
auto_global_dtor(zval * zv)576 static void auto_global_dtor(zval *zv) /* {{{ */
577 {
578 free(Z_PTR_P(zv));
579 }
580 /* }}} */
581
582 #ifdef ZTS
function_copy_ctor(zval * zv)583 static void function_copy_ctor(zval *zv) /* {{{ */
584 {
585 zend_function *old_func = Z_FUNC_P(zv);
586 zend_function *func;
587
588 if (old_func->type == ZEND_USER_FUNCTION) {
589 ZEND_ASSERT(old_func->op_array.fn_flags & ZEND_ACC_IMMUTABLE);
590 return;
591 }
592 func = pemalloc(sizeof(zend_internal_function), 1);
593 Z_FUNC_P(zv) = func;
594 memcpy(func, old_func, sizeof(zend_internal_function));
595 function_add_ref(func);
596 if ((old_func->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))
597 && old_func->common.arg_info) {
598 uint32_t i;
599 uint32_t num_args = old_func->common.num_args + 1;
600 zend_arg_info *arg_info = old_func->common.arg_info - 1;
601 zend_arg_info *new_arg_info;
602
603 if (old_func->common.fn_flags & ZEND_ACC_VARIADIC) {
604 num_args++;
605 }
606 new_arg_info = pemalloc(sizeof(zend_arg_info) * num_args, 1);
607 memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
608 for (i = 0 ; i < num_args; i++) {
609 if (ZEND_TYPE_HAS_LIST(arg_info[i].type)) {
610 zend_type_list *old_list = ZEND_TYPE_LIST(arg_info[i].type);
611 zend_type_list *new_list = pemalloc(ZEND_TYPE_LIST_SIZE(old_list->num_types), 1);
612 memcpy(new_list, old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types));
613 ZEND_TYPE_SET_PTR(new_arg_info[i].type, new_list);
614
615 zend_type *list_type;
616 ZEND_TYPE_LIST_FOREACH(new_list, list_type) {
617 zend_string *name = zend_string_dup(ZEND_TYPE_NAME(*list_type), 1);
618 ZEND_TYPE_SET_PTR(*list_type, name);
619 } ZEND_TYPE_LIST_FOREACH_END();
620 } else if (ZEND_TYPE_HAS_NAME(arg_info[i].type)) {
621 zend_string *name = zend_string_dup(ZEND_TYPE_NAME(arg_info[i].type), 1);
622 ZEND_TYPE_SET_PTR(new_arg_info[i].type, name);
623 }
624 }
625 func->common.arg_info = new_arg_info + 1;
626 }
627 if (old_func->common.attributes) {
628 zend_attribute *old_attr;
629
630 func->common.attributes = NULL;
631
632 ZEND_HASH_FOREACH_PTR(old_func->common.attributes, old_attr) {
633 uint32_t i;
634 zend_attribute *attr;
635
636 attr = zend_add_attribute(&func->common.attributes, old_attr->name, old_attr->argc, old_attr->flags, old_attr->offset, old_attr->lineno);
637
638 for (i = 0 ; i < old_attr->argc; i++) {
639 ZVAL_DUP(&attr->args[i].value, &old_attr->args[i].value);
640 }
641 } ZEND_HASH_FOREACH_END();
642 }
643 }
644 /* }}} */
645
auto_global_copy_ctor(zval * zv)646 static void auto_global_copy_ctor(zval *zv) /* {{{ */
647 {
648 zend_auto_global *old_ag = (zend_auto_global *) Z_PTR_P(zv);
649 zend_auto_global *new_ag = pemalloc(sizeof(zend_auto_global), 1);
650
651 new_ag->name = old_ag->name;
652 new_ag->auto_global_callback = old_ag->auto_global_callback;
653 new_ag->jit = old_ag->jit;
654
655 Z_PTR_P(zv) = new_ag;
656 }
657 /* }}} */
658
compiler_globals_ctor(zend_compiler_globals * compiler_globals)659 static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */
660 {
661 compiler_globals->compiled_filename = NULL;
662
663 compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
664 zend_hash_init(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
665 zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
666
667 compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
668 zend_hash_init(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1);
669 zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
670
671 zend_set_default_compile_time_values();
672
673 compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
674 zend_hash_init(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1);
675 zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);
676
677 compiler_globals->script_encoding_list = NULL;
678
679 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
680 /* Map region is going to be created and resized at run-time. */
681 ZEND_MAP_PTR_SET_REAL_BASE(compiler_globals->map_ptr_base, NULL);
682 compiler_globals->map_ptr_size = 0;
683 compiler_globals->map_ptr_last = global_map_ptr_last;
684 if (compiler_globals->map_ptr_last) {
685 /* Allocate map_ptr table */
686 void *base;
687 compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096);
688 base = pemalloc(compiler_globals->map_ptr_size * sizeof(void*), 1);
689 ZEND_MAP_PTR_SET_REAL_BASE(compiler_globals->map_ptr_base, base);
690 memset(base, 0, compiler_globals->map_ptr_last * sizeof(void*));
691 }
692 #else
693 # error "Unknown ZEND_MAP_PTR_KIND"
694 #endif
695 }
696 /* }}} */
697
compiler_globals_dtor(zend_compiler_globals * compiler_globals)698 static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
699 {
700 if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
701 zend_hash_destroy(compiler_globals->function_table);
702 free(compiler_globals->function_table);
703 }
704 if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
705 zend_hash_destroy(compiler_globals->class_table);
706 free(compiler_globals->class_table);
707 }
708 if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
709 zend_hash_destroy(compiler_globals->auto_globals);
710 free(compiler_globals->auto_globals);
711 }
712 if (compiler_globals->script_encoding_list) {
713 pefree((char*)compiler_globals->script_encoding_list, 1);
714 }
715 if (ZEND_MAP_PTR_REAL_BASE(compiler_globals->map_ptr_base)) {
716 free(ZEND_MAP_PTR_REAL_BASE(compiler_globals->map_ptr_base));
717 ZEND_MAP_PTR_SET_REAL_BASE(compiler_globals->map_ptr_base, NULL);
718 compiler_globals->map_ptr_size = 0;
719 }
720 }
721 /* }}} */
722
executor_globals_ctor(zend_executor_globals * executor_globals)723 static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
724 {
725 zend_startup_constants();
726 zend_copy_constants(executor_globals->zend_constants, GLOBAL_CONSTANTS_TABLE);
727 zend_init_rsrc_plist();
728 zend_init_exception_op();
729 zend_init_call_trampoline_op();
730 memset(&executor_globals->trampoline, 0, sizeof(zend_op_array));
731 executor_globals->lambda_count = 0;
732 ZVAL_UNDEF(&executor_globals->user_error_handler);
733 ZVAL_UNDEF(&executor_globals->user_exception_handler);
734 executor_globals->in_autoload = NULL;
735 executor_globals->current_execute_data = NULL;
736 executor_globals->current_module = NULL;
737 executor_globals->exit_status = 0;
738 #if XPFPA_HAVE_CW
739 executor_globals->saved_fpu_cw = 0;
740 #endif
741 executor_globals->saved_fpu_cw_ptr = NULL;
742 executor_globals->active = 0;
743 executor_globals->bailout = NULL;
744 executor_globals->error_handling = EH_NORMAL;
745 executor_globals->exception_class = NULL;
746 executor_globals->exception = NULL;
747 executor_globals->objects_store.object_buckets = NULL;
748 #ifdef ZEND_WIN32
749 zend_get_windows_version_info(&executor_globals->windows_version_info);
750 #endif
751 executor_globals->flags = EG_FLAGS_INITIAL;
752 }
753 /* }}} */
754
executor_globals_dtor(zend_executor_globals * executor_globals)755 static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
756 {
757 zend_ini_dtor(executor_globals->ini_directives);
758
759 if (&executor_globals->persistent_list != global_persistent_list) {
760 zend_destroy_rsrc_list(&executor_globals->persistent_list);
761 }
762 if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
763 zend_hash_destroy(executor_globals->zend_constants);
764 free(executor_globals->zend_constants);
765 }
766 }
767 /* }}} */
768
zend_new_thread_end_handler(THREAD_T thread_id)769 static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
770 {
771 zend_copy_ini_directives();
772 zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
773 }
774 /* }}} */
775 #endif
776
777 #if defined(__FreeBSD__) || defined(__DragonFly__)
778 /* FreeBSD and DragonFly floating point precision fix */
779 #include <floatingpoint.h>
780 #endif
781
ini_scanner_globals_ctor(zend_ini_scanner_globals * scanner_globals_p)782 static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
783 {
784 memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
785 }
786 /* }}} */
787
php_scanner_globals_ctor(zend_php_scanner_globals * scanner_globals_p)788 static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
789 {
790 memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
791 }
792 /* }}} */
793
module_destructor_zval(zval * zv)794 static void module_destructor_zval(zval *zv) /* {{{ */
795 {
796 zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
797
798 module_destructor(module);
799 free(module);
800 }
801 /* }}} */
802
php_auto_globals_create_globals(zend_string * name)803 static zend_bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
804 {
805 zval globals;
806
807 /* IS_ARRAY, but with ref-counter 1 and not IS_TYPE_REFCOUNTED */
808 ZVAL_ARR(&globals, &EG(symbol_table));
809 Z_TYPE_FLAGS_P(&globals) = 0;
810 ZVAL_NEW_REF(&globals, &globals);
811 zend_hash_update(&EG(symbol_table), name, &globals);
812 return 0;
813 }
814 /* }}} */
815
zend_startup(zend_utility_functions * utility_functions)816 void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
817 {
818 #ifdef ZTS
819 zend_compiler_globals *compiler_globals;
820 zend_executor_globals *executor_globals;
821 extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
822 extern ZEND_API ts_rsrc_id language_scanner_globals_id;
823 #else
824 extern zend_ini_scanner_globals ini_scanner_globals;
825 extern zend_php_scanner_globals language_scanner_globals;
826 #endif
827
828 zend_cpu_startup();
829
830 #ifdef ZEND_WIN32
831 php_win32_cp_set_by_id(65001);
832 #endif
833
834 start_memory_manager();
835
836 virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
837
838 #if defined(__FreeBSD__) || defined(__DragonFly__)
839 /* FreeBSD and DragonFly floating point precision fix */
840 fpsetmask(0);
841 #endif
842
843 zend_startup_strtod();
844 zend_startup_extensions_mechanism();
845
846 /* Set up utility functions and values */
847 zend_error_cb = utility_functions->error_function;
848 zend_printf = utility_functions->printf_function;
849 zend_write = utility_functions->write_function;
850 zend_fopen = utility_functions->fopen_function;
851 if (!zend_fopen) {
852 zend_fopen = zend_fopen_wrapper;
853 }
854 zend_stream_open_function = utility_functions->stream_open_function;
855 zend_message_dispatcher_p = utility_functions->message_handler;
856 zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
857 zend_ticks_function = utility_functions->ticks_function;
858 zend_on_timeout = utility_functions->on_timeout;
859 zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function;
860 zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function;
861 zend_getenv = utility_functions->getenv_function;
862 zend_resolve_path = utility_functions->resolve_path_function;
863
864 zend_interrupt_function = NULL;
865
866 #ifdef HAVE_DTRACE
867 /* build with dtrace support */
868 {
869 char *tmp = getenv("USE_ZEND_DTRACE");
870
871 if (tmp && zend_atoi(tmp, 0)) {
872 zend_dtrace_enabled = 1;
873 zend_compile_file = dtrace_compile_file;
874 zend_execute_ex = dtrace_execute_ex;
875 zend_execute_internal = dtrace_execute_internal;
876
877 zend_observer_error_register(dtrace_error_notify_cb);
878 } else {
879 zend_compile_file = compile_file;
880 zend_execute_ex = execute_ex;
881 zend_execute_internal = NULL;
882 }
883 }
884 #else
885 zend_compile_file = compile_file;
886 zend_execute_ex = execute_ex;
887 zend_execute_internal = NULL;
888 #endif /* HAVE_DTRACE */
889 zend_compile_string = compile_string;
890 zend_throw_exception_hook = NULL;
891
892 /* Set up the default garbage collection implementation. */
893 gc_collect_cycles = zend_gc_collect_cycles;
894
895 zend_vm_init();
896
897 /* set up version */
898 zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
899 zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
900
901 GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
902 GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
903 GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
904 GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
905
906 zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
907 zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1);
908 zend_hash_init(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1);
909 zend_hash_init(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1);
910
911 zend_hash_init(&module_registry, 32, NULL, module_destructor_zval, 1);
912 zend_init_rsrc_list_dtors();
913
914 #ifdef ZTS
915 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);
916 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);
917 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);
918 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);
919 compiler_globals = ts_resource(compiler_globals_id);
920 executor_globals = ts_resource(executor_globals_id);
921
922 compiler_globals_dtor(compiler_globals);
923 compiler_globals->in_compilation = 0;
924 compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
925 compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
926
927 *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
928 *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
929 compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
930
931 zend_hash_destroy(executor_globals->zend_constants);
932 *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
933 #else
934 ini_scanner_globals_ctor(&ini_scanner_globals);
935 php_scanner_globals_ctor(&language_scanner_globals);
936 zend_set_default_compile_time_values();
937 #ifdef ZEND_WIN32
938 zend_get_windows_version_info(&EG(windows_version_info));
939 #endif
940 # if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
941 /* Create a map region, used for indirect pointers from shared to
942 * process memory. It's allocatred once and never resized.
943 * All processes must map it into the same address space.
944 */
945 CG(map_ptr_size) = 1024 * 1024; // TODO: initial size ???
946 CG(map_ptr_last) = 0;
947 ZEND_MAP_PTR_SET_REAL_BASE(CG(map_ptr_base), pemalloc(CG(map_ptr_size) * sizeof(void*), 1));
948 # elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
949 /* Map region is going to be created and resized at run-time. */
950 ZEND_MAP_PTR_SET_REAL_BASE(CG(map_ptr_base), NULL);
951 CG(map_ptr_size) = 0;
952 CG(map_ptr_last) = 0;
953 # else
954 # error "Unknown ZEND_MAP_PTR_KIND"
955 # endif
956 #endif
957 EG(error_reporting) = E_ALL & ~E_NOTICE;
958
959 zend_interned_strings_init();
960 zend_startup_builtin_functions();
961 zend_register_standard_constants();
962 zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
963
964 #ifndef ZTS
965 zend_init_rsrc_plist();
966 zend_init_exception_op();
967 zend_init_call_trampoline_op();
968 #endif
969
970 zend_ini_startup();
971
972 #ifdef ZEND_WIN32
973 /* Uses INI settings, so needs to be run after it. */
974 php_win32_cp_setup();
975 #endif
976
977 #ifdef ZTS
978 tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
979 tsrm_set_shutdown_handler(zend_interned_strings_dtor);
980 #endif
981 }
982 /* }}} */
983
zend_register_standard_ini_entries(void)984 void zend_register_standard_ini_entries(void) /* {{{ */
985 {
986 int module_number = 0;
987
988 REGISTER_INI_ENTRIES();
989 }
990 /* }}} */
991
resolve_type_name(zend_string * type_name)992 static zend_class_entry *resolve_type_name(zend_string *type_name) {
993 zend_string *lc_type_name = zend_string_tolower(type_name);
994 zend_class_entry *ce = zend_hash_find_ptr(CG(class_table), lc_type_name);
995
996 ZEND_ASSERT(ce && ce->type == ZEND_INTERNAL_CLASS);
997 zend_string_release(lc_type_name);
998 return ce;
999 }
1000
zend_resolve_property_types(void)1001 static void zend_resolve_property_types(void) /* {{{ */
1002 {
1003 zend_class_entry *ce;
1004 zend_property_info *prop_info;
1005
1006 ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1007 if (ce->type != ZEND_INTERNAL_CLASS) {
1008 continue;
1009 }
1010
1011 if (UNEXPECTED(ZEND_CLASS_HAS_TYPE_HINTS(ce))) {
1012 ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
1013 zend_type *single_type;
1014 ZEND_TYPE_FOREACH(prop_info->type, single_type) {
1015 if (ZEND_TYPE_HAS_NAME(*single_type)) {
1016 zend_string *type_name = ZEND_TYPE_NAME(*single_type);
1017 ZEND_TYPE_SET_CE(*single_type, resolve_type_name(type_name));
1018 zend_string_release(type_name);
1019 }
1020 } ZEND_TYPE_FOREACH_END();
1021 } ZEND_HASH_FOREACH_END();
1022 }
1023 ce->ce_flags |= ZEND_ACC_PROPERTY_TYPES_RESOLVED;
1024 } ZEND_HASH_FOREACH_END();
1025 }
1026 /* }}} */
1027
1028 /* Unlink the global (r/o) copies of the class, function and constant tables,
1029 * and use a fresh r/w copy for the startup thread
1030 */
zend_post_startup(void)1031 zend_result zend_post_startup(void) /* {{{ */
1032 {
1033 #ifdef ZTS
1034 zend_encoding **script_encoding_list;
1035
1036 zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
1037 zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
1038 #endif
1039
1040 zend_resolve_property_types();
1041
1042 if (zend_post_startup_cb) {
1043 zend_result (*cb)(void) = zend_post_startup_cb;
1044
1045 zend_post_startup_cb = NULL;
1046 if (cb() != SUCCESS) {
1047 return FAILURE;
1048 }
1049 }
1050
1051 #ifdef ZTS
1052 *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
1053 *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
1054 *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
1055 global_map_ptr_last = compiler_globals->map_ptr_last;
1056
1057 short_tags_default = CG(short_tags);
1058 compiler_options_default = CG(compiler_options);
1059
1060 zend_destroy_rsrc_list(&EG(persistent_list));
1061 free(compiler_globals->function_table);
1062 compiler_globals->function_table = NULL;
1063 free(compiler_globals->class_table);
1064 compiler_globals->class_table = NULL;
1065 if (ZEND_MAP_PTR_REAL_BASE(compiler_globals->map_ptr_base)) {
1066 free(ZEND_MAP_PTR_REAL_BASE(compiler_globals->map_ptr_base));
1067 }
1068 ZEND_MAP_PTR_SET_REAL_BASE(compiler_globals->map_ptr_base, NULL);
1069 if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
1070 compiler_globals_ctor(compiler_globals);
1071 compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
1072 } else {
1073 compiler_globals_ctor(compiler_globals);
1074 }
1075 free(EG(zend_constants));
1076 EG(zend_constants) = NULL;
1077
1078 executor_globals_ctor(executor_globals);
1079 global_persistent_list = &EG(persistent_list);
1080 zend_copy_ini_directives();
1081 #else
1082 global_map_ptr_last = CG(map_ptr_last);
1083 #endif
1084
1085 return SUCCESS;
1086 }
1087 /* }}} */
1088
zend_shutdown(void)1089 void zend_shutdown(void) /* {{{ */
1090 {
1091 zend_vm_dtor();
1092
1093 zend_destroy_rsrc_list(&EG(persistent_list));
1094 zend_destroy_modules();
1095
1096 virtual_cwd_deactivate();
1097 virtual_cwd_shutdown();
1098
1099 zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
1100 zend_hash_destroy(GLOBAL_CLASS_TABLE);
1101
1102 zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
1103 free(GLOBAL_AUTO_GLOBALS_TABLE);
1104
1105 zend_shutdown_extensions();
1106 free(zend_version_info);
1107
1108 free(GLOBAL_FUNCTION_TABLE);
1109 free(GLOBAL_CLASS_TABLE);
1110
1111 zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
1112 free(GLOBAL_CONSTANTS_TABLE);
1113 zend_shutdown_strtod();
1114 zend_attributes_shutdown();
1115
1116 #ifdef ZTS
1117 GLOBAL_FUNCTION_TABLE = NULL;
1118 GLOBAL_CLASS_TABLE = NULL;
1119 GLOBAL_AUTO_GLOBALS_TABLE = NULL;
1120 GLOBAL_CONSTANTS_TABLE = NULL;
1121 ts_free_id(executor_globals_id);
1122 ts_free_id(compiler_globals_id);
1123 #else
1124 if (ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base))) {
1125 free(ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)));
1126 ZEND_MAP_PTR_SET_REAL_BASE(CG(map_ptr_base), NULL);
1127 CG(map_ptr_size) = 0;
1128 }
1129 if (CG(script_encoding_list)) {
1130 free(ZEND_VOIDP(CG(script_encoding_list)));
1131 CG(script_encoding_list) = NULL;
1132 CG(script_encoding_list_size) = 0;
1133 }
1134 #endif
1135 zend_destroy_rsrc_list_dtors();
1136 }
1137 /* }}} */
1138
zend_set_utility_values(zend_utility_values * utility_values)1139 void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
1140 {
1141 zend_uv = *utility_values;
1142 }
1143 /* }}} */
1144
1145 /* this should be compatible with the standard zenderror */
zenderror(const char * error)1146 ZEND_COLD void zenderror(const char *error) /* {{{ */
1147 {
1148 CG(parse_error) = 0;
1149
1150 if (EG(exception)) {
1151 /* An exception was thrown in the lexer, don't throw another in the parser. */
1152 return;
1153 }
1154
1155 zend_throw_exception(zend_ce_parse_error, error, 0);
1156 }
1157 /* }}} */
1158
BEGIN_EXTERN_C()1159 BEGIN_EXTERN_C()
1160 ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
1161 {
1162
1163 if (!EG(bailout)) {
1164 zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
1165 exit(-1);
1166 }
1167 gc_protect(1);
1168 CG(unclean_shutdown) = 1;
1169 CG(active_class_entry) = NULL;
1170 CG(in_compilation) = 0;
1171 EG(current_execute_data) = NULL;
1172 LONGJMP(*EG(bailout), FAILURE);
1173 }
1174 /* }}} */
END_EXTERN_C()1175 END_EXTERN_C()
1176
1177 ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
1178 {
1179 char *new_info;
1180 uint32_t new_info_length;
1181
1182 new_info_length = (uint32_t)(sizeof(" with v, , by \n")
1183 + strlen(extension->name)
1184 + strlen(extension->version)
1185 + strlen(extension->copyright)
1186 + strlen(extension->author));
1187
1188 new_info = (char *) malloc(new_info_length + 1);
1189
1190 snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
1191
1192 zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
1193 strncat(zend_version_info, new_info, new_info_length);
1194 zend_version_info_length += new_info_length;
1195 free(new_info);
1196 }
1197 /* }}} */
1198
get_zend_version(void)1199 ZEND_API const char *get_zend_version(void) /* {{{ */
1200 {
1201 return zend_version_info;
1202 }
1203 /* }}} */
1204
zend_activate(void)1205 ZEND_API void zend_activate(void) /* {{{ */
1206 {
1207 #ifdef ZTS
1208 virtual_cwd_activate();
1209 #endif
1210 gc_reset();
1211 init_compiler();
1212 init_executor();
1213 startup_scanner();
1214 if (CG(map_ptr_last)) {
1215 memset(ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)), 0, CG(map_ptr_last) * sizeof(void*));
1216 }
1217 zend_observer_activate();
1218 }
1219 /* }}} */
1220
zend_call_destructors(void)1221 void zend_call_destructors(void) /* {{{ */
1222 {
1223 zend_try {
1224 shutdown_destructors();
1225 } zend_end_try();
1226 }
1227 /* }}} */
1228
zend_deactivate(void)1229 ZEND_API void zend_deactivate(void) /* {{{ */
1230 {
1231 /* we're no longer executing anything */
1232 EG(current_execute_data) = NULL;
1233
1234 zend_try {
1235 shutdown_scanner();
1236 } zend_end_try();
1237
1238 /* shutdown_executor() takes care of its own bailout handling */
1239 shutdown_executor();
1240
1241 zend_try {
1242 zend_ini_deactivate();
1243 } zend_end_try();
1244
1245 zend_try {
1246 shutdown_compiler();
1247 } zend_end_try();
1248
1249 zend_destroy_rsrc_list(&EG(regular_list));
1250
1251 #if GC_BENCH
1252 fprintf(stderr, "GC Statistics\n");
1253 fprintf(stderr, "-------------\n");
1254 fprintf(stderr, "Runs: %d\n", GC_G(gc_runs));
1255 fprintf(stderr, "Collected: %d\n", GC_G(collected));
1256 fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
1257 fprintf(stderr, "Root buffer peak: %d\n\n", GC_G(root_buf_peak));
1258 fprintf(stderr, " Possible Remove from Marked\n");
1259 fprintf(stderr, " Root Buffered buffer grey\n");
1260 fprintf(stderr, " -------- -------- ----------- ------\n");
1261 fprintf(stderr, "ZVAL %8d %8d %9d %8d\n", GC_G(zval_possible_root), GC_G(zval_buffered), GC_G(zval_remove_from_buffer), GC_G(zval_marked_grey));
1262 #endif
1263 }
1264 /* }}} */
1265
BEGIN_EXTERN_C()1266 BEGIN_EXTERN_C()
1267 ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
1268 {
1269 if (zend_message_dispatcher_p) {
1270 zend_message_dispatcher_p(message, data);
1271 }
1272 }
1273 /* }}} */
END_EXTERN_C()1274 END_EXTERN_C()
1275
1276 ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1277 {
1278 if (zend_get_configuration_directive_p) {
1279 return zend_get_configuration_directive_p(name);
1280 } else {
1281 return NULL;
1282 }
1283 }
1284 /* }}} */
1285
1286 #define SAVE_STACK(stack) do { \
1287 if (CG(stack).top) { \
1288 memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1289 CG(stack).top = CG(stack).max = 0; \
1290 CG(stack).elements = NULL; \
1291 } else { \
1292 stack.top = 0; \
1293 } \
1294 } while (0)
1295
1296 #define RESTORE_STACK(stack) do { \
1297 if (stack.top) { \
1298 zend_stack_destroy(&CG(stack)); \
1299 memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1300 } \
1301 } while (0)
1302
zend_error_impl(int orig_type,const char * error_filename,uint32_t error_lineno,zend_string * message)1303 static ZEND_COLD void zend_error_impl(
1304 int orig_type, const char *error_filename, uint32_t error_lineno, zend_string *message)
1305 {
1306 zval params[4];
1307 zval retval;
1308 zval orig_user_error_handler;
1309 zend_bool in_compilation;
1310 zend_class_entry *saved_class_entry;
1311 zend_stack loop_var_stack;
1312 zend_stack delayed_oplines_stack;
1313 int type = orig_type & E_ALL;
1314
1315 /* Report about uncaught exception in case of fatal errors */
1316 if (EG(exception)) {
1317 zend_execute_data *ex;
1318 const zend_op *opline;
1319
1320 if (type & E_FATAL_ERRORS) {
1321 ex = EG(current_execute_data);
1322 opline = NULL;
1323 while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1324 ex = ex->prev_execute_data;
1325 }
1326 if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1327 EG(opline_before_exception)) {
1328 opline = EG(opline_before_exception);
1329 }
1330 zend_exception_error(EG(exception), E_WARNING);
1331 EG(exception) = NULL;
1332 if (opline) {
1333 ex->opline = opline;
1334 }
1335 }
1336 }
1337
1338 zend_observer_error_notify(type, error_filename, error_lineno, message);
1339
1340 /* if we don't have a user defined error handler */
1341 if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1342 || !(EG(user_error_handler_error_reporting) & type)
1343 || EG(error_handling) != EH_NORMAL) {
1344 zend_error_cb(orig_type, error_filename, error_lineno, message);
1345 } else switch (type) {
1346 case E_ERROR:
1347 case E_PARSE:
1348 case E_CORE_ERROR:
1349 case E_CORE_WARNING:
1350 case E_COMPILE_ERROR:
1351 case E_COMPILE_WARNING:
1352 /* The error may not be safe to handle in user-space */
1353 zend_error_cb(orig_type, error_filename, error_lineno, message);
1354 break;
1355 default:
1356 /* Handle the error in user space */
1357 ZVAL_STR_COPY(¶ms[1], message);
1358 ZVAL_LONG(¶ms[0], type);
1359
1360 if (error_filename) {
1361 ZVAL_STRING(¶ms[2], error_filename);
1362 } else {
1363 ZVAL_NULL(¶ms[2]);
1364 }
1365
1366 ZVAL_LONG(¶ms[3], error_lineno);
1367
1368 ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1369 ZVAL_UNDEF(&EG(user_error_handler));
1370
1371 /* User error handler may include() additinal PHP files.
1372 * If an error was generated during comilation PHP will compile
1373 * such scripts recursively, but some CG() variables may be
1374 * inconsistent. */
1375
1376 in_compilation = CG(in_compilation);
1377 if (in_compilation) {
1378 saved_class_entry = CG(active_class_entry);
1379 CG(active_class_entry) = NULL;
1380 SAVE_STACK(loop_var_stack);
1381 SAVE_STACK(delayed_oplines_stack);
1382 CG(in_compilation) = 0;
1383 }
1384
1385 if (call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 4, params) == SUCCESS) {
1386 if (Z_TYPE(retval) != IS_UNDEF) {
1387 if (Z_TYPE(retval) == IS_FALSE) {
1388 zend_error_cb(orig_type, error_filename, error_lineno, message);
1389 }
1390 zval_ptr_dtor(&retval);
1391 }
1392 } else if (!EG(exception)) {
1393 /* The user error handler failed, use built-in error handler */
1394 zend_error_cb(orig_type, error_filename, error_lineno, message);
1395 }
1396
1397 if (in_compilation) {
1398 CG(active_class_entry) = saved_class_entry;
1399 RESTORE_STACK(loop_var_stack);
1400 RESTORE_STACK(delayed_oplines_stack);
1401 CG(in_compilation) = 1;
1402 }
1403
1404 zval_ptr_dtor(¶ms[2]);
1405 zval_ptr_dtor(¶ms[1]);
1406
1407 if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1408 ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1409 } else {
1410 zval_ptr_dtor(&orig_user_error_handler);
1411 }
1412 break;
1413 }
1414
1415 if (type == E_PARSE) {
1416 /* eval() errors do not affect exit_status */
1417 if (!(EG(current_execute_data) &&
1418 EG(current_execute_data)->func &&
1419 ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1420 EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1421 EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1422 EG(exit_status) = 255;
1423 }
1424 }
1425 }
1426 /* }}} */
1427
zend_error_va_list(int orig_type,const char * error_filename,uint32_t error_lineno,const char * format,va_list args)1428 static ZEND_COLD void zend_error_va_list(
1429 int orig_type, const char *error_filename, uint32_t error_lineno,
1430 const char *format, va_list args)
1431 {
1432 zend_string *message = zend_vstrpprintf(0, format, args);
1433 zend_error_impl(orig_type, error_filename, error_lineno, message);
1434 zend_string_release(message);
1435 }
1436
get_filename_lineno(int type,const char ** filename,uint32_t * lineno)1437 static ZEND_COLD void get_filename_lineno(int type, const char **filename, uint32_t *lineno) {
1438 /* Obtain relevant filename and lineno */
1439 switch (type) {
1440 case E_CORE_ERROR:
1441 case E_CORE_WARNING:
1442 *filename = NULL;
1443 *lineno = 0;
1444 break;
1445 case E_PARSE:
1446 case E_COMPILE_ERROR:
1447 case E_COMPILE_WARNING:
1448 case E_ERROR:
1449 case E_NOTICE:
1450 case E_STRICT:
1451 case E_DEPRECATED:
1452 case E_WARNING:
1453 case E_USER_ERROR:
1454 case E_USER_WARNING:
1455 case E_USER_NOTICE:
1456 case E_USER_DEPRECATED:
1457 case E_RECOVERABLE_ERROR:
1458 if (zend_is_compiling()) {
1459 *filename = ZSTR_VAL(zend_get_compiled_filename());
1460 *lineno = zend_get_compiled_lineno();
1461 } else if (zend_is_executing()) {
1462 *filename = zend_get_executed_filename();
1463 if ((*filename)[0] == '[') { /* [no active file] */
1464 *filename = NULL;
1465 *lineno = 0;
1466 } else {
1467 *lineno = zend_get_executed_lineno();
1468 }
1469 } else {
1470 *filename = NULL;
1471 *lineno = 0;
1472 }
1473 break;
1474 default:
1475 *filename = NULL;
1476 *lineno = 0;
1477 break;
1478 }
1479 if (!*filename) {
1480 *filename = "Unknown";
1481 }
1482 }
1483
zend_error_at(int type,const char * filename,uint32_t lineno,const char * format,...)1484 ZEND_API ZEND_COLD void zend_error_at(
1485 int type, const char *filename, uint32_t lineno, const char *format, ...) {
1486 va_list args;
1487
1488 if (!filename) {
1489 uint32_t dummy_lineno;
1490 get_filename_lineno(type, &filename, &dummy_lineno);
1491 }
1492
1493 va_start(args, format);
1494 zend_error_va_list(type, filename, lineno, format, args);
1495 va_end(args);
1496 }
1497
zend_error(int type,const char * format,...)1498 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) {
1499 const char *filename;
1500 uint32_t lineno;
1501 va_list args;
1502
1503 get_filename_lineno(type, &filename, &lineno);
1504 va_start(args, format);
1505 zend_error_va_list(type, filename, lineno, format, args);
1506 va_end(args);
1507 }
1508
zend_error_at_noreturn(int type,const char * filename,uint32_t lineno,const char * format,...)1509 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
1510 int type, const char *filename, uint32_t lineno, const char *format, ...)
1511 {
1512 va_list args;
1513
1514 if (!filename) {
1515 uint32_t dummy_lineno;
1516 get_filename_lineno(type, &filename, &dummy_lineno);
1517 }
1518
1519 va_start(args, format);
1520 zend_error_va_list(type, filename, lineno, format, args);
1521 va_end(args);
1522 /* Should never reach this. */
1523 abort();
1524 }
1525
zend_error_noreturn(int type,const char * format,...)1526 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1527 {
1528 const char *filename;
1529 uint32_t lineno;
1530 va_list args;
1531
1532 get_filename_lineno(type, &filename, &lineno);
1533 va_start(args, format);
1534 zend_error_va_list(type, filename, lineno, format, args);
1535 va_end(args);
1536 /* Should never reach this. */
1537 abort();
1538 }
1539
zend_error_zstr(int type,zend_string * message)1540 ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
1541 const char *filename;
1542 uint32_t lineno;
1543 get_filename_lineno(type, &filename, &lineno);
1544 zend_error_impl(type, filename, lineno, message);
1545 }
1546
zend_throw_error(zend_class_entry * exception_ce,const char * format,...)1547 ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
1548 {
1549 va_list va;
1550 char *message = NULL;
1551
1552 if (!exception_ce) {
1553 exception_ce = zend_ce_error;
1554 }
1555
1556 /* Marker used to disable exception generation during preloading. */
1557 if (EG(exception) == (void*)(uintptr_t)-1) {
1558 return;
1559 }
1560
1561 va_start(va, format);
1562 zend_vspprintf(&message, 0, format, va);
1563
1564 //TODO: we can't convert compile-time errors to exceptions yet???
1565 if (EG(current_execute_data) && !CG(in_compilation)) {
1566 zend_throw_exception(exception_ce, message, 0);
1567 } else {
1568 zend_error(E_ERROR, "%s", message);
1569 }
1570
1571 efree(message);
1572 va_end(va);
1573 }
1574 /* }}} */
1575
zend_type_error(const char * format,...)1576 ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
1577 {
1578 va_list va;
1579 char *message = NULL;
1580
1581 va_start(va, format);
1582 zend_vspprintf(&message, 0, format, va);
1583 zend_throw_exception(zend_ce_type_error, message, 0);
1584 efree(message);
1585 va_end(va);
1586 } /* }}} */
1587
zend_argument_count_error(const char * format,...)1588 ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) /* {{{ */
1589 {
1590 va_list va;
1591 char *message = NULL;
1592
1593 va_start(va, format);
1594 zend_vspprintf(&message, 0, format, va);
1595 zend_throw_exception(zend_ce_argument_count_error, message, 0);
1596 efree(message);
1597
1598 va_end(va);
1599 } /* }}} */
1600
zend_value_error(const char * format,...)1601 ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) /* {{{ */
1602 {
1603 va_list va;
1604 char *message = NULL;
1605
1606 va_start(va, format);
1607 zend_vspprintf(&message, 0, format, va);
1608 zend_throw_exception(zend_ce_value_error, message, 0);
1609 efree(message);
1610 va_end(va);
1611 } /* }}} */
1612
zend_output_debug_string(zend_bool trigger_break,const char * format,...)1613 ZEND_API ZEND_COLD void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
1614 {
1615 #if ZEND_DEBUG
1616 va_list args;
1617
1618 va_start(args, format);
1619 # ifdef ZEND_WIN32
1620 {
1621 char output_buf[1024];
1622
1623 vsnprintf(output_buf, 1024, format, args);
1624 OutputDebugString(output_buf);
1625 OutputDebugString("\n");
1626 if (trigger_break && IsDebuggerPresent()) {
1627 DebugBreak();
1628 }
1629 }
1630 # else
1631 vfprintf(stderr, format, args);
1632 fprintf(stderr, "\n");
1633 # endif
1634 va_end(args);
1635 #endif
1636 }
1637 /* }}} */
1638
zend_user_exception_handler(void)1639 ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
1640 {
1641 zval orig_user_exception_handler;
1642 zval params[1], retval2;
1643 zend_object *old_exception;
1644
1645 if (zend_is_unwind_exit(EG(exception))) {
1646 return;
1647 }
1648
1649 old_exception = EG(exception);
1650 EG(exception) = NULL;
1651 ZVAL_OBJ(¶ms[0], old_exception);
1652 ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1653
1654 if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) {
1655 zval_ptr_dtor(&retval2);
1656 if (EG(exception)) {
1657 OBJ_RELEASE(EG(exception));
1658 EG(exception) = NULL;
1659 }
1660 OBJ_RELEASE(old_exception);
1661 } else {
1662 EG(exception) = old_exception;
1663 }
1664 } /* }}} */
1665
zend_execute_scripts(int type,zval * retval,int file_count,...)1666 ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1667 {
1668 va_list files;
1669 int i;
1670 zend_file_handle *file_handle;
1671 zend_op_array *op_array;
1672 zend_result ret = SUCCESS;
1673
1674 va_start(files, file_count);
1675 for (i = 0; i < file_count; i++) {
1676 file_handle = va_arg(files, zend_file_handle *);
1677 if (!file_handle) {
1678 continue;
1679 }
1680
1681 if (ret == FAILURE) {
1682 /* If a failure occurred in one of the earlier files,
1683 * only destroy the following file handles. */
1684 zend_file_handle_dtor(file_handle);
1685 continue;
1686 }
1687
1688 op_array = zend_compile_file(file_handle, type);
1689 if (file_handle->opened_path) {
1690 zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1691 }
1692 zend_destroy_file_handle(file_handle);
1693 if (op_array) {
1694 zend_execute(op_array, retval);
1695 zend_exception_restore();
1696 if (UNEXPECTED(EG(exception))) {
1697 if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1698 zend_user_exception_handler();
1699 }
1700 if (EG(exception)) {
1701 ret = zend_exception_error(EG(exception), E_ERROR);
1702 }
1703 }
1704 destroy_op_array(op_array);
1705 efree_size(op_array, sizeof(zend_op_array));
1706 } else if (type==ZEND_REQUIRE) {
1707 ret = FAILURE;
1708 }
1709 }
1710 va_end(files);
1711
1712 return ret;
1713 }
1714 /* }}} */
1715
1716 #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1717
zend_make_compiled_string_description(const char * name)1718 ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1719 {
1720 const char *cur_filename;
1721 int cur_lineno;
1722 char *compiled_string_description;
1723
1724 if (zend_is_compiling()) {
1725 cur_filename = ZSTR_VAL(zend_get_compiled_filename());
1726 cur_lineno = zend_get_compiled_lineno();
1727 } else if (zend_is_executing()) {
1728 cur_filename = zend_get_executed_filename();
1729 cur_lineno = zend_get_executed_lineno();
1730 } else {
1731 cur_filename = "Unknown";
1732 cur_lineno = 0;
1733 }
1734
1735 zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1736 return compiled_string_description;
1737 }
1738 /* }}} */
1739
free_estring(char ** str_p)1740 void free_estring(char **str_p) /* {{{ */
1741 {
1742 efree(*str_p);
1743 }
1744 /* }}} */
1745
zend_map_ptr_reset(void)1746 ZEND_API void zend_map_ptr_reset(void)
1747 {
1748 CG(map_ptr_last) = global_map_ptr_last;
1749 }
1750
zend_map_ptr_new(void)1751 ZEND_API void *zend_map_ptr_new(void)
1752 {
1753 void **ptr;
1754
1755 if (CG(map_ptr_last) >= CG(map_ptr_size)) {
1756 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
1757 // TODO: error ???
1758 ZEND_UNREACHABLE();
1759 #elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
1760 /* Grow map_ptr table */
1761 CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
1762 ZEND_MAP_PTR_SET_REAL_BASE(CG(map_ptr_base), perealloc(ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)), CG(map_ptr_size) * sizeof(void*), 1));
1763 #else
1764 # error "Unknown ZEND_MAP_PTR_KIND"
1765 #endif
1766 }
1767 ptr = (void**)ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)) + CG(map_ptr_last);
1768 *ptr = NULL;
1769 CG(map_ptr_last)++;
1770 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
1771 return ptr;
1772 #elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
1773 return ZEND_MAP_PTR_PTR2OFFSET(ptr);
1774 #else
1775 # error "Unknown ZEND_MAP_PTR_KIND"
1776 #endif
1777 }
1778
zend_map_ptr_extend(size_t last)1779 ZEND_API void zend_map_ptr_extend(size_t last)
1780 {
1781 if (last > CG(map_ptr_last)) {
1782 void **ptr;
1783
1784 if (last >= CG(map_ptr_size)) {
1785 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
1786 /* This may never happen */
1787 ZEND_UNREACHABLE();
1788 #elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
1789 /* Grow map_ptr table */
1790 CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);
1791 ZEND_MAP_PTR_SET_REAL_BASE(CG(map_ptr_base), perealloc(ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)), CG(map_ptr_size) * sizeof(void*), 1));
1792 #else
1793 # error "Unknown ZEND_MAP_PTR_KIND"
1794 #endif
1795 }
1796 ptr = (void**)ZEND_MAP_PTR_REAL_BASE(CG(map_ptr_base)) + CG(map_ptr_last);
1797 memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*));
1798 CG(map_ptr_last) = last;
1799 }
1800 }
1801