Repository navigation
linux-user, fix: Keep emulated file mappings across MADV_DONTNEED - #515
Merged
Merged
Conversation
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]>
Contributor
Author
|
Thanks! |
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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_LOADsegments withp_align = 0x1000, soelf_load_exec_pagesize()selects 4K load granularity on a 16K host. For thewritable
PT_LOAD:0x5c88000is 16K aligned;p_offset - vaddr_po = 0x5a86000is 4K aligned but not 16Kaligned.
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 inlinux-user/mmap.cthat emulatesthe mapping with an anonymous
mmapplus apreadof the file contents.The recursive anonymous mapping marks the guest pages with the sticky
PAGE_ANONflag.PAGE_ANONis deliberately sticky (page_set_flags()andmprotect()preserve it), so the pages stay "anonymous" from LAT's point ofview 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.bunbytecode(including
chunk-gmq0qwep.js) to release pages, then re-reads them. On anative private file mapping,
MADV_DONTNEEDdiscards the pages and they arere-faulted from the file. LAT's 16K
MADV_DONTNEEDhandler instead seesPAGE_ANONand zero-fills the range, destroying the copied file contents.Bun's parser then reads NUL bytes and throws the
SyntaxErrorabove.Fix
In the anonymous fallback used for file-backed mappings
(
linux-user/mmap.c,target_mmap()), clear the stickyPAGE_ANONbit forthe guest range immediately after the recursive host mapping succeeds:
With
PAGE_ANONcleared,MADV_DONTNEEDon the emulated mapping is handledas 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 4Khosts are unaffected.
Validation / 验证
Regression test
Added
tests/integration/test-latx-16k-file-dontneed-x86_64.shandtests/integration/latx-16k-file-dontneed-x86_64.c, registered under theprocess domain.
The guest:
bytes for a 4K-aligned file offset;
MAP_PRIVATE | MAP_FIXEDat that address and at a4K-aligned but not host-page-aligned file offset, which forces the
anonymous fallback on a 16K host;
madvise(MADV_DONTNEED), and verifiesthe 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 buildlatxbuild/build64.sh -c -aand a--enable-testsbuild for the suites../build64/latx-x86_64 /home/zhaixiang/claudeprints the
SyntaxErrorand exits 1.claude --versionworks andclaude -p "..."returns answers.lat-pr-fast: 46/46 pass.latx-integration: 60 OK, 20 SKIP, 13 FAIL; the same 13 tests fail on thepristine build (AVX / prctl / aes environment issues), so the change
introduces no new failures. The new test and
test-latx-16k-page-permission-i386pass.Checklist / 检查项
CONTRIBUTING.md. / 我已阅读CONTRIBUTING.md。git commit -s). /每个提交都包含 DCO 签署(
git commit -s)。are not applicable. /
我已提供相关构建或测试结果,或说明了不适用的原因。