1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2017 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@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #include "zend.h"
23 #include "zend_constants.h"
24 #include "zend_exceptions.h"
25 #include "zend_execute.h"
26 #include "zend_variables.h"
27 #include "zend_operators.h"
28 #include "zend_globals.h"
29 #include "zend_API.h"
30
free_zend_constant(zval * zv)31 void free_zend_constant(zval *zv)
32 {
33 zend_constant *c = Z_PTR_P(zv);
34
35 if (!(c->flags & CONST_PERSISTENT)) {
36 zval_dtor(&c->value);
37 } else {
38 zval_internal_dtor(&c->value);
39 }
40 if (c->name) {
41 zend_string_release(c->name);
42 }
43 pefree(c, c->flags & CONST_PERSISTENT);
44 }
45
46
copy_zend_constant(zval * zv)47 static void copy_zend_constant(zval *zv)
48 {
49 zend_constant *c = Z_PTR_P(zv);
50
51 Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
52 memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
53
54 c = Z_PTR_P(zv);
55 c->name = zend_string_copy(c->name);
56 if (!(c->flags & CONST_PERSISTENT)) {
57 zval_copy_ctor(&c->value);
58 } else {
59 if (Z_TYPE(c->value) == IS_STRING) {
60 Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
61 }
62 }
63 }
64
65
zend_copy_constants(HashTable * target,HashTable * source)66 void zend_copy_constants(HashTable *target, HashTable *source)
67 {
68 zend_hash_copy(target, source, copy_zend_constant);
69 }
70
71
clean_non_persistent_constant(zval * zv)72 static int clean_non_persistent_constant(zval *zv)
73 {
74 zend_constant *c = Z_PTR_P(zv);
75 return (c->flags & CONST_PERSISTENT) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
76 }
77
78
clean_non_persistent_constant_full(zval * zv)79 static int clean_non_persistent_constant_full(zval *zv)
80 {
81 zend_constant *c = Z_PTR_P(zv);
82 return (c->flags & CONST_PERSISTENT) ? 0 : 1;
83 }
84
85
clean_module_constant(zval * el,void * arg)86 static int clean_module_constant(zval *el, void *arg)
87 {
88 zend_constant *c = (zend_constant *)Z_PTR_P(el);
89 int module_number = *(int *)arg;
90
91 if (c->module_number == module_number) {
92 return 1;
93 } else {
94 return 0;
95 }
96 }
97
98
clean_module_constants(int module_number)99 void clean_module_constants(int module_number)
100 {
101 zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
102 }
103
104
zend_startup_constants(void)105 int zend_startup_constants(void)
106 {
107 EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
108
109 zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
110 return SUCCESS;
111 }
112
113
114
zend_register_standard_constants(void)115 void zend_register_standard_constants(void)
116 {
117 REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
118 REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS);
119 REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
120 REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
121 REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
122 REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
123 REGISTER_MAIN_LONG_CONSTANT("E_DEPRECATED", E_DEPRECATED, CONST_PERSISTENT | CONST_CS);
124 REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
125 REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
126 REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
127 REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
128 REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
129 REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
130 REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
131 REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
132
133 REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
134
135 REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_PROVIDE_OBJECT", DEBUG_BACKTRACE_PROVIDE_OBJECT, CONST_PERSISTENT | CONST_CS);
136 REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_IGNORE_ARGS", DEBUG_BACKTRACE_IGNORE_ARGS, CONST_PERSISTENT | CONST_CS);
137 /* true/false constants */
138 {
139 REGISTER_MAIN_BOOL_CONSTANT("TRUE", 1, CONST_PERSISTENT | CONST_CT_SUBST);
140 REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT | CONST_CT_SUBST);
141 REGISTER_MAIN_BOOL_CONSTANT("ZEND_THREAD_SAFE", ZTS_V, CONST_PERSISTENT | CONST_CS);
142 REGISTER_MAIN_BOOL_CONSTANT("ZEND_DEBUG_BUILD", ZEND_DEBUG, CONST_PERSISTENT | CONST_CS);
143 }
144 REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT | CONST_CT_SUBST);
145 }
146
147
zend_shutdown_constants(void)148 int zend_shutdown_constants(void)
149 {
150 zend_hash_destroy(EG(zend_constants));
151 free(EG(zend_constants));
152 return SUCCESS;
153 }
154
155
clean_non_persistent_constants(void)156 void clean_non_persistent_constants(void)
157 {
158 if (EG(full_tables_cleanup)) {
159 zend_hash_apply(EG(zend_constants), clean_non_persistent_constant_full);
160 } else {
161 zend_hash_reverse_apply(EG(zend_constants), clean_non_persistent_constant);
162 }
163 }
164
zend_register_null_constant(const char * name,size_t name_len,int flags,int module_number)165 ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
166 {
167 zend_constant c;
168
169 ZVAL_NULL(&c.value);
170 c.flags = flags;
171 c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
172 c.module_number = module_number;
173 zend_register_constant(&c);
174 }
175
zend_register_bool_constant(const char * name,size_t name_len,zend_bool bval,int flags,int module_number)176 ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, zend_bool bval, int flags, int module_number)
177 {
178 zend_constant c;
179
180 ZVAL_BOOL(&c.value, bval);
181 c.flags = flags;
182 c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
183 c.module_number = module_number;
184 zend_register_constant(&c);
185 }
186
zend_register_long_constant(const char * name,size_t name_len,zend_long lval,int flags,int module_number)187 ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
188 {
189 zend_constant c;
190
191 ZVAL_LONG(&c.value, lval);
192 c.flags = flags;
193 c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
194 c.module_number = module_number;
195 zend_register_constant(&c);
196 }
197
198
zend_register_double_constant(const char * name,size_t name_len,double dval,int flags,int module_number)199 ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
200 {
201 zend_constant c;
202
203 ZVAL_DOUBLE(&c.value, dval);
204 c.flags = flags;
205 c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
206 c.module_number = module_number;
207 zend_register_constant(&c);
208 }
209
210
zend_register_stringl_constant(const char * name,size_t name_len,char * strval,size_t strlen,int flags,int module_number)211 ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, char *strval, size_t strlen, int flags, int module_number)
212 {
213 zend_constant c;
214
215 ZVAL_NEW_STR(&c.value, zend_string_init(strval, strlen, flags & CONST_PERSISTENT));
216 c.flags = flags;
217 c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
218 c.module_number = module_number;
219 zend_register_constant(&c);
220 }
221
222
zend_register_string_constant(const char * name,size_t name_len,char * strval,int flags,int module_number)223 ZEND_API void zend_register_string_constant(const char *name, size_t name_len, char *strval, int flags, int module_number)
224 {
225 zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
226 }
227
zend_get_special_constant(const char * name,size_t name_len)228 static zend_constant *zend_get_special_constant(const char *name, size_t name_len)
229 {
230 zend_constant *c;
231 static char haltoff[] = "__COMPILER_HALT_OFFSET__";
232
233 if (!EG(current_execute_data)) {
234 return NULL;
235 } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
236 !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
237 const char *cfilename;
238 zend_string *haltname;
239 size_t clen;
240
241 cfilename = zend_get_executed_filename();
242 clen = strlen(cfilename);
243 /* check for __COMPILER_HALT_OFFSET__ */
244 haltname = zend_mangle_property_name(haltoff,
245 sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
246 c = zend_hash_find_ptr(EG(zend_constants), haltname);
247 zend_string_free(haltname);
248 return c;
249 } else {
250 return NULL;
251 }
252 }
253
254
zend_get_constant_str(const char * name,size_t name_len)255 ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
256 {
257 zend_constant *c;
258 ALLOCA_FLAG(use_heap)
259
260 if ((c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len)) == NULL) {
261 char *lcname = do_alloca(name_len + 1, use_heap);
262 zend_str_tolower_copy(lcname, name, name_len);
263 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name_len)) != NULL) {
264 if (c->flags & CONST_CS) {
265 c = NULL;
266 }
267 } else {
268 c = zend_get_special_constant(name, name_len);
269 }
270 free_alloca(lcname, use_heap);
271 }
272
273 return c ? &c->value : NULL;
274 }
275
zend_get_constant(zend_string * name)276 ZEND_API zval *zend_get_constant(zend_string *name)
277 {
278 zend_constant *c;
279 ALLOCA_FLAG(use_heap)
280
281 if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) {
282 char *lcname = do_alloca(ZSTR_LEN(name) + 1, use_heap);
283 zend_str_tolower_copy(lcname, ZSTR_VAL(name), ZSTR_LEN(name));
284 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, ZSTR_LEN(name))) != NULL) {
285 if (c->flags & CONST_CS) {
286 c = NULL;
287 }
288 } else {
289 c = zend_get_special_constant(ZSTR_VAL(name), ZSTR_LEN(name));
290 }
291 free_alloca(lcname, use_heap);
292 }
293
294 return c ? &c->value : NULL;
295 }
296
zend_get_constant_ex(zend_string * cname,zend_class_entry * scope,zend_ulong flags)297 ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, zend_ulong flags)
298 {
299 zend_constant *c;
300 const char *colon;
301 zend_class_entry *ce = NULL;
302 const char *name = ZSTR_VAL(cname);
303 size_t name_len = ZSTR_LEN(cname);
304
305 /* Skip leading \\ */
306 if (name[0] == '\\') {
307 name += 1;
308 name_len -= 1;
309 cname = NULL;
310 }
311
312 if ((colon = zend_memrchr(name, ':', name_len)) &&
313 colon > name && (*(colon - 1) == ':')) {
314 int class_name_len = colon - name - 1;
315 size_t const_name_len = name_len - class_name_len - 2;
316 zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0);
317 zend_string *class_name = zend_string_init(name, class_name_len, 0);
318 zval *ret_constant = NULL;
319
320 if (!scope) {
321 if (EG(current_execute_data)) {
322 scope = EG(scope);
323 } else {
324 scope = CG(active_class_entry);
325 }
326 }
327
328 if (zend_string_equals_literal_ci(class_name, "self")) {
329 if (UNEXPECTED(!scope)) {
330 zend_throw_error(NULL, "Cannot access self:: when no class scope is active");
331 goto failure;
332 }
333 ce = scope;
334 } else if (zend_string_equals_literal_ci(class_name, "parent")) {
335 if (UNEXPECTED(!scope)) {
336 zend_throw_error(NULL, "Cannot access parent:: when no class scope is active");
337 goto failure;
338 } else if (UNEXPECTED(!scope->parent)) {
339 zend_throw_error(NULL, "Cannot access parent:: when current class scope has no parent");
340 goto failure;
341 } else {
342 ce = scope->parent;
343 }
344 } else if (zend_string_equals_literal_ci(class_name, "static")) {
345 ce = zend_get_called_scope(EG(current_execute_data));
346 if (UNEXPECTED(!ce)) {
347 zend_throw_error(NULL, "Cannot access static:: when no class scope is active");
348 goto failure;
349 }
350 } else {
351 ce = zend_fetch_class(class_name, flags);
352 }
353 if (ce) {
354 ret_constant = zend_hash_find(&ce->constants_table, constant_name);
355 if (ret_constant == NULL) {
356 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
357 zend_throw_error(NULL, "Undefined class constant '%s::%s'", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
358 }
359 } else if (Z_ISREF_P(ret_constant)) {
360 ret_constant = Z_REFVAL_P(ret_constant);
361 }
362 }
363
364 failure:
365 zend_string_release(class_name);
366 zend_string_free(constant_name);
367 if (ret_constant && Z_CONSTANT_P(ret_constant)) {
368 if (UNEXPECTED(zval_update_constant_ex(ret_constant, 1, ce) != SUCCESS)) {
369 return NULL;
370 }
371 }
372 return ret_constant;
373 }
374
375 /* non-class constant */
376 if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
377 /* compound constant name */
378 int prefix_len = colon - name;
379 size_t const_name_len = name_len - prefix_len - 1;
380 const char *constant_name = colon + 1;
381 char *lcname;
382 size_t lcname_len;
383 ALLOCA_FLAG(use_heap)
384
385 lcname_len = prefix_len + 1 + const_name_len;
386 lcname = do_alloca(lcname_len + 1, use_heap);
387 zend_str_tolower_copy(lcname, name, prefix_len);
388 /* Check for namespace constant */
389
390 lcname[prefix_len] = '\\';
391 memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1);
392
393 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) == NULL) {
394 /* try lowercase */
395 zend_str_tolower(lcname + prefix_len + 1, const_name_len);
396 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) != NULL) {
397 if ((c->flags & CONST_CS) != 0) {
398 c = NULL;
399 }
400 }
401 }
402 free_alloca(lcname, use_heap);
403 if (c) {
404 return &c->value;
405 }
406 /* name requires runtime resolution, need to check non-namespaced name */
407 if ((flags & IS_CONSTANT_UNQUALIFIED) != 0) {
408 return zend_get_constant_str(constant_name, const_name_len);
409 }
410 return NULL;
411 }
412
413 if (cname) {
414 return zend_get_constant(cname);
415 } else {
416 return zend_get_constant_str(name, name_len);
417 }
418 }
419
zend_quick_get_constant(const zval * key,zend_ulong flags)420 zend_constant *zend_quick_get_constant(const zval *key, zend_ulong flags)
421 {
422 zend_constant *c;
423
424 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
425 key++;
426 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
427 (c->flags & CONST_CS) != 0) {
428 if ((flags & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) {
429 key++;
430 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
431 key++;
432 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
433 (c->flags & CONST_CS) != 0) {
434
435 key--;
436 c = NULL;
437 }
438 }
439 } else {
440 key--;
441 c = NULL;
442 }
443 }
444 }
445 return c;
446 }
447
zend_hash_add_constant(HashTable * ht,zend_string * key,zend_constant * c)448 static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
449 {
450 void *ret;
451 zend_constant *copy = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
452
453 memcpy(copy, c, sizeof(zend_constant));
454 ret = zend_hash_add_ptr(ht, key, copy);
455 if (!ret) {
456 pefree(copy, c->flags & CONST_PERSISTENT);
457 }
458 return ret;
459 }
460
zend_register_constant(zend_constant * c)461 ZEND_API int zend_register_constant(zend_constant *c)
462 {
463 zend_string *lowercase_name = NULL;
464 zend_string *name;
465 int ret = SUCCESS;
466
467 #if 0
468 printf("Registering constant for module %d\n", c->module_number);
469 #endif
470
471 if (!(c->flags & CONST_CS)) {
472 lowercase_name = zend_string_alloc(ZSTR_LEN(c->name), c->flags & CONST_PERSISTENT);
473 zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ZSTR_VAL(c->name), ZSTR_LEN(c->name));
474 lowercase_name = zend_new_interned_string(lowercase_name);
475 name = lowercase_name;
476 } else {
477 char *slash = strrchr(ZSTR_VAL(c->name), '\\');
478 if (slash) {
479 lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), c->flags & CONST_PERSISTENT);
480 zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
481 lowercase_name = zend_new_interned_string(lowercase_name);
482 name = lowercase_name;
483 } else {
484 name = c->name;
485 }
486 }
487
488 /* Check if the user is trying to define the internal pseudo constant name __COMPILER_HALT_OFFSET__ */
489 if ((ZSTR_LEN(c->name) == sizeof("__COMPILER_HALT_OFFSET__")-1
490 && !memcmp(ZSTR_VAL(name), "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1))
491 || zend_hash_add_constant(EG(zend_constants), name, c) == NULL) {
492
493 /* The internal __COMPILER_HALT_OFFSET__ is prefixed by NULL byte */
494 if (ZSTR_VAL(c->name)[0] == '\0' && ZSTR_LEN(c->name) > sizeof("\0__COMPILER_HALT_OFFSET__")-1
495 && memcmp(ZSTR_VAL(name), "\0__COMPILER_HALT_OFFSET__", sizeof("\0__COMPILER_HALT_OFFSET__")) == 0) {
496 }
497 zend_error(E_NOTICE,"Constant %s already defined", ZSTR_VAL(name));
498 zend_string_release(c->name);
499 if (!(c->flags & CONST_PERSISTENT)) {
500 zval_dtor(&c->value);
501 }
502 ret = FAILURE;
503 }
504 if (lowercase_name) {
505 zend_string_release(lowercase_name);
506 }
507 return ret;
508 }
509
510
511 /*
512 * Local variables:
513 * tab-width: 4
514 * c-basic-offset: 4
515 * indent-tabs-mode: t
516 * End:
517 */
518