Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/AOT/HelperRuntimeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ private static function helperIndex(): array
if (null === $sourceAbs || !self::manifestFingerprintMatches($manifest, $sourceAbs)) {
continue; // stale — emitter will refresh it
}
if (!is_file($unitDir.'/unit.o') || !is_file($unitDir.'/unit.bc')) {
if (!self::unitObjectIsLinkable($unitDir) || !is_file($unitDir.'/unit.bc')) {
continue;
}
if (!isset($manifest['init_symbol']) || '' === (string) $manifest['init_symbol']) {
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public static function linkObjects(): array
$objects = [];
foreach (array_keys(self::$usedUnits) as $unitDir) {
$object = $unitDir.'/unit.o';
if (is_file($object)) {
if (self::unitObjectIsLinkable($unitDir)) {
$objects[] = $object;
}
}
Expand All @@ -1227,6 +1227,17 @@ public static function markEmitting(): void
putenv(self::ENV_EMITTING.'=1');
}

/**
* A zero-byte unit.o can exist when emit was interrupted; it must not shadow the
* committed prelinked tier or link as an empty object (undefined helper symbols, #6229).
*/
public static function unitObjectIsLinkable(string $unitDir): bool
{
$object = $unitDir.'/unit.o';

return is_file($object) && filesize($object) > 0;
}

private static function shouldInlineOnlyForUserScript(string $logicalLc): bool
{
if (!isset(self::USER_SCRIPT_INLINE_ONLY_LOGICALS[$logicalLc])) {
Expand Down
77 changes: 77 additions & 0 deletions test/unit/AOT/HelperRuntimeCacheFingerprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,83 @@ public function testEmitDiscoversValidatePathCompiledValidatePairs(): void
);
}

public function testUnitObjectIsLinkableRejectsZeroByteObject(): void
{
$root = \dirname(__DIR__, 3);
$unitDir = \sys_get_temp_dir().'/phpc_helper_unit_'.bin2hex(random_bytes(4));
mkdir($unitDir);
try {
touch($unitDir.'/unit.o');
$this->assertFalse(HelperRuntimeCache::unitObjectIsLinkable($unitDir));

$prelinked = $root.'/prelinked/helper-runtime/x86_64-linux/units/ext_standard_ArrayIsListJitHelper_php';
$this->assertFileExists($prelinked.'/unit.o');
$this->assertTrue(HelperRuntimeCache::unitObjectIsLinkable($prelinked));
} finally {
@unlink($unitDir.'/unit.o');
@rmdir($unitDir);
}
}

/**
* @runInSeparateProcess
*/
public function testHelperIndexFallsBackToPrelinkedWhenBuildCacheObjectEmpty(): void
{
$root = \dirname(__DIR__, 3);
$slug = 'ext_standard_ArrayIsListJitHelper_php';
$prelinkedDir = $root.'/prelinked/helper-runtime/x86_64-linux/units/'.$slug;
if (!is_dir($prelinkedDir) || !is_file($prelinkedDir.'/manifest.json')) {
$this->markTestSkipped('prelinked ArrayIsListJitHelper unit missing');
}
$fakeUnitsRoot = \sys_get_temp_dir().'/phpc_helper_units_'.bin2hex(random_bytes(4));
$buildDir = $fakeUnitsRoot.'/'.$slug;
mkdir($buildDir, 0775, true);
try {
copy($prelinkedDir.'/manifest.json', $buildDir.'/manifest.json');
copy($prelinkedDir.'/unit.bc', $buildDir.'/unit.bc');
touch($buildDir.'/unit.o');

$ref = new \ReflectionClass(HelperRuntimeCache::class);
$unitsProp = $ref->getProperty('helperIndex');
$unitsProp->setAccessible(true);
$unitsProp->setValue(null, null);

$scan = $ref->getMethod('helperIndex');
$scan->setAccessible(true);

// Mirror helperIndex scan: empty build unit.o must not win over prelinked.
$index = [];
foreach ([$fakeUnitsRoot, $root.'/prelinked/helper-runtime/x86_64-linux/units'] as $unitsRoot) {
foreach (glob($unitsRoot.'/*/manifest.json') ?: [] as $manifestPath) {
$unitDir = \dirname($manifestPath);
$manifest = json_decode((string) file_get_contents($manifestPath), true);
if (!\is_array($manifest) || !isset($manifest['helpers'])) {
continue;
}
if (!HelperRuntimeCache::unitObjectIsLinkable($unitDir) || !is_file($unitDir.'/unit.bc')) {
continue;
}
foreach ($manifest['helpers'] as $logical => $symbol) {
if (isset($index[$logical])) {
continue;
}
$index[$logical] = $unitDir;
}
}
}
$logical = 'phpcompiler\\ext\\standard\\arrayislistjithelper::islist';
$this->assertArrayHasKey($logical, $index);
$this->assertSame($prelinkedDir, $index[$logical]);
} finally {
@unlink($buildDir.'/unit.o');
@unlink($buildDir.'/unit.bc');
@unlink($buildDir.'/manifest.json');
@rmdir($buildDir);
@rmdir($fakeUnitsRoot);
}
}

private function fingerprintViaSubprocess(string $root, string $unitFile, string $llvmPath): string
{
$php = escapeshellarg(PHP_BINARY);
Expand Down
Loading