mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
cpu-exec exists so that a binary needing an instruction-set feature the CPU does not have (AVX masked by a hypervisor - QEMU/KVM/Proxmox, the reported #6458 environment - an old CPU, or ARMv8.0 on a Raspberry Pi) runs through bundled qemu-user instead of dying instantly with SIGILL. With no features declared it is a plain exec, so it is safe and free to route EVERY binary through it - which is why the design is "route everything". Two platforms were not doing that: * Sandstorm SHIPPED cpu-exec and qemu-x86_64 (sandstorm-src/build-deps.sh) but the grain launcher spawned all eight bundled binaries directly - ferretdb, mongod 3.0 (twice), niscud, the legacy mongo CLI (twice) and node for the bridge and the importer. So the safety net was in the package and unreachable, on the platform with the LEAST control over its hardware: a grain runs on whatever CPU the Sandstorm host has. * The snap routed mongod (mongodb-control, migration-control) but not FerretDB (ferretdb-control, both the external-DB and SQLite launches) and not node (wekan-control: the main application start and both maintenance-page starts). Both now route through cpu-exec, each with a direct-exec fallback so an older deps image or snap revision still works. On Sandstorm the guard also requires /bin/bash, since cpu-exec is a bash script; without it the binary runs directly exactly as before. The snap's main start keeps its `ulimit -s 65500` - the limit is set before the exec and inherited across it (verified by expanding the line). Docker (releases/ferretdb/wekan-entrypoint.sh) and the bundle launcher (releases/ferretdb/start-wekan.sh) already routed ferretdb and node, and were checked for other unrouted launches - there are none. tests/cpuExecWiring.test.cjs previously asserted only that the Sandstorm build SHIPS cpu-exec, which is exactly how the gap survived: it now asserts that the grain launcher routes each of the six bundled binaries, that no bundled binary is spawned directly any more (/bin/sleep is a grain utility, not bundled, so it is exempt), and that the snap routes FerretDB and node with their fallbacks intact. Thanks to xet7 !
151 lines
8.0 KiB
JavaScript
151 lines
8.0 KiB
JavaScript
'use strict';
|
|
|
|
// Plain-Node regression guard (no Meteor) for the #6458 cpu-exec WIRING: the
|
|
// general qemu-user fallback is only useful if every platform actually ships
|
|
// it and every launcher actually routes binaries through it. The cpu-exec
|
|
// script's own BEHAVIOR is tested in tests/cpuExec.test.cjs; this test pins
|
|
// the delivery pipeline.
|
|
// Run: node tests/cpuExecWiring.test.cjs
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const repoRoot = path.resolve(__dirname, '..');
|
|
const read = rel => fs.readFileSync(path.join(repoRoot, rel), 'utf8');
|
|
|
|
const releaseAll = read('.github/workflows/release-all.yml');
|
|
const sandstormYml = read('.github/workflows/sandstorm.yml');
|
|
const buildDeps = read('sandstorm-src/build-deps.sh');
|
|
const entrypoint = read('releases/ferretdb/wekan-entrypoint.sh');
|
|
const startWekan = read('releases/ferretdb/start-wekan.sh');
|
|
const snapcraft = read('snapcraft.yaml');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
// --- release-all.yml: every Linux bundle ships cpu-exec + its own qemu -------
|
|
|
|
test('amd64 bundle embeds cpu-exec and qemu-x86_64', () => {
|
|
assert.ok(releaseAll.includes('cp snap-src/bin/cpu-exec .build/bundle/cpu-exec'));
|
|
assert.ok(releaseAll.includes('cp /usr/bin/qemu-x86_64-static .build/bundle/qemu-x86_64'));
|
|
});
|
|
|
|
test('arm64 bundle replaces the inherited amd64 qemu with qemu-aarch64', () => {
|
|
assert.ok(/rm -f bundle\/qemu-x86_64\s*\n\s*cp \/usr\/bin\/qemu-aarch64-static bundle\/qemu-aarch64/.test(releaseAll));
|
|
});
|
|
|
|
test('extra arches (ppc64le/s390x/riscv64) bundle their own-arch qemu, tolerantly', () => {
|
|
assert.ok(releaseAll.includes('rm -f /bundle/qemu-x86_64'));
|
|
assert.ok(/apt-get install -y -q qemu-user-static[\s\S]*?\/usr\/bin\/qemu-\$\(uname -m\)-static/.test(releaseAll));
|
|
assert.ok(releaseAll.includes('bundle ships without a bundled qemu-user'),
|
|
'a missing qemu package on an exotic arch must not fail the release');
|
|
});
|
|
|
|
test('negative: Windows and macOS bundles strip the Linux-only cpu-exec + qemu', () => {
|
|
const strips = releaseAll.match(/rm -f bundle\/cpu-exec bundle\/qemu-x86_64/g) || [];
|
|
assert.strictEqual(strips.length, 2, 'one strip in build-win64, one in build-mac-arm64');
|
|
});
|
|
|
|
test('the qemu-user-static build dependency is installed where bundles are built', () => {
|
|
// amd64 + arm64 bundle jobs, and both sandstorm jobs (release-all + standalone)
|
|
const aptLines = releaseAll.match(/apt-get install[^\n]*qemu-user-static/g) || [];
|
|
assert.ok(aptLines.length >= 3, `found ${aptLines.length} apt lines with qemu-user-static in release-all.yml`);
|
|
assert.ok(/apt-get install[^\n]*qemu-user-static/.test(sandstormYml));
|
|
});
|
|
|
|
// --- Sandstorm .spk ------------------------------------------------------------
|
|
|
|
test('sandstorm build-deps ships cpu-exec and (tolerantly) qemu-x86_64', () => {
|
|
assert.ok(buildDeps.includes('cp -f "$REPO/snap-src/bin/cpu-exec" "$DEPS/bin/cpu-exec"'));
|
|
assert.ok(/if \[ -x \/usr\/bin\/qemu-x86_64-static \]; then\s*\n\s*cp -f \/usr\/bin\/qemu-x86_64-static "\$DEPS\/bin\/qemu-x86_64"/.test(buildDeps));
|
|
assert.ok(buildDeps.includes('spk ships cpu-exec without a bundled qemu-user'),
|
|
'missing qemu on the build host must not fail the spk build');
|
|
});
|
|
|
|
// Shipping cpu-exec is not enough - the grain launcher must actually USE it.
|
|
// It shipped it and spawned every binary directly, so a grain on a CPU without a
|
|
// needed feature died with SIGILL and the bundled qemu-user was never reached.
|
|
test('the sandstorm grain launcher routes every bundled binary through cpu-exec', () => {
|
|
const start = read('sandstorm-src/start.js');
|
|
assert.ok(/const CPU_EXEC = path\.join\(APPROOT, 'bin\/cpu-exec'\)/.test(start),
|
|
'the launcher locates cpu-exec in the package');
|
|
assert.ok(/function cpuExec\(bin, args\)/.test(start), 'has the wrap helper');
|
|
// cpu-exec is a bash script: both it and bash must be executable, else run direct.
|
|
assert.ok(/fs\.accessSync\('\/bin\/bash', fs\.constants\.X_OK\)/.test(start),
|
|
'falls back when bash is missing');
|
|
assert.ok(/HAVE_CPU_EXEC \? \[CPU_EXEC, \[bin, \.\.\.args\]\] : \[bin, args\]/.test(start),
|
|
'direct-exec fallback keeps older deps images working');
|
|
// Every bundled binary the launcher starts must go through the helper.
|
|
for (const bin of ['NODE, [BRIDGE]', 'FERRETDB, args', 'MONGO_CLI', 'NISCUD', 'MONGOD3',
|
|
'NODE, [IMPORTER]']) {
|
|
assert.ok(start.includes(`cpuExec(${bin}`), `not routed through cpu-exec: ${bin}`);
|
|
}
|
|
// ...and nothing bundled is still spawned directly. /bin/sleep is a grain
|
|
// utility, not a bundled binary, so it is exempt.
|
|
const direct = start.match(/spawn(?:Sync)?\((?!\.\.\.cpuExec|'\/bin\/sleep')/g) || [];
|
|
assert.deepStrictEqual(direct, [], 'a bundled binary is still spawned directly');
|
|
});
|
|
|
|
// --- Snap: FerretDB + node, not just mongod --------------------------------------
|
|
|
|
test('the snap runs FerretDB through cpu-exec', () => {
|
|
const ferretdbControl = read('snap-src/bin/ferretdb-control');
|
|
assert.ok(/CPU_EXEC="\$SNAP\/bin\/cpu-exec"/.test(ferretdbControl));
|
|
assert.ok(/\[ -x "\$CPU_EXEC" \] \|\| CPU_EXEC=""/.test(ferretdbControl),
|
|
'missing cpu-exec must fall back to a direct exec');
|
|
const routed = ferretdbControl.match(/exec \$\{CPU_EXEC:\+bash "\$CPU_EXEC"\} "\$FERRETDB_BIN"/g) || [];
|
|
assert.strictEqual(routed.length, 2, 'both the external-DB and SQLite launches are routed');
|
|
});
|
|
|
|
test('the snap runs node (the app and the maintenance page) through cpu-exec', () => {
|
|
const wekanControl = read('snap-src/bin/wekan-control');
|
|
assert.ok(/CPU_EXEC="\$SNAP\/bin\/cpu-exec"/.test(wekanControl));
|
|
assert.ok(/\[ -x "\$CPU_EXEC" \] \|\| CPU_EXEC=""/.test(wekanControl));
|
|
// The main application start, which keeps its ulimit -s 65500.
|
|
assert.ok(/ulimit -s 65500; exec \$\{CPU_EXEC:\+bash \\"\$CPU_EXEC\\"\} \$NODE_PATH\/node \$APPLICATION_START/
|
|
.test(wekanControl), 'the main app start is routed and keeps its stack ulimit');
|
|
const pages = wekanControl.match(/\$\{CPU_EXEC:\+bash "\$CPU_EXEC"\} "\$SNAP\/bin\/node"/g) || [];
|
|
assert.strictEqual(pages.length, 2, 'both maintenance-page launches are routed');
|
|
});
|
|
|
|
// --- Docker entrypoint ----------------------------------------------------------
|
|
|
|
test('Docker entrypoint routes ferretdb and node through cpu-exec when present', () => {
|
|
assert.ok(/if \[ -x \/build\/cpu-exec \]; then\s*\n\s*\/build\/cpu-exec "\$FERRETDB_BIN"/.test(entrypoint));
|
|
assert.ok(/if \[ -x \/build\/cpu-exec \]; then\s*\n\s*exec \/build\/cpu-exec node \/build\/main\.js/.test(entrypoint));
|
|
});
|
|
|
|
test('negative: Docker entrypoint still works without cpu-exec (older bundles)', () => {
|
|
// the guarded blocks must keep direct-exec fallbacks
|
|
assert.ok(/\nexec node \/build\/main\.js/.test(entrypoint), 'plain node fallback kept');
|
|
const direct = entrypoint.match(/"\$FERRETDB_BIN" \\\n/g) || [];
|
|
assert.ok(direct.length >= 1, 'plain ferretdb fallback kept');
|
|
});
|
|
|
|
// --- Bundle launcher -------------------------------------------------------------
|
|
|
|
test('bundle launcher routes ferretdb and node through cpu-exec when present', () => {
|
|
assert.ok(startWekan.includes('CPU_EXEC="$DIR/cpu-exec"'));
|
|
assert.ok(startWekan.includes('[ -x "$CPU_EXEC" ] || CPU_EXEC=""'),
|
|
'missing cpu-exec (e.g. macOS bundle) must fall back to direct execution');
|
|
assert.ok(/\$\{CPU_EXEC:\+"\$CPU_EXEC"\} "\$FERRETDB_BIN"/.test(startWekan));
|
|
assert.ok(/\$\{CPU_EXEC:\+"\$CPU_EXEC"\} "\$NODE" "\$DIR\/main\.js"/.test(startWekan));
|
|
});
|
|
|
|
// --- Snap -----------------------------------------------------------------------
|
|
|
|
test('the snap ships cpu-exec via the snap-src helpers part', () => {
|
|
// helpers part dumps snap-src -> $SNAP, so snap-src/bin/cpu-exec becomes
|
|
// $SNAP/bin/cpu-exec (which mongodb-control and migration-control invoke —
|
|
// pinned in tests/snapMigrationRecovery.test.cjs).
|
|
assert.ok(/helpers:\s*\n\s*source: snap-src\s*\n\s*plugin: dump/.test(snapcraft));
|
|
assert.ok(fs.existsSync(path.join(repoRoot, 'snap-src/bin/cpu-exec')));
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|