[llvm-branch-commits] [llvm] release/21.x: Work around documented Linux mmap bug. (#152595) (PR #153486)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Aug 13 13:19:47 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: None (llvmbot)
<details>
<summary>Changes</summary>
Backport 85cd3d9
Requested by: @<!-- -->zygoloid
---
Full diff: https://github.com/llvm/llvm-project/pull/153486.diff
1 Files Affected:
- (modified) llvm/lib/Support/MemoryBuffer.cpp (+8-2)
``````````diff
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 601f11f6d23c8..1c4645ad83641 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -501,8 +501,14 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
std::unique_ptr<MB> Result(
new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
RequiresNullTerminator, FD, MapSize, Offset, EC));
- if (!EC)
- return std::move(Result);
+ if (!EC) {
+ // On at least Linux, and possibly on other systems, mmap may return pages
+ // from the page cache that are not properly filled with trailing zeroes,
+ // if some prior user of the page wrote non-zero bytes. Detect this and
+ // don't use mmap in that case.
+ if (!RequiresNullTerminator || *Result->getBufferEnd() == '\0')
+ return std::move(Result);
+ }
}
#ifdef __MVS__
``````````
</details>
https://github.com/llvm/llvm-project/pull/153486
More information about the llvm-branch-commits
mailing list