Skip to content

[BoundsSafety] Don't bind lvalues with temporary locations #10989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: swift/release/6.2
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6663,14 +6663,8 @@ LValue CodeGenFunction::EmitMaterializeSequenceExprLValue(
const MaterializeSequenceExpr *MSE) {
if (MSE->isBinding()) {
for (auto *OVE : MSE->opaquevalues()) {
if (CodeGenFunction::OpaqueValueMappingData::shouldBindAsLValue(OVE)) {
RValue PtrRV = EmitAnyExpr(OVE->getSourceExpr());
LValue LV = MakeAddrLValue(PtrRV.getAggregateAddress(), OVE->getType());
CodeGenFunction::OpaqueValueMappingData::bind(*this, OVE, LV);
} else {
CodeGenFunction::OpaqueValueMappingData::bind(
*this, OVE, OVE->getSourceExpr());
}
CodeGenFunction::OpaqueValueMappingData::bind(
*this, OVE, OVE->getSourceExpr());
}
}

Expand Down
20 changes: 7 additions & 13 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,13 @@ void AggExprEmitter::EmitWidePointerBitCast(CastExpr *E) {
RValue SrcRV = CGF.EmitAnyExpr(E->getSubExpr());
assert(SrcRV.isAggregate());
Address SrcAddr = SrcRV.getAggregateAddress();
EmitWidePointerToDest(E->getType(),
CGF.GetWidePointerElement(SrcAddr, WPIndex::Pointer),
CGF.GetWidePointerElement(SrcAddr, WPIndex::Upper),
E->getType()->isBidiIndexablePointerType()
llvm::Value *Ptr = CGF.GetWidePointerElement(SrcAddr, WPIndex::Pointer);
llvm::Value *Upper = CGF.GetWidePointerElement(SrcAddr, WPIndex::Upper);
llvm::Value *Lower = E->getType()->isBidiIndexablePointerType()
? CGF.GetWidePointerElement(SrcAddr, WPIndex::Lower)
: nullptr,
ElemBitCast);
: nullptr;

EmitWidePointerToDest(E->getType(), Ptr, Upper, Lower, ElemBitCast);
}

void AggExprEmitter::EmitWidePointer(LValue DestLV, llvm::Value *Ptr,
Expand Down Expand Up @@ -591,13 +591,7 @@ void AggExprEmitter::VisitPredefinedBoundsCheckExpr(
void AggExprEmitter::VisitMaterializeSequenceExpr(MaterializeSequenceExpr *MSE) {
if (MSE->isBinding()) {
for (auto *OVE : MSE->opaquevalues()) {
if (CodeGenFunction::OpaqueValueMappingData::shouldBindAsLValue(OVE)) {
RValue PtrRV = CGF.EmitAnyExpr(OVE->getSourceExpr());
LValue LV = CGF.MakeAddrLValue(PtrRV.getAggregateAddress(), OVE->getType());
CodeGenFunction::OpaqueValueMappingData::bind(CGF, OVE, LV);
} else {
CodeGenFunction::OpaqueValueMappingData::bind(CGF, OVE, OVE->getSourceExpr());
}
CodeGenFunction::OpaqueValueMappingData::bind(CGF, OVE, OVE->getSourceExpr());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ const Item oidRsa = { _oidRsa, sizeof(_oidRsa)};
// CHECK-NEXT: entry:
// CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
// CHECK-NEXT: [[AGG_TEMP:%.*]] = alloca %"__bounds_safety::wide_ptr.bidi_indexable", align 8
// CHECK-NEXT: [[AGG_TEMP1:%.*]] = alloca [[STRUCT_ITEM:%.*]], align 8
// CHECK-NEXT: store i32 0, ptr [[RETVAL]], align 4
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[AGG_TEMP1]], ptr align 8 @oidRsa, i64 16, i1 false)
// CHECK-NEXT: [[LENGTH:%.*]] = getelementptr inbounds [[STRUCT_ITEM]], ptr [[AGG_TEMP1]], i32 0, i32 1
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[LENGTH]], align 8
// CHECK-NEXT: [[DATA:%.*]] = getelementptr inbounds [[STRUCT_ITEM]], ptr [[AGG_TEMP1]], i32 0, i32 0
// CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[DATA]], align 8
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr getelementptr inbounds ([[STRUCT_ITEM:%.*]], ptr @oidRsa, i32 0, i32 1), align 8
// CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr @oidRsa, align 8
// CHECK-NEXT: [[IDX_EXT:%.*]] = sext i32 [[TMP0]] to i64
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[TMP1]], i64 [[IDX_EXT]]
// CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds %"__bounds_safety::wide_ptr.bidi_indexable", ptr [[AGG_TEMP]], i32 0, i32 0
Expand All @@ -45,11 +41,11 @@ const Item oidRsa = { _oidRsa, sizeof(_oidRsa)};
// CHECK-NEXT: unreachable
// CHECK: cont:
// CHECK-NEXT: [[TMP7:%.*]] = icmp uge ptr [[TMP5]], [[WIDE_PTR_LB]], {{!annotation ![0-9]+}}
// CHECK-NEXT: br i1 [[TMP7]], label [[CONT3:%.*]], label [[TRAP2:%.*]], {{!annotation ![0-9]+}}
// CHECK: trap2:
// CHECK-NEXT: br i1 [[TMP7]], label %[[CONT3:.*]], label %[[TRAP2:.*]], {{!annotation ![0-9]+}}
// CHECK: [[TRAP2]]:
// CHECK-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-NEXT: unreachable
// CHECK: cont3:
// CHECK: [[CONT3]]:
// CHECK-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP5]], align 4
// CHECK-NEXT: ret i32 [[TMP8]]
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ struct S {
// CHECK-O0-NEXT: [[AGG_TEMP33:%.*]] = alloca %"__bounds_safety::wide_ptr.bidi_indexable", align 8
// CHECK-O0-NEXT: [[I:%.*]] = alloca i32, align 4
// CHECK-O0-NEXT: [[AGG_TEMP42:%.*]] = alloca %"__bounds_safety::wide_ptr.bidi_indexable", align 8
// CHECK-O0-NEXT: [[AGG_TEMP43:%.*]] = alloca [[STRUCT_S]], align 8
// CHECK-O0-NEXT: [[AGG_TEMP56:%.*]] = alloca %"__bounds_safety::wide_ptr.bidi_indexable", align 8
// CHECK-O0-NEXT: [[AGG_TEMP57:%.*]] = alloca [[STRUCT_S]], align 8
// CHECK-O0-NEXT: [[AGG_TEMP73:%.*]] = alloca %"__bounds_safety::wide_ptr.bidi_indexable", align 8
// CHECK-O0-NEXT: [[AGG_TEMP74:%.*]] = alloca [[STRUCT_S]], align 8
// CHECK-O0-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [10 x i32], ptr [[ARR]], i64 0, i64 0
// CHECK-O0-NEXT: [[UPPER:%.*]] = getelementptr inbounds i32, ptr [[ARRAYDECAY]], i64 10
// CHECK-O0-NEXT: [[TMP0:%.*]] = getelementptr inbounds %"__bounds_safety::wide_ptr.bidi_indexable", ptr [[TMP]], i32 0, i32 0
Expand Down Expand Up @@ -146,10 +143,9 @@ struct S {
// CHECK-O0-NEXT: br i1 [[CMP]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
// CHECK-O0: for.body:
// CHECK-O0-NEXT: [[TMP23:%.*]] = load i32, ptr [[I]], align 4
// CHECK-O0-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[AGG_TEMP43]], ptr align 8 [[S]], i64 24, i1 false)
// CHECK-O0-NEXT: [[L44:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP43]], i32 0, i32 2
// CHECK-O0-NEXT: [[L44:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 2
// CHECK-O0-NEXT: [[TMP24:%.*]] = load i32, ptr [[L44]], align 8
// CHECK-O0-NEXT: [[BP45:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP43]], i32 0, i32 0
// CHECK-O0-NEXT: [[BP45:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 0
// CHECK-O0-NEXT: [[TMP25:%.*]] = load ptr, ptr [[BP45]], align 8
// CHECK-O0-NEXT: [[IDX_EXT:%.*]] = sext i32 [[TMP24]] to i64
// CHECK-O0-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i32, ptr [[TMP25]], i64 [[IDX_EXT]]
Expand All @@ -169,17 +165,17 @@ struct S {
// CHECK-O0-NEXT: [[WIDE_PTR_LB_ADDR50:%.*]] = getelementptr inbounds %"__bounds_safety::wide_ptr.bidi_indexable", ptr [[AGG_TEMP42]], i32 0, i32 2
// CHECK-O0-NEXT: [[WIDE_PTR_LB51:%.*]] = load ptr, ptr [[WIDE_PTR_LB_ADDR50]], align 8
// CHECK-O0-NEXT: [[TMP30:%.*]] = icmp ult ptr [[ARRAYIDX]], [[WIDE_PTR_UB49]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP30]], label [[CONT53:%.*]], label [[TRAP52:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap52:
// CHECK-O0-NEXT: br i1 [[TMP30]], label %[[CONT53:.*]], label %[[TRAP52:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP52]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont53:
// CHECK-O0: [[CONT53]]:
// CHECK-O0-NEXT: [[TMP31:%.*]] = icmp uge ptr [[ARRAYIDX]], [[WIDE_PTR_LB51]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP31]], label [[CONT55:%.*]], label [[TRAP54:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap54:
// CHECK-O0-NEXT: br i1 [[TMP31]], label %[[CONT55:.*]], label %[[TRAP54:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP54]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont55:
// CHECK-O0: [[CONT55]]:
// CHECK-O0-NEXT: store i32 [[TMP23]], ptr [[ARRAYIDX]], align 4
// CHECK-O0-NEXT: br label [[FOR_INC:%.*]]
// CHECK-O0: for.inc:
Expand All @@ -188,10 +184,9 @@ struct S {
// CHECK-O0-NEXT: store i32 [[INC]], ptr [[I]], align 4
// CHECK-O0-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP5:![0-9]+]]
// CHECK-O0: for.end:
// CHECK-O0-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[AGG_TEMP57]], ptr align 8 [[S]], i64 24, i1 false)
// CHECK-O0-NEXT: [[L58:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP57]], i32 0, i32 2
// CHECK-O0-NEXT: [[L58:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 2
// CHECK-O0-NEXT: [[TMP33:%.*]] = load i32, ptr [[L58]], align 8
// CHECK-O0-NEXT: [[BP259:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP57]], i32 0, i32 1
// CHECK-O0-NEXT: [[BP259:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 1
// CHECK-O0-NEXT: [[TMP34:%.*]] = load ptr, ptr [[BP259]], align 8
// CHECK-O0-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP33]], 1
// CHECK-O0-NEXT: [[IDX_EXT60:%.*]] = sext i32 [[ADD]] to i64
Expand All @@ -210,22 +205,21 @@ struct S {
// CHECK-O0-NEXT: [[WIDE_PTR_LB_ADDR67:%.*]] = getelementptr inbounds %"__bounds_safety::wide_ptr.bidi_indexable", ptr [[AGG_TEMP56]], i32 0, i32 2
// CHECK-O0-NEXT: [[WIDE_PTR_LB68:%.*]] = load ptr, ptr [[WIDE_PTR_LB_ADDR67]], align 8
// CHECK-O0-NEXT: [[TMP38:%.*]] = icmp ult ptr [[ARRAYIDX64]], [[WIDE_PTR_UB66]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP38]], label [[CONT70:%.*]], label [[TRAP69:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap69:
// CHECK-O0-NEXT: br i1 [[TMP38]], label %[[CONT70:.*]], label %[[TRAP69:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP69]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont70:
// CHECK-O0: [[CONT70]]:
// CHECK-O0-NEXT: [[TMP39:%.*]] = icmp uge ptr [[ARRAYIDX64]], [[WIDE_PTR_LB68]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP39]], label [[CONT72:%.*]], label [[TRAP71:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap71:
// CHECK-O0-NEXT: br i1 [[TMP39]], label %[[CONT72:.*]], label %[[TRAP71:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP71]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont72:
// CHECK-O0: [[CONT72]]:
// CHECK-O0-NEXT: [[TMP40:%.*]] = load i32, ptr [[ARRAYIDX64]], align 4
// CHECK-O0-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[AGG_TEMP74]], ptr align 8 [[S]], i64 24, i1 false)
// CHECK-O0-NEXT: [[L75:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP74]], i32 0, i32 2
// CHECK-O0-NEXT: [[L75:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 2
// CHECK-O0-NEXT: [[TMP41:%.*]] = load i32, ptr [[L75]], align 8
// CHECK-O0-NEXT: [[BP76:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[AGG_TEMP74]], i32 0, i32 0
// CHECK-O0-NEXT: [[BP76:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[S]], i32 0, i32 0
// CHECK-O0-NEXT: [[TMP42:%.*]] = load ptr, ptr [[BP76]], align 8
// CHECK-O0-NEXT: [[IDX_EXT77:%.*]] = sext i32 [[TMP41]] to i64
// CHECK-O0-NEXT: [[ADD_PTR78:%.*]] = getelementptr inbounds i32, ptr [[TMP42]], i64 [[IDX_EXT77]]
Expand All @@ -243,17 +237,17 @@ struct S {
// CHECK-O0-NEXT: [[WIDE_PTR_LB_ADDR84:%.*]] = getelementptr inbounds %"__bounds_safety::wide_ptr.bidi_indexable", ptr [[AGG_TEMP73]], i32 0, i32 2
// CHECK-O0-NEXT: [[WIDE_PTR_LB85:%.*]] = load ptr, ptr [[WIDE_PTR_LB_ADDR84]], align 8
// CHECK-O0-NEXT: [[TMP46:%.*]] = icmp ult ptr [[ARRAYIDX81]], [[WIDE_PTR_UB83]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP46]], label [[CONT87:%.*]], label [[TRAP86:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap86:
// CHECK-O0-NEXT: br i1 [[TMP46]], label %[[CONT87:.*]], label %[[TRAP86:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP86]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont87:
// CHECK-O0: [[CONT87]]:
// CHECK-O0-NEXT: [[TMP47:%.*]] = icmp uge ptr [[ARRAYIDX81]], [[WIDE_PTR_LB85]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: br i1 [[TMP47]], label [[CONT89:%.*]], label [[TRAP88:%.*]], {{!annotation ![0-9]+}}
// CHECK-O0: trap88:
// CHECK-O0-NEXT: br i1 [[TMP47]], label %[[CONT89:.*]], label %[[TRAP88:.*]], {{!annotation ![0-9]+}}
// CHECK-O0: [[TRAP88]]:
// CHECK-O0-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR3]], {{!annotation ![0-9]+}}
// CHECK-O0-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK-O0: cont89:
// CHECK-O0: [[CONT89]]:
// CHECK-O0-NEXT: [[TMP48:%.*]] = load i32, ptr [[ARRAYIDX81]], align 4
// CHECK-O0-NEXT: [[ADD90:%.*]] = add nsw i32 [[TMP40]], [[TMP48]]
// CHECK-O0-NEXT: ret i32 [[ADD90]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ void TestAccessFail() {
// CHECK-NEXT: [[TMP1:%.*]] = icmp ult ptr [[TMP0]], [[UPPER]], {{!annotation ![0-9]+}}
// CHECK-NEXT: [[TMP2:%.*]] = icmp uge ptr [[TMP0]], [[ARR]], {{!annotation ![0-9]+}}
// CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[TMP1]], [[TMP2]], {{!annotation ![0-9]+}}
// CHECK-NEXT: br i1 [[OR_COND]], label [[CONT47:%.*]], label [[TRAP:%.*]], {{!annotation ![0-9]+}}
// CHECK-NEXT: br i1 [[OR_COND]], label %[[CONT47:.*]], label [[TRAP:%.*]], {{!annotation ![0-9]+}}
// CHECK: trap:
// CHECK-NEXT: call void @llvm.ubsantrap(i8 25) #[[ATTR6]], {{!annotation ![0-9]+}}
// CHECK-NEXT: unreachable, {{!annotation ![0-9]+}}
// CHECK: cont47:
// CHECK: [[CONT47]]:
// CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 36, ptr nonnull [[ARR]]) #[[ATTR7]]
// CHECK-NEXT: ret void
//
Expand Down
Loading