[PATCH] Have more strict type checks when creating BinOp nodes in BitcodeReader
Filipe Cabecinhas
filcab+llvm.phabricator at gmail.com
Tue Apr 14 07:15:57 PDT 2015
Hi rafael, bkramer,
Bug found with AFL.
http://reviews.llvm.org/D9015
Files:
lib/Bitcode/Reader/BitcodeReader.cpp
test/Bitcode/Inputs/invalid-fp-shift.bc
test/Bitcode/invalid.test
Index: lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- lib/Bitcode/Reader/BitcodeReader.cpp
+++ lib/Bitcode/Reader/BitcodeReader.cpp
@@ -607,27 +607,42 @@
case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
}
}
+
static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
+ bool IsFP = Ty->isFPOrFPVectorTy();
+ // BinOps are only valid for int/fp or vector of int/fp types
+ if (!IsFP && !Ty->isIntOrIntVectorTy())
+ return -1;
+
switch (Val) {
- default: return -1;
+ default:
+ return -1;
case bitc::BINOP_ADD:
- return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
+ return IsFP ? Instruction::FAdd : Instruction::Add;
case bitc::BINOP_SUB:
- return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
+ return IsFP ? Instruction::FSub : Instruction::Sub;
case bitc::BINOP_MUL:
- return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
- case bitc::BINOP_UDIV: return Instruction::UDiv;
+ return IsFP ? Instruction::FMul : Instruction::Mul;
+ case bitc::BINOP_UDIV:
+ return IsFP ? -1 : Instruction::UDiv;
case bitc::BINOP_SDIV:
- return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
- case bitc::BINOP_UREM: return Instruction::URem;
+ return IsFP ? Instruction::FDiv : Instruction::SDiv;
+ case bitc::BINOP_UREM:
+ return IsFP ? -1 : Instruction::URem;
case bitc::BINOP_SREM:
- return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
- case bitc::BINOP_SHL: return Instruction::Shl;
- case bitc::BINOP_LSHR: return Instruction::LShr;
- case bitc::BINOP_ASHR: return Instruction::AShr;
- case bitc::BINOP_AND: return Instruction::And;
- case bitc::BINOP_OR: return Instruction::Or;
- case bitc::BINOP_XOR: return Instruction::Xor;
+ return IsFP ? Instruction::FRem : Instruction::SRem;
+ case bitc::BINOP_SHL:
+ return IsFP ? -1 : Instruction::Shl;
+ case bitc::BINOP_LSHR:
+ return IsFP ? -1 : Instruction::LShr;
+ case bitc::BINOP_ASHR:
+ return IsFP ? -1 : Instruction::AShr;
+ case bitc::BINOP_AND:
+ return IsFP ? -1 : Instruction::And;
+ case bitc::BINOP_OR:
+ return IsFP ? -1 : Instruction::Or;
+ case bitc::BINOP_XOR:
+ return IsFP ? -1 : Instruction::Xor;
}
}
Index: test/Bitcode/invalid.test
===================================================================
--- test/Bitcode/invalid.test
+++ test/Bitcode/invalid.test
@@ -55,3 +55,8 @@
RUN: FileCheck --check-prefix=NO-MODULE %s
NO-MODULE: Malformed IR file
+
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-fp-shift.bc 2>&1 | \
+RUN: FileCheck --check-prefix=FP-SHIFT %s
+
+FP-SHIFT: Invalid record
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9015.23732.patch
Type: text/x-patch
Size: 2750 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150414/26961206/attachment.bin>
More information about the llvm-commits
mailing list