Operations.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //===-- Operations.cpp ----------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/FuzzMutate/Operations.h"
  10. #include "llvm/IR/BasicBlock.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/Instructions.h"
  14. using namespace llvm;
  15. using namespace fuzzerop;
  16. void llvm::describeFuzzerIntOps(std::vector<fuzzerop::OpDescriptor> &Ops) {
  17. Ops.push_back(binOpDescriptor(1, Instruction::Add));
  18. Ops.push_back(binOpDescriptor(1, Instruction::Sub));
  19. Ops.push_back(binOpDescriptor(1, Instruction::Mul));
  20. Ops.push_back(binOpDescriptor(1, Instruction::SDiv));
  21. Ops.push_back(binOpDescriptor(1, Instruction::UDiv));
  22. Ops.push_back(binOpDescriptor(1, Instruction::SRem));
  23. Ops.push_back(binOpDescriptor(1, Instruction::URem));
  24. Ops.push_back(binOpDescriptor(1, Instruction::Shl));
  25. Ops.push_back(binOpDescriptor(1, Instruction::LShr));
  26. Ops.push_back(binOpDescriptor(1, Instruction::AShr));
  27. Ops.push_back(binOpDescriptor(1, Instruction::And));
  28. Ops.push_back(binOpDescriptor(1, Instruction::Or));
  29. Ops.push_back(binOpDescriptor(1, Instruction::Xor));
  30. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_EQ));
  31. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_NE));
  32. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_UGT));
  33. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_UGE));
  34. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_ULT));
  35. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_ULE));
  36. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_SGT));
  37. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_SGE));
  38. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_SLT));
  39. Ops.push_back(cmpOpDescriptor(1, Instruction::ICmp, CmpInst::ICMP_SLE));
  40. }
  41. void llvm::describeFuzzerFloatOps(std::vector<fuzzerop::OpDescriptor> &Ops) {
  42. Ops.push_back(binOpDescriptor(1, Instruction::FAdd));
  43. Ops.push_back(binOpDescriptor(1, Instruction::FSub));
  44. Ops.push_back(binOpDescriptor(1, Instruction::FMul));
  45. Ops.push_back(binOpDescriptor(1, Instruction::FDiv));
  46. Ops.push_back(binOpDescriptor(1, Instruction::FRem));
  47. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_FALSE));
  48. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_OEQ));
  49. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_OGT));
  50. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_OGE));
  51. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_OLT));
  52. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_OLE));
  53. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_ONE));
  54. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_ORD));
  55. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_UNO));
  56. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_UEQ));
  57. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_UGT));
  58. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_UGE));
  59. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_ULT));
  60. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_ULE));
  61. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_UNE));
  62. Ops.push_back(cmpOpDescriptor(1, Instruction::FCmp, CmpInst::FCMP_TRUE));
  63. }
  64. void llvm::describeFuzzerControlFlowOps(
  65. std::vector<fuzzerop::OpDescriptor> &Ops) {
  66. Ops.push_back(splitBlockDescriptor(1));
  67. }
  68. void llvm::describeFuzzerPointerOps(std::vector<fuzzerop::OpDescriptor> &Ops) {
  69. Ops.push_back(gepDescriptor(1));
  70. }
  71. void llvm::describeFuzzerAggregateOps(
  72. std::vector<fuzzerop::OpDescriptor> &Ops) {
  73. Ops.push_back(extractValueDescriptor(1));
  74. Ops.push_back(insertValueDescriptor(1));
  75. }
  76. void llvm::describeFuzzerVectorOps(std::vector<fuzzerop::OpDescriptor> &Ops) {
  77. Ops.push_back(extractElementDescriptor(1));
  78. Ops.push_back(insertElementDescriptor(1));
  79. Ops.push_back(shuffleVectorDescriptor(1));
  80. }
  81. OpDescriptor llvm::fuzzerop::binOpDescriptor(unsigned Weight,
  82. Instruction::BinaryOps Op) {
  83. auto buildOp = [Op](ArrayRef<Value *> Srcs, Instruction *Inst) {
  84. return BinaryOperator::Create(Op, Srcs[0], Srcs[1], "B", Inst);
  85. };
  86. switch (Op) {
  87. case Instruction::Add:
  88. case Instruction::Sub:
  89. case Instruction::Mul:
  90. case Instruction::SDiv:
  91. case Instruction::UDiv:
  92. case Instruction::SRem:
  93. case Instruction::URem:
  94. case Instruction::Shl:
  95. case Instruction::LShr:
  96. case Instruction::AShr:
  97. case Instruction::And:
  98. case Instruction::Or:
  99. case Instruction::Xor:
  100. return {Weight, {anyIntType(), matchFirstType()}, buildOp};
  101. case Instruction::FAdd:
  102. case Instruction::FSub:
  103. case Instruction::FMul:
  104. case Instruction::FDiv:
  105. case Instruction::FRem:
  106. return {Weight, {anyFloatType(), matchFirstType()}, buildOp};
  107. case Instruction::BinaryOpsEnd:
  108. llvm_unreachable("Value out of range of enum");
  109. }
  110. llvm_unreachable("Covered switch");
  111. }
  112. OpDescriptor llvm::fuzzerop::cmpOpDescriptor(unsigned Weight,
  113. Instruction::OtherOps CmpOp,
  114. CmpInst::Predicate Pred) {
  115. auto buildOp = [CmpOp, Pred](ArrayRef<Value *> Srcs, Instruction *Inst) {
  116. return CmpInst::Create(CmpOp, Pred, Srcs[0], Srcs[1], "C", Inst);
  117. };
  118. switch (CmpOp) {
  119. case Instruction::ICmp:
  120. return {Weight, {anyIntType(), matchFirstType()}, buildOp};
  121. case Instruction::FCmp:
  122. return {Weight, {anyFloatType(), matchFirstType()}, buildOp};
  123. default:
  124. llvm_unreachable("CmpOp must be ICmp or FCmp");
  125. }
  126. }
  127. OpDescriptor llvm::fuzzerop::splitBlockDescriptor(unsigned Weight) {
  128. auto buildSplitBlock = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  129. BasicBlock *Block = Inst->getParent();
  130. BasicBlock *Next = Block->splitBasicBlock(Inst, "BB");
  131. if (Block != &Block->getParent()->getEntryBlock()) {
  132. // Loop back on this block by replacing the unconditional forward branch
  133. // with a conditional with a backedge.
  134. BranchInst::Create(Block, Next, Srcs[0], Block->getTerminator());
  135. Block->getTerminator()->eraseFromParent();
  136. // We need values for each phi in the block. Since there isn't a good way
  137. // to do a variable number of input values currently, we just fill them
  138. // with undef.
  139. for (PHINode &PHI : Block->phis())
  140. PHI.addIncoming(UndefValue::get(PHI.getType()), Block);
  141. }
  142. return nullptr;
  143. };
  144. SourcePred isInt1Ty{[](ArrayRef<Value *>, const Value *V) {
  145. return V->getType()->isIntegerTy(1);
  146. },
  147. None};
  148. return {Weight, {isInt1Ty}, buildSplitBlock};
  149. }
  150. OpDescriptor llvm::fuzzerop::gepDescriptor(unsigned Weight) {
  151. auto buildGEP = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  152. Type *Ty = cast<PointerType>(Srcs[0]->getType())->getElementType();
  153. auto Indices = makeArrayRef(Srcs).drop_front(1);
  154. return GetElementPtrInst::Create(Ty, Srcs[0], Indices, "G", Inst);
  155. };
  156. // TODO: Handle aggregates and vectors
  157. // TODO: Support multiple indices.
  158. // TODO: Try to avoid meaningless accesses.
  159. return {Weight, {anyPtrType(), anyIntType()}, buildGEP};
  160. }
  161. static uint64_t getAggregateNumElements(Type *T) {
  162. assert(T->isAggregateType() && "Not a struct or array");
  163. if (isa<StructType>(T))
  164. return T->getStructNumElements();
  165. return T->getArrayNumElements();
  166. }
  167. static SourcePred validExtractValueIndex() {
  168. auto Pred = [](ArrayRef<Value *> Cur, const Value *V) {
  169. if (auto *CI = dyn_cast<ConstantInt>(V))
  170. if (!CI->uge(getAggregateNumElements(Cur[0]->getType())))
  171. return true;
  172. return false;
  173. };
  174. auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *> Ts) {
  175. std::vector<Constant *> Result;
  176. auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext());
  177. uint64_t N = getAggregateNumElements(Cur[0]->getType());
  178. // Create indices at the start, end, and middle, but avoid dups.
  179. Result.push_back(ConstantInt::get(Int32Ty, 0));
  180. if (N > 1)
  181. Result.push_back(ConstantInt::get(Int32Ty, N - 1));
  182. if (N > 2)
  183. Result.push_back(ConstantInt::get(Int32Ty, N / 2));
  184. return Result;
  185. };
  186. return {Pred, Make};
  187. }
  188. OpDescriptor llvm::fuzzerop::extractValueDescriptor(unsigned Weight) {
  189. auto buildExtract = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  190. // TODO: It's pretty inefficient to shuffle this all through constants.
  191. unsigned Idx = cast<ConstantInt>(Srcs[1])->getZExtValue();
  192. return ExtractValueInst::Create(Srcs[0], {Idx}, "E", Inst);
  193. };
  194. // TODO: Should we handle multiple indices?
  195. return {Weight, {anyAggregateType(), validExtractValueIndex()}, buildExtract};
  196. }
  197. static SourcePred matchScalarInAggregate() {
  198. auto Pred = [](ArrayRef<Value *> Cur, const Value *V) {
  199. if (isa<ArrayType>(Cur[0]->getType()))
  200. return V->getType() == Cur[0]->getType();
  201. auto *STy = cast<StructType>(Cur[0]->getType());
  202. for (int I = 0, E = STy->getNumElements(); I < E; ++I)
  203. if (STy->getTypeAtIndex(I) == V->getType())
  204. return true;
  205. return false;
  206. };
  207. auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *>) {
  208. if (isa<ArrayType>(Cur[0]->getType()))
  209. return makeConstantsWithType(Cur[0]->getType());
  210. std::vector<Constant *> Result;
  211. auto *STy = cast<StructType>(Cur[0]->getType());
  212. for (int I = 0, E = STy->getNumElements(); I < E; ++I)
  213. makeConstantsWithType(STy->getTypeAtIndex(I), Result);
  214. return Result;
  215. };
  216. return {Pred, Make};
  217. }
  218. static SourcePred validInsertValueIndex() {
  219. auto Pred = [](ArrayRef<Value *> Cur, const Value *V) {
  220. auto *CTy = cast<CompositeType>(Cur[0]->getType());
  221. if (auto *CI = dyn_cast<ConstantInt>(V))
  222. if (CI->getBitWidth() == 32 &&
  223. CTy->getTypeAtIndex(CI->getZExtValue()) == Cur[1]->getType())
  224. return true;
  225. return false;
  226. };
  227. auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *> Ts) {
  228. std::vector<Constant *> Result;
  229. auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext());
  230. auto *CTy = cast<CompositeType>(Cur[0]->getType());
  231. for (int I = 0, E = getAggregateNumElements(CTy); I < E; ++I)
  232. if (CTy->getTypeAtIndex(I) == Cur[1]->getType())
  233. Result.push_back(ConstantInt::get(Int32Ty, I));
  234. return Result;
  235. };
  236. return {Pred, Make};
  237. }
  238. OpDescriptor llvm::fuzzerop::insertValueDescriptor(unsigned Weight) {
  239. auto buildInsert = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  240. // TODO: It's pretty inefficient to shuffle this all through constants.
  241. unsigned Idx = cast<ConstantInt>(Srcs[2])->getZExtValue();
  242. return InsertValueInst::Create(Srcs[0], Srcs[1], {Idx}, "I", Inst);
  243. };
  244. return {
  245. Weight,
  246. {anyAggregateType(), matchScalarInAggregate(), validInsertValueIndex()},
  247. buildInsert};
  248. }
  249. OpDescriptor llvm::fuzzerop::extractElementDescriptor(unsigned Weight) {
  250. auto buildExtract = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  251. return ExtractElementInst::Create(Srcs[0], Srcs[1], "E", Inst);
  252. };
  253. // TODO: Try to avoid undefined accesses.
  254. return {Weight, {anyVectorType(), anyIntType()}, buildExtract};
  255. }
  256. OpDescriptor llvm::fuzzerop::insertElementDescriptor(unsigned Weight) {
  257. auto buildInsert = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  258. return InsertElementInst::Create(Srcs[0], Srcs[1], Srcs[2], "I", Inst);
  259. };
  260. // TODO: Try to avoid undefined accesses.
  261. return {Weight,
  262. {anyVectorType(), matchScalarOfFirstType(), anyIntType()},
  263. buildInsert};
  264. }
  265. static SourcePred validShuffleVectorIndex() {
  266. auto Pred = [](ArrayRef<Value *> Cur, const Value *V) {
  267. return ShuffleVectorInst::isValidOperands(Cur[0], Cur[1], V);
  268. };
  269. auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *> Ts) {
  270. auto *FirstTy = cast<VectorType>(Cur[0]->getType());
  271. auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext());
  272. // TODO: It's straighforward to make up reasonable values, but listing them
  273. // exhaustively would be insane. Come up with a couple of sensible ones.
  274. return std::vector<Constant *>{
  275. UndefValue::get(VectorType::get(Int32Ty, FirstTy->getNumElements()))};
  276. };
  277. return {Pred, Make};
  278. }
  279. OpDescriptor llvm::fuzzerop::shuffleVectorDescriptor(unsigned Weight) {
  280. auto buildShuffle = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
  281. return new ShuffleVectorInst(Srcs[0], Srcs[1], Srcs[2], "S", Inst);
  282. };
  283. return {Weight,
  284. {anyVectorType(), matchFirstType(), validShuffleVectorIndex()},
  285. buildShuffle};
  286. }