Skip to content

Commit 26253b3

Browse files
Add regression tests for interfaces added via attribute validators
1 parent b0c7865 commit 26253b3

6 files changed

Lines changed: 185 additions & 1 deletion

File tree

ext/zend_test/test.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "zend_attributes.h"
3131
#include "zend_enum.h"
3232
#include "zend_interfaces.h"
33+
#include "zend_inheritance.h"
3334
#include "zend_weakrefs.h"
3435
#include "Zend/Optimizer/zend_optimizer.h"
3536
#include "Zend/zend_alloc.h"
@@ -61,6 +62,7 @@ static zend_class_entry *zend_test_attribute;
6162
static zend_class_entry *zend_test_repeatable_attribute;
6263
static zend_class_entry *zend_test_parameter_attribute;
6364
static zend_class_entry *zend_test_property_attribute;
65+
static zend_class_entry *zend_test_attribute_add_interface;
6466
static zend_class_entry *zend_test_attribute_with_arguments;
6567
static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
6668
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
@@ -913,6 +915,95 @@ void zend_attribute_validate_zendtestattribute(zend_attribute *attr, uint32_t ta
913915
}
914916
}
915917

918+
/**
919+
* Recursive handler to check if a class implements the custom casting
920+
* interface, either directly or via inheritance.
921+
*
922+
* Technically this shouldn't be needed since attribute validators run before
923+
* interfaces are added and classes are linked, but better safe than sorry
924+
*/
925+
static bool check_class_has_interface(zend_class_entry *scope) {
926+
// Might be *linked* (so already have class entries) but not *resolved*,
927+
// since that waits until the inherited parent class is resolved
928+
if (scope->ce_flags & ZEND_ACC_LINKED) {
929+
for (uint32_t iii = 0; iii < scope->num_interfaces; iii++) {
930+
if (scope->interfaces[iii] == zend_test_interface) {
931+
return true;
932+
}
933+
}
934+
} else {
935+
for (uint32_t iii = 0; iii < scope->num_interfaces; iii++) {
936+
if (zend_string_equals_literal(
937+
scope->interface_names[iii].lc_name,
938+
"_zendtestinterface"
939+
)) {
940+
// Interface was added manually be the developer
941+
return true;
942+
}
943+
}
944+
}
945+
zend_class_entry *parent = NULL;
946+
if (scope->ce_flags & ZEND_ACC_LINKED) {
947+
if (scope->parent == NULL) {
948+
return false;
949+
}
950+
parent = scope->parent;
951+
} else if (scope->parent_name == NULL) {
952+
return false;
953+
} else {
954+
parent = zend_lookup_class_ex(
955+
scope->parent_name,
956+
NULL,
957+
ZEND_FETCH_CLASS_ALLOW_UNLINKED
958+
);
959+
}
960+
if (parent == NULL) {
961+
// Invalid class to extend? Leave that up to normal PHP to deal with
962+
return false;
963+
}
964+
return check_class_has_interface(parent);
965+
}
966+
967+
void zend_attribute_validate_add_interface(zend_attribute *attr, uint32_t target, zend_class_entry *scope)
968+
{
969+
if (target != ZEND_ATTRIBUTE_TARGET_CLASS) {
970+
return;
971+
}
972+
if (scope->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) {
973+
zend_error_noreturn(E_ERROR, "Only classes can be marked with #[ZendTestAttributeAddsInterface]");
974+
}
975+
if (check_class_has_interface(scope)) {
976+
return;
977+
}
978+
if (scope->ce_flags & ZEND_ACC_LINKED) {
979+
// There is already a method to add interfaces
980+
zend_do_implement_interface(scope, zend_test_interface);
981+
return;
982+
}
983+
984+
// Add the interface automatically to the list
985+
const uint32_t interfaceIdx = scope->num_interfaces;
986+
scope->num_interfaces++;
987+
988+
zend_class_name *newInterfaceSet = safe_erealloc(
989+
scope->interface_names,
990+
scope->num_interfaces,
991+
sizeof(*newInterfaceSet),
992+
0
993+
);
994+
newInterfaceSet[interfaceIdx].name = zend_string_init(
995+
"_ZendTestInterface",
996+
strlen("_ZendTestInterface"),
997+
0
998+
);
999+
newInterfaceSet[interfaceIdx].lc_name = zend_string_init(
1000+
"_zendtestinterface",
1001+
strlen("_zendtestinterface"),
1002+
0
1003+
);
1004+
scope->interface_names = newInterfaceSet;
1005+
}
1006+
9161007
static ZEND_METHOD(_ZendTestClass, __toString)
9171008
{
9181009
ZEND_PARSE_PARAMETERS_NONE();
@@ -1296,6 +1387,12 @@ PHP_MINIT_FUNCTION(zend_test)
12961387
zend_test_property_attribute = register_class_ZendTestPropertyAttribute();
12971388
zend_mark_internal_attribute(zend_test_property_attribute);
12981389

1390+
zend_test_attribute_add_interface = register_class_ZendTestAttributeAddsInterface();
1391+
{
1392+
zend_internal_attribute *attr = zend_mark_internal_attribute(zend_test_attribute_add_interface);
1393+
attr->validator = zend_attribute_validate_add_interface;
1394+
}
1395+
12991396
zend_test_attribute_with_arguments = register_class_ZendTestAttributeWithArguments();
13001397
zend_mark_internal_attribute(zend_test_attribute_with_arguments);
13011398

ext/zend_test/test.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ final class ZendTestPropertyAttribute {
154154
public function __construct(string $parameter) {}
155155
}
156156

157+
#[Attribute(Attribute::TARGET_CLASS)]
158+
final class ZendTestAttributeAddsInterface {}
159+
157160
class ZendTestClassWithMethodWithParameterAttribute {
158161
final public function no_override(
159162
#[ZendTestParameterAttribute("value2")]

ext/zend_test/test_arginfo.h

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Verify that #[ZendTestAttributeAddsInterface] adding an interface doesn't leak (no manual implements)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
#[ZendTestAttributeAddsInterface]
9+
class Demo {}
10+
11+
var_dump(class_implements(Demo::class));
12+
13+
?>
14+
--EXPECT--
15+
array(1) {
16+
["_ZendTestInterface"]=>
17+
string(18) "_ZendTestInterface"
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Verify that #[ZendTestAttributeAddsInterface] adding an interface doesn't leak (manually implement same interface)
3+
--XFAIL--
4+
Currently leaks
5+
--EXTENSIONS--
6+
zend_test
7+
--FILE--
8+
<?php
9+
10+
#[ZendTestAttributeAddsInterface]
11+
class Demo implements _ZendTestInterface {}
12+
13+
var_dump(class_implements(Demo::class));
14+
15+
?>
16+
--EXPECT--
17+
array(1) {
18+
["_ZendTestInterface"]=>
19+
string(18) "_ZendTestInterface"
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Verify that #[ZendTestAttributeAddsInterface] adding an interface doesn't leak (manually implement different interface)
3+
--XFAIL--
4+
Currently leaks and overwrites the interface added by the attribute
5+
--EXTENSIONS--
6+
zend_test
7+
--FILE--
8+
<?php
9+
10+
interface MyInterface {}
11+
12+
#[ZendTestAttributeAddsInterface]
13+
class Demo implements MyInterface {}
14+
15+
var_dump(class_implements(Demo::class));
16+
17+
?>
18+
--EXPECT--
19+
array(2) {
20+
["_ZendTestInterface"]=>
21+
string(18) "_ZendTestInterface"
22+
["MyInterface"]=>
23+
string(11) "MyInterface"
24+
}

0 commit comments

Comments
 (0)