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 ||
47 type_instruction->opcode() != spv::Op::OpTypePointer ||
48 spv::StorageClass(type_instruction->GetSingleWordInOperand(0)) !=
49 spv::StorageClass::Function) {
50 return false;
51 }
52 // The initializer must...
53 auto initializer_instruction =
54 ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
55 // ... exist, ...
56 if (!initializer_instruction) {
57 return false;
58 }
59 // ... be a constant, ...
60 if (!spvOpcodeIsConstant(initializer_instruction->opcode())) {
61 return false;
62 }
63 // ... and have the same type as the pointee type.
64 if (initializer_instruction->type_id() !=
65 type_instruction->GetSingleWordInOperand(1)) {
66 return false;
67 }
68 // The function to which the local variable is to be added must exist.
69 return fuzzerutil::FindFunction(ir_context, message_.function_id());
70 }
71
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const72 void TransformationAddLocalVariable::Apply(
73 opt::IRContext* ir_context,
74 TransformationContext* transformation_context) const {
75 opt::Instruction* new_instruction = fuzzerutil::AddLocalVariable(
76 ir_context, message_.fresh_id(), message_.type_id(),
77 message_.function_id(), message_.initializer_id());
78
79 // Inform the def-use manager about the new instruction.
80 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
81 ir_context->set_instr_block(
82 new_instruction,
83 fuzzerutil::FindFunction(ir_context, message_.function_id())
84 ->entry()
85 .get());
86
87 if (message_.value_is_irrelevant()) {
88 transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
89 message_.fresh_id());
90 }
91 }
92
ToMessage() const93 protobufs::Transformation TransformationAddLocalVariable::ToMessage() const {
94 protobufs::Transformation result;
95 *result.mutable_add_local_variable() = message_;
96 return result;
97 }
98
GetFreshIds() const99 std::unordered_set<uint32_t> TransformationAddLocalVariable::GetFreshIds()
100 const {
101 return {message_.fresh_id()};
102 }
103
104 } // namespace fuzz
105 } // namespace spvtools
106