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