[clang] [clang] Report errors from writing the PCM instead of crashing (PR #243333)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 16 21:04:49 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-modules
Author: Jan Svoboda (jansvoboda11)
<details>
<summary>Changes</summary>
The `writeImpl()` function never checks the `raw_fd_ostream` for errors.
If writing the PCM fails (e.g. because the disk is full), the stream's
destructor reports a fatal error instead of `writeImpl()` returning the
error code to the caller. This PR checks the stream for errors, closing
it explicitly to also catch errors reported on close, and returns them.
---
Full diff: https://github.com/llvm/llvm-project/pull/243333.diff
2 Files Affected:
- (modified) clang/lib/Serialization/ModuleCache.cpp (+12)
- (modified) clang/unittests/Serialization/ModuleCacheTest.cpp (+102)
``````````diff
diff --git a/clang/lib/Serialization/ModuleCache.cpp b/clang/lib/Serialization/ModuleCache.cpp
index b360ac5f42f16..c7f643c768c90 100644
--- a/clang/lib/Serialization/ModuleCache.cpp
+++ b/clang/lib/Serialization/ModuleCache.cpp
@@ -192,6 +192,11 @@ clang::writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size,
OS << Buffer.getBuffer();
// Make sure all contents reach the file before querying its status.
OS.flush();
+ if ((EC = OS.error())) {
+ // Prevent the destructor from reporting a fatal error.
+ OS.clear_error();
+ return EC;
+ }
// Using the status from an open file descriptor ensures this is not racy.
if ((EC = llvm::sys::fs::status(FD, Status)))
return EC;
@@ -204,6 +209,13 @@ clang::writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size,
return Buf.getError();
Written = std::move(*Buf);
}
+ // Close explicitly, so that we can handle any errors ourselves.
+ OS.close();
+ if ((EC = OS.error())) {
+ // Prevent the destructor from reporting a fatal error.
+ OS.clear_error();
+ return EC;
+ }
}
Size = Status.getSize();
diff --git a/clang/unittests/Serialization/ModuleCacheTest.cpp b/clang/unittests/Serialization/ModuleCacheTest.cpp
index ef16e492b6b34..1915193f6c092 100644
--- a/clang/unittests/Serialization/ModuleCacheTest.cpp
+++ b/clang/unittests/Serialization/ModuleCacheTest.cpp
@@ -15,6 +15,7 @@
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/HeaderSearch.h"
+#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -23,6 +24,11 @@
#include "gtest/gtest.h"
+#ifndef _WIN32
+#include <csignal>
+#include <sys/resource.h>
+#endif
+
using namespace llvm;
using namespace clang;
@@ -367,4 +373,100 @@ TEST_F(ModuleCacheTest, WriteReturnsMappedBuffer) {
EXPECT_EQ(Written->getBuffer(), Contents);
}
+#ifndef _WIN32
+static std::error_code writeError(StringRef Path, StringRef Contents,
+ bool ReadBack = true) {
+ off_t Size;
+ time_t ModTime;
+ return writeImpl(Path, MemoryBufferRef(Contents, Path), Size, ModTime,
+ ReadBack)
+ .getError();
+}
+
+// Test that failing to create the temporary file for a reason other than a
+// missing parent directory is reported.
+TEST_F(ModuleCacheTest, WriteErrorCreateTempFile) {
+ // The parent "directory" is a regular file.
+ SmallString<256> RelPath(ModuleCacheDir);
+ sys::path::append(RelPath, "file");
+ addFile(RelPath, "");
+ SmallString<256> Path(TestDir);
+ sys::path::append(Path, RelPath, "M.pcm");
+
+ EXPECT_EQ(writeError(Path, "contents"), std::errc::not_a_directory);
+}
+
+// Test that failing to create the missing parent directory is reported.
+TEST_F(ModuleCacheTest, WriteErrorCreateDirectories) {
+ // Creating the temporary file in "dangling/sub" fails because "sub" doesn't
+ // exist. Creating "dangling/sub" then fails, because "dangling" is a dangling
+ // symlink and it's not possible to create a directory inside of it.
+ SmallString<256> Dangling(ModuleCachePath);
+ sys::path::append(Dangling, "dangling");
+ ASSERT_FALSE(sys::fs::create_link("does-not-exist", Dangling));
+ SmallString<256> Path(Dangling);
+ sys::path::append(Path, "sub", "M.pcm");
+
+ EXPECT_EQ(writeError(Path, "contents"),
+ std::errc::no_such_file_or_directory);
+}
+
+// Test that failing to create the temporary file after creating the missing
+// parent directory is reported.
+TEST_F(ModuleCacheTest, WriteErrorCreateTempFileAfterCreateDirectories) {
+ // Creating the temporary file in "dangling" fails because it's a dangling
+ // symlink. Creating the directory "dangling" then succeeds, because it
+ // already exists (as a symlink), but creating the temporary file again fails.
+ SmallString<256> Dangling(ModuleCachePath);
+ sys::path::append(Dangling, "dangling");
+ ASSERT_FALSE(sys::fs::create_link("does-not-exist", Dangling));
+ SmallString<256> Path(Dangling);
+ sys::path::append(Path, "M.pcm");
+
+ EXPECT_EQ(writeError(Path, "contents"),
+ std::errc::no_such_file_or_directory);
+}
+
+// Test that failing to write the contents is reported instead of crashing in
+// the destructor of the stream.
+TEST_F(ModuleCacheTest, WriteErrorWrite) {
+ SmallString<256> Path(ModuleCachePath);
+ sys::path::append(Path, "M.pcm");
+
+ // Limit the file size, making writes past the limit fail with EFBIG instead
+ // of delivering SIGXFSZ.
+ struct rlimit OldLimit;
+ ASSERT_EQ(getrlimit(RLIMIT_FSIZE, &OldLimit), 0);
+ struct rlimit NewLimit = OldLimit;
+ NewLimit.rlim_cur = 1024;
+ auto *OldHandler = std::signal(SIGXFSZ, SIG_IGN);
+ ASSERT_NE(OldHandler, SIG_ERR);
+ ASSERT_EQ(setrlimit(RLIMIT_FSIZE, &NewLimit), 0);
+ llvm::scope_exit Restore([&] {
+ setrlimit(RLIMIT_FSIZE, &OldLimit);
+ std::signal(SIGXFSZ, OldHandler);
+ });
+
+ std::string Contents(1024 * 1024, 'x');
+ EXPECT_EQ(writeError(Path, Contents, /*ReadBack=*/true),
+ std::errc::file_too_large);
+ EXPECT_EQ(writeError(Path, Contents, /*ReadBack=*/false),
+ std::errc::file_too_large);
+ EXPECT_FALSE(sys::fs::exists(Path));
+}
+
+// Test that failing to rename the temporary file to the final path is
+// reported.
+TEST_F(ModuleCacheTest, WriteErrorRename) {
+ // The final path is a non-empty directory.
+ SmallString<256> RelPath(ModuleCacheDir);
+ sys::path::append(RelPath, "M.pcm", "file");
+ addFile(RelPath, "");
+ SmallString<256> Path(ModuleCachePath);
+ sys::path::append(Path, "M.pcm");
+
+ EXPECT_EQ(writeError(Path, "contents"), std::errc::is_a_directory);
+}
+#endif
+
} // anonymous namespace
``````````
</details>
https://github.com/llvm/llvm-project/pull/243333
More information about the cfe-commits
mailing list