mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
Merge remote-tracking branch 'origin/main' into fix/steam-compat-pick-wins
This commit is contained in:
commit
a4abb797a7
140 files changed
+6314
-836
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
missing=()
|
||||
for package in "$@"; do
|
||||
if [ "$(dpkg-query -W -f='${Status}' "$package" 2>/dev/null || true)" != 'install ok installed' ]; then
|
||||
missing+=("$package")
|
||||
fi
|
||||
done
|
||||
if [ "${#missing[@]}" = 0 ]; then
|
||||
echo "Ubuntu build dependencies are already installed."
|
||||
exit 0
|
||||
fi
|
||||
options=(
|
||||
-o Dir::Etc::sourcelist=sources.list.d/ubuntu.sources
|
||||
-o Dir::Etc::sourceparts=-
|
||||
-o Acquire::Retries=1
|
||||
-o Acquire::http::Timeout=15
|
||||
-o Acquire::https::Timeout=15
|
||||
-o DPkg::Lock::Timeout=30
|
||||
)
|
||||
install_packages() {
|
||||
echo "Updating Ubuntu package indexes."
|
||||
sudo timeout --kill-after=10s 60s apt-get "${options[@]}" -o APT::Update::Error-Mode=any update -q || return $?
|
||||
echo "Installing: ${missing[*]}"
|
||||
sudo timeout --kill-after=10s 120s env DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get "${options[@]}" install -y -q --no-install-recommends "${missing[@]}"
|
||||
}
|
||||
if install_packages; then
|
||||
exit 0
|
||||
fi
|
||||
echo "::warning::Runner mirror failed or timed out; retrying with the official Ubuntu mirror."
|
||||
sources=$(mktemp --suffix=.sources)
|
||||
trap 'rm -f "$sources"' EXIT
|
||||
mirror=https://archive.ubuntu.com/ubuntu
|
||||
case "$(dpkg --print-architecture)" in
|
||||
arm64) mirror=https://ports.ubuntu.com/ubuntu-ports ;;
|
||||
esac
|
||||
sed -E "s@^URIs:.*@URIs: $mirror@" /etc/apt/sources.list.d/ubuntu.sources > "$sources"
|
||||
chmod 644 "$sources"
|
||||
options[1]="Dir::Etc::sourcelist=$sources"
|
||||
install_packages
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
repo=$(cd "$(dirname "$0")/../.." && pwd)
|
||||
source "$repo/tools/ci/toolchains.env"
|
||||
ndk="$ANDROID_HOME/ndk/$CI_ANDROID_NDK_VERSION"
|
||||
if [ ! -f "$ndk/source.properties" ]; then
|
||||
sdkmanager --sdk_root="$ANDROID_HOME" --install "ndk;$CI_ANDROID_NDK_VERSION"
|
||||
fi
|
||||
actual=$(sed -n 's/^Pkg.Revision *= *//p' "$ndk/source.properties" | tr -d '\r')
|
||||
test "$actual" = "$CI_ANDROID_NDK_VERSION"
|
||||
{
|
||||
echo "NDK_VERSION=$actual"
|
||||
echo "ANDROID_NDK_HOME=$ndk"
|
||||
echo "ANDROID_NDK_ROOT=$ndk"
|
||||
echo "ANDROID_NDK_LATEST_HOME=$ndk"
|
||||
} >> "$GITHUB_ENV"
|
||||
@@ -0,0 +1 @@
|
||||
CI_ANDROID_NDK_VERSION=30.0.16248370
|
||||
@@ -0,0 +1,65 @@
|
||||
# Runtime component preparation
|
||||
|
||||
`recipes.json` owns component source inputs, builders, outputs, package requirements
|
||||
and pinned downloads. `prepare.py` runs those recipes, calculates cache identities,
|
||||
installs the union of missing tools and verifies the prepared artifact handoff.
|
||||
Builders live in `recipes/`; the workflow describes scheduling and gates.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Source[Pin source tree] --> Components[Prepare verified components]
|
||||
Source --> Xbox[Xbox ARM64 backend]
|
||||
Source --> Tests[Python and native tests]
|
||||
Components --> APK[Compile APK and Android tests]
|
||||
Xbox --> APK
|
||||
APK --> Gate[Compile and test APK gate]
|
||||
Tests --> Gate
|
||||
Gate --> Publish[Main only: signing and publishing]
|
||||
```
|
||||
|
||||
All workers verify the same source tree. The component artifact records that tree,
|
||||
recipe identities and every output hash; the APK worker verifies the inventory
|
||||
before building. This handoff verifies consistency within the same CI run. It is
|
||||
not independent provenance for arbitrary artifacts. Source policy checks, tests,
|
||||
APK content/ABI checks and signing/publication gates remain in place.
|
||||
|
||||
`tools/ci/toolchains.env` pins the Android NDK shared by component, APK and native
|
||||
test workers. Linux builds use Ubuntu 24.04. Recipe and input changes invalidate
|
||||
the relevant component cache; ordinary caches remain performance aids.
|
||||
|
||||
## Verified preload snapshots
|
||||
|
||||
`preloads.lock.json` pins a small immutable preload archive. It records each
|
||||
binary's hash, its producing main CI run, the original APK artifact identity and
|
||||
digest, source/header hashes, the ARM64 build recipe and x86 compiler-image inputs.
|
||||
It covers core ARM64 preloads and the x86 preload libraries. VR, Android native
|
||||
libraries, Comet and Xbox use their registered recipes and existing release pins
|
||||
or component caches.
|
||||
|
||||
Every build compares current source inputs, including transitive quoted headers,
|
||||
and build recipes with this manifest. Changed groups rebuild from source. A
|
||||
missing release also selects a rebuild. Corrupt archives, unexpected files and
|
||||
binary hash mismatches fail the build. Existing preload checks, runtime packaging
|
||||
checks and all test jobs still run.
|
||||
|
||||
The trust anchor is the manifest in the pinned main base. A new or changed PR
|
||||
manifest is independently checked against a successful `push` build of main:
|
||||
GitHub must confirm the run/source/workflow and original APK artifact digest, and
|
||||
every claimed binary must match that APK. The claimed source inputs and recipe
|
||||
must match that run's Git source. A PR cannot establish trust by changing both a
|
||||
binary and its claimed checksum. This is approved-build reuse, not a claim of
|
||||
bit-for-bit reproducible compilation or a signed SLSA attestation.
|
||||
|
||||
To refresh after source or compiler changes:
|
||||
|
||||
1. Let a main build compile and test the new inputs.
|
||||
2. Run `python tools/components/preloads.py snapshot --run RUN_ID`.
|
||||
3. Publish `app/build/component-snapshot/preloads.tar.gz` to
|
||||
DroidDeck-Components as `preloads-<full archive SHA-256>`, without replacing an
|
||||
existing release. Include the manifest and producing run in the release notes.
|
||||
4. Review and commit the updated manifest. CI independently checks its origin.
|
||||
|
||||
The workflow dispatch `rebuild_preloads` option forces source compilation even
|
||||
when an approved snapshot matches. Use it when refreshing a compiler or system
|
||||
header revision. Runner image updates do not silently replace an approved binary;
|
||||
the producing run remains the record of its compiler environment.
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
D=${1:?output directory}
|
||||
mkdir -p "$D"
|
||||
aarch64-linux-gnu-g++ -shared -fPIC -O2 -Wall -Wno-attributes -Wno-nonnull-compare \
|
||||
-pthread -std=c++17 -static-libstdc++ -static-libgcc -Wl,--exclude-libs,ALL \
|
||||
-o "$D/libfakeinput.so" app/src/main/cpp/fakeinput_steam.cpp -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/libfakeinput.so"
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
|
||||
-o "$D/libblsession.so" tools/linuxfs/preload/*.c -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/libblsession.so"
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
|
||||
-o "$D/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/libblfastpath.so"
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \
|
||||
-fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$D/libblaudit.so" tools/proot/fastpath/audit.c
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/libblaudit.so"
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
|
||||
-o "$D/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/libssbs.so"
|
||||
mkdir -p "$D/usr/local/bin"
|
||||
aarch64-linux-gnu-gcc -O2 -Wall -Wextra -o "$D/usr/local/bin/droiddeck-clipboard" tools/linuxfs/clipboard/clipboard.c -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$D/usr/local/bin/droiddeck-clipboard"
|
||||
@@ -0,0 +1,126 @@
|
||||
{
|
||||
"archive_sha256": "cb6487a94f25a3068fd07958174eb5b5e14f709655ff4ef0a8cdbd99138513b2",
|
||||
"arm64_recipe": "1c92a1b0669277de3e78e9f292634affb5b5a19d5ea49d3ad8610c1dc638c2fb",
|
||||
"groups": {
|
||||
"arm64": {
|
||||
"@runner": "84f825635599f81ed33f62c5fc4c73f66044d1468b060d65f83855eb9c8bfe77",
|
||||
"app/src/main/cpp/adrenotools/src/hook/kgsl.h": "396c56761034a4ed783543ee3312c6ec64d63b2a1cf7eecf1939c23251f25adf",
|
||||
"app/src/main/cpp/fakeinput_steam.cpp": "3d11e859ca58a42a9da92a3fc589d404df790e151f7e18ea6114645750e70ef5",
|
||||
"app/src/main/cpp/venc/venc_proto.h": "2242ddbc8981d14a33bac70264bb3e7f701fd1c3ebb988e17bbb2b19b6b808f7",
|
||||
"tools/linuxfs/clipboard/clipboard.c": "96291c2bbc040c83caabf059ab3e95a9ed90d6a768c759d7cb4c0b64706bddfa",
|
||||
"tools/linuxfs/preload/binfmt.c": "0933cc49cb53f0aa1206e2eae516d38dc3337e74174be07e10619c42aa56792b",
|
||||
"tools/linuxfs/preload/dircache.c": "8e7f19ea4a1198783d53a3f357326c6177da157b0cc7e0d22e1670963e1c26e7",
|
||||
"tools/linuxfs/preload/drm.c": "e0eabab0d8ed9e841918e827bf87168efa7f2dfb8404169e9bed3ab5eda2836c",
|
||||
"tools/linuxfs/preload/fsync.c": "795c114c2a05d3f250d07145d04c5658091699da3b96290da36348ccadfb0c09",
|
||||
"tools/linuxfs/preload/inputudev.c": "2648248d3d235a582eb8459709e0a85de537c03ac10c5040fbfb50d9846c68eb",
|
||||
"tools/linuxfs/preload/kgslid.c": "88a7875fd5289930204d155d92ac9d15492cd06db16ed87aa9c95af37f3e7089",
|
||||
"tools/linuxfs/preload/kgslpoll.c": "f0d853d919a52487c04cbda5fb3fa22269da54b7f4ee2f7dd3b5bd9909418670",
|
||||
"tools/linuxfs/preload/links.c": "b7bbcf9112393b152f358fe6f8dc9a1955f0c8532e1eddf016a971415cd13c64",
|
||||
"tools/linuxfs/preload/lock.c": "f71b3fd40e6a2b74908d3cdc601e8748699ae9c7a89b542d2445b999015ffd66",
|
||||
"tools/linuxfs/preload/net.c": "335853c8ea0e9a8ebbc5a5500dc4bded9c34c4310a6ab8aa46b07d5ce1c2b55e",
|
||||
"tools/linuxfs/preload/netif.c": "cc26d50b8d4639d8dd4697811bbf85c9d9f31761f9504a4af42473de3cb22b3c",
|
||||
"tools/linuxfs/preload/noexec.c": "1855aeaeda3d506329bd9af1107580c74d1f56787c411b2e01c0a8a9d4059c7b",
|
||||
"tools/linuxfs/preload/ntsync.c": "0614c48af0c062bfcb4c30141efe1b792b340767a67d2fe92ee9877d2ba16e0d",
|
||||
"tools/linuxfs/preload/opens.c": "92ae4aea16aa457a898dedf4a70eb332d80d3b1e77f3a60bace7715a4317399e",
|
||||
"tools/linuxfs/preload/pathcache.c": "eb0d4b5238fe6c8891e805d90bd837e466754100c978fad2d0ff717116b9b8a3",
|
||||
"tools/linuxfs/preload/perms.c": "f600619c4ca130f69001ed7ad7bd74a3dc215b14c61479c45bb770f916d244b9",
|
||||
"tools/linuxfs/preload/pulseguard.c": "57674ea40283e53a27bc78362de1ba41ee972d172ea38b1f818b1d6eed0abe65",
|
||||
"tools/linuxfs/preload/quest_kgsl.c": "59d9b8416be6a1c680f1d1e4c2427d2cbb7b415344261adb357d9d211ec95cfe",
|
||||
"tools/linuxfs/preload/robust.c": "9eb6c0ed50e0f48a8cfb86bd92587f471354bdf2531b9177f4b3ab83bdfed39a",
|
||||
"tools/linuxfs/preload/rptouch.c": "0e3f8bf2f9a41324361f38fa4da8d2d47593c9555f0cabf8228767397846c7c3",
|
||||
"tools/linuxfs/preload/space.c": "118389bcc89dfa2b4230b80915e21d2d4331c4cff1f5a58265f85e6ecb5c29d2",
|
||||
"tools/linuxfs/preload/storage.c": "08a8b46c3588f3160bb8608489768010ec36d5ccdb2b25a431ec9d6fcd413a6e",
|
||||
"tools/linuxfs/preload/sysv.c": "49774ee5e2e39b84dcfadf63abdc741b3df236cb11783744a1280adee36965e8",
|
||||
"tools/linuxfs/preload/tracefs.c": "be3f8a2f032b56e6e08a931e77a9386aea4bb93421a482271e06386ec0c469b6",
|
||||
"tools/linuxfs/preload/tracer.c": "f34e5377c891b0a8d00489245d66f96bc352df4cd90f47a951dcadae980af4c1",
|
||||
"tools/linuxfs/preload/udevmon.c": "2045dfb883cfb6e2016c359a3dc3af17f3b8f383f6dd0d5dfee407ef0d344852",
|
||||
"tools/linuxfs/preload/usr2.c": "6ae704380b45a9b3a74c4fe2b3ce4482470dae59ced26cf0aee64f4b4dcc11f9",
|
||||
"tools/linuxfs/preload/venc.c": "24c3c46fe49d28adfc8c977e52c9cc407f534b26f56d94b3425b8bb01803dba8",
|
||||
"tools/linuxfs/preload/vmwrite.c": "1a3ea2ec4354e0716c17f7db537b19ab82bcd524f2aece2fa5dec89b076a2188",
|
||||
"tools/linuxfs/preload/webhelper.c": "a8213a7a96b407459c83fb93365cfe58fd05964e7bb262b8061c1c0792a6aac8",
|
||||
"tools/linuxfs/preload/writewatch.c": "61e395d1caa184d329dead0e36c4e92cb1069b1154ac36581f37406c1c242240",
|
||||
"tools/linuxfs/preload/xmode.c": "9a09995bf52d40b07f16828bcc545b59b8fb1ba60d9cb09f14f7f31dba1e65a6",
|
||||
"tools/linuxfs/preload/zram.c": "12bcf07459728d4a7ede6fa0fea8deb330f7c5d0f0dd61e610e81b9efecd4262",
|
||||
"tools/linuxfs/ssbs/ssbs_adapter.c": "9e4e0fe637f7b8f91f12d8151f02e8d31bcb4294be2b2c62d3cd3ce76f200306",
|
||||
"tools/proot/fastpath/audit.c": "bfe347a2ddaceef40a78d7d590e4e679724ee4c82f3f1f1e3c7017a18ad536d4",
|
||||
"tools/proot/fastpath/fastpath.c": "4cd07b25ba14f6faa6bac5091ae323ded6b646dbb34b9e5a92ea6385e4d37cb5",
|
||||
"tools/quest/kgsl_compat.c": "111afffda7a6df2469610f6c342ae086b1a97a559c586efc4a28015b86efd6bd",
|
||||
"tools/quest/kgsl_compat.h": "2e9e0eb647530a35277954b7e61a22ee958067fe46056a608fb32f40f273f1bf"
|
||||
},
|
||||
"x86": {
|
||||
"@runner": "84f825635599f81ed33f62c5fc4c73f66044d1468b060d65f83855eb9c8bfe77",
|
||||
"app/src/main/cpp/adrenotools/src/hook/kgsl.h": "396c56761034a4ed783543ee3312c6ec64d63b2a1cf7eecf1939c23251f25adf",
|
||||
"app/src/main/cpp/fakeinput_steam.cpp": "3d11e859ca58a42a9da92a3fc589d404df790e151f7e18ea6114645750e70ef5",
|
||||
"app/src/main/cpp/venc/venc_proto.h": "2242ddbc8981d14a33bac70264bb3e7f701fd1c3ebb988e17bbb2b19b6b808f7",
|
||||
"tools/linuxfs/build-x86-preloads.sh": "d0820252809881db89db5a17427e74b9aa4964aae70623cb7f4b474e90c7a23b",
|
||||
"tools/linuxfs/debian-image.env": "1c79dea4314c784042a5c59208765c8401771098f293185d61a157d89edf6e52",
|
||||
"tools/linuxfs/fex/faultreport.c": "573bf780f32f7256dc2fbf40fe850d67e67c354509fdedcac3ff7f1a261daac3",
|
||||
"tools/linuxfs/fex/vulkanpin.c": "f2693ac2be8d15f7881115e61b1c6b3fe4aeec8f603584c6fce5b0f046563d51",
|
||||
"tools/linuxfs/fex/webkitinput.c": "693ffd6c8c8c97e87156444ad2255c84dbf22cd0053d135fb8d9be094b5534d1",
|
||||
"tools/linuxfs/preload/binfmt.c": "0933cc49cb53f0aa1206e2eae516d38dc3337e74174be07e10619c42aa56792b",
|
||||
"tools/linuxfs/preload/dircache.c": "8e7f19ea4a1198783d53a3f357326c6177da157b0cc7e0d22e1670963e1c26e7",
|
||||
"tools/linuxfs/preload/drm.c": "e0eabab0d8ed9e841918e827bf87168efa7f2dfb8404169e9bed3ab5eda2836c",
|
||||
"tools/linuxfs/preload/fsync.c": "795c114c2a05d3f250d07145d04c5658091699da3b96290da36348ccadfb0c09",
|
||||
"tools/linuxfs/preload/inputudev.c": "2648248d3d235a582eb8459709e0a85de537c03ac10c5040fbfb50d9846c68eb",
|
||||
"tools/linuxfs/preload/kgslid.c": "88a7875fd5289930204d155d92ac9d15492cd06db16ed87aa9c95af37f3e7089",
|
||||
"tools/linuxfs/preload/kgslpoll.c": "f0d853d919a52487c04cbda5fb3fa22269da54b7f4ee2f7dd3b5bd9909418670",
|
||||
"tools/linuxfs/preload/links.c": "b7bbcf9112393b152f358fe6f8dc9a1955f0c8532e1eddf016a971415cd13c64",
|
||||
"tools/linuxfs/preload/lock.c": "f71b3fd40e6a2b74908d3cdc601e8748699ae9c7a89b542d2445b999015ffd66",
|
||||
"tools/linuxfs/preload/net.c": "335853c8ea0e9a8ebbc5a5500dc4bded9c34c4310a6ab8aa46b07d5ce1c2b55e",
|
||||
"tools/linuxfs/preload/netif.c": "cc26d50b8d4639d8dd4697811bbf85c9d9f31761f9504a4af42473de3cb22b3c",
|
||||
"tools/linuxfs/preload/noexec.c": "1855aeaeda3d506329bd9af1107580c74d1f56787c411b2e01c0a8a9d4059c7b",
|
||||
"tools/linuxfs/preload/ntsync.c": "0614c48af0c062bfcb4c30141efe1b792b340767a67d2fe92ee9877d2ba16e0d",
|
||||
"tools/linuxfs/preload/opens.c": "92ae4aea16aa457a898dedf4a70eb332d80d3b1e77f3a60bace7715a4317399e",
|
||||
"tools/linuxfs/preload/pathcache.c": "eb0d4b5238fe6c8891e805d90bd837e466754100c978fad2d0ff717116b9b8a3",
|
||||
"tools/linuxfs/preload/perms.c": "f600619c4ca130f69001ed7ad7bd74a3dc215b14c61479c45bb770f916d244b9",
|
||||
"tools/linuxfs/preload/pulseguard.c": "57674ea40283e53a27bc78362de1ba41ee972d172ea38b1f818b1d6eed0abe65",
|
||||
"tools/linuxfs/preload/quest_kgsl.c": "59d9b8416be6a1c680f1d1e4c2427d2cbb7b415344261adb357d9d211ec95cfe",
|
||||
"tools/linuxfs/preload/robust.c": "9eb6c0ed50e0f48a8cfb86bd92587f471354bdf2531b9177f4b3ab83bdfed39a",
|
||||
"tools/linuxfs/preload/rptouch.c": "0e3f8bf2f9a41324361f38fa4da8d2d47593c9555f0cabf8228767397846c7c3",
|
||||
"tools/linuxfs/preload/space.c": "118389bcc89dfa2b4230b80915e21d2d4331c4cff1f5a58265f85e6ecb5c29d2",
|
||||
"tools/linuxfs/preload/storage.c": "08a8b46c3588f3160bb8608489768010ec36d5ccdb2b25a431ec9d6fcd413a6e",
|
||||
"tools/linuxfs/preload/sysv.c": "49774ee5e2e39b84dcfadf63abdc741b3df236cb11783744a1280adee36965e8",
|
||||
"tools/linuxfs/preload/tracefs.c": "be3f8a2f032b56e6e08a931e77a9386aea4bb93421a482271e06386ec0c469b6",
|
||||
"tools/linuxfs/preload/tracer.c": "f34e5377c891b0a8d00489245d66f96bc352df4cd90f47a951dcadae980af4c1",
|
||||
"tools/linuxfs/preload/udevmon.c": "2045dfb883cfb6e2016c359a3dc3af17f3b8f383f6dd0d5dfee407ef0d344852",
|
||||
"tools/linuxfs/preload/usr2.c": "6ae704380b45a9b3a74c4fe2b3ce4482470dae59ced26cf0aee64f4b4dcc11f9",
|
||||
"tools/linuxfs/preload/venc.c": "24c3c46fe49d28adfc8c977e52c9cc407f534b26f56d94b3425b8bb01803dba8",
|
||||
"tools/linuxfs/preload/vmwrite.c": "1a3ea2ec4354e0716c17f7db537b19ab82bcd524f2aece2fa5dec89b076a2188",
|
||||
"tools/linuxfs/preload/webhelper.c": "a8213a7a96b407459c83fb93365cfe58fd05964e7bb262b8061c1c0792a6aac8",
|
||||
"tools/linuxfs/preload/writewatch.c": "61e395d1caa184d329dead0e36c4e92cb1069b1154ac36581f37406c1c242240",
|
||||
"tools/linuxfs/preload/xmode.c": "9a09995bf52d40b07f16828bcc545b59b8fb1ba60d9cb09f14f7f31dba1e65a6",
|
||||
"tools/linuxfs/preload/zram.c": "12bcf07459728d4a7ede6fa0fea8deb330f7c5d0f0dd61e610e81b9efecd4262",
|
||||
"tools/quest/kgsl_compat.c": "111afffda7a6df2469610f6c342ae086b1a97a559c586efc4a28015b86efd6bd",
|
||||
"tools/quest/kgsl_compat.h": "2e9e0eb647530a35277954b7e61a22ee958067fe46056a608fb32f40f273f1bf"
|
||||
}
|
||||
},
|
||||
"origin": {
|
||||
"artifact": 12520502054,
|
||||
"artifact_sha256": "b7e800a1884a516ec52b050b7f904f9881427bca4579f660b909fd18f15e49ff",
|
||||
"commit": "b76f13f7acc0af0cc00369dde61fb88e40529e6d",
|
||||
"run": 39992155294,
|
||||
"url": "https://github.com/Droid-Deck/DroidDeck/actions/runs/39992155294"
|
||||
},
|
||||
"outputs": {
|
||||
"i386/libblsession.so": "5343cdb704217ddc81ee593e67d5987363dc7b5ac78bd4bd36000219e5063268",
|
||||
"i386/libfakeinput.so": "188d88dfb13b6e2f2df948e7b8b4d9ec604770f55d1ea5b6b9e21ad56a74ce5e",
|
||||
"i386/libfaultreport.so": "95f9ef60f04a13ee60cfbd4d2d2efb60a7063b722e646c78c9dc27162292877b",
|
||||
"libblaudit.so": "6c6abe89baf5d7309babee0c3372a4b00c2b689e2a63de38c5e9a660626509b5",
|
||||
"libblfastpath.so": "0e1f151b5c1c5ea9dd380ad58d09f8fe7ba3ae037d0991cb3eef6899e9d5ec83",
|
||||
"libblsession.so": "29d4b5ea46dba941107e02c0ee544244b137a339cada43bff65c350f2ef2755a",
|
||||
"libfakeinput.so": "5f6dc3aea05de2dc1fbe0bbfac2ce4d11e3d47f761c6b06c8badb15fe1904392",
|
||||
"libssbs.so": "c288ed0da26c2481aff270d1cdafe1205061c4a300c2c46ae1b53c6e0c94f44b",
|
||||
"usr/local/bin/droiddeck-clipboard": "ec50e8f67fdf76372927189b0fdb670f9b70fd3484f2fe3e833bc8f9fdc44205",
|
||||
"x86_64/libblsession.so": "61d19f06aebab6a789f8c99f687af9903376bfc853e46b70c48a2ce5e497accc",
|
||||
"x86_64/libfakeinput.so": "8663dd2086bf8ae53069a480dd9ae4619109d32f0947a29e2ce38f07a21ae2e6",
|
||||
"x86_64/libfaultreport.so": "bb0dc9165d0d764473e552508650edd586df16d620fc5b16b8b1c5740a28ce22",
|
||||
"x86_64/libvulkan-thunk.so": "74f01b652d7e8179561a096a552328b68da640cc4bad3981edd38e6afbf90243",
|
||||
"x86_64/libwebkitinput.so": "9b07380a7b017397b9c56bb7bb1161e046b0c6a647fc413888904606b0def6d0"
|
||||
},
|
||||
"toolchain": {
|
||||
"arm64": "Ubuntu 24.04 GCC cross compiler; exact build commands pinned by arm64_recipe",
|
||||
"x86": "Debian bullseye compiler image and recipe pinned by source inputs"
|
||||
},
|
||||
"version": 1,
|
||||
"x86_recipe": "36d06d49f7a4bff5ef675c1e03ce5a5b61498df4c36b9d15a938b41564f755cc"
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
"""Reuse reviewed preload snapshots; changed snapshots must prove their main-build origin."""
|
||||
import argparse
|
||||
import gzip
|
||||
import http.client
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import posixpath
|
||||
import re
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import textwrap
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from zipfile import ZipFile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
REPO = 'Droid-Deck/DroidDeck'
|
||||
LOCK = 'tools/components/preloads.lock.json'
|
||||
DEFINITIONS = json.loads((ROOT / 'tools/components/recipes.json').read_text())
|
||||
INPUTS = {group: DEFINITIONS[name]['source_inputs'] for group, name in
|
||||
[('arm64', 'arm64-preloads'), ('x86', 'x86-preloads')]}
|
||||
OUTPUTS = {group: DEFINITIONS[name]['snapshot_outputs'] for group, name in
|
||||
[('arm64', 'arm64-preloads'), ('x86', 'x86-preloads')]}
|
||||
COMMON = [name for name in INPUTS['arm64'] if name in INPUTS['x86']]
|
||||
FILES = set(sum(OUTPUTS.values(), []))
|
||||
|
||||
|
||||
def sha(data):
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def run(*args, root=ROOT):
|
||||
return subprocess.check_output(args, cwd=root)
|
||||
|
||||
|
||||
def api(path):
|
||||
return json.loads(run('gh', 'api', 'repos/' + REPO + '/' + path))
|
||||
|
||||
|
||||
def source_files(group, root=ROOT, commit=None, patterns=None):
|
||||
patterns = patterns if patterns is not None else INPUTS[group]
|
||||
if commit:
|
||||
paths = run('git', 'ls-tree', '-r', '--name-only', commit, '--', *patterns, root=root).decode().splitlines()
|
||||
read = lambda path: run('git', 'show', f'{commit}:{path}', root=root)
|
||||
else:
|
||||
paths = run('git', 'ls-files', '--cached', '--others', '--exclude-standard', '--', *patterns, root=root).decode().splitlines()
|
||||
read = lambda path: (root / path).read_bytes()
|
||||
paths = [p for p in paths if not p.endswith('.md') and '__pycache__' not in p and not p.endswith('.pyc')]
|
||||
if not paths:
|
||||
raise ValueError('No preload inputs found')
|
||||
result = {}
|
||||
pending = list(paths)
|
||||
while pending:
|
||||
path = pending.pop()
|
||||
if path in result:
|
||||
continue
|
||||
if path.startswith('../') or path.startswith('/'):
|
||||
raise ValueError('Preload include escapes repository')
|
||||
content = read(path).replace(b'\r\n', b'\n')
|
||||
result[path] = sha(content)
|
||||
for include in re.findall(rb'^\s*#\s*include\s*"([^"\n]+)"', content, re.M):
|
||||
pending.append(posixpath.normpath(posixpath.join(posixpath.dirname(path), include.decode())))
|
||||
workflow = (run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode() if commit
|
||||
else (root / '.github/workflows/build.yml').read_text())
|
||||
job = 'components' if re.search(r'(?m)^ components:', workflow) else 'build'
|
||||
runner = re.search(r'(?ms)^ ' + job + r':\n.*?^ runs-on: ([^\n]+)', workflow)
|
||||
if runner is None:
|
||||
raise ValueError('APK build runner policy is missing')
|
||||
result['@runner'] = sha(runner[1].strip().encode())
|
||||
return dict(sorted(result.items()))
|
||||
|
||||
|
||||
def recipe(root=ROOT, commit=None):
|
||||
def read(path):
|
||||
return (run('git', 'show', f'{commit}:{path}', root=root).decode() if commit
|
||||
else (root / path).read_text())
|
||||
script = 'tools/components/build-arm64-preloads.sh'
|
||||
if commit and run('git', 'ls-tree', commit, script, root=root) == b'':
|
||||
workflow = read('.github/workflows/build.yml')
|
||||
text = workflow[workflow.index(' aarch64-linux-gnu-g++ -shared'):]
|
||||
text = textwrap.dedent(text[:text.index(' VK_INCLUDE=')])
|
||||
else:
|
||||
text = read(script)
|
||||
preamble = '#!/usr/bin/env bash\nset -euo pipefail\nD=${1:?output directory}\nmkdir -p "$D"\n'
|
||||
if not text.startswith(preamble):
|
||||
return sha(text.encode())
|
||||
text = text[len(preamble):]
|
||||
return sha(text.replace('\r\n', '\n').encode())
|
||||
|
||||
|
||||
def x86_recipe(root=ROOT, commit=None):
|
||||
path = 'tools/components/recipes/x86-preloads.sh'
|
||||
if commit and not run('git', 'ls-tree', commit, path, root=root):
|
||||
workflow = run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode()
|
||||
section = workflow[workflow.index(' - name: Build the x86 preloads'):]
|
||||
text = section.split(' run: |\n', 1)[1].split('\n - name:', 1)[0]
|
||||
text = textwrap.dedent(text).rstrip() + '\n'
|
||||
else:
|
||||
text = (run('git', 'show', f'{commit}:{path}', root=root).decode() if commit
|
||||
else (root / path).read_text())
|
||||
text = text.removeprefix('#!/usr/bin/env bash\n')
|
||||
return sha(text.replace('\r\n', '\n').encode())
|
||||
|
||||
|
||||
def artifact_bytes(origin, cache):
|
||||
build = api(f"actions/runs/{origin['run']}")
|
||||
if (build['event'] != 'push' or build['head_branch'] != 'main' or build['conclusion'] != 'success'
|
||||
or build['head_sha'] != origin['commit'] or build['path'] != '.github/workflows/build.yml'
|
||||
or build['repository']['full_name'] != REPO):
|
||||
raise ValueError('Snapshot is not from a successful main APK build')
|
||||
artifact = api(f"actions/artifacts/{origin['artifact']}")
|
||||
if (artifact['name'] != 'droiddeck-apk' or artifact['workflow_run']['id'] != origin['run']
|
||||
or artifact['digest'] != 'sha256:' + origin['artifact_sha256']):
|
||||
raise ValueError('Original APK artifact identity or digest mismatch')
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
path = cache / (origin['artifact_sha256'] + '.zip')
|
||||
if not path.exists():
|
||||
path.write_bytes(run('gh', 'api', f"repos/{REPO}/actions/artifacts/{origin['artifact']}/zip"))
|
||||
content = path.read_bytes()
|
||||
if sha(content) != origin['artifact_sha256']:
|
||||
raise ValueError('Original APK archive checksum mismatch')
|
||||
with ZipFile(io.BytesIO(content)) as archive:
|
||||
names = [name for name in archive.namelist() if name.endswith('.apk')]
|
||||
if len(names) != 1:
|
||||
raise ValueError('Expected one original APK')
|
||||
with ZipFile(io.BytesIO(archive.read(names[0]))) as apk:
|
||||
return {name: apk.read('assets/linuxfs/' + name) for name in FILES}
|
||||
|
||||
|
||||
def validate_lock(data):
|
||||
if data.get('version') != 1 or set(data['groups']) != set(INPUTS):
|
||||
raise ValueError('Unsupported preload snapshot')
|
||||
if set(data['outputs']) != FILES:
|
||||
raise ValueError('Unexpected preload output set')
|
||||
for digest in [data['archive_sha256'], *data['outputs'].values()]:
|
||||
if len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest):
|
||||
raise ValueError('Invalid preload checksum')
|
||||
|
||||
|
||||
def producer_sources(group, commit, root=ROOT):
|
||||
config = 'tools/components/recipes.json'
|
||||
if run('git', 'ls-tree', commit, config, root=root):
|
||||
definitions = json.loads(run('git', 'show', f'{commit}:{config}', root=root))
|
||||
name = 'arm64-preloads' if group == 'arm64' else 'x86-preloads'
|
||||
patterns = definitions[name]['source_inputs']
|
||||
else:
|
||||
# Older approved builds kept ARM64 commands inline, before the Vulkan/VR build.
|
||||
if group == 'arm64':
|
||||
text = run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode()
|
||||
text = text[text.index(' aarch64-linux-gnu-g++ -shared'):]
|
||||
text = text[:text.index(' VK_INCLUDE=')]
|
||||
else:
|
||||
text = run('git', 'show', f'{commit}:tools/linuxfs/build-x86-preloads.sh', root=root).decode()
|
||||
patterns = re.findall(r'\b((?:tools|app)/[\w./*+-]+\.(?:cpp|c|h))(?=\s|$)', text)
|
||||
if group == 'x86':
|
||||
patterns += ['tools/linuxfs/fex', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/debian-image.env']
|
||||
return source_files(group, root, commit, patterns)
|
||||
|
||||
|
||||
def prove_origin(data, cache, root=ROOT):
|
||||
content = artifact_bytes(data['origin'], cache)
|
||||
if {name: sha(value) for name, value in content.items()} != data['outputs']:
|
||||
raise ValueError('Snapshot binaries differ from the original main APK')
|
||||
commit = data['origin']['commit']
|
||||
run('git', 'fetch', '--no-tags', 'origin', commit, root=root)
|
||||
for group in INPUTS:
|
||||
if source_files(group, root, commit) != data['groups'][group]:
|
||||
raise ValueError('Snapshot inputs differ from the producing source')
|
||||
for name, checksum in producer_sources(group, commit, root).items():
|
||||
if data['groups'][group].get(name) != checksum:
|
||||
raise ValueError('Snapshot omits or changes a producing build dependency')
|
||||
if recipe(root, commit) != data['arm64_recipe']:
|
||||
raise ValueError('Snapshot recipe differs from the producing source')
|
||||
if x86_recipe(root, commit) != data['x86_recipe']:
|
||||
raise ValueError('Snapshot x86 recipe differs from the producing source')
|
||||
|
||||
|
||||
def unpack(content, data):
|
||||
if sha(content) != data['archive_sha256']:
|
||||
raise ValueError('Preload archive checksum mismatch')
|
||||
result = {}
|
||||
total = 0
|
||||
with tarfile.open(fileobj=io.BytesIO(content), mode='r:gz') as archive:
|
||||
for entry in archive:
|
||||
if entry.name not in FILES or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024:
|
||||
raise ValueError('Unsafe or unexpected preload archive entry')
|
||||
total += entry.size
|
||||
if total > 64 * 1024 * 1024:
|
||||
raise ValueError('Preload archive exceeds expanded size limit')
|
||||
value = archive.extractfile(entry).read()
|
||||
if sha(value) != data['outputs'][entry.name]:
|
||||
raise ValueError('Preload binary checksum mismatch')
|
||||
result[entry.name] = value
|
||||
if set(result) != FILES:
|
||||
raise ValueError('Incomplete preload archive')
|
||||
return result
|
||||
|
||||
|
||||
def restore(root=ROOT):
|
||||
data = json.loads((root / LOCK).read_text())
|
||||
validate_lock(data)
|
||||
matching = {group: source_files(group, root) == data['groups'][group] for group in INPUTS}
|
||||
matching['arm64'] &= recipe(root) == data['arm64_recipe']
|
||||
matching['x86'] &= x86_recipe(root) == data.get('x86_recipe')
|
||||
if os.environ.get('FORCE_PRELOAD_REBUILD') == 'true':
|
||||
matching = dict.fromkeys(INPUTS, False)
|
||||
if any(matching.values()):
|
||||
base = os.environ.get('COMPONENT_TRUSTED_BASE')
|
||||
approved = None
|
||||
if base:
|
||||
result = subprocess.run(['git', 'show', f'{base}:{LOCK}'], cwd=root, capture_output=True)
|
||||
if result.returncode == 0:
|
||||
approved = json.loads(result.stdout)
|
||||
if approved != data:
|
||||
try:
|
||||
prove_origin(data, root / 'app/build/component-downloads', root)
|
||||
except (subprocess.CalledProcessError, OSError, http.client.HTTPException):
|
||||
print('Snapshot origin is unavailable; rebuilding without trusting the snapshot.')
|
||||
matching = dict.fromkeys(INPUTS, False)
|
||||
if any(matching.values()):
|
||||
url = (f"https://github.com/Droid-Deck/DroidDeck-Components/releases/download/"
|
||||
f"preloads-{data['archive_sha256']}/preloads.tar.gz")
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=60) as response:
|
||||
content = response.read(64 * 1024 * 1024)
|
||||
except (OSError, http.client.HTTPException) as error:
|
||||
if isinstance(error, urllib.error.HTTPError) and error.code not in (404, 429) and error.code < 500:
|
||||
raise
|
||||
print('Preload snapshot is unavailable; rebuilding from source.')
|
||||
matching = dict.fromkeys(INPUTS, False)
|
||||
else:
|
||||
binaries = unpack(content, data)
|
||||
for group, matches in matching.items():
|
||||
if matches:
|
||||
for name in OUTPUTS[group]:
|
||||
path = root / 'app/src/main/assets/linuxfs' / name
|
||||
if not path.resolve().is_relative_to(root.resolve()) or path.is_symlink():
|
||||
raise ValueError('Preload output escapes repository or is a symlink')
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(binaries[name])
|
||||
path.chmod(0o755 if name.endswith('droiddeck-clipboard') else 0o644)
|
||||
for group, matches in matching.items():
|
||||
print(f"{group}: {'verified snapshot reused' if matches else 'inputs changed or snapshot unavailable; rebuild required'}")
|
||||
if os.environ.get('GITHUB_OUTPUT'):
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a') as output:
|
||||
output.write(f'{group}={str(matches).lower()}\n')
|
||||
return matching
|
||||
|
||||
|
||||
def snapshot(run_id, destination, root=ROOT):
|
||||
build = api(f'actions/runs/{run_id}')
|
||||
candidates = api(f'actions/runs/{run_id}/artifacts?per_page=100')['artifacts']
|
||||
artifact = next(a for a in candidates if a['name'] == 'droiddeck-apk')
|
||||
origin = dict(run=run_id, commit=build['head_sha'], artifact=artifact['id'],
|
||||
artifact_sha256=artifact['digest'].removeprefix('sha256:'),
|
||||
url=build['html_url'])
|
||||
content = artifact_bytes(origin, root / 'app/build/component-downloads')
|
||||
run('git', 'fetch', '--no-tags', 'origin', origin['commit'], root=root)
|
||||
data = dict(version=1, origin=origin, groups={g: source_files(g, root, origin['commit']) for g in INPUTS},
|
||||
arm64_recipe=recipe(root, origin['commit']), x86_recipe=x86_recipe(root, origin['commit']),
|
||||
outputs={name: sha(value) for name, value in content.items()},
|
||||
toolchain=dict(arm64='Ubuntu 24.04 GCC cross compiler; exact build commands pinned by arm64_recipe',
|
||||
x86='Debian bullseye compiler image and recipe pinned by source inputs'))
|
||||
stream = io.BytesIO()
|
||||
with gzip.GzipFile(fileobj=stream, mode='wb', mtime=0) as compressed:
|
||||
with tarfile.open(fileobj=compressed, mode='w') as archive:
|
||||
for name, value in sorted(content.items()):
|
||||
entry = tarfile.TarInfo(name)
|
||||
entry.size, entry.mode = len(value), 0o644
|
||||
archive.addfile(entry, io.BytesIO(value))
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
(destination / 'preloads.tar.gz').write_bytes(stream.getvalue())
|
||||
data['archive_sha256'] = sha(stream.getvalue())
|
||||
(root / LOCK).write_text(json.dumps(data, indent=2, sort_keys=True) + '\n')
|
||||
print('Snapshot SHA-256: ' + data['archive_sha256'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('operation', choices=['restore', 'snapshot'])
|
||||
parser.add_argument('--run', type=int)
|
||||
parser.add_argument('--out', type=Path, default=ROOT / 'app/build/component-snapshot')
|
||||
args = parser.parse_args()
|
||||
if args.operation == 'restore':
|
||||
restore()
|
||||
else:
|
||||
snapshot(args.run, args.out)
|
||||
@@ -0,0 +1,167 @@
|
||||
"""One entry point for runtime recipes, cache identities and verified job handoff."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path, PurePosixPath
|
||||
import shutil
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
REGISTRY = 'tools/components/recipes.json'
|
||||
TREES = ['app/src/main/assets/linuxfs', 'app/src/main/assets/directaudio',
|
||||
'app/src/main/assets/droiddeck-esync', 'app/build/prepared-assets',
|
||||
'app/build/component-records']
|
||||
LIBRARIES = ['app/src/main/jniLibs/arm64-v8a/' + name for name in
|
||||
('libproot.so', 'libproot-loader.so', 'libdirectaudiorelay.so')]
|
||||
MANIFEST = 'app/build/prepared-components.json'
|
||||
|
||||
|
||||
def digest(path):
|
||||
with path.open('rb') as stream:
|
||||
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
||||
|
||||
|
||||
def registry(root=ROOT):
|
||||
return json.loads((root / REGISTRY).read_text())
|
||||
|
||||
|
||||
def identity(name, root=ROOT):
|
||||
spec = registry(root)[name]
|
||||
paths = subprocess.check_output(['git', 'ls-files', '--cached', '--others', '--exclude-standard',
|
||||
'--', *spec['inputs'], *spec.get('source_inputs', [])], cwd=root, text=True).splitlines()
|
||||
values = {}
|
||||
for name_ in sorted(set(paths)):
|
||||
if '__pycache__' in name_ or name_.endswith(('.pyc', '.md')):
|
||||
continue
|
||||
content = (root / name_).read_bytes()
|
||||
if b'\0' not in content:
|
||||
content = content.replace(b'\r\n', b'\n')
|
||||
values[name_] = hashlib.sha256(content).hexdigest()
|
||||
policy = dict(spec=spec, sources=values, runner=os.environ.get('RUNNER_OS', 'Linux'),
|
||||
architecture=os.environ.get('RUNNER_ARCH', 'X64'))
|
||||
entrypoint = root / 'tools/components/prepare.py'
|
||||
if entrypoint.is_file():
|
||||
policy['entrypoint'] = hashlib.sha256(entrypoint.read_bytes().replace(b'\r\n', b'\n')).hexdigest()
|
||||
installer = root / 'tools/ci/install-ubuntu-packages.sh'
|
||||
if spec.get('packages') and installer.is_file():
|
||||
policy['installer'] = hashlib.sha256(installer.read_bytes().replace(b'\r\n', b'\n')).hexdigest()
|
||||
if spec.get('ndk'):
|
||||
policy['ndk'] = os.environ.get('NDK_VERSION', '')
|
||||
policy['ndk_policy'] = hashlib.sha256((root / 'tools/ci/toolchains.env').read_bytes().replace(b'\r\n', b'\n')).hexdigest()
|
||||
return hashlib.sha256(json.dumps(policy, sort_keys=True).encode()).hexdigest()
|
||||
|
||||
|
||||
def prepare(name, root=ROOT):
|
||||
spec = registry(root)[name]
|
||||
prepare_tools([name], root)
|
||||
if 'command' in spec:
|
||||
command = spec['command']
|
||||
else:
|
||||
script = (root / spec['script']).resolve()
|
||||
if not script.is_relative_to((root / 'tools/components').resolve()):
|
||||
raise ValueError('Recipe script escapes components directory')
|
||||
command = ['bash', str(script)]
|
||||
environment = dict(os.environ)
|
||||
if spec.get('source'):
|
||||
environment.update(COMPONENT_SOURCE_URL=spec['source']['url'],
|
||||
COMPONENT_SOURCE_SHA256=spec['source']['sha256'])
|
||||
subprocess.run(command, cwd=root, env=environment, check=True)
|
||||
missing = [name_ for name_ in spec['outputs'] if not (root / name_).is_file()]
|
||||
if missing:
|
||||
raise ValueError(f'{name} did not prepare required outputs: {missing}')
|
||||
record = dict(component=name, input_sha256=identity(name, root),
|
||||
outputs={p: digest(root / p) for p in spec['outputs']})
|
||||
destination = root / 'app/build/component-records' / (name + '.json')
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(json.dumps(record, sort_keys=True, indent=2) + '\n')
|
||||
|
||||
|
||||
def prepare_tools(names, root=ROOT):
|
||||
packages = sorted({package for name in names for package in registry(root)[name].get('packages', [])})
|
||||
if packages:
|
||||
subprocess.run(['bash', 'tools/ci/install-ubuntu-packages.sh', *packages], cwd=root, check=True)
|
||||
|
||||
|
||||
def files(root=ROOT):
|
||||
result = []
|
||||
for name in TREES:
|
||||
folder = root / name
|
||||
if not folder.is_dir() or folder.is_symlink():
|
||||
raise ValueError('Missing prepared component tree: ' + name)
|
||||
for path in folder.rglob('*'):
|
||||
if path.is_symlink():
|
||||
raise ValueError('Symlink in prepared components: ' + str(path))
|
||||
if path.is_file():
|
||||
result.append(path.relative_to(root).as_posix())
|
||||
for name in LIBRARIES:
|
||||
if not (root / name).is_file() or (root / name).is_symlink():
|
||||
raise ValueError('Missing prepared component library: ' + name)
|
||||
result.append(name)
|
||||
return sorted(result)
|
||||
|
||||
|
||||
def pack(destination, root=ROOT):
|
||||
names = files(root)
|
||||
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=root, text=True).strip()
|
||||
data = dict(version=1, source_tree=tree, run=os.environ.get('GITHUB_RUN_ID', ''),
|
||||
recipes={name: identity(name, root) for name in registry(root)},
|
||||
files={name: digest(root / name) for name in names})
|
||||
(root / MANIFEST).write_text(json.dumps(data, sort_keys=True, indent=2) + '\n')
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
with tarfile.open(destination, mode='w:gz', compresslevel=1) as archive:
|
||||
for name in names + [MANIFEST]:
|
||||
archive.add(root / name, arcname=name, recursive=False)
|
||||
|
||||
|
||||
def unpack(archive_path, source_tree, root=ROOT):
|
||||
total = 0
|
||||
seen = set()
|
||||
with tarfile.open(archive_path, mode='r:gz') as archive:
|
||||
for entry in archive:
|
||||
allowed = entry.name in LIBRARIES + [MANIFEST] or any(entry.name.startswith(p + '/') for p in TREES)
|
||||
path = root / entry.name
|
||||
total += entry.size
|
||||
if (not allowed or '\\' in entry.name or '..' in PurePosixPath(entry.name).parts
|
||||
or PurePosixPath(entry.name).as_posix() != entry.name
|
||||
or not entry.isfile() or entry.name in seen or len(seen) >= 100000
|
||||
or entry.size > 512 * 1024 * 1024
|
||||
or total > 2 * 1024**3 or not path.resolve().is_relative_to(root.resolve())
|
||||
or path.is_symlink()):
|
||||
raise ValueError('Unsafe prepared component archive')
|
||||
seen.add(entry.name)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open('wb') as output:
|
||||
shutil.copyfileobj(archive.extractfile(entry), output)
|
||||
path.chmod(entry.mode & 0o777)
|
||||
data = json.loads((root / MANIFEST).read_text())
|
||||
actual = {name: digest(root / name) for name in files(root)}
|
||||
recipes = {name: identity(name, root) for name in registry(root)}
|
||||
if (data.get('version') != 1 or data['source_tree'] != source_tree or data['files'] != actual
|
||||
or data['recipes'] != recipes):
|
||||
raise ValueError('Prepared components do not match the pinned source tree and file hashes')
|
||||
print('Prepared component source identity and all output hashes verified.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('recipe')
|
||||
parser.add_argument('--archive', type=Path, default=ROOT / 'app/build/prepared-components.tar.gz')
|
||||
parser.add_argument('--tree')
|
||||
args = parser.parse_args()
|
||||
if args.recipe == 'plan':
|
||||
for name in registry():
|
||||
value = identity(name)
|
||||
print(f'{name}: {value}')
|
||||
if os.environ.get('GITHUB_OUTPUT'):
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a') as stream:
|
||||
stream.write(f'{name}={value}\n')
|
||||
elif args.recipe == 'tools':
|
||||
prepare_tools(os.environ.get('COMPONENTS_TO_BUILD', '').split())
|
||||
elif args.recipe == 'pack':
|
||||
pack(args.archive)
|
||||
elif args.recipe == 'unpack':
|
||||
unpack(args.archive, args.tree)
|
||||
else:
|
||||
prepare(args.recipe)
|
||||
@@ -0,0 +1,225 @@
|
||||
{
|
||||
"runtime": {
|
||||
"script": "tools/components/recipes/runtime.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/runtime.sh",
|
||||
"tools/quest",
|
||||
"tools/linuxfs/overlay",
|
||||
"tools/linuxfs/desktop",
|
||||
"tools/linuxfs/licenses",
|
||||
"tools/linuxfs/uruntime.env",
|
||||
"tools/gamescope/release.env",
|
||||
"tools/wlroots/release.env",
|
||||
"tools/mangoapp",
|
||||
"tools/msitools",
|
||||
"tools/pipewire",
|
||||
"tools/vr",
|
||||
"app/src/main/cpp/waylandcomp/generated",
|
||||
"app/src/main/cpp/waylandcomp/src/headpose_shm.h",
|
||||
"app/src/main/cpp/waylandcomp/src/spatialpose_shm.h",
|
||||
"app/src/main/cpp/venc/venc_proto.h"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/usr/local/bin/gamescope",
|
||||
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/bin/linuxarm64/driver_droiddeck.so"
|
||||
],
|
||||
"ndk": true,
|
||||
"packages": [
|
||||
"gcc-aarch64-linux-gnu",
|
||||
"g++-aarch64-linux-gnu",
|
||||
"libvulkan1",
|
||||
"mesa-vulkan-drivers"
|
||||
]
|
||||
},
|
||||
"esync": {
|
||||
"script": "tools/components/recipes/esync.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/esync.sh",
|
||||
"tools/droiddeck-esync/release.env"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/droiddeck-esync/index.json",
|
||||
"app/src/main/assets/droiddeck-esync/index.json.sig"
|
||||
]
|
||||
},
|
||||
"debian-image": {
|
||||
"script": "tools/components/recipes/debian-image.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/debian-image.sh"
|
||||
],
|
||||
"outputs": []
|
||||
},
|
||||
"x86-preloads": {
|
||||
"script": "tools/components/recipes/x86-preloads.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/x86-preloads.sh",
|
||||
"tools/linuxfs/build-x86-preloads.sh",
|
||||
"tools/linuxfs/preload",
|
||||
"tools/linuxfs/fex"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/x86_64/libfakeinput.so",
|
||||
"app/src/main/assets/linuxfs/x86_64/libblsession.so",
|
||||
"app/src/main/assets/linuxfs/x86_64/libfaultreport.so",
|
||||
"app/src/main/assets/linuxfs/i386/libfakeinput.so",
|
||||
"app/src/main/assets/linuxfs/i386/libblsession.so",
|
||||
"app/src/main/assets/linuxfs/i386/libfaultreport.so",
|
||||
"app/src/main/assets/linuxfs/x86_64/libwebkitinput.so",
|
||||
"app/src/main/assets/linuxfs/x86_64/libvulkan-thunk.so"
|
||||
],
|
||||
"source_inputs": [
|
||||
"app/src/main/cpp/fakeinput_steam.cpp",
|
||||
"app/src/main/cpp/adrenotools/src/hook/kgsl.h",
|
||||
"app/src/main/cpp/venc/venc_proto.h",
|
||||
"tools/linuxfs/preload",
|
||||
"tools/quest/kgsl_compat.c",
|
||||
"tools/quest/kgsl_compat.h",
|
||||
"tools/linuxfs/fex",
|
||||
"tools/linuxfs/build-x86-preloads.sh",
|
||||
"tools/linuxfs/debian-image.env"
|
||||
],
|
||||
"snapshot_outputs": [
|
||||
"x86_64/libfakeinput.so",
|
||||
"x86_64/libblsession.so",
|
||||
"x86_64/libfaultreport.so",
|
||||
"i386/libfakeinput.so",
|
||||
"i386/libblsession.so",
|
||||
"i386/libfaultreport.so",
|
||||
"x86_64/libwebkitinput.so",
|
||||
"x86_64/libvulkan-thunk.so"
|
||||
]
|
||||
},
|
||||
"pulseaudio-source": {
|
||||
"script": "tools/components/recipes/pulseaudio-source.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/pulseaudio-source.sh"
|
||||
],
|
||||
"outputs": [
|
||||
"pa13/src/pulsecore/module.h"
|
||||
],
|
||||
"source": {
|
||||
"url": "https://github.com/pulseaudio/pulseaudio/archive/refs/tags/v13.0.tar.gz",
|
||||
"sha256": "bd7980bc9bd44bd3b3363545dabd19476ebf4d6bb6d2527e7d179256394a6758"
|
||||
}
|
||||
},
|
||||
"directaudio": {
|
||||
"script": "tools/components/recipes/directaudio.sh",
|
||||
"inputs": [
|
||||
"tools/components/recipes/directaudio.sh",
|
||||
"tools/directaudio"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so"
|
||||
]
|
||||
},
|
||||
"audio": {
|
||||
"script": "tools/components/recipes/audio.sh",
|
||||
"inputs": [
|
||||
"tools/aaudio-sink",
|
||||
"tools/directaudio",
|
||||
"tools/components/recipes/audio.sh",
|
||||
"app/src/main/assets/pulseaudio.tzst"
|
||||
],
|
||||
"outputs": [
|
||||
"app/build/prepared-assets/pulseaudio.tzst"
|
||||
],
|
||||
"ndk": true
|
||||
},
|
||||
"proot": {
|
||||
"script": "tools/components/recipes/proot.sh",
|
||||
"inputs": [
|
||||
"tools/proot",
|
||||
"tools/components/recipes/proot.sh"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/jniLibs/arm64-v8a/libproot.so",
|
||||
"app/src/main/jniLibs/arm64-v8a/libproot-loader.so"
|
||||
],
|
||||
"ndk": true
|
||||
},
|
||||
"comet": {
|
||||
"script": "tools/components/recipes/comet.sh",
|
||||
"inputs": [
|
||||
"tools/comet",
|
||||
"tools/components/recipes/comet.sh"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-comet/comet",
|
||||
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-comet/version"
|
||||
],
|
||||
"packages": [
|
||||
"gcc-aarch64-linux-gnu"
|
||||
]
|
||||
},
|
||||
"arm64-preloads": {
|
||||
"script": "tools/components/recipes/arm64-preloads.sh",
|
||||
"inputs": [
|
||||
"tools/components/build-arm64-preloads.sh",
|
||||
"tools/linuxfs/preload",
|
||||
"app/src/main/cpp/fakeinput_steam.cpp",
|
||||
"tools/components/recipes/arm64-preloads.sh"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/libfakeinput.so",
|
||||
"app/src/main/assets/linuxfs/libblsession.so",
|
||||
"app/src/main/assets/linuxfs/libblfastpath.so",
|
||||
"app/src/main/assets/linuxfs/libblaudit.so",
|
||||
"app/src/main/assets/linuxfs/libssbs.so",
|
||||
"app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard"
|
||||
],
|
||||
"packages": [
|
||||
"gcc-aarch64-linux-gnu",
|
||||
"g++-aarch64-linux-gnu"
|
||||
],
|
||||
"source_inputs": [
|
||||
"app/src/main/cpp/fakeinput_steam.cpp",
|
||||
"app/src/main/cpp/adrenotools/src/hook/kgsl.h",
|
||||
"app/src/main/cpp/venc/venc_proto.h",
|
||||
"tools/linuxfs/preload",
|
||||
"tools/quest/kgsl_compat.c",
|
||||
"tools/quest/kgsl_compat.h",
|
||||
"tools/proot/fastpath",
|
||||
"tools/linuxfs/ssbs",
|
||||
"tools/linuxfs/clipboard"
|
||||
],
|
||||
"snapshot_outputs": [
|
||||
"libfakeinput.so",
|
||||
"libblsession.so",
|
||||
"libblfastpath.so",
|
||||
"libblaudit.so",
|
||||
"libssbs.so",
|
||||
"usr/local/bin/droiddeck-clipboard"
|
||||
]
|
||||
},
|
||||
"preload-snapshot": {
|
||||
"command": [
|
||||
"python3",
|
||||
"tools/components/preloads.py",
|
||||
"restore"
|
||||
],
|
||||
"inputs": [
|
||||
"tools/components/preloads.py",
|
||||
"tools/components/preloads.lock.json"
|
||||
],
|
||||
"outputs": []
|
||||
},
|
||||
"xbox": {
|
||||
"script": "tools/components/recipes/xbox.sh",
|
||||
"inputs": [
|
||||
"tools/xbox",
|
||||
"tools/components/recipes/xbox.sh"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-xbox/xodus-cli",
|
||||
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-xbox/xodus-service"
|
||||
],
|
||||
"packages": [
|
||||
"build-essential",
|
||||
"pkg-config",
|
||||
"libssl-dev",
|
||||
"protobuf-compiler",
|
||||
"cmake",
|
||||
"xz-utils"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
bash tools/components/build-arm64-preloads.sh app/src/main/assets/linuxfs
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"
|
||||
tools/aaudio-sink/build.sh "$PWD/pa13" "$PWD/sink-out"
|
||||
rm -rf pa-bundle && mkdir -p pa-bundle
|
||||
tar --use-compress-program=unzstd -xf app/src/main/assets/pulseaudio.tzst -C pa-bundle
|
||||
test ! -f pa-bundle/modules/arm64/module-aaudio-sink.so
|
||||
install -m755 sink-out/module-aaudio-sink.so pa-bundle/modules/arm64/module-aaudio-sink.so
|
||||
install -m755 sink-out/module-directaudio-native-sink.so pa-bundle/modules/arm64/module-directaudio-native-sink.so
|
||||
(cd pa-bundle && tar --use-compress-program='zstd -19' -cf ../pulseaudio.tzst .)
|
||||
mkdir -p app/build/prepared-assets
|
||||
mv pulseaudio.tzst app/build/prepared-assets/pulseaudio.tzst
|
||||
tar --use-compress-program=unzstd -tf app/build/prepared-assets/pulseaudio.tzst | grep -c '\.so$'
|
||||
rm -rf pa-bundle sink-out
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
rustup toolchain install 1.90.0 --profile minimal
|
||||
rustup target add --toolchain 1.90.0 aarch64-unknown-linux-gnu
|
||||
RUSTUP_TOOLCHAIN=1.90.0 bash tools/comet/build.sh
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
. tools/linuxfs/debian-image.env
|
||||
image=public.ecr.aws/docker/library/debian:bullseye
|
||||
if gh release download "$DEBIAN_IMAGE_TAG" -R "$GITHUB_REPOSITORY" -p "$DEBIAN_IMAGE_ASSET" -O /tmp/debian-image.tar.zst \
|
||||
&& { [ -z "$DEBIAN_IMAGE_SHA256" ] || echo "$DEBIAN_IMAGE_SHA256 /tmp/debian-image.tar.zst" | sha256sum -c -; }; then
|
||||
zstd -dc /tmp/debian-image.tar.zst | docker load
|
||||
rm -f /tmp/debian-image.tar.zst
|
||||
else
|
||||
echo "::warning::the hosted Debian image could not be fetched; pulling from the registry instead"
|
||||
for attempt in 1 2 3 4 5; do
|
||||
docker pull "$image" && break
|
||||
[ "$attempt" -lt 5 ] || { echo "::error::could not pull $image after $attempt attempts"; exit 1; }
|
||||
echo "pull attempt $attempt failed (registry busy?); retrying in $((attempt * 30)) s"
|
||||
sleep $((attempt * 30))
|
||||
done
|
||||
fi
|
||||
docker image ls "$image"
|
||||
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
tools/directaudio/fetch.sh "$PWD" "$PWD/sink-out"
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
if [ -f tools/droiddeck-esync/release.env ]; then
|
||||
. tools/droiddeck-esync/release.env
|
||||
release_url="https://github.com/${SYNC_BUNDLE_REPO}/releases/download/${SYNC_BUNDLE_TAG}"
|
||||
curl -fsSL --retry 3 -o sync-bundle.tzst "$release_url/$SYNC_BUNDLE_ASSET"
|
||||
echo "$SYNC_BUNDLE_SHA256 sync-bundle.tzst" | sha256sum -c -
|
||||
mkdir -p app/src/main/assets/droiddeck-esync
|
||||
tar --use-compress-program=unzstd -xf sync-bundle.tzst -C app/src/main/assets/droiddeck-esync
|
||||
rm sync-bundle.tzst
|
||||
curl -fsSL --retry 3 -o sync-index.json "$release_url/index.json"
|
||||
revoked=$(python3 -c 'import json, sys; print(" ".join(p["id"] for p in json.load(open(sys.argv[1]))["packs"] if p.get("revoked") is True))' sync-index.json)
|
||||
for id in $revoked; do
|
||||
if [ -e "app/src/main/assets/droiddeck-esync/packs/$id.tzst" ]; then
|
||||
echo "::warning::$SYNC_BUNDLE_ASSET carries revoked pack $id; it is left out of the APK (point tools/droiddeck-esync/release.env at a newer bundle)"
|
||||
rm -f "app/src/main/assets/droiddeck-esync/packs/$id.tzst"
|
||||
fi
|
||||
done
|
||||
rm sync-index.json
|
||||
test -f app/src/main/assets/droiddeck-esync/index.json
|
||||
test -f app/src/main/assets/droiddeck-esync/index.json.sig
|
||||
ls -lR app/src/main/assets/droiddeck-esync
|
||||
fi
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"
|
||||
tools/proot/build.sh app/src/main/jniLibs/arm64-v8a
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
curl -fsSL --retry 3 -o pa.tar.gz "$COMPONENT_SOURCE_URL"
|
||||
echo "$COMPONENT_SOURCE_SHA256 pa.tar.gz" | sha256sum -c -
|
||||
mkdir -p pa13 && tar -xzf pa.tar.gz -C pa13 --strip-components=1 && rm pa.tar.gz
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
D=app/src/main/assets/linuxfs
|
||||
mkdir -p "$D"
|
||||
VK_INCLUDE=$(ls -d "${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"/toolchains/llvm/prebuilt/*/sysroot/usr/include | head -1) \
|
||||
tools/vr/build.sh --native "$D/usr/local/lib/droiddeck-vr"
|
||||
for script in tools/linuxfs/overlay/usr/local/bin/droiddeck-* tools/linuxfs/overlay/usr/local/bin/steam-compatibility; do
|
||||
install -Dm644 "$script" "$D/usr/local/bin/$(basename "$script")"
|
||||
done
|
||||
for f in tools/linuxfs/overlay/usr/bin/* tools/linuxfs/overlay/usr/bin/steamos-polkit-helpers/*; do
|
||||
[ -f "$f" ] && install -Dm644 "$f" "$D/${f#tools/linuxfs/overlay/}"
|
||||
done
|
||||
if [ -f tools/gamescope/release.env ]; then
|
||||
. tools/gamescope/release.env
|
||||
curl -fsSL --retry 3 -o gamescope.tzst "https://github.com/${GAMESCOPE_REPO:-${GITHUB_REPOSITORY}}/releases/download/${GAMESCOPE_TAG}/gamescope.tzst"
|
||||
echo "${GAMESCOPE_SHA256} gamescope.tzst" | sha256sum -c -
|
||||
tar --use-compress-program=unzstd -xf gamescope.tzst -C "$D"
|
||||
test -f "$D/usr/local/bin/gamescope"
|
||||
rm gamescope.tzst
|
||||
fi
|
||||
. tools/linuxfs/uruntime.env
|
||||
curl -fsSL --retry 3 -o uruntime "https://github.com/VHSgunzo/uruntime/releases/download/${URUNTIME_VERSION}/${URUNTIME_ASSET}"
|
||||
echo "${URUNTIME_SHA256} uruntime" | sha256sum -c -
|
||||
install -Dm644 uruntime "$D/usr/local/lib/droiddeck/uruntime"
|
||||
install -Dm644 tools/linuxfs/licenses/uruntime-LICENSE "$D/usr/local/share/licenses/uruntime/LICENSE"
|
||||
rm uruntime
|
||||
M="$D/usr/local/lib/mangoapp"
|
||||
mkdir -p "$M" mango-pkgs
|
||||
grep -v '^#' tools/mangoapp/packages.txt | while read -r sum url; do
|
||||
[ -n "$url" ] || continue
|
||||
curl -fsSL --retry 3 -o mango-pkgs/pkg.tar.zst "$url"
|
||||
echo "$sum mango-pkgs/pkg.tar.zst" | sha256sum -c -
|
||||
tar --use-compress-program=unzstd -xf mango-pkgs/pkg.tar.zst -C mango-pkgs
|
||||
rm mango-pkgs/pkg.tar.zst
|
||||
done
|
||||
install -m644 mango-pkgs/usr/bin/mangoapp "$M/mangoapp"
|
||||
for so in libfmt.so.10 libspdlog.so.1.13 libglfw.so.3 libtraceevent.so.1; do
|
||||
cp -L "mango-pkgs/usr/lib/$so" "$M/$so"
|
||||
done
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -Wl,-soname,libtracefs.so.1 \
|
||||
-o "$M/libtracefs.so.1" tools/mangoapp/libtracefs-shim.c
|
||||
aarch64-linux-gnu-strip --strip-unneeded "$M/libtracefs.so.1"
|
||||
install -Dm644 tools/mangoapp/mangoapp "$D/usr/local/bin/mangoapp"
|
||||
rm -rf mango-pkgs
|
||||
ls -l "$M"
|
||||
mkdir -p pw-pkgs
|
||||
grep -v '^#' tools/pipewire/packages.txt | while read -r sum url; do
|
||||
[ -n "$url" ] || continue
|
||||
curl -fsSL --retry 3 -o pw-pkgs/pkg.tar.zst "$url"
|
||||
echo "$sum pw-pkgs/pkg.tar.zst" | sha256sum -c -
|
||||
tar --use-compress-program=unzstd -xf pw-pkgs/pkg.tar.zst -C pw-pkgs
|
||||
rm pw-pkgs/pkg.tar.zst
|
||||
done
|
||||
tools/pipewire/assemble.sh pw-pkgs "$D/usr/local/lib/droiddeck-pipewire.tzst"
|
||||
rm -rf pw-pkgs
|
||||
ls -l "$D/usr/local/lib/droiddeck-pipewire.tzst"
|
||||
T="$D/usr/local/lib/droiddeck-msitools"
|
||||
mkdir -p "$T" msi-pkgs
|
||||
grep -v '^#' tools/msitools/packages.txt | while read -r sum url; do
|
||||
[ -n "$url" ] || continue
|
||||
curl -fsSL --retry 3 -o msi-pkgs/pkg.tar.xz "$url"
|
||||
echo "$sum msi-pkgs/pkg.tar.xz" | sha256sum -c -
|
||||
tar -xJf msi-pkgs/pkg.tar.xz -C msi-pkgs
|
||||
rm msi-pkgs/pkg.tar.xz
|
||||
done
|
||||
install -m644 msi-pkgs/usr/bin/msiinfo "$T/msiinfo"
|
||||
install -m644 msi-pkgs/usr/bin/cabextract "$T/cabextract"
|
||||
install -m644 msi-pkgs/usr/lib/7zip/7z "$T/7z"
|
||||
install -m644 msi-pkgs/usr/lib/7zip/7z.so "$T/7z.so"
|
||||
for so in libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do
|
||||
cp -L "msi-pkgs/usr/lib/$so" "$T/$so"
|
||||
done
|
||||
for f in NOTICE GPL-2 GPL-3 LGPL-2.1 7zip-License 7zip-unRarLicense; do
|
||||
install -Dm644 "tools/msitools/licenses/$f" "$D/usr/local/share/licenses/droiddeck-msitools/$f"
|
||||
done
|
||||
rm -rf msi-pkgs
|
||||
ls -l "$T"
|
||||
install -Dm644 tools/linuxfs/desktop/droiddeck-desktop "$D/usr/local/bin/droiddeck-desktop"
|
||||
install -Dm644 tools/linuxfs/desktop/droiddeck-gpu "$D/usr/local/bin/droiddeck-gpu"
|
||||
install -Dm644 tools/linuxfs/desktop/droiddeck-desktop-gpu "$D/usr/local/bin/droiddeck-desktop-gpu"
|
||||
install -Dm644 tools/linuxfs/desktop/kwin_wayland_wrapper "$D/usr/local/bin/kwin_wayland_wrapper"
|
||||
install -Dm644 tools/linuxfs/desktop/firefox-droiddeck.js "$D/usr/lib/firefox/defaults/pref/droiddeck.js"
|
||||
ls -lR "$D"
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
docker run --rm -v "$PWD:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c '
|
||||
set -euo pipefail
|
||||
install_compilers() {
|
||||
apt-get -o Acquire::Check-Valid-Until=false update -qq &&
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends gcc g++ gcc-multilib g++-multilib binutils >/dev/null
|
||||
}
|
||||
printf "deb http://deb.debian.org/debian bullseye main\ndeb http://security.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list
|
||||
if ! install_compilers; then
|
||||
printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list
|
||||
install_compilers
|
||||
fi
|
||||
tools/linuxfs/build-x86-preloads.sh app/src/main/assets/linuxfs
|
||||
'
|
||||
sudo chown -R "$(id -u):$(id -g)" app/src/main/assets/linuxfs
|
||||
ls -l app/src/main/assets/linuxfs/x86_64 app/src/main/assets/linuxfs/i386
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
rustup toolchain install 1.99.0 --profile minimal
|
||||
bash tools/xbox/build.sh
|
||||
@@ -227,7 +227,9 @@ if [ -z "${BL_INSIDE:-}" ]; then
|
||||
echo "== gamescope: games at ${nested}x${height} stretched to the ${width}x${height} output"
|
||||
fi
|
||||
fi
|
||||
if [ "$mode" = steam ]; then
|
||||
# Off unless Performance asks for it. Compositing the cursor here adds a second
|
||||
# plane on the phone's compositor; a controller-driven cursor is the reason to turn it on.
|
||||
if [ "$mode" = steam ] && [ "${BL_CURSOR_COMPOSITE:-0}" = 1 ]; then
|
||||
export gamescope_cursor_composite=2
|
||||
fi
|
||||
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$height")
|
||||
@@ -403,6 +405,11 @@ case $mode in
|
||||
droiddeck-steam-x64 --sync-login || true
|
||||
fi
|
||||
steam_client=("$steam_root/steamrtarm64/steam")
|
||||
_bl_srt_shim=$HOME/.droiddeck-steamrt-shim
|
||||
if droiddeck-steamrt-shim "$steam_root/steamrtarm64" "$_bl_srt_shim"; then
|
||||
echo "== steam: Valve containerized the arm64 Steam client (it carries a Steam Runtime container); starting it through $_bl_srt_shim"
|
||||
steam_client=(env STEAM_RUNTIME_STEAMRT="$_bl_srt_shim" PATH="$_bl_srt_shim/bin:$PATH" "${steam_client[@]}")
|
||||
fi
|
||||
fi
|
||||
_bl_proton_root=$steam_root
|
||||
if [ "$steam_x64" = 1 ]; then
|
||||
@@ -526,6 +533,11 @@ case $mode in
|
||||
fi
|
||||
droiddeck-seed-redists "$steam_root" || true
|
||||
fi
|
||||
if [ -n "${BL_SHADER_PRECACHE:-}" ]; then
|
||||
droiddeck-steam-shaders "$BL_SHADER_PRECACHE" "$XDG_DATA_HOME/Steam" \
|
||||
"${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam" &&
|
||||
echo "$BL_SHADER_PRECACHE" > "$HOME/.bl-shader-precache-applied" || true
|
||||
fi
|
||||
if [ "$steam_x64" = 0 ] && [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
|
||||
step "asking the client to fetch the ARM64 Proton"
|
||||
set -- "$@" steam://install/4427310
|
||||
@@ -746,10 +758,13 @@ BUSCONF
|
||||
fi
|
||||
|
||||
# The UI bridge launches SteamVR, which needs the guest agent's library environment.
|
||||
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME)
|
||||
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME -u STEAM_STEAMRT_RECURSION_GUARD)
|
||||
[ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH")
|
||||
[ "${STEAM_RUNTIME+x}" ] && _bl_compat_env+=("STEAM_RUNTIME=$STEAM_RUNTIME")
|
||||
[ "${STEAM_STEAMRT_RECURSION_GUARD+x}" ] && _bl_compat_env+=("STEAM_STEAMRT_RECURSION_GUARD=$STEAM_STEAMRT_RECURSION_GUARD")
|
||||
export STEAM_RUNTIME=1
|
||||
# A containerized client re-executes itself through pressure-vessel-wrap (bwrap) unless told it is already inside.
|
||||
export STEAM_STEAMRT_RECURSION_GUARD=1
|
||||
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
|
||||
export SDL_VIDEO_X11_DGAMOUSE=0
|
||||
decky_loader=$HOME/homebrew/services/PluginLoader
|
||||
@@ -829,6 +844,9 @@ BUSCONF
|
||||
unset _stale _f
|
||||
fi
|
||||
steam_channel=steamdeck_publicbeta
|
||||
if [ "$steam_x64" = 0 ]; then
|
||||
steam_channel=$(droiddeck-steam-install --channel 2>/dev/null || echo steamdeck_publicbeta)
|
||||
fi
|
||||
_bl_lang=()
|
||||
[ -n "${BL_STEAM_LANGUAGE:-}" ] && _bl_lang=(-language "$BL_STEAM_LANGUAGE")
|
||||
steam_beta=$steam_root/package/beta
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
#!/bin/bash
|
||||
set -eu
|
||||
cdn_list="https://client-update.steamstatic.com https://client-update.akamai.steamstatic.com https://client-update.fastly.steamstatic.com"
|
||||
steam_channel=steamdeck_publicbeta
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
beta_channel=steamdeck_publicbeta
|
||||
stable_channel=steamdeck_stable
|
||||
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
|
||||
channel_state=$steam_root/package/droiddeck-channel
|
||||
pinned=1
|
||||
wanted=${BL_STEAM_CHANNEL:-$(cat "$HOME/.config/droiddeck/steam-channel" 2>/dev/null || true)}
|
||||
case $wanted in
|
||||
stable|"$stable_channel") steam_channel=$stable_channel ;;
|
||||
beta|"$beta_channel") steam_channel=$beta_channel ;;
|
||||
*)
|
||||
pinned=
|
||||
steam_channel=$beta_channel
|
||||
[ "$(cat "$channel_state" 2>/dev/null)" = "$stable_channel" ] && steam_channel=$stable_channel
|
||||
;;
|
||||
esac
|
||||
if [ "${1:-}" = --channel ]; then
|
||||
echo "$steam_channel"
|
||||
exit 0
|
||||
fi
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
stamp=$steam_root/package/droiddeck-installed
|
||||
legacy_stamp=$steam_root/package/bannerlator-installed
|
||||
unconfirmed=$steam_root/package/droiddeck-unconfirmed
|
||||
@@ -12,11 +29,21 @@ if [ -f "$legacy_stamp" ] && [ ! -f "$stamp" ]; then
|
||||
mv -f "$legacy_stamp" "$stamp" || true
|
||||
fi
|
||||
repair=""
|
||||
fallback=""
|
||||
starts=0
|
||||
[ -f "$unconfirmed" ] && starts=$(grep -cx start "$unconfirmed" || true)
|
||||
if [ -e "$repair_request" ]; then
|
||||
repair="requested"
|
||||
elif [ -f "$unconfirmed" ] && ! grep -qx repaired "$unconfirmed" \
|
||||
&& [ "$(grep -cx start "$unconfirmed")" -ge 2 ]; then
|
||||
repair="the client was started $(grep -cx start "$unconfirmed") times without showing anything"
|
||||
elif [ "$starts" -ge 2 ] && ! grep -qx repaired "$unconfirmed"; then
|
||||
repair="the client was started $starts times without showing anything"
|
||||
elif [ "$starts" -ge 2 ] && [ -z "$pinned" ] && [ "$steam_channel" = "$beta_channel" ] && [ "${1:-}" != --sync ]; then
|
||||
repair="the repaired $beta_channel client was started $starts more times without showing anything"
|
||||
fallback=1
|
||||
fi
|
||||
if [ -n "$fallback" ]; then
|
||||
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $repair"
|
||||
steam_channel=$stable_channel
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
fi
|
||||
if [ -n "$repair" ]; then
|
||||
echo "== STEP $(date -u +%H:%M:%S) repairing the Steam client ($repair)"
|
||||
@@ -42,20 +69,42 @@ fi
|
||||
|
||||
work=$(mktemp -d)
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
cdn=""
|
||||
for candidate in $cdn_list; do
|
||||
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
|
||||
"$candidate/$manifest" -o "$work/manifest"; then
|
||||
cdn=$candidate
|
||||
break
|
||||
fetch_manifest() {
|
||||
cdn=""
|
||||
for candidate in $cdn_list; do
|
||||
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
|
||||
"$candidate/$manifest" -o "$work/manifest"; then
|
||||
cdn=$candidate
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
|
||||
cdn_order=("$cdn")
|
||||
for candidate in $cdn_list; do
|
||||
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
|
||||
done
|
||||
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
|
||||
awk -F'"' '
|
||||
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
|
||||
$2 == "file" { file[block] = $4 }
|
||||
$2 == "sha2" { sha[block] = $4 }
|
||||
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
|
||||
' "$work/manifest" | sort > "$work/parts"
|
||||
[ -s "$work/parts" ] && return 0
|
||||
echo "droiddeck-steam-install: no client components in $manifest; its blocks are:" \
|
||||
"$(awk -F'"' '/^\t"/ && NF >= 3 && $4 == "" { printf "%s ", $2 }' "$work/manifest")" >&2
|
||||
return 1
|
||||
}
|
||||
if ! fetch_manifest; then
|
||||
if [ -n "$sync" ] || [ -n "$pinned" ] || [ "$steam_channel" = "$stable_channel" ]; then
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
|
||||
cdn_order=("$cdn")
|
||||
for candidate in $cdn_list; do
|
||||
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
|
||||
done
|
||||
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
|
||||
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $manifest has no arm64 client"
|
||||
steam_channel=$stable_channel
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
fallback=1
|
||||
fetch_manifest || exit 1
|
||||
fi
|
||||
if [ -n "$sync" ]; then
|
||||
installed=$(awk -F'"' '$2 == "version" { print $4; exit }' "$steam_root/package/$manifest.manifest" 2>/dev/null || true)
|
||||
if [ -x "$steam_root/steamrtarm64/steam" ] && [ "$installed" = "$version" ]; then
|
||||
@@ -64,13 +113,6 @@ if [ -n "$sync" ]; then
|
||||
fi
|
||||
echo "== STEP $(date -u +%H:%M:%S) updating the arm64 Steam client to $version (was ${installed:-none}) for the x86-64 client"
|
||||
fi
|
||||
awk -F'"' '
|
||||
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
|
||||
$2 == "file" { file[block] = $4 }
|
||||
$2 == "sha2" { sha[block] = $4 }
|
||||
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
|
||||
' "$work/manifest" | sort > "$work/parts"
|
||||
[ -s "$work/parts" ] || { echo "droiddeck-steam-install: no client components in $manifest" >&2; exit 1; }
|
||||
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
|
||||
|
||||
mkdir -p "$steam_root/package"
|
||||
@@ -117,6 +159,10 @@ ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
|
||||
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
|
||||
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
|
||||
echo "$version" > "$stamp"
|
||||
if [ -n "$fallback" ]; then
|
||||
echo "$stable_channel" > "$channel_state"
|
||||
echo "== STEP $(date -u +%H:%M:%S) the Steam client now follows $stable_channel (beta in ~/.config/droiddeck/steam-channel returns to $beta_channel)"
|
||||
fi
|
||||
if [ -n "$repair" ]; then
|
||||
echo repaired > "$unconfirmed"
|
||||
rm -f "$repair_request"
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python3
|
||||
"""Sets the Steam client's shader pre-caching (Settings > Downloads), before the client starts.
|
||||
|
||||
droiddeck-steam-shaders <0|1> <steam root>...
|
||||
|
||||
The client stores the toggle as InstallConfigStore/Software/Valve/Steam/ShaderCacheManager/
|
||||
DisableShaderCache in config/config.vdf: "1" when off, "0" when on. It keeps the setting in memory
|
||||
and writes config.vdf back on exit, so this runs only while the client is stopped. The launcher
|
||||
passes a value only after its own toggle changes; otherwise the client's toggle stays the user's."""
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import sys
|
||||
|
||||
|
||||
VDF = runpy.run_path(str(Path(__file__).with_name("droiddeck-steam-ui-scale")))
|
||||
PATH = ("Software", "Valve", "Steam", "ShaderCacheManager", "DisableShaderCache")
|
||||
|
||||
|
||||
def set_text(text, enabled):
|
||||
value = "0" if enabled else "1"
|
||||
updated, changed = VDF["seed_value"](text, PATH, value)
|
||||
if changed:
|
||||
return updated, True
|
||||
block = VDF["parse"](text).get("InstallConfigStore")
|
||||
for section in PATH[:-1]:
|
||||
block = block.get(section)
|
||||
current = block.get(PATH[-1])
|
||||
if isinstance(current, VDF["Block"]):
|
||||
raise ValueError("invalid DisableShaderCache setting")
|
||||
if current == value:
|
||||
return text, False
|
||||
at = block.offset(PATH[-1])
|
||||
end = VDF["TOKEN"].match(text, at).end()
|
||||
return text[:at] + f'"{value}"' + text[end:], True
|
||||
|
||||
|
||||
def apply(steam_root, enabled):
|
||||
config = Path(steam_root) / "config/config.vdf"
|
||||
text = config.read_text() if config.exists() else '"InstallConfigStore"\n{\n}\n'
|
||||
updated, changed = set_text(text, enabled)
|
||||
if changed:
|
||||
VDF["atomic_write"](config, updated)
|
||||
return changed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 3 or sys.argv[1] not in ("0", "1"):
|
||||
print("usage: droiddeck-steam-shaders <0|1> <steam root>...", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
enabled = sys.argv[1] == "1"
|
||||
status = 0
|
||||
for root in sys.argv[2:]:
|
||||
if not Path(root).is_dir():
|
||||
continue
|
||||
try:
|
||||
if apply(root, enabled):
|
||||
print(f"== steam: shader pre-caching {'on' if enabled else 'off'} in {root}")
|
||||
except (OSError, ValueError, RecursionError) as error:
|
||||
print(f"== steam: left shader pre-caching unchanged in {root}: {error}", file=sys.stderr)
|
||||
status = 1
|
||||
sys.exit(status)
|
||||
@@ -26,9 +26,10 @@ TOKEN = re.compile(r'\s+|//[^\n]*|"(?:\\.|[^"\\])*"|[{}]')
|
||||
|
||||
|
||||
class Block:
|
||||
def __init__(self, pairs, end):
|
||||
def __init__(self, pairs, end, offsets=()):
|
||||
self.pairs = pairs
|
||||
self.end = end
|
||||
self.offsets = offsets
|
||||
|
||||
def get(self, name):
|
||||
matches = [value for key, value in self.pairs if key.lower() == name.lower()]
|
||||
@@ -36,6 +37,9 @@ class Block:
|
||||
raise ValueError("duplicate UI setting")
|
||||
return matches[0] if matches else None
|
||||
|
||||
def offset(self, name):
|
||||
return next(at for (key, _), at in zip(self.pairs, self.offsets) if key.lower() == name.lower())
|
||||
|
||||
|
||||
def parse(text):
|
||||
tokens = []
|
||||
@@ -51,17 +55,17 @@ def parse(text):
|
||||
raise ValueError("invalid config.vdf")
|
||||
|
||||
def block(index, nested=False):
|
||||
pairs = []
|
||||
pairs, offsets = [], []
|
||||
while index < len(tokens):
|
||||
token, offset = tokens[index]
|
||||
if token == '}':
|
||||
if not nested:
|
||||
raise ValueError("unexpected closing brace")
|
||||
return Block(pairs, offset), index + 1
|
||||
return Block(pairs, offset, offsets), index + 1
|
||||
if not token.startswith('"') or index + 1 == len(tokens):
|
||||
raise ValueError("missing config value")
|
||||
key = token[1:-1]
|
||||
value = tokens[index + 1][0]
|
||||
value, at = tokens[index + 1]
|
||||
index += 2
|
||||
if value == '{':
|
||||
value, index = block(index, True)
|
||||
@@ -70,9 +74,10 @@ def parse(text):
|
||||
else:
|
||||
raise ValueError("invalid config value")
|
||||
pairs.append((key, value))
|
||||
offsets.append(at)
|
||||
if nested:
|
||||
raise ValueError("unclosed config block")
|
||||
return Block(pairs, len(text)), index
|
||||
return Block(pairs, len(text), offsets), index
|
||||
|
||||
return block(0)[0]
|
||||
|
||||
|
||||
@@ -520,39 +520,8 @@ ln -sfn "$STEAM_X64/ubuntu12_32" "$X64_HOME/.steam/bin32"
|
||||
ln -sfn "$STEAM_X64/ubuntu12_64" "$X64_HOME/.steam/bin64"
|
||||
ln -sfn "$STEAM_X64/linux32" "$X64_HOME/.steam/sdk32"
|
||||
ln -sfn "$STEAM_X64/linux64" "$X64_HOME/.steam/sdk64"
|
||||
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
|
||||
srt_real=$STEAM_X64/steamrt64/pv-runtime/steam-runtime-steamrt
|
||||
srt_shim=$X64_HOME/steamrt-shim
|
||||
if [ -d "$srt_real" ]; then
|
||||
rm -rf "$srt_shim"
|
||||
mkdir -p "$srt_shim/bin"
|
||||
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
|
||||
[ -e "$e" ] || continue
|
||||
[ "$(basename "$e")" = bin ] || ln -s "$e" "$srt_shim/$(basename "$e")"
|
||||
done
|
||||
for e in "$srt_real"/bin/*; do ln -s "$e" "$srt_shim/bin/$(basename "$e")"; done
|
||||
rm -f "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
printf '#!/bin/sh
|
||||
exit 0
|
||||
' > "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
chmod 755 "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
rm -f "$srt_shim/bin/steam-runtime-launch-client"
|
||||
cat > "$srt_shim/bin/steam-runtime-launch-client" <<'LAUNCH'
|
||||
#!/bin/sh
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--) shift; break ;;
|
||||
--directory=*) cd "${1#--directory=}" || exit 127 ;;
|
||||
--env=*) export "${1#--env=}" ;;
|
||||
--unset-env=*) unset "${1#--unset-env=}" ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
[ $# -gt 0 ] || exit 0
|
||||
exec "$@"
|
||||
LAUNCH
|
||||
chmod 755 "$srt_shim/bin/steam-runtime-launch-client"
|
||||
fi
|
||||
# Valve's Steam Runtime container needs user namespaces and a session-bus launcher; steamrt-shim stands in for both.
|
||||
droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"
|
||||
# Valve's platform libraries ship without soname links (pressure-vessel makes them); ldconfig adds them here.
|
||||
srt_libs=$X64_HOME/steamrt-libs
|
||||
rm -rf "$srt_libs"
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
# droiddeck-steamrt-shim <client runtime dir> <shim dir>
|
||||
# Builds <shim dir> from the Steam Runtime container the client carries and exits 0, or removes it and exits 1
|
||||
# when the client carries none.
|
||||
set -eu
|
||||
client=$1
|
||||
shim=$2
|
||||
srt_real=$client/pv-runtime/steam-runtime-steamrt
|
||||
if [ ! -d "$srt_real" ]; then
|
||||
probe=$(find "$client" -maxdepth 4 -name steam-runtime-check-requirements -path '*/bin/*' -print -quit 2>/dev/null || true)
|
||||
srt_real=${probe:+${probe%/bin/*}}
|
||||
fi
|
||||
if [ -z "$srt_real" ] || [ ! -d "$srt_real" ]; then
|
||||
rm -rf "$shim"
|
||||
exit 1
|
||||
fi
|
||||
rm -rf "$shim"
|
||||
trap 'rm -rf "$shim"' ERR
|
||||
mkdir -p "$shim/bin"
|
||||
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
|
||||
[ -e "$e" ] || continue
|
||||
[ "$(basename "$e")" = bin ] || ln -s "$e" "$shim/$(basename "$e")"
|
||||
done
|
||||
for e in "$srt_real"/bin/*; do
|
||||
[ -e "$e" ] || continue
|
||||
ln -s "$e" "$shim/bin/$(basename "$e")"
|
||||
done
|
||||
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
|
||||
rm -f "$shim/bin/steam-runtime-check-requirements"
|
||||
printf '#!/bin/sh
|
||||
exit 0
|
||||
' > "$shim/bin/steam-runtime-check-requirements"
|
||||
chmod 755 "$shim/bin/steam-runtime-check-requirements"
|
||||
# Valve's launcher asks a service on the session bus; proot is already the container, so the command runs here.
|
||||
rm -f "$shim/bin/steam-runtime-launch-client"
|
||||
cat > "$shim/bin/steam-runtime-launch-client" <<'LAUNCH'
|
||||
#!/bin/sh
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--) shift; break ;;
|
||||
--directory=*) cd "${1#--directory=}" || exit 127 ;;
|
||||
--env=*) export "${1#--env=}" ;;
|
||||
--unset-env=*) unset "${1#--unset-env=}" ;;
|
||||
--directory|--env|--unset-env)
|
||||
[ $# -ge 2 ] || exit 127
|
||||
case $1 in
|
||||
--directory) cd "$2" || exit 127 ;;
|
||||
--env) export "$2" ;;
|
||||
--unset-env) unset "$2" ;;
|
||||
esac
|
||||
shift ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
[ $# -gt 0 ] || exit 0
|
||||
exec "$@"
|
||||
LAUNCH
|
||||
chmod 755 "$shim/bin/steam-runtime-launch-client"
|
||||
+279
-74
@@ -17,6 +17,7 @@
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct ns_sem_args {
|
||||
@@ -68,12 +69,13 @@ struct ns_wait_args {
|
||||
#define NS_PROCS 256u
|
||||
#define NS_FDS 65536
|
||||
#define NS_DEVS 8
|
||||
#define NS_VERSION 1u
|
||||
#define NS_VERSION 2u
|
||||
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
|
||||
#define NS_DEV_MAGIC 0x4e54445600000000ull
|
||||
#define NS_OBJ_MAGIC 0x27ull
|
||||
#define NS_WAITERS 0x80000000u
|
||||
#define NS_SWEEP_NS 2000000000ull
|
||||
#define NS_SPIN_NS 4000ull
|
||||
#define NS_DEAD 0xffffffffu
|
||||
|
||||
#ifndef CLOSE_RANGE_CLOEXEC
|
||||
@@ -144,6 +146,7 @@ struct ns_q {
|
||||
uint32_t waiting;
|
||||
uint32_t inuse;
|
||||
uint32_t next_free;
|
||||
uint32_t sleeping;
|
||||
struct ns_ent ent[NS_ENTS];
|
||||
};
|
||||
|
||||
@@ -168,6 +171,7 @@ struct ns_tls {
|
||||
struct ns_dev *d;
|
||||
uint32_t q;
|
||||
int waiting;
|
||||
int sleep;
|
||||
};
|
||||
|
||||
static struct ns_dev ns_devs[NS_DEVS];
|
||||
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ns_block(sigset_t *old) {
|
||||
sigset_t s;
|
||||
sigfillset(&s);
|
||||
sigdelset(&s, SIGSEGV);
|
||||
sigdelset(&s, SIGBUS);
|
||||
sigdelset(&s, SIGILL);
|
||||
sigdelset(&s, SIGFPE);
|
||||
sigdelset(&s, SIGTRAP);
|
||||
pthread_sigmask(SIG_BLOCK, &s, old);
|
||||
#define NS_DEFER 4
|
||||
#define NS_LEVELS 8
|
||||
|
||||
struct ns_cs {
|
||||
int depth;
|
||||
int count;
|
||||
int level;
|
||||
unsigned held_levels;
|
||||
int sigs[NS_DEFER];
|
||||
siginfo_t info[NS_DEFER];
|
||||
sigset_t held[NS_LEVELS];
|
||||
};
|
||||
|
||||
static __thread struct ns_cs ns_cs;
|
||||
static uintptr_t ns_handlers[_NSIG];
|
||||
static uint8_t ns_siginfo[_NSIG];
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static int ns_deferrable(int sig) {
|
||||
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
|
||||
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
|
||||
}
|
||||
|
||||
static void ns_restore(const sigset_t *old) {
|
||||
pthread_sigmask(SIG_SETMASK, old, NULL);
|
||||
static int ns_defer(int sig, const siginfo_t *si) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
if (sig < SIGRTMIN)
|
||||
for (int i = 0; i < NS_DEFER; i++)
|
||||
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
|
||||
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
if (i >= NS_DEFER) {
|
||||
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
if (si) memcpy(&cs->info[i], si, sizeof(*si));
|
||||
else memset(&cs->info[i], 0, sizeof(*si));
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
|
||||
int level = ns_cs.level;
|
||||
if (level < 0 || level >= NS_LEVELS) return 0;
|
||||
sigset_t one;
|
||||
sigemptyset(&one);
|
||||
sigaddset(&one, sig);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &one, NULL);
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
|
||||
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
|
||||
errno = saved;
|
||||
if (!ok) return 0;
|
||||
sigaddset(&uc->uc_sigmask, sig);
|
||||
unsigned long *words = (unsigned long *)&ns_cs.held[level];
|
||||
unsigned bits = 8 * sizeof(unsigned long);
|
||||
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
|
||||
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
|
||||
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
|
||||
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
if (!h) return;
|
||||
ns_cs.level++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
|
||||
else ((void (*)(int))h)(sig);
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
ns_cs.level--;
|
||||
}
|
||||
|
||||
static void ns_flush(void) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
sigset_t all, mask;
|
||||
sigfillset(&all);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &all, &mask);
|
||||
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
for (int i = 0; i < n; i++)
|
||||
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
|
||||
memset(cs->sigs, 0, sizeof(cs->sigs));
|
||||
cs->count = 0;
|
||||
int level = cs->level;
|
||||
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
|
||||
for (int sig = 1; sig < _NSIG; sig++)
|
||||
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
|
||||
sigemptyset(&cs->held[level]);
|
||||
cs->held_levels &= ~(1u << level);
|
||||
}
|
||||
pthread_sigmask(SIG_SETMASK, &mask, NULL);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static void ns_block(void) {
|
||||
ns_cs.depth++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
static void ns_restore(void) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (--ns_cs.depth == 0) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
|
||||
ns_flush();
|
||||
}
|
||||
}
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
|
||||
static int (*real)(int, const struct sigaction *, struct sigaction *);
|
||||
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
|
||||
if (!real) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
|
||||
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
|
||||
struct sigaction mine;
|
||||
const struct sigaction *use = act;
|
||||
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
|
||||
bl_from_ntdll(__builtin_return_address(0))) {
|
||||
mine = *act;
|
||||
mine.sa_sigaction = ns_trampoline;
|
||||
mine.sa_flags |= SA_SIGINFO;
|
||||
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
|
||||
use = &mine;
|
||||
}
|
||||
int r = real(sig, use, old);
|
||||
if (r != 0 && use == &mine) {
|
||||
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
|
||||
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
|
||||
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
|
||||
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
|
||||
if (dead) return 0;
|
||||
}
|
||||
int32_t want = -1;
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void ns_wake(struct ns_q *q) {
|
||||
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
|
||||
int saved = errno;
|
||||
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
|
||||
errno = saved;
|
||||
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
|
||||
|
||||
static void ns_dev_put(struct ns_dev *d) {
|
||||
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
uint32_t zero = 0;
|
||||
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
|
||||
}
|
||||
|
||||
static void ns_forget(int fd) {
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
ns_forget_locked(fd);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
|
||||
uint64_t e = 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
|
||||
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
|
||||
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
uint64_t e = 0;
|
||||
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
|
||||
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
|
||||
if (d) {
|
||||
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
|
||||
|
||||
static void ns_thread_gone(void *p) {
|
||||
struct ns_tls *t = p;
|
||||
if (!t || !t->d || !t->q) return;
|
||||
sigset_t old;
|
||||
if (!t || !t->d) return;
|
||||
struct ns_dev *d = t->d;
|
||||
ns_block(&old);
|
||||
ns_free_q(d->h, t->q - 1);
|
||||
ns_block();
|
||||
if (t->q) ns_free_q(d->h, t->q - 1);
|
||||
t->q = 0;
|
||||
t->waiting = 0;
|
||||
t->d = NULL;
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
ns_dev_put(d);
|
||||
}
|
||||
|
||||
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
|
||||
pthread_key_create(&ns_key, ns_thread_gone);
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.d == d && ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
if (ns_tq.d && ns_tq.q) {
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
ns_free_q(old->h, ns_tq.q - 1);
|
||||
static int ns_hold(struct ns_dev *d) {
|
||||
if (ns_tq.d == d) return 1;
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
if (ns_tq.sleep) return 2;
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
if (old) {
|
||||
ns_block();
|
||||
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
|
||||
ns_tq.q = 0;
|
||||
ns_tq.waiting = 0;
|
||||
ns_tq.d = NULL;
|
||||
ns_restore();
|
||||
ns_dev_put(old);
|
||||
}
|
||||
pthread_once(&ns_once, ns_key_init);
|
||||
ns_tq.d = d;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
uint32_t i;
|
||||
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
|
||||
ns_sweep_dead(h);
|
||||
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_q *q = ns_q(h, i);
|
||||
q->proc = (uint32_t)d->proc;
|
||||
q->waiting = 0;
|
||||
q->sleeping = 0;
|
||||
q->inuse = 1;
|
||||
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
|
||||
ns_tq.d = d;
|
||||
ns_tq.q = i + 1;
|
||||
ns_tq.waiting = 0;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
*qi = i;
|
||||
return q;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d != d) return NULL;
|
||||
if (ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
struct ns_q *q = ns_new_q(d, qi);
|
||||
if (!q) return NULL;
|
||||
ns_tq.q = *qi + 1;
|
||||
ns_tq.waiting = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
struct ns_head *h = d->h;
|
||||
sigset_t old;
|
||||
int fd = -1, err = 0;
|
||||
uint32_t s = 0;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!ns_table() || ns_attach(d) < 0) {
|
||||
err = ENOMEM;
|
||||
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (fd < 0) errno = err;
|
||||
return fd;
|
||||
}
|
||||
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
|
||||
}
|
||||
|
||||
static uint64_t ns_now(void) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
|
||||
}
|
||||
|
||||
static void ns_spin(struct ns_q *q) {
|
||||
uint64_t end = ns_now() + NS_SPIN_NS;
|
||||
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
|
||||
ns_relax();
|
||||
if (!(i & 63) && ns_now() >= end) return;
|
||||
}
|
||||
}
|
||||
|
||||
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
struct timespec ts, *tsp = NULL;
|
||||
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
|
||||
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
|
||||
tsp = &ts;
|
||||
}
|
||||
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
if (tsp && ns_expired(args)) return ETIMEDOUT;
|
||||
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
|
||||
if (errno == ETIMEDOUT) return ETIMEDOUT;
|
||||
if (errno == EINTR) return EINTR;
|
||||
}
|
||||
ns_spin(q);
|
||||
int ret = 0;
|
||||
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
|
||||
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
|
||||
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
|
||||
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
|
||||
ret = errno;
|
||||
}
|
||||
return 0;
|
||||
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
|
||||
for (uint32_t i = 0; i < total; i++) {
|
||||
struct ns_obj *o = ns_obj(h, slots[i]);
|
||||
ns_lock(&o->lock);
|
||||
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
|
||||
if (!busy && ns_signaled(o, owner)) {
|
||||
switch (o->type) {
|
||||
case NS_SEM:
|
||||
o->a--;
|
||||
break;
|
||||
case NS_MUTEX:
|
||||
*dead = (int)o->ownerdead;
|
||||
o->ownerdead = 0;
|
||||
o->a++;
|
||||
o->b = owner;
|
||||
break;
|
||||
case NS_EVENT:
|
||||
if (!o->a) o->b = 0;
|
||||
break;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
return (int)i;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
if (busy) return -1;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
for (uint32_t j = 0; j < i; j++)
|
||||
if (slots[j] == slots[i]) return EINVAL;
|
||||
}
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
int nested = ns_tq.sleep > 0;
|
||||
ns_block();
|
||||
if (!all && (nested || !ns_tq.waiting)) {
|
||||
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
|
||||
if (got >= 0) {
|
||||
ns_restore();
|
||||
ua->index = (uint32_t)got;
|
||||
return dead ? EOWNERDEAD : 0;
|
||||
}
|
||||
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
|
||||
ns_restore();
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
uint32_t qi;
|
||||
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
|
||||
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
|
||||
if (!q) {
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return ENOMEM;
|
||||
}
|
||||
ns_maybe_sweep(h);
|
||||
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
q->total = total;
|
||||
q->waiting = 1;
|
||||
ns_tq.waiting = 1;
|
||||
if (!nested) ns_tq.waiting = 1;
|
||||
int bad = 0;
|
||||
if (!all) {
|
||||
for (uint32_t i = 0; i < total && !bad; i++) {
|
||||
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
int ret = 0;
|
||||
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
sigset_t held;
|
||||
ns_restore(&old);
|
||||
ns_tq.sleep++;
|
||||
ns_restore();
|
||||
ret = ns_sleep(q, &args);
|
||||
ns_block(&held);
|
||||
ns_block();
|
||||
ns_tq.sleep--;
|
||||
}
|
||||
ns_drop_q(h, qi);
|
||||
ns_tq.waiting = 0;
|
||||
if (!nested) ns_tq.waiting = 0;
|
||||
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
|
||||
int dead = (int)q->ownerdead;
|
||||
ns_restore(&old);
|
||||
if (nested) ns_free_q(h, qi);
|
||||
ns_restore();
|
||||
if (bad) return EINVAL;
|
||||
if (got != -1) {
|
||||
ua->index = (uint32_t)got;
|
||||
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
return ENOTTY;
|
||||
}
|
||||
if (type != (uint32_t)want) return EINVAL;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
int all = ns_lock_obj(h, o);
|
||||
if (o->type != (uint32_t)want || o->gen != gen) {
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return EINVAL;
|
||||
}
|
||||
switch (req) {
|
||||
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
}
|
||||
}
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (ret) return ret;
|
||||
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
|
||||
memcpy(arg, &prev, sizeof(prev));
|
||||
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
|
||||
uint64_t e = ns_lookup(fd);
|
||||
if (!e) return 0;
|
||||
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
int held = ns_hold(d);
|
||||
if (!held) return 0;
|
||||
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
|
||||
ns_dev_put(d);
|
||||
if (held == 2) ns_dev_put(d);
|
||||
if (err) {
|
||||
errno = err;
|
||||
*rc = -1;
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#define STATUS_MAX 16384
|
||||
#define TRACER_FIELD "\nTracerPid:\t"
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static const char *skip_digits(const char *s) {
|
||||
const char *end = s;
|
||||
while (isdigit((unsigned char)*end)) end++;
|
||||
@@ -77,7 +79,8 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p
|
||||
int out = -1;
|
||||
|
||||
long pid;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller)) return -1;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller))
|
||||
return -1;
|
||||
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (in < 0) goto done;
|
||||
for (;;) {
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <link.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static uintptr_t ntdll_lo, ntdll_hi;
|
||||
|
||||
static int find_ntdll(struct dl_phdr_info *info, size_t size, void *data) {
|
||||
(void)size;
|
||||
(void)data;
|
||||
const char *slash = strrchr(info->dlpi_name, '/');
|
||||
if (strcmp(slash ? slash + 1 : info->dlpi_name, "ntdll.so") != 0) return 0;
|
||||
for (int i = 0; i < info->dlpi_phnum; i++) {
|
||||
const ElfW(Phdr) *ph = &info->dlpi_phdr[i];
|
||||
if (ph->p_type != PT_LOAD || !(ph->p_flags & PF_X)) continue;
|
||||
uintptr_t lo = info->dlpi_addr + ph->p_vaddr;
|
||||
__atomic_store_n(&ntdll_hi, lo + ph->p_memsz, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ntdll_lo, lo, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_from_ntdll(const void *caller) {
|
||||
uintptr_t pc = (uintptr_t)caller, lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) {
|
||||
dl_iterate_phdr(find_ntdll, NULL);
|
||||
lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) return 0;
|
||||
}
|
||||
return pc >= lo && pc < __atomic_load_n(&ntdll_hi, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
#if defined(__aarch64__)
|
||||
#include <asm/hwcap.h>
|
||||
#include <asm/sigcontext.h>
|
||||
|
||||
#define BL_STR2(x) #x
|
||||
#define BL_STR(x) BL_STR2(x)
|
||||
|
||||
#define BL_HWCAP_GCS (1UL << 32)
|
||||
#define BL_FRAME_SIZE 4688
|
||||
#define BL_REGS 312
|
||||
#define BL_SP 560
|
||||
#define BL_PC 568
|
||||
#define BL_PSTATE 576
|
||||
#define BL_FPSIMD 592
|
||||
|
||||
struct bl_sigframe {
|
||||
siginfo_t info;
|
||||
ucontext_t uc;
|
||||
};
|
||||
|
||||
struct bl_kernel_sigaction {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
unsigned long flags;
|
||||
void *restorer;
|
||||
uint64_t mask;
|
||||
};
|
||||
|
||||
struct bl_usr2_call {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
uint64_t old_mask;
|
||||
};
|
||||
|
||||
_Static_assert(sizeof(struct bl_sigframe) == BL_FRAME_SIZE, "rt_sigframe size");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.regs) == BL_REGS, "regs offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.sp) == BL_SP, "sp offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pc) == BL_PC, "pc offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pstate) == BL_PSTATE, "pstate offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.__reserved) == BL_FPSIMD, "reserved offset");
|
||||
_Static_assert(sizeof(struct fpsimd_context) + sizeof(struct _aarch64_ctx) <=
|
||||
sizeof(((mcontext_t *)0)->__reserved), "reserved space");
|
||||
|
||||
__attribute__((visibility("hidden"))) void bl_usr2_frame(struct bl_usr2_call *call);
|
||||
__attribute__((visibility("hidden"), used)) void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame);
|
||||
|
||||
__asm__(".text\n"
|
||||
".p2align 2\n"
|
||||
".type bl_usr2_frame, %function\n"
|
||||
"bl_usr2_frame:\n"
|
||||
" mov x16, #" BL_STR(BL_FRAME_SIZE) "\n"
|
||||
" sub sp, sp, x16\n"
|
||||
" add x17, sp, #" BL_STR(BL_REGS) "\n"
|
||||
" stp x0, x1, [x17, #0]\n"
|
||||
" stp x2, x3, [x17, #16]\n"
|
||||
" stp x4, x5, [x17, #32]\n"
|
||||
" stp x6, x7, [x17, #48]\n"
|
||||
" stp x8, x9, [x17, #64]\n"
|
||||
" stp x10, x11, [x17, #80]\n"
|
||||
" stp x12, x13, [x17, #96]\n"
|
||||
" stp x14, x15, [x17, #112]\n"
|
||||
" stp xzr, xzr, [x17, #128]\n"
|
||||
" stp x18, x19, [x17, #144]\n"
|
||||
" stp x20, x21, [x17, #160]\n"
|
||||
" stp x22, x23, [x17, #176]\n"
|
||||
" stp x24, x25, [x17, #192]\n"
|
||||
" stp x26, x27, [x17, #208]\n"
|
||||
" stp x28, x29, [x17, #224]\n"
|
||||
" str x30, [x17, #240]\n"
|
||||
" str xzr, [x17, #0]\n"
|
||||
" add x16, sp, x16\n"
|
||||
" str x16, [sp, #" BL_STR(BL_SP) "]\n"
|
||||
" adr x16, 1f\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PC) "]\n"
|
||||
" mrs x16, nzcv\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PSTATE) "]\n"
|
||||
" add x17, sp, #" BL_STR(BL_FPSIMD) "\n"
|
||||
" mrs x16, fpsr\n"
|
||||
" str w16, [x17, #8]\n"
|
||||
" mrs x16, fpcr\n"
|
||||
" str w16, [x17, #12]\n"
|
||||
" stp q0, q1, [x17, #16]\n"
|
||||
" stp q2, q3, [x17, #48]\n"
|
||||
" stp q4, q5, [x17, #80]\n"
|
||||
" stp q6, q7, [x17, #112]\n"
|
||||
" stp q8, q9, [x17, #144]\n"
|
||||
" stp q10, q11, [x17, #176]\n"
|
||||
" stp q12, q13, [x17, #208]\n"
|
||||
" stp q14, q15, [x17, #240]\n"
|
||||
" stp q16, q17, [x17, #272]\n"
|
||||
" stp q18, q19, [x17, #304]\n"
|
||||
" stp q20, q21, [x17, #336]\n"
|
||||
" stp q22, q23, [x17, #368]\n"
|
||||
" stp q24, q25, [x17, #400]\n"
|
||||
" stp q26, q27, [x17, #432]\n"
|
||||
" stp q28, q29, [x17, #464]\n"
|
||||
" stp q30, q31, [x17, #496]\n"
|
||||
" mov x1, sp\n"
|
||||
" bl bl_usr2_run\n"
|
||||
" mov x8, #" BL_STR(__NR_rt_sigreturn) "\n"
|
||||
" svc #0\n"
|
||||
" brk #0\n"
|
||||
"1:\n"
|
||||
" ret\n"
|
||||
".size bl_usr2_frame, .-bl_usr2_frame\n");
|
||||
|
||||
static int (*usr2_real_raise)(int);
|
||||
|
||||
static long usr2_sys(long nr, long a, long b, long c, long d) {
|
||||
register long x8 __asm__("x8") = nr;
|
||||
register long x0 __asm__("x0") = a;
|
||||
register long x1 __asm__("x1") = b;
|
||||
register long x2 __asm__("x2") = c;
|
||||
register long x3 __asm__("x3") = d;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
return x0;
|
||||
}
|
||||
|
||||
static uint64_t usr2_ssbs(void) {
|
||||
uint64_t value = 0;
|
||||
if (getauxval(AT_HWCAP) & HWCAP_SSBS) __asm__ volatile("mrs %0, s3_3_c4_c2_6" : "=r"(value));
|
||||
return value & (1UL << 12);
|
||||
}
|
||||
|
||||
void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame) {
|
||||
struct fpsimd_context *fp = (struct fpsimd_context *)frame->uc.uc_mcontext.__reserved;
|
||||
struct _aarch64_ctx *end = (struct _aarch64_ctx *)(fp + 1);
|
||||
|
||||
memset(&frame->info, 0, sizeof(frame->info));
|
||||
frame->info.si_signo = SIGUSR2;
|
||||
frame->info.si_code = SI_TKILL;
|
||||
frame->info.si_pid = (pid_t)usr2_sys(__NR_getpid, 0, 0, 0, 0);
|
||||
frame->info.si_uid = (uid_t)usr2_sys(__NR_getuid, 0, 0, 0, 0);
|
||||
frame->uc.uc_flags = 0;
|
||||
frame->uc.uc_link = NULL;
|
||||
if (usr2_sys(__NR_sigaltstack, 0, (long)&frame->uc.uc_stack, 0, 0) != 0) {
|
||||
memset(&frame->uc.uc_stack, 0, sizeof(frame->uc.uc_stack));
|
||||
frame->uc.uc_stack.ss_flags = SS_DISABLE;
|
||||
}
|
||||
memset(&frame->uc.uc_sigmask, 0, sizeof(frame->uc.uc_sigmask));
|
||||
memcpy(&frame->uc.uc_sigmask, &call->old_mask, sizeof(call->old_mask));
|
||||
frame->uc.uc_mcontext.fault_address = 0;
|
||||
frame->uc.uc_mcontext.pstate |= usr2_ssbs();
|
||||
fp->head.magic = FPSIMD_MAGIC;
|
||||
fp->head.size = sizeof(*fp);
|
||||
end->magic = 0;
|
||||
end->size = 0;
|
||||
call->handler(SIGUSR2, &frame->info, &frame->uc);
|
||||
}
|
||||
|
||||
int raise(int sig) {
|
||||
int (*real)(int) = __atomic_load_n(&usr2_real_raise, __ATOMIC_ACQUIRE);
|
||||
if (!real) {
|
||||
real = (int (*)(int))dlsym(RTLD_NEXT, "raise");
|
||||
__atomic_store_n(&usr2_real_raise, real, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (sig != SIGUSR2 || (getauxval(AT_HWCAP) & BL_HWCAP_GCS) || !bl_from_ntdll(__builtin_return_address(0))) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
struct bl_kernel_sigaction action;
|
||||
if (usr2_sys(__NR_rt_sigaction, SIGUSR2, 0, (long)&action, sizeof(action.mask)) != 0) return real(sig);
|
||||
if (!(action.flags & SA_SIGINFO) || (action.flags & SA_RESETHAND) || (uintptr_t)action.handler <= 1) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
uint64_t bit = 1ULL << (SIGUSR2 - 1);
|
||||
uint64_t block = action.mask | ((action.flags & SA_NODEFER) ? 0 : bit);
|
||||
struct bl_usr2_call call = {action.handler, 0};
|
||||
if (usr2_sys(__NR_rt_sigprocmask, SIG_BLOCK, (long)&block, (long)&call.old_mask, sizeof(block)) != 0) {
|
||||
return real(sig);
|
||||
}
|
||||
if (call.old_mask & bit) {
|
||||
usr2_sys(__NR_rt_sigprocmask, SIG_SETMASK, (long)&call.old_mask, 0, sizeof(call.old_mask));
|
||||
return real(sig);
|
||||
}
|
||||
bl_usr2_frame(&call);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
if [ -z "$WEBHOOK" ]; then
|
||||
echo "::notice::No DISCORD_BUILDS_WEBHOOK secret; nothing posted"
|
||||
exit 0
|
||||
fi
|
||||
commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA")
|
||||
subject=$(jq -r '.commit.message | split("\n")[0]' <<<"$commit")
|
||||
title=$(jq -r '.commit.message | split("\n") | map(select(length > 0)) | .[1] // ""' <<<"$commit")
|
||||
pr=$(sed -n 's/^Merge pull request #\([0-9]*\).*/\1/p' <<<"$subject")
|
||||
if [ -n "$pr" ]; then
|
||||
what="[#$pr]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$pr): ${title:-$subject}"
|
||||
else
|
||||
what="[\`${GITHUB_SHA::7}\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA): $subject"
|
||||
fi
|
||||
get=$(printf 'Download: [standard APK](<%s>) · [all variants](<%s>)' "$APK" "$RELEASE")
|
||||
content=$(printf '**New signed Preview build** `main-%s`\n%s\nIn the app: **Updates → Preview**\n%s' "${GITHUB_SHA::7}" "$what" "$get")
|
||||
jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' |
|
||||
curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null
|
||||
echo "Posted: $content"
|
||||
@@ -4,7 +4,7 @@ from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from check_authorship import BOT, COAUTHOR, check_revision
|
||||
from check_authorship import COAUTHOR, check_revision, is_bot
|
||||
|
||||
|
||||
def main():
|
||||
@@ -23,13 +23,13 @@ def main():
|
||||
return 1
|
||||
else:
|
||||
identity = subprocess.check_output(["git", "var", "GIT_AUTHOR_IDENT"], text=True)
|
||||
if BOT.search(identity):
|
||||
if is_bot(identity):
|
||||
print("Bot commit author is forbidden: " + identity.strip(), file=sys.stderr)
|
||||
return 1
|
||||
message = Path(args[0])
|
||||
original = message.read_text(encoding="utf-8")
|
||||
cleaned = "".join(line for line in original.splitlines(keepends=True)
|
||||
if not (COAUTHOR.match(line) and BOT.search(line)))
|
||||
if not (COAUTHOR.match(line) and is_bot(line)))
|
||||
if cleaned != original:
|
||||
message.write_text(cleaned, encoding="utf-8")
|
||||
print("Removed bot co-author trailers; human credits preserved.", file=sys.stderr)
|
||||
|
||||
@@ -6,15 +6,34 @@ import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
BOT = re.compile(
|
||||
r"Claude|Anthropic|OpenAI|ChatGPT|GPT|Copilot|Cursor|Gemini|Codex|Devin|"
|
||||
r"Windsurf|Codeium|Amazon Q|Tabnine|Cody|\[bot\]|github-actions|"
|
||||
r"dependabot|renovate(?:\[bot\])?",
|
||||
PRODUCT = re.compile(
|
||||
r"(?<![a-z0-9])(?:Anthropic|OpenAI|ChatGPT|GPT(?:-?\d[\w.]*)?|Copilot|Codex|Codeium|Windsurf|"
|
||||
r"Amazon Q|Tabnine|github-actions|dependabot|renovate)(?![a-z0-9])|\[bot\]",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
# These are also people's names, so they only count alone or followed by a product word.
|
||||
ASSISTANT = r"(?:Claude|Cursor|Devin|Cody|Gemini)"
|
||||
ASSISTANT_ALONE = re.compile(ASSISTANT, re.IGNORECASE)
|
||||
ASSISTANT_PRODUCT = re.compile(
|
||||
ASSISTANT + r"[\s_-]+(?:Code|Opus|Sonnet|Haiku|Fable|Instant|AI|Agent|Bot|Assistant|CLI|Pro|Flash|Ultra|\d)(?![a-z])",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
BOT_DOMAIN = re.compile(
|
||||
r"@(?:[\w-]+\.)*(?:anthropic\.com|openai\.com|cursor\.(?:com|sh)|cognition\.ai|sourcegraph\.com|"
|
||||
r"codeium\.com|tabnine\.com)$",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
COAUTHOR = re.compile(r"^\s*Co-authored-by\s*:", re.IGNORECASE)
|
||||
|
||||
|
||||
def is_bot(identity):
|
||||
name, _, rest = identity.partition("<")
|
||||
email = rest.partition(">")[0].strip()
|
||||
name = COAUTHOR.sub("", name).strip()
|
||||
return bool(PRODUCT.search(name) or PRODUCT.search(email) or BOT_DOMAIN.search(email)
|
||||
or ASSISTANT_ALONE.fullmatch(name) or ASSISTANT_PRODUCT.search(name))
|
||||
|
||||
|
||||
def commit_violations(commit, raw):
|
||||
header, separator, message = raw.partition(b"\n\n")
|
||||
if not separator:
|
||||
@@ -24,10 +43,10 @@ def commit_violations(commit, raw):
|
||||
return [f"{commit}: expected one author"]
|
||||
identity = authors[0].decode("utf-8", errors="replace")
|
||||
errors = []
|
||||
if BOT.search(identity):
|
||||
if is_bot(identity):
|
||||
errors.append(f"{commit}: bot author: {identity}")
|
||||
for line in message.decode("utf-8", errors="replace").splitlines():
|
||||
if COAUTHOR.match(line) and BOT.search(line):
|
||||
if COAUTHOR.match(line) and is_bot(line):
|
||||
errors.append(f"{commit}: bot co-author: {line.strip()}")
|
||||
return errors
|
||||
|
||||
@@ -36,11 +55,11 @@ def event_violations(event):
|
||||
pr = event.get("pull_request", {})
|
||||
user = pr.get("user", {})
|
||||
errors = []
|
||||
if user.get("type") == "Bot" or BOT.search(user.get("login", "")):
|
||||
if user.get("type") == "Bot" or is_bot(user.get("login", "")):
|
||||
errors.append("pull request: bot authorship is forbidden")
|
||||
for field in ("title", "body"):
|
||||
for line in (pr.get(field) or "").splitlines():
|
||||
if COAUTHOR.match(line) and BOT.search(line):
|
||||
if COAUTHOR.match(line) and is_bot(line):
|
||||
errors.append(f"pull request {field}: bot co-author trailer")
|
||||
return errors
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
pr=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" \
|
||||
--jq '[.[] | select(.merged_at != null and .base.ref == "main")] | first // empty')
|
||||
if [ -n "$pr" ]; then
|
||||
number=$(jq -r .number <<<"$pr")
|
||||
title=$(jq -r .title <<<"$pr")
|
||||
merged=$(jq -r '.merged_at[:10]' <<<"$pr")
|
||||
summary=$(jq -r '.body // ""' <<<"$pr" | tr -d '\r' | { grep -v '^🤖' || true; } |
|
||||
awk '!p && /^#/ { next } NF { p = 1; print; next } p { exit }' | head -n 6 | cut -c1-400)
|
||||
{
|
||||
echo "**Merged to main** - [PR #$number]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$number): $title (merged $merged)"
|
||||
if [ -n "$summary" ]; then
|
||||
echo
|
||||
echo "$summary"
|
||||
fi
|
||||
} > notes-head.md
|
||||
else
|
||||
subject=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.message | split("\n")[0]')
|
||||
echo "**Pushed to main** without a PR - $subject" > notes-head.md
|
||||
fi
|
||||
cat notes-head.md
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
tag=main-${GITHUB_SHA::7}
|
||||
cat signed/SHA256SUMS-*.txt > signed/SHA256SUMS.txt
|
||||
rm signed/SHA256SUMS-*.txt
|
||||
notes="$(cat notes-head.md)
|
||||
|
||||
Signed build of $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA - in main, not yet in a DroidDeck release; for testing.
|
||||
Commit: $GITHUB_SHA
|
||||
VersionCode: $(sed -n 's/^ *versionCode \([0-9][0-9]*\)$/\1/p' app/build.gradle)
|
||||
Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it.
|
||||
|
||||
SHA-256:
|
||||
\`\`\`
|
||||
$(cat signed/SHA256SUMS.txt)
|
||||
\`\`\`"
|
||||
gh release delete "$tag" -R "$CI_REPO" --yes --cleanup-tag 2>/dev/null || true
|
||||
gh release create "$tag" -R "$CI_REPO" --target main --prerelease \
|
||||
--title "main ${GITHUB_SHA::7}" --notes "$notes" signed/*
|
||||
gh release list -R "$CI_REPO" -L 200 --json tagName,publishedAt \
|
||||
--jq '[.[] | select(.tagName | startswith("main-"))] | sort_by(.publishedAt) | reverse | .['"$KEEP"':][] | .tagName' |
|
||||
while IFS= read -r old; do
|
||||
if [ "$old" = "$tag" ]; then
|
||||
continue
|
||||
fi
|
||||
if ! gh release delete "$old" -R "$CI_REPO" --yes --cleanup-tag 2>/dev/null; then
|
||||
if gh release view "$old" -R "$CI_REPO" >/dev/null 2>&1; then
|
||||
echo "::error::Could not delete old release $old"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
published=$(gh release view "$tag" -R "$CI_REPO" --json tagName,assets)
|
||||
jq -e --arg tag "$tag" '.tagName == $tag and (.assets | length) > 0' <<< "$published" >/dev/null
|
||||
url="$GITHUB_SERVER_URL/$CI_REPO/releases/tag/$tag"
|
||||
apk=$(cd signed && ls DroidDeck-*.apk | awk 'length(a) == 0 || length($0) < length(a) { a = $0 } END { print a }')
|
||||
echo "release=$url" >> "$GITHUB_OUTPUT"
|
||||
echo "apk=$GITHUB_SERVER_URL/$CI_REPO/releases/download/$tag/$apk" >> "$GITHUB_OUTPUT"
|
||||
echo "Published $url"
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
MARK="<!-- droiddeck-pr-build -->"
|
||||
{
|
||||
echo "$MARK"
|
||||
echo "## Build of this pull request"
|
||||
echo
|
||||
[ -f /tmp/pr-merge.md ] && cat /tmp/pr-merge.md
|
||||
echo
|
||||
case "$BUILD_OUTCOME:$UNIT_TEST_OUTCOME:$APK_OUTCOME" in
|
||||
success:success:success)
|
||||
echo "### Build passed"
|
||||
echo
|
||||
echo "The \`droiddeck-apk\` artifact is [on this run]($RUN_URL), signed with the public test key."
|
||||
echo
|
||||
if [ "$SAME_REPO" = true ]; then
|
||||
echo "After the parallel regression tests pass, a release-signed tester build that installs over Stable or Preview will be published and will appear in DroidDeck's **Test builds** list. Label the PR \`no-test-build\` to skip that."
|
||||
else
|
||||
echo "**Maintainer:** add the \`publish-test\` label to publish a release-signed tester build that installs over Stable or Preview and appears in DroidDeck's **Test builds** list. The label is removed automatically after publishing."
|
||||
fi
|
||||
echo
|
||||
echo "Fallback: [open **PR Release Publish**]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/workflows/pr-release-publish.yml) on \`main\` and enter PR \`$PR\`."
|
||||
;;
|
||||
success:success:*)
|
||||
echo "### APK publication incomplete"
|
||||
echo
|
||||
echo "Compilation and unit tests passed, but APK publication did not complete. See [the run]($RUN_URL)."
|
||||
;;
|
||||
success:failure:*)
|
||||
echo "### ERROR: Unit tests failed"
|
||||
echo
|
||||
echo "The APK compiled, but unit tests failed. No installable artifact was published."
|
||||
echo
|
||||
if [ -n "$TEST_DIAGNOSTICS_URL" ]; then
|
||||
echo "The [unit-test-results]($TEST_DIAGNOSTICS_URL) artifact contains the test log and any generated reports."
|
||||
else
|
||||
echo "The test diagnostics upload did not complete. See [the test log]($RUN_URL)."
|
||||
fi
|
||||
;;
|
||||
success:*)
|
||||
echo "### Build incomplete"
|
||||
echo
|
||||
echo "The APK compiled, but unit tests did not complete successfully. See [the run]($RUN_URL)."
|
||||
;;
|
||||
failure:*)
|
||||
echo "### ERROR: Build failed"
|
||||
echo
|
||||
echo "The compiler's errors ([full log]($RUN_URL)):"
|
||||
echo
|
||||
echo '```'
|
||||
{ grep -E '^e: |^w: .*error|error:|CMake Error|fatal error|ninja: error|FAILED|What went wrong|Execution failed for task|Unresolved reference|Type mismatch|Unresolved' /tmp/build.log 2>/dev/null \
|
||||
| sed -e 's#file:///home/runner/work/[^/]*/[^/]*/##' -e 's#/home/runner/work/[^/]*/[^/]*/##' \
|
||||
| grep -v '^w: ' | awk '!seen[$0]++' | head -80; } || true
|
||||
echo '```'
|
||||
echo
|
||||
echo "<details><summary>What went wrong (Gradle)</summary>"
|
||||
echo
|
||||
echo '```'
|
||||
awk '/^\* What went wrong/{p=1} /^\* Try:/{p=0} p{print}' /tmp/build.log 2>/dev/null | head -60
|
||||
echo '```'
|
||||
echo "</details>"
|
||||
;;
|
||||
skipped:*|cancelled:*)
|
||||
echo "### Not built"
|
||||
echo
|
||||
echo "The build did not run (see above, or [the run]($RUN_URL))."
|
||||
;;
|
||||
esac
|
||||
echo
|
||||
} > /tmp/pr-report.md
|
||||
cat /tmp/pr-report.md >> "$GITHUB_STEP_SUMMARY"
|
||||
ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" --paginate --jq ".[] | select(.body | startswith(\"$MARK\")) | .id" | head -1 || true)
|
||||
if [ -n "$ID" ]; then
|
||||
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${ID}" -F body=@/tmp/pr-report.md >/dev/null
|
||||
else
|
||||
gh api -X POST "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" -F body=@/tmp/pr-report.md >/dev/null
|
||||
fi
|
||||
@@ -23,7 +23,7 @@ PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet']
|
||||
SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst',
|
||||
'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio',
|
||||
'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env',
|
||||
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/build_local.sh',
|
||||
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/components', 'tools/ci', 'tools/build_local.sh',
|
||||
'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal',
|
||||
'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh',
|
||||
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
|
||||
|
||||
@@ -40,6 +40,27 @@ class AuthorshipTest(unittest.TestCase):
|
||||
def test_bot_pr_author_is_rejected(self):
|
||||
self.assertTrue(check.event_violations({"pull_request": {"user": {"login": "service", "type": "Bot"}}}))
|
||||
|
||||
def test_people_named_like_assistants_are_not_bots(self):
|
||||
for author in ("Claudemirovsky <63046606+Claudemirovsky@users.noreply.github.com>",
|
||||
"Claude Monet <claude@example.com>", "Devin Smith <devin@example.com>",
|
||||
"Cody Allen <cody@example.com>", "Gemini Patel <gp@example.com>"):
|
||||
with self.subTest(author=author):
|
||||
self.assertEqual([], check.commit_violations("abc", self.commit(author=author)))
|
||||
self.assertEqual([], check.event_violations({"pull_request": {"user": {"login": "Claudemirovsky", "type": "User"}}}))
|
||||
|
||||
def test_assistant_identities_are_still_bots(self):
|
||||
for author in ("Claude Opus 5.5 <noreply@anthropic.com>", "Claude Code <cc@example.com>",
|
||||
"Cursor Agent <cursoragent@cursor.com>",
|
||||
"Devin AI <devin-ai-integration[bot]@users.noreply.github.com>",
|
||||
"Gemini <gemini@example.com>", "GPT-5 <a@example.com>"):
|
||||
with self.subTest(author=author):
|
||||
self.assertTrue(check.commit_violations("abc", self.commit(author=author)))
|
||||
for login in ("chatgpt-codex-connector", "Copilot", "claude"):
|
||||
with self.subTest(login=login):
|
||||
self.assertTrue(check.event_violations({"pull_request": {"user": {"login": login, "type": "User"}}}))
|
||||
message = "Fix\n\nCo-authored-by: Claude Opus 5.5 <noreply@anthropic.com>\n"
|
||||
self.assertEqual(1, len(check.commit_violations("abc", self.commit(message=message))))
|
||||
|
||||
def test_missing_author_fails_closed(self):
|
||||
self.assertTrue(check.commit_violations("abc", b"tree 000\n\nmessage\n"))
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import itertools
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class CiChecksTest(unittest.TestCase):
|
||||
def test_independent_jobs_wait_only_for_the_pinned_source(self):
|
||||
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
|
||||
for name in ('xbox', 'components', 'python-tests'):
|
||||
self.assertEqual(jobs[name]['needs'], 'source')
|
||||
self.assertEqual(set(jobs['build']['needs']), {'source', 'components', 'xbox'})
|
||||
|
||||
def test_dependency_update_errors_block_installation(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
sudo = root / 'sudo'
|
||||
sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\nexit "${APT_RESULT:-0}"\n')
|
||||
sudo.chmod(0o755)
|
||||
query = root / 'dpkg-query'
|
||||
query.write_text('#!/bin/sh\nexit 1\n')
|
||||
query.chmod(0o755)
|
||||
log = root / 'apt.log'
|
||||
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], APT_LOG=str(log))
|
||||
installer = ROOT / 'tools/ci/install-ubuntu-packages.sh'
|
||||
result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17'))
|
||||
self.assertEqual(result.returncode, 17)
|
||||
self.assertEqual(len(log.read_text().splitlines()), 2)
|
||||
log.unlink()
|
||||
subprocess.run(['bash', str(installer), 'qemu-user'], env=env, check=True)
|
||||
update, install = log.read_text().splitlines()
|
||||
for command in (update, install):
|
||||
self.assertIn('Dir::Etc::sourceparts=-', command)
|
||||
self.assertIn('Acquire::https::Timeout=15', command)
|
||||
self.assertIn('Acquire::Retries=1', command)
|
||||
self.assertIn('APT::Update::Error-Mode=any', update)
|
||||
self.assertIn('install -y -q --no-install-recommends qemu-user', install)
|
||||
|
||||
def test_required_status_rejects_failed_cancelled_or_skipped_test_jobs(self):
|
||||
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
|
||||
gate = jobs['checks']
|
||||
self.assertEqual(gate['name'], 'Compile and test APK')
|
||||
self.assertEqual(set(gate['needs']), {'build', 'python-tests'})
|
||||
self.assertIn('always()', gate['if'])
|
||||
for build, python in itertools.product(('success', 'failure', 'cancelled', 'skipped'), repeat=2):
|
||||
with self.subTest(build=build, python=python):
|
||||
result = subprocess.run(
|
||||
['bash', '-c', gate['steps'][0]['run']],
|
||||
env=dict(os.environ, BUILD_RESULT=build, PYTHON_RESULT=python),
|
||||
capture_output=True)
|
||||
self.assertEqual(result.returncode == 0, build == python == 'success')
|
||||
for publisher in ('sign', 'packs'):
|
||||
self.assertIn('checks', jobs[publisher]['needs'])
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class CiNdkTest(unittest.TestCase):
|
||||
def test_installed_ndk_must_match_the_declared_version(self):
|
||||
version = (ROOT / 'tools/ci/toolchains.env').read_text().strip().split('=', 1)[1]
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
ndk = root / 'ndk' / version
|
||||
ndk.mkdir(parents=True)
|
||||
properties = ndk / 'source.properties'
|
||||
properties.write_text('Pkg.Revision = ' + version + '\n')
|
||||
output = root / 'environment'
|
||||
env = dict(os.environ, ANDROID_HOME=str(root), GITHUB_ENV=str(output))
|
||||
script = ROOT / 'tools/ci/resolve-ndk.sh'
|
||||
subprocess.run(['bash', str(script)], env=env, check=True)
|
||||
self.assertIn('NDK_VERSION=' + version, output.read_text())
|
||||
self.assertIn('ANDROID_NDK_HOME=' + str(ndk), output.read_text())
|
||||
properties.write_text('Pkg.Revision = unexpected\n')
|
||||
self.assertNotEqual(subprocess.run(['bash', str(script)], env=env).returncode, 0)
|
||||
@@ -0,0 +1,106 @@
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location('component_prepare', Path(__file__).resolve().parents[1] / 'components/prepare.py')
|
||||
components = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(components)
|
||||
|
||||
|
||||
class ComponentPrepareTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
self.root = Path(directory.name)
|
||||
self.fixture(self.root)
|
||||
|
||||
def fixture(self, root):
|
||||
for name in components.TREES:
|
||||
(root / name).mkdir(parents=True)
|
||||
(root / 'tools/components/recipes').mkdir(parents=True)
|
||||
(root / 'input.h').write_text('#define VALUE 1\n')
|
||||
(root / 'tools/components/recipes/fake.sh').write_text('printf binary > app/src/main/assets/linuxfs/demo.so\n')
|
||||
spec = dict(script='tools/components/recipes/fake.sh', inputs=['input.h', 'tools/components/recipes/fake.sh'],
|
||||
outputs=['app/src/main/assets/linuxfs/demo.so'])
|
||||
(root / components.REGISTRY).write_text(json.dumps(dict(fake=spec)))
|
||||
for name in components.LIBRARIES:
|
||||
(root / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(root / name).write_bytes(b'library')
|
||||
subprocess.run(['git', 'init', '-q', str(root)], check=True)
|
||||
subprocess.run(['git', '-C', str(root), 'config', 'core.autocrlf', 'false'], check=True)
|
||||
subprocess.run(['git', '-C', str(root), 'add', '.'], check=True)
|
||||
subprocess.run(['git', '-C', str(root), '-c', 'user.name=Test Developer', '-c', 'user.email=test@example.test',
|
||||
'commit', '-qm', 'baseline'], check=True)
|
||||
|
||||
def test_recipe_failure_or_missing_output_does_not_succeed(self):
|
||||
script = self.root / 'tools/components/recipes/fake.sh'
|
||||
script.write_text('exit 7\n')
|
||||
with self.assertRaises(subprocess.CalledProcessError):
|
||||
components.prepare('fake', self.root)
|
||||
script.write_text('exit 0\n')
|
||||
with self.assertRaisesRegex(ValueError, 'required outputs'):
|
||||
components.prepare('fake', self.root)
|
||||
|
||||
def test_identity_covers_source_and_recipe_changes(self):
|
||||
original = components.identity('fake', self.root)
|
||||
(self.root / 'input.h').write_text('#define VALUE 2\n')
|
||||
changed = components.identity('fake', self.root)
|
||||
self.assertNotEqual(original, changed)
|
||||
script = self.root / 'tools/components/recipes/fake.sh'
|
||||
script.write_text(script.read_text() + '\nprintf changed\n')
|
||||
self.assertNotEqual(changed, components.identity('fake', self.root))
|
||||
|
||||
def test_tool_requirements_are_installed_in_one_union(self):
|
||||
definitions = dict(one=dict(packages=['gcc', 'cmake']), two=dict(packages=['gcc', 'qemu-user']))
|
||||
with patch.object(components, 'registry', return_value=definitions), patch.object(components.subprocess, 'run') as run:
|
||||
components.prepare_tools(['one', 'two'], self.root)
|
||||
run.assert_called_once_with(['bash', 'tools/ci/install-ubuntu-packages.sh', 'cmake', 'gcc', 'qemu-user'],
|
||||
cwd=self.root, check=True)
|
||||
|
||||
def test_prepared_handoff_verifies_every_file_and_source_tree(self):
|
||||
components.prepare('fake', self.root)
|
||||
archive = self.root / 'prepared.tar.gz'
|
||||
components.pack(archive, self.root)
|
||||
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
|
||||
target = self.root / 'target'
|
||||
self.fixture(target)
|
||||
components.unpack(archive, tree, target)
|
||||
self.assertEqual((target / 'app/src/main/assets/linuxfs/demo.so').read_bytes(), b'binary')
|
||||
with self.assertRaisesRegex(ValueError, 'pinned source'):
|
||||
components.unpack(archive, 'wrong-tree', target)
|
||||
|
||||
def test_tampered_component_bytes_fail_handoff_verification(self):
|
||||
components.prepare('fake', self.root)
|
||||
archive = self.root / 'prepared.tar.gz'
|
||||
components.pack(archive, self.root)
|
||||
corrupt = self.root / 'corrupt.tar.gz'
|
||||
with tarfile.open(archive) as original, tarfile.open(corrupt, 'w:gz') as output:
|
||||
for entry in original:
|
||||
content = original.extractfile(entry).read()
|
||||
if entry.name.endswith('demo.so'):
|
||||
content = b'tampered'
|
||||
entry.size = len(content)
|
||||
output.addfile(entry, io.BytesIO(content))
|
||||
target = self.root / 'target'
|
||||
self.fixture(target)
|
||||
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
|
||||
with self.assertRaisesRegex(ValueError, 'file hashes'):
|
||||
components.unpack(corrupt, tree, target)
|
||||
|
||||
def test_archive_cannot_overwrite_source_through_traversal(self):
|
||||
archive = self.root / 'bad.tar.gz'
|
||||
with tarfile.open(archive, mode='w:gz') as tar:
|
||||
name = 'app/src/main/assets/linuxfs/../../../../../input.h'
|
||||
entry = tarfile.TarInfo(name)
|
||||
entry.size = 3
|
||||
tar.addfile(entry, io.BytesIO(b'bad'))
|
||||
with self.assertRaisesRegex(ValueError, 'Unsafe'):
|
||||
components.unpack(archive, 'tree', self.root)
|
||||
self.assertEqual((self.root / 'input.h').read_text(), '#define VALUE 1\n')
|
||||
@@ -1160,8 +1160,12 @@ class BundleTest(unittest.TestCase):
|
||||
except ImportError:
|
||||
self.skipTest("PyYAML is not installed")
|
||||
apk = yaml.safe_load((self.ROOT / ".github/workflows/build.yml").read_text())
|
||||
steps = {step.get("name"): step for step in apk["jobs"]["build"]["steps"]}
|
||||
bundle = steps["Bundle the droiddeck-esync packs"]
|
||||
steps = {step.get("name"): step for step in apk["jobs"]["components"]["steps"]}
|
||||
import json
|
||||
recipes = json.loads((self.ROOT / "tools/components/recipes.json").read_text())
|
||||
bundle = dict(steps["Bundle the droiddeck-esync packs"])
|
||||
self.assertIn("prepare.py esync", bundle["run"])
|
||||
bundle["run"] = (self.ROOT / recipes["esync"]["script"]).read_text()
|
||||
self.assertNotIn("if", bundle)
|
||||
self.assertIn("tools/droiddeck-esync/release.env", bundle["run"])
|
||||
self.assertIn('release_url="https://github.com/${SYNC_BUNDLE_REPO}/releases/download/${SYNC_BUNDLE_TAG}"', bundle["run"])
|
||||
@@ -1169,7 +1173,7 @@ class BundleTest(unittest.TestCase):
|
||||
self.assertIn('curl -fsSL --retry 3 -o sync-index.json "$release_url/index.json"', bundle["run"])
|
||||
self.assertIn("sha256sum -c -", bundle["run"])
|
||||
self.assertNotIn("revoked.txt", bundle["run"])
|
||||
stage = next(step for step in apk["jobs"]["build"]["steps"] if "overlay/usr/local/bin/droiddeck-*" in step.get("run", ""))
|
||||
stage = {"run": (self.ROOT / recipes["runtime"]["script"]).read_text()}
|
||||
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", stage["run"])
|
||||
local = (self.ROOT / "tools/build_local.sh").read_text()
|
||||
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", local)
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../app/src/main/cpp/adrenotools/src/hook/kgsl.h"
|
||||
#include "../../app/src/main/cpp/waylandcomp/src/gpu_clock_pin.h"
|
||||
|
||||
enum { NONE, OPEN, DEVICE, CONTEXT, CONSTRAINT, MEMORY, MAP, SYNC, SUBMIT, THREAD, WAIT };
|
||||
static atomic_int failure, descriptors, contexts, memories, mappings, submissions, waits;
|
||||
static atomic_bool timeout_wait;
|
||||
static bool legacy_core;
|
||||
static uint32_t commands[1024];
|
||||
|
||||
int __android_log_print(int priority, const char *tag, const char *format, ...) {
|
||||
(void)priority; (void)tag; (void)format;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int __wrap_open(const char *path, int flags, ...) {
|
||||
(void)flags;
|
||||
assert(!strcmp(path, "/dev/kgsl-3d0"));
|
||||
if (atomic_load(&failure) == OPEN) { errno = EACCES; return -1; }
|
||||
assert(atomic_fetch_add(&descriptors, 1) == 0);
|
||||
return 91;
|
||||
}
|
||||
|
||||
int __wrap_close(int fd) {
|
||||
assert(fd == 91);
|
||||
assert(atomic_fetch_sub(&descriptors, 1) == 1);
|
||||
errno = EBADF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *__wrap_mmap(void *address, size_t size, int protection, int flags, int fd, off_t offset) {
|
||||
(void)address; (void)protection; (void)flags;
|
||||
assert(fd == 91 && size == sizeof(commands) && offset == (23 << 12));
|
||||
if (atomic_load(&failure) == MAP) { errno = EACCES; return MAP_FAILED; }
|
||||
assert(atomic_fetch_add(&mappings, 1) == 0);
|
||||
return commands;
|
||||
}
|
||||
|
||||
int __wrap_munmap(void *address, size_t size) {
|
||||
assert(address == commands && size == sizeof(commands));
|
||||
assert(atomic_fetch_sub(&mappings, 1) == 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
int __wrap_ioctl(int fd, int request, ...) {
|
||||
#else
|
||||
int __wrap_ioctl(int fd, unsigned long request, ...) {
|
||||
#endif
|
||||
assert(fd == 91);
|
||||
va_list args;
|
||||
va_start(args, request);
|
||||
void *arg = va_arg(args, void *);
|
||||
va_end(args);
|
||||
int step = NONE;
|
||||
switch ((unsigned int)request) {
|
||||
case IOCTL_KGSL_DEVICE_GETPROPERTY: step = DEVICE; break;
|
||||
case IOCTL_KGSL_DRAWCTXT_CREATE: step = CONTEXT; break;
|
||||
case IOCTL_KGSL_SETPROPERTY: step = CONSTRAINT; break;
|
||||
case IOCTL_KGSL_GPUMEM_ALLOC_ID: step = MEMORY; break;
|
||||
case IOCTL_KGSL_GPUMEM_SYNC_CACHE: step = SYNC; break;
|
||||
case IOCTL_KGSL_GPU_COMMAND: step = SUBMIT; break;
|
||||
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID: step = WAIT; break;
|
||||
}
|
||||
if (step && atomic_load(&failure) == step) { errno = EACCES; return -1; }
|
||||
switch ((unsigned int)request) {
|
||||
case IOCTL_KGSL_DEVICE_GETPROPERTY: {
|
||||
struct kgsl_device_getproperty *property = arg;
|
||||
assert(property->type == KGSL_PROP_DEVICE_INFO);
|
||||
((struct kgsl_devinfo *)property->value)->chip_id = legacy_core ? 0x04030000 : 0x07030000;
|
||||
break;
|
||||
}
|
||||
case IOCTL_KGSL_DRAWCTXT_CREATE: {
|
||||
struct kgsl_drawctxt_create *context = arg;
|
||||
assert(context->flags & KGSL_CONTEXT_PWR_CONSTRAINT);
|
||||
context->drawctxt_id = 17;
|
||||
atomic_fetch_add(&contexts, 1);
|
||||
break;
|
||||
}
|
||||
case IOCTL_KGSL_SETPROPERTY: {
|
||||
struct kgsl_device_getproperty *property = arg;
|
||||
assert(property->type == KGSL_PROP_PWR_CONSTRAINT);
|
||||
struct kgsl_device_constraint *constraint = property->value;
|
||||
assert(constraint->context_id == 17 && constraint->type == KGSL_CONSTRAINT_PWRLEVEL);
|
||||
assert(((struct kgsl_device_constraint_pwrlevel *)constraint->data)->level == KGSL_CONSTRAINT_PWR_MAX);
|
||||
break;
|
||||
}
|
||||
case IOCTL_KGSL_GPUMEM_ALLOC_ID: {
|
||||
struct kgsl_gpumem_alloc_id *memory = arg;
|
||||
memory->id = 23; memory->gpuaddr = 0x100000; memory->mmapsize = sizeof(commands);
|
||||
atomic_fetch_add(&memories, 1);
|
||||
break;
|
||||
}
|
||||
case IOCTL_KGSL_GPUMEM_SYNC_CACHE:
|
||||
assert(((struct kgsl_gpumem_sync_cache *)arg)->op == KGSL_GPUMEM_CACHE_CLEAN);
|
||||
break;
|
||||
case IOCTL_KGSL_GPU_COMMAND: {
|
||||
struct kgsl_gpu_command *command = arg;
|
||||
struct kgsl_command_object *object = (void *)(uintptr_t)command->cmdlist;
|
||||
assert(command->context_id == 17 && command->numcmds == 1);
|
||||
assert(command->flags & KGSL_CMDBATCH_PWR_CONSTRAINT);
|
||||
assert(object->id == 23 && object->gpuaddr == 0x100000 && object->size == 8);
|
||||
assert(commands[0] == (legacy_core ? 0xc0001000 : 0x70100001) && commands[1] == 0);
|
||||
command->timestamp = atomic_fetch_add(&submissions, 1) + 1;
|
||||
break;
|
||||
}
|
||||
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID:
|
||||
atomic_fetch_add(&waits, 1);
|
||||
if (atomic_load(&timeout_wait)) { errno = ETIMEDOUT; return -1; }
|
||||
break;
|
||||
case IOCTL_KGSL_DRAWCTXT_DESTROY:
|
||||
assert(atomic_fetch_sub(&contexts, 1) == 1);
|
||||
break;
|
||||
case IOCTL_KGSL_GPUMEM_FREE_ID:
|
||||
assert(atomic_fetch_sub(&memories, 1) == 1);
|
||||
break;
|
||||
default: assert(!"unexpected ioctl");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int __real_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
|
||||
int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg) {
|
||||
if (atomic_load(&failure) == THREAD) return EAGAIN;
|
||||
return __real_pthread_create(thread, attr, start, arg);
|
||||
}
|
||||
|
||||
static void assert_released(void) {
|
||||
assert(!atomic_load(&descriptors) && !atomic_load(&contexts));
|
||||
assert(!atomic_load(&memories) && !atomic_load(&mappings));
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert_released();
|
||||
legacy_core = true;
|
||||
assert(gpu_clock_pin_set(true));
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert_released();
|
||||
legacy_core = false;
|
||||
for (int step = OPEN; step <= THREAD; ++step) {
|
||||
atomic_store(&failure, step);
|
||||
assert(!gpu_clock_pin_set(true));
|
||||
assert(errno == (step == THREAD ? EAGAIN : EACCES));
|
||||
assert_released();
|
||||
}
|
||||
atomic_store(&failure, NONE);
|
||||
atomic_store(&submissions, 0);
|
||||
assert(gpu_clock_pin_set(true));
|
||||
assert(gpu_clock_pin_set(true));
|
||||
for (int i = 0; i < 100 && atomic_load(&submissions) < 2; ++i) usleep(10000);
|
||||
assert(atomic_load(&submissions) > 1);
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert_released();
|
||||
|
||||
atomic_store(&timeout_wait, true);
|
||||
atomic_store(&submissions, 0);
|
||||
atomic_store(&waits, 0);
|
||||
assert(gpu_clock_pin_set(true));
|
||||
for (int i = 0; i < 100 && atomic_load(&waits) < 2; ++i) usleep(10000);
|
||||
assert(atomic_load(&waits) >= 2);
|
||||
assert(atomic_load(&submissions) == 1);
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert_released();
|
||||
atomic_store(&timeout_wait, false);
|
||||
|
||||
atomic_store(&failure, WAIT);
|
||||
assert(gpu_clock_pin_set(true));
|
||||
for (int i = 0; i < 100 && atomic_load(&descriptors); ++i) usleep(10000);
|
||||
assert_released();
|
||||
atomic_store(&failure, NONE);
|
||||
assert(gpu_clock_pin_set(true));
|
||||
assert(gpu_clock_pin_set(false));
|
||||
assert_released();
|
||||
puts("gpu clock pin: failure cleanup, renewal, bounded queue and restart passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import pathlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class GpuClockPinTest(unittest.TestCase):
|
||||
@unittest.skipUnless(sys.platform.startswith('linux') and shutil.which('cc'), 'requires a Linux C compiler')
|
||||
def test_constraint_lifecycle(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
directory = pathlib.Path(tmp)
|
||||
(directory / 'android').mkdir()
|
||||
(directory / 'android/log.h').write_text(
|
||||
'#define ANDROID_LOG_WARN 5\n'
|
||||
'int __android_log_print(int, const char *, const char *, ...);\n')
|
||||
binary = directory / 'gpu-clock-pin-test'
|
||||
subprocess.run([
|
||||
'cc', '-std=c11', '-D_DEFAULT_SOURCE', '-D__user=', '-Wall', '-Wextra', '-Werror',
|
||||
'-I', str(directory), '-pthread',
|
||||
str(ROOT / 'tools/tests/test_gpu_clock_pin.c'),
|
||||
str(ROOT / 'app/src/main/cpp/waylandcomp/src/gpu_clock_pin.c'),
|
||||
'-Wl,--wrap=open,--wrap=close,--wrap=mmap,--wrap=munmap,--wrap=ioctl,--wrap=pthread_create',
|
||||
'-o', str(binary),
|
||||
], check=True)
|
||||
subprocess.run([str(binary)], check=True, timeout=10)
|
||||
@@ -0,0 +1,580 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
NTDLL = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen) {
|
||||
struct sigaction sa, old;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_sigaction = fn;
|
||||
sa.sa_flags = SA_SIGINFO | SA_RESTART;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(sig, &sa, NULL) != 0) return -1;
|
||||
if (sigaction(sig, NULL, &old) != 0) return -1;
|
||||
*seen = (void *)old.sa_sigaction;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *)) {
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_sigaction = fn;
|
||||
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_RESETHAND;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
return sigaction(sig, &sa, NULL);
|
||||
}
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/ioctl.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct sem_args { uint32_t count, max; };
|
||||
struct mutex_args { uint32_t owner, count; };
|
||||
struct event_args { uint32_t manual, signaled; };
|
||||
struct wait_args { uint64_t timeout, objs; uint32_t count, index, flags, owner, alert, pad; };
|
||||
#define NT_CREATE_SEM _IOW('N', 0x80, struct sem_args)
|
||||
#define NT_SEM_RELEASE _IOWR('N', 0x81, uint32_t)
|
||||
#define NT_WAIT_ANY _IOWR('N', 0x82, struct wait_args)
|
||||
#define NT_WAIT_ALL _IOWR('N', 0x83, struct wait_args)
|
||||
#define NT_CREATE_MUTEX _IOW('N', 0x84, struct mutex_args)
|
||||
#define NT_MUTEX_UNLOCK _IOWR('N', 0x85, struct mutex_args)
|
||||
#define NT_MUTEX_KILL _IOW('N', 0x86, uint32_t)
|
||||
#define NT_CREATE_EVENT _IOW('N', 0x87, struct event_args)
|
||||
#define NT_EVENT_SET _IOR('N', 0x88, uint32_t)
|
||||
#define NT_EVENT_RESET _IOR('N', 0x89, uint32_t)
|
||||
#define NT_EVENT_PULSE _IOR('N', 0x8a, uint32_t)
|
||||
#define NT_SEM_READ _IOR('N', 0x8b, struct sem_args)
|
||||
#define NT_MUTEX_READ _IOR('N', 0x8c, struct mutex_args)
|
||||
#define NT_EVENT_READ _IOR('N', 0x8d, struct event_args)
|
||||
|
||||
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen);
|
||||
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *));
|
||||
|
||||
static int dev, fails;
|
||||
#define CHECK(name, cond) do { if (!(cond)) { printf("FAIL %s line %d errno %d\n", name, __LINE__, errno); fails++; } } while (0)
|
||||
|
||||
static uint64_t mono_ns(int64_t delta_ms) {
|
||||
struct timespec t;
|
||||
clock_gettime(CLOCK_MONOTONIC, &t);
|
||||
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec + (uint64_t)(delta_ms * 1000000);
|
||||
}
|
||||
|
||||
static int wait_on(unsigned long req, const int *objs, uint32_t count, uint32_t owner, int alert, uint64_t timeout, uint32_t *index) {
|
||||
struct wait_args w = {timeout, (uintptr_t)objs, count, 0xdead, 0, owner, (uint32_t)alert, 0};
|
||||
int r = ioctl(dev, req, &w);
|
||||
if (index) *index = w.index;
|
||||
return r < 0 ? errno : 0;
|
||||
}
|
||||
|
||||
static int event(int manual, int signaled) {
|
||||
struct event_args a = {(uint32_t)manual, (uint32_t)signaled};
|
||||
return ioctl(dev, NT_CREATE_EVENT, &a);
|
||||
}
|
||||
|
||||
static int sem(uint32_t count, uint32_t max) {
|
||||
struct sem_args a = {count, max};
|
||||
return ioctl(dev, NT_CREATE_SEM, &a);
|
||||
}
|
||||
|
||||
static int mutex(uint32_t owner, uint32_t count) {
|
||||
struct mutex_args a = {owner, count};
|
||||
return ioctl(dev, NT_CREATE_MUTEX, &a);
|
||||
}
|
||||
|
||||
static uint32_t prev_of(int fd, unsigned long req, uint32_t in) {
|
||||
uint32_t v = in;
|
||||
if (ioctl(fd, req, &v) < 0) return 0xffffffff;
|
||||
return v;
|
||||
}
|
||||
|
||||
static void semaphores(void) {
|
||||
struct sem_args bad = {3, 2}, r;
|
||||
CHECK("sem_create_invalid", ioctl(dev, NT_CREATE_SEM, &bad) < 0 && errno == EINVAL);
|
||||
int s = sem(2, 2);
|
||||
CHECK("sem_create", s >= 0);
|
||||
uint32_t idx;
|
||||
CHECK("sem_wait1", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0 && idx == 0);
|
||||
CHECK("sem_wait2", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0);
|
||||
CHECK("sem_empty", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, NULL) == ETIMEDOUT);
|
||||
CHECK("sem_release_prev", prev_of(s, NT_SEM_RELEASE, 1) == 0);
|
||||
CHECK("sem_overflow", prev_of(s, NT_SEM_RELEASE, 2) == 0xffffffff && errno == EOVERFLOW);
|
||||
CHECK("sem_read", ioctl(s, NT_SEM_READ, &r) == 0 && r.count == 1 && r.max == 2);
|
||||
close(s);
|
||||
}
|
||||
|
||||
static void mutexes(void) {
|
||||
struct mutex_args bad = {0, 1}, r, u;
|
||||
CHECK("mutex_create_invalid", ioctl(dev, NT_CREATE_MUTEX, &bad) < 0 && errno == EINVAL);
|
||||
int m = mutex(0, 0);
|
||||
uint32_t idx;
|
||||
CHECK("mutex_owner_zero", wait_on(NT_WAIT_ANY, &m, 1, 0, 0, 0, NULL) == EINVAL);
|
||||
CHECK("mutex_take", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, &idx) == 0 && idx == 0);
|
||||
CHECK("mutex_recurse", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, NULL) == 0);
|
||||
CHECK("mutex_other", wait_on(NT_WAIT_ANY, &m, 1, 8, 0, 0, NULL) == ETIMEDOUT);
|
||||
CHECK("mutex_read", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 7 && r.count == 2);
|
||||
u = (struct mutex_args){8, 0};
|
||||
CHECK("mutex_unlock_eperm", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EPERM);
|
||||
u = (struct mutex_args){0, 0};
|
||||
CHECK("mutex_unlock_einval", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EINVAL);
|
||||
u = (struct mutex_args){7, 0};
|
||||
CHECK("mutex_unlock_prev", ioctl(m, NT_MUTEX_UNLOCK, &u) == 0 && u.count == 2);
|
||||
uint32_t who = 8;
|
||||
CHECK("mutex_kill_eperm", ioctl(m, NT_MUTEX_KILL, &who) < 0 && errno == EPERM);
|
||||
who = 7;
|
||||
CHECK("mutex_kill", ioctl(m, NT_MUTEX_KILL, &who) == 0);
|
||||
CHECK("mutex_read_dead", ioctl(m, NT_MUTEX_READ, &r) < 0 && errno == EOWNERDEAD && r.owner == 0 && r.count == 0);
|
||||
CHECK("mutex_abandoned", wait_on(NT_WAIT_ANY, &m, 1, 9, 0, 0, &idx) == EOWNERDEAD && idx == 0);
|
||||
CHECK("mutex_after_abandon", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 9 && r.count == 1);
|
||||
close(m);
|
||||
}
|
||||
|
||||
static void events(void) {
|
||||
int a = event(0, 1), m = event(1, 0);
|
||||
struct event_args r;
|
||||
CHECK("auto_take", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == 0);
|
||||
CHECK("auto_consumed", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == ETIMEDOUT);
|
||||
CHECK("set_prev0", prev_of(m, NT_EVENT_SET, 0) == 0);
|
||||
CHECK("set_prev1", prev_of(m, NT_EVENT_SET, 0) == 1);
|
||||
CHECK("manual_kept", wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0 && wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0);
|
||||
CHECK("reset_prev", prev_of(m, NT_EVENT_RESET, 0) == 1);
|
||||
CHECK("pulse_prev", prev_of(m, NT_EVENT_PULSE, 0) == 0);
|
||||
CHECK("pulse_leaves_unsignaled", ioctl(m, NT_EVENT_READ, &r) == 0 && r.manual == 1 && r.signaled == 0);
|
||||
close(a);
|
||||
close(m);
|
||||
}
|
||||
|
||||
static void any_order(void) {
|
||||
int o[4] = {event(1, 0), sem(1, 5), event(1, 1), sem(0, 5)};
|
||||
int alert = event(0, 1);
|
||||
uint32_t idx;
|
||||
CHECK("any_lowest", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 1);
|
||||
CHECK("any_next", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 2);
|
||||
prev_of(o[2], NT_EVENT_RESET, 0);
|
||||
CHECK("any_alert", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 4);
|
||||
CHECK("any_alert_consumed", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, NULL) == ETIMEDOUT);
|
||||
int dup[3] = {o[3], o[0], o[3]};
|
||||
prev_of(o[3], NT_SEM_RELEASE, 1);
|
||||
CHECK("any_duplicate_lowest", wait_on(NT_WAIT_ANY, dup, 3, 1, 0, 0, &idx) == 0 && idx == 0);
|
||||
int many[65];
|
||||
for (int i = 0; i < 65; i++) many[i] = o[0];
|
||||
CHECK("any_too_many", wait_on(NT_WAIT_ANY, many, 65, 1, 0, 0, NULL) == EINVAL);
|
||||
int badfd = 0;
|
||||
CHECK("any_invalid_fd", wait_on(NT_WAIT_ANY, &badfd, 1, 1, 0, 0, NULL) == EINVAL);
|
||||
CHECK("any_timeout", wait_on(NT_WAIT_ANY, o, 1, 1, 0, mono_ns(30), NULL) == ETIMEDOUT);
|
||||
for (int i = 0; i < 4; i++) close(o[i]);
|
||||
close(alert);
|
||||
}
|
||||
|
||||
static void all_semantics(void) {
|
||||
int s = sem(1, 5), e = event(0, 0), m = mutex(0, 0);
|
||||
int o[3] = {s, e, m};
|
||||
struct sem_args sr;
|
||||
uint32_t idx;
|
||||
CHECK("all_not_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, NULL) == ETIMEDOUT);
|
||||
CHECK("all_untouched", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 1);
|
||||
prev_of(e, NT_EVENT_SET, 0);
|
||||
CHECK("all_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, &idx) == 0 && idx == 0);
|
||||
CHECK("all_took_sem", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
|
||||
int dup[2] = {s, s};
|
||||
CHECK("all_duplicate", wait_on(NT_WAIT_ALL, dup, 2, 4, 0, 0, NULL) == EINVAL);
|
||||
int alert = event(1, 1);
|
||||
CHECK("all_alert", wait_on(NT_WAIT_ALL, o, 3, 4, alert, 0, &idx) == 0 && idx == 3);
|
||||
close(s);
|
||||
close(e);
|
||||
close(m);
|
||||
close(alert);
|
||||
}
|
||||
|
||||
static int wake_obj;
|
||||
static uint32_t wake_index;
|
||||
static int wake_ret;
|
||||
static void *waiter(void *p) {
|
||||
int all = (int)(intptr_t)p;
|
||||
int objs[2] = {wake_obj, wake_obj + 0};
|
||||
wake_ret = wait_on(all ? NT_WAIT_ALL : NT_WAIT_ANY, objs, 1, 3, 0, UINT64_MAX, &wake_index);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void cross_thread(void) {
|
||||
wake_obj = event(0, 0);
|
||||
pthread_t t;
|
||||
pthread_create(&t, NULL, waiter, NULL);
|
||||
usleep(30000);
|
||||
prev_of(wake_obj, NT_EVENT_SET, 0);
|
||||
pthread_join(t, NULL);
|
||||
CHECK("thread_wake", wake_ret == 0 && wake_index == 0);
|
||||
struct event_args r;
|
||||
CHECK("thread_auto_reset", ioctl(wake_obj, NT_EVENT_READ, &r) == 0 && r.signaled == 0);
|
||||
close(wake_obj);
|
||||
int s = sem(0, 10);
|
||||
wake_obj = s;
|
||||
pthread_t ts[3];
|
||||
for (int i = 0; i < 3; i++) pthread_create(&ts[i], NULL, waiter, NULL);
|
||||
usleep(30000);
|
||||
CHECK("sem_release_three", prev_of(s, NT_SEM_RELEASE, 3) == 0);
|
||||
for (int i = 0; i < 3; i++) pthread_join(ts[i], NULL);
|
||||
struct sem_args sr;
|
||||
CHECK("sem_all_taken", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
|
||||
close(s);
|
||||
}
|
||||
|
||||
static void cross_process(void) {
|
||||
int sv[2];
|
||||
CHECK("socketpair", socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
|
||||
int inherited = event(0, 0);
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
uint32_t idx;
|
||||
int r1 = wait_on(NT_WAIT_ANY, &inherited, 1, 5, 0, mono_ns(5000), &idx);
|
||||
char c;
|
||||
struct iovec io = {&c, 1};
|
||||
char cbuf[CMSG_SPACE(sizeof(int))];
|
||||
struct msghdr msg = {0};
|
||||
msg.msg_iov = &io;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cbuf;
|
||||
msg.msg_controllen = sizeof(cbuf);
|
||||
int got = -1;
|
||||
if (recvmsg(sv[1], &msg, 0) == 1) memcpy(&got, CMSG_DATA(CMSG_FIRSTHDR(&msg)), sizeof(int));
|
||||
int r2 = wait_on(NT_WAIT_ANY, &got, 1, 5, 0, mono_ns(5000), NULL);
|
||||
_exit(r1 == 0 && r2 == 0 ? 0 : 1);
|
||||
}
|
||||
usleep(30000);
|
||||
prev_of(inherited, NT_EVENT_SET, 0);
|
||||
int sent = sem(0, 1);
|
||||
char c = 'x';
|
||||
struct iovec io = {&c, 1};
|
||||
char cbuf[CMSG_SPACE(sizeof(int))];
|
||||
struct msghdr msg = {0};
|
||||
msg.msg_iov = &io;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cbuf;
|
||||
msg.msg_controllen = sizeof(cbuf);
|
||||
struct cmsghdr *cm = CMSG_FIRSTHDR(&msg);
|
||||
cm->cmsg_level = SOL_SOCKET;
|
||||
cm->cmsg_type = SCM_RIGHTS;
|
||||
cm->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
memcpy(CMSG_DATA(cm), &sent, sizeof(int));
|
||||
sendmsg(sv[0], &msg, 0);
|
||||
usleep(30000);
|
||||
prev_of(sent, NT_SEM_RELEASE, 1);
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
CHECK("process_wake", WIFEXITED(status) && WEXITSTATUS(status) == 0);
|
||||
close(inherited);
|
||||
close(sent);
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
}
|
||||
|
||||
static int ping_obj, pong_obj, stress_sem;
|
||||
static void *stress_ponger(void *p) {
|
||||
long n = (long)p;
|
||||
uint32_t v;
|
||||
for (long i = 0; i < n; i++) {
|
||||
if (wait_on(NT_WAIT_ANY, &ping_obj, 1, 21, 0, UINT64_MAX, NULL) != 0) return (void *)1;
|
||||
ioctl(pong_obj, NT_EVENT_SET, &v);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static void *stress_hammer(void *p) {
|
||||
long n = (long)p;
|
||||
for (long i = 0; i < n; i++) {
|
||||
if (wait_on(NT_WAIT_ANY, &stress_sem, 1, 22, 0, UINT64_MAX, NULL) != 0) return (void *)1;
|
||||
uint32_t c = 1;
|
||||
ioctl(stress_sem, NT_SEM_RELEASE, &c);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void stress(void) {
|
||||
long rounds = 20000;
|
||||
ping_obj = event(0, 0);
|
||||
pong_obj = event(0, 0);
|
||||
pthread_t t;
|
||||
void *bad = NULL;
|
||||
pthread_create(&t, NULL, stress_ponger, (void *)rounds);
|
||||
uint32_t v;
|
||||
int ok = 1;
|
||||
for (long i = 0; i < rounds && ok; i++) {
|
||||
ioctl(ping_obj, NT_EVENT_SET, &v);
|
||||
ok = wait_on(NT_WAIT_ANY, &pong_obj, 1, 20, 0, UINT64_MAX, NULL) == 0;
|
||||
}
|
||||
pthread_join(t, &bad);
|
||||
CHECK("stress_pingpong", ok && bad == NULL);
|
||||
stress_sem = sem(1, 1);
|
||||
pthread_t th[4];
|
||||
for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, stress_hammer, (void *)5000L);
|
||||
int hammer_ok = 1;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
pthread_join(th[i], &bad);
|
||||
hammer_ok &= bad == NULL;
|
||||
}
|
||||
struct sem_args sr;
|
||||
CHECK("stress_contended", hammer_ok && ioctl(stress_sem, NT_SEM_READ, &sr) == 0 && sr.count == 1);
|
||||
close(ping_obj);
|
||||
close(pong_obj);
|
||||
close(stress_sem);
|
||||
}
|
||||
|
||||
static int busy_obj;
|
||||
static volatile int handled, stop_worker;
|
||||
static sem_t parked, unpark;
|
||||
static void reentrant(int sig, siginfo_t *si, void *uc) {
|
||||
(void)sig; (void)si; (void)uc;
|
||||
uint32_t v = 0;
|
||||
ioctl(busy_obj, NT_EVENT_SET, &v);
|
||||
handled++;
|
||||
}
|
||||
static void suspending(int sig, siginfo_t *si, void *uc) {
|
||||
(void)sig; (void)si; (void)uc;
|
||||
sem_post(&parked);
|
||||
while (sem_wait(&unpark) != 0) {}
|
||||
handled++;
|
||||
}
|
||||
static void *worker(void *p) {
|
||||
(void)p;
|
||||
uint32_t v;
|
||||
while (!stop_worker) {
|
||||
ioctl(busy_obj, NT_EVENT_SET, &v);
|
||||
wait_on(NT_WAIT_ANY, &busy_obj, 1, 11, 0, 0, NULL);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void signals(void) {
|
||||
void *seen = NULL;
|
||||
busy_obj = event(0, 0);
|
||||
CHECK("install_reentrant", install_handler(SIGUSR1, reentrant, &seen) == 0);
|
||||
CHECK("query_original", seen == (void *)reentrant);
|
||||
pthread_t w;
|
||||
stop_worker = 0;
|
||||
handled = 0;
|
||||
pthread_create(&w, NULL, worker, NULL);
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
pthread_kill(w, SIGUSR1);
|
||||
if (i % 64 == 0) usleep(100);
|
||||
}
|
||||
usleep(20000);
|
||||
stop_worker = 1;
|
||||
pthread_join(w, NULL);
|
||||
CHECK("reentrant_ran", handled > 0);
|
||||
sem_init(&parked, 0, 0);
|
||||
sem_init(&unpark, 0, 0);
|
||||
CHECK("install_suspending", install_handler(SIGUSR1, suspending, &seen) == 0);
|
||||
stop_worker = 0;
|
||||
handled = 0;
|
||||
pthread_create(&w, NULL, worker, NULL);
|
||||
uint32_t v;
|
||||
for (int i = 0; i < 300; i++) {
|
||||
pthread_kill(w, SIGUSR1);
|
||||
while (sem_wait(&parked) != 0) {}
|
||||
ioctl(busy_obj, NT_EVENT_SET, &v);
|
||||
wait_on(NT_WAIT_ANY, &busy_obj, 1, 12, 0, 0, NULL);
|
||||
sem_post(&unpark);
|
||||
}
|
||||
stop_worker = 1;
|
||||
pthread_join(w, NULL);
|
||||
CHECK("suspend_ran", handled == 300);
|
||||
close(busy_obj);
|
||||
}
|
||||
|
||||
static volatile int burst_count;
|
||||
static void bursting(int sig, siginfo_t *si, void *uc) {
|
||||
(void)sig; (void)si; (void)uc;
|
||||
uint32_t v = 0;
|
||||
ioctl(busy_obj, NT_EVENT_SET, &v);
|
||||
__atomic_add_fetch(&burst_count, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void bursts(void) {
|
||||
void *seen = NULL;
|
||||
busy_obj = event(0, 0);
|
||||
for (int s = 0; s < 8; s++) CHECK("install_burst", install_handler(SIGRTMIN + s, bursting, &seen) == 0);
|
||||
pthread_t w;
|
||||
stop_worker = 0;
|
||||
burst_count = 0;
|
||||
pthread_create(&w, NULL, worker, NULL);
|
||||
union sigval val = {0};
|
||||
int sent = 0;
|
||||
for (int i = 0; i < 400; i++) {
|
||||
for (int s = 0; s < 8; s++)
|
||||
if (pthread_sigqueue(w, SIGRTMIN + s, val) == 0) sent++;
|
||||
usleep(200);
|
||||
}
|
||||
for (int i = 0; i < 2000 && __atomic_load_n(&burst_count, __ATOMIC_RELAXED) < sent; i++) usleep(1000);
|
||||
stop_worker = 1;
|
||||
pthread_join(w, NULL);
|
||||
CHECK("burst_all_handled", burst_count == sent && sent > 0);
|
||||
close(busy_obj);
|
||||
}
|
||||
|
||||
static volatile int oneshot_count;
|
||||
static void oneshot(int sig, siginfo_t *si, void *uc) {
|
||||
(void)sig; (void)si; (void)uc;
|
||||
__atomic_add_fetch(&oneshot_count, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void oneshots(void) {
|
||||
busy_obj = event(0, 0);
|
||||
pthread_t w;
|
||||
stop_worker = 0;
|
||||
oneshot_count = 0;
|
||||
pthread_create(&w, NULL, worker, NULL);
|
||||
int fired = 0;
|
||||
for (int i = 0; i < 300; i++) {
|
||||
if (install_oneshot(SIGWINCH, oneshot) != 0) break;
|
||||
int before = __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED);
|
||||
pthread_kill(w, SIGWINCH);
|
||||
fired++;
|
||||
for (int j = 0; j < 2000 && __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED) == before; j++) usleep(50);
|
||||
}
|
||||
stop_worker = 1;
|
||||
pthread_join(w, NULL);
|
||||
CHECK("oneshot_handlers_run", oneshot_count == fired && fired == 300);
|
||||
close(busy_obj);
|
||||
}
|
||||
|
||||
static int outer_obj, inner_obj, other_dev, other_obj;
|
||||
static volatile int nested_ok, nested_done, outer_done;
|
||||
static void nesting(int sig, siginfo_t *si, void *uc) {
|
||||
(void)sig; (void)si; (void)uc;
|
||||
uint32_t idx = 99;
|
||||
int ok = wait_on(NT_WAIT_ANY, &inner_obj, 1, 21, 0, mono_ns(50), &idx) == 0 && idx == 0;
|
||||
struct wait_args w = {mono_ns(2), (uintptr_t)&other_obj, 1, 0xdead, 0, 22, 0, 0};
|
||||
ok = ok && ioctl(other_dev, NT_WAIT_ANY, &w) < 0 && errno == ETIMEDOUT;
|
||||
nested_ok += ok;
|
||||
__atomic_store_n(&nested_done, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
static void *outer_waiter(void *p) {
|
||||
struct wait_args w = {mono_ns(3000), (uintptr_t)&outer_obj, 1, 99, 0, 23, 0, 0};
|
||||
int r;
|
||||
do { r = ioctl(dev, NT_WAIT_ANY, &w); } while (r < 0 && errno == EINTR);
|
||||
*(int *)p = r == 0 && w.index == 0;
|
||||
__atomic_store_n(&outer_done, 1, __ATOMIC_RELEASE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void nested_waits(void) {
|
||||
void *seen = NULL;
|
||||
other_dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
|
||||
CHECK("second_device", other_dev >= 0);
|
||||
struct event_args ea = {0, 0};
|
||||
other_obj = ioctl(other_dev, NT_CREATE_EVENT, &ea);
|
||||
inner_obj = event(1, 1);
|
||||
CHECK("install_nesting", install_handler(SIGUSR2, nesting, &seen) == 0);
|
||||
int good = 0, early = 0;
|
||||
nested_ok = 0;
|
||||
for (int i = 0; i < 40; i++) {
|
||||
outer_obj = event(0, 0);
|
||||
int result = 0;
|
||||
pthread_t t;
|
||||
nested_done = outer_done = 0;
|
||||
pthread_create(&t, NULL, outer_waiter, &result);
|
||||
usleep(5000);
|
||||
pthread_kill(t, SIGUSR2);
|
||||
while (!__atomic_load_n(&nested_done, __ATOMIC_ACQUIRE)) usleep(200);
|
||||
usleep(5000);
|
||||
early += __atomic_load_n(&outer_done, __ATOMIC_ACQUIRE);
|
||||
uint32_t v = 0;
|
||||
ioctl(outer_obj, NT_EVENT_SET, &v);
|
||||
pthread_join(t, NULL);
|
||||
good += result;
|
||||
close(outer_obj);
|
||||
}
|
||||
CHECK("outer_wait_not_woken_by_nested_wait", early == 0);
|
||||
CHECK("outer_wait_survives_nested_waits", good == 40);
|
||||
CHECK("nested_waits_complete", nested_ok == 40);
|
||||
close(inner_obj);
|
||||
close(other_obj);
|
||||
close(other_dev);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
alarm(60);
|
||||
dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
|
||||
if (dev < 0) {
|
||||
printf("no device %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
semaphores();
|
||||
mutexes();
|
||||
events();
|
||||
any_order();
|
||||
all_semantics();
|
||||
cross_thread();
|
||||
cross_process();
|
||||
stress();
|
||||
signals();
|
||||
bursts();
|
||||
oneshots();
|
||||
nested_waits();
|
||||
printf("done fails=%d\n", fails);
|
||||
return fails != 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class NtsyncPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if not shutil.which("cc"):
|
||||
raise unittest.SkipTest("no compiler")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
(tmp / "ntdll.c").write_text(NTDLL)
|
||||
(tmp / "prog.c").write_text(PROGRAM)
|
||||
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"), *sources, "-ldl"],
|
||||
check=True)
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True)
|
||||
subprocess.run(["cc", "-O1", "-pthread", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"),
|
||||
"-Wl,-rpath," + str(tmp)], check=True)
|
||||
cls.program = tmp / "prog"
|
||||
cls.preload = tmp / "libblsession.so"
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def run_program(self):
|
||||
env = dict(os.environ, LD_PRELOAD=str(self.preload), BL_SYNC="1")
|
||||
return subprocess.run([str(self.program)], env=env, capture_output=True, text=True, timeout=90)
|
||||
|
||||
def test_emulation_matches_the_ntsync_driver(self):
|
||||
out = self.run_program()
|
||||
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
|
||||
self.assertIn("done fails=0", out.stdout)
|
||||
|
||||
def test_repeated_runs_stay_consistent(self):
|
||||
for _ in range(3):
|
||||
out = self.run_program()
|
||||
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,187 @@
|
||||
import copy
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location('preload_snapshots', Path(__file__).resolve().parents[1] / 'components/preloads.py')
|
||||
snapshots = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(snapshots)
|
||||
|
||||
|
||||
def archive(files):
|
||||
stream = io.BytesIO()
|
||||
with tarfile.open(fileobj=stream, mode='w:gz') as tar:
|
||||
for name, content in files.items():
|
||||
entry = tarfile.TarInfo(name)
|
||||
entry.size = len(content)
|
||||
tar.addfile(entry, io.BytesIO(content))
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
class PreloadSnapshotTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
self.root = Path(directory.name)
|
||||
# Real repository inputs exercise the transitive include walker and build recipe.
|
||||
for group in snapshots.INPUTS:
|
||||
for name in snapshots.source_files(group):
|
||||
if name.startswith('@'):
|
||||
continue
|
||||
target = self.root / name
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes((snapshots.ROOT / name).read_bytes())
|
||||
recipe = 'tools/components/build-arm64-preloads.sh'
|
||||
(self.root / recipe).parent.mkdir(parents=True, exist_ok=True)
|
||||
(self.root / recipe).write_text((snapshots.ROOT / recipe).read_text())
|
||||
x86 = 'tools/components/recipes/x86-preloads.sh'
|
||||
(self.root / x86).parent.mkdir(parents=True, exist_ok=True)
|
||||
(self.root / x86).write_text((snapshots.ROOT / x86).read_text())
|
||||
(self.root / '.github/workflows').mkdir(parents=True)
|
||||
(self.root / '.github/workflows/build.yml').write_text((snapshots.ROOT / '.github/workflows/build.yml').read_text())
|
||||
self.git('init', '-q')
|
||||
self.git('config', 'core.autocrlf', 'false')
|
||||
self.git('config', 'user.name', 'Test Developer')
|
||||
self.git('config', 'user.email', 'test@example.test')
|
||||
self.git('add', '.')
|
||||
self.binaries = {name: ('verified-' + name).encode() for name in snapshots.FILES}
|
||||
self.content = archive(self.binaries)
|
||||
self.data = dict(version=1, origin=dict(run=123, commit='abc', artifact=456, artifact_sha256='0' * 64),
|
||||
groups={g: snapshots.source_files(g, self.root) for g in snapshots.INPUTS},
|
||||
arm64_recipe=snapshots.recipe(self.root), x86_recipe=snapshots.x86_recipe(self.root),
|
||||
archive_sha256=snapshots.sha(self.content),
|
||||
outputs={name: snapshots.sha(value) for name, value in self.binaries.items()})
|
||||
(self.root / snapshots.LOCK).write_text(json.dumps(self.data))
|
||||
self.git('add', '.')
|
||||
self.git('commit', '-qm', 'Approved baseline')
|
||||
self.base = self.git('rev-parse', 'HEAD').strip()
|
||||
|
||||
def git(self, *args):
|
||||
return subprocess.check_output(['git', *args], cwd=self.root, text=True)
|
||||
|
||||
def restore(self, base=None):
|
||||
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=base or self.base, FORCE_PRELOAD_REBUILD='false'), \
|
||||
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(self.content)) as fetch, \
|
||||
patch.object(snapshots, 'prove_origin') as prove:
|
||||
result = snapshots.restore(self.root)
|
||||
return result, fetch, prove
|
||||
|
||||
def test_reviewed_unchanged_binaries_reused_and_written(self):
|
||||
result, fetch, prove = self.restore()
|
||||
self.assertEqual(result, dict(arm64=True, x86=True))
|
||||
prove.assert_not_called()
|
||||
fetch.assert_called_once()
|
||||
for name, value in self.binaries.items():
|
||||
self.assertEqual((self.root / 'app/src/main/assets/linuxfs' / name).read_bytes(), value)
|
||||
|
||||
def test_changed_common_source_requires_both_rebuilds(self):
|
||||
path = self.root / snapshots.COMMON[0]
|
||||
path.write_text(path.read_text() + '\n// Changed source\n')
|
||||
result, fetch, prove = self.restore()
|
||||
self.assertEqual(result, dict(arm64=False, x86=False))
|
||||
fetch.assert_not_called()
|
||||
prove.assert_not_called()
|
||||
|
||||
def test_new_transitive_header_is_an_input(self):
|
||||
source = self.root / snapshots.COMMON[0]
|
||||
source.write_text(source.read_text() + '\n#include "new-header.h"\n')
|
||||
header = source.with_name('new-header.h')
|
||||
header.write_text('#define VALUE 1\n')
|
||||
before = snapshots.source_files('arm64', self.root)
|
||||
header.write_text('#define VALUE 2\n')
|
||||
self.assertNotEqual(before, snapshots.source_files('arm64', self.root))
|
||||
|
||||
def test_changed_recipe_rebuilds_arm64_only(self):
|
||||
script = self.root / 'tools/components/build-arm64-preloads.sh'
|
||||
script.write_text(script.read_text().replace('-O2', '-O3'))
|
||||
result, _, _ = self.restore()
|
||||
self.assertEqual(result, dict(arm64=False, x86=True))
|
||||
|
||||
def test_shell_newline_changes_invalidate_the_recipe(self):
|
||||
script = self.root / 'tools/components/build-arm64-preloads.sh'
|
||||
original = snapshots.recipe(self.root)
|
||||
text = script.read_text()
|
||||
script.write_text(text.replace('-ldl\n', '-ldl ', 1))
|
||||
self.assertNotEqual(original, snapshots.recipe(self.root))
|
||||
|
||||
def test_x86_container_recipe_changes_rebuild_x86_only(self):
|
||||
script = self.root / 'tools/components/recipes/x86-preloads.sh'
|
||||
script.write_text(script.read_text().replace('debian:bullseye', 'debian:bookworm'))
|
||||
result, _, _ = self.restore()
|
||||
self.assertEqual(result, dict(arm64=True, x86=False))
|
||||
|
||||
def test_new_manifest_requires_main_build_proof(self):
|
||||
result, _, prove = self.restore('HEAD~1')
|
||||
self.assertTrue(all(result.values()))
|
||||
prove.assert_called_once()
|
||||
|
||||
def test_forged_binary_hash_cannot_prove_main_origin(self):
|
||||
data = copy.deepcopy(self.data)
|
||||
data['outputs']['libblsession.so'] = 'f' * 64
|
||||
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries):
|
||||
with self.assertRaisesRegex(ValueError, 'original main APK'):
|
||||
snapshots.prove_origin(data, self.root / 'cache', self.root)
|
||||
|
||||
def test_modified_input_policy_cannot_omit_producer_dependencies(self):
|
||||
data = copy.deepcopy(self.data)
|
||||
data['origin']['commit'] = self.base
|
||||
omitted = snapshots.COMMON[0]
|
||||
del data['groups']['arm64'][omitted]
|
||||
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries), \
|
||||
patch.object(snapshots, 'run', return_value=b''), \
|
||||
patch.object(snapshots, 'source_files', side_effect=lambda group, *args: data['groups'][group]), \
|
||||
patch.object(snapshots, 'producer_sources', return_value={omitted: self.data['groups']['arm64'][omitted]}):
|
||||
with self.assertRaisesRegex(ValueError, 'producing build dependency'):
|
||||
snapshots.prove_origin(data, self.root / 'cache', self.root)
|
||||
|
||||
def test_non_main_or_unsuccessful_run_cannot_prove_origin(self):
|
||||
build = dict(event='pull_request', head_branch='main', conclusion='success', head_sha='abc',
|
||||
path='.github/workflows/build.yml', repository=dict(full_name=snapshots.REPO))
|
||||
for field, value in [('event', 'pull_request'), ('head_branch', 'feature'), ('conclusion', 'failure')]:
|
||||
candidate = dict(build, event='push')
|
||||
candidate[field] = value
|
||||
with self.subTest(field=field), patch.object(snapshots, 'api', return_value=candidate):
|
||||
with self.assertRaisesRegex(ValueError, 'successful main'):
|
||||
snapshots.artifact_bytes(self.data['origin'], self.root / 'cache')
|
||||
|
||||
def test_corrupt_archive_is_rejected(self):
|
||||
with self.assertRaisesRegex(ValueError, 'checksum'):
|
||||
snapshots.unpack(self.content + b'tampered', self.data)
|
||||
|
||||
def test_missing_duplicate_or_traversal_entries_are_rejected(self):
|
||||
for files in ({}, {'../escaped': b'bad'}, {'libblsession.so': b'wrong'}):
|
||||
content = archive(files)
|
||||
data = dict(self.data, archive_sha256=snapshots.sha(content))
|
||||
with self.subTest(names=list(files)), self.assertRaises(ValueError):
|
||||
snapshots.unpack(content, data)
|
||||
|
||||
def test_missing_release_rebuilds_without_accepting_unverified_bytes(self):
|
||||
error = snapshots.urllib.error.HTTPError('https://example.test', 404, 'missing', {}, None)
|
||||
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
|
||||
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
|
||||
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
|
||||
|
||||
def test_transient_download_failure_rebuilds_but_integrity_errors_fail(self):
|
||||
errors = [TimeoutError('network'), snapshots.http.client.IncompleteRead(b'partial'),
|
||||
snapshots.urllib.error.HTTPError('https://example.test', 503, 'unavailable', {}, None)]
|
||||
for error in errors:
|
||||
with self.subTest(error=type(error).__name__), patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
|
||||
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
|
||||
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
|
||||
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
|
||||
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(b'corrupt')):
|
||||
with self.assertRaisesRegex(ValueError, 'checksum'):
|
||||
snapshots.restore(self.root)
|
||||
|
||||
def test_manual_rebuild_does_not_download_snapshot(self):
|
||||
with patch.dict(os.environ, FORCE_PRELOAD_REBUILD='true'), \
|
||||
patch.object(snapshots.urllib.request, 'urlopen') as fetch:
|
||||
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
|
||||
fetch.assert_not_called()
|
||||
@@ -1,4 +1,4 @@
|
||||
"""The XTest cursor path is enabled only for Steam sessions."""
|
||||
"""Gamescope composites the cursor only when a Steam session asks for it."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
@@ -8,29 +8,37 @@ import unittest
|
||||
SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
|
||||
SOURCE = SESSION.read_text()
|
||||
MATCH = re.search(
|
||||
r'^ if \[ "\$mode" = steam \]; then\n.*?^ fi\n', SOURCE, re.M | re.S)
|
||||
r'^ if \[ "\$mode" = steam \] && \[ "\$\{BL_CURSOR_COMPOSITE:-0\}" = 1 \]; then\n.*?^ fi\n',
|
||||
SOURCE, re.M | re.S)
|
||||
assert MATCH is not None
|
||||
CURSOR_SETUP = MATCH.group(0)
|
||||
|
||||
|
||||
class SessionCursorRoutingTest(unittest.TestCase):
|
||||
def run_setup(self, mode, initial=None):
|
||||
def run_setup(self, mode, initial=None, composite=None):
|
||||
env = dict(os.environ, mode=mode)
|
||||
if initial is not None:
|
||||
env['gamescope_cursor_composite'] = initial
|
||||
else:
|
||||
env.pop('gamescope_cursor_composite', None)
|
||||
if composite is not None:
|
||||
env['BL_CURSOR_COMPOSITE'] = composite
|
||||
else:
|
||||
env.pop('BL_CURSOR_COMPOSITE', None)
|
||||
result = subprocess.run(
|
||||
['bash', '-c', CURSOR_SETUP + 'printf "%s\n" "${gamescope_cursor_composite-}"'],
|
||||
env=env, text=True, capture_output=True, check=True)
|
||||
return result.stdout.strip()
|
||||
|
||||
def test_steam_enables_manual_composition_for_the_nested_cursor(self):
|
||||
self.assertEqual('2', self.run_setup('steam'))
|
||||
def test_steam_leaves_the_cursor_with_the_host_unless_asked(self):
|
||||
self.assertEqual('', self.run_setup('steam'))
|
||||
|
||||
def test_steam_composites_the_cursor_when_the_setting_is_on(self):
|
||||
self.assertEqual('2', self.run_setup('steam', composite='1'))
|
||||
|
||||
def test_other_sessions_keep_their_existing_cursor_composition(self):
|
||||
self.assertEqual('', self.run_setup('desktop'))
|
||||
self.assertEqual('1', self.run_setup('run', '1'))
|
||||
self.assertEqual('', self.run_setup('desktop', composite='1'))
|
||||
self.assertEqual('1', self.run_setup('run', '1', composite='1'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -56,7 +56,7 @@ cp "$TEST_CDN/${url##*/}" "$out"
|
||||
self.env = dict(os.environ, HOME=str(self.home), XDG_DATA_HOME=str(self.data),
|
||||
PATH=str(self.bin) + os.pathsep + os.environ['PATH'],
|
||||
TEST_CDN=str(self.cdn), TEST_REQUESTS=str(self.requests))
|
||||
for channel in ('steamdeck_publicbeta',):
|
||||
for channel in ('steamdeck_publicbeta', 'steamdeck_stable'):
|
||||
archive = self.cdn / (channel + '.zip')
|
||||
with zipfile.ZipFile(archive, 'w') as package:
|
||||
package.writestr('steamrtarm64/steam', channel)
|
||||
@@ -68,12 +68,17 @@ cp "$TEST_CDN/${url##*/}" "$out"
|
||||
f'\t\t"file" "{archive.name}"\n\t\t"sha2" "{digest}"\n'
|
||||
'\t}\n}\n')
|
||||
|
||||
def install(self, fail_host=None):
|
||||
def install(self, fail_host=None, *args, **extra):
|
||||
env = self.env if fail_host is None else dict(self.env, TEST_FAIL_HOST=fail_host)
|
||||
return subprocess.run(['bash', str(SCRIPT)], env=env, text=True, capture_output=True)
|
||||
return subprocess.run(['bash', str(SCRIPT), *args], env=dict(env, **extra), text=True, capture_output=True)
|
||||
|
||||
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None):
|
||||
result = self.install(fail_host)
|
||||
def channel(self, **extra):
|
||||
result = self.install(None, '--channel', **extra)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
return result.stdout.strip()
|
||||
|
||||
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None, **extra):
|
||||
result = self.install(fail_host, **extra)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual(channel + '\n', (self.steam / 'package/beta').read_text())
|
||||
self.assertEqual('123456\n', (self.steam / 'package/droiddeck-installed').read_text())
|
||||
@@ -87,6 +92,7 @@ cp "$TEST_CDN/${url##*/}" "$out"
|
||||
'https://client-update.steamstatic.com/' + channel + '.zip',
|
||||
]
|
||||
self.assertEqual(expected, self.requests.read_text().splitlines())
|
||||
return result
|
||||
|
||||
def test_downloads_the_deck_beta_client(self):
|
||||
self.assert_installed()
|
||||
@@ -155,8 +161,80 @@ cp "$TEST_CDN/${url##*/}" "$out"
|
||||
self.requests.unlink()
|
||||
with (package / 'droiddeck-unconfirmed').open('a') as marker:
|
||||
marker.write('start\nstart\nstart\n')
|
||||
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
|
||||
self.assertFalse(self.requests.exists())
|
||||
|
||||
def test_the_default_channel_is_the_deck_beta(self):
|
||||
self.assertEqual('steamdeck_publicbeta', self.channel())
|
||||
|
||||
def stable_requests(self, beta_manifest=True):
|
||||
stable = [
|
||||
'https://client-update.steamstatic.com/steam_client_steamdeck_stable_linuxarm64',
|
||||
'https://client-update.steamstatic.com/steamdeck_stable.zip',
|
||||
]
|
||||
if beta_manifest:
|
||||
stable.insert(0, 'https://client-update.steamstatic.com/steam_client_steamdeck_publicbeta_linuxarm64')
|
||||
return stable
|
||||
|
||||
def test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable(self):
|
||||
package = self.break_client(2)
|
||||
self.assertEqual(0, self.install().returncode)
|
||||
self.requests.unlink()
|
||||
with (package / 'droiddeck-unconfirmed').open('a') as marker:
|
||||
marker.write('start\nstart\n')
|
||||
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
|
||||
self.assertIn("falling back to Valve's steamdeck_stable Steam client", result.stdout)
|
||||
self.assertEqual('steamdeck_stable\n', (package / 'droiddeck-channel').read_text())
|
||||
self.assertEqual('repaired\n', (package / 'droiddeck-unconfirmed').read_text())
|
||||
self.assertEqual('steamdeck_stable', self.channel())
|
||||
|
||||
def test_the_stable_fallback_is_not_repeated(self):
|
||||
self.test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable()
|
||||
self.requests.unlink()
|
||||
with (self.steam / 'package/droiddeck-unconfirmed').open('a') as marker:
|
||||
marker.write('start\nstart\nstart\n')
|
||||
self.assertEqual(0, self.install().returncode)
|
||||
self.assertFalse(self.requests.exists())
|
||||
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/beta').read_text())
|
||||
|
||||
def test_a_pinned_beta_channel_never_falls_back(self):
|
||||
package = self.break_client(2)
|
||||
self.assertEqual(0, self.install().returncode)
|
||||
with (package / 'droiddeck-unconfirmed').open('a') as marker:
|
||||
marker.write('start\nstart\n')
|
||||
self.assertEqual('steamdeck_publicbeta', self.channel(BL_STEAM_CHANNEL='beta'))
|
||||
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
|
||||
self.assertFalse((package / 'droiddeck-channel').exists())
|
||||
|
||||
def test_the_channel_setting_selects_stable(self):
|
||||
setting = self.home / '.config/droiddeck/steam-channel'
|
||||
setting.parent.mkdir(parents=True)
|
||||
setting.write_text('stable\n')
|
||||
self.assertEqual('steamdeck_stable', self.channel())
|
||||
self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
|
||||
self.assertFalse((self.steam / 'package/droiddeck-channel').exists())
|
||||
|
||||
def empty_beta_manifest(self):
|
||||
(self.cdn / 'steam_client_steamdeck_publicbeta_linuxarm64').write_text(
|
||||
'"linuxarm64"\n{\n\t"version" "123457"\n'
|
||||
'\t"client_steamrtarm64_linuxarm64"\n\t{\n'
|
||||
'\t\t"file" "client.zip"\n\t\t"sha2" "00"\n'
|
||||
'\t}\n}\n')
|
||||
|
||||
def test_a_beta_manifest_without_an_arm64_client_falls_back_to_stable(self):
|
||||
self.empty_beta_manifest()
|
||||
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests())
|
||||
self.assertIn('no client components in steam_client_steamdeck_publicbeta_linuxarm64', result.stderr)
|
||||
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
|
||||
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/droiddeck-channel').read_text())
|
||||
self.assertEqual('steamdeck_stable', self.channel())
|
||||
|
||||
def test_a_pinned_beta_manifest_without_an_arm64_client_fails_and_names_its_blocks(self):
|
||||
self.empty_beta_manifest()
|
||||
result = self.install(BL_STEAM_CHANNEL='beta')
|
||||
self.assertNotEqual(0, result.returncode)
|
||||
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
|
||||
self.assertFalse((self.steam / 'package/droiddeck-installed').exists())
|
||||
|
||||
def test_requested_repair_runs_once_and_is_retried_until_it_completes(self):
|
||||
package = self.break_client(0)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
|
||||
MODULE = runpy.run_path(str(BIN / 'droiddeck-steam-shaders'))
|
||||
apply = MODULE['apply']
|
||||
set_text = MODULE['set_text']
|
||||
parse = runpy.run_path(str(BIN / 'droiddeck-steam-ui-scale'))['parse']
|
||||
|
||||
THOR = '''"InstallConfigStore"
|
||||
{
|
||||
\t"Software"
|
||||
\t{
|
||||
\t\t"Valve"
|
||||
\t\t{
|
||||
\t\t\t"Steam"
|
||||
\t\t\t{
|
||||
\t\t\t\t"ShaderCacheManager"
|
||||
\t\t\t\t{
|
||||
\t\t\t\t\t"HasCurrentBucket"\t\t"1"
|
||||
\t\t\t\t\t"ProcessingQueue"\t\t"207140;848350;"
|
||||
\t\t\t\t\t"App"
|
||||
\t\t\t\t\t{
|
||||
\t\t\t\t\t}
|
||||
\t\t\t\t}
|
||||
\t\t\t}
|
||||
\t\t}
|
||||
\t}
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def disabled(text):
|
||||
steam = parse(text).get('InstallConfigStore').get('Software').get('Valve').get('Steam')
|
||||
return steam.get('ShaderCacheManager').get('DisableShaderCache')
|
||||
|
||||
|
||||
class SteamShadersTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
self.config = self.root / 'config/config.vdf'
|
||||
self.config.parent.mkdir()
|
||||
|
||||
def test_off_adds_the_key_the_client_writes(self):
|
||||
text, changed = set_text(THOR, False)
|
||||
self.assertTrue(changed)
|
||||
self.assertEqual('1', disabled(text))
|
||||
self.assertIn('\t\t\t\t\t"DisableShaderCache"\t\t"1"\n\t\t\t\t}', text)
|
||||
self.assertIn('"ProcessingQueue"\t\t"207140;848350;"', text)
|
||||
|
||||
def test_existing_value_is_replaced_in_place(self):
|
||||
off, _ = set_text(THOR, False)
|
||||
on, changed = set_text(off, True)
|
||||
self.assertTrue(changed)
|
||||
self.assertEqual('0', disabled(on))
|
||||
self.assertEqual(off.replace('"DisableShaderCache"\t\t"1"', '"DisableShaderCache"\t\t"0"'), on)
|
||||
|
||||
def test_matching_value_leaves_the_file_alone(self):
|
||||
off, _ = set_text(THOR, False)
|
||||
self.assertEqual((off, False), set_text(off, False))
|
||||
|
||||
def test_missing_sections_are_created(self):
|
||||
text, changed = set_text('"InstallConfigStore"\n{\n}\n', False)
|
||||
self.assertTrue(changed)
|
||||
self.assertEqual('1', disabled(text))
|
||||
|
||||
def test_apply_writes_config_and_creates_a_fresh_one(self):
|
||||
self.config.write_text(THOR)
|
||||
self.assertTrue(apply(self.root, False))
|
||||
self.assertEqual('1', disabled(self.config.read_text()))
|
||||
self.assertFalse(apply(self.root, False))
|
||||
fresh = Path(self.temp.name) / 'fresh'
|
||||
fresh.mkdir()
|
||||
self.assertTrue(apply(fresh, True))
|
||||
self.assertEqual('0', disabled((fresh / 'config/config.vdf').read_text()))
|
||||
|
||||
def test_invalid_config_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
set_text('"InstallConfigStore" {', False)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,123 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
|
||||
SHIM = BIN / 'droiddeck-steamrt-shim'
|
||||
SESSION = (BIN / 'droiddeck-session').read_text()
|
||||
X64 = (BIN / 'droiddeck-steam-x64').read_text()
|
||||
|
||||
|
||||
class SteamrtShimTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
self.client = self.root / 'steamrtarm64'
|
||||
self.client.mkdir()
|
||||
self.shim = self.root / 'shim'
|
||||
|
||||
def runtime(self, path):
|
||||
runtime = self.client / path
|
||||
(runtime / 'bin').mkdir(parents=True)
|
||||
(runtime / 'bin/steam-runtime-check-requirements').write_text('#!/bin/sh\nexit 1\n')
|
||||
(runtime / 'bin/steam-runtime-system-info').write_text('#!/bin/sh\necho info\n')
|
||||
(runtime / 'files').mkdir()
|
||||
(runtime / 'files/marker').write_text('runtime files')
|
||||
return runtime
|
||||
|
||||
def build(self):
|
||||
return subprocess.run(['bash', str(SHIM), str(self.client), str(self.shim)],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
|
||||
def sh(self, *args):
|
||||
return subprocess.run(['sh', *map(str, args)], capture_output=True, text=True, timeout=30)
|
||||
|
||||
def assert_shim(self):
|
||||
result = self.build()
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual(0, self.sh(self.shim / 'bin/steam-runtime-check-requirements').returncode)
|
||||
self.assertEqual('info\n', self.sh(self.shim / 'bin/steam-runtime-system-info').stdout)
|
||||
self.assertEqual('runtime files', (self.shim / 'files/marker').read_text())
|
||||
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env=BL_SHIM_TEST=yes',
|
||||
f'--directory={self.root}', '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
|
||||
self.assertEqual(f'yes {self.root.name}\n', launched.stdout)
|
||||
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env', 'BL_SHIM_TEST=split',
|
||||
'--directory', self.root, '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
|
||||
self.assertEqual(f'split {self.root.name}\n', launched.stdout)
|
||||
missing = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--directory', self.root / 'missing',
|
||||
'--', 'true')
|
||||
self.assertEqual(127, missing.returncode)
|
||||
|
||||
def test_a_client_without_a_container_gets_no_shim(self):
|
||||
self.shim.mkdir()
|
||||
self.assertEqual(1, self.build().returncode)
|
||||
self.assertFalse(self.shim.exists())
|
||||
|
||||
def test_the_pressure_vessel_runtime_is_shimmed(self):
|
||||
self.runtime('pv-runtime/steam-runtime-steamrt')
|
||||
self.assert_shim()
|
||||
|
||||
def test_a_requirements_probe_elsewhere_is_shimmed(self):
|
||||
self.runtime('steam-runtime')
|
||||
self.assert_shim()
|
||||
|
||||
def test_the_arm64_session_starts_a_containerized_client_through_the_shim(self):
|
||||
block = re.search(r'\n( +steam_client=\("\$steam_root/steamrtarm64/steam"\)\n.*?\n fi\n)', SESSION, re.S).group(1)
|
||||
stub = self.root / 'droiddeck-steamrt-shim'
|
||||
for status, shimmed in ((1, False), (0, True)):
|
||||
stub.write_text(f'#!/bin/bash\nexit {status}\n')
|
||||
stub.chmod(0o755)
|
||||
script = 'if :; then\n' + block + 'printf "%s\\n" "${steam_client[@]}"\n'
|
||||
env = dict(os.environ, HOME=str(self.root), steam_root='/steam',
|
||||
PATH=str(self.root) + os.pathsep + os.environ['PATH'])
|
||||
result = subprocess.run(['bash', '-c', script], env=env, capture_output=True, text=True, timeout=30)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
lines = result.stdout.splitlines()
|
||||
self.assertEqual('/steam/steamrtarm64/steam', lines[-1])
|
||||
self.assertEqual(shimmed, 'Valve containerized the arm64 Steam client' in result.stdout)
|
||||
self.assertEqual(shimmed, any(line.startswith('STEAM_RUNTIME_STEAMRT=') and
|
||||
line.endswith('/.droiddeck-steamrt-shim') for line in lines))
|
||||
|
||||
def test_both_clients_are_told_they_already_run_inside_the_container(self):
|
||||
guard = SESSION.index('export STEAM_STEAMRT_RECURSION_GUARD=1')
|
||||
self.assertLess(guard, SESSION.index('"${steam_client[@]}" "${_bl_ui[@]}"'))
|
||||
self.assertIn('-u STEAM_STEAMRT_RECURSION_GUARD', SESSION[:guard])
|
||||
self.assertIn('export STEAM_STEAMRT_RECURSION_GUARD=1', X64)
|
||||
self.assertIn('droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"', X64)
|
||||
|
||||
|
||||
class SessionChannelTest(unittest.TestCase):
|
||||
BLOCK = re.search(r'\n( +steam_channel=steamdeck_publicbeta\n.*?\n +fi\n)', SESSION, re.S).group(1)
|
||||
|
||||
def channel(self, x64, installer):
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
stub = Path(temp) / 'droiddeck-steam-install'
|
||||
stub.write_text('#!/bin/bash\n' + installer + '\n')
|
||||
stub.chmod(0o755)
|
||||
env = dict(os.environ, steam_x64=x64, PATH=temp + os.pathsep + os.environ['PATH'])
|
||||
result = subprocess.run(['bash', '-c', self.BLOCK + 'echo "$steam_channel"\n'], env=env,
|
||||
capture_output=True, text=True, timeout=30)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
return result.stdout.strip()
|
||||
|
||||
def test_the_arm64_client_follows_the_installed_channel(self):
|
||||
self.assertEqual('steamdeck_stable', self.channel('0', '[ "$1" = --channel ] && echo steamdeck_stable'))
|
||||
|
||||
def test_an_installer_without_an_answer_keeps_the_deck_beta(self):
|
||||
self.assertEqual('steamdeck_publicbeta', self.channel('0', 'exit 1'))
|
||||
|
||||
def test_the_x86_client_keeps_its_own_channel(self):
|
||||
self.assertEqual('steamdeck_publicbeta', self.channel('1', 'echo steamdeck_stable'))
|
||||
|
||||
def test_the_client_branch_on_disk_and_clientbeta_use_the_same_channel(self):
|
||||
self.assertIn('-clientbeta "$steam_channel"', SESSION)
|
||||
self.assertIn('echo "$steam_channel" > "$steam_beta"', SESSION)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,154 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
NTDLL = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
__attribute__((noinline)) int ntdll_open(const char *path) {
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
__asm__ volatile("" ::: "memory");
|
||||
return fd;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) int ntdll_openat(const char *path) {
|
||||
int fd = openat(AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
__asm__ volatile("" ::: "memory");
|
||||
return fd;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) FILE *ntdll_fopen(const char *path) {
|
||||
FILE *f = fopen(path, "r");
|
||||
__asm__ volatile("" ::: "memory");
|
||||
return f;
|
||||
}
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ptrace.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int ntdll_open(const char *path);
|
||||
int ntdll_openat(const char *path);
|
||||
FILE *ntdll_fopen(const char *path);
|
||||
|
||||
static void report(const char *name, int fd) {
|
||||
char link[64], target[256] = "", text[16384], *field;
|
||||
ssize_t n;
|
||||
long tracer = -1;
|
||||
if (fd < 0) {
|
||||
printf("%s failed\n", name);
|
||||
return;
|
||||
}
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
|
||||
n = readlink(link, target, sizeof(target) - 1);
|
||||
target[n > 0 ? n : 0] = '\0';
|
||||
n = pread(fd, text, sizeof(text) - 1, 0);
|
||||
text[n > 0 ? n : 0] = '\0';
|
||||
if ((field = strstr(text, "\nTracerPid:\t")) != NULL) tracer = strtol(field + 12, NULL, 10);
|
||||
const char *kind = strncmp(target, "/proc/", 6) == 0 ? "proc"
|
||||
: strncmp(target, "/memfd:", 7) == 0 ? "memfd"
|
||||
: strncmp(target, "/tmp/.droiddeck-status-", 23) == 0 ? "copy" : target;
|
||||
printf("%s kind=%s tracer=%s rdonly=%d vm=%d\n", name, kind, tracer == 0 ? "hidden" : tracer > 0 ? "shown" : "missing",
|
||||
(fcntl(fd, F_GETFL) & O_ACCMODE) == O_RDONLY, strstr(text, "\nVmRSS:") != NULL);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void report_stream(const char *name, FILE *f) {
|
||||
if (f == NULL) {
|
||||
printf("%s failed\n", name);
|
||||
return;
|
||||
}
|
||||
report(name, dup(fileno(f)));
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ready[2];
|
||||
if (pipe(ready) != 0) return 1;
|
||||
pid_t child = fork();
|
||||
if (child == 0) {
|
||||
char ok = ptrace(PTRACE_TRACEME, 0, NULL, NULL) == 0;
|
||||
if (write(ready[1], &ok, 1) != 1) _exit(1);
|
||||
for (;;) pause();
|
||||
}
|
||||
char ok = 0;
|
||||
if (read(ready[0], &ok, 1) != 1 || !ok) {
|
||||
kill(child, SIGKILL);
|
||||
waitpid(child, NULL, 0);
|
||||
puts("no ptrace");
|
||||
return 0;
|
||||
}
|
||||
char other[64], self[64];
|
||||
snprintf(other, sizeof(other), "/proc/%d/status", (int)child);
|
||||
snprintf(self, sizeof(self), "/proc/%d/status", (int)getpid());
|
||||
report("main_other_open", open(other, O_RDONLY | O_CLOEXEC));
|
||||
report_stream("main_other_fopen", fopen(other, "r"));
|
||||
report("main_self_open", open("/proc/self/status", O_RDONLY | O_CLOEXEC));
|
||||
report("ntdll_other_open", ntdll_open(other));
|
||||
report("ntdll_other_openat", ntdll_openat(other));
|
||||
report_stream("ntdll_other_fopen", ntdll_fopen(other));
|
||||
report_stream("ntdll_self_fopen", ntdll_fopen(self));
|
||||
kill(child, SIGKILL);
|
||||
waitpid(child, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class TracerPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if not shutil.which("cc"):
|
||||
raise unittest.SkipTest("no compiler")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
(tmp / "ntdll.c").write_text(NTDLL)
|
||||
(tmp / "prog.c").write_text(PROGRAM)
|
||||
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"), *sources, "-ldl"],
|
||||
check=True)
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True)
|
||||
subprocess.run(["cc", "-O1", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"), "-Wl,-rpath," + str(tmp)],
|
||||
check=True)
|
||||
env = dict(os.environ, LD_PRELOAD=str(tmp / "libblsession.so"))
|
||||
out = subprocess.run([str(tmp / "prog")], env=env, capture_output=True, text=True, timeout=60)
|
||||
if out.returncode != 0:
|
||||
raise AssertionError(out.stdout + out.stderr)
|
||||
if "no ptrace" in out.stdout:
|
||||
raise unittest.SkipTest("ptrace not allowed")
|
||||
cls.lines = dict(line.split(" ", 1) for line in out.stdout.splitlines())
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def test_other_programs_still_see_no_tracer(self):
|
||||
self.assertEqual(self.lines["main_other_open"], "kind=copy tracer=hidden rdonly=1 vm=1")
|
||||
self.assertEqual(self.lines["main_other_fopen"], "kind=copy tracer=hidden rdonly=1 vm=1")
|
||||
self.assertEqual(self.lines["main_self_open"], "kind=memfd tracer=hidden rdonly=0 vm=1")
|
||||
|
||||
def test_wine_reads_the_real_status_file(self):
|
||||
for name in ("ntdll_other_open", "ntdll_other_openat", "ntdll_other_fopen"):
|
||||
self.assertEqual(self.lines[name], "kind=proc tracer=shown rdonly=1 vm=1", name)
|
||||
self.assertEqual(self.lines["ntdll_self_fopen"], "kind=proc tracer=hidden rdonly=1 vm=1")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,301 @@
|
||||
import ctypes
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
NTDLL = r"""
|
||||
#include <signal.h>
|
||||
__attribute__((noinline)) void syscall_dispatcher_return_slowpath(void) { raise(SIGUSR2); }
|
||||
void slowpath(void) {
|
||||
syscall_dispatcher_return_slowpath();
|
||||
__asm__ volatile("" ::: "memory");
|
||||
}
|
||||
void slowpath_other(int sig) {
|
||||
raise(sig);
|
||||
__asm__ volatile("" ::: "memory");
|
||||
}
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <asm/sigcontext.h>
|
||||
#include <pthread.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void slowpath(void);
|
||||
void slowpath_other(int sig);
|
||||
|
||||
static char altstack[65536];
|
||||
static char landing_stack[65536] __attribute__((aligned(16)));
|
||||
static volatile int mode;
|
||||
static int calls;
|
||||
static volatile int on_alt, usr2_blocked, usr1_blocked, code_ok, stack_ok;
|
||||
static volatile long handler_sp;
|
||||
static jmp_buf back;
|
||||
static uint64_t saved[31];
|
||||
static unsigned __int128 saved_v[32];
|
||||
static uint32_t saved_fpsr;
|
||||
|
||||
void landing(void);
|
||||
__asm__(".text\n.globl landing\n.type landing, %function\nlanding:\n"
|
||||
" sub sp, sp, #784\n"
|
||||
" stp x0, x1, [sp, #0]\n stp x2, x3, [sp, #16]\n stp x4, x5, [sp, #32]\n stp x6, x7, [sp, #48]\n"
|
||||
" stp x8, x9, [sp, #64]\n stp x10, x11, [sp, #80]\n stp x12, x13, [sp, #96]\n stp x14, x15, [sp, #112]\n"
|
||||
" stp x16, x17, [sp, #128]\n stp x18, x19, [sp, #144]\n stp x20, x21, [sp, #160]\n stp x22, x23, [sp, #176]\n"
|
||||
" stp x24, x25, [sp, #192]\n stp x26, x27, [sp, #208]\n stp x28, x29, [sp, #224]\n str x30, [sp, #240]\n"
|
||||
" add x9, sp, #256\n"
|
||||
" stp q0, q1, [x9, #0]\n stp q2, q3, [x9, #32]\n stp q4, q5, [x9, #64]\n stp q6, q7, [x9, #96]\n"
|
||||
" stp q8, q9, [x9, #128]\n stp q10, q11, [x9, #160]\n stp q12, q13, [x9, #192]\n stp q14, q15, [x9, #224]\n"
|
||||
" stp q16, q17, [x9, #256]\n stp q18, q19, [x9, #288]\n stp q20, q21, [x9, #320]\n stp q22, q23, [x9, #352]\n"
|
||||
" stp q24, q25, [x9, #384]\n stp q26, q27, [x9, #416]\n stp q28, q29, [x9, #448]\n stp q30, q31, [x9, #480]\n"
|
||||
" mrs x10, fpsr\n str w10, [sp, #768]\n"
|
||||
" mov x0, sp\n bl landed\n brk #0\n");
|
||||
|
||||
void landed(uint64_t *regs) {
|
||||
memcpy(saved, regs, sizeof(saved));
|
||||
memcpy(saved_v, regs + 32, sizeof(saved_v));
|
||||
saved_fpsr = *(uint32_t *)(regs + 96);
|
||||
longjmp(back, 1);
|
||||
}
|
||||
|
||||
static struct fpsimd_context *fpsimd_of(ucontext_t *uc) {
|
||||
struct _aarch64_ctx *c = (struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
|
||||
while (c->magic) {
|
||||
if (c->magic == FPSIMD_MAGIC) return (struct fpsimd_context *)c;
|
||||
c = (struct _aarch64_ctx *)((char *)c + c->size);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void handler(int sig, siginfo_t *info, void *ctx) {
|
||||
ucontext_t *uc = ctx;
|
||||
sigset_t now;
|
||||
long sp = (long)&now;
|
||||
__atomic_add_fetch(&calls, 1, __ATOMIC_RELAXED);
|
||||
handler_sp = sp;
|
||||
on_alt = sp >= (long)altstack && sp < (long)altstack + (long)sizeof(altstack);
|
||||
pthread_sigmask(SIG_BLOCK, NULL, &now);
|
||||
usr2_blocked = sigismember(&now, SIGUSR2);
|
||||
usr1_blocked = sigismember(&now, SIGUSR1);
|
||||
code_ok = sig == SIGUSR2 && info->si_signo == SIGUSR2 && info->si_code == SI_TKILL && info->si_pid == getpid();
|
||||
stack_ok = uc->uc_stack.ss_sp == altstack && uc->uc_stack.ss_size == sizeof(altstack) && !(uc->uc_stack.ss_flags & SS_DISABLE);
|
||||
if (mode == 1) {
|
||||
struct fpsimd_context *fp = fpsimd_of(uc);
|
||||
for (int i = 0; i <= 28; i++) uc->uc_mcontext.regs[i] = 0x1000 + i;
|
||||
uc->uc_mcontext.regs[29] = 0x2900;
|
||||
uc->uc_mcontext.regs[30] = 0x3000;
|
||||
uc->uc_mcontext.sp = (uint64_t)(landing_stack + sizeof(landing_stack));
|
||||
uc->uc_mcontext.pc = (uint64_t)landing;
|
||||
uc->uc_mcontext.pstate = 0x60000000;
|
||||
for (int i = 0; i < 32; i++) fp->vregs[i] = ((unsigned __int128)(0x7700 + i) << 64) | (0x5500 + i);
|
||||
fp->fpsr = 0x8;
|
||||
}
|
||||
}
|
||||
|
||||
static void usr1(int sig, siginfo_t *info, void *ctx) {
|
||||
(void)sig;
|
||||
(void)info;
|
||||
(void)ctx;
|
||||
}
|
||||
|
||||
uint64_t kept_x[10];
|
||||
double kept_d[8];
|
||||
|
||||
static int keep_callee_saved(void) {
|
||||
uint64_t *out = kept_x;
|
||||
double *d = kept_d;
|
||||
__asm__ volatile(
|
||||
"mov x19, #19\n mov x20, #20\n mov x21, #21\n mov x22, #22\n mov x23, #23\n"
|
||||
"mov x24, #24\n mov x25, #25\n mov x26, #26\n mov x27, #27\n mov x28, #28\n"
|
||||
"fmov d8, #1.0\n fmov d9, #2.0\n fmov d10, #3.0\n fmov d11, #4.0\n"
|
||||
"fmov d12, #5.0\n fmov d13, #6.0\n fmov d14, #7.0\n fmov d15, #8.0\n"
|
||||
"bl slowpath\n"
|
||||
"adrp x0, kept_x\n add x0, x0, :lo12:kept_x\n"
|
||||
"stp x19, x20, [x0, #0]\n stp x21, x22, [x0, #16]\n stp x23, x24, [x0, #32]\n"
|
||||
"stp x25, x26, [x0, #48]\n stp x27, x28, [x0, #64]\n"
|
||||
"adrp x0, kept_d\n add x0, x0, :lo12:kept_d\n"
|
||||
"stp d8, d9, [x0, #0]\n stp d10, d11, [x0, #16]\n stp d12, d13, [x0, #32]\n stp d14, d15, [x0, #48]\n"
|
||||
:
|
||||
:
|
||||
: "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
|
||||
"x16", "x17", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x30",
|
||||
"v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
|
||||
"v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30",
|
||||
"v31", "memory", "cc");
|
||||
for (int i = 0; i < 10; i++)
|
||||
if (out[i] != (uint64_t)(19 + i)) return 0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
if (d[i] != (double)(i + 1)) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void *stress(void *arg) {
|
||||
(void)arg;
|
||||
for (int i = 0; i < 2000; i++) slowpath();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
stack_t ss = {.ss_sp = altstack, .ss_size = sizeof(altstack), .ss_flags = 0};
|
||||
struct sigaction sa;
|
||||
sigset_t mask, now;
|
||||
sigaltstack(&ss, NULL);
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_sigaction = handler;
|
||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaddset(&sa.sa_mask, SIGUSR1);
|
||||
sigaction(SIGUSR2, &sa, NULL);
|
||||
sa.sa_sigaction = usr1;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
|
||||
if (argc > 1 && strcmp(argv[1], "bench") == 0) {
|
||||
int n = argc > 2 ? atoi(argv[2]) : 20000;
|
||||
struct timespec a, b;
|
||||
clock_gettime(CLOCK_MONOTONIC, &a);
|
||||
for (int i = 0; i < n; i++) slowpath();
|
||||
clock_gettime(CLOCK_MONOTONIC, &b);
|
||||
printf("%.2f us per raise, alt %d\n", ((b.tv_sec - a.tv_sec) * 1e9 + (b.tv_nsec - a.tv_nsec)) / 1e3 / n, on_alt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
mode = 0;
|
||||
calls = 0;
|
||||
int kept = keep_callee_saved();
|
||||
pthread_sigmask(SIG_BLOCK, NULL, &now);
|
||||
printf("returned calls=%d kept=%d usr2_blocked=%d usr1_blocked=%d code=%d stack=%d after_usr2=%d after_usr1=%d\n",
|
||||
calls, kept, usr2_blocked, usr1_blocked, code_ok, stack_ok, sigismember(&now, SIGUSR2),
|
||||
sigismember(&now, SIGUSR1));
|
||||
printf("path %s\n", on_alt ? "kernel" : "inline");
|
||||
|
||||
mode = 1;
|
||||
calls = 0;
|
||||
if (!setjmp(back)) {
|
||||
slowpath();
|
||||
printf("redirect did not happen\n");
|
||||
} else {
|
||||
int ok = 1;
|
||||
for (int i = 0; i <= 28; i++)
|
||||
if (saved[i] != (uint64_t)(0x1000 + i)) ok = 0;
|
||||
if (saved[29] != 0x2900 || saved[30] != 0x3000) ok = 0;
|
||||
for (int i = 0; i < 32; i++)
|
||||
if (saved_v[i] != (((unsigned __int128)(0x7700 + i) << 64) | (0x5500 + i))) ok = 0;
|
||||
printf("redirect calls=%d regs=%d fpsr=%u\n", calls, ok, saved_fpsr & 0x8);
|
||||
}
|
||||
mode = 0;
|
||||
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGUSR2);
|
||||
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
||||
calls = 0;
|
||||
slowpath();
|
||||
int while_blocked = calls;
|
||||
pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
|
||||
printf("blocked during=%d after=%d\n", while_blocked, calls);
|
||||
|
||||
calls = 0;
|
||||
slowpath_other(SIGUSR1);
|
||||
raise(SIGUSR2);
|
||||
printf("other calls=%d main_path=%s\n", calls, on_alt ? "kernel" : "inline");
|
||||
|
||||
calls = 0;
|
||||
pthread_t t[4];
|
||||
for (int i = 0; i < 4; i++) pthread_create(&t[i], NULL, stress, NULL);
|
||||
for (int i = 0; i < 4; i++) pthread_join(t[i], NULL);
|
||||
printf("threads calls=%d\n", calls);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def has_gcs_hwcap():
|
||||
try:
|
||||
getauxval = ctypes.CDLL(None).getauxval
|
||||
except (AttributeError, OSError):
|
||||
return False
|
||||
getauxval.restype = ctypes.c_ulong
|
||||
getauxval.argtypes = [ctypes.c_ulong]
|
||||
return bool(getauxval(16) & (1 << 32))
|
||||
|
||||
|
||||
def runner():
|
||||
if platform.machine() in ("aarch64", "arm64") and shutil.which("gcc") and not has_gcs_hwcap():
|
||||
return "gcc", []
|
||||
if shutil.which("aarch64-linux-gnu-gcc") and shutil.which("qemu-aarch64") and Path("/usr/aarch64-linux-gnu").is_dir():
|
||||
return "aarch64-linux-gnu-gcc", ["qemu-aarch64", "-cpu", "neoverse-n1", "-L", "/usr/aarch64-linux-gnu"]
|
||||
return None, None
|
||||
|
||||
|
||||
class Usr2PreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.cc, cls.prefix = runner()
|
||||
if cls.cc is None:
|
||||
raise unittest.SkipTest("no aarch64 compiler and runner")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
(tmp / "ntdll.c").write_text(NTDLL)
|
||||
(tmp / "prog.c").write_text(PROGRAM)
|
||||
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
|
||||
subprocess.run([cls.cc, "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"),
|
||||
*sources, "-ldl"], check=True)
|
||||
subprocess.run([cls.cc, "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True)
|
||||
subprocess.run([cls.cc, "-O1", "-U_FORTIFY_SOURCE", "-pthread", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"),
|
||||
"-Wl,-rpath," + str(tmp)], check=True)
|
||||
cls.program = tmp / "prog"
|
||||
cls.preload = tmp / "libblsession.so"
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def run_program(self, preload):
|
||||
env = dict(os.environ)
|
||||
env.pop("LD_PRELOAD", None)
|
||||
cmd = list(self.prefix) + [str(self.program)]
|
||||
if preload:
|
||||
if self.prefix:
|
||||
cmd = list(self.prefix) + ["-E", "LD_PRELOAD=" + str(self.preload), str(self.program)]
|
||||
else:
|
||||
env["LD_PRELOAD"] = str(self.preload)
|
||||
out = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=120)
|
||||
self.assertEqual(out.returncode, 0, out.stderr)
|
||||
return out.stdout.splitlines()
|
||||
|
||||
def test_a_ntdll_raise_runs_the_handler_without_a_kernel_signal(self):
|
||||
lines = self.run_program(True)
|
||||
self.assertIn("path inline", lines)
|
||||
self.assertIn("returned calls=1 kept=1 usr2_blocked=1 usr1_blocked=1 code=1 stack=1 after_usr2=0 after_usr1=0", lines)
|
||||
|
||||
def test_b_context_the_handler_writes_is_the_one_that_resumes(self):
|
||||
self.assertIn("redirect calls=1 regs=1 fpsr=8", self.run_program(True))
|
||||
|
||||
def test_c_behaves_like_the_kernel_signal(self):
|
||||
stock = [line for line in self.run_program(False) if not line.startswith("path ")]
|
||||
inline = [line for line in self.run_program(True) if not line.startswith("path ")]
|
||||
self.assertIn("returned calls=1 kept=1 usr2_blocked=1 usr1_blocked=1 code=1 stack=1 after_usr2=0 after_usr1=0", stock)
|
||||
self.assertEqual(stock, inline)
|
||||
|
||||
def test_d_blocked_other_signals_and_other_callers_use_the_kernel(self):
|
||||
lines = self.run_program(True)
|
||||
self.assertIn("blocked during=0 after=1", lines)
|
||||
self.assertIn("other calls=1 main_path=kernel", lines)
|
||||
self.assertIn("threads calls=8000", lines)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -31,14 +31,14 @@ class XboxBuildSourceTest(unittest.TestCase):
|
||||
git('switch', '-q', 'main')
|
||||
(root / 'backend').write_text('merged backend')
|
||||
git('commit', '-qam', 'backend changed after branch')
|
||||
for name in ('xbox', 'apk'):
|
||||
for name in ('source', 'xbox', 'components', 'apk', 'tests', 'dispatch'):
|
||||
git('clone', '-q', str(root), str(root / name))
|
||||
git('checkout', '-q', head, cwd=root / name)
|
||||
output = root / 'output'
|
||||
env['GITHUB_OUTPUT'] = str(output)
|
||||
subprocess.run(['bash', '-c', step('xbox', 'Pin and merge the APK source')], cwd=root / 'xbox', env=env, check=True, capture_output=True)
|
||||
subprocess.run(['bash', '-c', step('source', 'Pin and merge the APK source')], cwd=root / 'source', env=env, check=True, capture_output=True)
|
||||
base = output.read_text().strip().split('=', 1)[1]
|
||||
tree = git('rev-parse', 'HEAD^{tree}', cwd=root / 'xbox')
|
||||
tree = git('rev-parse', 'HEAD^{tree}', cwd=root / 'source')
|
||||
(root / 'backend').write_text('newer main, after backend build')
|
||||
git('commit', '-qam', 'main advanced again')
|
||||
env.update(BUILD_BASE_SHA=base, XBOX_SOURCE_TREE=tree)
|
||||
@@ -46,3 +46,19 @@ class XboxBuildSourceTest(unittest.TestCase):
|
||||
subprocess.run(['bash', '-c', step('build', 'Verify backend and APK source match')], cwd=root / 'apk', env=env, check=True, capture_output=True)
|
||||
self.assertEqual((root / 'apk/backend').read_text(), 'merged backend')
|
||||
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / 'apk'), tree)
|
||||
|
||||
subprocess.run(['bash', '-c', step('python-tests', 'Merge the pinned APK base')], cwd=root / 'tests', env=env, check=True, capture_output=True)
|
||||
subprocess.run(['bash', '-c', step('python-tests', 'Verify backend and test source match')], cwd=root / 'tests', env=env, check=True, capture_output=True)
|
||||
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / 'tests'), tree)
|
||||
|
||||
env['SOURCE_TREE'] = tree
|
||||
for job in ('xbox', 'components'):
|
||||
subprocess.run(['bash', '-c', step(job, 'Merge the pinned APK base')], cwd=root / job, env=env, check=True, capture_output=True)
|
||||
subprocess.run(['bash', '-c', step(job, 'Verify pinned source tree')], cwd=root / job, env=env, check=True, capture_output=True)
|
||||
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / job), tree)
|
||||
|
||||
trusted = root / 'trusted-output'
|
||||
env['GITHUB_OUTPUT'] = str(trusted)
|
||||
subprocess.run(['bash', '-c', step('source', 'Resolve trusted manifest base')], cwd=root / 'dispatch', env=env, check=True, capture_output=True)
|
||||
self.assertEqual(trusted.read_text().strip(), 'base-sha=' + git('rev-parse', 'main'))
|
||||
self.assertNotEqual(git('rev-parse', 'HEAD', cwd=root / 'dispatch'), git('rev-parse', 'main'))
|
||||
Reference in new issue
Block a user