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_add_local_variable.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18
19 namespace spvtools {
20 namespace fuzz {
21
TransformationAddLocalVariable(spvtools::fuzz::protobufs::TransformationAddLocalVariable message)22 TransformationAddLocalVariable::TransformationAddLocalVariable(
23 spvtools::fuzz::protobufs::TransformationAddLocalVariable message)
24 : message_(std::move(message)) {}
25
TransformationAddLocalVariable(uint32_t fresh_id,uint32_t type_id,uint32_t function_id,uint32_t initializer_id,bool value_is_irrelevant)26 TransformationAddLocalVariable::TransformationAddLocalVariable(
27 uint32_t fresh_id, uint32_t type_id, uint32_t function_id,
28 uint32_t initializer_id, bool value_is_irrelevant) {
29 message_.set_fresh_id(fresh_id);
30 message_.set_type_id(type_id);
31 message_.set_function_id(function_id);
32 message_.set_initializer_id(initializer_id);
33 message_.set_value_is_irrelevant(value_is_irrelevant);
34 }
35
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const36 bool TransformationAddLocalVariable::IsApplicable(
37 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
38 // The provided id must be fresh.
39 if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
40 return false;
41 }
42 // The pointer type id must indeed correspond to a pointer, and it must have
43 // function storage class.
44 auto type_instruction =
45 ir_context->get_def_use_mgr()->GetDef(message_.type_id());
46 if (!type_instruction || type_instruction->opcode() != SpvOpTypePointer ||
47 type_instruction->GetSingleWordInOperand(0) != SpvStorageClassFunction) {
48 return false;
49 }
50 // The initializer must...
51 auto initializer_instruction =
52 ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
53 // ... exist, ...
54 if (!initializer_instruction) {
55 return false;
56 }
57 // ... be a constant, ...
58 if (!spvOpcodeIsConstant(initializer_instruction->opcode())) {
59 return false;
60 }
61 // ... and have the same type as the pointee type.
62 if (initializer_instruction->type_id() !=
63 type_instruction->GetSingleWordInOperand(1)) {
64 return false;
65 }
66 // The function to which the local variable is to be added must exist.
67 return fuzzerutil::FindFunction(ir_context, message_.function_id());
68 }
69
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const70 void TransformationAddLocalVariable::Apply(
71 opt::IRContext* ir_context,
72 TransformationContext* transformation_context) const {
73 opt::Instruction* new_instruction = fuzzerutil::AddLocalVariable(
74 ir_context, message_.fresh_id(), message_.type_id(),
75 message_.function_id(), message_.initializer_id());
76
77 // Inform the def-use manager about the new instruction.
78 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
79 ir_context->set_instr_block(
80 new_instruction,
81 fuzzerutil::FindFunction(ir_context, message_.function_id())
82 ->entry()
83 .get());
84
85 if (message_.value_is_irrelevant()) {
86 transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
87 message_.fresh_id());
88 }
89 }
90
ToMessage() const91 protobufs::Transformation TransformationAddLocalVariable::ToMessage() const {
92 protobufs::Transformation result;
93 *result.mutable_add_local_variable() = message_;
94 return result;
95 }
96
GetFreshIds() const97 std::unordered_set<uint32_t> TransformationAddLocalVariable::GetFreshIds()
98 const {
99 return {message_.fresh_id()};
100 }
101
102 } // namespace fuzz
103 } // namespace spvtools
104