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_constants.h"
22 #include "zend_exceptions.h"
23 #include "zend_execute.h"
24 #include "zend_variables.h"
25 #include "zend_operators.h"
26 #include "zend_globals.h"
27 #include "zend_API.h"
28 #include "zend_constants_arginfo.h"
29
30 /* Protection from recursive self-referencing class constants */
31 #define IS_CONSTANT_VISITED_MARK 0x80
32
33 #define IS_CONSTANT_VISITED(zv) (Z_CONSTANT_FLAGS_P(zv) & IS_CONSTANT_VISITED_MARK)
34 #define MARK_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) |= IS_CONSTANT_VISITED_MARK
35 #define RESET_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK
36
37 /* Use for special null/true/false constants. */
38 static zend_constant *null_const, *true_const, *false_const;
39
free_zend_constant(zval * zv)40 void free_zend_constant(zval *zv)
41 {
42 zend_constant *c = Z_PTR_P(zv);
43
44 if (!(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) {
45 zval_ptr_dtor_nogc(&c->value);
46 if (c->name) {
47 zend_string_release_ex(c->name, 0);
48 }
49 efree(c);
50 } else {
51 zval_internal_ptr_dtor(&c->value);
52 if (c->name) {
53 zend_string_release_ex(c->name, 1);
54 }
55 free(c);
56 }
57 }
58
59
60 #ifdef ZTS
copy_zend_constant(zval * zv)61 static void copy_zend_constant(zval *zv)
62 {
63 zend_constant *c = Z_PTR_P(zv);
64
65 ZEND_ASSERT(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
66 Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), 1);
67 memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
68
69 c = Z_PTR_P(zv);
70 c->name = zend_string_copy(c->name);
71 if (Z_TYPE(c->value) == IS_STRING) {
72 Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
73 }
74 }
75
76
zend_copy_constants(HashTable * target,HashTable * source)77 void zend_copy_constants(HashTable *target, HashTable *source)
78 {
79 zend_hash_copy(target, source, copy_zend_constant);
80 }
81 #endif
82
83
clean_module_constant(zval * el,void * arg)84 static int clean_module_constant(zval *el, void *arg)
85 {
86 zend_constant *c = (zend_constant *)Z_PTR_P(el);
87 int module_number = *(int *)arg;
88
89 if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) {
90 return ZEND_HASH_APPLY_REMOVE;
91 } else {
92 return ZEND_HASH_APPLY_KEEP;
93 }
94 }
95
96
clean_module_constants(int module_number)97 void clean_module_constants(int module_number)
98 {
99 zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
100 }
101
zend_startup_constants(void)102 void zend_startup_constants(void)
103 {
104 EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
105 zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
106 }
107
108
109
zend_register_standard_constants(void)110 void zend_register_standard_constants(void)
111 {
112 register_zend_constants_symbols(0);
113
114 true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
115 false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
116 null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
117 }
118
zend_register_null_constant(const char * name,size_t name_len,int flags,int module_number)119 ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
120 {
121 zend_constant c;
122
123 ZVAL_NULL(&c.value);
124 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
125 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
126 zend_register_constant(&c);
127 }
128
zend_register_bool_constant(const char * name,size_t name_len,bool bval,int flags,int module_number)129 ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
130 {
131 zend_constant c;
132
133 ZVAL_BOOL(&c.value, bval);
134 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
135 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
136 zend_register_constant(&c);
137 }
138
zend_register_long_constant(const char * name,size_t name_len,zend_long lval,int flags,int module_number)139 ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
140 {
141 zend_constant c;
142
143 ZVAL_LONG(&c.value, lval);
144 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
145 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
146 zend_register_constant(&c);
147 }
148
149
zend_register_double_constant(const char * name,size_t name_len,double dval,int flags,int module_number)150 ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
151 {
152 zend_constant c;
153
154 ZVAL_DOUBLE(&c.value, dval);
155 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
156 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
157 zend_register_constant(&c);
158 }
159
160
zend_register_stringl_constant(const char * name,size_t name_len,const char * strval,size_t strlen,int flags,int module_number)161 ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number)
162 {
163 zend_constant c;
164
165 ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
166 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
167 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
168 zend_register_constant(&c);
169 }
170
171
zend_register_string_constant(const char * name,size_t name_len,const char * strval,int flags,int module_number)172 ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
173 {
174 zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
175 }
176
zend_get_halt_offset_constant(const char * name,size_t name_len)177 static zend_constant *zend_get_halt_offset_constant(const char *name, size_t name_len)
178 {
179 zend_constant *c;
180 static const char haltoff[] = "__COMPILER_HALT_OFFSET__";
181
182 if (!EG(current_execute_data)) {
183 return NULL;
184 } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
185 !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
186 const char *cfilename;
187 zend_string *haltname;
188 size_t clen;
189
190 cfilename = zend_get_executed_filename();
191 clen = strlen(cfilename);
192 /* check for __COMPILER_HALT_OFFSET__ */
193 haltname = zend_mangle_property_name(haltoff,
194 sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
195 c = zend_hash_find_ptr(EG(zend_constants), haltname);
196 zend_string_efree(haltname);
197 return c;
198 } else {
199 return NULL;
200 }
201 }
202
_zend_get_special_const(const char * name,size_t len)203 ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
204 {
205 if (len == 4) {
206 if ((name[0] == 'n' || name[0] == 'N') &&
207 (name[1] == 'u' || name[1] == 'U') &&
208 (name[2] == 'l' || name[2] == 'L') &&
209 (name[3] == 'l' || name[3] == 'L')
210 ) {
211 return null_const;
212 }
213 if ((name[0] == 't' || name[0] == 'T') &&
214 (name[1] == 'r' || name[1] == 'R') &&
215 (name[2] == 'u' || name[2] == 'U') &&
216 (name[3] == 'e' || name[3] == 'E')
217 ) {
218 return true_const;
219 }
220 } else {
221 if ((name[0] == 'f' || name[0] == 'F') &&
222 (name[1] == 'a' || name[1] == 'A') &&
223 (name[2] == 'l' || name[2] == 'L') &&
224 (name[3] == 's' || name[3] == 'S') &&
225 (name[4] == 'e' || name[4] == 'E')
226 ) {
227 return false_const;
228 }
229 }
230 return NULL;
231 }
232 /* }}} */
233
zend_verify_const_access(zend_class_constant * c,zend_class_entry * scope)234 ZEND_API bool zend_verify_const_access(zend_class_constant *c, zend_class_entry *scope) /* {{{ */
235 {
236 if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
237 return 1;
238 } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
239 return (c->ce == scope);
240 } else {
241 ZEND_ASSERT(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PROTECTED);
242 return zend_check_protected(c->ce, scope);
243 }
244 }
245 /* }}} */
246
zend_get_constant_str_impl(const char * name,size_t name_len)247 static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
248 {
249 zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
250 if (c) {
251 return c;
252 }
253
254 c = zend_get_halt_offset_constant(name, name_len);
255 if (c) {
256 return c;
257 }
258
259 return zend_get_special_const(name, name_len);
260 }
261
zend_get_constant_str(const char * name,size_t name_len)262 ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
263 {
264 zend_constant *c = zend_get_constant_str_impl(name, name_len);
265 if (c) {
266 return &c->value;
267 }
268 return NULL;
269 }
270
zend_get_constant_ptr(zend_string * name)271 ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name)
272 {
273 zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
274 if (c) {
275 return c;
276 }
277
278 c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
279 if (c) {
280 return c;
281 }
282
283 return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
284 }
285
zend_get_constant(zend_string * name)286 ZEND_API zval *zend_get_constant(zend_string *name)
287 {
288 zend_constant *c = zend_get_constant_ptr(name);
289 if (c) {
290 return &c->value;
291 }
292 return NULL;
293 }
294
zend_get_class_constant_ex(zend_string * class_name,zend_string * constant_name,zend_class_entry * scope,uint32_t flags)295 ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, zend_class_entry *scope, uint32_t flags)
296 {
297 zend_class_entry *ce = NULL;
298 zend_class_constant *c = NULL;
299 zval *ret_constant = NULL;
300
301 if (ZSTR_HAS_CE_CACHE(class_name)) {
302 ce = ZSTR_GET_CE_CACHE(class_name);
303 if (!ce) {
304 ce = zend_fetch_class(class_name, flags);
305 }
306 } else if (zend_string_equals_literal_ci(class_name, "self")) {
307 if (UNEXPECTED(!scope)) {
308 zend_throw_error(NULL, "Cannot access \"self\" when no class scope is active");
309 goto failure;
310 }
311 ce = scope;
312 } else if (zend_string_equals_literal_ci(class_name, "parent")) {
313 if (UNEXPECTED(!scope)) {
314 zend_throw_error(NULL, "Cannot access \"parent\" when no class scope is active");
315 goto failure;
316 } else if (UNEXPECTED(!scope->parent)) {
317 zend_throw_error(NULL, "Cannot access \"parent\" when current class scope has no parent");
318 goto failure;
319 } else {
320 ce = scope->parent;
321 }
322 } else if (zend_string_equals_ci(class_name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
323 ce = zend_get_called_scope(EG(current_execute_data));
324 if (UNEXPECTED(!ce)) {
325 zend_throw_error(NULL, "Cannot access \"static\" when no class scope is active");
326 goto failure;
327 }
328 } else {
329 ce = zend_fetch_class(class_name, flags);
330 }
331 if (ce) {
332 c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constant_name);
333 if (c == NULL) {
334 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
335 zend_throw_error(NULL, "Undefined constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
336 goto failure;
337 }
338 ret_constant = NULL;
339 } else {
340 if (!zend_verify_const_access(c, scope)) {
341 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
342 zend_throw_error(NULL, "Cannot access %s constant %s::%s", zend_visibility_string(ZEND_CLASS_CONST_FLAGS(c)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
343 }
344 goto failure;
345 }
346
347 if (UNEXPECTED(ce->ce_flags & ZEND_ACC_TRAIT)) {
348 /** Prevent accessing trait constants directly on cases like \defined() or \constant(), etc. */
349 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
350 zend_throw_error(NULL, "Cannot access trait constant %s::%s directly", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
351 }
352 goto failure;
353 }
354
355 if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
356 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
357 zend_deprecated_class_constant(c, constant_name);
358 if (EG(exception)) {
359 goto failure;
360 }
361 }
362 }
363 ret_constant = &c->value;
364 }
365 }
366
367 if (ret_constant && Z_TYPE_P(ret_constant) == IS_CONSTANT_AST) {
368 zend_result ret;
369
370 if (IS_CONSTANT_VISITED(ret_constant)) {
371 zend_throw_error(NULL, "Cannot declare self-referencing constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
372 ret_constant = NULL;
373 goto failure;
374 }
375
376 MARK_CONSTANT_VISITED(ret_constant);
377 ret = zend_update_class_constant(c, constant_name, c->ce);
378 RESET_CONSTANT_VISITED(ret_constant);
379
380 if (UNEXPECTED(ret != SUCCESS)) {
381 ret_constant = NULL;
382 goto failure;
383 }
384 }
385 failure:
386 return ret_constant;
387 }
388
zend_get_constant_ex(zend_string * cname,zend_class_entry * scope,uint32_t flags)389 ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
390 {
391 zend_constant *c;
392 const char *colon;
393 const char *name = ZSTR_VAL(cname);
394 size_t name_len = ZSTR_LEN(cname);
395
396 /* Skip leading \\ */
397 if (name[0] == '\\') {
398 name += 1;
399 name_len -= 1;
400 cname = NULL;
401 }
402
403 if ((colon = zend_memrchr(name, ':', name_len)) &&
404 colon > name && (*(colon - 1) == ':')) {
405 int class_name_len = colon - name - 1;
406 size_t const_name_len = name_len - class_name_len - 2;
407 zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0);
408 zend_string *class_name = zend_string_init_interned(name, class_name_len, 0);
409 zval *ret_constant = zend_get_class_constant_ex(class_name, constant_name, scope, flags);
410
411 zend_string_release_ex(class_name, 0);
412 zend_string_efree(constant_name);
413 return ret_constant;
414 }
415
416 /* non-class constant */
417 if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
418 /* compound constant name */
419 int prefix_len = colon - name;
420 size_t const_name_len = name_len - prefix_len - 1;
421 const char *constant_name = colon + 1;
422 char *lcname;
423 size_t lcname_len;
424 ALLOCA_FLAG(use_heap)
425
426 /* Lowercase the namespace portion */
427 lcname_len = prefix_len + 1 + const_name_len;
428 lcname = do_alloca(lcname_len + 1, use_heap);
429 zend_str_tolower_copy(lcname, name, prefix_len);
430
431 lcname[prefix_len] = '\\';
432 memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1);
433
434 c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
435 free_alloca(lcname, use_heap);
436
437 if (!c) {
438 if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
439 /* name requires runtime resolution, need to check non-namespaced name */
440 c = zend_get_constant_str_impl(constant_name, const_name_len);
441 }
442 }
443 } else {
444 if (cname) {
445 c = zend_get_constant_ptr(cname);
446 } else {
447 c = zend_get_constant_str_impl(name, name_len);
448 }
449 }
450
451 if (!c) {
452 if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
453 zend_throw_error(NULL, "Undefined constant \"%s\"", name);
454 }
455 return NULL;
456 }
457
458 if (!(flags & ZEND_FETCH_CLASS_SILENT) && (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)) {
459 zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
460 }
461 return &c->value;
462 }
463
zend_hash_add_constant(HashTable * ht,zend_string * key,zend_constant * c)464 static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
465 {
466 void *ret;
467 zend_constant *copy = pemalloc(sizeof(zend_constant), ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
468
469 memcpy(copy, c, sizeof(zend_constant));
470 ret = zend_hash_add_ptr(ht, key, copy);
471 if (!ret) {
472 pefree(copy, ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
473 }
474 return ret;
475 }
476
zend_register_constant(zend_constant * c)477 ZEND_API zend_result zend_register_constant(zend_constant *c)
478 {
479 zend_string *lowercase_name = NULL;
480 zend_string *name;
481 zend_result ret = SUCCESS;
482 bool persistent = (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) != 0;
483
484 #if 0
485 printf("Registering constant for module %d\n", c->module_number);
486 #endif
487
488 const char *slash = strrchr(ZSTR_VAL(c->name), '\\');
489 if (slash) {
490 lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), persistent);
491 zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
492 lowercase_name = zend_new_interned_string(lowercase_name);
493 name = lowercase_name;
494 } else {
495 name = c->name;
496 }
497
498 /* Check if the user is trying to define any special constant */
499 if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__")
500 || (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name)))
501 || zend_hash_add_constant(EG(zend_constants), name, c) == NULL
502 ) {
503 zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name));
504 zend_string_release(c->name);
505 if (!persistent) {
506 zval_ptr_dtor_nogc(&c->value);
507 }
508 ret = FAILURE;
509 }
510 if (lowercase_name) {
511 zend_string_release(lowercase_name);
512 }
513 return ret;
514 }
515