annotate include/llvm/FuzzMutate/IRMutator.h @ 125:56c5119fbcd2

fix
author mir3636
date Sun, 03 Dec 2017 20:09:16 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===-- IRMutator.h - Mutation engine for fuzzing IR ------------*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // Provides the IRMutator class, which drives mutations on IR based on a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // configurable set of strategies. Some common strategies are also included
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 // here.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #ifndef LLVM_FUZZMUTATE_IRMUTATOR_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #define LLVM_FUZZMUTATE_IRMUTATOR_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/FuzzMutate/OpDescriptor.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/Support/ErrorHandling.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 class BasicBlock;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 class Function;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 class Instruction;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 class Module;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 struct RandomIRBuilder;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 /// Base class for describing how to mutate a module. mutation functions for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 /// each IR unit forward to the contained unit.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 class IRMutationStrategy {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 virtual ~IRMutationStrategy() = default;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 /// Provide a weight to bias towards choosing this strategy for a mutation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 /// The value of the weight is arbitrary, but a good default is "the number of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 /// distinct ways in which this strategy can mutate a unit". This can also be
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 /// used to prefer strategies that shrink the overall size of the result when
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 /// we start getting close to \c MaxSize.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 virtual uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 uint64_t CurrentWeight) = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 /// @{
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 /// Mutators for each IR unit. By default these forward to a contained
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 /// instance of the next smaller unit.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 virtual void mutate(Module &M, RandomIRBuilder &IB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 virtual void mutate(Function &F, RandomIRBuilder &IB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 virtual void mutate(BasicBlock &BB, RandomIRBuilder &IB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 virtual void mutate(Instruction &I, RandomIRBuilder &IB) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 llvm_unreachable("Strategy does not implement any mutators");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 /// @}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 using TypeGetter = std::function<Type *(LLVMContext &)>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 /// Entry point for configuring and running IR mutations.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 class IRMutator {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 std::vector<TypeGetter> AllowedTypes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 IRMutator(std::vector<TypeGetter> &&AllowedTypes,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 std::vector<std::unique_ptr<IRMutationStrategy>> &&Strategies)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 : AllowedTypes(std::move(AllowedTypes)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 Strategies(std::move(Strategies)) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 void mutateModule(Module &M, int Seed, size_t CurSize, size_t MaxSize);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 /// Strategy that injects operations into the function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 class InjectorIRStrategy : public IRMutationStrategy {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 std::vector<fuzzerop::OpDescriptor> Operations;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 fuzzerop::OpDescriptor chooseOperation(Value *Src, RandomIRBuilder &IB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 InjectorIRStrategy(std::vector<fuzzerop::OpDescriptor> &&Operations)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 : Operations(std::move(Operations)) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 static std::vector<fuzzerop::OpDescriptor> getDefaultOps();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 uint64_t CurrentWeight) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 return Operations.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 using IRMutationStrategy::mutate;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 void mutate(Function &F, RandomIRBuilder &IB) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 class InstDeleterIRStrategy : public IRMutationStrategy {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 uint64_t CurrentWeight) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 using IRMutationStrategy::mutate;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 void mutate(Function &F, RandomIRBuilder &IB) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 void mutate(Instruction &Inst, RandomIRBuilder &IB) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 } // end llvm namespace
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 #endif // LLVM_FUZZMUTATE_IRMUTATOR_H