Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ meson setup build --buildtype=plain \
-Dskia=enabled -Dskia_prefix=/path/to/skia -Dvulkan=enabled -Dwayland=enabled
```

Attaching an app's own threads to bionic needs bionic's internal layout, measured from the
AOSP tree the bionic is staged from. By default the build measures it from `$AOSP` (else
`$ACL_AOSP_ROOT`, else `/mnt/dev/aosp`) and leaves attach out, without failing, when there is
no tree. A build made apart from that tree takes the layout as a file, and can require it:

```sh
tools/gen-bionic-offsets.sh --require bionic_offsets.h aarch64 # where the AOSP tree is
meson setup build -Dbionic_offsets=/path/to/bionic_offsets.h # where a4l is built
meson setup build -Dbionic_attach=enabled # or: no tree is an error
```

`meson install --skip-subprojects` installs a4l alone: `bin/a4l`, and with FFmpeg
`<libdir>/a4l/libsoundpool.so`, which a4l opens from there itself. Without the flag a
subproject built from a wrap installs its own files too (wayland-cxx-scanner's headers).
Expand Down
6 changes: 6 additions & 0 deletions meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ option('secret', type : 'feature', value : 'auto',
description : 'AndroidKeyStore on the Secret Service (libsecret).')
option('crypto', type : 'feature', value : 'auto',
description : 'APK signature verification (libcrypto 3 headers).')

# The bionic layout per-thread attach is built from (src/ndk/meson.build).
option('bionic_attach', type : 'feature', value : 'auto',
description : 'Per-thread attach of app threads to bionic, which needs bionic\'s internal layout. auto: when an AOSP tree is found at build time, else compiled out. enabled: no layout is an error. disabled: no tree is looked at.')
option('bionic_offsets', type : 'string', value : '',
description : 'A header from tools/gen-bionic-offsets.sh, run where the AOSP tree the bionic is staged from is. Set, nothing on the build machine is read for the layout.')
10 changes: 10 additions & 0 deletions src/ndk/bionic_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
// at build time, and attach then compiles out.
#include "bionic_offsets.h"

// The layout is one architecture's. A header measured for the other one
// compiles and then builds a TCB with every field in the wrong place.
#if ACL_BIONIC_OFFSETS_AVAILABLE
#if defined(__aarch64__) && !defined(ACL_BIONIC_OFFSETS_FOR_AARCH64)
#error "bionic_offsets.h was not measured for aarch64"
#elif defined(__x86_64__) && !defined(ACL_BIONIC_OFFSETS_FOR_X86_64)
#error "bionic_offsets.h was not measured for x86_64"
#endif
#endif

#include <atomic>
#include <cstdarg>
#include <cstdlib>
Expand Down
79 changes: 69 additions & 10 deletions src/ndk/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,75 @@ endif

# Per-thread attach builds a bionic TCB by hand, which needs bionic's INTERNAL struct layout.
# Measure it from the headers of the bionic we stage rather than hand-copying structs that drift
# (a moved field would not fail a build — it would corrupt the TCB). Without a usable $AOSP the
# script still succeeds, emitting ACL_BIONIC_OFFSETS_AVAILABLE 0, and attach compiles out.
# See tools/gen-bionic-offsets.sh + docs/ndk-scope.md.
bionic_offsets_h = custom_target(
'bionic-offsets-h',
output : 'bionic_offsets.h',
command : [files('../../tools/gen-bionic-offsets.sh'), '@OUTPUT@', host_machine.cpu_family()],
depend_files : files('../../tools/bionic_offsets_probe.cpp'),
build_by_default : true,
)
# (a moved field would not fail a build — it would corrupt the TCB). See
# tools/gen-bionic-offsets.sh + docs/ndk-scope.md. Where the layout comes from:
# -Dbionic_offsets=<file> that header, made by the script where the AOSP tree is. Nothing
# on this machine is read. For a4l built apart from the tree its
# bionic is staged from.
# otherwise the script, at build time, from $AOSP (else $ACL_AOSP_ROOT, else
# /mnt/dev/aosp). With no usable tree it writes
# ACL_BIONIC_OFFSETS_AVAILABLE 0 and attach compiles out; with
# -Dbionic_attach=enabled that is an error and not a quiet loss.
# -Dbionic_attach=disabled no layout, and no tree looked at.
bionic_attach = get_option('bionic_attach')
bionic_offsets_file = get_option('bionic_offsets')
bionic_for = host_machine.cpu_family() == 'aarch64' ? 'AARCH64' : 'X86_64'
if bionic_attach.disabled()
if bionic_offsets_file != ''
error('-Dbionic_offsets is given and -Dbionic_attach is disabled')
endif
bionic_offsets_h = custom_target(
'bionic-offsets-h',
output : 'bionic_offsets.h',
command : [files('../../tools/gen-bionic-offsets.sh'), '--disabled', '@OUTPUT@',
host_machine.cpu_family()],
build_by_default : true,
)
summary({'bionic thread attach' : 'disabled'})
elif bionic_offsets_file != ''
if not fs.is_file(bionic_offsets_file)
error('-Dbionic_offsets=' + bionic_offsets_file + ': no such file')
endif
_offsets = fs.read(bionic_offsets_file)
if not _offsets.contains('#define ACL_BIONIC_OFFSETS_AVAILABLE 1')
error('-Dbionic_offsets=' + bionic_offsets_file
+ ': not a header with offsets (ACL_BIONIC_OFFSETS_AVAILABLE 1); make it with tools/gen-bionic-offsets.sh --require')
endif
if not _offsets.contains('#define ACL_BIONIC_OFFSETS_FOR_' + bionic_for + ' 1')
error('-Dbionic_offsets=' + bionic_offsets_file + ': not measured for '
+ host_machine.cpu_family() + ' by this checkout\'s tools/gen-bionic-offsets.sh (no ACL_BIONIC_OFFSETS_FOR_'
+ bionic_for + ')')
endif
bionic_offsets_h = configure_file(
input : bionic_offsets_file,
output : 'bionic_offsets.h',
copy : true,
)
summary({'bionic thread attach' : 'offsets from ' + bionic_offsets_file})
else
if bionic_attach.enabled()
# Measured once here as well, so that a tree that is not there stops the configure
# with the reason, and not the build some minutes in.
_try = run_command(files('../../tools/gen-bionic-offsets.sh'), '--require',
meson.current_build_dir() / 'bionic_offsets.check.h',
host_machine.cpu_family(), check : false)
if _try.returncode() != 0
error('-Dbionic_attach=enabled: ' + _try.stderr().strip())
endif
endif
bionic_offsets_h = custom_target(
'bionic-offsets-h',
output : 'bionic_offsets.h',
command : [files('../../tools/gen-bionic-offsets.sh')]
+ (bionic_attach.enabled() ? ['--require'] : [])
+ ['@OUTPUT@', host_machine.cpu_family()],
depend_files : files('../../tools/bionic_offsets_probe.cpp'),
build_by_default : true,
)
summary({'bionic thread attach' : bionic_attach.enabled()
? 'required; offsets measured at build time from $AOSP'
: 'if $AOSP has a built tree (measured at build time)'})
endif

acl_ndk = static_library(
'acl_ndk',
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/bionic_offsets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ def main() -> int:
else:
sys.stdout.write("OK: no AOSP -> offsets unavailable, build stays green\n")

# 1b. --require: the same absence is an error, and leaves no header to build with.
out = os.path.join(work, "required.h")
p = subprocess.run([gen, "--require", out], env=dict(os.environ, AOSP="/nonexistent"),
capture_output=True, text=True, timeout=120)
if p.returncode == 0 or os.path.exists(out):
sys.stderr.write("FAIL: --require with no AOSP must fail and write no header\n")
ok = False
else:
sys.stdout.write("OK: --require with no AOSP fails\n")

# 1c. --disabled: says unavailable, whatever tree is there.
out = os.path.join(work, "disabled.h")
p = subprocess.run([gen, "--disabled", out], capture_output=True, text=True, timeout=120)
if p.returncode != 0 or parse(out).get("ACL_BIONIC_OFFSETS_AVAILABLE") != 0:
sys.stderr.write(f"FAIL: --disabled must write an unavailable header\n{p.stderr}\n")
ok = False
else:
sys.stdout.write("OK: --disabled -> offsets unavailable\n")

# 2. With AOSP (skip that half when there is none — CI has no checkout).
aosp = os.environ.get("AOSP", "/mnt/dev/aosp")
if not os.path.isdir(os.path.join(aosp, "bionic", "libc", "private")):
Expand All @@ -97,6 +116,19 @@ def main() -> int:
sys.stderr.write(f"FAIL: probe did not measure {aosp}/bionic:\n{p.stderr}\n")
return 1

# The header names the architecture it was measured for, which is what keeps one
# architecture's layout out of the other's build.
if defs.get("ACL_BIONIC_OFFSETS_FOR_X86_64") != 1 or "ACL_BIONIC_OFFSETS_FOR_AARCH64" in defs:
sys.stderr.write("FAIL: the x86_64 header must say it is x86_64's and only that\n")
ok = False
arm = os.path.join(work, "arm64.h")
pa = subprocess.run([gen, "--require", arm, "aarch64"], env=dict(os.environ, AOSP=aosp),
capture_output=True, text=True, timeout=300)
adefs = parse(arm) if pa.returncode == 0 else {}
if adefs.get("ACL_BIONIC_OFFSETS_FOR_AARCH64") != 1 or "ACL_BIONIC_OFFSETS_FOR_X86_64" in adefs:
sys.stderr.write(f"FAIL: the aarch64 header must say it is aarch64's\n{pa.stderr}\n")
ok = False

for name, want in EXPECTED.items():
got = defs.get(name)
if got != want:
Expand Down
36 changes: 30 additions & 6 deletions tools/gen-bionic-offsets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,41 @@
# refusing unattached threads, which is exactly the pre-existing behavior. That keeps a plain
# `meson setup && meson compile` green on a machine with no AOSP checkout.
#
# Usage: gen-bionic-offsets.sh <out.h>
# AOSP=/path/to/aosp AOSP checkout root (default /mnt/dev/aosp)
# Usage: gen-bionic-offsets.sh [--require | --disabled] <out.h> [x86_64|aarch64]
# --require no offsets is an error (exit 1), not a header that says so
# (-Dbionic_attach=enabled)
# --disabled write the header that says so and look at no tree
# (-Dbionic_attach=disabled)
# AOSP=/path/to/aosp AOSP checkout root (default $ACL_AOSP_ROOT, else /mnt/dev/aosp)
#
# A packager who builds a4l on a machine with no AOSP tree runs this where the tree is, for
# the architecture staged, ships <out.h> with the staged bionic and builds a4l with
# -Dbionic_offsets=<out.h>: offsets and bionic then come from one tree by construction.
set -euo pipefail

OUT="${1:?usage: gen-bionic-offsets.sh <out.h> [x86_64|aarch64]}"
REQUIRE=0
DISABLED=0
case "${1:-}" in
--require) REQUIRE=1; shift ;;
--disabled) DISABLED=1; shift ;;
esac
OUT="${1:?usage: gen-bionic-offsets.sh [--require | --disabled] <out.h> [x86_64|aarch64]}"
# The architecture a4l is being built for; the layout differs between them.
case "${2:-x86_64}" in
aarch64|arm64) TRIPLE=aarch64-linux-android31; ASM=asm-arm64 ;;
*) TRIPLE=x86_64-linux-android31; ASM=asm-x86 ;;
aarch64|arm64) TRIPLE=aarch64-linux-android31; ASM=asm-arm64; FOR=AARCH64 ;;
*) TRIPLE=x86_64-linux-android31; ASM=asm-x86; FOR=X86_64 ;;
esac
AOSP="${AOSP:-/mnt/dev/aosp}"
AOSP="${AOSP:-${ACL_AOSP_ROOT:-/mnt/dev/aosp}}"
HERE="$(cd "$(dirname "$0")" && pwd)"
CLANG="$AOSP/prebuilts/clang/host/linux-x86/clang-r450784d/bin/clang++"

unavailable() { # $1 = why
if [ "$REQUIRE" = 1 ]; then
echo "gen-bionic-offsets: $1" >&2
echo "gen-bionic-offsets: -Dbionic_attach=enabled needs the offsets: a built AOSP tree in" \
"\$AOSP, or a header made where one is, given with -Dbionic_offsets=<file>" >&2
exit 1
fi
mkdir -p "$(dirname "$OUT")"
cat >"$OUT" <<EOF
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -48,6 +68,7 @@ EOF
exit 0
}

[ "$DISABLED" = 0 ] || unavailable "turned off (-Dbionic_attach=disabled)"
[ -x "$CLANG" ] || unavailable "no AOSP clang at $CLANG"
[ -d "$AOSP/bionic/libc/private" ] || unavailable "no bionic sources under $AOSP"

Expand Down Expand Up @@ -97,6 +118,9 @@ mkdir -p "$(dirname "$OUT")"
echo "// Must match the bionic staged by tools/stage-bionic-runtime.sh (same \$AOSP)."
echo "#pragma once"
echo "#define ACL_BIONIC_OFFSETS_AVAILABLE 1"
# The architecture measured. src/ndk refuses to compile against the other one's, and
# -Dbionic_offsets refuses a header without this line.
echo "#define ACL_BIONIC_OFFSETS_FOR_$FOR 1"
echo "$DEFS"
} >"$OUT"
echo "gen-bionic-offsets: $(echo "$DEFS" | wc -l) offsets from $AOSP/bionic -> $OUT"