Files
DroidDeck/tools/components/reuse.py
T
LunaDLandKurt Himebauch 88d44b0dc6 Feat: Improve ARMv8.0 Steam compatibility with LSE emulation (#3720)
* feat: emulate LSE atomics so the Steam client starts on ARMv8.0 CPUs

* fix: keep NZCV across patched LSE min/max stubs and catch st* aliases

* fix: fail the LSE check when objdump fails

* fix: validate the CASP pair on a mismatch in the LSE emulator

* fix: sandboxes switch on the LSE emulator when it is preloaded

* fix: keep the MaliBridge pointer from main

* Handle LSE atomics before preload constructors

* Support early LSE handling in root guest processes

* Fix LSE-Emu signal and patching review findings

* Assert CASP and min/max sites use patched stubs

* Correct patch test statistics regexes

* Validate SIGILL syscall ABI and retain active state on restore failure

* Preserve the previous SIGILL disposition during early install

* Synchronize instruction fetch on generated LSE stubs

* Address LSE-Emu follow-up review findings

* Fix missing LSE signal handler state declarations

* Honor application signal mask when forwarding SIGILL

* Preserve SA_RESETHAND mask during handler forwarding

* Fix LSE opt-out after early handler install

* Avoid recursive SIGILL forwarding during LSE emulation

* Fix LSE SIGILL handler re-entry tracking

* Honor SA_NODEFER in forwarded SIGILL mask

* Preserve nested SIGILL delivery semantics

* Preserve explicit SIGILL mask with SA_NODEFER

* Reduce LSE patch contention across threads

* Guard nested faults before acquiring patch lock

* Fix early SIGILL restore and wake patch waiters

* Preserve libc restorer when restoring application SIGILL

* Wait for concurrent LSE patch instead of skipping

* Fix LSE patch lock release and fork recovery

* Avoid reinstalling the early SIGILL handler

* chore: retrigger CI

* Avoid proc maps reads in concurrent SIGILL patching

* Prevent late IFUNC handler install when emulation is disabled

* Clarify cached mapping lookup

* Move cached map declarations before find_gap

* Default LSE instruction patching to off

* Document conservative LSE emulation default

* Keep LSE instruction patching opt-in in sandbox preload

* Document default and opt-in LSE emulation checks

* Assert LSE patching remains opt-in in sandbox tests

* Align LSE device checks with SDK 34 patching behavior

* Check expected mprotect failures in opt-in device run

* Clarify LSE emulator device validation criteria

* Specify controlled SDK mprotect validation

* fix(lse-emu): publish SIGILL actions atomically

* fix(runtime): honor LSE_EMU=0 before preload

* test(runtime): keep LSE preload disabled when opted out

* fix(runtime): fully honor LSE_EMU=0 opt-out

* test(runtime): verify LSE opt-out removes inherited preload

* fix(runtime): filter inherited LSE preload without preload file

* fix(lse-emu): use bounded signal-safe action snapshots

* fix(runtime): split inherited preloads on spaces and colons

* test(runtime): cover both LSE preload entry orders

* fix(lse-emu): recycle displaced SIGILL snapshots

* fix(preload): preserve colliding path cache entries

* test(lse-emu): report patch failure details

* fix(lse-emu): serialize action snapshot publishers

* fix(lse-emu): fall back to reachable arena mapping

* fix(lse-emu): make signal action publication nonblocking

* refactor(lse-emu): remove obsolete publisher lock

* Fix SIGILL snapshot retirement and honor session LSE opt-out

* Preserve the Java staging API and isolate the SIGILL stress test

* fix(nativeguest): filter LSE preload when disabled

* fix(lse-emu): tighten seccomp and signal setup

* fix(session): preserve active LSE override while staging

* fix(lse-emu): return atomically displaced signal actions

* fix(lse-emu): stage raw signal masks safely

* fix(lse-emu): distinguish seccomp listener success

* fix(lse-emu): publish seccomp veto atomically

* fix(lse-emu): fail closed on RX restore errors

* fix(lse-emu): keep stack-using atomic stubs on trap path

* fix(lse-emu): allow patching with inherited seccomp

* test(lse-emu): cover LDAPR patch path

* test(lse-emu): assert LDAPR-only patching

* fix(nativeguest): filter disabled LSE from preload chains

* docs(lse-emu): define inherited seccomp patch policy

* fix(lse-emu): emulate LDAPR trap fallback

* refactor(lse-emu): remove unused seccomp query

* fix(nativeguest): release filtered preload chain in scope

* test(lse-emu): allow forwarded udf diagnostic

* fix(nativeguest): avoid unsafe preload fallback when disabled

* test(lse-emu): match full zero instruction field

* test(lse-emu): match complete instruction diagnostics

* Keep nested native preloads from duplicating host libraries

* fix(nativeguest): fail exec when disabled preload cannot map

* fix(nativeguest): stop exec when preload mapping fails

* fix(nativeguest): propagate preload filter allocation failures

* Consume only the selected one-shot SIGILL action

* Reclaim abandoned SIGILL action slots after fork

* Revalidate the live file mapping before optional LSE patching

---------

Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
2026-12-05 19:09:28 -05:00

151 lines
6.5 KiB
Python

"""Install the components a main CI run prepared when they match this tree.
The run's prepared-components archive is used only when every recipe identity it records equals
the identity of the local tree, the inputs CI builds outside the recipes are unchanged from the
run's source tree, and no recipe submodule has local edits. Files are verified in a staging
directory before any local output is replaced.
"""
import argparse
from contextlib import contextmanager
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import shutil
import subprocess
import sys
import tarfile
import tempfile
sys.path.insert(0, str(Path(__file__).resolve().parent))
import prepare # noqa: E402
# CI records identities under its runner labels and pinned NDK, not this host's.
RUNNER_ARCHITECTURES = ('X64', 'ARM64')
# The components job builds these outside the recipe registry.
UNREGISTERED_INPUTS = ['.github/workflows/build.yml', 'tools/linuxfs/xclipse-layer', 'tools/linuxfs/lse-emu',
'tools/venus/glprobe.c', 'tools/quest']
def ci_environment(architecture, root):
ndk = next(line.split('=', 1)[1].strip()
for line in (root / 'tools/ci/toolchains.env').read_text().splitlines()
if line.startswith('CI_ANDROID_NDK_VERSION='))
return dict(RUNNER_OS='Linux', RUNNER_ARCH=architecture, NDK_VERSION=ndk)
@contextmanager
def environ(values):
saved = {key: os.environ.get(key) for key in values}
os.environ.update(values)
try:
yield
finally:
for key, value in saved.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
def identities(environment, root):
with environ(environment):
return {name: prepare.identity(name, root) for name in prepare.registry(root)}
def git(root, *args):
return subprocess.run(['git', *args], cwd=root, capture_output=True, text=True)
def unregistered_changes(source_tree, root):
if git(root, 'cat-file', '-e', source_tree + '^{tree}').returncode:
return ['source tree ' + source_tree + ' (fetch origin main)']
changed = git(root, 'diff', '--name-only', source_tree, '--', *UNREGISTERED_INPUTS).stdout.split()
changed += git(root, 'ls-files', '--others', '--exclude-standard', '--', *UNREGISTERED_INPUTS).stdout.split()
return sorted(set(changed))
def edited_submodules(root):
inputs = [path for spec in prepare.registry(root).values()
for path in spec.get('inputs', []) + spec.get('source_inputs', [])]
staged = git(root, 'ls-files', '--stage', '--', *inputs).stdout.splitlines()
links = [line.split('\t', 1)[1] for line in staged if line.startswith('160000 ')]
return [link for link in links
if git(root, 'diff', '--quiet', '--ignore-submodules=none', '--', link).returncode]
def extract(archive_path, stage):
allowed = set(prepare.LIBRARIES + [prepare.MANIFEST])
total = 0
with tarfile.open(archive_path, mode='r:gz') as archive:
for entry in archive:
name = PurePosixPath(entry.name)
total += entry.size
if (not entry.isfile() or name.is_absolute() or '..' in name.parts or '\\' in entry.name
or name.as_posix() != entry.name or total > 2 * 1024**3
or not (entry.name in allowed or any(entry.name.startswith(t + '/') for t in prepare.TREES))):
raise ValueError('Unsafe prepared component archive: ' + entry.name)
path = stage / entry.name
path.parent.mkdir(parents=True, exist_ok=True)
with path.open('xb') as output:
shutil.copyfileobj(archive.extractfile(entry), output)
path.chmod(entry.mode & 0o777)
def digest(path):
with path.open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def install(stage, root):
for name in prepare.TREES:
shutil.rmtree(root / name, ignore_errors=True)
(root / name).parent.mkdir(parents=True, exist_ok=True)
os.replace(stage / name, root / name)
for name in prepare.LIBRARIES + [prepare.MANIFEST]:
(root / name).parent.mkdir(parents=True, exist_ok=True)
os.replace(stage / name, root / name)
def reuse(archive_path, root=prepare.ROOT):
with tarfile.open(archive_path, mode='r:gz') as archive:
data = json.load(archive.extractfile(prepare.MANIFEST))
if data.get('version') != 1:
raise ValueError('Unknown prepared component archive version')
recorded = data.get('recipes') or {}
environment = next((ci_environment(a, root) for a in RUNNER_ARCHITECTURES
if identities(ci_environment(a, root), root) == recorded), None)
if environment is None:
local = identities(ci_environment(RUNNER_ARCHITECTURES[0], root), root)
changed = sorted(name for name in local.keys() | recorded.keys() if local.get(name) != recorded.get(name))
print('CI run ' + str(data.get('run')) + ' prepared different components: ' + ', '.join(changed))
return False
differences = unregistered_changes(data['source_tree'], root) + edited_submodules(root)
if differences:
print('CI run ' + str(data.get('run')) + ' built from other sources: ' + ', '.join(differences))
return False
(root / 'app/build').mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(dir=root / 'app/build', prefix='.reuse-') as stage:
stage = Path(stage)
extract(archive_path, stage)
files = sorted(path.relative_to(stage).as_posix() for path in stage.rglob('*')
if path.is_file() and path.relative_to(stage).as_posix() != prepare.MANIFEST)
if data.get('files') != {name: digest(stage / name) for name in files}:
raise ValueError('Prepared components do not match their recorded file hashes')
missing = [name for name in prepare.TREES if not (stage / name).is_dir()]
missing += [name for name in prepare.LIBRARIES + [prepare.MANIFEST] if not (stage / name).is_file()]
if missing:
raise ValueError('Prepared component archive is missing ' + ', '.join(missing))
install(stage, root)
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('archive', type=Path)
args = parser.parse_args()
try:
sys.exit(0 if reuse(args.archive) else 3)
except (ValueError, OSError, KeyError, tarfile.TarError) as error:
sys.exit('Cannot reuse CI components: ' + str(error))