IRMutator.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- IRMutator.h - Mutation engine for fuzzing IR ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Provides the IRMutator class, which drives mutations on IR based on a
  15. // configurable set of strategies. Some common strategies are also included
  16. // here.
  17. //
  18. // Fuzzer-friendly (de)serialization functions are also provided, as these
  19. // are usually needed when mutating IR.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_FUZZMUTATE_IRMUTATOR_H
  23. #define LLVM_FUZZMUTATE_IRMUTATOR_H
  24. #include "llvm/FuzzMutate/OpDescriptor.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include <optional>
  27. namespace llvm {
  28. class BasicBlock;
  29. class Function;
  30. class Instruction;
  31. class Module;
  32. struct RandomIRBuilder;
  33. /// Base class for describing how to mutate a module. mutation functions for
  34. /// each IR unit forward to the contained unit.
  35. class IRMutationStrategy {
  36. public:
  37. virtual ~IRMutationStrategy() = default;
  38. /// Provide a weight to bias towards choosing this strategy for a mutation.
  39. ///
  40. /// The value of the weight is arbitrary, but a good default is "the number of
  41. /// distinct ways in which this strategy can mutate a unit". This can also be
  42. /// used to prefer strategies that shrink the overall size of the result when
  43. /// we start getting close to \c MaxSize.
  44. virtual uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  45. uint64_t CurrentWeight) = 0;
  46. /// @{
  47. /// Mutators for each IR unit. By default these forward to a contained
  48. /// instance of the next smaller unit.
  49. virtual void mutate(Module &M, RandomIRBuilder &IB);
  50. virtual void mutate(Function &F, RandomIRBuilder &IB);
  51. virtual void mutate(BasicBlock &BB, RandomIRBuilder &IB);
  52. virtual void mutate(Instruction &I, RandomIRBuilder &IB) {
  53. llvm_unreachable("Strategy does not implement any mutators");
  54. }
  55. /// @}
  56. };
  57. using TypeGetter = std::function<Type *(LLVMContext &)>;
  58. /// Entry point for configuring and running IR mutations.
  59. class IRMutator {
  60. std::vector<TypeGetter> AllowedTypes;
  61. std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
  62. public:
  63. IRMutator(std::vector<TypeGetter> &&AllowedTypes,
  64. std::vector<std::unique_ptr<IRMutationStrategy>> &&Strategies)
  65. : AllowedTypes(std::move(AllowedTypes)),
  66. Strategies(std::move(Strategies)) {}
  67. void mutateModule(Module &M, int Seed, size_t CurSize, size_t MaxSize);
  68. };
  69. /// Strategy that injects operations into the function.
  70. class InjectorIRStrategy : public IRMutationStrategy {
  71. std::vector<fuzzerop::OpDescriptor> Operations;
  72. std::optional<fuzzerop::OpDescriptor> chooseOperation(Value *Src,
  73. RandomIRBuilder &IB);
  74. public:
  75. InjectorIRStrategy(std::vector<fuzzerop::OpDescriptor> &&Operations)
  76. : Operations(std::move(Operations)) {}
  77. static std::vector<fuzzerop::OpDescriptor> getDefaultOps();
  78. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  79. uint64_t CurrentWeight) override {
  80. return Operations.size();
  81. }
  82. using IRMutationStrategy::mutate;
  83. void mutate(Function &F, RandomIRBuilder &IB) override;
  84. void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
  85. };
  86. /// Strategy that deletes instructions when the Module is too large.
  87. class InstDeleterIRStrategy : public IRMutationStrategy {
  88. public:
  89. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  90. uint64_t CurrentWeight) override;
  91. using IRMutationStrategy::mutate;
  92. void mutate(Function &F, RandomIRBuilder &IB) override;
  93. void mutate(Instruction &Inst, RandomIRBuilder &IB) override;
  94. };
  95. /// Strategy that modifies instruction attributes and operands.
  96. class InstModificationIRStrategy : public IRMutationStrategy {
  97. public:
  98. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  99. uint64_t CurrentWeight) override {
  100. return 4;
  101. }
  102. using IRMutationStrategy::mutate;
  103. void mutate(Instruction &Inst, RandomIRBuilder &IB) override;
  104. };
  105. /// Strategy to split a random block and insert a random CFG in between.
  106. class InsertCFGStrategy : public IRMutationStrategy {
  107. private:
  108. uint64_t MaxNumCases;
  109. enum CFGToSink { Return, DirectSink, SinkOrSelfLoop, EndOfCFGToLink };
  110. public:
  111. InsertCFGStrategy(uint64_t MNC = 8) : MaxNumCases(MNC){};
  112. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  113. uint64_t CurrentWeight) override {
  114. return 5;
  115. }
  116. void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
  117. private:
  118. void connectBlocksToSink(ArrayRef<BasicBlock *> Blocks, BasicBlock *Sink,
  119. RandomIRBuilder &IB);
  120. };
  121. /// Strategy to insert PHI Nodes at the head of each basic block.
  122. class InsertPHIStrategy : public IRMutationStrategy {
  123. public:
  124. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  125. uint64_t CurrentWeight) override {
  126. return 2;
  127. }
  128. void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
  129. };
  130. /// Strategy to select a random instruction and add a new sink (user) to it to
  131. /// increate data dependency.
  132. class SinkInstructionStrategy : public IRMutationStrategy {
  133. public:
  134. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  135. uint64_t CurrentWeight) override {
  136. return 2;
  137. }
  138. void mutate(Function &F, RandomIRBuilder &IB) override;
  139. void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
  140. };
  141. /// Strategy to randomly select a block and shuffle the operations without
  142. /// affecting data dependency.
  143. class ShuffleBlockStrategy : public IRMutationStrategy {
  144. public:
  145. uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
  146. uint64_t CurrentWeight) override {
  147. return 2;
  148. }
  149. void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
  150. };
  151. /// Fuzzer friendly interface for the llvm bitcode parser.
  152. ///
  153. /// \param Data Bitcode we are going to parse
  154. /// \param Size Size of the 'Data' in bytes
  155. /// \return New module or nullptr in case of error
  156. std::unique_ptr<Module> parseModule(const uint8_t *Data, size_t Size,
  157. LLVMContext &Context);
  158. /// Fuzzer friendly interface for the llvm bitcode printer.
  159. ///
  160. /// \param M Module to print
  161. /// \param Dest Location to store serialized module
  162. /// \param MaxSize Size of the destination buffer
  163. /// \return Number of bytes that were written. When module size exceeds MaxSize
  164. /// returns 0 and leaves Dest unchanged.
  165. size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize);
  166. /// Try to parse module and verify it. May output verification errors to the
  167. /// errs().
  168. /// \return New module or nullptr in case of error.
  169. std::unique_ptr<Module> parseAndVerify(const uint8_t *Data, size_t Size,
  170. LLVMContext &Context);
  171. } // namespace llvm
  172. #endif // LLVM_FUZZMUTATE_IRMUTATOR_H
  173. #ifdef __GNUC__
  174. #pragma GCC diagnostic pop
  175. #endif