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
8 changes: 8 additions & 0 deletions Documentation/rust/bindgen-layout-checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ builds. Packagers prepare this entry point in their build roots. Make does not
fetch tools or install anything system-wide. If the entry point is absent,
Kbuild uses ``bindgen`` from PATH and the same mandatory assertions apply.

``make LLVM=1 rustavailable`` probes the selected generator's empty-record
alignment support, including locally patched versions. An affected generator
prints the preparation command above. This is a warning so it cannot silently
disable Rust drivers during configuration; the layout assertions still reject
incorrect bindings when compiling the kernel. Prepare the tool before running
``defconfig`` and merging ``asahi.config``, then keep the resolved configuration
unchanged for the kernel build.

The generated bindings are consumed by the Rust kernel crate and by several
drivers. After changing the bindings generator, rebuild the kernel Image and
all modules together.
Expand Down
26 changes: 26 additions & 0 deletions scripts/rust_is_available.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ if [ "$bindgen_libclang_cversion" -ge 2200000 ] &&
fi
fi

# Check the behavior, not the version: distributions can backport the repair.
# Warn rather than making CONFIG_RUST unavailable, since that would silently
# remove Rust drivers from a configuration. The kernel's layout assertions
# remain the final check for every generated type.
bindgen_alignment_output=$( \
LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_alignment.h \
--rust-target 1.85 2>/dev/null
) || bindgen_alignment_code=$?
if [ -n "$bindgen_alignment_code" ] ||
! echo "$bindgen_alignment_output" | grep -Eq '#\[repr\(.*align\(64\)'; then
echo >&2 "***"
if [ -n "$bindgen_alignment_code" ]; then
echo >&2 "*** Could not check empty struct alignment with '$BINDGEN'."
echo >&2 "*** Exit code: $bindgen_alignment_code"
else
echo >&2 "*** Rust bindings generator '$BINDGEN'"
echo >&2 "*** does not preserve empty struct alignment."
fi
echo >&2 "*** Default Asahi builds can fail the sched_statistics layout assertion."
echo >&2 "*** Prepare the corrected generator before configuring the kernel:"
echo >&2 "*** python3 tools/aurora-bindgen/build.py"
echo >&2 "*** See Documentation/rust/bindgen-layout-checks.rst for details."
echo >&2 "***"
warning=1
fi

# If the C compiler is Clang, then we can also check whether its version
# matches the `libclang` version used by the Rust bindings generator.
#
Expand Down
3 changes: 3 additions & 0 deletions scripts/rust_is_available_bindgen_alignment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* A defined GNU C empty record still has alignment, despite having no storage. */
struct rust_bindgen_aligned_empty {} __attribute__((aligned(64)));
39 changes: 37 additions & 2 deletions scripts/rust_is_available_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TestRustIsAvailable(unittest.TestCase):
class Expected(enum.Enum):
SUCCESS = enum.auto()
SUCCESS_WITH_WARNINGS = enum.auto()
SUCCESS_WITH_OPTIONAL_WARNINGS = enum.auto()
SUCCESS_WITH_EXTRA_OUTPUT = enum.auto()
FAILURE = enum.auto()

Expand Down Expand Up @@ -54,7 +55,8 @@ def generate_rustc(cls, stdout):
""")

@classmethod
def generate_bindgen(cls, version_stdout, libclang_stderr, libclang_22_patched=False):
def generate_bindgen(cls, version_stdout, libclang_stderr, libclang_22_patched=False,
empty_alignment=True):
if libclang_stderr is None:
libclang_case = f"raise SystemExit({cls.bindgen_default_bindgen_libclang_failure_exit_code})"
else:
Expand All @@ -71,6 +73,8 @@ def generate_bindgen(cls, version_stdout, libclang_stderr, libclang_22_patched=F
{libclang_case}
elif "rust_is_available_bindgen_libclang_22.h" in " ".join(sys.argv):
{libclang_22_case}
elif "rust_is_available_bindgen_alignment.h" in " ".join(sys.argv):
print({repr('#[repr(C, align(64))]' if empty_alignment else '#[repr(C)]')})
else:
print({repr(version_stdout)})
""")
Expand Down Expand Up @@ -142,6 +146,10 @@ def run_script(self, expected, override_env):
# be checked further by the test).
self.assertEqual(result.returncode, 0)
self.assertNotEqual(result.stderr, b"")
elif expected == self.Expected.SUCCESS_WITH_OPTIONAL_WARNINGS:
self.assertEqual(result.returncode, 0)
if result.stderr:
self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
elif expected == self.Expected.SUCCESS_WITH_WARNINGS:
# When expecting a success with warnings, the script should return 0
# and it should output at least the instructions to `stderr`.
Expand Down Expand Up @@ -338,6 +346,33 @@ def test_success_bindgen_version(self):
bindgen = self.generate_bindgen_version(bindgen_stdout)
result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })

def test_bindgen_empty_alignment_unpatched(self):
# The same version can be patched by a distribution, so test behavior.
bindgen = self.generate_bindgen("bindgen 0.73.2",
self.bindgen_default_bindgen_libclang_stderr,
empty_alignment=False)
result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
self.assertIn("does not preserve empty struct alignment", result.stderr)
self.assertIn("python3 tools/aurora-bindgen/build.py", result.stderr)

def test_bindgen_empty_alignment_patched(self):
bindgen = self.generate_bindgen("bindgen 0.73.2",
self.bindgen_default_bindgen_libclang_stderr)
self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })

def test_bindgen_empty_alignment_probe_failure(self):
bindgen = self.generate_executable(f'''#!/usr/bin/env python3
import os
import sys
if "rust_is_available_bindgen_alignment.h" in " ".join(sys.argv):
print("#[repr(C, align(64))]")
print("probe failed", file=sys.stderr)
raise SystemExit(1)
os.execv({str(self.default_bindgen)!r}, [{str(self.default_bindgen)!r}] + sys.argv[1:])
''')
result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
self.assertIn("Could not check empty struct alignment", result.stderr)

def test_success_bindgen_libclang(self):
for stderr in (
f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1) [-W#pragma-messages], err: false",
Expand Down Expand Up @@ -370,7 +405,7 @@ def test_success_clang_version(self):
def test_success_real_programs(self):
for cc in ["gcc", "clang"]:
with self.subTest(cc=cc):
result = self.run_script(self.Expected.SUCCESS, {
result = self.run_script(self.Expected.SUCCESS_WITH_OPTIONAL_WARNINGS, {
"PATH": os.environ["PATH"],
"RUSTC": "rustc",
"BINDGEN": "bindgen",
Expand Down