• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,spv::Op opcode)29     TransformationAddEarlyTerminatorWrapper(uint32_t function_fresh_id,
30                                             uint32_t label_fresh_id,
31                                             spv::Op opcode) {
32   message_.set_function_fresh_id(function_fresh_id);
33   message_.set_label_fresh_id(label_fresh_id);
34   message_.set_opcode(uint32_t(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((spv::Op(message_.opcode()) == spv::Op::OpKill ||
40           spv::Op(message_.opcode()) == spv::Op::OpUnreachable ||
41           spv::Op(message_.opcode()) == spv::Op::OpTerminateInvocation) &&
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, spv::Op::OpLabel, 0, message_.label_fresh_id(),
70       opt::Instruction::OperandList()));
71   basic_block->AddInstruction(MakeUnique<opt::Instruction>(
72       ir_context, static_cast<spv::Op>(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, spv::Op::OpFunction, void_type_id,
79       message_.function_fresh_id(),
80       opt::Instruction::OperandList(
81           {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
82             {uint32_t(spv::FunctionControlMask::MaskNone)}},
83            {SPV_OPERAND_TYPE_TYPE_ID,
84             {fuzzerutil::FindFunctionType(ir_context, {void_type_id})}}})));
85 
86   // Add the basic block to the function as the sole block, and add the function
87   // to the module.
88   function->AddBasicBlock(std::move(basic_block));
89   function->SetFunctionEnd(
90       MakeUnique<opt::Instruction>(ir_context, spv::Op::OpFunctionEnd, 0, 0,
91                                    opt::Instruction::OperandList()));
92   ir_context->module()->AddFunction(std::move(function));
93 
94   ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
95 }
96 
97 std::unordered_set<uint32_t>
GetFreshIds() const98 TransformationAddEarlyTerminatorWrapper::GetFreshIds() const {
99   return std::unordered_set<uint32_t>(
100       {message_.function_fresh_id(), message_.label_fresh_id()});
101 }
102 
ToMessage() const103 protobufs::Transformation TransformationAddEarlyTerminatorWrapper::ToMessage()
104     const {
105   protobufs::Transformation result;
106   *result.mutable_add_early_terminator_wrapper() = message_;
107   return result;
108 }
109 
110 }  // namespace fuzz
111 }  // namespace spvtools
112