Skip to content

Commit 8bb926d

Browse files
authored
Merge pull request #25 from TheBlueMatt/main
Fix glibc compat tweaks, make jars deterministic
2 parents b358c36 + 79bfb86 commit 8bb926d

File tree

9 files changed

+133
-29
lines changed

9 files changed

+133
-29
lines changed

.github/workflows/build.yml

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
run: |
1616
apt-get update
1717
apt-get -y dist-upgrade
18-
apt-get -y install cargo libstd-rust-dev-wasm32 valgrind lld git g++ clang openjdk-11-jdk maven
18+
apt-get -y install cargo libstd-rust-dev-wasm32 valgrind lld git g++ clang openjdk-11-jdk maven faketime zip unzip llvm
1919
- name: Checkout source code
2020
uses: actions/checkout@v2
2121
with:
@@ -26,11 +26,10 @@ jobs:
2626
run: |
2727
git config --global user.email "[email protected]"
2828
git config --global user.name "LDK CI"
29-
git clone https://github.com/rust-bitcoin/rust-lightning
29+
# Note this is a different endpoint, as we need one non-upstream commit!
30+
git clone https://git.bitcoin.ninja/rust-lightning
3031
cd rust-lightning
31-
git remote add matt https://git.bitcoin.ninja/rust-lightning
32-
git fetch matt
33-
git merge matt/2021-03-java-bindings-base
32+
git checkout origin/2021-03-java-bindings-base
3433
cd ..
3534
git clone https://github.com/lightningdevkit/ldk-c-bindings
3635
- name: Rebuild C bindings without STD for WASM
@@ -54,7 +53,7 @@ jobs:
5453
mv liblightningjni_debug_Linux-amd64.so liblightningjni.so
5554
export ASAN_OPTIONS=detect_leaks=0
5655
LD_PRELOAD=/usr/lib/llvm-11/lib/clang/11.0.1/lib/linux/libclang_rt.asan-x86_64.so LD_LIBRARY_PATH=. mvn test
57-
- name: Build Java/TS Release Bindings
56+
- name: Detect current git version
5857
run: |
5958
# We assume the top commit is just a bindings update commit, so we
6059
# check out the previous commit to use as the commit we git describe.
@@ -68,18 +67,37 @@ jobs:
6867
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag $MERGE_HEAD^1)"
6968
fi
7069
echo "Using $LDK_GARBAGECOLLECTED_GIT_OVERRIDE as git version"
71-
./genbindings.sh ./ldk-c-bindings/ "-I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/" false false
72-
- name: Check latest headers and release lib are in git
70+
echo "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" > .git_ver
71+
- name: Checkout latest MacOS binaries
7372
run: |
74-
git diff --exit-code
73+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
74+
git clone https://git.bitcoin.ninja/ldk-java-bins
75+
mkdir -p src/main/resources/
76+
cp "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/liblightningjni_MacOSX-"* src/main/resources/
77+
- name: Build Java/TS Release Bindings
78+
run: |
79+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
80+
./genbindings.sh ./ldk-c-bindings/ "-I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/" false false
81+
- name: Build deterministic release jar
82+
run: ./build-release-jar.sh
7583
- name: Run Java Tests against built release jar
7684
run: |
77-
mvn -DskipTests=true package
7885
mvn install:install-file -Dfile=target/ldk-java-1.0-SNAPSHOT.jar -DgroupId=org.ldk -DartifactId=ldk-java -Dversion=1.0-SNAPSHOT -Dpackaging=jar
7986
cd javatester
8087
mvn package
8188
java -ea -jar target/ldk-java-tests-1.0-SNAPSHOT-jar-with-dependencies.jar
8289
cd ..
90+
- name: Check latest headers are in git
91+
run: |
92+
git diff --exit-code
93+
- name: Check latest library and jars are in bins repo
94+
run: |
95+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
96+
cp src/main/resources/liblightningjni_Linux-amd64.nativelib "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"
97+
cp target/ldk-java-1.0-SNAPSHOT.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"
98+
cp target/ldk-java-1.0-SNAPSHOT-sources.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"
99+
cd ldk-java-bins
100+
git diff --exit-code
83101
84102
osx:
85103
strategy:
@@ -137,7 +155,7 @@ jobs:
137155
tar xvvf openjdk-16.0.1_osx-x64_bin.tar.gz
138156
export JAVA_HOME=`pwd`/jdk-16.0.1.jdk/Contents/Home
139157
export PATH=$JAVA_HOME/bin:$PATH
140-
- name: Build Java/TS Release Bindings
158+
- name: Detect current git version
141159
run: |
142160
# We assume the top commit is just a bindings update commit, so we
143161
# check out the previous commit to use as the commit we git describe.
@@ -151,10 +169,19 @@ jobs:
151169
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag $MERGE_HEAD^1)"
152170
fi
153171
echo "Using $LDK_GARBAGECOLLECTED_GIT_OVERRIDE as git version"
172+
echo "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" > .git_ver
173+
- name: Checkout latest Linux binaries
174+
run: |
175+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
176+
git clone https://git.bitcoin.ninja/ldk-java-bins
177+
mkdir -p src/main/resources/
178+
cp "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/liblightningjni_Linux-"* src/main/resources/
179+
- name: Build Java/TS Release Bindings
180+
run: |
181+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
154182
# genbindings.sh always fails as there is no wasm32-wasi library
155-
# available, so instead we delete the expected JNI library and check
156-
# that it was created.
157-
rm src/main/resources/liblightningjni_MacOSX-x86_64.nativelib
183+
# available, so instead we check that the expected JNI library
184+
# is created.
158185
./genbindings.sh ./ldk-c-bindings/ "-I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin -isysroot$(xcrun --show-sdk-path)" false false || echo
159186
cat src/main/resources/liblightningjni_MacOSX-x86_64.nativelib > /dev/null
160187
- name: Fetch Maven 3.8.1
@@ -174,8 +201,11 @@ jobs:
174201
mvn package
175202
java -ea -jar target/ldk-java-tests-1.0-SNAPSHOT-jar-with-dependencies.jar
176203
cd ..
177-
- name: Check latest headers and release lib are in git
204+
- name: Check latest release libs are in git
178205
run: |
179206
if [ "${{ matrix.platform }}" = "macos-11" ]; then
207+
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)"
208+
cp src/main/resources/liblightningjni_MacOS-amd64.nativelib "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"
209+
cd ldk-java-bins
180210
git diff --exit-code
181211
fi

build-release-jar.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#/bin/sh
2+
set -e
3+
set -x
4+
5+
# zips are not generally deterministic without some tweaking, which we do here.
6+
7+
echo "Checking that required release native libraries are present..."
8+
ls src/main/resources/liblightningjni_Linux-amd64.nativelib
9+
ls src/main/resources/liblightningjni_MacOSX-x86_64.nativelib
10+
ls src/main/resources/liblightningjni_MacOSX-aarch64.nativelib
11+
12+
export LANG=C
13+
14+
mvn clean
15+
faketime 2021-01-01 mvn -DskipTests=true package
16+
17+
mkdir ziptmp
18+
cd ziptmp
19+
unzip ../target/ldk-java-1.0-SNAPSHOT-sources.jar | grep inflating | awk '{ print $2 }' | sort > ../sources-zip-files.txt
20+
sed -i 's/#Fri Jan 01 00:.* UTC 2021/#Fri Jan 01 00:00:00 UTC 2021/' META-INF/maven/org.ldk/ldk-java/pom.properties
21+
touch -d "2021-01-01 00:00 UTC" $(cat ../sources-zip-files.txt)
22+
cat ../sources-zip-files.txt | zip -X@ ../ldk-java-1.0-SNAPSHOT-sources-deterministic.jar
23+
cd ..
24+
rm -r ziptmp
25+
mv ldk-java-1.0-SNAPSHOT-sources-deterministic.jar target/ldk-java-1.0-SNAPSHOT-sources.jar
26+
27+
mkdir ziptmp
28+
cd ziptmp
29+
unzip ../target/ldk-java-1.0-SNAPSHOT.jar | grep inflating | awk '{ print $2 }' | sort > ../classes-zip-files.txt
30+
sed -i 's/#Fri Jan 01 00:.* UTC 2021/#Fri Jan 01 00:00:00 UTC 2021/' META-INF/maven/org.ldk/ldk-java/pom.properties
31+
sed -i 's/Built-By: .*/Built-By: ldk-deterministic-builds/' META-INF/MANIFEST.MF
32+
touch -d "2021-01-01 00:00 UTC" $(cat ../classes-zip-files.txt)
33+
cat ../classes-zip-files.txt | zip -X@ ../ldk-java-1.0-SNAPSHOT-deterministic.jar
34+
cd ..
35+
rm -r ziptmp
36+
mv ldk-java-1.0-SNAPSHOT-deterministic.jar target/ldk-java-1.0-SNAPSHOT.jar

genbindings.sh

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ if [ "$TARGET_STRING" = "" ]; then
2626
fi
2727
case "$TARGET_STRING" in
2828
"x86_64-pc-linux"*)
29-
LDK_TARGET_SUFFIX="_Linux-amd64" ;;
29+
LDK_TARGET_SUFFIX="_Linux-amd64"
30+
LDK_JAR_TARGET=true
31+
;;
3032
"x86_64-apple-darwin"*)
31-
LDK_TARGET_SUFFIX="_MacOSX-x86_64" ;;
33+
LDK_TARGET_SUFFIX="_MacOSX-x86_64"
34+
LDK_JAR_TARGET=true
35+
;;
3236
"aarch64-apple-darwin"*)
33-
LDK_TARGET_SUFFIX="_MacOSX-aarch64" ;;
37+
LDK_TARGET_SUFFIX="_MacOSX-aarch64"
38+
LDK_JAR_TARGET=true
39+
;;
3440
*)
35-
LDK_TARGET_SUFFIX=""
41+
LDK_TARGET_SUFFIX="_${TARGET_STRING}"
3642
esac
3743
if [ "$LDK_TARGET_CPU" = "" ]; then
3844
LDK_TARGET_CPU="sandybridge"
@@ -78,26 +84,58 @@ if [ "$3" = "true" ]; then
7884
[ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
7985
$COMPILE -o liblightningjni_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm
8086
else
87+
LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
8188
if [ "$IS_MAC" = "false" ]; then
8289
COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
8390
echo "// __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd." >> src/main/jni/bindings.c
8491
echo "// However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and fallback in case it is missing." >> src/main/jni/bindings.c
85-
echo "// Because it is weak-linked on the rust side, we can simply define it explicitly here, forcing rust to use the fallback." >> src/main/jni/bindings.c
92+
echo "// Because it is weak-linked on the rust side, we should be able to simply define it explicitly here, forcing rust to use the fallback." >> src/main/jni/bindings.c
8693
echo "void *__cxa_thread_atexit_impl = NULL;" >> src/main/jni/bindings.c
94+
# Note that the above is not sufficient. For some reason involving ancient dark magic and
95+
# haunted code segments, overriding the weak symbol only impacts sites which *call* the
96+
# symbol in question, not sites which *compare with* the symbol in question.
97+
# This means that the NULL check in rust's libstd will always think the function is
98+
# callable while the function which is called ends up being NULL (leading to a jmp to the
99+
# zero page and a quick SEGFAULT).
100+
# This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
101+
# and --defsym arguments.
102+
# In smaller programs, it appears to be possible to work around this with -Bsymbolic and
103+
# -nostdlib, however when applied the full-sized JNI library here it no longer works.
104+
# After exhausting nearly every flag documented in lld, the only reliable method appears
105+
# to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
106+
# disassemble it into very readable code, edit it, and then reassemble it.
107+
[ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
108+
if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
109+
echo "Archive contained non-object files!"
110+
exit 1
111+
fi
112+
if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
113+
echo "Archive contained more than one LDK object file"
114+
exit 1
115+
fi
116+
mkdir -p tmp
117+
rm -f tmp/*
118+
ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
119+
pushd tmp
120+
llvm-dis ldk.ldk.*-cgu.*.rcgu.o
121+
sed -i 's/br i1 icmp eq (i8\* @__cxa_thread_atexit_impl, i8\* null)/br i1 icmp eq (i8* null, i8* null)/g' ldk.ldk.*-cgu.*.rcgu.o.ll
122+
llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
123+
ar q libldk.a *.o
124+
popd
125+
LDK_LIB="tmp/libldk.bc tmp/libldk.a"
87126
fi
88-
$COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
127+
$COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
89128
if [ "$IS_MAC" = "false" ]; then
90-
set +e # grep exits with 1 if no lines were left, which is our success condition
91-
GLIBC_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep GLIBC_ | grep -v "GLIBC_2\.2\." | grep -v "GLIBC_2\.3\(\.\| \)" | grep -v "GLIBC_2.\(14\|17\) ")"
92-
set -e
129+
GLIBC_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep GLIBC_ | grep -v "GLIBC_2\.2\." | grep -v "GLIBC_2\.3\(\.\| \)" | grep -v "GLIBC_2.\(14\|17\) " || echo)"
93130
if [ "$GLIBC_SYMBS" != "" ]; then
94131
echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
95132
echo "$GLIBC_SYMBS"
96133
exit 1
97134
fi
98135
fi
99-
if [ "$LDK_TARGET_SUFFIX" != "" ]; then
136+
if [ "$LDK_JAR_TARGET" = "true" ]; then
100137
# Copy to JNI native directory for inclusion in JARs
138+
mkdir -p src/main/resources/
101139
cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib
102140
fi
103141
fi

src/main/java/org/ldk/impl/bindings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public VecOrSliceDef(long dataptr, long datalen, long stride) {
4848
static native String get_lib_version_string();
4949

5050
public static String get_ldk_java_bindings_version() {
51-
return "v0.0.98.3";
51+
return "v0.0.98.4";
5252
}
5353
public static native String get_ldk_c_bindings_version();
5454
public static native String get_ldk_version();

src/main/jni/bindings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
133133
}
134134

135135
JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
136-
return str_ref_to_java(env, "v0.0.98.3", strlen("v0.0.98.3"));
136+
return str_ref_to_java(env, "v0.0.98.4", strlen("v0.0.98.4"));
137137
}
138138
JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
139139
return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
@@ -25884,5 +25884,5 @@ JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_SiPrefix_1to_1str(JNIEnv *e
2588425884

2588525885
// __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
2588625886
// However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and fallback in case it is missing.
25887-
// Because it is weak-linked on the rust side, we can simply define it explicitly here, forcing rust to use the fallback.
25887+
// Because it is weak-linked on the rust side, we should be able to simply define it explicitly here, forcing rust to use the fallback.
2588825888
void *__cxa_thread_atexit_impl = NULL;

src/main/jni/bindings.c.body

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
131131
}
132132

133133
JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
134-
return str_ref_to_java(env, "v0.0.98.3", strlen("v0.0.98.3"));
134+
return str_ref_to_java(env, "v0.0.98.4", strlen("v0.0.98.4"));
135135
}
136136
JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
137137
return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
-8.61 MB
Binary file not shown.
-4.28 MB
Binary file not shown.
-4.85 MB
Binary file not shown.

0 commit comments

Comments
 (0)