tests/sims/syscalls.test.ts
The automated tests for the /simulators/syscall-journey simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- The 0x7ffff000 limit and the syscall numbers from arch/x86/entry/syscalls/syscall_64.tbl.
- Call counts follow from the file and buffer sizes.
- Arguments go in rdi, rsi, rdx, r10, r8, r9 with the number in rax.
// /simulators/syscall-journey: the system calls of a file copy on Linux x86-64.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { plan, registers, NR, MAX_RW } from '../../src/lib/sims/syscalls.ts';
test('the 0x7ffff000 limit and the syscall numbers from arch/x86/entry/syscalls/syscall_64.tbl', () => {
assert.equal(MAX_RW, 2147479552);
assert.deepEqual(NR, { read: 0, write: 1, close: 3, mmap: 9, munmap: 11, sendfile: 40, exit_group: 231, openat: 257, newfstatat: 262 });
});
test('call counts follow from the file and buffer sizes', () => {
const fixed = 3 /* openat, newfstatat, openat */ + 2 /* close × 2 */ + 1 /* exit_group */;
for (const [size, buf] of [[1048576, 131072], [1000000, 4096], [10, 1], [0, 4096], [5e9, 1 << 20], [7, 8]]) {
const p = plan('rw', size, buf);
const chunks = Math.ceil(size / buf);
assert.equal(p.counts.read, chunks + 1, `read ${size}/${buf}`); // one more read returns 0 at end of file
assert.equal(p.counts.write ?? 0, chunks, `write ${size}/${buf}`);
assert.equal(p.total, fixed + 2 * chunks + 1);
assert.equal(p.toUser, size);
assert.equal(p.fromUser, size);
}
for (const size of [1048576, 5e9, MAX_RW, MAX_RW + 1]) {
const s = plan('sendfile', size, 0);
assert.equal(s.counts.sendfile, Math.ceil(size / MAX_RW), `sendfile ${size}`);
assert.equal(s.toUser + s.fromUser, 0);
const m = plan('mmap', size, 0);
assert.equal(m.counts.write, Math.ceil(size / MAX_RW), `mmap writes ${size}`);
assert.equal(m.counts.mmap, 1);
assert.equal(m.counts.munmap, 1);
}
assert.equal(plan('mmap', 0, 0).counts.mmap, undefined); // mmap of length 0 fails with EINVAL, so a copy skips it
});
test('arguments go in rdi, rsi, rdx, r10, r8, r9 with the number in rax', () => {
const p = plan('mmap', 4096, 0);
const call = p.calls.find((c) => c.name === 'mmap')!;
const regs = registers(call);
assert.deepEqual(regs.map(([r]) => r), ['rax', 'rdi', 'rsi', 'rdx', 'r10', 'r8', 'r9']);
assert.equal(regs[0][1], '9 (mmap)');
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.