Author: Akira Hatanaka
Date: 2026-08-28T02:05:30Z
New Revision: 92efec0854b25c2344975e70a2048e7953a24bef

URL: 
https://github.com/llvm/llvm-project/commit/92efec0854b25c2344975e70a2048e7953a24bef
DIFF: 
https://github.com/llvm/llvm-project/commit/92efec0854b25c2344975e70a2048e7953a24bef.diff

LOG: [AST] Make err_struct_too_large check target-aware (#218749)

ASTContext::getASTRecordLayout used a fixed 1ULL << 60 threshold for
err_struct_too_large, regardless of the target's size_t width.

Scale the threshold to the target's size_t width instead, so it is below
(1 << 32) on 32-bit architectures. Diagnosing the overflow in Sema
avoids the crash in codegen.

rdar://183351516

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/AST/RecordLayoutBuilder.cpp
    clang/test/AST/absurdly_big_struct.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 35aae605d8476..b28898e95d32e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -550,6 +550,10 @@ features cannot lower the translation-unit ABI level;
 - Fixed a crash when instantiating an invalid dependent friend destructor 
declaration in a class template. (#GH210234)
 - Fixed an assertion failure in `-extract-api` when a documentation comment
   contains invalid UTF-8. (#GH212393)
+- Fixed a crash in codegen on 32-bit targets caused by a struct too large to
+  represent in `size_t`. The `err_struct_too_large` check now scales the
+  threshold to the target's `size_t` width instead of using a fixed
+  threshold of `1 << 60` regardless of the target.
 - Fixed a crash when generating fake uses for parameters of bodyless 
destructors with `-fextend-variable-liveness`.
 
 ### OpenACC Specific Changes

diff  --git a/clang/lib/AST/RecordLayoutBuilder.cpp 
b/clang/lib/AST/RecordLayoutBuilder.cpp
index e6da6c78238c1..45636457b4e8b 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3511,7 +3511,10 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) 
const {
 
   ASTRecordLayouts[D] = NewEntry;
 
-  constexpr uint64_t MaxStructSizeInBytes = 1ULL << 60;
+  // Cap at the target's size_t width (up to 60 bits) so oversized layouts on
+  // narrow targets are diagnosed instead of overflowing size_t in codegen.
+  uint64_t MaxStructSizeInBytes =
+      1ULL << std::min<unsigned>(getTypeSize(getSizeType()), 60);
   CharUnits StructSize = NewEntry->getSize();
   if (static_cast<uint64_t>(StructSize.getQuantity()) >= MaxStructSizeInBytes) 
{
     getDiagnostics().Report(D->getLocation(), diag::err_struct_too_large)

diff  --git a/clang/test/AST/absurdly_big_struct.cpp 
b/clang/test/AST/absurdly_big_struct.cpp
index c17274343d57a..69e191aaf70fa 100644
--- a/clang/test/AST/absurdly_big_struct.cpp
+++ b/clang/test/AST/absurdly_big_struct.cpp
@@ -1,8 +1,9 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-linux-gnu
+// RUN: %clang_cc1 -fsyntax-only -verify=bit64 %s -triple x86_64-linux-gnu
+// RUN: %clang_cc1 -fsyntax-only -verify=bit32 %s -triple 
armv7-unknown-linux-gnueabi
 
-struct a { // expected-error {{structure 'a' is too large, which exceeds 
maximum allowed size of 1152921504606846976 bytes}}
-  char x[1ull<<60]; 
-  char x2[1ull<<60]; 
+struct a { // bit64-error {{structure 'a' is too large, which exceeds maximum 
allowed size of 1152921504606846976 bytes}}
+  char x[1ull<<60]; // bit32-error {{array is too large}}
+  char x2[1ull<<60]; // bit32-error {{array is too large}}
 };
 
 a z[1];
@@ -11,3 +12,13 @@ long long x2() { return sizeof(a::x); }
 long long x3() { return sizeof(a::x2); }
 long long x4() { return sizeof(z); }
 
+// On 32-bit architectures, the struct size must be below (1 << 32).
+// This used to crash in CodeGen.
+struct b { // bit32-error {{structure 'b' is too large, which exceeds maximum 
allowed size of 4294967296 bytes}}
+  char c[0xFFFFFFFE];
+  char c1[4];
+  char c2[2];
+};
+
+long long y(int i) { return __builtin_offsetof(b, c2[i]); }
+


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to