IRMutator.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //===-- IRMutator.cpp -----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/FuzzMutate/IRMutator.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallSet.h"
  11. #include "llvm/Analysis/TargetLibraryInfo.h"
  12. #include "llvm/Bitcode/BitcodeReader.h"
  13. #include "llvm/Bitcode/BitcodeWriter.h"
  14. #include "llvm/FuzzMutate/Operations.h"
  15. #include "llvm/FuzzMutate/Random.h"
  16. #include "llvm/FuzzMutate/RandomIRBuilder.h"
  17. #include "llvm/IR/BasicBlock.h"
  18. #include "llvm/IR/FMF.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/InstIterator.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/Operator.h"
  24. #include "llvm/IR/Verifier.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include "llvm/Support/SourceMgr.h"
  27. #include "llvm/Transforms/Scalar/DCE.h"
  28. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  29. #include <optional>
  30. using namespace llvm;
  31. static void createEmptyFunction(Module &M) {
  32. // TODO: Some arguments and a return value would probably be more interesting.
  33. LLVMContext &Context = M.getContext();
  34. Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Context), {},
  35. /*isVarArg=*/false),
  36. GlobalValue::ExternalLinkage, "f", &M);
  37. BasicBlock *BB = BasicBlock::Create(Context, "BB", F);
  38. ReturnInst::Create(Context, BB);
  39. }
  40. void IRMutationStrategy::mutate(Module &M, RandomIRBuilder &IB) {
  41. auto RS = makeSampler<Function *>(IB.Rand);
  42. for (Function &F : M)
  43. if (!F.isDeclaration())
  44. RS.sample(&F, /*Weight=*/1);
  45. if (RS.isEmpty())
  46. createEmptyFunction(M);
  47. else
  48. mutate(*RS.getSelection(), IB);
  49. }
  50. void IRMutationStrategy::mutate(Function &F, RandomIRBuilder &IB) {
  51. mutate(*makeSampler(IB.Rand, make_pointer_range(F)).getSelection(), IB);
  52. }
  53. void IRMutationStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  54. mutate(*makeSampler(IB.Rand, make_pointer_range(BB)).getSelection(), IB);
  55. }
  56. void IRMutator::mutateModule(Module &M, int Seed, size_t CurSize,
  57. size_t MaxSize) {
  58. std::vector<Type *> Types;
  59. for (const auto &Getter : AllowedTypes)
  60. Types.push_back(Getter(M.getContext()));
  61. RandomIRBuilder IB(Seed, Types);
  62. auto RS = makeSampler<IRMutationStrategy *>(IB.Rand);
  63. for (const auto &Strategy : Strategies)
  64. RS.sample(Strategy.get(),
  65. Strategy->getWeight(CurSize, MaxSize, RS.totalWeight()));
  66. auto Strategy = RS.getSelection();
  67. Strategy->mutate(M, IB);
  68. }
  69. static void eliminateDeadCode(Function &F) {
  70. FunctionPassManager FPM;
  71. FPM.addPass(DCEPass());
  72. FunctionAnalysisManager FAM;
  73. FAM.registerPass([&] { return TargetLibraryAnalysis(); });
  74. FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
  75. FPM.run(F, FAM);
  76. }
  77. void InjectorIRStrategy::mutate(Function &F, RandomIRBuilder &IB) {
  78. IRMutationStrategy::mutate(F, IB);
  79. eliminateDeadCode(F);
  80. }
  81. std::vector<fuzzerop::OpDescriptor> InjectorIRStrategy::getDefaultOps() {
  82. std::vector<fuzzerop::OpDescriptor> Ops;
  83. describeFuzzerIntOps(Ops);
  84. describeFuzzerFloatOps(Ops);
  85. describeFuzzerControlFlowOps(Ops);
  86. describeFuzzerPointerOps(Ops);
  87. describeFuzzerAggregateOps(Ops);
  88. describeFuzzerVectorOps(Ops);
  89. return Ops;
  90. }
  91. std::optional<fuzzerop::OpDescriptor>
  92. InjectorIRStrategy::chooseOperation(Value *Src, RandomIRBuilder &IB) {
  93. auto OpMatchesPred = [&Src](fuzzerop::OpDescriptor &Op) {
  94. return Op.SourcePreds[0].matches({}, Src);
  95. };
  96. auto RS = makeSampler(IB.Rand, make_filter_range(Operations, OpMatchesPred));
  97. if (RS.isEmpty())
  98. return std::nullopt;
  99. return *RS;
  100. }
  101. void InjectorIRStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  102. SmallVector<Instruction *, 32> Insts;
  103. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  104. Insts.push_back(&*I);
  105. if (Insts.size() < 1)
  106. return;
  107. // Choose an insertion point for our new instruction.
  108. size_t IP = uniform<size_t>(IB.Rand, 0, Insts.size() - 1);
  109. auto InstsBefore = ArrayRef(Insts).slice(0, IP);
  110. auto InstsAfter = ArrayRef(Insts).slice(IP);
  111. // Choose a source, which will be used to constrain the operation selection.
  112. SmallVector<Value *, 2> Srcs;
  113. Srcs.push_back(IB.findOrCreateSource(BB, InstsBefore));
  114. // Choose an operation that's constrained to be valid for the type of the
  115. // source, collect any other sources it needs, and then build it.
  116. auto OpDesc = chooseOperation(Srcs[0], IB);
  117. // Bail if no operation was found
  118. if (!OpDesc)
  119. return;
  120. for (const auto &Pred : ArrayRef(OpDesc->SourcePreds).slice(1))
  121. Srcs.push_back(IB.findOrCreateSource(BB, InstsBefore, Srcs, Pred));
  122. if (Value *Op = OpDesc->BuilderFunc(Srcs, Insts[IP])) {
  123. // Find a sink and wire up the results of the operation.
  124. IB.connectToSink(BB, InstsAfter, Op);
  125. }
  126. }
  127. uint64_t InstDeleterIRStrategy::getWeight(size_t CurrentSize, size_t MaxSize,
  128. uint64_t CurrentWeight) {
  129. // If we have less than 200 bytes, panic and try to always delete.
  130. if (CurrentSize > MaxSize - 200)
  131. return CurrentWeight ? CurrentWeight * 100 : 1;
  132. // Draw a line starting from when we only have 1k left and increasing linearly
  133. // to double the current weight.
  134. int64_t Line = (-2 * static_cast<int64_t>(CurrentWeight)) *
  135. (static_cast<int64_t>(MaxSize) -
  136. static_cast<int64_t>(CurrentSize) - 1000) /
  137. 1000;
  138. // Clamp negative weights to zero.
  139. if (Line < 0)
  140. return 0;
  141. return Line;
  142. }
  143. void InstDeleterIRStrategy::mutate(Function &F, RandomIRBuilder &IB) {
  144. auto RS = makeSampler<Instruction *>(IB.Rand);
  145. for (Instruction &Inst : instructions(F)) {
  146. // TODO: We can't handle these instructions.
  147. if (Inst.isTerminator() || Inst.isEHPad() || Inst.isSwiftError() ||
  148. isa<PHINode>(Inst))
  149. continue;
  150. RS.sample(&Inst, /*Weight=*/1);
  151. }
  152. if (RS.isEmpty())
  153. return;
  154. // Delete the instruction.
  155. mutate(*RS.getSelection(), IB);
  156. // Clean up any dead code that's left over after removing the instruction.
  157. eliminateDeadCode(F);
  158. }
  159. void InstDeleterIRStrategy::mutate(Instruction &Inst, RandomIRBuilder &IB) {
  160. assert(!Inst.isTerminator() && "Deleting terminators invalidates CFG");
  161. if (Inst.getType()->isVoidTy()) {
  162. // Instructions with void type (ie, store) have no uses to worry about. Just
  163. // erase it and move on.
  164. Inst.eraseFromParent();
  165. return;
  166. }
  167. // Otherwise we need to find some other value with the right type to keep the
  168. // users happy.
  169. auto Pred = fuzzerop::onlyType(Inst.getType());
  170. auto RS = makeSampler<Value *>(IB.Rand);
  171. SmallVector<Instruction *, 32> InstsBefore;
  172. BasicBlock *BB = Inst.getParent();
  173. for (auto I = BB->getFirstInsertionPt(), E = Inst.getIterator(); I != E;
  174. ++I) {
  175. if (Pred.matches({}, &*I))
  176. RS.sample(&*I, /*Weight=*/1);
  177. InstsBefore.push_back(&*I);
  178. }
  179. if (!RS)
  180. RS.sample(IB.newSource(*BB, InstsBefore, {}, Pred), /*Weight=*/1);
  181. Inst.replaceAllUsesWith(RS.getSelection());
  182. Inst.eraseFromParent();
  183. }
  184. void InstModificationIRStrategy::mutate(Instruction &Inst,
  185. RandomIRBuilder &IB) {
  186. SmallVector<std::function<void()>, 8> Modifications;
  187. CmpInst *CI = nullptr;
  188. GetElementPtrInst *GEP = nullptr;
  189. switch (Inst.getOpcode()) {
  190. default:
  191. break;
  192. // Add nsw, nuw flag
  193. case Instruction::Add:
  194. case Instruction::Mul:
  195. case Instruction::Sub:
  196. case Instruction::Shl:
  197. Modifications.push_back(
  198. [&Inst]() { Inst.setHasNoSignedWrap(!Inst.hasNoSignedWrap()); });
  199. Modifications.push_back(
  200. [&Inst]() { Inst.setHasNoUnsignedWrap(!Inst.hasNoUnsignedWrap()); });
  201. break;
  202. case Instruction::ICmp:
  203. CI = cast<ICmpInst>(&Inst);
  204. for (unsigned p = CmpInst::FIRST_ICMP_PREDICATE;
  205. p <= CmpInst::LAST_ICMP_PREDICATE; p++) {
  206. Modifications.push_back(
  207. [CI, p]() { CI->setPredicate(static_cast<CmpInst::Predicate>(p)); });
  208. }
  209. break;
  210. // Add inbound flag.
  211. case Instruction::GetElementPtr:
  212. GEP = cast<GetElementPtrInst>(&Inst);
  213. Modifications.push_back(
  214. [GEP]() { GEP->setIsInBounds(!GEP->isInBounds()); });
  215. break;
  216. // Add exact flag.
  217. case Instruction::UDiv:
  218. case Instruction::SDiv:
  219. case Instruction::LShr:
  220. case Instruction::AShr:
  221. Modifications.push_back([&Inst] { Inst.setIsExact(!Inst.isExact()); });
  222. break;
  223. case Instruction::FCmp:
  224. CI = cast<ICmpInst>(&Inst);
  225. for (unsigned p = CmpInst::FIRST_FCMP_PREDICATE;
  226. p <= CmpInst::LAST_FCMP_PREDICATE; p++) {
  227. Modifications.push_back(
  228. [CI, p]() { CI->setPredicate(static_cast<CmpInst::Predicate>(p)); });
  229. }
  230. break;
  231. }
  232. // Add fast math flag if possible.
  233. if (isa<FPMathOperator>(&Inst)) {
  234. // Try setting everything unless they are already on.
  235. Modifications.push_back(
  236. [&Inst] { Inst.setFast(!Inst.getFastMathFlags().all()); });
  237. // Try unsetting everything unless they are already off.
  238. Modifications.push_back(
  239. [&Inst] { Inst.setFast(!Inst.getFastMathFlags().none()); });
  240. // Individual setting by flipping the bit
  241. Modifications.push_back(
  242. [&Inst] { Inst.setHasAllowReassoc(!Inst.hasAllowReassoc()); });
  243. Modifications.push_back([&Inst] { Inst.setHasNoNaNs(!Inst.hasNoNaNs()); });
  244. Modifications.push_back([&Inst] { Inst.setHasNoInfs(!Inst.hasNoInfs()); });
  245. Modifications.push_back(
  246. [&Inst] { Inst.setHasNoSignedZeros(!Inst.hasNoSignedZeros()); });
  247. Modifications.push_back(
  248. [&Inst] { Inst.setHasAllowReciprocal(!Inst.hasAllowReciprocal()); });
  249. Modifications.push_back(
  250. [&Inst] { Inst.setHasAllowContract(!Inst.hasAllowContract()); });
  251. Modifications.push_back(
  252. [&Inst] { Inst.setHasApproxFunc(!Inst.hasApproxFunc()); });
  253. }
  254. // Randomly switch operands of instructions
  255. std::pair<int, int> NoneItem({-1, -1}), ShuffleItems(NoneItem);
  256. switch (Inst.getOpcode()) {
  257. case Instruction::SDiv:
  258. case Instruction::UDiv:
  259. case Instruction::SRem:
  260. case Instruction::URem:
  261. case Instruction::FDiv:
  262. case Instruction::FRem: {
  263. // Verify that the after shuffle the second operand is not
  264. // constant 0.
  265. Value *Operand = Inst.getOperand(0);
  266. if (Constant *C = dyn_cast<Constant>(Operand)) {
  267. if (!C->isZeroValue()) {
  268. ShuffleItems = {0, 1};
  269. }
  270. }
  271. break;
  272. }
  273. case Instruction::Select:
  274. ShuffleItems = {1, 2};
  275. break;
  276. case Instruction::Add:
  277. case Instruction::Sub:
  278. case Instruction::Mul:
  279. case Instruction::Shl:
  280. case Instruction::LShr:
  281. case Instruction::AShr:
  282. case Instruction::And:
  283. case Instruction::Or:
  284. case Instruction::Xor:
  285. case Instruction::FAdd:
  286. case Instruction::FSub:
  287. case Instruction::FMul:
  288. case Instruction::ICmp:
  289. case Instruction::FCmp:
  290. case Instruction::ShuffleVector:
  291. ShuffleItems = {0, 1};
  292. break;
  293. }
  294. if (ShuffleItems != NoneItem) {
  295. Modifications.push_back([&Inst, &ShuffleItems]() {
  296. Value *Op0 = Inst.getOperand(ShuffleItems.first);
  297. Inst.setOperand(ShuffleItems.first, Inst.getOperand(ShuffleItems.second));
  298. Inst.setOperand(ShuffleItems.second, Op0);
  299. });
  300. }
  301. auto RS = makeSampler(IB.Rand, Modifications);
  302. if (RS)
  303. RS.getSelection()();
  304. }
  305. /// Return a case value that is not already taken to make sure we don't have two
  306. /// cases with same value.
  307. static uint64_t getUniqueCaseValue(SmallSet<uint64_t, 4> &CasesTaken,
  308. uint64_t MaxValue, RandomIRBuilder &IB) {
  309. uint64_t tmp;
  310. do {
  311. tmp = uniform<uint64_t>(IB.Rand, 0, MaxValue);
  312. } while (CasesTaken.count(tmp) != 0);
  313. CasesTaken.insert(tmp);
  314. return tmp;
  315. }
  316. void InsertCFGStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  317. SmallVector<Instruction *, 32> Insts;
  318. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  319. Insts.push_back(&*I);
  320. if (Insts.size() < 1)
  321. return;
  322. // Choose a point where we split the block.
  323. uint64_t IP = uniform<uint64_t>(IB.Rand, 0, Insts.size() - 1);
  324. auto InstsBeforeSplit = ArrayRef(Insts).slice(0, IP);
  325. // `Sink` inherits Blocks' terminator, `Source` will have a BranchInst
  326. // directly jumps to `Sink`. Here, we have to create a new terminator for
  327. // `Source`.
  328. BasicBlock *Block = Insts[IP]->getParent();
  329. BasicBlock *Source = Block;
  330. BasicBlock *Sink = Block->splitBasicBlock(Insts[IP], "BB");
  331. Function *F = BB.getParent();
  332. LLVMContext &C = F->getParent()->getContext();
  333. // A coin decides if it is branch or switch
  334. if (uniform<uint64_t>(IB.Rand, 0, 1)) {
  335. // Branch
  336. BasicBlock *IfTrue = BasicBlock::Create(C, "T", F);
  337. BasicBlock *IfFalse = BasicBlock::Create(C, "F", F);
  338. Value *Cond =
  339. IB.findOrCreateSource(*Source, InstsBeforeSplit, {},
  340. fuzzerop::onlyType(Type::getInt1Ty(C)), false);
  341. BranchInst *Branch = BranchInst::Create(IfTrue, IfFalse, Cond);
  342. // Remove the old terminator.
  343. ReplaceInstWithInst(Source->getTerminator(), Branch);
  344. // Connect these blocks to `Sink`
  345. connectBlocksToSink({IfTrue, IfFalse}, Sink, IB);
  346. } else {
  347. // Switch
  348. // Determine Integer type, it IS possible we use a boolean to switch.
  349. auto RS =
  350. makeSampler(IB.Rand, make_filter_range(IB.KnownTypes, [](Type *Ty) {
  351. return Ty->isIntegerTy();
  352. }));
  353. assert(RS && "There is no integer type in all allowed types, is the "
  354. "setting correct?");
  355. Type *Ty = RS.getSelection();
  356. IntegerType *IntTy = cast<IntegerType>(Ty);
  357. uint64_t BitSize = IntTy->getBitWidth();
  358. uint64_t MaxCaseVal =
  359. (BitSize >= 64) ? (uint64_t)-1 : ((uint64_t)1 << BitSize) - 1;
  360. // Create Switch inst in Block
  361. Value *Cond = IB.findOrCreateSource(*Source, InstsBeforeSplit, {},
  362. fuzzerop::onlyType(IntTy), false);
  363. BasicBlock *DefaultBlock = BasicBlock::Create(C, "SW_D", F);
  364. uint64_t NumCases = uniform<uint64_t>(IB.Rand, 1, MaxNumCases);
  365. NumCases = (NumCases > MaxCaseVal) ? MaxCaseVal + 1 : NumCases;
  366. SwitchInst *Switch = SwitchInst::Create(Cond, DefaultBlock, NumCases);
  367. // Remove the old terminator.
  368. ReplaceInstWithInst(Source->getTerminator(), Switch);
  369. // Create blocks, for each block assign a case value.
  370. SmallVector<BasicBlock *, 4> Blocks({DefaultBlock});
  371. SmallSet<uint64_t, 4> CasesTaken;
  372. for (uint64_t i = 0; i < NumCases; i++) {
  373. uint64_t CaseVal = getUniqueCaseValue(CasesTaken, MaxCaseVal, IB);
  374. BasicBlock *CaseBlock = BasicBlock::Create(C, "SW_C", F);
  375. ConstantInt *OnValue = ConstantInt::get(IntTy, CaseVal);
  376. Switch->addCase(OnValue, CaseBlock);
  377. Blocks.push_back(CaseBlock);
  378. }
  379. // Connect these blocks to `Sink`
  380. connectBlocksToSink(Blocks, Sink, IB);
  381. }
  382. }
  383. /// The caller has to guarantee that these blocks are "empty", i.e. it doesn't
  384. /// even have terminator.
  385. void InsertCFGStrategy::connectBlocksToSink(ArrayRef<BasicBlock *> Blocks,
  386. BasicBlock *Sink,
  387. RandomIRBuilder &IB) {
  388. uint64_t DirectSinkIdx = uniform<uint64_t>(IB.Rand, 0, Blocks.size() - 1);
  389. for (uint64_t i = 0; i < Blocks.size(); i++) {
  390. // We have at least one block that directly goes to sink.
  391. CFGToSink ToSink = (i == DirectSinkIdx)
  392. ? CFGToSink::DirectSink
  393. : static_cast<CFGToSink>(uniform<uint64_t>(
  394. IB.Rand, 0, CFGToSink::EndOfCFGToLink - 1));
  395. BasicBlock *BB = Blocks[i];
  396. Function *F = BB->getParent();
  397. LLVMContext &C = F->getParent()->getContext();
  398. switch (ToSink) {
  399. case CFGToSink::Return: {
  400. Type *RetTy = F->getReturnType();
  401. Value *RetValue = nullptr;
  402. if (!RetTy->isVoidTy())
  403. RetValue =
  404. IB.findOrCreateSource(*BB, {}, {}, fuzzerop::onlyType(RetTy));
  405. ReturnInst::Create(C, RetValue, BB);
  406. break;
  407. }
  408. case CFGToSink::DirectSink: {
  409. BranchInst::Create(Sink, BB);
  410. break;
  411. }
  412. case CFGToSink::SinkOrSelfLoop: {
  413. SmallVector<BasicBlock *, 2> Branches({Sink, BB});
  414. // A coin decides which block is true branch.
  415. uint64_t coin = uniform<uint64_t>(IB.Rand, 0, 1);
  416. Value *Cond = IB.findOrCreateSource(
  417. *BB, {}, {}, fuzzerop::onlyType(Type::getInt1Ty(C)), false);
  418. BranchInst::Create(Branches[coin], Branches[1 - coin], Cond, BB);
  419. break;
  420. }
  421. case CFGToSink::EndOfCFGToLink:
  422. llvm_unreachable("EndOfCFGToLink executed, something's wrong.");
  423. }
  424. }
  425. }
  426. void InsertPHIStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  427. // Can't insert PHI node to entry node.
  428. if (&BB == &BB.getParent()->getEntryBlock())
  429. return;
  430. Type *Ty = IB.randomType();
  431. PHINode *PHI = PHINode::Create(Ty, llvm::pred_size(&BB), "", &BB.front());
  432. // Use a map to make sure the same incoming basic block has the same value.
  433. DenseMap<BasicBlock *, Value *> IncomingValues;
  434. for (BasicBlock *Pred : predecessors(&BB)) {
  435. Value *Src = IncomingValues[Pred];
  436. // If `Pred` is not in the map yet, we'll get a nullptr.
  437. if (!Src) {
  438. SmallVector<Instruction *, 32> Insts;
  439. for (auto I = Pred->begin(); I != Pred->end(); ++I)
  440. Insts.push_back(&*I);
  441. // There is no need to inform IB what previously used values are if we are
  442. // using `onlyType`
  443. Src = IB.findOrCreateSource(*Pred, Insts, {}, fuzzerop::onlyType(Ty));
  444. IncomingValues[Pred] = Src;
  445. }
  446. PHI->addIncoming(Src, Pred);
  447. }
  448. SmallVector<Instruction *, 32> InstsAfter;
  449. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  450. InstsAfter.push_back(&*I);
  451. IB.connectToSink(BB, InstsAfter, PHI);
  452. }
  453. void SinkInstructionStrategy::mutate(Function &F, RandomIRBuilder &IB) {
  454. for (BasicBlock &BB : F) {
  455. this->mutate(BB, IB);
  456. }
  457. }
  458. void SinkInstructionStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  459. SmallVector<Instruction *, 32> Insts;
  460. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  461. Insts.push_back(&*I);
  462. if (Insts.size() < 1)
  463. return;
  464. // Choose an Instruction to mutate.
  465. uint64_t Idx = uniform<uint64_t>(IB.Rand, 0, Insts.size() - 1);
  466. Instruction *Inst = Insts[Idx];
  467. // `Idx + 1` so we don't sink to ourselves.
  468. auto InstsAfter = ArrayRef(Insts).slice(Idx + 1);
  469. LLVMContext &C = BB.getParent()->getParent()->getContext();
  470. // Don't sink terminators, void function calls, etc.
  471. if (Inst->getType() != Type::getVoidTy(C))
  472. // Find a new sink and wire up the results of the operation.
  473. IB.connectToSink(BB, InstsAfter, Inst);
  474. }
  475. void ShuffleBlockStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
  476. SmallPtrSet<Instruction *, 8> AliveInsts;
  477. for (auto &I : make_early_inc_range(make_range(
  478. BB.getFirstInsertionPt(), BB.getTerminator()->getIterator()))) {
  479. // First gather all instructions that can be shuffled. Don't take
  480. // terminator.
  481. AliveInsts.insert(&I);
  482. // Then remove these instructions from the block
  483. I.removeFromParent();
  484. }
  485. // Shuffle these instructions using topological sort.
  486. // Returns true if all current instruction's dependencies in this block have
  487. // been shuffled. If so, this instruction can be shuffled too.
  488. auto hasAliveParent = [&AliveInsts](Instruction *I) {
  489. for (Value *O : I->operands()) {
  490. Instruction *P = dyn_cast<Instruction>(O);
  491. if (P && AliveInsts.count(P))
  492. return true;
  493. }
  494. return false;
  495. };
  496. // Get all alive instructions that depend on the current instruction.
  497. auto getAliveChildren = [&AliveInsts](Instruction *I) {
  498. SmallPtrSet<Instruction *, 4> Children;
  499. for (Value *U : I->users()) {
  500. Instruction *P = dyn_cast<Instruction>(U);
  501. if (P && AliveInsts.count(P))
  502. Children.insert(P);
  503. }
  504. return Children;
  505. };
  506. SmallPtrSet<Instruction *, 8> Roots;
  507. SmallVector<Instruction *, 8> Insts;
  508. for (Instruction *I : AliveInsts) {
  509. if (!hasAliveParent(I))
  510. Roots.insert(I);
  511. }
  512. // Topological sort by randomly selecting a node without a parent, or root.
  513. while (!Roots.empty()) {
  514. auto RS = makeSampler<Instruction *>(IB.Rand);
  515. for (Instruction *Root : Roots)
  516. RS.sample(Root, 1);
  517. Instruction *Root = RS.getSelection();
  518. Roots.erase(Root);
  519. AliveInsts.erase(Root);
  520. Insts.push_back(Root);
  521. for (Instruction *Child : getAliveChildren(Root)) {
  522. if (!hasAliveParent(Child)) {
  523. Roots.insert(Child);
  524. }
  525. }
  526. }
  527. Instruction *Terminator = BB.getTerminator();
  528. // Then put instructions back.
  529. for (Instruction *I : Insts) {
  530. I->insertBefore(Terminator);
  531. }
  532. }
  533. std::unique_ptr<Module> llvm::parseModule(const uint8_t *Data, size_t Size,
  534. LLVMContext &Context) {
  535. if (Size <= 1)
  536. // We get bogus data given an empty corpus - just create a new module.
  537. return std::make_unique<Module>("M", Context);
  538. auto Buffer = MemoryBuffer::getMemBuffer(
  539. StringRef(reinterpret_cast<const char *>(Data), Size), "Fuzzer input",
  540. /*RequiresNullTerminator=*/false);
  541. SMDiagnostic Err;
  542. auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
  543. if (Error E = M.takeError()) {
  544. errs() << toString(std::move(E)) << "\n";
  545. return nullptr;
  546. }
  547. return std::move(M.get());
  548. }
  549. size_t llvm::writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
  550. std::string Buf;
  551. {
  552. raw_string_ostream OS(Buf);
  553. WriteBitcodeToFile(M, OS);
  554. }
  555. if (Buf.size() > MaxSize)
  556. return 0;
  557. memcpy(Dest, Buf.data(), Buf.size());
  558. return Buf.size();
  559. }
  560. std::unique_ptr<Module> llvm::parseAndVerify(const uint8_t *Data, size_t Size,
  561. LLVMContext &Context) {
  562. auto M = parseModule(Data, Size, Context);
  563. if (!M || verifyModule(*M, &errs()))
  564. return nullptr;
  565. return M;
  566. }