/src/php-src/Zend/zend_attributes.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Benjamin Eberlei <kontakt@beberlei.de> | |
15 | | | Martin Schröder <m.schroeder2007@gmail.com> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | #include "zend.h" |
20 | | #include "zend_API.h" |
21 | | #include "zend_attributes.h" |
22 | | #include "zend_attributes_arginfo.h" |
23 | | #include "zend_exceptions.h" |
24 | | #include "zend_smart_str.h" |
25 | | |
26 | | ZEND_API zend_class_entry *zend_ce_attribute; |
27 | | ZEND_API zend_class_entry *zend_ce_return_type_will_change_attribute; |
28 | | ZEND_API zend_class_entry *zend_ce_allow_dynamic_properties; |
29 | | ZEND_API zend_class_entry *zend_ce_sensitive_parameter; |
30 | | ZEND_API zend_class_entry *zend_ce_sensitive_parameter_value; |
31 | | ZEND_API zend_class_entry *zend_ce_override; |
32 | | ZEND_API zend_class_entry *zend_ce_deprecated; |
33 | | ZEND_API zend_class_entry *zend_ce_nodiscard; |
34 | | ZEND_API zend_class_entry *zend_ce_delayed_target_validation; |
35 | | |
36 | | static zend_object_handlers attributes_object_handlers_sensitive_parameter_value; |
37 | | |
38 | | static HashTable internal_attributes; |
39 | | |
40 | | uint32_t zend_attribute_attribute_get_flags(const zend_attribute *attr, zend_class_entry *scope) |
41 | 166 | { |
42 | | // TODO: More proper signature validation: Too many args, incorrect arg names. |
43 | 166 | if (attr->argc > 0) { |
44 | 136 | zval flags; |
45 | | |
46 | 136 | if (FAILURE == zend_get_attribute_value(&flags, attr, 0, scope)) { |
47 | 6 | ZEND_ASSERT(EG(exception)); |
48 | 6 | return 0; |
49 | 6 | } |
50 | | |
51 | 130 | if (Z_TYPE(flags) != IS_LONG) { |
52 | 2 | zend_throw_error(NULL, |
53 | 2 | "Attribute::__construct(): Argument #1 ($flags) must be of type int, %s given", |
54 | 2 | zend_zval_value_name(&flags) |
55 | 2 | ); |
56 | 2 | zval_ptr_dtor(&flags); |
57 | 2 | return 0; |
58 | 2 | } |
59 | | |
60 | 128 | uint32_t flags_l = Z_LVAL(flags); |
61 | 128 | if (flags_l & ~ZEND_ATTRIBUTE_FLAGS) { |
62 | 2 | zend_throw_error(NULL, "Invalid attribute flags specified"); |
63 | 2 | return 0; |
64 | 2 | } |
65 | | |
66 | 126 | return flags_l; |
67 | 128 | } |
68 | | |
69 | 30 | return ZEND_ATTRIBUTE_TARGET_ALL; |
70 | 166 | } |
71 | | |
72 | | static zend_string *validate_allow_dynamic_properties( |
73 | | zend_attribute *attr, uint32_t target, zend_class_entry *scope) |
74 | 258 | { |
75 | 258 | ZEND_ASSERT(scope != NULL); |
76 | 258 | const char *msg = NULL; |
77 | 258 | if (scope->ce_flags & ZEND_ACC_TRAIT) { |
78 | 4 | msg = "Cannot apply #[\\AllowDynamicProperties] to trait %s"; |
79 | 254 | } else if (scope->ce_flags & ZEND_ACC_INTERFACE) { |
80 | 4 | msg = "Cannot apply #[\\AllowDynamicProperties] to interface %s"; |
81 | 250 | } else if (scope->ce_flags & ZEND_ACC_READONLY_CLASS) { |
82 | 4 | msg = "Cannot apply #[\\AllowDynamicProperties] to readonly class %s"; |
83 | 246 | } else if (scope->ce_flags & ZEND_ACC_ENUM) { |
84 | 4 | msg = "Cannot apply #[\\AllowDynamicProperties] to enum %s"; |
85 | 4 | } |
86 | 258 | if (msg != NULL) { |
87 | 16 | return zend_strpprintf(0, msg, ZSTR_VAL(scope->name)); |
88 | 16 | } |
89 | 242 | scope->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES; |
90 | 242 | return NULL; |
91 | 258 | } |
92 | | |
93 | | static zend_string *validate_attribute( |
94 | | zend_attribute *attr, uint32_t target, zend_class_entry *scope) |
95 | 118 | { |
96 | 118 | const char *msg = NULL; |
97 | 118 | if (scope->ce_flags & ZEND_ACC_TRAIT) { |
98 | 6 | msg = "Cannot apply #[\\Attribute] to trait %s"; |
99 | 112 | } else if (scope->ce_flags & ZEND_ACC_INTERFACE) { |
100 | 6 | msg = "Cannot apply #[\\Attribute] to interface %s"; |
101 | 106 | } else if (scope->ce_flags & ZEND_ACC_ENUM) { |
102 | 4 | msg = "Cannot apply #[\\Attribute] to enum %s"; |
103 | 102 | } else if (scope->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) { |
104 | 6 | msg = "Cannot apply #[\\Attribute] to abstract class %s"; |
105 | 6 | } |
106 | 118 | if (msg != NULL) { |
107 | 22 | return zend_strpprintf(0, msg, ZSTR_VAL(scope->name)); |
108 | 22 | } |
109 | 96 | return NULL; |
110 | 118 | } |
111 | | |
112 | | static zend_string *validate_deprecated( |
113 | | zend_attribute *attr, |
114 | | uint32_t target, |
115 | | zend_class_entry *scope |
116 | 230 | ) { |
117 | 230 | if (target != ZEND_ATTRIBUTE_TARGET_CLASS) { |
118 | | /* Being used for a method or something, validation does not apply */ |
119 | 186 | return NULL; |
120 | 186 | } |
121 | 44 | if (!(scope->ce_flags & ZEND_ACC_TRAIT)) { |
122 | 14 | const char *type = zend_get_object_type_case(scope, false); |
123 | 14 | return zend_strpprintf(0, "Cannot apply #[\\Deprecated] to %s %s", type, ZSTR_VAL(scope->name)); |
124 | 14 | } |
125 | | |
126 | 30 | scope->ce_flags |= ZEND_ACC_DEPRECATED; |
127 | 30 | return NULL; |
128 | | |
129 | 44 | } |
130 | | |
131 | | ZEND_METHOD(Attribute, __construct) |
132 | 2 | { |
133 | 2 | zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL; |
134 | | |
135 | 6 | ZEND_PARSE_PARAMETERS_START(0, 1) |
136 | 6 | Z_PARAM_OPTIONAL |
137 | 8 | Z_PARAM_LONG(flags) |
138 | 2 | ZEND_PARSE_PARAMETERS_END(); |
139 | | |
140 | 2 | ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), flags); |
141 | 2 | } |
142 | | |
143 | | ZEND_METHOD(ReturnTypeWillChange, __construct) |
144 | 0 | { |
145 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
146 | 0 | } |
147 | | |
148 | | ZEND_METHOD(AllowDynamicProperties, __construct) |
149 | 2 | { |
150 | 2 | ZEND_PARSE_PARAMETERS_NONE(); |
151 | 2 | } |
152 | | |
153 | | ZEND_METHOD(SensitiveParameter, __construct) |
154 | 0 | { |
155 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
156 | 0 | } |
157 | | |
158 | | ZEND_METHOD(SensitiveParameterValue, __construct) |
159 | 94 | { |
160 | 94 | zval *value; |
161 | | |
162 | 282 | ZEND_PARSE_PARAMETERS_START(1, 1) |
163 | 376 | Z_PARAM_ZVAL(value) |
164 | 376 | ZEND_PARSE_PARAMETERS_END(); |
165 | | |
166 | 94 | zend_update_property_ex(zend_ce_sensitive_parameter_value, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_VALUE), value); |
167 | 94 | } |
168 | | |
169 | | ZEND_METHOD(SensitiveParameterValue, getValue) |
170 | 12 | { |
171 | 12 | ZEND_PARSE_PARAMETERS_NONE(); |
172 | | |
173 | 12 | ZVAL_COPY(return_value, OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0)); |
174 | 12 | } |
175 | | |
176 | | ZEND_METHOD(SensitiveParameterValue, __debugInfo) |
177 | 0 | { |
178 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
179 | | |
180 | 0 | RETURN_EMPTY_ARRAY(); |
181 | 0 | } |
182 | | |
183 | | static HashTable *attributes_sensitive_parameter_value_get_properties_for(zend_object *zobj, zend_prop_purpose purpose) |
184 | 64 | { |
185 | 64 | return NULL; |
186 | 64 | } |
187 | | |
188 | | ZEND_METHOD(Override, __construct) |
189 | 0 | { |
190 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
191 | 0 | } |
192 | | |
193 | | ZEND_METHOD(Deprecated, __construct) |
194 | 152 | { |
195 | 152 | zend_string *message = NULL; |
196 | 152 | zend_string *since = NULL; |
197 | 152 | zval value; |
198 | | |
199 | 456 | ZEND_PARSE_PARAMETERS_START(0, 2) |
200 | 456 | Z_PARAM_OPTIONAL |
201 | 608 | Z_PARAM_STR_OR_NULL(message) |
202 | 636 | Z_PARAM_STR_OR_NULL(since) |
203 | 152 | ZEND_PARSE_PARAMETERS_END(); |
204 | | |
205 | 152 | if (message) { |
206 | 122 | ZVAL_STR(&value, message); |
207 | 122 | } else { |
208 | 30 | ZVAL_NULL(&value); |
209 | 30 | } |
210 | 152 | zend_update_property_ex(zend_ce_deprecated, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value); |
211 | | |
212 | | /* The assignment might fail due to 'readonly'. */ |
213 | 152 | if (UNEXPECTED(EG(exception))) { |
214 | 0 | RETURN_THROWS(); |
215 | 0 | } |
216 | | |
217 | 152 | if (since) { |
218 | 90 | ZVAL_STR(&value, since); |
219 | 90 | } else { |
220 | 62 | ZVAL_NULL(&value); |
221 | 62 | } |
222 | 152 | zend_update_property_ex(zend_ce_deprecated, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_SINCE), &value); |
223 | | |
224 | | /* The assignment might fail due to 'readonly'. */ |
225 | 152 | if (UNEXPECTED(EG(exception))) { |
226 | 0 | RETURN_THROWS(); |
227 | 0 | } |
228 | 152 | } |
229 | | |
230 | | static zend_string *validate_nodiscard( |
231 | | zend_attribute *attr, uint32_t target, zend_class_entry *scope) |
232 | 89 | { |
233 | 89 | ZEND_ASSERT(CG(in_compilation)); |
234 | 89 | const zend_string *prop_info_name = CG(context).active_property_info_name; |
235 | 89 | if (prop_info_name != NULL) { |
236 | | // Applied to a hook |
237 | 16 | return ZSTR_INIT_LITERAL("#[\\NoDiscard] is not supported for property hooks", 0); |
238 | 16 | } |
239 | 73 | zend_op_array *op_array = CG(active_op_array); |
240 | 73 | op_array->fn_flags |= ZEND_ACC_NODISCARD; |
241 | 73 | return NULL; |
242 | 89 | } |
243 | | |
244 | | ZEND_METHOD(NoDiscard, __construct) |
245 | 4 | { |
246 | 4 | zend_string *message = NULL; |
247 | 4 | zval value; |
248 | | |
249 | 12 | ZEND_PARSE_PARAMETERS_START(0, 1) |
250 | 12 | Z_PARAM_OPTIONAL |
251 | 16 | Z_PARAM_STR_OR_NULL(message) |
252 | 4 | ZEND_PARSE_PARAMETERS_END(); |
253 | | |
254 | 4 | if (message) { |
255 | 4 | ZVAL_STR(&value, message); |
256 | 4 | } else { |
257 | 0 | ZVAL_NULL(&value); |
258 | 0 | } |
259 | 4 | zend_update_property_ex(zend_ce_nodiscard, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value); |
260 | | |
261 | | /* The assignment might fail due to 'readonly'. */ |
262 | 4 | if (UNEXPECTED(EG(exception))) { |
263 | 0 | RETURN_THROWS(); |
264 | 0 | } |
265 | 4 | } |
266 | | |
267 | | static zend_attribute *get_attribute(const HashTable *attributes, const zend_string *lcname, uint32_t offset) |
268 | 0 | { |
269 | 0 | if (attributes) { |
270 | 0 | zend_attribute *attr; |
271 | |
|
272 | 0 | ZEND_HASH_PACKED_FOREACH_PTR(attributes, attr) { |
273 | 0 | if (attr->offset == offset && zend_string_equals(attr->lcname, lcname)) { |
274 | 0 | return attr; |
275 | 0 | } |
276 | 0 | } ZEND_HASH_FOREACH_END(); |
277 | 0 | } |
278 | | |
279 | 0 | return NULL; |
280 | 0 | } |
281 | | |
282 | | static zend_attribute *get_attribute_str(const HashTable *attributes, const char *str, size_t len, uint32_t offset) |
283 | 446k | { |
284 | 446k | if (attributes) { |
285 | 3.17k | zend_attribute *attr; |
286 | | |
287 | 13.9k | ZEND_HASH_PACKED_FOREACH_PTR(attributes, attr) { |
288 | 13.9k | if (attr->offset == offset && zend_string_equals_cstr(attr->lcname, str, len)) { |
289 | 1.13k | return attr; |
290 | 1.13k | } |
291 | 13.9k | } ZEND_HASH_FOREACH_END(); |
292 | 3.17k | } |
293 | | |
294 | 445k | return NULL; |
295 | 446k | } |
296 | | |
297 | | ZEND_API zend_attribute *zend_get_attribute(const HashTable *attributes, const zend_string *lcname) |
298 | 0 | { |
299 | 0 | return get_attribute(attributes, lcname, 0); |
300 | 0 | } |
301 | | |
302 | | ZEND_API zend_attribute *zend_get_attribute_str(const HashTable *attributes, const char *str, size_t len) |
303 | 2.98k | { |
304 | 2.98k | return get_attribute_str(attributes, str, len, 0); |
305 | 2.98k | } |
306 | | |
307 | | ZEND_API zend_attribute *zend_get_parameter_attribute(const HashTable *attributes, const zend_string *lcname, uint32_t offset) |
308 | 0 | { |
309 | 0 | return get_attribute(attributes, lcname, offset + 1); |
310 | 0 | } |
311 | | |
312 | | ZEND_API zend_attribute *zend_get_parameter_attribute_str(const HashTable *attributes, const char *str, size_t len, uint32_t offset) |
313 | 443k | { |
314 | 443k | return get_attribute_str(attributes, str, len, offset + 1); |
315 | 443k | } |
316 | | |
317 | | ZEND_API zend_result zend_get_attribute_value(zval *ret, const zend_attribute *attr, uint32_t i, zend_class_entry *scope) |
318 | 696 | { |
319 | 696 | if (i >= attr->argc) { |
320 | 0 | return FAILURE; |
321 | 0 | } |
322 | | |
323 | 696 | ZVAL_COPY_OR_DUP(ret, &attr->args[i].value); |
324 | | |
325 | 696 | if (Z_TYPE_P(ret) == IS_CONSTANT_AST) { |
326 | 180 | if (SUCCESS != zval_update_constant_ex(ret, scope)) { |
327 | 18 | zval_ptr_dtor(ret); |
328 | 18 | return FAILURE; |
329 | 18 | } |
330 | 180 | } |
331 | | |
332 | 678 | return SUCCESS; |
333 | 696 | } |
334 | | |
335 | | ZEND_API zend_result zend_get_attribute_object(zval *obj, zend_class_entry *attribute_ce, zend_attribute *attribute_data, zend_class_entry *scope, zend_string *filename) |
336 | 250 | { |
337 | 250 | zend_execute_data *call = NULL; |
338 | | |
339 | 250 | if (filename) { |
340 | | /* Set up dummy call frame that makes it look like the attribute was invoked |
341 | | * from where it occurs in the code. */ |
342 | 88 | zend_function dummy_func; |
343 | 88 | zend_op *opline; |
344 | | |
345 | 88 | memset(&dummy_func, 0, sizeof(zend_function)); |
346 | | |
347 | 88 | call = zend_vm_stack_push_call_frame_ex( |
348 | 88 | ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_execute_data), sizeof(zval)) + |
349 | 88 | ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op), sizeof(zval)) + |
350 | 88 | ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_function), sizeof(zval)), |
351 | 88 | 0, &dummy_func, 0, NULL); |
352 | | |
353 | 88 | opline = (zend_op*)(call + 1); |
354 | 88 | memset(opline, 0, sizeof(zend_op)); |
355 | 88 | opline->opcode = ZEND_DO_FCALL; |
356 | 88 | opline->lineno = attribute_data->lineno; |
357 | | |
358 | 88 | call->opline = opline; |
359 | 88 | call->call = NULL; |
360 | 88 | call->return_value = NULL; |
361 | 88 | call->func = (zend_function*)(call->opline + 1); |
362 | 88 | call->prev_execute_data = EG(current_execute_data); |
363 | | |
364 | 88 | memset(call->func, 0, sizeof(zend_function)); |
365 | 88 | call->func->type = ZEND_USER_FUNCTION; |
366 | 88 | call->func->op_array.fn_flags = |
367 | 88 | attribute_data->flags & ZEND_ATTRIBUTE_STRICT_TYPES ? ZEND_ACC_STRICT_TYPES : 0; |
368 | 88 | call->func->op_array.fn_flags |= ZEND_ACC_CALL_VIA_TRAMPOLINE; |
369 | 88 | call->func->op_array.filename = filename; |
370 | | |
371 | 88 | EG(current_execute_data) = call; |
372 | 88 | } |
373 | | |
374 | 250 | zval *args = NULL; |
375 | 250 | HashTable *named_params = NULL; |
376 | | |
377 | 250 | zend_result result = FAILURE; |
378 | | |
379 | 250 | uint32_t argc = 0; |
380 | 250 | if (attribute_data->argc) { |
381 | 206 | args = emalloc(attribute_data->argc * sizeof(zval)); |
382 | | |
383 | 472 | for (uint32_t i = 0; i < attribute_data->argc; i++) { |
384 | 274 | zval val; |
385 | 274 | if (FAILURE == zend_get_attribute_value(&val, attribute_data, i, scope)) { |
386 | 8 | result = FAILURE; |
387 | 8 | goto out; |
388 | 8 | } |
389 | 266 | if (attribute_data->args[i].name) { |
390 | 160 | if (!named_params) { |
391 | 104 | named_params = zend_new_array(0); |
392 | 104 | } |
393 | 160 | zend_hash_add_new(named_params, attribute_data->args[i].name, &val); |
394 | 160 | } else { |
395 | 106 | ZVAL_COPY_VALUE(&args[i], &val); |
396 | 106 | argc++; |
397 | 106 | } |
398 | 266 | } |
399 | 206 | } |
400 | | |
401 | 242 | result = object_init_with_constructor(obj, attribute_ce, argc, args, named_params); |
402 | | |
403 | 250 | out: |
404 | 356 | for (uint32_t i = 0; i < argc; i++) { |
405 | 106 | zval_ptr_dtor(&args[i]); |
406 | 106 | } |
407 | | |
408 | 250 | efree(args); |
409 | | |
410 | 250 | if (named_params) { |
411 | 104 | zend_array_destroy(named_params); |
412 | 104 | } |
413 | | |
414 | 250 | if (filename) { |
415 | 88 | EG(current_execute_data) = call->prev_execute_data; |
416 | 88 | zend_vm_stack_free_call_frame(call); |
417 | 88 | } |
418 | | |
419 | 250 | return result; |
420 | 242 | } |
421 | | |
422 | | static const char *target_names[] = { |
423 | | "class", |
424 | | "function", |
425 | | "method", |
426 | | "property", |
427 | | "class constant", |
428 | | "parameter", |
429 | | "constant" |
430 | | }; |
431 | | |
432 | | ZEND_API zend_string *zend_get_attribute_target_names(uint32_t flags) |
433 | 60 | { |
434 | 60 | smart_str str = { 0 }; |
435 | | |
436 | 480 | for (uint32_t i = 0; i < (sizeof(target_names) / sizeof(char *)); i++) { |
437 | 420 | if (flags & (1 << i)) { |
438 | 68 | if (smart_str_get_len(&str)) { |
439 | 8 | smart_str_appends(&str, ", "); |
440 | 8 | } |
441 | | |
442 | 68 | smart_str_appends(&str, target_names[i]); |
443 | 68 | } |
444 | 420 | } |
445 | | |
446 | 60 | return smart_str_extract(&str); |
447 | 60 | } |
448 | | |
449 | | ZEND_API bool zend_is_attribute_repeated(const HashTable *attributes, const zend_attribute *attr) |
450 | 1.24k | { |
451 | 1.24k | zend_attribute *other; |
452 | | |
453 | 6.05k | ZEND_HASH_PACKED_FOREACH_PTR(attributes, other) { |
454 | 6.05k | if (other != attr && other->offset == attr->offset) { |
455 | 482 | if (zend_string_equals(other->lcname, attr->lcname)) { |
456 | 16 | return 1; |
457 | 16 | } |
458 | 482 | } |
459 | 6.05k | } ZEND_HASH_FOREACH_END(); |
460 | | |
461 | 1.23k | return 0; |
462 | 1.24k | } |
463 | | |
464 | | static void attr_free(zval *v) |
465 | 110 | { |
466 | 110 | zend_attribute *attr = Z_PTR_P(v); |
467 | 110 | bool persistent = attr->flags & ZEND_ATTRIBUTE_PERSISTENT; |
468 | | |
469 | 110 | zend_string_release(attr->name); |
470 | 110 | zend_string_release(attr->lcname); |
471 | 110 | if (attr->validation_error != NULL) { |
472 | 14 | zend_string_release(attr->validation_error); |
473 | 14 | } |
474 | | |
475 | 118 | for (uint32_t i = 0; i < attr->argc; i++) { |
476 | 8 | if (attr->args[i].name) { |
477 | 4 | zend_string_release(attr->args[i].name); |
478 | 4 | } |
479 | 8 | if (persistent) { |
480 | 0 | zval_internal_ptr_dtor(&attr->args[i].value); |
481 | 8 | } else { |
482 | 8 | zval_ptr_dtor(&attr->args[i].value); |
483 | 8 | } |
484 | 8 | } |
485 | | |
486 | 110 | pefree(attr, persistent); |
487 | 110 | } |
488 | | |
489 | | ZEND_API zend_attribute *zend_add_attribute(HashTable **attributes, zend_string *name, uint32_t argc, uint32_t flags, uint32_t offset, uint32_t lineno) |
490 | 1.86k | { |
491 | 1.86k | bool persistent = flags & ZEND_ATTRIBUTE_PERSISTENT; |
492 | 1.86k | if (*attributes == NULL) { |
493 | 1.45k | *attributes = pemalloc(sizeof(HashTable), persistent); |
494 | 1.45k | zend_hash_init(*attributes, 8, NULL, attr_free, persistent); |
495 | 1.45k | } |
496 | | |
497 | 1.86k | zend_attribute *attr = pemalloc(ZEND_ATTRIBUTE_SIZE(argc), persistent); |
498 | | |
499 | 1.86k | if (persistent == ((GC_FLAGS(name) & IS_STR_PERSISTENT) != 0)) { |
500 | 1.86k | attr->name = zend_string_copy(name); |
501 | 1.86k | } else { |
502 | 0 | attr->name = zend_string_dup(name, persistent); |
503 | 0 | } |
504 | | |
505 | 1.86k | attr->lcname = zend_string_tolower_ex(attr->name, persistent); |
506 | 1.86k | attr->validation_error = NULL; |
507 | 1.86k | attr->flags = flags; |
508 | 1.86k | attr->lineno = lineno; |
509 | 1.86k | attr->offset = offset; |
510 | 1.86k | attr->argc = argc; |
511 | | |
512 | | /* Initialize arguments to avoid partial initialization in case of fatal errors. */ |
513 | 2.65k | for (uint32_t i = 0; i < argc; i++) { |
514 | 788 | attr->args[i].name = NULL; |
515 | 788 | ZVAL_UNDEF(&attr->args[i].value); |
516 | 788 | } |
517 | | |
518 | 1.86k | zend_hash_next_index_insert_ptr(*attributes, attr); |
519 | | |
520 | 1.86k | return attr; |
521 | 1.86k | } |
522 | | |
523 | | static void free_internal_attribute(zval *v) |
524 | 0 | { |
525 | 0 | pefree(Z_PTR_P(v), 1); |
526 | 0 | } |
527 | | |
528 | | ZEND_API zend_internal_attribute *zend_mark_internal_attribute(zend_class_entry *ce) |
529 | 16 | { |
530 | 16 | zend_internal_attribute *internal_attr; |
531 | 16 | zend_attribute *attr; |
532 | | |
533 | 16 | if (ce->type != ZEND_INTERNAL_CLASS) { |
534 | 0 | zend_error_noreturn(E_ERROR, "Only internal classes can be registered as compiler attribute"); |
535 | 0 | } |
536 | | |
537 | 48 | ZEND_HASH_FOREACH_PTR(ce->attributes, attr) { |
538 | 48 | if (zend_string_equals(attr->name, zend_ce_attribute->name)) { |
539 | 16 | internal_attr = pemalloc(sizeof(zend_internal_attribute), 1); |
540 | 16 | internal_attr->ce = ce; |
541 | 16 | internal_attr->flags = Z_LVAL(attr->args[0].value); |
542 | 16 | internal_attr->validator = NULL; |
543 | | |
544 | 16 | zend_string *lcname = zend_string_tolower_ex(ce->name, 1); |
545 | 16 | zend_hash_update_ptr(&internal_attributes, lcname, internal_attr); |
546 | 16 | zend_string_release(lcname); |
547 | | |
548 | 16 | return internal_attr; |
549 | 16 | } |
550 | 48 | } ZEND_HASH_FOREACH_END(); |
551 | | |
552 | 0 | zend_error_noreturn(E_ERROR, "Classes must be first marked as attribute before being able to be registered as internal attribute class"); |
553 | 16 | } |
554 | | |
555 | | ZEND_API zend_internal_attribute *zend_internal_attribute_register(zend_class_entry *ce, uint32_t flags) |
556 | 0 | { |
557 | 0 | zend_attribute *attr = zend_add_class_attribute(ce, zend_ce_attribute->name, 1); |
558 | 0 | ZVAL_LONG(&attr->args[0].value, flags); |
559 | |
|
560 | 0 | return zend_mark_internal_attribute(ce); |
561 | 0 | } |
562 | | |
563 | | ZEND_API zend_internal_attribute *zend_internal_attribute_get(zend_string *lcname) |
564 | 3.44k | { |
565 | 3.44k | return zend_hash_find_ptr(&internal_attributes, lcname); |
566 | 3.44k | } |
567 | | |
568 | | void zend_register_attribute_ce(void) |
569 | 2 | { |
570 | 2 | zend_internal_attribute *attr; |
571 | | |
572 | 2 | zend_hash_init(&internal_attributes, 8, NULL, free_internal_attribute, 1); |
573 | | |
574 | 2 | zend_ce_attribute = register_class_Attribute(); |
575 | 2 | attr = zend_mark_internal_attribute(zend_ce_attribute); |
576 | 2 | attr->validator = validate_attribute; |
577 | | |
578 | 2 | zend_ce_return_type_will_change_attribute = register_class_ReturnTypeWillChange(); |
579 | 2 | zend_mark_internal_attribute(zend_ce_return_type_will_change_attribute); |
580 | | |
581 | 2 | zend_ce_allow_dynamic_properties = register_class_AllowDynamicProperties(); |
582 | 2 | attr = zend_mark_internal_attribute(zend_ce_allow_dynamic_properties); |
583 | 2 | attr->validator = validate_allow_dynamic_properties; |
584 | | |
585 | 2 | zend_ce_sensitive_parameter = register_class_SensitiveParameter(); |
586 | 2 | zend_mark_internal_attribute(zend_ce_sensitive_parameter); |
587 | | |
588 | 2 | memcpy(&attributes_object_handlers_sensitive_parameter_value, &std_object_handlers, sizeof(zend_object_handlers)); |
589 | 2 | attributes_object_handlers_sensitive_parameter_value.get_properties_for = attributes_sensitive_parameter_value_get_properties_for; |
590 | | |
591 | | /* This is not an actual attribute, thus the zend_mark_internal_attribute() call is missing. */ |
592 | 2 | zend_ce_sensitive_parameter_value = register_class_SensitiveParameterValue(); |
593 | 2 | zend_ce_sensitive_parameter_value->default_object_handlers = &attributes_object_handlers_sensitive_parameter_value; |
594 | | |
595 | 2 | zend_ce_override = register_class_Override(); |
596 | 2 | zend_mark_internal_attribute(zend_ce_override); |
597 | | |
598 | 2 | zend_ce_deprecated = register_class_Deprecated(); |
599 | 2 | attr = zend_mark_internal_attribute(zend_ce_deprecated); |
600 | 2 | attr->validator = validate_deprecated; |
601 | | |
602 | 2 | zend_ce_nodiscard = register_class_NoDiscard(); |
603 | 2 | attr = zend_mark_internal_attribute(zend_ce_nodiscard); |
604 | 2 | attr->validator = validate_nodiscard; |
605 | | |
606 | 2 | zend_ce_delayed_target_validation = register_class_DelayedTargetValidation(); |
607 | 2 | attr = zend_mark_internal_attribute(zend_ce_delayed_target_validation); |
608 | 2 | } |
609 | | |
610 | | void zend_attributes_shutdown(void) |
611 | 0 | { |
612 | 0 | zend_hash_destroy(&internal_attributes); |
613 | 0 | } |