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_early_terminator_wrapper.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/util/make_unique.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
23 TransformationAddEarlyTerminatorWrapper::
TransformationAddEarlyTerminatorWrapper(protobufs::TransformationAddEarlyTerminatorWrapper message)24 TransformationAddEarlyTerminatorWrapper(
25 protobufs::TransformationAddEarlyTerminatorWrapper message)
26 : message_(std::move(message)) {}
27
28 TransformationAddEarlyTerminatorWrapper::
TransformationAddEarlyTerminatorWrapper(uint32_t function_fresh_id,uint32_t label_fresh_id,SpvOp opcode)29 TransformationAddEarlyTerminatorWrapper(uint32_t function_fresh_id,
30 uint32_t label_fresh_id,
31 SpvOp opcode) {
32 message_.set_function_fresh_id(function_fresh_id);
33 message_.set_label_fresh_id(label_fresh_id);
34 message_.set_opcode(opcode);
35 }
36
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const37 bool TransformationAddEarlyTerminatorWrapper::IsApplicable(
38 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
39 assert((message_.opcode() == SpvOpKill ||
40 message_.opcode() == SpvOpUnreachable ||
41 message_.opcode() == SpvOpTerminateInvocation) &&
42 "Invalid opcode.");
43
44 if (!fuzzerutil::IsFreshId(ir_context, message_.function_fresh_id())) {
45 return false;
46 }
47 if (!fuzzerutil::IsFreshId(ir_context, message_.label_fresh_id())) {
48 return false;
49 }
50 if (message_.function_fresh_id() == message_.label_fresh_id()) {
51 return false;
52 }
53 uint32_t void_type_id = fuzzerutil::MaybeGetVoidType(ir_context);
54 if (!void_type_id) {
55 return false;
56 }
57 return fuzzerutil::FindFunctionType(ir_context, {void_type_id});
58 }
59
Apply(opt::IRContext * ir_context,TransformationContext *) const60 void TransformationAddEarlyTerminatorWrapper::Apply(
61 opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
62 fuzzerutil::UpdateModuleIdBound(ir_context, message_.function_fresh_id());
63 fuzzerutil::UpdateModuleIdBound(ir_context, message_.label_fresh_id());
64
65 // Create a basic block of the form:
66 // %label_fresh_id = OpLabel
67 // OpKill|Unreachable|TerminateInvocation
68 auto basic_block = MakeUnique<opt::BasicBlock>(MakeUnique<opt::Instruction>(
69 ir_context, SpvOpLabel, 0, message_.label_fresh_id(),
70 opt::Instruction::OperandList()));
71 basic_block->AddInstruction(MakeUnique<opt::Instruction>(
72 ir_context, static_cast<SpvOp>(message_.opcode()), 0, 0,
73 opt::Instruction::OperandList()));
74
75 // Create a zero-argument void function.
76 auto void_type_id = fuzzerutil::MaybeGetVoidType(ir_context);
77 auto function = MakeUnique<opt::Function>(MakeUnique<opt::Instruction>(
78 ir_context, SpvOpFunction, void_type_id, message_.function_fresh_id(),
79 opt::Instruction::OperandList(
80 {{SPV_OPERAND_TYPE_FUNCTION_CONTROL, {SpvFunctionControlMaskNone}},
81 {SPV_OPERAND_TYPE_TYPE_ID,
82 {fuzzerutil::FindFunctionType(ir_context, {void_type_id})}}})));
83
84 // Add the basic block to the function as the sole block, and add the function
85 // to the module.
86 function->AddBasicBlock(std::move(basic_block));
87 function->SetFunctionEnd(MakeUnique<opt::Instruction>(
88 ir_context, SpvOpFunctionEnd, 0, 0, opt::Instruction::OperandList()));
89 ir_context->module()->AddFunction(std::move(function));
90
91 ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
92 }
93
94 std::unordered_set<uint32_t>
GetFreshIds() const95 TransformationAddEarlyTerminatorWrapper::GetFreshIds() const {
96 return std::unordered_set<uint32_t>(
97 {message_.function_fresh_id(), message_.label_fresh_id()});
98 }
99
ToMessage() const100 protobufs::Transformation TransformationAddEarlyTerminatorWrapper::ToMessage()
101 const {
102 protobufs::Transformation result;
103 *result.mutable_add_early_terminator_wrapper() = message_;
104 return result;
105 }
106
107 } // namespace fuzz
108 } // namespace spvtools
109