[llvm] [LLVM][Verifier] Fix buffer overflow when verifying gc.statepoint (PR #208278)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 9 03:13:47 PDT 2026
nikic wrote:
I think my overall inclination would be to add bounds checks to getDerivedPtr() / getBasePtr() after all. Even if we don't print in these specific cases, making the IR printer crash when the IR does not pass the verifier generally makes for a pretty bad debugging experience. So I would suggest a patch like this:
```diff
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index e90630a8cae5..6752d6aa344d 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -4372,9 +4372,15 @@ void AssemblyWriter::printInstructionLine(const Instruction &I) {
/// intrinsic indicating base and derived pointer names.
void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Out << " ; (";
- writeOperand(Relocate.getBasePtr(), false);
+ if (Value *BasePtr = Relocate.getBasePtr())
+ writeOperand(BasePtr, false);
+ else
+ Out << "invalid";
Out << ", ";
- writeOperand(Relocate.getDerivedPtr(), false);
+ if (Value *DerivedPtr = Relocate.getDerivedPtr())
+ writeOperand(DerivedPtr, false);
+ else
+ Out << "invalid";
Out << ")";
}
diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index 3e9f3257956a..eb964d566f29 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -867,10 +867,16 @@ Value *GCRelocateInst::getBasePtr() const {
auto Statepoint = getStatepoint();
if (isa<UndefValue>(Statepoint))
return UndefValue::get(Statepoint->getType());
-
+ // Handle too few (bundle) arguments to avoid crashes when printing invalid
+ // IR, e.g. in the verifier.
auto *GCInst = cast<GCStatepointInst>(Statepoint);
- if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
+ if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live)) {
+ if (getBasePtrIndex() > Opt->Inputs.size())
+ return nullptr;
return *(Opt->Inputs.begin() + getBasePtrIndex());
+ }
+ if (getBasePtrIndex() > GCInst->arg_size())
+ return nullptr;
return *(GCInst->arg_begin() + getBasePtrIndex());
}
@@ -879,9 +885,16 @@ Value *GCRelocateInst::getDerivedPtr() const {
if (isa<UndefValue>(Statepoint))
return UndefValue::get(Statepoint->getType());
+ // Handle too few (bundle) arguments to avoid crashes when printing invalid
+ // IR, e.g. in the verifier.
auto *GCInst = cast<GCStatepointInst>(Statepoint);
- if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live))
+ if (auto Opt = GCInst->getOperandBundle(LLVMContext::OB_gc_live)) {
+ if (getDerivedPtrIndex() > Opt->Inputs.size())
+ return nullptr;
return *(Opt->Inputs.begin() + getDerivedPtrIndex());
+ }
+ if (getDerivedPtrIndex() > GCInst->arg_size())
+ return nullptr;
return *(GCInst->arg_begin() + getDerivedPtrIndex());
}
```
The verifier output then becomes:
```
gc.relocate: statepoint base index out of bounds
%reloc = call ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %safepoint_token, i32 -1, i32 0) ; (invalid, %a)
gc.relocate: statepoint derived index out of bounds
%reloc = call ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %safepoint_token, i32 0, i32 -1) ; (%a, invalid)
build/bin/opt: -: error: input module is broken!
```
https://github.com/llvm/llvm-project/pull/208278
More information about the llvm-commits
mailing list