| 1 |
// Copyright (c) 2020 André Perez Maselco |
| 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/fuzzer_pass_add_image_sample_unused_components.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/instruction_descriptor.h" |
| 19 |
#include "source/fuzz/transformation_add_image_sample_unused_components.h" |
| 20 |
#include "source/fuzz/transformation_composite_construct.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
FuzzerPassAddImageSampleUnusedComponents:: |
| 26 |
FuzzerPassAddImageSampleUnusedComponents( |
| 27 |
opt::IRContext* ir_context, |
| 28 |
TransformationContext* transformation_context, |
| 29 |
FuzzerContext* fuzzer_context, |
| 30 |
protobufs::TransformationSequence* transformations) |
| 31 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 32 |
transformations) {} |
| 33 |
|
| 34 |
FuzzerPassAddImageSampleUnusedComponents:: |
| 35 |
~FuzzerPassAddImageSampleUnusedComponents() = default; |
| 36 |
|
| 37 |
void FuzzerPassAddImageSampleUnusedComponents::Apply() { |
| 38 |
// SPIR-V module to help understand the transformation. |
| 39 |
// |
| 40 |
// OpCapability Shader |
| 41 |
// %1 = OpExtInstImport "GLSL.std.450" |
| 42 |
// OpMemoryModel Logical GLSL450 |
| 43 |
// OpEntryPoint Fragment %15 "main" %12 %14 |
| 44 |
// OpExecutionMode %15 OriginUpperLeft |
| 45 |
// |
| 46 |
// ; Decorations |
| 47 |
// OpDecorate %12 Location 0 ; Input color variable location |
| 48 |
// OpDecorate %13 DescriptorSet 0 ; Image coordinate variable |
| 49 |
// descriptor set OpDecorate %13 Binding 0 ; Image coordinate |
| 50 |
// variable binding OpDecorate %14 Location 0 ; Fragment color |
| 51 |
// variable location |
| 52 |
// |
| 53 |
// ; Types |
| 54 |
// %2 = OpTypeVoid |
| 55 |
// %3 = OpTypeFunction %2 |
| 56 |
// %4 = OpTypeFloat 32 |
| 57 |
// %5 = OpTypeVector %4 2 |
| 58 |
// %6 = OpTypeVector %4 4 |
| 59 |
// %7 = OpTypeImage %4 2D 0 0 0 1 Rgba32f |
| 60 |
// %8 = OpTypeSampledImage %7 |
| 61 |
// %9 = OpTypePointer Input %5 |
| 62 |
// %10 = OpTypePointer UniformConstant %8 |
| 63 |
// %11 = OpTypePointer Output %6 |
| 64 |
// |
| 65 |
// ; Variables |
| 66 |
// %12 = OpVariable %9 Input ; Input image coordinate variable |
| 67 |
// %13 = OpVariable %10 UniformConstant ; Image variable |
| 68 |
// %14 = OpVariable %11 Output ; Fragment color variable |
| 69 |
// |
| 70 |
// ; main function |
| 71 |
// %15 = OpFunction %2 None %3 |
| 72 |
// %16 = OpLabel |
| 73 |
// %17 = OpLoad %5 %12 |
| 74 |
// %18 = OpLoad %8 %13 |
| 75 |
// %19 = OpImageSampleImplicitLod %6 %18 %17 |
| 76 |
// OpStore %14 %19 |
| 77 |
// OpReturn |
| 78 |
// OpFunctionEnd |
| 79 |
|
| 80 |
GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) { |
| 81 |
// |instruction| %19 = OpImageSampleImplicitLod %6 %18 %17 |
| 82 |
if (!spvOpcodeIsImageSample(instruction->opcode())) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 87 |
GetFuzzerContext() |
| 88 |
->GetChanceOfAddingImageSampleUnusedComponents())) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Gets image sample coordinate information. |
| 93 |
// |coordinate_instruction| %17 = OpLoad %5 %12 |
| 94 |
uint32_t coordinate_id = instruction->GetSingleWordInOperand(1); |
| 95 |
auto coordinate_instruction = |
| 96 |
GetIRContext()->get_def_use_mgr()->GetDef(coordinate_id); |
| 97 |
auto coordinate_type = GetIRContext()->get_type_mgr()->GetType( |
| 98 |
coordinate_instruction->type_id()); |
| 99 |
|
| 100 |
// If the coordinate is a 4-dimensional vector, then no unused components |
| 101 |
// may be added. |
| 102 |
if (coordinate_type->AsVector() && |
| 103 |
coordinate_type->AsVector()->element_count() == 4) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// If the coordinate is a scalar, then at most 3 unused components may be |
| 108 |
// added. If the coordinate is a vector, then the maximum number of unused |
| 109 |
// components depends on the vector size. |
| 110 |
// For the sample module, the coordinate type instruction is %5 = |
| 111 |
// OpTypeVector %4 2, thus |max_unused_component_count| = 4 - 2 = 2. |
| 112 |
uint32_t max_unused_component_count = |
| 113 |
coordinate_type->AsInteger() || coordinate_type->AsFloat() |
| 114 |
? 3 |
| 115 |
: 4 - coordinate_type->AsVector()->element_count(); |
| 116 |
|
| 117 |
// |unused_component_count| may be 1 or 2. |
| 118 |
uint32_t unused_component_count = |
| 119 |
GetFuzzerContext()->GetRandomUnusedComponentCountForImageSample( |
| 120 |
max_unused_component_count); |
| 121 |
|
| 122 |
// Gets a type for the zero-unused components. |
| 123 |
uint32_t zero_constant_type_id; |
| 124 |
switch (unused_component_count) { |
| 125 |
case 1: |
| 126 |
// If the coordinate is an integer or float, then the unused components |
| 127 |
// type is the same as the coordinate. If the coordinate is a vector, |
| 128 |
// then the unused components type is the same as the vector components |
| 129 |
// type. |
| 130 |
zero_constant_type_id = |
| 131 |
coordinate_type->AsInteger() || coordinate_type->AsFloat() |
| 132 |
? coordinate_instruction->type_id() |
| 133 |
: GetIRContext()->get_type_mgr()->GetId( |
| 134 |
coordinate_type->AsVector()->element_type()); |
| 135 |
break; |
| 136 |
case 2: |
| 137 |
case 3: |
| 138 |
// If the coordinate is an integer or float, then the unused components |
| 139 |
// type is the same as the coordinate. If the coordinate is a vector, |
| 140 |
// then the unused components type is the same as the coordinate |
| 141 |
// components type. |
| 142 |
// |zero_constant_type_id| %5 = OpTypeVector %4 2 |
| 143 |
zero_constant_type_id = |
| 144 |
coordinate_type->AsInteger() || coordinate_type->AsFloat() |
| 145 |
? FindOrCreateVectorType(coordinate_instruction->type_id(), |
| 146 |
unused_component_count) |
| 147 |
: FindOrCreateVectorType( |
| 148 |
GetIRContext()->get_type_mgr()->GetId( |
| 149 |
coordinate_type->AsVector()->element_type()), |
| 150 |
unused_component_count); |
| 151 |
break; |
| 152 |
default: |
| 153 |
assert(false && "Should be unreachable."); |
| 154 |
zero_constant_type_id = 0; |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
// Gets |coordinate_type| again because the module may have changed due to |
| 159 |
// the use of FindOrCreateVectorType above. |
| 160 |
coordinate_type = GetIRContext()->get_type_mgr()->GetType( |
| 161 |
coordinate_instruction->type_id()); |
| 162 |
|
| 163 |
// If the new vector type with unused components does not exist, then create |
| 164 |
// it. |coordinate_with_unused_components_type_id| %6 = OpTypeVector %4 4 |
| 165 |
uint32_t coordinate_with_unused_components_type_id = |
| 166 |
coordinate_type->AsInteger() || coordinate_type->AsFloat() |
| 167 |
? FindOrCreateVectorType(coordinate_instruction->type_id(), |
| 168 |
1 + unused_component_count) |
| 169 |
: FindOrCreateVectorType( |
| 170 |
GetIRContext()->get_type_mgr()->GetId( |
| 171 |
coordinate_type->AsVector()->element_type()), |
| 172 |
coordinate_type->AsVector()->element_count() + |
| 173 |
unused_component_count); |
| 174 |
|
| 175 |
// Inserts an OpCompositeConstruct instruction which |
| 176 |
// represents the coordinate with unused components. |
| 177 |
// |coordinate_with_unused_components_id| |
| 178 |
// %22 = OpCompositeConstruct %6 %17 %21 |
| 179 |
uint32_t coordinate_with_unused_components_id = |
| 180 |
GetFuzzerContext()->GetFreshId(); |
| 181 |
ApplyTransformation(TransformationCompositeConstruct( |
| 182 |
coordinate_with_unused_components_type_id, |
| 183 |
{coordinate_instruction->result_id(), |
| 184 |
// FindOrCreateZeroConstant |
| 185 |
// %20 = OpConstant %4 0 |
| 186 |
// %21 = OpConstantComposite %5 %20 %20 |
| 187 |
FindOrCreateZeroConstant(zero_constant_type_id, true)}, |
| 188 |
MakeInstructionDescriptor(GetIRContext(), instruction), |
| 189 |
coordinate_with_unused_components_id)); |
| 190 |
|
| 191 |
// Tries to add unused components to the image sample coordinate. |
| 192 |
// %19 = OpImageSampleImplicitLod %6 %18 %22 |
| 193 |
ApplyTransformation(TransformationAddImageSampleUnusedComponents( |
| 194 |
coordinate_with_unused_components_id, |
| 195 |
MakeInstructionDescriptor(GetIRContext(), instruction))); |
| 196 |
}); |
| 197 |
} |
| 198 |
|
| 199 |
} // namespace fuzz |
| 200 |
} // namespace spvtools |