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
56 changes: 51 additions & 5 deletions ext/standard/VmFsGlob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace PHPCompiler\ext\standard;

/**
* glob() for VM — libc glob(3) via FFI, matching __phpc_glob_vec (issue #4859, #1153).
* glob() for VM — libc glob(3) via FFI, host glob(), or PHP fallback (#4859, #7314).
*
* php-src: ext/standard/dir.c — PHP_FUNCTION(glob)
* JIT/AOT: __phpc_glob_vec via StringFsGlobVecJit.php
*/
final class VmFsGlob
{
Expand All @@ -23,20 +24,65 @@ public static function glob(string $pattern, int $flags = 0)
$onlyDir = 0 !== ($flags & StdlibConstants::GLOB_ONLYDIR);
$libcFlags = $flags & StdlibConstants::GLOB_AVAILABLE_FLAGS & ~StdlibConstants::GLOB_ONLYDIR;

$libc = self::libcGlob($pattern, $libcFlags, $onlyDir);
if (null !== $libc) {
return $libc;
if (self::ffiEnabled()) {
$libc = self::libcGlob($pattern, $libcFlags, $onlyDir);
if (null !== $libc) {
return $libc;
}
}

$host = self::hostGlob($pattern, $flags, $onlyDir, $libcFlags);
if (null !== $host) {
return $host;
}

return self::globFallback($pattern, $libcFlags, $onlyDir);
}

private static function ffiEnabled(): bool
{
$v = getenv('PHP_COMPILER_DISABLE_FFI');
if (false !== $v && '' !== $v && '0' !== $v && 'false' !== strtolower($v)) {
return false;
}

return true;
}

/**
* Host Zend glob() when VM driver runs under php-src (no ext/ffi required).
*
* @return list<string>|false|null null when host glob unavailable
*/
private static function hostGlob(string $pattern, int $flags, bool $onlyDir, int $libcFlags): array|false|null
{
if (!\function_exists('glob')) {
return null;
}
$hostFlags = $libcFlags | ($onlyDir ? StdlibConstants::GLOB_ONLYDIR : 0);
$matches = @\glob($pattern, $hostFlags);
if (false === $matches) {
return false;
}
if (!$onlyDir) {
return $matches;
}
$filtered = [];
foreach ($matches as $path) {
if (self::pathIsDir($path)) {
$filtered[] = $path;
}
}

return $filtered;
}

/**
* @return list<string>|false|null null when FFI/libc path unavailable (use fallback)
*/
private static function libcGlob(string $pattern, int $libcFlags, bool $onlyDir): array|false|null
{
if (!\extension_loaded('ffi')) {
if (!self::ffiEnabled() || !\extension_loaded('ffi')) {
return null;
}
try {
Expand Down
26 changes: 22 additions & 4 deletions ext/standard/VmFsUnlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace PHPCompiler\ext\standard;

/**
* unlink(2) for VM without calling host PHP unlink() (bootstrap #5063, php-src filestat.c).
* unlink(2) for VM — libc FFI when allowed; host unlink() otherwise (#5063, #7314).
*
* php-src: ext/standard/filestat.c — php_unlink
* JIT/AOT: ext/standard/JitUnlink.php calls libc unlink(2) directly.
*/
final class VmFsUnlink
{
Expand All @@ -16,12 +19,27 @@ public static function unlink(string $path): bool
if (str_contains($path, "\0")) {
return false;
}
$ffi = self::ffi();
if (null === $ffi) {
if (self::ffiEnabled()) {
$ffi = self::ffi();
if (null !== $ffi) {
return 0 === (int) $ffi->unlink($path);
}
}
if (\function_exists('unlink')) {
return @\unlink($path);
}

return false;
}

private static function ffiEnabled(): bool
{
$v = getenv('PHP_COMPILER_DISABLE_FFI');
if (false !== $v && '' !== $v && '0' !== $v && 'false' !== strtolower($v)) {
return false;
}

return 0 === (int) $ffi->unlink($path);
return true;
}

private static function ffi(): ?\FFI
Expand Down
14 changes: 14 additions & 0 deletions test/compliance/cases/stdlib/unlink_bootstrap.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
stdlib unlink() VM bootstrap without ext/ffi (#7314)
--FILE--
<?php
$path = tempnam(sys_get_temp_dir(), 'phpc_unlink_boot_');
echo file_exists($path) ? "exists\n" : "missing\n";
echo unlink($path) ? "removed\n" : "fail\n";
echo file_exists($path) ? "still\n" : "gone\n";
echo unlink($path) ? "again\n" : "nogone\n";
--EXPECT--
exists
removed
gone
nogone
36 changes: 36 additions & 0 deletions test/unit/VmFsUnlinkBootstrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\Test\Unit;

use PHPCompiler\ext\standard\VmFsUnlink;
use PHPUnit\Framework\TestCase;

/** VM unlink() without ext/ffi (#7314). */
final class VmFsUnlinkBootstrapTest extends TestCase
{
public function testUnlinkWithoutFfiViaHostLibc(): void
{
if (!\function_exists('unlink')) {
$this->markTestSkipped('host unlink() unavailable');
}
$path = tempnam(sys_get_temp_dir(), 'phpc_vm_unlink_boot_');
$this->assertNotFalse($path);
$this->assertFileExists($path);

$prev = getenv('PHP_COMPILER_DISABLE_FFI');
putenv('PHP_COMPILER_DISABLE_FFI=1');
try {
$this->assertTrue(VmFsUnlink::unlink($path));
$this->assertFileDoesNotExist($path);
$this->assertFalse(VmFsUnlink::unlink($path));
} finally {
if (false === $prev) {
putenv('PHP_COMPILER_DISABLE_FFI');
} else {
putenv('PHP_COMPILER_DISABLE_FFI='.$prev);
}
}
}
}
4 changes: 2 additions & 2 deletions test/unit/VmFsUnlinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ final class VmFsUnlinkTest extends TestCase
{
public function testLibcUnlinkRemovesFile(): void
{
if (!\extension_loaded('ffi')) {
$this->markTestSkipped('FFI extension required for VmFsUnlink');
if (!\extension_loaded('ffi') && !\function_exists('unlink')) {
$this->markTestSkipped('FFI or host unlink() required for VmFsUnlink');
}
$path = tempnam(sys_get_temp_dir(), 'phpc_vm_unlink_');
$this->assertNotFalse($path);
Expand Down