| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 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_spec_constant_op.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include "source/fuzz/fuzzer_util.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
|
| 24 |
TransformationAddSpecConstantOp::TransformationAddSpecConstantOp( |
| 25 |
spvtools::fuzz::protobufs::TransformationAddSpecConstantOp message) |
| 26 |
: message_(std::move(message)) {} |
| 27 |
|
| 28 |
TransformationAddSpecConstantOp::TransformationAddSpecConstantOp( |
| 29 |
uint32_t fresh_id, uint32_t type_id, SpvOp opcode, |
| 30 |
const opt::Instruction::OperandList& operands) { |
| 31 |
message_.set_fresh_id(fresh_id); |
| 32 |
message_.set_type_id(type_id); |
| 33 |
message_.set_opcode(opcode); |
| 34 |
for (const auto& operand : operands) { |
| 35 |
auto* op = message_.add_operand(); |
| 36 |
op->set_operand_type(operand.type); |
| 37 |
for (auto word : operand.words) { |
| 38 |
op->add_operand_data(word); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
bool TransformationAddSpecConstantOp::IsApplicable( |
| 44 |
opt::IRContext* ir_context, |
| 45 |
const TransformationContext& transformation_context) const { |
| 46 |
auto clone = fuzzerutil::CloneIRContext(ir_context); |
| 47 |
ApplyImpl(clone.get()); |
| 48 |
return fuzzerutil::IsValid(clone.get(), |
| 49 |
transformation_context.GetValidatorOptions(), |
| 50 |
fuzzerutil::kSilentMessageConsumer); |
| 51 |
} |
| 52 |
|
| 53 |
void TransformationAddSpecConstantOp::Apply( |
| 54 |
opt::IRContext* ir_context, TransformationContext* /*unused*/) const { |
| 55 |
ApplyImpl(ir_context); |
| 56 |
ir_context->InvalidateAnalysesExceptFor( |
| 57 |
opt::IRContext::Analysis::kAnalysisNone); |
| 58 |
} |
| 59 |
|
| 60 |
void TransformationAddSpecConstantOp::ApplyImpl( |
| 61 |
opt::IRContext* ir_context) const { |
| 62 |
opt::Instruction::OperandList operands = { |
| 63 |
{SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER, {message_.opcode()}}}; |
| 64 |
|
| 65 |
for (const auto& operand : message_.operand()) { |
| 66 |
std::vector<uint32_t> words(operand.operand_data().begin(), |
| 67 |
operand.operand_data().end()); |
| 68 |
operands.push_back({static_cast<spv_operand_type_t>(operand.operand_type()), |
| 69 |
std::move(words)}); |
| 70 |
} |
| 71 |
|
| 72 |
ir_context->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 73 |
ir_context, SpvOpSpecConstantOp, message_.type_id(), message_.fresh_id(), |
| 74 |
std::move(operands))); |
| 75 |
|
| 76 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 77 |
} |
| 78 |
|
| 79 |
protobufs::Transformation TransformationAddSpecConstantOp::ToMessage() const { |
| 80 |
protobufs::Transformation result; |
| 81 |
*result.mutable_add_spec_constant_op() = message_; |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
std::unordered_set<uint32_t> TransformationAddSpecConstantOp::GetFreshIds() |
| 86 |
const { |
| 87 |
return {message_.fresh_id()}; |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace fuzz |
| 91 |
} // namespace spvtools |