Skip to content

Commit 070012d

Browse files
TimWollabwoebi
authored andcommitted
Add tests for function parameter attributes to ext/zend_test
These tests verify the correct workings of the previous fixes: - Parameter attributes for native functions should not leak memory. - Parameter attributes for native functions should behave as expected.
1 parent a44d99f commit 070012d

File tree

5 files changed

+444
-1
lines changed

5 files changed

+444
-1
lines changed

ext/zend_test/test.c

+113
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ static zend_class_entry *zend_test_class;
3737
static zend_class_entry *zend_test_child_class;
3838
static zend_class_entry *zend_test_trait;
3939
static zend_class_entry *zend_test_attribute;
40+
static zend_class_entry *zend_test_parameter_attribute;
41+
static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
42+
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
4043
static zend_class_entry *zend_test_ns_foo_class;
4144
static zend_class_entry *zend_test_ns2_foo_class;
4245
static zend_class_entry *zend_test_ns2_ns_foo_class;
@@ -278,6 +281,17 @@ static ZEND_FUNCTION(namespaced_func)
278281
RETURN_TRUE;
279282
}
280283

284+
static ZEND_FUNCTION(zend_test_parameter_with_attribute)
285+
{
286+
zend_string *parameter;
287+
288+
ZEND_PARSE_PARAMETERS_START(1, 1)
289+
Z_PARAM_STR(parameter)
290+
ZEND_PARSE_PARAMETERS_END();
291+
292+
RETURN_LONG(1);
293+
}
294+
281295
static zend_object *zend_test_class_new(zend_class_entry *class_type)
282296
{
283297
zend_object *obj = zend_objects_new(class_type);
@@ -390,6 +404,50 @@ static ZEND_METHOD(ZendTestNS2_ZendSubNS_Foo, method)
390404
ZEND_PARSE_PARAMETERS_NONE();
391405
}
392406

407+
static ZEND_METHOD(ZendTestParameterAttribute, __construct)
408+
{
409+
zend_string *parameter;
410+
411+
ZEND_PARSE_PARAMETERS_START(1, 1)
412+
Z_PARAM_STR(parameter)
413+
ZEND_PARSE_PARAMETERS_END();
414+
415+
ZVAL_STR_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), parameter);
416+
}
417+
418+
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, no_override)
419+
{
420+
zend_string *parameter;
421+
422+
ZEND_PARSE_PARAMETERS_START(1, 1)
423+
Z_PARAM_STR(parameter)
424+
ZEND_PARSE_PARAMETERS_END();
425+
426+
RETURN_LONG(2);
427+
}
428+
429+
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, override)
430+
{
431+
zend_string *parameter;
432+
433+
ZEND_PARSE_PARAMETERS_START(1, 1)
434+
Z_PARAM_STR(parameter)
435+
ZEND_PARSE_PARAMETERS_END();
436+
437+
RETURN_LONG(3);
438+
}
439+
440+
static ZEND_METHOD(ZendTestChildClassWithMethodWithParameterAttribute, override)
441+
{
442+
zend_string *parameter;
443+
444+
ZEND_PARSE_PARAMETERS_START(1, 1)
445+
Z_PARAM_STR(parameter)
446+
ZEND_PARSE_PARAMETERS_END();
447+
448+
RETURN_LONG(4);
449+
}
450+
393451
PHP_INI_BEGIN()
394452
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
395453
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
@@ -425,6 +483,61 @@ PHP_MINIT_FUNCTION(zend_test)
425483
attr->validator = zend_attribute_validate_zendtestattribute;
426484
}
427485

486+
zend_test_parameter_attribute = register_class_ZendTestParameterAttribute();
487+
zend_internal_attribute_register(zend_test_parameter_attribute, ZEND_ATTRIBUTE_TARGET_PARAMETER);
488+
489+
{
490+
zend_attribute *attr;
491+
492+
attr = zend_add_parameter_attribute(
493+
zend_hash_str_find_ptr(CG(function_table), "zend_test_parameter_with_attribute", sizeof("zend_test_parameter_with_attribute") - 1),
494+
0,
495+
zend_test_parameter_attribute->name,
496+
1
497+
);
498+
499+
ZVAL_PSTRING(&attr->args[0].value, "value1");
500+
}
501+
502+
zend_test_class_with_method_with_parameter_attribute = register_class_ZendTestClassWithMethodWithParameterAttribute();
503+
504+
{
505+
zend_attribute *attr;
506+
507+
attr = zend_add_parameter_attribute(
508+
zend_hash_str_find_ptr(&zend_test_class_with_method_with_parameter_attribute->function_table, "no_override", sizeof("no_override") - 1),
509+
0,
510+
zend_test_parameter_attribute->name,
511+
1
512+
);
513+
514+
ZVAL_PSTRING(&attr->args[0].value, "value2");
515+
516+
attr = zend_add_parameter_attribute(
517+
zend_hash_str_find_ptr(&zend_test_class_with_method_with_parameter_attribute->function_table, "override", sizeof("override") - 1),
518+
0,
519+
zend_test_parameter_attribute->name,
520+
1
521+
);
522+
523+
ZVAL_PSTRING(&attr->args[0].value, "value3");
524+
}
525+
526+
zend_test_child_class_with_method_with_parameter_attribute = register_class_ZendTestChildClassWithMethodWithParameterAttribute(zend_test_class_with_method_with_parameter_attribute);
527+
528+
{
529+
zend_attribute *attr;
530+
531+
attr = zend_add_parameter_attribute(
532+
zend_hash_str_find_ptr(&zend_test_child_class_with_method_with_parameter_attribute->function_table, "override", sizeof("override") - 1),
533+
0,
534+
zend_test_parameter_attribute->name,
535+
1
536+
);
537+
538+
ZVAL_PSTRING(&attr->args[0].value, "value4");
539+
}
540+
428541
zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
429542
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
430543
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();

ext/zend_test/test.stub.php

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ final class ZendTestAttribute {
4848

4949
}
5050

51+
final class ZendTestParameterAttribute {
52+
public string $parameter;
53+
54+
public function __construct(string $parameter) {}
55+
}
56+
57+
class ZendTestClassWithMethodWithParameterAttribute {
58+
final public function no_override(string $parameter): int {}
59+
public function override(string $parameter): int {}
60+
}
61+
62+
class ZendTestChildClassWithMethodWithParameterAttribute extends ZendTestClassWithMethodWithParameterAttribute {
63+
public function override(string $parameter): int {}
64+
}
65+
5166
enum ZendTestUnitEnum {
5267
case Foo;
5368
case Bar;
@@ -93,6 +108,8 @@ function zend_weakmap_remove(object $object): bool {}
93108
function zend_weakmap_dump(): array {}
94109

95110
function zend_get_unit_enum(): ZendTestUnitEnum {}
111+
112+
function zend_test_parameter_with_attribute(#[ZendTestParameterAttribute] string $parameter): int {}
96113
}
97114

98115
namespace ZendTestNS {

ext/zend_test/test_arginfo.h

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 0af85c5349efaff6f19e003712d9ba929adc9ce1 */
2+
* Stub hash: 57eb52664b0a7b3dd2a38294e9a936d248274a14 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
55
ZEND_END_ARG_INFO()
@@ -65,6 +65,10 @@ ZEND_END_ARG_INFO()
6565
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_zend_get_unit_enum, 0, 0, ZendTestUnitEnum, 0)
6666
ZEND_END_ARG_INFO()
6767

68+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_parameter_with_attribute, 0, 1, IS_LONG, 0)
69+
ZEND_ARG_TYPE_INFO(0, parameter, IS_STRING, 0)
70+
ZEND_END_ARG_INFO()
71+
6872
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ZendTestNS2_ZendSubNS_namespaced_func, 0, 0, _IS_BOOL, 0)
6973
ZEND_END_ARG_INFO()
7074

@@ -85,6 +89,16 @@ ZEND_END_ARG_INFO()
8589

8690
#define arginfo_class__ZendTestTrait_testMethod arginfo_ZendTestNS2_ZendSubNS_namespaced_func
8791

92+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZendTestParameterAttribute___construct, 0, 0, 1)
93+
ZEND_ARG_TYPE_INFO(0, parameter, IS_STRING, 0)
94+
ZEND_END_ARG_INFO()
95+
96+
#define arginfo_class_ZendTestClassWithMethodWithParameterAttribute_no_override arginfo_zend_test_parameter_with_attribute
97+
98+
#define arginfo_class_ZendTestClassWithMethodWithParameterAttribute_override arginfo_zend_test_parameter_with_attribute
99+
100+
#define arginfo_class_ZendTestChildClassWithMethodWithParameterAttribute_override arginfo_zend_test_parameter_with_attribute
101+
88102
#define arginfo_class_ZendTestNS_Foo_method arginfo_zend_test_void_return
89103

90104
#define arginfo_class_ZendTestNS2_Foo_method arginfo_zend_test_void_return
@@ -109,13 +123,18 @@ static ZEND_FUNCTION(zend_weakmap_attach);
109123
static ZEND_FUNCTION(zend_weakmap_remove);
110124
static ZEND_FUNCTION(zend_weakmap_dump);
111125
static ZEND_FUNCTION(zend_get_unit_enum);
126+
static ZEND_FUNCTION(zend_test_parameter_with_attribute);
112127
static ZEND_FUNCTION(namespaced_func);
113128
static ZEND_METHOD(_ZendTestClass, is_object);
114129
static ZEND_METHOD(_ZendTestClass, __toString);
115130
static ZEND_METHOD(_ZendTestClass, returnsStatic);
116131
static ZEND_METHOD(_ZendTestClass, returnsThrowable);
117132
static ZEND_METHOD(_ZendTestChildClass, returnsThrowable);
118133
static ZEND_METHOD(_ZendTestTrait, testMethod);
134+
static ZEND_METHOD(ZendTestParameterAttribute, __construct);
135+
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, no_override);
136+
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, override);
137+
static ZEND_METHOD(ZendTestChildClassWithMethodWithParameterAttribute, override);
119138
static ZEND_METHOD(ZendTestNS_Foo, method);
120139
static ZEND_METHOD(ZendTestNS2_Foo, method);
121140
static ZEND_METHOD(ZendTestNS2_ZendSubNS_Foo, method);
@@ -139,6 +158,7 @@ static const zend_function_entry ext_functions[] = {
139158
ZEND_FE(zend_weakmap_remove, arginfo_zend_weakmap_remove)
140159
ZEND_FE(zend_weakmap_dump, arginfo_zend_weakmap_dump)
141160
ZEND_FE(zend_get_unit_enum, arginfo_zend_get_unit_enum)
161+
ZEND_FE(zend_test_parameter_with_attribute, arginfo_zend_test_parameter_with_attribute)
142162
ZEND_NS_FE("ZendTestNS2\\ZendSubNS", namespaced_func, arginfo_ZendTestNS2_ZendSubNS_namespaced_func)
143163
ZEND_FE_END
144164
};
@@ -175,6 +195,25 @@ static const zend_function_entry class_ZendTestAttribute_methods[] = {
175195
};
176196

177197

198+
static const zend_function_entry class_ZendTestParameterAttribute_methods[] = {
199+
ZEND_ME(ZendTestParameterAttribute, __construct, arginfo_class_ZendTestParameterAttribute___construct, ZEND_ACC_PUBLIC)
200+
ZEND_FE_END
201+
};
202+
203+
204+
static const zend_function_entry class_ZendTestClassWithMethodWithParameterAttribute_methods[] = {
205+
ZEND_ME(ZendTestClassWithMethodWithParameterAttribute, no_override, arginfo_class_ZendTestClassWithMethodWithParameterAttribute_no_override, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
206+
ZEND_ME(ZendTestClassWithMethodWithParameterAttribute, override, arginfo_class_ZendTestClassWithMethodWithParameterAttribute_override, ZEND_ACC_PUBLIC)
207+
ZEND_FE_END
208+
};
209+
210+
211+
static const zend_function_entry class_ZendTestChildClassWithMethodWithParameterAttribute_methods[] = {
212+
ZEND_ME(ZendTestChildClassWithMethodWithParameterAttribute, override, arginfo_class_ZendTestChildClassWithMethodWithParameterAttribute_override, ZEND_ACC_PUBLIC)
213+
ZEND_FE_END
214+
};
215+
216+
178217
static const zend_function_entry class_ZendTestUnitEnum_methods[] = {
179218
ZEND_FE_END
180219
};
@@ -306,6 +345,43 @@ static zend_class_entry *register_class_ZendTestAttribute(void)
306345
return class_entry;
307346
}
308347

348+
static zend_class_entry *register_class_ZendTestParameterAttribute(void)
349+
{
350+
zend_class_entry ce, *class_entry;
351+
352+
INIT_CLASS_ENTRY(ce, "ZendTestParameterAttribute", class_ZendTestParameterAttribute_methods);
353+
class_entry = zend_register_internal_class_ex(&ce, NULL);
354+
class_entry->ce_flags |= ZEND_ACC_FINAL;
355+
356+
zval property_parameter_default_value;
357+
ZVAL_UNDEF(&property_parameter_default_value);
358+
zend_string *property_parameter_name = zend_string_init("parameter", sizeof("parameter") - 1, 1);
359+
zend_declare_typed_property(class_entry, property_parameter_name, &property_parameter_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
360+
zend_string_release(property_parameter_name);
361+
362+
return class_entry;
363+
}
364+
365+
static zend_class_entry *register_class_ZendTestClassWithMethodWithParameterAttribute(void)
366+
{
367+
zend_class_entry ce, *class_entry;
368+
369+
INIT_CLASS_ENTRY(ce, "ZendTestClassWithMethodWithParameterAttribute", class_ZendTestClassWithMethodWithParameterAttribute_methods);
370+
class_entry = zend_register_internal_class_ex(&ce, NULL);
371+
372+
return class_entry;
373+
}
374+
375+
static zend_class_entry *register_class_ZendTestChildClassWithMethodWithParameterAttribute(zend_class_entry *class_entry_ZendTestClassWithMethodWithParameterAttribute)
376+
{
377+
zend_class_entry ce, *class_entry;
378+
379+
INIT_CLASS_ENTRY(ce, "ZendTestChildClassWithMethodWithParameterAttribute", class_ZendTestChildClassWithMethodWithParameterAttribute_methods);
380+
class_entry = zend_register_internal_class_ex(&ce, class_entry_ZendTestClassWithMethodWithParameterAttribute);
381+
382+
return class_entry;
383+
}
384+
309385
static zend_class_entry *register_class_ZendTestUnitEnum(void)
310386
{
311387
zend_class_entry *class_entry = zend_register_internal_enum("ZendTestUnitEnum", IS_UNDEF, class_ZendTestUnitEnum_methods);

0 commit comments

Comments
 (0)