Skip to content

Commit

Permalink
add dlerror() information (#32)
Browse files Browse the repository at this point in the history
When the uplink library cannot be loaded, there is currently a generic
message originating from PHP. This is in all PHP versions.

This change adds extra information which will help the user diagnose the
issue.

Example of old output:

Fatal error: Uncaught FFI\Exception: Failed loading '/app/build/libuplink-x86_64-linux.so' in /app/src/Uplink.php:42

Example of new output:

Fatal error: Uncaught FFI\Exception: Failed loading '/app/build/libuplink-x86_64-linux.so'. /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by /app/build/libuplink-x86_64-linux.so) in /app/src/Uplink.php:60

Fixes #21
  • Loading branch information
Erikvv authored Jul 20, 2022
1 parent b5346b5 commit 8cb12c3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pipeline {
stage('PHPStan') {
agent {
docker {
image 'ghcr.io/phpstan/phpstan:1.0.2'
image 'ghcr.io/phpstan/phpstan:1.8.1'
args '--mount type=volume,source=phpstan-cache,destination=/tmp/phpstan ' +
'--user root:root ' +
"--entrypoint='' "
Expand Down
8 changes: 0 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ parameters:
ignoreErrors:
- '/Call to an undefined method FFI::.*/'
- '/Access to an undefined property FFI\\CData::.*/'
-
message: '/Binary operation "-" between numeric-string and string results in an error./'
paths:
- test/ListObjectsTest.php
-
message: "#^Binary operation \"\\-\" between int and string results in an error\\.$#"
count: 1
path: test/StatObjectTest.php
-
message: "#^Property Storj\\\\Uplink\\\\Access\\:\\:\\$scope is never read, only written\\.$#"
count: 1
Expand Down
43 changes: 39 additions & 4 deletions src/Uplink.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,45 @@ public static function create(): self
$os = str_replace(' ', '_', strtolower(php_uname('s')));
$extension = strpos($os, 'windows') !== false ? 'dll' : 'so';

$ffi = FFI::cdef(
file_get_contents($root . '/build/uplink-php.h'),
"{$root}/build/libuplink-{$arch}-{$os}.{$extension}"
);
$libuplinkSo = "{$root}/build/libuplink-{$arch}-{$os}.{$extension}";
try {
$ffi = FFI::cdef(
file_get_contents($root . '/build/uplink-php.h'),
$libuplinkSo
);
} catch (\FFI\Exception $ffiException) {
// dlerror() may have a more detailed error.
// Example: no permission, wrong glibc version, unknown binary format etc.
// This check should really be in PHP core.
try {
$ffi2 = FFI::cdef("
void *dlopen(const char *filename, int flags);
char *dlerror();
int dlclose(void *handle);
");
$handle = $ffi2->dlopen($libuplinkSo, 1);
$scope = Scope::exit(function() use ($handle, $ffi2) {
if ($handle !== null) {
$ffi2->dlclose($handle);
}
});
$cError = $ffi2->dlerror();
} catch (\Throwable $innerException) {
// error when calling dlopen/dlerror, rethrow original error
throw $ffiException;
}

if ($cError == null) {
// dlerror had no info, rethrow original error
throw $ffiException;
}

$dlErrorMessage = FFI::string($cError);

throw new \FFI\Exception(
$ffiException->getMessage() . ": " . $dlErrorMessage
);
}

return new self($ffi);
}
Expand Down

0 comments on commit 8cb12c3

Please sign in to comment.