diff --git a/ci/ci-image/Dockerfile b/ci/ci-image/Dockerfile index 8ea01354ea..4c62c77aeb 100644 --- a/ci/ci-image/Dockerfile +++ b/ci/ci-image/Dockerfile @@ -26,7 +26,11 @@ RUN set -eu; \ while IFS= read -r runtime_library; do \ cp -L "$runtime_library" /swiftlint-output/opt/swiftlint/lib/; \ done < /swiftlint-input/runtime-libraries.txt -RUN python3 /swiftlint-input/producer.py collect-licenses +# Fail here, naming the missing lines, when the binary or a shipped library links +# a Swift toolchain library that runtime-libraries.txt does not copy. +COPY ci/scripts/swiftlint/verify-runtime-libraries.py /swiftlint-input/verify-runtime-libraries.py +RUN python3 /swiftlint-input/verify-runtime-libraries.py /swiftlint-output/opt/swiftlint /usr/lib/swift \ + && python3 /swiftlint-input/producer.py collect-licenses FROM debian:bookworm-slim@sha256:e67eeead736ff7bbe258ef088bff683635c561b1fcfefad0806c1df4ab49ce0e AS swiftlint-runtime # hadolint ignore=DL3008 diff --git a/ci/scripts/images/build-ci-image-core.ts b/ci/scripts/images/build-ci-image-core.ts index 16e6eb0426..9561840dc3 100644 --- a/ci/scripts/images/build-ci-image-core.ts +++ b/ci/scripts/images/build-ci-image-core.ts @@ -67,6 +67,7 @@ export function ciImageDefinition(name: string): CiImageDefinition { "ci/scripts/swiftlint/runtime-libraries.txt", "ci/scripts/swiftlint/runtime.sh", "ci/scripts/swiftlint/verify-capabilities.sh", + "ci/scripts/swiftlint/verify-runtime-libraries.py", "ci/scripts/android-sdk.sh", "packages/tasknotes-macos/.swiftlint.yml", "packages/tasknotes-macos/.swiftlint-appkit-baseline.json", diff --git a/ci/scripts/swiftlint/bootstrap.sh b/ci/scripts/swiftlint/bootstrap.sh index 5b0470340a..603e019ee1 100644 --- a/ci/scripts/swiftlint/bootstrap.sh +++ b/ci/scripts/swiftlint/bootstrap.sh @@ -51,6 +51,7 @@ for swiftlint_input in \ ci/scripts/swiftlint/runtime-libraries.txt \ ci/scripts/swiftlint/runtime.sh \ ci/scripts/swiftlint/verify-capabilities.sh \ + ci/scripts/swiftlint/verify-runtime-libraries.py \ packages/tasknotes-macos/.swiftlint.yml \ packages/tasknotes-macos/.swiftlint-appkit-baseline.json; do mkdir -p "$swiftlint_build/context/$(dirname "$swiftlint_input")" diff --git a/ci/scripts/swiftlint/runtime-libraries.txt b/ci/scripts/swiftlint/runtime-libraries.txt index 14838d1931..bff42c0f23 100644 --- a/ci/scripts/swiftlint/runtime-libraries.txt +++ b/ci/scripts/swiftlint/runtime-libraries.txt @@ -12,6 +12,7 @@ /usr/lib/swift/host/compiler/lib_CompilerSwiftSyntaxBuilder.so /usr/lib/swift/host/compiler/lib_CompilerSwiftSyntaxMacroExpansion.so /usr/lib/swift/host/compiler/lib_CompilerSwiftSyntaxMacros.so +/usr/lib/swift/host/compiler/lib_CompilerSwiftWarningControl.so /usr/lib/swift/linux/libBlocksRuntime.so /usr/lib/swift/linux/libFoundation.so /usr/lib/swift/linux/libFoundationEssentials.so @@ -23,6 +24,7 @@ /usr/lib/swift/linux/libswiftCore.so /usr/lib/swift/linux/libswiftDispatch.so /usr/lib/swift/linux/libswiftGlibc.so +/usr/lib/swift/linux/libswiftObservation.so /usr/lib/swift/linux/libswiftSynchronization.so /usr/lib/swift/linux/libswift_Builtin_float.so /usr/lib/swift/linux/libswift_Concurrency.so diff --git a/ci/scripts/swiftlint/verify-runtime-libraries.py b/ci/scripts/swiftlint/verify-runtime-libraries.py new file mode 100644 index 0000000000..f90d7a3b6e --- /dev/null +++ b/ci/scripts/swiftlint/verify-runtime-libraries.py @@ -0,0 +1,77 @@ +"""Fail the builder stage when the SwiftLint runtime omits a Swift toolchain library it links.""" + +from __future__ import annotations + +import os +import pathlib +import subprocess +import sys + +ELF_MAGIC = b"\x7fELF" + + +def is_elf(path: pathlib.Path) -> bool: + with path.open("rb") as handle: + return handle.read(4) == ELF_MAGIC + + +def needed(path: pathlib.Path) -> list[str]: + """DT_NEEDED sonames, read statically so the result never depends on the loader cache.""" + result = subprocess.run( + [os.environ.get("READELF", "readelf"), "--dynamic", str(path)], + check=True, + capture_output=True, + text=True, + ) + return [ + line.split("[", 1)[1].split("]", 1)[0] + for line in result.stdout.splitlines() + if "(NEEDED)" in line + ] + + +def toolchain_libraries(roots: list[pathlib.Path]) -> dict[str, pathlib.Path]: + found: dict[str, pathlib.Path] = {} + for root in roots: + for path in sorted(root.rglob("*.so")): + found.setdefault(path.name, path) + return found + + +def missing_toolchain_libraries( + output: pathlib.Path, toolchain_roots: list[pathlib.Path] +) -> dict[str, pathlib.Path]: + """Transitive closure of toolchain libraries the shipped files need but do not ship.""" + toolchain = toolchain_libraries(toolchain_roots) + shipped = {path.name for path in (output / "lib").iterdir()} + queue = [ + path + for directory in (output / "bin", output / "lib") + for path in sorted(directory.iterdir()) + if path.is_file() and is_elf(path) + ] + missing: dict[str, pathlib.Path] = {} + while queue: + for soname in needed(queue.pop()): + if soname in toolchain and soname not in shipped and soname not in missing: + missing[soname] = toolchain[soname] + queue.append(toolchain[soname].resolve()) + return missing + + +if __name__ == "__main__": + if len(sys.argv) < 3: + raise SystemExit("Usage: verify-runtime-libraries.py ...") + absent = missing_toolchain_libraries( + pathlib.Path(sys.argv[1]), [pathlib.Path(root) for root in sys.argv[2:]] + ) + if absent: + print( + "SwiftLint links Swift toolchain libraries that runtime-libraries.txt does not ship." + " Add these lines to ci/scripts/swiftlint/runtime-libraries.txt:", + file=sys.stderr, + ) + for path in sorted(absent.values()): + print(path, file=sys.stderr) + raise SystemExit(1) + print("Every Swift toolchain library linked by SwiftLint is shipped in its runtime.") diff --git a/ci/scripts/swiftlint/verify-runtime-libraries.test.sh b/ci/scripts/swiftlint/verify-runtime-libraries.test.sh new file mode 100755 index 0000000000..030298bedb --- /dev/null +++ b/ci/scripts/swiftlint/verify-runtime-libraries.test.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +set -euo pipefail +swiftlint_test_root=$(mktemp -d) +trap 'rm -rf "$swiftlint_test_root"' EXIT +swiftlint_test_script=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/verify-runtime-libraries.py + +# A fake readelf prints the NEEDED entries recorded for each file's basename. +cat >"$swiftlint_test_root/readelf" <<'SH' +#!/usr/bin/env bash +file=$(basename "$2") +if [ -f "$FAKE_NEEDED/$file" ]; then + while IFS= read -r soname; do + printf ' 0x0000000000000001 (NEEDED) Shared library: [%s]\n' "$soname" + done <"$FAKE_NEEDED/$file" +fi +SH +chmod +x "$swiftlint_test_root/readelf" +export READELF="$swiftlint_test_root/readelf" FAKE_NEEDED="$swiftlint_test_root/needed" + +swiftlint_test_case() { + rm -rf "$swiftlint_test_root/out" "$swiftlint_test_root/toolchain" "$FAKE_NEEDED" + mkdir -p "$swiftlint_test_root/out/bin" "$swiftlint_test_root/out/lib" \ + "$swiftlint_test_root/toolchain/linux" "$FAKE_NEEDED" + for toolchain_library in libswiftCore.so libswiftObservation.so libswiftChained.so; do + printf '\177ELF' >"$swiftlint_test_root/toolchain/linux/$toolchain_library" + done + printf '\177ELF' >"$swiftlint_test_root/out/bin/swiftlint" + printf '\177ELF' >"$swiftlint_test_root/out/lib/libswiftCore.so" + printf 'not an elf file\n' >"$swiftlint_test_root/out/lib/notes.txt" +} +swiftlint_test_run() { + python3 "$swiftlint_test_script" "$swiftlint_test_root/out" "$swiftlint_test_root/toolchain" \ + >"$swiftlint_test_root/log" 2>&1 +} + +# Accepted: Swift libraries shipped, system libraries left to the runtime image. +swiftlint_test_case +printf 'libswiftCore.so\nlibc.so.6\nlibcurl.so.4\n' >"$FAKE_NEEDED/swiftlint" +swiftlint_test_run + +# Rejected: the binary links a toolchain library that is not shipped. +swiftlint_test_case +printf 'libswiftCore.so\nlibswiftObservation.so\n' >"$FAKE_NEEDED/swiftlint" +if swiftlint_test_run; then + echo "SwiftLint runtime check accepted an unshipped Swift library" >&2 + exit 1 +fi +rg -Fq "toolchain/linux/libswiftObservation.so" "$swiftlint_test_root/log" + +# Rejected: a missing library's own toolchain dependencies are reported together. +swiftlint_test_case +printf 'libswiftObservation.so\n' >"$FAKE_NEEDED/libswiftCore.so" +printf 'libswiftChained.so\n' >"$FAKE_NEEDED/libswiftObservation.so" +printf 'libswiftCore.so\n' >"$FAKE_NEEDED/swiftlint" +if swiftlint_test_run; then + echo "SwiftLint runtime check accepted an unshipped transitive Swift library" >&2 + exit 1 +fi +rg -Fq "libswiftObservation.so" "$swiftlint_test_root/log" +rg -Fq "libswiftChained.so" "$swiftlint_test_root/log" +echo "SwiftLint runtime library controls passed: three cases" diff --git a/ci/scripts/toolchain.test.sh b/ci/scripts/toolchain.test.sh index 8d600c1e08..69074915da 100644 --- a/ci/scripts/toolchain.test.sh +++ b/ci/scripts/toolchain.test.sh @@ -103,6 +103,7 @@ if ! rg -Fq 'bash ci/scripts/swiftlint/bootstrap.sh' "$TOOLCHAIN" || exit 1 fi bash "${SCRIPT_DIR}/swiftlint/verify-capabilities.test.sh" +bash "${SCRIPT_DIR}/swiftlint/verify-runtime-libraries.test.sh" bash "${SCRIPT_DIR}/swiftlint/install-runtime.test.sh" bash "${SCRIPT_DIR}/swiftlint/extract-source.test.sh" python3 "${SCRIPT_DIR}/swiftlint/validate-inputs.py" "${SCRIPT_DIR}/../.." diff --git a/scripts/script-migrations.json b/scripts/script-migrations.json index 788afd174b..950c99dc91 100644 --- a/scripts/script-migrations.json +++ b/scripts/script-migrations.json @@ -782,6 +782,11 @@ "disposition": "retain", "reason": "Fast isolated boundary controls reject wrong versions, tool crashes, skipped rules and unknown-rule acceptance without building containers or launching apps." }, + { + "source": "ci/scripts/swiftlint/verify-runtime-libraries.test.sh", + "disposition": "retain", + "reason": "Fast isolated controls with a synthetic readelf prove the producer rejects a linked-but-unshipped Swift toolchain library, directly or transitively, without building containers." + }, { "source": "ci/scripts/swiftlint/extract-source.test.sh", "disposition": "retain",