| 1 |
// Copyright (c) 2019 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/instruction_message.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
protobufs::Instruction MakeInstructionMessage( |
| 23 |
SpvOp opcode, uint32_t result_type_id, uint32_t result_id, |
| 24 |
const opt::Instruction::OperandList& input_operands) { |
| 25 |
protobufs::Instruction result; |
| 26 |
result.set_opcode(opcode); |
| 27 |
result.set_result_type_id(result_type_id); |
| 28 |
result.set_result_id(result_id); |
| 29 |
for (auto& operand : input_operands) { |
| 30 |
auto operand_message = result.add_input_operand(); |
| 31 |
operand_message->set_operand_type(static_cast<uint32_t>(operand.type)); |
| 32 |
for (auto operand_word : operand.words) { |
| 33 |
operand_message->add_operand_data(operand_word); |
| 34 |
} |
| 35 |
} |
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
protobufs::Instruction MakeInstructionMessage( |
| 40 |
const opt::Instruction* instruction) { |
| 41 |
opt::Instruction::OperandList input_operands; |
| 42 |
for (uint32_t input_operand_index = 0; |
| 43 |
input_operand_index < instruction->NumInOperands(); |
| 44 |
input_operand_index++) { |
| 45 |
input_operands.push_back(instruction->GetInOperand(input_operand_index)); |
| 46 |
} |
| 47 |
return MakeInstructionMessage(instruction->opcode(), instruction->type_id(), |
| 48 |
instruction->result_id(), input_operands); |
| 49 |
} |
| 50 |
|
| 51 |
std::unique_ptr<opt::Instruction> InstructionFromMessage( |
| 52 |
opt::IRContext* ir_context, |
| 53 |
const protobufs::Instruction& instruction_message) { |
| 54 |
// First, update the module's id bound with respect to the new instruction, |
| 55 |
// if it has a result id. |
| 56 |
if (instruction_message.result_id()) { |
| 57 |
fuzzerutil::UpdateModuleIdBound(ir_context, |
| 58 |
instruction_message.result_id()); |
| 59 |
} |
| 60 |
// Now create a sequence of input operands from the input operand data in the |
| 61 |
// protobuf message. |
| 62 |
opt::Instruction::OperandList in_operands; |
| 63 |
for (auto& operand_message : instruction_message.input_operand()) { |
| 64 |
opt::Operand::OperandData operand_data; |
| 65 |
for (auto& word : operand_message.operand_data()) { |
| 66 |
operand_data.push_back(word); |
| 67 |
} |
| 68 |
in_operands.push_back( |
| 69 |
{static_cast<spv_operand_type_t>(operand_message.operand_type()), |
| 70 |
operand_data}); |
| 71 |
} |
| 72 |
// Create and return the instruction. |
| 73 |
return MakeUnique<opt::Instruction>( |
| 74 |
ir_context, static_cast<SpvOp>(instruction_message.opcode()), |
| 75 |
instruction_message.result_type_id(), instruction_message.result_id(), |
| 76 |
in_operands); |
| 77 |
} |
| 78 |
|
| 79 |
} // namespace fuzz |
| 80 |
} // namespace spvtools |