• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FUZZERS_TINT_AST_FUZZER_MUTATION_H_
16 #define FUZZERS_TINT_AST_FUZZER_MUTATION_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "fuzzers/tint_ast_fuzzer/node_id_map.h"
22 #include "fuzzers/tint_ast_fuzzer/protobufs/tint_ast_fuzzer.h"
23 
24 #include "src/clone_context.h"
25 #include "src/program.h"
26 
27 namespace tint {
28 namespace fuzzers {
29 namespace ast_fuzzer {
30 
31 /// The base class for all the mutations in the fuzzer. Children must override
32 /// three methods:
33 /// - `IsApplicable` - checks whether it is possible to apply the mutation
34 ///   in a manner that will lead to a valid program.
35 /// - `Apply` - applies the mutation.
36 /// - `ToMessage` - converts the mutation data into a protobuf message.
37 class Mutation {
38  public:
39   /// Virtual destructor.
40   virtual ~Mutation();
41 
42   /// @brief Determines whether this mutation is applicable to the `program`.
43   ///
44   /// @param program - the program this mutation will be applied to. The program
45   ///     must be valid.
46   /// @param node_id_map - the map from `tint::ast::` nodes to their ids.
47   /// @return `true` if `Apply` method can be called without breaking the
48   ///     semantics of the `program`.
49   /// @return `false` otherwise.
50   virtual bool IsApplicable(const tint::Program& program,
51                             const NodeIdMap& node_id_map) const = 0;
52 
53   /// @brief Applies this mutation to the `clone_context`.
54   ///
55   /// Precondition: `IsApplicable` must return `true` when invoked on the same
56   /// `node_id_map` and `clone_context->src` instance of `tint::Program`. A new
57   /// `tint::Program` that arises in `clone_context` must be valid.
58   ///
59   /// @param node_id_map - the map from `tint::ast::` nodes to their ids.
60   /// @param clone_context - the context that will clone the program with some
61   ///     changes introduced by this mutation.
62   /// @param new_node_id_map - this map will store ids for the mutated and
63   ///     cloned program. This argument cannot be a `nullptr` nor can it point
64   ///     to the same object as `node_id_map`.
65   virtual void Apply(const NodeIdMap& node_id_map,
66                      tint::CloneContext* clone_context,
67                      NodeIdMap* new_node_id_map) const = 0;
68 
69   /// @return a protobuf message for this mutation.
70   virtual protobufs::Mutation ToMessage() const = 0;
71 
72   /// @brief Converts a protobuf message into the mutation instance.
73   ///
74   /// @param message - a protobuf message.
75   /// @return the instance of this class.
76   static std::unique_ptr<Mutation> FromMessage(
77       const protobufs::Mutation& message);
78 };
79 
80 using MutationList = std::vector<std::unique_ptr<Mutation>>;
81 
82 }  // namespace ast_fuzzer
83 }  // namespace fuzzers
84 }  // namespace tint
85 
86 #endif  // FUZZERS_TINT_AST_FUZZER_MUTATION_H_
87