Skip to content

Commit

Permalink
Tighten checks for IR aggregate sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Aug 18, 2024
1 parent 6bd5d9a commit 6551a7d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ir/iraggr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "dmd/aggregate.h"
#include "dmd/declaration.h"
#include "dmd/errors.h"
#include "dmd/expression.h"
#include "dmd/identifier.h"
#include "dmd/init.h"
Expand Down Expand Up @@ -217,8 +218,12 @@ IrAggr::createInitializerConstant(const VarInitMap &explicitInitializers) {

// tail padding?
const size_t structsize = aggrdecl->size(Loc());
if (offset < structsize)
if (offset < structsize) {
add_zeros(constants, offset, structsize);
} else if (offset > structsize) {
error(Loc(), "ICE: IR aggregate constant size exceeds the frontend size");
fatal();
}

// get LL field types
llvm::SmallVector<llvm::Type *, 16> types;
Expand Down
6 changes: 6 additions & 0 deletions ir/irtypeclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "dmd/aggregate.h"
#include "dmd/declaration.h"
#include "dmd/dsymbol.h"
#include "dmd/errors.h"
#include "dmd/mtype.h"
#include "dmd/target.h"
#include "dmd/template.h"
Expand Down Expand Up @@ -98,6 +99,11 @@ IrTypeClass *IrTypeClass::get(ClassDeclaration *cd) {
isaStruct(t->type)->setBody(builder.defaultTypes(), builder.isPacked());
t->varGEPIndices = builder.varGEPIndices();

if (instanceSize && getTypeAllocSize(t->type) != instanceSize) {
error(cd->loc, "ICE: class IR size does not match the frontend size");
fatal();
}

IF_LOG Logger::cout() << "class type: " << *t->type << std::endl;

return t;
Expand Down
6 changes: 6 additions & 0 deletions ir/irtypestruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "dmd/aggregate.h"
#include "dmd/declaration.h"
#include "dmd/errors.h"
#include "dmd/init.h"
#include "dmd/mtype.h"
#include "dmd/template.h"
Expand Down Expand Up @@ -91,6 +92,11 @@ IrTypeStruct *IrTypeStruct::get(StructDeclaration *sd) {
builder.addTailPadding(sd->structsize);
isaStruct(t->type)->setBody(builder.defaultTypes(), builder.isPacked());
t->varGEPIndices = builder.varGEPIndices();

if (getTypeAllocSize(t->type) != sd->structsize) {
error(sd->loc, "ICE: struct IR size does not match the frontend size");
fatal();
}
}

IF_LOG Logger::cout() << "final struct type: " << *t->type << std::endl;
Expand Down
20 changes: 20 additions & 0 deletions tests/compilable/gh4734.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %ldc -c %s

align(1) struct Item {
KV v;
uint i;
}

struct KV {
align(1) S* s;
uint k;
}

struct S {
Table table;
}

struct Table {
char a;
Item v;
}

0 comments on commit 6551a7d

Please sign in to comment.