1 // Copyright (c) 2019 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_add_type_boolean.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 
19 namespace spvtools {
20 namespace fuzz {
21 
TransformationAddTypeBoolean(protobufs::TransformationAddTypeBoolean message)22 TransformationAddTypeBoolean::TransformationAddTypeBoolean(
23     protobufs::TransformationAddTypeBoolean message)
24     : message_(std::move(message)) {}
25 
TransformationAddTypeBoolean(uint32_t fresh_id)26 TransformationAddTypeBoolean::TransformationAddTypeBoolean(uint32_t fresh_id) {
27   message_.set_fresh_id(fresh_id);
28 }
29 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const30 bool TransformationAddTypeBoolean::IsApplicable(
31     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
32   // The id must be fresh.
33   if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
34     return false;
35   }
36 
37   // Applicable if there is no bool type already declared in the module.
38   opt::analysis::Bool bool_type;
39   return ir_context->get_type_mgr()->GetId(&bool_type) == 0;
40 }
41 
Apply(opt::IRContext * ir_context,TransformationContext *) const42 void TransformationAddTypeBoolean::Apply(
43     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
44   opt::Instruction::OperandList empty_operands;
45   auto type_instruction = MakeUnique<opt::Instruction>(
46       ir_context, SpvOpTypeBool, 0, message_.fresh_id(), empty_operands);
47   auto type_instruction_ptr = type_instruction.get();
48   ir_context->module()->AddType(std::move(type_instruction));
49 
50   fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
51 
52   // Inform the def use manager that there is a new definition. Invalidate the
53   // type manager since we have added a new type.
54   ir_context->get_def_use_mgr()->AnalyzeInstDef(type_instruction_ptr);
55   ir_context->InvalidateAnalyses(opt::IRContext::kAnalysisTypes);
56 }
57 
ToMessage() const58 protobufs::Transformation TransformationAddTypeBoolean::ToMessage() const {
59   protobufs::Transformation result;
60   *result.mutable_add_type_boolean() = message_;
61   return result;
62 }
63 
GetFreshIds() const64 std::unordered_set<uint32_t> TransformationAddTypeBoolean::GetFreshIds() const {
65   return {message_.fresh_id()};
66 }
67 
68 }  // namespace fuzz
69 }  // namespace spvtools
70