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