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