php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_constants.c
Go to the documentation of this file.
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"
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. */
38static zend_constant *null_const, *true_const, *false_const;
39
41{
43
45 zval_ptr_dtor_nogc(&c->value);
46 if (c->name) {
48 }
49 efree(c);
50 } else {
52 if (c->name) {
54 }
55 free(c);
56 }
57}
58
59
60#ifdef ZTS
61static void copy_zend_constant(zval *zv)
62{
64
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
77void zend_copy_constants(HashTable *target, HashTable *source)
78{
79 zend_hash_copy(target, source, copy_zend_constant);
80}
81#endif
82
83
84static int clean_module_constant(zval *el, void *arg)
85{
87 int module_number = *(int *)arg;
88
89 if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) {
91 } else {
93 }
94}
95
96
97void clean_module_constants(int module_number)
98{
99 zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
100}
101
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
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
119ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
120{
122
123 ZVAL_NULL(&c.value);
124 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
127}
128
129ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
130{
132
133 ZVAL_BOOL(&c.value, bval);
134 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
137}
138
139ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
140{
142
143 ZVAL_LONG(&c.value, lval);
144 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
147}
148
149
150ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
151{
153
155 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
158}
159
160
161ZEND_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{
164
166 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
169}
170
171
172ZEND_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
177static 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
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
235{
237 return 1;
239 return (c->ce == scope);
240 } else {
242 return zend_check_protected(c->ce, scope);
243 }
244}
245/* }}} */
246
247static 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
262ZEND_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
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
287{
289 if (c) {
290 return &c->value;
291 }
292 return NULL;
293}
294
296{
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 {
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
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
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) {
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 }
385failure:
386 return ret_constant;
387}
388
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) {
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) {
453 zend_throw_error(NULL, "Undefined constant \"%s\"", name);
454 }
455 return NULL;
456 }
457
459 zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
460 }
461 return &c->value;
462}
463
464static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
465{
466 void *ret;
468
469 memcpy(copy, c, sizeof(zend_constant));
470 ret = zend_hash_add_ptr(ht, key, copy);
471 if (!ret) {
473 }
474 return ret;
475}
476
478{
479 zend_string *lowercase_name = NULL;
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}
size_t len
Definition apprentice.c:174
bool exception
Definition assert.c:30
strval(mixed $value)
printf(string $format, mixed ... $values)
copy(string $from, string $to, $context=null)
strrchr(string $haystack, string $needle, bool $before_needle=false)
zval * zv
Definition ffi.c:3975
memcpy(ptr1, ptr2, size)
zval * arg
Definition ffi.c:3975
HashTable * ht
Definition ffi.c:4838
ffi persistent
Definition ffi.c:3633
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
unsigned char key[REFLECTION_KEY_LEN]
zend_string * lcname
zend_class_entry * ce
uint32_t ce_flags
Definition zend.h:156
zend_string * name
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_error(int type, const char *format,...)
Definition zend.c:1666
ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope)
Definition zend_API.c:1490
#define CE_CONSTANTS_TABLE(ce)
Definition zend_API.h:331
#define efree(ptr)
Definition zend_alloc.h:155
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
struct _zval_struct zval
strlen(string $string)
zend_string_release_ex(func->internal_function.function_name, 0)
ZEND_API zend_string * zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal)
#define ZEND_CLASS_CONST_FLAGS(c)
struct _zend_class_constant zend_class_constant
#define ZEND_ACC_TRAIT
#define ZEND_ACC_PRIVATE
#define ZEND_ACC_PUBLIC
#define ZEND_ACC_DEPRECATED
#define ZEND_FETCH_CLASS_SILENT
#define IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE
char * zend_visibility_string(uint32_t fn_flags)
#define ZEND_ACC_PROTECTED
#define ZEND_API
ZEND_API zval * zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
#define IS_CONSTANT_VISITED(zv)
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)
ZEND_API zend_constant * _zend_get_special_const(const char *name, size_t len)
ZEND_API zval * zend_get_constant(zend_string *name)
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
#define RESET_CONSTANT_VISITED(zv)
void clean_module_constants(int module_number)
void zend_register_standard_constants(void)
ZEND_API zend_constant * zend_get_constant_ptr(zend_string *name)
ZEND_API zend_result zend_register_constant(zend_constant *c)
ZEND_API zval * zend_get_constant_str(const char *name, size_t name_len)
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
void free_zend_constant(zval *zv)
ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
#define MARK_CONSTANT_VISITED(zv)
ZEND_API bool zend_verify_const_access(zend_class_constant *c, zend_class_entry *scope)
ZEND_API zval * zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, zend_class_entry *scope, uint32_t flags)
void zend_startup_constants(void)
#define ZEND_CONSTANT_FLAGS(c)
#define ZEND_CONSTANT_SET_FLAGS(c, _flags, _module_number)
#define ZEND_CONSTANT_DTOR
#define ZEND_CONSTANT_MODULE_NUMBER(c)
#define CONST_PERSISTENT
struct _zend_constant zend_constant
#define CONST_DEPRECATED
#define E_WARNING
Definition zend_errors.h:24
#define E_DEPRECATED
Definition zend_errors.h:37
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_class_constant(const zend_class_constant *c, const zend_string *constant_name)
ZEND_API zend_class_entry * zend_get_called_scope(zend_execute_data *ex)
ZEND_API const char * zend_get_executed_filename(void)
ZEND_API zend_class_entry * zend_fetch_class(zend_string *class_name, uint32_t fetch_type)
#define EG(v)
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument)
Definition zend_hash.c:2099
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
Definition zend_hash.c:2240
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_APPLY_REMOVE
Definition zend_hash.h:147
#define ZEND_HASH_APPLY_KEEP
Definition zend_hash.h:146
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_entry *scope)
ZEND_API char *ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length)
ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length)
#define ALLOCA_FLAG(name)
#define do_alloca(p, use_heap)
#define ZEND_ASSERT(c)
#define free_alloca(p, use_heap)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
ZEND_API zend_string_init_interned_func_t zend_string_init_interned
Definition zend_string.c:31
ZEND_API zend_new_interned_string_func_t zend_new_interned_string
Definition zend_string.c:30
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_KNOWN(idx)
#define zend_string_equals_literal(str, literal)
#define zend_string_equals_ci(s1, s2)
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define zend_string_equals_literal_ci(str, c)
#define dval(x)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_STR(z, s)
#define ZVAL_NULL(z)
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define Z_PTR_P(zval_p)
#define Z_STR(zval)
Definition zend_types.h:971
#define ZSTR_HAS_CE_CACHE(s)
Definition zend_types.h:841
@ FAILURE
Definition zend_types.h:61
#define ZSTR_GET_CE_CACHE(s)
Definition zend_types.h:842
#define ZVAL_DOUBLE(z, d)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define IS_CONSTANT_AST
Definition zend_types.h:611
#define Z_TYPE(zval)
Definition zend_types.h:659
#define ZVAL_BOOL(z, b)
ZEND_API void zval_internal_ptr_dtor(zval *zval_ptr)
zend_string * name
zval * ret
new_op_array scope