[llvm-branch-commits] [clang] [clang] Ensure the AST file extracted from a PCM is aligned (PR #244698)
Jan Svoboda via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 20 12:07:21 PST 2026
https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/244698
The COFF object writer doesn't align section contents within the file, so
the `clangast` section of a `-fmodule-format=obj` PCM may start at an
unaligned offset. The readers use the AST file in place, which trips the
on-disk hash table alignment assertion.
Before #242384, the importing compiler instance copied the AST file out of
a freshly built PCM into a (suitably aligned) heap buffer, which hid the
issue for that PCM. However, reading the PCM from disk has always used the
memory-mapped file directly, which happens when:
* reusing the implicit module cache in a subsequent compilation,
* reading a PCM built by another process (while waiting on the lock),
* reading dependencies that already exist on disk,
* loading explicit modules or PCHs,
* building the global module index.
Since #242384, the freshly built PCM is no longer copied either, so the
assertion now fires on the very first compilation. Note that the reads
themselves are alignment-agnostic, so builds without assertions are most
likely unaffected.
This patch makes the object file reader's `ExtractPCH()` expose the AST
file without copying when it's suitably aligned, and makes an aligned copy
otherwise.
Fixes #244580.
This PR is stacked on top of #244697.
>From 9c3548940639e123184f88f52ac1e6226cf33bb2 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Fri, 20 Nov 2026 11:52:56 -0800
Subject: [PATCH] [clang] Ensure the AST file extracted from a PCM is aligned
The COFF object writer doesn't align section contents within the file, so
the `clangast` section of a `-fmodule-format=obj` PCM may start at an
unaligned offset. The readers use the AST file in place, which trips the
on-disk hash table alignment assertion.
Before #242384, the importing compiler instance copied the AST file out of
a freshly built PCM into a (suitably aligned) heap buffer, which hid the
issue for that PCM. However, reading the PCM from disk has always used the
memory-mapped file directly, which happens when:
* reusing the implicit module cache in a subsequent compilation,
* reading a PCM built by another process (while waiting on the lock),
* reading dependencies that already exist on disk,
* loading explicit modules or PCHs,
* building the global module index.
Since #242384, the freshly built PCM is no longer copied either, so the
assertion now fires on the very first compilation. Note that the reads
themselves are alignment-agnostic, so builds without assertions are most
likely unaffected.
This patch makes the object file reader's `ExtractPCH()` expose the AST
file without copying when it's suitably aligned, and makes an aligned copy
otherwise.
Fixes #244580.
---
.../ObjectFilePCHContainerReader.h | 3 ++
.../ObjectFilePCHContainerReader.cpp | 6 +++
.../Modules/pch-container-coff-alignment.c | 41 +++++++++++++++++++
3 files changed, 50 insertions(+)
create mode 100644 clang/test/Modules/pch-container-coff-alignment.c
diff --git a/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h b/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
index 916d81aa6d2aba..a490460ed31392 100644
--- a/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
+++ b/clang/include/clang/Serialization/ObjectFilePCHContainerReader.h
@@ -18,6 +18,9 @@ class ObjectFilePCHContainerReader : public PCHContainerReader {
ArrayRef<StringRef> getFormats() const override;
/// Returns a buffer with the serialized AST inside the PCH container Buffer.
+ /// This may copy a part of the buffer if the AST is not properly aligned.
+ /// That can happen with some object file formats like COFF that don't align
+ /// section contents within the file.
std::unique_ptr<llvm::MemoryBuffer>
ExtractPCH(std::unique_ptr<llvm::MemoryBuffer> Buffer) const override;
};
diff --git a/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp b/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp
index 26ac26c3d7253f..79b32e603f1206 100644
--- a/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp
+++ b/clang/lib/Serialization/ObjectFilePCHContainerReader.cpp
@@ -9,6 +9,7 @@
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/Alignment.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace clang;
@@ -87,5 +88,10 @@ ObjectFilePCHContainerReader::ExtractPCH(
if (Extracted.begin() == Buffer->getBufferStart() &&
Extracted.end() == Buffer->getBufferEnd())
return Buffer;
+ // The on-disk hash tables need to be aligned. This matches the alignment the
+ // object file container writer requests for the serialized AST.
+ if (!llvm::isAddrAligned(llvm::Align(8), Extracted.data()))
+ return llvm::MemoryBuffer::getMemBufferCopy(Extracted,
+ Buffer->getBufferIdentifier());
return std::make_unique<SubMemoryBuffer>(std::move(Buffer), Extracted);
}
diff --git a/clang/test/Modules/pch-container-coff-alignment.c b/clang/test/Modules/pch-container-coff-alignment.c
new file mode 100644
index 00000000000000..7415d41780cb0d
--- /dev/null
+++ b/clang/test/Modules/pch-container-coff-alignment.c
@@ -0,0 +1,41 @@
+// Check that the AST file is read correctly from a COFF PCM, even when the
+// clangast section contents aren't aligned within the file.
+
+// REQUIRES: aarch64-registered-target
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -triple aarch64-unknown-windows-msvc -fsanitize=address \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
+// RUN: -fmodule-format=obj -fno-modules-global-index -fsyntax-only \
+// RUN: %t/t.c -I %t
+
+// The MC COFF writer aligns section contents within the file, but llvm-objcopy
+// lays them out without any padding. Use it to misalign the clangast section.
+// RUN: llvm-objcopy %t/cache/*/M-*.pcm
+// RUN: llvm-readobj --sections %t/cache/*/M-*.pcm | FileCheck %s
+// CHECK: Name: clangast
+// CHECK-NEXT: VirtualSize:
+// CHECK-NEXT: VirtualAddress:
+// CHECK-NEXT: RawDataSize:
+// CHECK-NEXT: PointerToRawData: 0x{{[0-9A-F]*}}{{[1235679ABDEF]}}{{$}}
+
+// Read the PCM from disk.
+// RUN: %clang_cc1 -triple aarch64-unknown-windows-msvc -fsanitize=address \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
+// RUN: -fmodule-format=obj -fno-modules-global-index -fsyntax-only \
+// RUN: %t/t.c -I %t -Rmodule-import 2>&1 | FileCheck %s --check-prefix=IMPORT
+// IMPORT: remark: importing module 'M' from '{{.*}}M-{{.*}}.pcm'
+// IMPORT-NOT: remark: building module
+
+// Build the global module index, which reads the PCM from disk too.
+// RUN: %clang_cc1 -triple aarch64-unknown-windows-msvc -fsanitize=address \
+// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
+// RUN: -fmodule-format=obj -fsyntax-only %t/t.c -I %t
+
+//--- module.modulemap
+module M { header "m.h" }
+//--- m.h
+int m(void);
+//--- t.c
+#include "m.h"
More information about the llvm-branch-commits
mailing list