1 parent
818d391022
commit
639362fbf5
40 files changed
+6701
-429
No files matched your search
Executable
+237
@@ -0,0 +1,237 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
if [[ $# != 1 ]]; then
|
||||
echo "usage: $0 SOURCE_ROOT" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
source_root=$(realpath -e -- "$1")
|
||||
for command_name in cc cmake patchelf readelf python3; do
|
||||
command -v "$command_name" >/dev/null || {
|
||||
echo "missing test command: $command_name" >&2
|
||||
exit 2
|
||||
}
|
||||
done
|
||||
|
||||
temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-runtime-deps.XXXXXX")
|
||||
trap 'rm -rf -- "$temporary"' EXIT
|
||||
configured_build="$temporary/configured-build"
|
||||
mkdir -p "$configured_build"
|
||||
configured_compiler=$(realpath -e -- "$(command -v c++)")
|
||||
printf 'CMAKE_CXX_COMPILER:FILEPATH=%s\n' "$configured_compiler" \
|
||||
>"$configured_build/CMakeCache.txt"
|
||||
queried_compiler=$(CXX=/bin/false \
|
||||
"$source_root/scripts/configured-cxx-compiler.sh" "$configured_build")
|
||||
[[ $queried_compiler == "$configured_compiler" ]] || {
|
||||
echo "runtime compiler query followed CXX instead of the configured cache" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
preferred_lib="$temporary/preferred-lib"
|
||||
other_lib="$temporary/other-lib"
|
||||
bundle="$temporary/bundle"
|
||||
resolver="$temporary/resolver"
|
||||
seed_lib="$temporary/seed-lib"
|
||||
mkdir -p \
|
||||
"$preferred_lib" \
|
||||
"$other_lib" \
|
||||
"$resolver" \
|
||||
"$seed_lib" \
|
||||
"$bundle/bin" \
|
||||
"$bundle/lib" \
|
||||
"$bundle/service" \
|
||||
"$bundle/share/echoion/web"
|
||||
|
||||
cat >"$temporary/dependency.c" <<'C'
|
||||
int echoion_dependency(void) { return 42; }
|
||||
__attribute__((used, section(".echoion_variant")))
|
||||
static const char echoion_variant[] = ECHOION_VARIANT;
|
||||
C
|
||||
cat >"$temporary/core.c" <<'C'
|
||||
extern int echoion_dependency(void);
|
||||
extern int echoion_compiler_runtime(void);
|
||||
int echoion_core(void) {
|
||||
return echoion_dependency() + echoion_compiler_runtime();
|
||||
}
|
||||
C
|
||||
cat >"$temporary/main.c" <<'C'
|
||||
int main(void) { return 0; }
|
||||
C
|
||||
cat >"$temporary/compiler-runtime.c" <<'C'
|
||||
int echoion_compiler_runtime(void) { return 7; }
|
||||
__attribute__((used, section(".echoion_compiler_variant")))
|
||||
static const char echoion_compiler_variant[] = ECHOION_VARIANT;
|
||||
C
|
||||
|
||||
for variant in preferred other; do
|
||||
case $variant in
|
||||
preferred) variant_lib=$preferred_lib ;;
|
||||
other) variant_lib=$other_lib ;;
|
||||
esac
|
||||
cc -fPIC -shared "$temporary/dependency.c" \
|
||||
'-DECHOION_VARIANT="'"$variant"'-runtime"' \
|
||||
-Wl,-soname,libechoion-dependency.so.1 \
|
||||
-o "$variant_lib/libechoion-dependency.so.1.2.3"
|
||||
ln -s libechoion-dependency.so.1.2.3 \
|
||||
"$variant_lib/libechoion-dependency.so.1"
|
||||
ln -s libechoion-dependency.so.1 \
|
||||
"$variant_lib/libechoion-dependency.so"
|
||||
done
|
||||
for variant in seeded resolver; do
|
||||
case $variant in
|
||||
seeded)
|
||||
variant_lib=$seed_lib
|
||||
variant_version=100
|
||||
;;
|
||||
resolver)
|
||||
variant_lib=$preferred_lib
|
||||
variant_version=200
|
||||
;;
|
||||
esac
|
||||
cc -fPIC -shared "$temporary/compiler-runtime.c" \
|
||||
'-DECHOION_VARIANT="'"$variant"'-compiler-runtime"' \
|
||||
-Wl,-soname,libstdc++.so.6 \
|
||||
-o "$variant_lib/libstdc++.so.6.0.$variant_version"
|
||||
ln -s "libstdc++.so.6.0.$variant_version" \
|
||||
"$variant_lib/libstdc++.so.6"
|
||||
done
|
||||
resolver_core="$resolver/Echoion.ts"
|
||||
cc -fPIC -shared "$temporary/core.c" \
|
||||
-L"$preferred_lib" -lechoion-dependency \
|
||||
-Wl,-l:libstdc++.so.6 \
|
||||
-Wl,-soname,Echoion.ts \
|
||||
-Wl,-rpath,"$preferred_lib" \
|
||||
-o "$resolver_core"
|
||||
install -m 0644 "$resolver_core" "$bundle/lib/Echoion.ts"
|
||||
patchelf --set-rpath "\$ORIGIN" "$bundle/lib/Echoion.ts"
|
||||
cc "$temporary/main.c" -o "$bundle/bin/echoiond"
|
||||
printf '%s\n' '[Service]' >"$bundle/service/echoion.service.in"
|
||||
printf '%s\n' '<!doctype html><title>Echoion</title>' \
|
||||
>"$bundle/share/echoion/web/index.html"
|
||||
|
||||
cmake \
|
||||
"-DBUNDLE_ROOT=$bundle" \
|
||||
"-DBUNDLE_RESOLVER_LIBRARIES=$resolver_core" \
|
||||
"-DBUNDLE_SEARCH_DIRECTORIES=$other_lib" \
|
||||
"-DBUNDLE_SEED_FILES=$seed_lib/libstdc++.so.6" \
|
||||
"-DPATCHELF_EXECUTABLE=$(command -v patchelf)" \
|
||||
-P "$source_root/cmake/BundleRuntimeDependencies.cmake"
|
||||
|
||||
python3 - "$source_root/scripts/validate-release-elf.py" <<'PY'
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
module_path = Path(sys.argv[1])
|
||||
spec = importlib.util.spec_from_file_location("validate_release_elf", module_path)
|
||||
assert spec is not None and spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
fixture = """
|
||||
Version needs section '.gnu.version_r' contains 2 entries:
|
||||
000000: Version: 1 File: libm.so.6 Cnt: 1
|
||||
0x0010: Name: GLIBC_9.99 Flags: none Version: 4
|
||||
0x0020: Version: 1 File: libc.so.6 Cnt: 3
|
||||
0x0030: Name: GLIBC_2.17 Flags: none Version: 3
|
||||
0x0040: Name: GLIBC_2.36 Flags: none Version: 2
|
||||
0x0050: Name: GLIBC_ABI_DT_RELR Flags: none Version: 1
|
||||
"""
|
||||
assert module.required_glibc_versions(fixture) == {(2, 17), (2, 36), (9, 99)}
|
||||
assert module.parse_version("2.35") == (2, 35)
|
||||
assert module.is_system_dependency("libc.so.6")
|
||||
assert module.is_system_dependency("libm.so.6")
|
||||
assert not module.is_system_dependency("libc.so.evil")
|
||||
assert not module.is_system_dependency("libm.so.99")
|
||||
|
||||
unknown_abi = """
|
||||
Version needs section '.gnu.version_r' contains 1 entry:
|
||||
000000: Version: 1 File: libc.so.6 Cnt: 1
|
||||
0x0010: Name: GLIBC_ABI_FUTURE Flags: none Version: 1
|
||||
"""
|
||||
try:
|
||||
module.required_glibc_versions(unknown_abi)
|
||||
except ValueError as error:
|
||||
assert "unknown GLIBC ABI requirement" in str(error)
|
||||
else:
|
||||
raise AssertionError("unknown GLIBC ABI requirement was accepted")
|
||||
PY
|
||||
|
||||
evil_bundle="$temporary/evil-bundle"
|
||||
evil_build="$temporary/evil-build"
|
||||
cp -a -- "$bundle" "$evil_bundle"
|
||||
mkdir "$evil_build"
|
||||
cat >"$temporary/evil-dependency.c" <<'C'
|
||||
int echoion_evil_dependency(void) { return 13; }
|
||||
C
|
||||
cat >"$temporary/evil-user.c" <<'C'
|
||||
extern int echoion_evil_dependency(void);
|
||||
int echoion_evil_user(void) { return echoion_evil_dependency(); }
|
||||
C
|
||||
cc -fPIC -shared "$temporary/evil-dependency.c" \
|
||||
-Wl,-soname,libc.so.evil -o "$evil_build/libc.so.evil"
|
||||
cc -fPIC -shared "$temporary/evil-user.c" \
|
||||
-L"$evil_build" -Wl,-l:libc.so.evil -Wl,-rpath,"$evil_build" \
|
||||
-o "$evil_build/libevil-user.so"
|
||||
rm "$evil_build/libc.so.evil"
|
||||
if cmake \
|
||||
"-DBUNDLE_ROOT=$evil_bundle" \
|
||||
"-DBUNDLE_RESOLVER_LIBRARIES=$evil_build/libevil-user.so" \
|
||||
"-DBUNDLE_SEARCH_DIRECTORIES=$other_lib" \
|
||||
"-DBUNDLE_SEED_FILES=$seed_lib/libstdc++.so.6" \
|
||||
"-DPATCHELF_EXECUTABLE=$(command -v patchelf)" \
|
||||
-P "$source_root/cmake/BundleRuntimeDependencies.cmake" \
|
||||
>/dev/null 2>&1; then
|
||||
echo "fake glibc-like SONAME was treated as a system dependency" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
report="$temporary/dependencies.txt"
|
||||
python3 "$source_root/scripts/validate-release-elf.py" \
|
||||
--root "$bundle" --max-glibc 2.35 --report "$report"
|
||||
grep -F $'NEEDED\tlib/Echoion.ts\tlibechoion-dependency.so.1\tbundled' "$report"
|
||||
grep -F $'NEEDED\tbin/echoiond\tlibc.so.6\tsystem' "$report"
|
||||
[[ -L $bundle/lib/libechoion-dependency.so.1 ]]
|
||||
[[ $(readlink "$bundle/lib/libechoion-dependency.so.1") != /* ]]
|
||||
[[ -f $bundle/lib/libechoion-dependency.so.1.2.3 ]]
|
||||
readelf -p .echoion_variant \
|
||||
"$bundle/lib/libechoion-dependency.so.1.2.3" | grep -F preferred-runtime
|
||||
if readelf -p .echoion_variant \
|
||||
"$bundle/lib/libechoion-dependency.so.1.2.3" | grep -Fq other-runtime; then
|
||||
echo "search-directory runtime replaced the exact build resolver runtime" >&2
|
||||
exit 1
|
||||
fi
|
||||
readelf -p .echoion_compiler_variant \
|
||||
"$bundle/lib/libstdc++.so.6.0.100" | grep -F seeded-compiler-runtime
|
||||
if find "$bundle/lib" -maxdepth 1 -name 'libstdc++.so.6.0.200' | grep -q .; then
|
||||
echo "resolver compiler runtime leaked beside the exact seed" >&2
|
||||
exit 1
|
||||
fi
|
||||
if find "$bundle/lib" -maxdepth 1 -name 'libc.so*' | grep -q .; then
|
||||
echo "glibc was unexpectedly bundled" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
patchelf --set-rpath /tmp/unsafe-build-path "$bundle/lib/Echoion.ts"
|
||||
if python3 "$source_root/scripts/validate-release-elf.py" \
|
||||
--root "$bundle" >/dev/null 2>&1; then
|
||||
echo "absolute Core RUNPATH unexpectedly passed validation" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
patchelf --set-rpath "\$ORIGIN" "$bundle/lib/Echoion.ts"
|
||||
python3 - "$bundle/lib/Echoion.ts" <<'PY'
|
||||
import sys
|
||||
|
||||
with open(sys.argv[1], "r+b") as stream:
|
||||
stream.seek(18)
|
||||
stream.write(b"\xb7\x00") # EM_AARCH64 in a little-endian ELF header.
|
||||
PY
|
||||
if python3 "$source_root/scripts/validate-release-elf.py" \
|
||||
--root "$bundle" >/dev/null 2>&1; then
|
||||
echo "non-x86-64 Core unexpectedly passed validation" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "runtime dependency bundling regressions passed"
|
||||
Reference in new issue
Block a user