Skip to content

Marshal sched_getaffinity masks across process memory - #884

Closed
brandonpayton wants to merge 1 commit into
mainfrom
fix/sched-getaffinity-marshalling
Closed

Marshal sched_getaffinity masks across process memory#884
brandonpayton wants to merge 1 commit into
mainfrom
fix/sched-getaffinity-marshalling

Conversation

@brandonpayton

@brandonpayton brandonpayton commented Jul 11, 2026

Copy link
Copy Markdown
Member

Purpose

Make sched_getaffinity(2) return its CPU mask through the normal guest/host/kernel memory contract instead of dereferencing a guest address in kernel memory.

This surfaced while validating the Kandelo Homebrew xz formula: liblzma probes the available CPU count before using its multithreaded encoder.

Root Cause

Syscall 238 was implemented in the Rust kernel, but absent from the shared host syscall-argument descriptors. The host therefore forwarded the process-memory pointer unchanged. The kernel interpreted that address in its own Wasm memory when constructing the output slice, trapped out of bounds, and the host converted the trap to EIO.

The change:

  • declares SchedGetaffinity in the shared syscall inventory;
  • marshals argument 2 as an output buffer sized by argument 1;
  • returns the written byte count from the raw syscall, as musl's wrapper requires before it can preserve the mask;
  • adds descriptor, generated-ABI, runtime, and libc fixture coverage.

ABI

The branch is rebased unchanged onto ABI 18 at 02e63bc46; its patch ID remains ee5f0f65460ab68429029f020def965b93fc4dac.

scripts/check-abi-version.sh classifies the two snapshot entries as additive-compatible:

  • syscall-argument descriptor 238 added;
  • syscall number 238 named.

Existing ABI entries are unchanged, so ABI_VERSION remains 18. The ABI snapshot and generated TypeScript bindings are committed in sync.

Validation

Exact ABI 18 rebase validation through scripts/dev-shell.sh:

  • bash scripts/check-abi-version.sh: passed; snapshot, libc header, and TypeScript bindings are in sync;
  • focused shared descriptor test sched_getaffinity_marshals_the_requested_cpuset: passed;
  • git diff --check: passed;
  • original versus rebased patch ID: identical.

The pre-rebase branch also passed kernel/shared tests, generated ABI and focused host tests, Node and Chromium runtime fixtures, POSIX, libc-test, and Sortix validation as recorded in the earlier PR run. CI is rebuilding the rebased branch against ABI 18.

cargo fmt is not available in the repository dev shell; the Rust changes retain the surrounding formatter style.

This main-repository PR is intentionally left for Brandon's review and must not be auto-merged.

@brandonpayton
brandonpayton force-pushed the fix/sched-getaffinity-marshalling branch from 8fa793e to 02e63bc Compare July 11, 2026 21:30
@brandonpayton

brandonpayton commented Jul 11, 2026

Copy link
Copy Markdown
Member Author

CI note: the failed Vitest job was rerun and reproduced only the same setup-timing assertion at host/test/teardown-reclaim.test.ts:71; every other suite remains green. The race is root-caused and fixed independently in #889, whose complete kernel/fork/Vitest/browser/libc/POSIX/Sortix gate is green. The unchanged pre-#889 merge base cannot make that test deterministic, so further reruns would not add evidence; merge or rebase after #889 is the correct path.

brandonpayton added a commit that referenced this pull request Jul 13, 2026
Implement sched_getaffinity as a Linux-compatible one-CPU surface with fixed four-byte raw masks, exact live-task lookup, Linux errno ordering, and unsigned-u32 length normalization across wasm32 and wasm64 callers.

Route the output through the generated host ABI for both Node and browser workers. Cover raw versus musl return behavior, large and precision-sensitive lengths, missing and dead tasks, live pthread TIDs, and browser execution.

Advance ABI 37 to 38 and regenerate the snapshot, C header, and TypeScript bindings atomically.

Source-PR: #884
@brandonpayton

Copy link
Copy Markdown
Member Author

Merged PR #907 supersedes this work through the strengthened affinity marshalling implementation now on main at 74339d68f. This head was not merged verbatim; the behavior was batch-validated.

@brandonpayton
brandonpayton deleted the fix/sched-getaffinity-marshalling branch July 13, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant