[llvm] [CAS] Give Windows file mappings names to better ensure same mappings are used (PR #190692)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 14:54:36 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Hiroshi Yamauchi (hjyamauchi)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/190692.diff
4 Files Affected:
- (modified) llvm/include/llvm/Support/FileSystem.h (+3-2)
- (modified) llvm/lib/CAS/MappedFileRegionArena.cpp (+10-1)
- (modified) llvm/lib/Support/Unix/Path.inc (+4-3)
- (modified) llvm/lib/Support/Windows/Path.inc (+11-4)
``````````diff
diff --git a/llvm/include/llvm/Support/FileSystem.h b/llvm/include/llvm/Support/FileSystem.h
index 38b801dffeb29..320daf56fd1f6 100644
--- a/llvm/include/llvm/Support/FileSystem.h
+++ b/llvm/include/llvm/Support/FileSystem.h
@@ -1335,7 +1335,7 @@ class mapped_file_region {
LLVM_ABI void willNeedImpl();
LLVM_ABI std::error_code init(sys::fs::file_t FD, uint64_t Offset,
- mapmode Mode);
+ mapmode Mode, const char* Name);
public:
mapped_file_region() = default;
@@ -1351,7 +1351,8 @@ class mapped_file_region {
/// \param fd An open file descriptor to map. Does not take ownership of fd.
LLVM_ABI mapped_file_region(sys::fs::file_t fd, mapmode mode, size_t length,
- uint64_t offset, std::error_code &ec);
+ uint64_t offset, std::error_code &ec,
+ const char* name = nullptr);
~mapped_file_region() { unmapImpl(); }
diff --git a/llvm/lib/CAS/MappedFileRegionArena.cpp b/llvm/lib/CAS/MappedFileRegionArena.cpp
index 2dae3f2d05116..5712edaa629b0 100644
--- a/llvm/lib/CAS/MappedFileRegionArena.cpp
+++ b/llvm/lib/CAS/MappedFileRegionArena.cpp
@@ -252,8 +252,17 @@ Expected<MappedFileRegionArena> MappedFileRegionArena::create(
// Create the mapped region.
{
std::error_code EC;
+ const char *Name = nullptr;
+#ifdef _WIN32
+ // Give the file mapping a name to ensure the same mappings are
+ // shared across processes.
+ std::string MapName = Result.Path;
+ std::replace(MapName.begin(), MapName.end(), '\\', '/');
+ MapName = "Local\\" + MapName;
+ Name = MapName.c_str();
+#endif
sys::fs::mapped_file_region Map(
- File, sys::fs::mapped_file_region::readwrite, Capacity, 0, EC);
+ File, sys::fs::mapped_file_region::readwrite, Capacity, 0, EC, Name);
if (EC)
return createFileError(Result.Path, EC);
Result.Region = std::move(Map);
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 057276f9b7eb2..aaa11a7b5e9f1 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -861,7 +861,7 @@ std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
}
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
- mapmode Mode) {
+ mapmode Mode, const char* Name) {
assert(Size != 0);
int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
@@ -896,12 +896,13 @@ std::error_code mapped_file_region::init(int FD, uint64_t Offset,
}
mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
- uint64_t offset, std::error_code &ec)
+ uint64_t offset, std::error_code &ec,
+ const char* name)
: Size(length), Mode(mode) {
sandbox::violationIfEnabled();
(void)Mode;
- ec = init(fd, offset, mode);
+ ec = init(fd, offset, mode, name);
if (ec)
copyFrom(mapped_file_region());
}
diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 48825a2de4993..5f54744fd2046 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -1030,7 +1030,8 @@ std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
}
std::error_code mapped_file_region::init(sys::fs::file_t OrigFileHandle,
- uint64_t Offset, mapmode Mode) {
+ uint64_t Offset, mapmode Mode,
+ const char* Name) {
this->Mode = Mode;
if (OrigFileHandle == INVALID_HANDLE_VALUE)
return make_error_code(errc::bad_file_descriptor);
@@ -1048,8 +1049,14 @@ std::error_code mapped_file_region::init(sys::fs::file_t OrigFileHandle,
break;
}
+ SmallVector<wchar_t, 128> NameUTF16;
+ if (Name)
+ if (std::error_code EC = UTF8ToUTF16(Name, NameUTF16))
+ return EC;
+
HANDLE FileMappingHandle = ::CreateFileMappingW(OrigFileHandle, 0, flprotect,
- Hi_32(Size), Lo_32(Size), 0);
+ Hi_32(Size), Lo_32(Size),
+ Name ? c_str(NameUTF16) : 0);
if (FileMappingHandle == NULL) {
std::error_code ec = mapWindowsError(GetLastError());
return ec;
@@ -1106,11 +1113,11 @@ std::error_code mapped_file_region::init(sys::fs::file_t OrigFileHandle,
mapped_file_region::mapped_file_region(sys::fs::file_t fd, mapmode mode,
size_t length, uint64_t offset,
- std::error_code &ec)
+ std::error_code &ec, const char* name)
: Size(length) {
sandbox::violationIfEnabled();
- ec = init(fd, offset, mode);
+ ec = init(fd, offset, mode, name);
if (ec)
copyFrom(mapped_file_region());
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/190692
More information about the llvm-commits
mailing list