|
| 1 | +--TEST-- |
| 2 | +GH-20469: Child delayed variance can resolve parent before direct delayed resolution |
| 3 | +--DESCRIPTION-- |
| 4 | +This variant ensures the cacheability check after load_delayed_classes() is |
| 5 | +needed. Loading the delayed child resolves the parent class's variance |
| 6 | +obligations reentrantly, so the parent no longer has ZEND_ACC_UNRESOLVED_VARIANCE |
| 7 | +when control returns from load_delayed_classes(). The parent was still used while |
| 8 | +nearly linked, and must not be inserted into the inheritance cache. |
| 9 | +--EXTENSIONS-- |
| 10 | +opcache |
| 11 | +--CONFLICTS-- |
| 12 | +server |
| 13 | +--FILE-- |
| 14 | +<?php |
| 15 | +$dir = __DIR__ . '/gh20469_child_variance_resolves_parent'; |
| 16 | +@mkdir($dir . '/classes', 0777, true); |
| 17 | + |
| 18 | +file_put_contents($dir . '/autoload.php', <<<'PHP' |
| 19 | +<?php |
| 20 | +spl_autoload_register(function ($class) { |
| 21 | + $prefix = 'APP\\'; |
| 22 | + if (strncmp($class, $prefix, strlen($prefix)) === 0) { |
| 23 | + require __DIR__ . '/classes/' . substr($class, strlen($prefix)) . '.php'; |
| 24 | + } |
| 25 | +}); |
| 26 | +PHP); |
| 27 | + |
| 28 | +/* The dependency cycle is: |
| 29 | + * ParentBeingLinked -> CovariantReturnWithTrait -> RequiresRootReturnTrait |
| 30 | + * -> ChildOfParentBeingLinked -> ParentBeingLinked. |
| 31 | + * |
| 32 | + * ChildOfParentBeingLinked also has delayed variance, so resolving the child's |
| 33 | + * dependency on ParentBeingLinked can resolve ParentBeingLinked before it |
| 34 | + * reaches its direct resolve_delayed_variance_obligations() call. |
| 35 | + */ |
| 36 | +file_put_contents($dir . '/test1.php', <<<'PHP' |
| 37 | +<?php |
| 38 | +require __DIR__ . '/autoload.php'; |
| 39 | +// Prime the inheritance cache with the full dependency cycle. |
| 40 | +echo \APP\ChildOfParentBeingLinked::SOME_CONSTANT; |
| 41 | +PHP); |
| 42 | + |
| 43 | +file_put_contents($dir . '/test2.php', <<<'PHP' |
| 44 | +<?php |
| 45 | +require __DIR__ . '/autoload.php'; |
| 46 | +// Link ParentBeingLinked first. During load_delayed_classes(), loading the |
| 47 | +// delayed child resolves ParentBeingLinked's variance obligations reentrantly. |
| 48 | +echo \APP\ParentBeingLinked::SOME_CONSTANT; |
| 49 | +$i = new \APP\ChildOfParentBeingLinked(); |
| 50 | +var_dump($i->test()); |
| 51 | +PHP); |
| 52 | + |
| 53 | +file_put_contents($dir . '/test3.php', <<<'PHP' |
| 54 | +<?php |
| 55 | +require __DIR__ . '/autoload.php'; |
| 56 | +// Replay the cache state created by test2. If ParentBeingLinked was cached even |
| 57 | +// though it was used while nearly linked, this request fails before test() runs. |
| 58 | +echo \APP\ParentBeingLinked::SOME_CONSTANT; |
| 59 | +$i = new \APP\ChildOfParentBeingLinked(); |
| 60 | +var_dump($i->test()); |
| 61 | +PHP); |
| 62 | + |
| 63 | +file_put_contents($dir . '/classes/RootForTraitReturn.php', <<<'PHP' |
| 64 | +<?php |
| 65 | +namespace APP; |
| 66 | +
|
| 67 | +class RootForTraitReturn |
| 68 | +{ |
| 69 | + function createResult(): BaseCovariantReturn |
| 70 | + { |
| 71 | + } |
| 72 | +
|
| 73 | + function test() {} |
| 74 | +} |
| 75 | +PHP); |
| 76 | + |
| 77 | +file_put_contents($dir . '/classes/ParentBeingLinked.php', <<<'PHP' |
| 78 | +<?php |
| 79 | +namespace APP; |
| 80 | +
|
| 81 | +class ParentBeingLinked extends RootForTraitReturn |
| 82 | +{ |
| 83 | + public const SOME_CONSTANT = 3; |
| 84 | +
|
| 85 | + // CovariantReturnWithTrait is unavailable when this method is checked, |
| 86 | + // putting ParentBeingLinked into delayed variance resolution. |
| 87 | + function createResult(): CovariantReturnWithTrait |
| 88 | + { |
| 89 | + } |
| 90 | +} |
| 91 | +PHP); |
| 92 | + |
| 93 | +file_put_contents($dir . '/classes/ChildOfParentBeingLinked.php', <<<'PHP' |
| 94 | +<?php |
| 95 | +namespace APP; |
| 96 | +
|
| 97 | +class ChildOfParentBeingLinked extends ParentBeingLinked |
| 98 | +{ |
| 99 | + // MoreSpecificReturn is also unavailable when the child is linked. Resolving |
| 100 | + // this child's delayed variance obligation recursively resolves the parent. |
| 101 | + function createResult(): MoreSpecificReturn |
| 102 | + { |
| 103 | + } |
| 104 | +} |
| 105 | +PHP); |
| 106 | + |
| 107 | +file_put_contents($dir . '/classes/BaseCovariantReturn.php', <<<'PHP' |
| 108 | +<?php |
| 109 | +namespace APP; |
| 110 | +
|
| 111 | +abstract class BaseCovariantReturn |
| 112 | +{ |
| 113 | +} |
| 114 | +PHP); |
| 115 | + |
| 116 | +file_put_contents($dir . '/classes/RequiresRootReturnTrait.php', <<<'PHP' |
| 117 | +<?php |
| 118 | +namespace APP; |
| 119 | +
|
| 120 | +trait RequiresRootReturnTrait |
| 121 | +{ |
| 122 | + abstract function build(): RootForTraitReturn; |
| 123 | +} |
| 124 | +PHP); |
| 125 | + |
| 126 | +file_put_contents($dir . '/classes/CovariantReturnWithTrait.php', <<<'PHP' |
| 127 | +<?php |
| 128 | +namespace APP; |
| 129 | +
|
| 130 | +class CovariantReturnWithTrait extends BaseCovariantReturn |
| 131 | +{ |
| 132 | + use RequiresRootReturnTrait; |
| 133 | +
|
| 134 | + // This pulls ChildOfParentBeingLinked into the delayed autoload queue while |
| 135 | + // ParentBeingLinked is nearly linked. |
| 136 | + function build(): ChildOfParentBeingLinked |
| 137 | + { |
| 138 | + } |
| 139 | +} |
| 140 | +PHP); |
| 141 | + |
| 142 | +file_put_contents($dir . '/classes/MoreSpecificReturn.php', <<<'PHP' |
| 143 | +<?php |
| 144 | +namespace APP; |
| 145 | +
|
| 146 | +class MoreSpecificReturn extends CovariantReturnWithTrait |
| 147 | +{ |
| 148 | +} |
| 149 | +PHP); |
| 150 | + |
| 151 | +include 'php_cli_server.inc'; |
| 152 | +$ini = trim((string) getenv('TEST_PHP_EXTRA_ARGS')); |
| 153 | +$ini .= ($ini !== '' ? ' ' : '') . '-d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.file_update_protection=0'; |
| 154 | +php_cli_server_start($ini); |
| 155 | + |
| 156 | +echo rtrim(file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/gh20469_child_variance_resolves_parent/test1.php'), "\n"), "\n"; |
| 157 | +echo rtrim(file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/gh20469_child_variance_resolves_parent/test2.php'), "\n"), "\n"; |
| 158 | +echo rtrim(file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/gh20469_child_variance_resolves_parent/test3.php'), "\n"), "\n"; |
| 159 | +?> |
| 160 | +--CLEAN-- |
| 161 | +<?php |
| 162 | +$dir = __DIR__ . '/gh20469_child_variance_resolves_parent'; |
| 163 | +if (is_dir($dir)) { |
| 164 | + $iterator = new RecursiveIteratorIterator( |
| 165 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
| 166 | + RecursiveIteratorIterator::CHILD_FIRST |
| 167 | + ); |
| 168 | + foreach ($iterator as $file) { |
| 169 | + if ($file->isDir()) { |
| 170 | + rmdir($file->getPathname()); |
| 171 | + } else { |
| 172 | + unlink($file->getPathname()); |
| 173 | + } |
| 174 | + } |
| 175 | + rmdir($dir); |
| 176 | +} |
| 177 | +?> |
| 178 | +--EXPECT-- |
| 179 | +3 |
| 180 | +3NULL |
| 181 | +3NULL |
0 commit comments