2019-10-08 13:04:10 +00:00
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
#include "source/fuzz/transformation_composite_construct.h"
|
2019-10-08 13:04:10 +00:00
|
|
|
|
2019-10-25 16:37:55 +00:00
|
|
|
#include "source/fuzz/data_descriptor.h"
|
2019-10-08 13:04:10 +00:00
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2019-10-15 19:00:17 +00:00
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
|
|
|
#include "source/opt/instruction.h"
|
2019-10-08 13:04:10 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
TransformationCompositeConstruct::TransformationCompositeConstruct(
|
|
|
|
const protobufs::TransformationCompositeConstruct& message)
|
2019-10-08 13:04:10 +00:00
|
|
|
: message_(message) {}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
TransformationCompositeConstruct::TransformationCompositeConstruct(
|
2019-10-08 13:04:10 +00:00
|
|
|
uint32_t composite_type_id, std::vector<uint32_t> component,
|
2019-10-15 19:00:17 +00:00
|
|
|
const protobufs::InstructionDescriptor& instruction_to_insert_before,
|
|
|
|
uint32_t fresh_id) {
|
2019-10-08 13:04:10 +00:00
|
|
|
message_.set_composite_type_id(composite_type_id);
|
|
|
|
for (auto a_component : component) {
|
|
|
|
message_.add_component(a_component);
|
|
|
|
}
|
2019-10-15 19:00:17 +00:00
|
|
|
*message_.mutable_instruction_to_insert_before() =
|
|
|
|
instruction_to_insert_before;
|
2019-10-08 13:04:10 +00:00
|
|
|
message_.set_fresh_id(fresh_id);
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
bool TransformationCompositeConstruct::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
|
|
|
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
|
2019-10-08 13:04:10 +00:00
|
|
|
// We require the id for the composite constructor to be unused.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before =
|
2020-04-02 14:54:46 +00:00
|
|
|
FindInstruction(message_.instruction_to_insert_before(), ir_context);
|
2019-10-15 19:00:17 +00:00
|
|
|
if (!insert_before) {
|
|
|
|
// The instruction before which the composite should be inserted was not
|
|
|
|
// found.
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto composite_type =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_type_mgr()->GetType(message_.composite_type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
|
|
|
|
if (!fuzzerutil::IsCompositeType(composite_type)) {
|
|
|
|
// The type must actually be a composite.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is an array, matrix, struct or vector, the components need to
|
|
|
|
// be suitable for constructing something of that type.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (composite_type->AsArray() &&
|
|
|
|
!ComponentsForArrayConstructionAreOK(ir_context,
|
|
|
|
*composite_type->AsArray())) {
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
if (composite_type->AsMatrix() &&
|
|
|
|
!ComponentsForMatrixConstructionAreOK(ir_context,
|
|
|
|
*composite_type->AsMatrix())) {
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
if (composite_type->AsStruct() &&
|
|
|
|
!ComponentsForStructConstructionAreOK(ir_context,
|
|
|
|
*composite_type->AsStruct())) {
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
if (composite_type->AsVector() &&
|
|
|
|
!ComponentsForVectorConstructionAreOK(ir_context,
|
|
|
|
*composite_type->AsVector())) {
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check whether every component being used to initialize the composite is
|
|
|
|
// available at the desired program point.
|
|
|
|
for (auto& component : message_.component()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, insert_before,
|
2020-02-06 16:54:34 +00:00
|
|
|
component)) {
|
2019-10-08 13:04:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationCompositeConstruct::Apply(
|
|
|
|
opt::IRContext* ir_context,
|
|
|
|
TransformationContext* transformation_context) const {
|
2019-10-08 13:04:10 +00:00
|
|
|
// Use the base and offset information from the transformation to determine
|
|
|
|
// where in the module a new instruction should be inserted.
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before_inst =
|
2020-04-02 14:54:46 +00:00
|
|
|
FindInstruction(message_.instruction_to_insert_before(), ir_context);
|
|
|
|
auto destination_block = ir_context->get_instr_block(insert_before_inst);
|
2019-10-15 19:00:17 +00:00
|
|
|
auto insert_before = fuzzerutil::GetIteratorForInstruction(
|
|
|
|
destination_block, insert_before_inst);
|
2019-10-08 13:04:10 +00:00
|
|
|
|
|
|
|
// Prepare the input operands for an OpCompositeConstruct instruction.
|
|
|
|
opt::Instruction::OperandList in_operands;
|
|
|
|
for (auto& component_id : message_.component()) {
|
|
|
|
in_operands.push_back({SPV_OPERAND_TYPE_ID, {component_id}});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an OpCompositeConstruct instruction.
|
|
|
|
insert_before.InsertBefore(MakeUnique<opt::Instruction>(
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context, SpvOpCompositeConstruct, message_.composite_type_id(),
|
2019-10-08 13:04:10 +00:00
|
|
|
message_.fresh_id(), in_operands));
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
|
|
|
|
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
|
2019-11-01 17:50:01 +00:00
|
|
|
|
2019-10-08 13:04:10 +00:00
|
|
|
// Inform the fact manager that we now have new synonyms: every component of
|
2019-11-01 17:50:01 +00:00
|
|
|
// the composite is synonymous with the id used to construct that component,
|
|
|
|
// except in the case of a vector where a single vector id can span multiple
|
|
|
|
// components.
|
2019-10-08 13:04:10 +00:00
|
|
|
auto composite_type =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_type_mgr()->GetType(message_.composite_type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
for (auto component : message_.component()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto component_type = ir_context->get_type_mgr()->GetType(
|
|
|
|
ir_context->get_def_use_mgr()->GetDef(component)->type_id());
|
2019-11-01 17:50:01 +00:00
|
|
|
if (composite_type->AsVector() && component_type->AsVector()) {
|
|
|
|
// The case where the composite being constructed is a vector and the
|
|
|
|
// component provided for construction is also a vector is special. It
|
|
|
|
// requires adding a synonym fact relating each element of the sub-vector
|
|
|
|
// to the corresponding element of the composite being constructed.
|
|
|
|
assert(component_type->AsVector()->element_type() ==
|
|
|
|
composite_type->AsVector()->element_type());
|
|
|
|
assert(component_type->AsVector()->element_count() <
|
|
|
|
composite_type->AsVector()->element_count());
|
|
|
|
for (uint32_t subvector_index = 0;
|
|
|
|
subvector_index < component_type->AsVector()->element_count();
|
|
|
|
subvector_index++) {
|
2020-04-02 14:54:46 +00:00
|
|
|
transformation_context->GetFactManager()->AddFactDataSynonym(
|
2019-11-01 17:50:01 +00:00
|
|
|
MakeDataDescriptor(component, {subvector_index}),
|
2020-04-02 14:54:46 +00:00
|
|
|
MakeDataDescriptor(message_.fresh_id(), {index}), ir_context);
|
2019-11-01 17:50:01 +00:00
|
|
|
index++;
|
2019-10-08 13:04:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-01 17:50:01 +00:00
|
|
|
// The other cases are simple: the component is made directly synonymous
|
|
|
|
// with the element of the composite being constructed.
|
2020-04-02 14:54:46 +00:00
|
|
|
transformation_context->GetFactManager()->AddFactDataSynonym(
|
2019-11-01 17:50:01 +00:00
|
|
|
MakeDataDescriptor(component, {}),
|
2020-04-02 14:54:46 +00:00
|
|
|
MakeDataDescriptor(message_.fresh_id(), {index}), ir_context);
|
2019-11-01 17:50:01 +00:00
|
|
|
index++;
|
2019-10-08 13:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
bool TransformationCompositeConstruct::ComponentsForArrayConstructionAreOK(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const opt::analysis::Array& array_type) const {
|
2019-10-08 13:04:10 +00:00
|
|
|
if (array_type.length_info().words[0] !=
|
|
|
|
opt::analysis::Array::LengthInfo::kConstant) {
|
|
|
|
// We only handle constant-sized arrays.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (array_type.length_info().words.size() != 2) {
|
|
|
|
// We only handle the case where the array size can be captured in a single
|
|
|
|
// word.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Get the array size.
|
|
|
|
auto array_size = array_type.length_info().words[1];
|
|
|
|
if (static_cast<uint32_t>(message_.component().size()) != array_size) {
|
|
|
|
// The number of components must match the array size.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check that each component is the result id of an instruction whose type is
|
|
|
|
// the array's element type.
|
|
|
|
for (auto component_id : message_.component()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
|
2019-10-08 13:04:10 +00:00
|
|
|
if (inst == nullptr || !inst->type_id()) {
|
|
|
|
// The component does not correspond to an instruction with a result
|
|
|
|
// type.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
assert(component_type);
|
|
|
|
if (component_type != array_type.element_type()) {
|
|
|
|
// The component's type does not match the array's element type.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
bool TransformationCompositeConstruct::ComponentsForMatrixConstructionAreOK(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const opt::analysis::Matrix& matrix_type) const {
|
2019-10-08 13:04:10 +00:00
|
|
|
if (static_cast<uint32_t>(message_.component().size()) !=
|
|
|
|
matrix_type.element_count()) {
|
|
|
|
// The number of components must match the number of columns of the matrix.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check that each component is the result id of an instruction whose type is
|
|
|
|
// the matrix's column type.
|
|
|
|
for (auto component_id : message_.component()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
|
2019-10-08 13:04:10 +00:00
|
|
|
if (inst == nullptr || !inst->type_id()) {
|
|
|
|
// The component does not correspond to an instruction with a result
|
|
|
|
// type.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
assert(component_type);
|
|
|
|
if (component_type != matrix_type.element_type()) {
|
|
|
|
// The component's type does not match the matrix's column type.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
bool TransformationCompositeConstruct::ComponentsForStructConstructionAreOK(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const opt::analysis::Struct& struct_type) const {
|
2019-10-08 13:04:10 +00:00
|
|
|
if (static_cast<uint32_t>(message_.component().size()) !=
|
|
|
|
struct_type.element_types().size()) {
|
|
|
|
// The number of components must match the number of fields of the struct.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check that each component is the result id of an instruction those type
|
|
|
|
// matches the associated field type.
|
|
|
|
for (uint32_t field_index = 0;
|
|
|
|
field_index < struct_type.element_types().size(); field_index++) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto inst = ir_context->get_def_use_mgr()->GetDef(
|
|
|
|
message_.component()[field_index]);
|
2019-10-08 13:04:10 +00:00
|
|
|
if (inst == nullptr || !inst->type_id()) {
|
|
|
|
// The component does not correspond to an instruction with a result
|
|
|
|
// type.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
assert(component_type);
|
|
|
|
if (component_type != struct_type.element_types()[field_index]) {
|
|
|
|
// The component's type does not match the corresponding field type.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
bool TransformationCompositeConstruct::ComponentsForVectorConstructionAreOK(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const opt::analysis::Vector& vector_type) const {
|
2019-10-08 13:04:10 +00:00
|
|
|
uint32_t base_element_count = 0;
|
|
|
|
auto element_type = vector_type.element_type();
|
|
|
|
for (auto& component_id : message_.component()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
|
2019-10-08 13:04:10 +00:00
|
|
|
if (inst == nullptr || !inst->type_id()) {
|
|
|
|
// The component does not correspond to an instruction with a result
|
|
|
|
// type.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
|
2019-10-08 13:04:10 +00:00
|
|
|
assert(component_type);
|
|
|
|
if (component_type == element_type) {
|
|
|
|
base_element_count++;
|
|
|
|
} else if (component_type->AsVector() &&
|
|
|
|
component_type->AsVector()->element_type() == element_type) {
|
|
|
|
base_element_count += component_type->AsVector()->element_count();
|
|
|
|
} else {
|
|
|
|
// The component was not appropriate; e.g. no type corresponding to the
|
|
|
|
// given id was found, or the type that was found was not compatible
|
|
|
|
// with the vector being constructed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The number of components provided (when vector components are flattened
|
|
|
|
// out) needs to match the length of the vector being constructed.
|
|
|
|
return base_element_count == vector_type.element_count();
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:11:07 +00:00
|
|
|
protobufs::Transformation TransformationCompositeConstruct::ToMessage() const {
|
2019-10-08 13:04:10 +00:00
|
|
|
protobufs::Transformation result;
|
2019-10-27 18:11:07 +00:00
|
|
|
*result.mutable_composite_construct() = message_;
|
2019-10-08 13:04:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|