Skip to content

Commit c9648d7

Browse files
authored
[Verifier] Make sure all constexprs in instructions are visited (#171643)
Previously this only happened for constants of some types and missed incorrect ptrtoaddr.
1 parent 4882029 commit c9648d7

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,9 @@ void Verifier::verifyFunctionMetadata(
26762676
}
26772677

26782678
void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
2679+
if (EntryC->getNumOperands() == 0)
2680+
return;
2681+
26792682
if (!ConstantExprVisited.insert(EntryC).second)
26802683
return;
26812684

@@ -5610,14 +5613,8 @@ void Verifier::visitInstruction(Instruction &I) {
56105613
} else if (isa<InlineAsm>(I.getOperand(i))) {
56115614
Check(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
56125615
"Cannot take the address of an inline asm!", &I);
5613-
} else if (auto *CPA = dyn_cast<ConstantPtrAuth>(I.getOperand(i))) {
5614-
visitConstantExprsRecursively(CPA);
5615-
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
5616-
if (CE->getType()->isPtrOrPtrVectorTy()) {
5617-
// If we have a ConstantExpr pointer, we need to see if it came from an
5618-
// illegal bitcast.
5619-
visitConstantExprsRecursively(CE);
5620-
}
5616+
} else if (auto *C = dyn_cast<Constant>(I.getOperand(i))) {
5617+
visitConstantExprsRecursively(C);
56215618
}
56225619
}
56235620

llvm/test/Assembler/ptrtoaddr-invalid-constexpr.ll

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151
@g = global i32 ptrtoaddr (ptr @g to i32)
5252
; DST_NOT_ADDR_SIZE-NEXT: PtrToAddr result must be address width
5353
; DST_NOT_ADDR_SIZE-NEXT: i32 ptrtoaddr (ptr @g to i32)
54-
@g_vec = global <4 x i32> ptrtoaddr (<4 x ptr> <ptr @g, ptr @g, ptr @g, ptr @g> to <4 x i32>)
55-
; TODO: Verifier.cpp does not visit ConstantVector/ConstantStruct values
56-
; TODO-DST_NOT_ADDR_SIZE: PtrToAddr result must be address width
54+
@g_vec = global <4 x i32> ptrtoaddr (<4 x ptr> <ptr @g_vec, ptr @g_vec, ptr @g_vec, ptr @g_vec> to <4 x i32>)
55+
; DST_NOT_ADDR_SIZE-NEXT: PtrToAddr result must be address width
56+
; DST_NOT_ADDR_SIZE-NEXT: i32 ptrtoaddr (ptr @g_vec to i32)
57+
58+
;--- dst_not_addr_size_in_inst.ll
59+
; RUN: not llvm-as %t/dst_not_addr_size_in_inst.ll -o /dev/null 2>&1 | FileCheck -check-prefix=DST_NOT_ADDR_SIZE_IN_INST %s --implicit-check-not="error:"
60+
; DST_NOT_ADDR_SIZE_IN_INST: PtrToAddr result must be address width
61+
; DST_NOT_ADDR_SIZE_IN_INST-NEXT: i32 ptrtoaddr (ptr @fn to i32)
62+
define i32 @fn() {
63+
ret i32 ptrtoaddr (ptr @fn to i32)
64+
}
65+
66+
; DST_NOT_ADDR_SIZE_IN_INST: PtrToAddr result must be address width
67+
; DST_NOT_ADDR_SIZE_IN_INST-NEXT: i32 ptrtoaddr (ptr @fn2 to i32)
68+
define <2 x i32> @fn2() {
69+
ret <2 x i32> <i32 ptrtoaddr (ptr @fn2 to i32), i32 ptrtoaddr (ptr @fn2 to i32)>
70+
}

0 commit comments

Comments
 (0)