Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticASTKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,8 @@ def note_module_odr_violation_mismatch_decl_unknown : Note<
"different friend declaration|different function template|different method|"
"different instance variable|different property|another unexpected decl}2">;

def err_struct_too_large : Error<
"structure '%0' is too large, which exceeds maximum allowed size of %1 bytes">;

def remark_sanitize_address_insert_extra_padding_accepted : Remark<
"-fsanitize-address-field-padding applied to %0">, ShowInSystemHeader,
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/AST/RecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3463,6 +3463,13 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {

ASTRecordLayouts[D] = NewEntry;

constexpr uint64_t MaxStructSizeInBytes = 1ULL << 60;
CharUnits StructSize = NewEntry->getSize();
if (static_cast<uint64_t>(StructSize.getQuantity()) >= MaxStructSizeInBytes) {
getDiagnostics().Report(D->getLocation(), diag::err_struct_too_large)
<< D->getName() << MaxStructSizeInBytes;
}

if (getLangOpts().DumpRecordLayouts) {
llvm::outs() << "\n*** Dumping AST Record Layout\n";
DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/absurdly_big_struct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-linux-gnu

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];
};

a z[1];
long long x() { return sizeof(a); }
long long x2() { return sizeof(a::x); }
long long x3() { return sizeof(a::x2); }
long long x4() { return sizeof(z); }

4 changes: 2 additions & 2 deletions clang/test/Sema/offsetof-64.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

// PR15216
// Don't crash when taking computing the offset of structs with large arrays.
const unsigned long Size = (1l << 60);
const unsigned long Size = (1l << 58);

struct Chunk1 {
char padding[Size]; // expected-warning {{folded to constant}}
char more_padding[1][Size]; // expected-warning {{folded to constant}}
char data;
};

int test1 = __builtin_offsetof(struct Chunk1, data);
unsigned long test1 = __builtin_offsetof(struct Chunk1, data);

struct Chunk2 {
char padding[Size][Size][Size]; // expected-error {{array is too large}}
Expand Down