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_global_undef.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/opt/reflect.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
TransformationAddGlobalUndef(spvtools::fuzz::protobufs::TransformationAddGlobalUndef message)23 TransformationAddGlobalUndef::TransformationAddGlobalUndef(
24 spvtools::fuzz::protobufs::TransformationAddGlobalUndef message)
25 : message_(std::move(message)) {}
26
TransformationAddGlobalUndef(uint32_t fresh_id,uint32_t type_id)27 TransformationAddGlobalUndef::TransformationAddGlobalUndef(uint32_t fresh_id,
28 uint32_t type_id) {
29 message_.set_fresh_id(fresh_id);
30 message_.set_type_id(type_id);
31 }
32
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const33 bool TransformationAddGlobalUndef::IsApplicable(
34 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
35 // A fresh id is required.
36 if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
37 return false;
38 }
39 auto type = ir_context->get_def_use_mgr()->GetDef(message_.type_id());
40 // The type must exist, and must not be a function or pointer type.
41 return type != nullptr && opt::IsTypeInst(type->opcode()) &&
42 type->opcode() != SpvOpTypeFunction &&
43 type->opcode() != SpvOpTypePointer;
44 }
45
Apply(opt::IRContext * ir_context,TransformationContext *) const46 void TransformationAddGlobalUndef::Apply(
47 opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
48 auto new_instruction = MakeUnique<opt::Instruction>(
49 ir_context, SpvOpUndef, message_.type_id(), message_.fresh_id(),
50 opt::Instruction::OperandList());
51 auto new_instruction_ptr = new_instruction.get();
52 ir_context->module()->AddGlobalValue(std::move(new_instruction));
53 fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
54 // Inform the def-use manager about the new instruction.
55 ir_context->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr);
56 }
57
ToMessage() const58 protobufs::Transformation TransformationAddGlobalUndef::ToMessage() const {
59 protobufs::Transformation result;
60 *result.mutable_add_global_undef() = message_;
61 return result;
62 }
63
GetFreshIds() const64 std::unordered_set<uint32_t> TransformationAddGlobalUndef::GetFreshIds() const {
65 return {message_.fresh_id()};
66 }
67
68 } // namespace fuzz
69 } // namespace spvtools
70