[IR] Add bounds checking to paramHasAttr
authorSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 4 Nov 2015 20:33:45 +0000 (20:33 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 4 Nov 2015 20:33:45 +0000 (20:33 +0000)
Summary:
This is intended to make a later change simpler.

Note: adding this bounds checking required fixing `X86FastISel`.  As
far I can tell I've preserved original behavior but a careful review
will be appreciated.

Reviewers: reames

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D14304

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252073 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/Instructions.cpp
lib/Target/X86/X86FastISel.cpp

index 59ac99b666628d26efbf7db2b48e8d2ac992739d..3394355cfb45739dfef6cc4b57923b4e7a9b6c07 100644 (file)
@@ -331,6 +331,8 @@ void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
 }
 
 bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
+  assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
+
   if (AttributeList.hasAttribute(i, A))
     return true;
   if (const Function *F = getCalledFunction())
@@ -575,6 +577,8 @@ bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
 }
 
 bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
+  assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
+
   if (AttributeList.hasAttribute(i, A))
     return true;
   if (const Function *F = getCalledFunction())
index 2cda8211ba91a6fc0bd623f83d79c8187391cfd8..914fd04ad6b7f7cfaa18ae284f17a2988487c6ef 100644 (file)
@@ -2817,10 +2817,12 @@ static unsigned computeBytesPoppedByCallee(const X86Subtarget *Subtarget,
   if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
       CC == CallingConv::HiPE)
     return 0;
-  if (CS && !CS->paramHasAttr(1, Attribute::StructRet))
-    return 0;
-  if (CS && CS->paramHasAttr(1, Attribute::InReg))
-    return 0;
+
+  if (CS)
+    if (CS->arg_empty() || !CS->paramHasAttr(1, Attribute::StructRet) ||
+        CS->paramHasAttr(1, Attribute::InReg))
+      return 0;
+
   return 4;
 }