LLVM  8.0.1
IRMutator.h
Go to the documentation of this file.
1 //===-- IRMutator.h - Mutation engine for fuzzing IR ------------*- C++ -*-===//
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 //
10 // Provides the IRMutator class, which drives mutations on IR based on a
11 // configurable set of strategies. Some common strategies are also included
12 // here.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_FUZZMUTATE_IRMUTATOR_H
17 #define LLVM_FUZZMUTATE_IRMUTATOR_H
18 
19 #include "llvm/ADT/Optional.h"
22 
23 namespace llvm {
24 class BasicBlock;
25 class Function;
26 class Instruction;
27 class Module;
28 
29 struct RandomIRBuilder;
30 
31 /// Base class for describing how to mutate a module. mutation functions for
32 /// each IR unit forward to the contained unit.
34 public:
35  virtual ~IRMutationStrategy() = default;
36 
37  /// Provide a weight to bias towards choosing this strategy for a mutation.
38  ///
39  /// The value of the weight is arbitrary, but a good default is "the number of
40  /// distinct ways in which this strategy can mutate a unit". This can also be
41  /// used to prefer strategies that shrink the overall size of the result when
42  /// we start getting close to \c MaxSize.
43  virtual uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
44  uint64_t CurrentWeight) = 0;
45 
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 
58 using TypeGetter = std::function<Type *(LLVMContext &)>;
59 
60 /// Entry point for configuring and running IR mutations.
61 class IRMutator {
62  std::vector<TypeGetter> AllowedTypes;
63  std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
64 
65 public:
66  IRMutator(std::vector<TypeGetter> &&AllowedTypes,
67  std::vector<std::unique_ptr<IRMutationStrategy>> &&Strategies)
68  : AllowedTypes(std::move(AllowedTypes)),
69  Strategies(std::move(Strategies)) {}
70 
71  void mutateModule(Module &M, int Seed, size_t CurSize, size_t MaxSize);
72 };
73 
74 /// Strategy that injects operations into the function.
76  std::vector<fuzzerop::OpDescriptor> Operations;
77 
78  Optional<fuzzerop::OpDescriptor> chooseOperation(Value *Src,
79  RandomIRBuilder &IB);
80 
81 public:
82  InjectorIRStrategy(std::vector<fuzzerop::OpDescriptor> &&Operations)
83  : Operations(std::move(Operations)) {}
84  static std::vector<fuzzerop::OpDescriptor> getDefaultOps();
85 
86  uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
87  uint64_t CurrentWeight) override {
88  return Operations.size();
89  }
90 
92  void mutate(Function &F, RandomIRBuilder &IB) override;
93  void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
94 };
95 
97 public:
98  uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
99  uint64_t CurrentWeight) override;
100 
102  void mutate(Function &F, RandomIRBuilder &IB) override;
103  void mutate(Instruction &Inst, RandomIRBuilder &IB) override;
104 };
105 
106 } // end llvm namespace
107 
108 #endif // LLVM_FUZZMUTATE_IRMUTATOR_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Various leaf nodes.
Definition: ISDOpcodes.h:60
virtual ~IRMutationStrategy()=default
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Base class for describing how to mutate a module.
Definition: IRMutator.h:33
F(f)
Entry point for configuring and running IR mutations.
Definition: IRMutator.h:61
Definition: BitVector.h:938
IRMutator(std::vector< TypeGetter > &&AllowedTypes, std::vector< std::unique_ptr< IRMutationStrategy >> &&Strategies)
Definition: IRMutator.h:66
InjectorIRStrategy(std::vector< fuzzerop::OpDescriptor > &&Operations)
Definition: IRMutator.h:82
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
uint64_t getWeight(size_t CurrentSize, size_t MaxSize, uint64_t CurrentWeight) override
Provide a weight to bias towards choosing this strategy for a mutation.
Definition: IRMutator.h:86
virtual void mutate(Instruction &I, RandomIRBuilder &IB)
Definition: IRMutator.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual void mutate(Module &M, RandomIRBuilder &IB)
Definition: IRMutator.cpp:36
#define I(x, y, z)
Definition: MD5.cpp:58
Strategy that injects operations into the function.
Definition: IRMutator.h:75
LLVM Value Representation.
Definition: Value.h:73
static cl::opt< unsigned long long > Seed("rng-seed", cl::value_desc("seed"), cl::Hidden, cl::desc("Seed for the random number generator"), cl::init(0))
virtual uint64_t getWeight(size_t CurrentSize, size_t MaxSize, uint64_t CurrentWeight)=0
Provide a weight to bias towards choosing this strategy for a mutation.
std::function< Type *(LLVMContext &)> TypeGetter
Definition: IRMutator.h:58