/src/php-src/Zend/zend_attributes.c
Line | Count | Source (jump to first uncovered line) |
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: Benjamin Eberlei <kontakt@beberlei.de> | |
16 | | | Martin Schröder <m.schroeder2007@gmail.com> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "zend.h" |
21 | | #include "zend_API.h" |
22 | | #include "zend_attributes.h" |
23 | | #include "zend_attributes_arginfo.h" |
24 | | #include "zend_smart_str.h" |
25 | | |
26 | | ZEND_API zend_class_entry *zend_ce_attribute; |
27 | | |
28 | | static HashTable internal_attributes; |
29 | | |
30 | | void validate_attribute(zend_attribute *attr, uint32_t target, zend_class_entry *scope) |
31 | 1.25k | { |
32 | | // TODO: More proper signature validation: Too many args, incorrect arg names. |
33 | 1.25k | if (attr->argc > 0) { |
34 | 647 | zval flags; |
35 | | |
36 | 647 | if (FAILURE == zend_get_attribute_value(&flags, attr, 0, scope)) { |
37 | 0 | return; |
38 | 0 | } |
39 | | |
40 | 647 | if (Z_TYPE(flags) != IS_LONG) { |
41 | 18 | zend_error_noreturn(E_ERROR, |
42 | 18 | "Attribute::__construct(): Argument #1 ($flags) must must be of type int, %s given", |
43 | 18 | zend_zval_type_name(&flags) |
44 | 18 | ); |
45 | 18 | } |
46 | | |
47 | 629 | if (Z_LVAL(flags) & ~ZEND_ATTRIBUTE_FLAGS) { |
48 | 18 | zend_error_noreturn(E_ERROR, "Invalid attribute flags specified"); |
49 | 18 | } |
50 | | |
51 | 611 | zval_ptr_dtor(&flags); |
52 | 611 | } |
53 | 1.25k | } |
54 | | |
55 | | ZEND_METHOD(Attribute, __construct) |
56 | 18 | { |
57 | 18 | zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL; |
58 | | |
59 | 54 | ZEND_PARSE_PARAMETERS_START(0, 1) |
60 | 18 | Z_PARAM_OPTIONAL |
61 | 0 | Z_PARAM_LONG(flags) |
62 | 18 | ZEND_PARSE_PARAMETERS_END(); |
63 | | |
64 | 18 | ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), flags); |
65 | 18 | } |
66 | | |
67 | | static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset) |
68 | 0 | { |
69 | 0 | if (attributes) { |
70 | 0 | zend_attribute *attr; |
71 | |
|
72 | 0 | ZEND_HASH_FOREACH_PTR(attributes, attr) { |
73 | 0 | if (attr->offset == offset && zend_string_equals(attr->lcname, lcname)) { |
74 | 0 | return attr; |
75 | 0 | } |
76 | 0 | } ZEND_HASH_FOREACH_END(); |
77 | 0 | } |
78 | |
|
79 | 0 | return NULL; |
80 | 0 | } |
81 | | |
82 | | static zend_attribute *get_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset) |
83 | 1.14k | { |
84 | 1.14k | if (attributes) { |
85 | 1.09k | zend_attribute *attr; |
86 | | |
87 | 3.28k | ZEND_HASH_FOREACH_PTR(attributes, attr) { |
88 | 1.09k | if (attr->offset == offset && ZSTR_LEN(attr->lcname) == len) { |
89 | 1.07k | if (0 == memcmp(ZSTR_VAL(attr->lcname), str, len)) { |
90 | 1.03k | return attr; |
91 | 1.03k | } |
92 | 1.07k | } |
93 | 1.09k | } ZEND_HASH_FOREACH_END(); |
94 | 1.09k | } |
95 | | |
96 | 108 | return NULL; |
97 | 1.14k | } |
98 | | |
99 | | ZEND_API zend_attribute *zend_get_attribute(HashTable *attributes, zend_string *lcname) |
100 | 0 | { |
101 | 0 | return get_attribute(attributes, lcname, 0); |
102 | 0 | } |
103 | | |
104 | | ZEND_API zend_attribute *zend_get_attribute_str(HashTable *attributes, const char *str, size_t len) |
105 | 1.14k | { |
106 | 1.14k | return get_attribute_str(attributes, str, len, 0); |
107 | 1.14k | } |
108 | | |
109 | | ZEND_API zend_attribute *zend_get_parameter_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset) |
110 | 0 | { |
111 | 0 | return get_attribute(attributes, lcname, offset + 1); |
112 | 0 | } |
113 | | |
114 | | ZEND_API zend_attribute *zend_get_parameter_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset) |
115 | 0 | { |
116 | 0 | return get_attribute_str(attributes, str, len, offset + 1); |
117 | 0 | } |
118 | | |
119 | | ZEND_API zend_result zend_get_attribute_value(zval *ret, zend_attribute *attr, uint32_t i, zend_class_entry *scope) |
120 | 6.72k | { |
121 | 6.72k | if (i >= attr->argc) { |
122 | 0 | return FAILURE; |
123 | 0 | } |
124 | | |
125 | 6.72k | ZVAL_COPY_OR_DUP(ret, &attr->args[i].value); |
126 | | |
127 | 6.72k | if (Z_TYPE_P(ret) == IS_CONSTANT_AST) { |
128 | 3.20k | if (SUCCESS != zval_update_constant_ex(ret, scope)) { |
129 | 260 | zval_ptr_dtor(ret); |
130 | 260 | return FAILURE; |
131 | 260 | } |
132 | 6.46k | } |
133 | | |
134 | 6.46k | return SUCCESS; |
135 | 6.46k | } |
136 | | |
137 | | static const char *target_names[] = { |
138 | | "class", |
139 | | "function", |
140 | | "method", |
141 | | "property", |
142 | | "class constant", |
143 | | "parameter" |
144 | | }; |
145 | | |
146 | | ZEND_API zend_string *zend_get_attribute_target_names(uint32_t flags) |
147 | 398 | { |
148 | 398 | smart_str str = { 0 }; |
149 | | |
150 | 2.78k | for (uint32_t i = 0; i < (sizeof(target_names) / sizeof(char *)); i++) { |
151 | 2.38k | if (flags & (1 << i)) { |
152 | 523 | if (smart_str_get_len(&str)) { |
153 | 125 | smart_str_appends(&str, ", "); |
154 | 125 | } |
155 | | |
156 | 523 | smart_str_appends(&str, target_names[i]); |
157 | 523 | } |
158 | 2.38k | } |
159 | | |
160 | 398 | return smart_str_extract(&str); |
161 | 398 | } |
162 | | |
163 | | ZEND_API zend_bool zend_is_attribute_repeated(HashTable *attributes, zend_attribute *attr) |
164 | 2.56k | { |
165 | 2.56k | zend_attribute *other; |
166 | | |
167 | 8.20k | ZEND_HASH_FOREACH_PTR(attributes, other) { |
168 | 2.82k | if (other != attr && other->offset == attr->offset) { |
169 | 264 | if (zend_string_equals(other->lcname, attr->lcname)) { |
170 | 225 | return 1; |
171 | 225 | } |
172 | 264 | } |
173 | 2.82k | } ZEND_HASH_FOREACH_END(); |
174 | | |
175 | 2.33k | return 0; |
176 | 2.56k | } |
177 | | |
178 | | static zend_always_inline void free_attribute(zend_attribute *attr, bool persistent) |
179 | 17.6k | { |
180 | 17.6k | uint32_t i; |
181 | | |
182 | 17.6k | zend_string_release(attr->name); |
183 | 17.6k | zend_string_release(attr->lcname); |
184 | | |
185 | 28.0k | for (i = 0; i < attr->argc; i++) { |
186 | 10.3k | if (attr->args[i].name) { |
187 | 218 | zend_string_release(attr->args[i].name); |
188 | 218 | } |
189 | 10.3k | zval_ptr_dtor(&attr->args[i].value); |
190 | 10.3k | } |
191 | | |
192 | 17.6k | pefree(attr, persistent); |
193 | 17.6k | } |
194 | | |
195 | | static void attr_free(zval *v) |
196 | 17.6k | { |
197 | 17.6k | free_attribute((zend_attribute *) Z_PTR_P(v), 0); |
198 | 17.6k | } |
199 | | |
200 | | static void attr_pfree(zval *v) |
201 | 0 | { |
202 | 0 | free_attribute((zend_attribute *) Z_PTR_P(v), 1); |
203 | 0 | } |
204 | | |
205 | | ZEND_API zend_attribute *zend_add_attribute(HashTable **attributes, zend_bool persistent, uint32_t offset, zend_string *name, uint32_t argc) |
206 | 24.2k | { |
207 | 24.2k | if (*attributes == NULL) { |
208 | 17.5k | *attributes = pemalloc(sizeof(HashTable), persistent); |
209 | 17.5k | zend_hash_init(*attributes, 8, NULL, persistent ? attr_pfree : attr_free, persistent); |
210 | 17.5k | } |
211 | | |
212 | 24.2k | zend_attribute *attr = pemalloc(ZEND_ATTRIBUTE_SIZE(argc), persistent); |
213 | | |
214 | 24.2k | if (persistent == ((GC_FLAGS(name) & IS_STR_PERSISTENT) != 0)) { |
215 | 24.1k | attr->name = zend_string_copy(name); |
216 | 137 | } else { |
217 | 137 | attr->name = zend_string_dup(name, persistent); |
218 | 137 | } |
219 | | |
220 | 24.2k | attr->lcname = zend_string_tolower_ex(attr->name, persistent); |
221 | 24.2k | attr->offset = offset; |
222 | 24.2k | attr->argc = argc; |
223 | | |
224 | | /* Initialize arguments to avoid partial initialization in case of fatal errors. */ |
225 | 35.0k | for (uint32_t i = 0; i < argc; i++) { |
226 | 10.7k | attr->args[i].name = NULL; |
227 | 10.7k | ZVAL_UNDEF(&attr->args[i].value); |
228 | 10.7k | } |
229 | | |
230 | 24.2k | zend_hash_next_index_insert_ptr(*attributes, attr); |
231 | | |
232 | 24.2k | return attr; |
233 | 24.2k | } |
234 | | |
235 | | static void free_internal_attribute(zval *v) |
236 | 0 | { |
237 | 0 | pefree(Z_PTR_P(v), 1); |
238 | 0 | } |
239 | | |
240 | | ZEND_API zend_internal_attribute *zend_internal_attribute_register(zend_class_entry *ce, uint32_t flags) |
241 | 6.27k | { |
242 | 6.27k | zend_internal_attribute *attr; |
243 | | |
244 | 6.27k | if (ce->type != ZEND_INTERNAL_CLASS) { |
245 | 0 | zend_error_noreturn(E_ERROR, "Only internal classes can be registered as compiler attribute"); |
246 | 0 | } |
247 | | |
248 | 6.27k | attr = pemalloc(sizeof(zend_internal_attribute), 1); |
249 | 6.27k | attr->ce = ce; |
250 | 6.27k | attr->flags = flags; |
251 | 6.27k | attr->validator = NULL; |
252 | | |
253 | 6.27k | zend_string *lcname = zend_string_tolower_ex(ce->name, 1); |
254 | | |
255 | 6.27k | zend_hash_update_ptr(&internal_attributes, lcname, attr); |
256 | 6.27k | zend_add_class_attribute(ce, zend_ce_attribute->name, 0); |
257 | 6.27k | zend_string_release(lcname); |
258 | | |
259 | 6.27k | return attr; |
260 | 6.27k | } |
261 | | |
262 | | ZEND_API zend_internal_attribute *zend_internal_attribute_get(zend_string *lcname) |
263 | 17.8k | { |
264 | 17.8k | return zend_hash_find_ptr(&internal_attributes, lcname); |
265 | 17.8k | } |
266 | | |
267 | | void zend_register_attribute_ce(void) |
268 | 6.27k | { |
269 | 6.27k | zend_internal_attribute *attr; |
270 | 6.27k | zend_class_entry ce; |
271 | 6.27k | zend_string *str; |
272 | 6.27k | zval tmp; |
273 | | |
274 | 6.27k | zend_hash_init(&internal_attributes, 8, NULL, free_internal_attribute, 1); |
275 | | |
276 | 6.27k | INIT_CLASS_ENTRY(ce, "Attribute", class_Attribute_methods); |
277 | 6.27k | zend_ce_attribute = zend_register_internal_class(&ce); |
278 | 6.27k | zend_ce_attribute->ce_flags |= ZEND_ACC_FINAL; |
279 | | |
280 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_CLASS"), ZEND_ATTRIBUTE_TARGET_CLASS); |
281 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_FUNCTION"), ZEND_ATTRIBUTE_TARGET_FUNCTION); |
282 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_METHOD"), ZEND_ATTRIBUTE_TARGET_METHOD); |
283 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_PROPERTY"), ZEND_ATTRIBUTE_TARGET_PROPERTY); |
284 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_CLASS_CONSTANT"), ZEND_ATTRIBUTE_TARGET_CLASS_CONST); |
285 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_PARAMETER"), ZEND_ATTRIBUTE_TARGET_PARAMETER); |
286 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_ALL"), ZEND_ATTRIBUTE_TARGET_ALL); |
287 | 6.27k | zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("IS_REPEATABLE"), ZEND_ATTRIBUTE_IS_REPEATABLE); |
288 | | |
289 | 6.27k | ZVAL_UNDEF(&tmp); |
290 | 6.27k | str = zend_string_init(ZEND_STRL("flags"), 1); |
291 | 6.27k | zend_declare_typed_property(zend_ce_attribute, str, &tmp, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CODE(IS_LONG, 0, 0)); |
292 | 6.27k | zend_string_release(str); |
293 | | |
294 | 6.27k | attr = zend_internal_attribute_register(zend_ce_attribute, ZEND_ATTRIBUTE_TARGET_CLASS); |
295 | 6.27k | attr->validator = validate_attribute; |
296 | 6.27k | } |
297 | | |
298 | | void zend_attributes_shutdown(void) |
299 | 0 | { |
300 | 0 | zend_hash_destroy(&internal_attributes); |
301 | 0 | } |