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
11 changes: 9 additions & 2 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4922,6 +4922,9 @@ static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
unsigned idx =
CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);

if (CGF.getLangOpts().PointerOverflowDefined)
return CGF.Builder.CreateConstGEP2_32(base, 0, idx, field->getName());

return CGF.Builder.CreateStructGEP(base, idx, field->getName());
}

Expand Down Expand Up @@ -4979,9 +4982,13 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
if (!UseVolatile) {
if (!IsInPreservedAIRegion &&
(!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
if (Idx != 0)
if (Idx != 0) {
// For structs, we GEP to the field that the record layout suggests.
Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
if (getLangOpts().PointerOverflowDefined)
Addr = Builder.CreateConstGEP2_32(Addr, 0, Idx, field->getName());
else
Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
}
} else {
llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
getContext().getRecordType(rec), rec->getLocation());
Expand Down
21 changes: 21 additions & 0 deletions clang/test/CodeGen/pointer-overflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@ void test(void) {
// DEFAULT: getelementptr inbounds nuw i32, ptr
// FWRAPV-POINTER: getelementptr i32, ptr
}

struct S {
int a;
int b;
int c: 10;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious why did you add bit-fields in specifically to this test? Are they particularly different wrt other types of fields like arrays, pointers, member pointers etc that we would expect them to break?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bitfields are handled by a different code path:

Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());

int d: 10;
};

int test_struct(struct S* s) {
// -fwrapv-pointer should turn off inbounds nuw for struct GEP's
return s->b;
// DEFAULT: getelementptr inbounds nuw %struct.S, ptr
// FWRAPV-POINTER: getelementptr %struct.S, ptr
}

int test_struct_bitfield(struct S* s) {
// -fwrapv-pointer should turn off inbounds nuw for struct GEP's
return s->d;
// DEFAULT: getelementptr inbounds nuw %struct.S, ptr
// FWRAPV-POINTER: getelementptr %struct.S, ptr
}