From 6d163e72769707ef86a66dfafce6570729455979 Mon Sep 17 00:00:00 2001 From: wadakatu Date: Thu, 24 Sep 2026 03:46:43 +0900 Subject: [PATCH] fix startup with the default ffi.enable=preload --- CHANGELOG.md | 2 ++ phpunit.xml | 2 +- src/FFI.php | 19 ++++++++---- tests/StartupTest.php | 54 +++++++++++++++++++++++++++++++++ tests/scripts/print-version.php | 11 +++++++ 5 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 tests/StartupTest.php create mode 100644 tests/scripts/print-version.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d970bf..f641589c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to `php-vips` will be documented in this file. - better ffi startup diagnostics [ping-localhost] - add setBlock() and setBlockUntrusted() to control operation blocking [jcupitt] +- fix startup with the default `ffi.enable=preload` [wadakatu] +- include the FFI error in the startup exception [wadakatu] ## 2.6.1 - 2025-12-10 diff --git a/phpunit.xml b/phpunit.xml index cd2dbadd..17be659e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,7 +11,7 @@ stopOnFailure="false"> - ./tests/ + ./tests/ diff --git a/src/FFI.php b/src/FFI.php index 926f24c0..d2fcac2a 100644 --- a/src/FFI.php +++ b/src/FFI.php @@ -106,6 +106,13 @@ class FFI private static int $library_minor; private static int $library_micro; + /** + * The last exception that FFI extension throws after loading library. + * + * @internal + */ + private static ?\FFI\Exception $ffi_last_exception = null; + public static function glib(): \FFI { self::init(); @@ -254,6 +261,7 @@ private static function libraryLoad( string $interface ): ?\FFI { Utils::debugLog("trying to open", ["libraryName" => $libraryName]); + self::$ffi_last_exception = null; foreach (self::$libraryPaths as $path) { Utils::debugLog("trying path", ["path" => $path]); try { @@ -265,6 +273,7 @@ private static function libraryLoad( "msg" => "library load failed", "exception" => $e->getMessage() ]); + self::$ffi_last_exception = $e; } } return null; @@ -281,11 +290,6 @@ private static function init(): void if (!extension_loaded("ffi")) { throw new Exception("FFI extension not loaded"); } - $enable = ini_get("ffi.enable"); - if ($enable != "true" && - $enable != "1") { - throw new Exception("ffi.enable set to '$enable', not 'true'"); - } $vips_libname = self::libraryName("libvips", 42); $glib_libname = self::libraryName("libglib-2.0", 0); @@ -317,7 +321,10 @@ private static function init(): void } $msg .= ". Make sure that you've installed libvips and that '$vips_libname'"; $msg .= " is on your system's library search path."; - throw new Exception($msg); + if (self::$ffi_last_exception instanceof \FFI\Exception) { + $msg .= " FFI extension error: ". self::$ffi_last_exception->getMessage(); + } + throw new Exception($msg, 0, self::$ffi_last_exception); } $result = $vips->vips_init(""); diff --git a/tests/StartupTest.php b/tests/StartupTest.php new file mode 100644 index 00000000..3653c58b --- /dev/null +++ b/tests/StartupTest.php @@ -0,0 +1,54 @@ +execFFI("ffi.enable=preload"); + + $this->assertEquals(0, $result_code); + $this->assertEquals(preg_match("/\d+\.\d+\.\d+/", implode("\n", $output)), 1); + } + + public function testReportsFfiReasonWhenFfiDisabled() + { + [$output, $result_code] = $this->execFFI("ffi.enable=0"); + + $this->assertNotEquals(0, $result_code); + $this->assertTrue(strpos(implode("\n", $output), "FFI API is restricted") !== false); + } + + private function execFFI(string $ffi_enable_arg): array + { + exec( + escapeshellarg(PHP_BINARY) + ." -d " + .$ffi_enable_arg . " " + .escapeshellarg(__DIR__ . "/scripts/print-version.php") + ." 2>&1", + $output, + $result_code + ); + + return [$output, $result_code]; + } +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: expandtab sw=4 ts=4 fdm=marker + * vim<600: expandtab sw=4 ts=4 + */ diff --git a/tests/scripts/print-version.php b/tests/scripts/print-version.php new file mode 100644 index 00000000..47037468 --- /dev/null +++ b/tests/scripts/print-version.php @@ -0,0 +1,11 @@ +