Skip to content

Commit

Permalink
dlopen: Fix system libraries searchpath (#441)
Browse files Browse the repository at this point in the history
The canonical path for native system libraries is `/system/lib` for 32bits and `/system/lib64`
for 64bits (c.f., https://source.android.com/docs/core/permissions/namespaces_libraries).

This was completely ignored, leading to explicit ffi.load calls on system libraries (e.g., libc for threading) attempting to load a 32bits variant on a 64bits ABI...
  • Loading branch information
benoit-pierre authored Oct 9, 2023
1 parent 0eca21b commit c553909
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion assets/android.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2654,9 +2654,10 @@ local function run(android_app_state)

-- load the dlopen() implementation
android.dl = require("dl")
android.dl.system_libdir = ffi.abi("64bit") and "/system/lib64" or "/system/lib"
android.dl.library_path = android.nativeLibraryDir..":"..
android.dir..":"..android.dir.."/libs:"..
"/lib:/system/lib:/lib/lib?.so:/system/lib/lib?.so"
string.gsub("@:@/lib?.so", "@", android.dl.system_libdir)

-- register the dependency lib loader
table.insert(package.loaders, 3, android.deplib_loader)
Expand Down
5 changes: 4 additions & 1 deletion assets/dl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ end
local dl = {
-- set this to search in certain directories
library_path = '/lib/?;/usr/lib/?;/usr/local/lib/?',
-- set this to the directory of system libraries
-- (to be ignored when loading dependencies).
system_libdir = nil,
}

local function sys_dlopen(library, global, padding)
Expand Down Expand Up @@ -114,7 +117,7 @@ function dl.dlopen(library, load_func, depth)
-- libvulkan, and libz
-- c.f., https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#private-api-enforced-for-api-level-24
-- Our current code should *never* hit any private system libs, so, this is basically overkill ;).
if depth > 0 and (pspec == "/system/lib" or library == "libdl.so") then
if depth > 0 and (pspec == dl.system_libdir or library == "libdl.so") then
-- depth > 0 to allow explicitly loading a system lib
-- (because this might have genuine use cases, as some early API levels do not put DT_NEEDED libraries into the global namespace)
-- pspec to reject system libs
Expand Down

0 comments on commit c553909

Please sign in to comment.