Skip to content

Commit 6ecc752

Browse files
committed
fix(android): fix segfault from bionic cleanup by forcing RTLD_NODELETE
and use dynamic openssl
1 parent 8f43b53 commit 6ecc752

4 files changed

Lines changed: 12 additions & 37 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,6 @@ jobs:
3838
if: matrix.os == 'windows-11-arm'
3939
uses: jwlawson/actions-setup-cmake@v2
4040

41-
- name: Cache Android NDK
42-
if: matrix.android
43-
id: cache-android-ndk
44-
uses: actions/cache@v5
45-
with:
46-
path: android-ndk-r29
47-
key: android-ndk-r29-aarch64
48-
49-
- name: Setup Android NDK
50-
if: matrix.android
51-
run: |
52-
if [[ "${{ steps.cache-android-ndk.outputs.cache-hit }}" != "true" ]]; then
53-
wget -q https://github.com/lzhiyong/termux-ndk/releases/download/android-ndk/android-ndk-r29-aarch64.7z
54-
7z x android-ndk-r29-aarch64.7z -o. > /dev/null
55-
fi
56-
echo "ANDROID_NDK_ROOT=$PWD/android-ndk-r29" >> $GITHUB_ENV
57-
5841
- name: Setup lde
5942
if: "!matrix.android"
6043
uses: lde-org/setup-lde@master
@@ -87,7 +70,7 @@ jobs:
8770
docker run --privileged --platform linux/arm64 --name termux-build \
8871
-e DEBIAN_FRONTEND=noninteractive \
8972
termux/termux-docker:aarch64 \
90-
bash -c "apt update && apt upgrade -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold && apt install -y git clang curl zip make cmake && curl -fsSL https://lde.sh/install | sh -s -- --nightly"
73+
bash -c "apt update && apt upgrade -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold && apt install -y git clang curl zip make cmake openssl-dev && curl -fsSL https://lde.sh/install | sh -s -- --nightly"
9174
docker commit termux-build termux-android:latest
9275
docker rm termux-build
9376
docker save termux-android:latest -o /tmp/termux-android.tar
@@ -100,6 +83,5 @@ jobs:
10083
--platform linux/arm64 \
10184
-v ${{ github.workspace }}:/workspace \
10285
-w /workspace \
103-
-e ANDROID_NDK_ROOT=/workspace/android-ndk-r29 \
10486
termux-android:latest \
10587
bash -c "/data/data/com.termux/files/home/.lde/lde test"

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
[submodule "vendor/libgit2"]
22
path = vendor/libgit2
33
url = https://github.com/libgit2/libgit2
4-
[submodule "vendor/mbedtls"]
5-
path = vendor/mbedtls
6-
url = https://github.com/Mbed-TLS/mbedtls

build.lua

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ local function exec(cmd)
1616
end
1717

1818
local build = src .. sep .. "build"
19-
local ndkRoot = os.getenv("ANDROID_NDK_ROOT")
2019

2120
---@format disable-next
2221
local gitMin = "-DBUILD_TESTS=OFF -DBUILD_CLI=OFF -DUSE_SSH=OFF -DUSE_GSSAPI=OFF -DUSE_NTLMCLIENT=OFF -DREGEX_BACKEND=builtin -DUSE_HTTP_PARSER=builtin -DCMAKE_C_FLAGS=-g0"
@@ -28,17 +27,6 @@ if isWindows then
2827
elseif isMac then
2928
https = "SecureTransport"
3029
cmakeExtra = ""
31-
elseif isAndroid and ndkRoot then
32-
https = "mbedTLS"
33-
local mbedSrc = scriptDir .. "vendor" .. sep .. "mbedtls"
34-
local mbedBuild = mbedSrc .. sep .. "build"
35-
local mbedOut = mbedSrc .. sep .. "install"
36-
local toolchain = ndkRoot .. "/build/cmake/android.toolchain.cmake"
37-
local androidFlags = '-DCMAKE_TOOLCHAIN_FILE="' .. toolchain .. '" -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-24'
38-
exec('cmake -S "' .. mbedSrc .. '" -B "' .. mbedBuild .. '" ' .. androidFlags .. ' -DCMAKE_INSTALL_PREFIX="' .. mbedOut .. '" -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF')
39-
exec('cmake --build "' .. mbedBuild .. '" -j$(nproc)')
40-
exec('cmake --install "' .. mbedBuild .. '"')
41-
cmakeExtra = androidFlags .. ' -DMBEDTLS_ROOT_DIR="' .. mbedOut .. '"'
4230
else
4331
https = "OpenSSL"
4432
cmakeExtra = ""
@@ -54,6 +42,5 @@ elseif isMac then
5442
exec('strip -x "' .. outLib .. '"')
5543
else
5644
exec('cp "' .. build .. '/libgit2.so" "' .. outLib .. '"')
57-
local strip = (isAndroid and ndkRoot) and (ndkRoot .. "/toolchains/llvm/prebuilt/linux-aarch64/bin/llvm-strip") or "strip"
58-
exec(strip .. ' --strip-unneeded --remove-section=.eh_frame --remove-section=.eh_frame_hdr "' .. outLib .. '"')
45+
exec('strip --strip-unneeded --remove-section=.eh_frame --remove-section=.eh_frame_hdr "' .. outLib .. '"')
5946
end

src/init.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ffi.cdef [[
2929

3030
const git_error *git_error_last(void);
3131
int git_libgit2_init(void);
32-
void git_libgit2_shutdown(void);
32+
int git_libgit2_shutdown(void);
3333

3434
int git_clone_options_init(git_clone_options *opts, unsigned int version);
3535
int git_clone(git_repository **out, const char *url, const char *local_path, const git_clone_options *options);
@@ -83,6 +83,15 @@ ffi.cdef [[
8383

8484
local here = debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or ""
8585
local libName = jit.os == "Windows" and "git2.dll" or (jit.os == "OSX" and "libgit2.dylib" or "libgit2.so")
86+
87+
-- On Android/Bionic, OpenSSL's atexit cleanup crashes if libgit2.so has been
88+
-- dlclose'd (it holds callbacks into it). Pre-open with RTLD_NODELETE so the
89+
-- library stays mapped through process teardown even after ffi's dlclose.
90+
if os.getenv("ANDROID_ROOT") then
91+
ffi.cdef [[ void *dlopen(const char *filename, int flags); ]]
92+
ffi.C.dlopen(here .. libName, 0x2 + 0x1000) -- RTLD_NOW | RTLD_NODELETE
93+
end
94+
8695
local lib = ffi.load(here .. libName)
8796

8897
lib.git_libgit2_init()

0 commit comments

Comments
 (0)