https://github.com/ahatanak created 
https://github.com/llvm/llvm-project/pull/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

>From 076d724816418669350d0bc8f94025910644413a Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <[email protected]>
Date: Tue, 25 Aug 2026 11:41:19 -0700
Subject: [PATCH] [AST] Make err_struct_too_large check target-aware

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
---
 clang/lib/AST/RecordLayoutBuilder.cpp  |  5 ++++-
 clang/test/AST/absurdly_big_struct.cpp | 17 +++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

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..2677f7c1e3b60 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,11 @@ 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).
+struct b { // bit32-error {{structure 'b' is too large, which exceeds maximum 
allowed size of 4294967296 bytes}}
+  char c[0xFFFFFFFF];
+  char c2[1];
+};
+
+long long y() { return sizeof(b); }
+

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

Reply via email to