forked from LuaJIT/LuaJIT
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FFI: Workaround for platform dlerror() returning NULL.
Contributed by mcclure. (cherry picked from commit 478bcfe) The `ffi.load()` implementation assumes the string returned from `dlerror()` is non-NULL and immediately dereferences it. This may lead to a crash on some platforms like Android (Oculus Quest) where it is possible. This patch adds the corresponding check and the default "dlopen failed" error message. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#10199
- Loading branch information
Showing
6 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
test/tarantool-tests/lj-522-fix-dlerror-return-null.test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
local tap = require('tap') | ||
local test = tap.test('lj-522-fix-dlerror-return-null'):skipcond({ | ||
-- XXX: Unfortunately, it's too hard to overload (or even | ||
-- impossible, who knows, since Cupertino fellows do not | ||
-- provide any information about their system) something from | ||
-- dyld sharing cache (includes the <libdyld.dylib> library | ||
-- providing `dlerror()`). | ||
-- All in all, this test checks the part that is common for all | ||
-- platforms, so it's not vital to run this test on macOS since | ||
-- everything can be checked on Linux in a much easier way. | ||
['<dlerror> cannot be overridden on macOS'] = jit.os == 'OSX', | ||
}) | ||
|
||
test:plan(1) | ||
|
||
-- `makecmd()` runs <%testname%/script.lua> by | ||
-- `LUAJIT_TEST_BINARY` with the given environment and launch | ||
-- options. | ||
local script = require('utils').exec.makecmd(arg, { | ||
env = { LD_PRELOAD = 'mydlerror.so' }, | ||
redirect = '2>&1', | ||
}) | ||
|
||
local output = script() | ||
test:like(output, 'dlopen failed', 'correct error message') | ||
|
||
test:done(true) |
1 change: 1 addition & 0 deletions
1
test/tarantool-tests/lj-522-fix-dlerror-return-null/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BuildTestCLib(mydlerror mydlerror.c) |
6 changes: 6 additions & 0 deletions
6
test/tarantool-tests/lj-522-fix-dlerror-return-null/mydlerror.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <stddef.h> | ||
|
||
char *dlerror(void) | ||
{ | ||
return NULL; | ||
} |
10 changes: 10 additions & 0 deletions
10
test/tarantool-tests/lj-522-fix-dlerror-return-null/script.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
local ffi = require('ffi') | ||
|
||
-- Overloaded `dlerror()` returns NULL after trying to load an | ||
-- unexisting file. | ||
local res, errmsg = pcall(ffi.load, 'lj-522-fix-dlerror-return-null-unexisted') | ||
|
||
assert(not res, 'pcall should fail') | ||
|
||
-- Return the error message to be checked by the TAP. | ||
io.write(errmsg) |