|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: llvm unix-shell |
| 3 | +# 1053 -- a Mach-O image's C library is up before the program's initialisers |
| 4 | +# (2026.11.9.1). |
| 5 | +# |
| 6 | +# The loader of this format runs an image's initialisers before its entry, in |
| 7 | +# the order the linker placed their objects. A C library of the graph |
| 8 | +# (`mcpp:c-abi`: openkal-musl, in openkal-platform's hosted arrangement) is |
| 9 | +# brought up by an initialiser of its own; the program's objects came first on |
| 10 | +# the link line, and a global constructor that allocated -- the registration of |
| 11 | +# an mtest case -- stopped in an allocator not yet started (mcpplibs/os#22, |
| 12 | +# hosted macOS, EXC_BAD_ACCESS in mimalloc). The C library's objects now lead |
| 13 | +# the link, as a C runtime's start objects do on ELF. |
| 14 | +# |
| 15 | +# Criteria: |
| 16 | +# A. In the image built for `aarch64-macos-musl` over the hosted arrangement, the |
| 17 | +# C library's initialiser (`okm_early_init`, openkal-musl's |
| 18 | +# port/src/mach/early_init.c) precedes the program's own. |
| 19 | +# B. On an arm64 macOS host the program runs, its global constructor |
| 20 | +# allocating before `main`. |
| 21 | +set -e |
| 22 | +source "$(dirname "${BASH_SOURCE[0]}")/_toolchain_env.sh" |
| 23 | + |
| 24 | +TMP=$(mktemp -d) |
| 25 | +trap 'rm -rf "$TMP"' EXIT |
| 26 | +fail() { echo "FAIL: $1"; shift; for f in "$@"; do echo "--- $f ---"; cat "$f" 2>/dev/null; done; exit 1; } |
| 27 | + |
| 28 | +mkdir -p "$TMP/app/src" |
| 29 | +cd "$TMP/app" |
| 30 | +printf '[package]\nname = "early"\nversion = "0.1.0"\n' > mcpp.toml |
| 31 | +cat > src/main.cpp <<'CPP' |
| 32 | +import std; |
| 33 | +// A global constructor that allocates, as a test framework's registration does. |
| 34 | +std::vector<std::string>* registered = new std::vector<std::string>{std::string(64, 'x')}; |
| 35 | +int main() { |
| 36 | + std::println("registered {}", registered->front().size()); |
| 37 | + return 0; |
| 38 | +} |
| 39 | +CPP |
| 40 | + |
| 41 | +"$MCPP" build --target aarch64-macos-musl > build.log 2>&1 || fail "the hosted macOS build" build.log |
| 42 | +grep -q "openkal-platform" build.log || { |
| 43 | + echo "SKIP: aarch64-macos-musl is not served by openkal-platform's hosted arrangement here" |
| 44 | + exit 0 |
| 45 | +} |
| 46 | +bin=$(find target -path '*/bin/early' -type f | head -1) |
| 47 | +[ -n "$bin" ] || fail "no image was produced" build.log |
| 48 | + |
| 49 | +# A |
| 50 | +objdump="$_e2e_registry_base/xim-x-llvm/${LLVM_VERSION}/bin/llvm-objdump" |
| 51 | +nm="$_e2e_registry_base/xim-x-llvm/${LLVM_VERSION}/bin/llvm-nm" |
| 52 | +[ -x "$objdump" ] && [ -x "$nm" ] || { echo "SKIP(A): no llvm@${LLVM_VERSION} payload to read the image with"; objdump=""; } |
| 53 | +if [ -n "$objdump" ]; then |
| 54 | + python3 - "$bin" "$objdump" "$nm" > a.log 2>&1 <<'PY' || fail "A: the order of the image's initialisers" a.log build.log |
| 55 | +import subprocess, sys |
| 56 | +image, objdump, nm = sys.argv[1:4] |
| 57 | +dump = subprocess.run([objdump, "--macho", "-s", "-j", "__init_offsets", image], |
| 58 | + capture_output=True, text=True, check=True).stdout |
| 59 | +words = [] |
| 60 | +for line in dump.splitlines(): |
| 61 | + parts = line.split() |
| 62 | + if len(parts) < 2 or not all(c in "0123456789abcdef" for c in parts[0]): |
| 63 | + continue |
| 64 | + for w in parts[1:5]: |
| 65 | + if len(w) == 8 and all(c in "0123456789abcdef" for c in w): |
| 66 | + words.append(int.from_bytes(bytes.fromhex(w), "little")) |
| 67 | +symbols = [] |
| 68 | +for line in subprocess.run([nm, "-n", image], capture_output=True, text=True, check=True).stdout.splitlines(): |
| 69 | + parts = line.split() |
| 70 | + if len(parts) == 3: |
| 71 | + symbols.append((int(parts[0], 16), parts[2])) |
| 72 | +def name_at(offset): |
| 73 | + address = 0x100000000 + offset |
| 74 | + best = None |
| 75 | + for value, name in symbols: |
| 76 | + if value > address: |
| 77 | + break |
| 78 | + best = name |
| 79 | + return best or "?" |
| 80 | +names = [name_at(o) for o in words] |
| 81 | +print("initialisers:", names[:8]) |
| 82 | +assert any("okm_early_init" in n for n in names), "the C library's initialiser is not in the image" |
| 83 | +early = next(i for i, n in enumerate(names) if "okm_early_init" in n) |
| 84 | +program = [i for i, n in enumerate(names) if "main.cpp" in n] |
| 85 | +assert program, "the program's initialiser is not in the image" |
| 86 | +assert early < program[0], f"the program's initialiser runs at {program[0]}, before the C library's at {early}" |
| 87 | +PY |
| 88 | + echo " ok the C library's initialiser precedes the program's" |
| 89 | +fi |
| 90 | + |
| 91 | +# B |
| 92 | +if [ "$(uname -s)-$(uname -m)" = Darwin-arm64 ]; then |
| 93 | + "$bin" > b.log 2>&1 || fail "B: the image ended with $?" b.log |
| 94 | + grep -q "registered 64" b.log || fail "B: the program's output" b.log |
| 95 | + echo " ok the program runs, its constructor allocating before main" |
| 96 | +fi |
| 97 | + |
| 98 | +echo "OK: a Mach-O image's C library is up before its initialisers" |
0 commit comments