1 // Copyright (c) 2020 Google LLC
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 #include "source/fuzz/transformation_context.h"
16 
17 #include <cassert>
18 
19 #include "source/util/make_unique.h"
20 
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24 
25 // An overflow id source that should never be used: its methods assert false.
26 // This is the right id source for use during fuzzing, when overflow ids should
27 // never be required.
28 class NullOverflowIdSource : public OverflowIdSource {
HasOverflowIds() const29   bool HasOverflowIds() const override {
30     assert(false && "Bad attempt to query whether overflow ids are available.");
31     return false;
32   }
33 
GetNextOverflowId()34   uint32_t GetNextOverflowId() override {
35     assert(false && "Bad attempt to request an overflow id.");
36     return 0;
37   }
38 
GetIssuedOverflowIds() const39   const std::unordered_set<uint32_t>& GetIssuedOverflowIds() const override {
40     assert(false && "Operation not supported.");
41     return placeholder_;
42   }
43 
44  private:
45   std::unordered_set<uint32_t> placeholder_;
46 };
47 
48 }  // namespace
49 
TransformationContext(std::unique_ptr<FactManager> fact_manager,spv_validator_options validator_options)50 TransformationContext::TransformationContext(
51     std::unique_ptr<FactManager> fact_manager,
52     spv_validator_options validator_options)
53     : fact_manager_(std::move(fact_manager)),
54       validator_options_(validator_options),
55       overflow_id_source_(MakeUnique<NullOverflowIdSource>()) {}
56 
TransformationContext(std::unique_ptr<FactManager> fact_manager,spv_validator_options validator_options,std::unique_ptr<OverflowIdSource> overflow_id_source)57 TransformationContext::TransformationContext(
58     std::unique_ptr<FactManager> fact_manager,
59     spv_validator_options validator_options,
60     std::unique_ptr<OverflowIdSource> overflow_id_source)
61     : fact_manager_(std::move(fact_manager)),
62       validator_options_(validator_options),
63       overflow_id_source_(std::move(overflow_id_source)) {}
64 
65 TransformationContext::~TransformationContext() = default;
66 
67 }  // namespace fuzz
68 }  // namespace spvtools
69