Skip to content

linux-user, fix: Keep emulated file mappings across MADV_DONTNEED - #515

Merged
LaurenIsACoder merged 1 commit into
lat-opensource:masterfrom
xiangzhai:non4kpage
Oct 16, 2026
Merged

LaurenIsACoder merged 1 commit into
lat-opensource:masterfrom
xiangzhai:non4kpage

Conversation

@xiangzhai

Copy link
Copy Markdown
Contributor

Summary / 变更说明

On a host with a larger page size than the guest (16K host emulating 4K
x86_64 pages), running the Bun-compiled Claude Code binary fails:

./build64/latx-x86_64 /home/zhaixiang/claude
SyntaxError: Invalid character: '\0'
    at <parse> (/$bunfs/root/chunk-gmq0qwep.js:11:1)
Bun v1.4.3 (Linux x64)

The same binary runs normally on a 4K-page kernel. The failure is reported as
a problem with LAT shadow-page emulation of 4K pages.

Root cause

The Claude Code x86_64 ELF has PT_LOAD segments with p_align = 0x1000, so
elf_load_exec_pagesize() selects 4K load granularity on a 16K host. For the
writable PT_LOAD:

  • virtual address 0x5c88000 is 16K aligned;
  • file offset p_offset - vaddr_po = 0x5a86000 is 4K aligned but not 16K
    aligned.

Because the file offset and the virtual address have different 16K-page
remainders, the kernel cannot map the file directly on a 16K host.
target_mmap() takes the fallback path in linux-user/mmap.c that emulates
the mapping with an anonymous mmap plus a pread of the file contents.

The recursive anonymous mapping marks the guest pages with the sticky
PAGE_ANON flag. PAGE_ANON is deliberately sticky (page_set_flags() and
mprotect() preserve it), so the pages stay "anonymous" from LAT's point of
view even though the guest mapping is file-backed.

Bun mmaps its own executable privately and calls
madvise(MADV_DONTNEED) over the region holding the embedded .bun bytecode
(including chunk-gmq0qwep.js) to release pages, then re-reads them. On a
native private file mapping, MADV_DONTNEED discards the pages and they are
re-faulted from the file. LAT's 16K MADV_DONTNEED handler instead sees
PAGE_ANON and zero-fills the range, destroying the copied file contents.
Bun's parser then reads NUL bytes and throws the SyntaxError above.

Fix

In the anonymous fallback used for file-backed mappings
(linux-user/mmap.c, target_mmap()), clear the sticky PAGE_ANON bit for
the guest range immediately after the recursive host mapping succeeds:

/*
 * The guest mapping is file-backed even though the host mapping
 * had to be anonymous because the file offset is not host-page
 * aligned.  Clear the sticky PAGE_ANON bit so that MADV_DONTNEED
 * is not treated as discarding anonymous memory: the copied file
 * contents must not be lost.
 */
pageflags_set_clear(start, start + len - 1, 0, PAGE_ANON);

With PAGE_ANON cleared, MADV_DONTNEED on the emulated mapping is handled
as a file mapping (LAT ignores it and returns success, matching the existing
"this is a hint" policy) instead of zeroing the copied contents.

The fallback only runs when qemu_host_page_size > TARGET_PAGE_SIZE, so 4K
hosts are unaffected.

Validation / 验证

Regression test

Added tests/integration/test-latx-16k-file-dontneed-x86_64.sh and
tests/integration/latx-16k-file-dontneed-x86_64.c, registered under the
process domain.

The guest:

  1. creates a file, fills it with a known pattern, and reads back the expected
    bytes for a 4K-aligned file offset;
  2. reserves an address range and picks a host-page-aligned address in it;
  3. maps the file MAP_PRIVATE | MAP_FIXED at that address and at a
    4K-aligned but not host-page-aligned file offset, which forces the
    anonymous fallback on a 16K host;
  4. verifies the mapped contents, calls madvise(MADV_DONTNEED), and verifies
    the contents again.

The test skips when the host page size is not 16384 or when clang/LLD is
unavailable. It exits 21 without the fix ("MADV_DONTNEED discarded the
emulated file contents") and 0 with it.

Evidence

Environment: LoongArch host, kernel 7.1.13-aosc-main-16k, PAGE_SIZE=16384;
sources at d768edc362; release build latxbuild/build64.sh -c -a and a
--enable-tests build for the suites.

  • RED (unfixed): pty run of ./build64/latx-x86_64 /home/zhaixiang/claude
    prints the SyntaxError and exits 1.
  • GREEN (fixed): the interactive TUI reaches the folder-trust prompt;
    claude --version works and claude -p "..." returns answers.
  • Focused test: exits 21 before the fix, 0 after.
  • lat-pr-fast: 46/46 pass.
  • latx-integration: 60 OK, 20 SKIP, 13 FAIL; the same 13 tests fail on the
    pristine build (AVX / prctl / aes environment issues), so the change
    introduces no new failures. The new test and
    test-latx-16k-page-permission-i386 pass.

Checklist / 检查项

  • I have read CONTRIBUTING.md. / 我已阅读 CONTRIBUTING.md。
  • Every commit contains a DCO sign-off (git commit -s). /
    每个提交都包含 DCO 签署(git commit -s)。
  • I have included relevant build or test results, or explained why they
    are not applicable. /
    我已提供相关构建或测试结果,或说明了不适用的原因。

On hosts whose page size is larger than the guest page size, a private
file mapping whose file offset is not host-page aligned cannot be mapped
file-backed directly.  target_mmap() emulates it with an anonymous host
mapping filled by pread, and the recursive mapping marks the guest pages
with the sticky PAGE_ANON flag.

The Bun-compiled Claude Code binary mmaps its own executable, calls
madvise(MADV_DONTNEED) over the region holding its embedded bytecode, and
re-reads it.  A native private file mapping reloads those pages from the
file, but the 16K MADV_DONTNEED handler saw PAGE_ANON and zero-filled the
range, discarding the copied file contents.  Bun then parsed NUL bytes and
failed with "SyntaxError: Invalid character: '\0'" at
/$bunfs/root/chunk-gmq0qwep.js:11:1.

Clear the sticky PAGE_ANON bit for the emulated file mapping so that
MADV_DONTNEED is handled as a file mapping and the copied contents are
preserved.  The fallback only runs when the host page is larger than the
guest page, so 4K hosts are unaffected.

Add test-latx-16k-file-dontneed-x86_64: a static x86_64 guest maps a file
privately at a 4K-aligned but not host-page-aligned offset, verifies the
contents, calls MADV_DONTNEED, and verifies them again.

Tests:
- Claude Code 2.1.299 on a 16K LoongArch host: interactive TUI starts,
  claude --version and claude -p work
- test-latx-16k-file-dontneed-x86_64: exits 21 before the fix, 0 after
- lat-pr-fast: 46/46
- latx-integration: no new failures versus the pristine build

Signed-off-by: Leslie Zhai <[email protected]>
@LaurenIsACoder
LaurenIsACoder merged commit 7acad1f into lat-opensource:master Oct 16, 2026
26 checks passed
@xiangzhai

Copy link
Copy Markdown
Contributor Author

Thanks!

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.

2 participants