| 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/transformation_make_vector_operation_dynamic.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/instruction_descriptor.h" |
| 20 |
#include "test/fuzz/fuzz_test_util.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
namespace { |
| 25 |
|
| 26 |
TEST(TransformationMakeVectorOperationDynamicTest, IsApplicable) { |
| 27 |
std::string reference_shader = R"( |
| 28 |
OpCapability Shader |
| 29 |
%1 = OpExtInstImport "GLSL.std.450" |
| 30 |
OpMemoryModel Logical GLSL450 |
| 31 |
OpEntryPoint Vertex %22 "main" |
| 32 |
|
| 33 |
; Types |
| 34 |
%2 = OpTypeVoid |
| 35 |
%3 = OpTypeFunction %2 |
| 36 |
%4 = OpTypeInt 32 0 |
| 37 |
%5 = OpTypeFloat 32 |
| 38 |
%6 = OpTypeVector %5 2 |
| 39 |
%7 = OpTypeVector %5 3 |
| 40 |
%8 = OpTypeVector %5 4 |
| 41 |
%9 = OpTypeMatrix %6 2 |
| 42 |
|
| 43 |
; Constant scalars |
| 44 |
%10 = OpConstant %4 0 |
| 45 |
%11 = OpConstant %4 1 |
| 46 |
%12 = OpConstant %4 2 |
| 47 |
%13 = OpConstant %5 0 |
| 48 |
%14 = OpConstant %5 1 |
| 49 |
%15 = OpConstant %5 2 |
| 50 |
%16 = OpConstant %5 3 |
| 51 |
|
| 52 |
; Constant composites |
| 53 |
%17 = OpConstantComposite %6 %13 %14 |
| 54 |
%18 = OpConstantComposite %6 %15 %16 |
| 55 |
%19 = OpConstantComposite %7 %13 %14 %15 |
| 56 |
%20 = OpConstantComposite %8 %13 %14 %15 %16 |
| 57 |
%21 = OpConstantComposite %9 %17 %18 |
| 58 |
|
| 59 |
; main function |
| 60 |
%22 = OpFunction %2 None %3 |
| 61 |
%23 = OpLabel |
| 62 |
%24 = OpCompositeExtract %5 %17 0 |
| 63 |
%25 = OpCompositeExtract %5 %17 1 |
| 64 |
%26 = OpCompositeExtract %5 %18 0 |
| 65 |
%27 = OpCompositeExtract %5 %18 1 |
| 66 |
%28 = OpCompositeExtract %5 %19 0 |
| 67 |
%29 = OpCompositeExtract %5 %19 1 |
| 68 |
%30 = OpCompositeExtract %5 %19 2 |
| 69 |
%31 = OpCompositeExtract %5 %20 0 |
| 70 |
%32 = OpCompositeExtract %5 %20 1 |
| 71 |
%33 = OpCompositeExtract %5 %20 2 |
| 72 |
%34 = OpCompositeExtract %5 %20 3 |
| 73 |
%35 = OpCompositeExtract %6 %21 0 |
| 74 |
%36 = OpCompositeExtract %6 %21 1 |
| 75 |
%37 = OpCompositeInsert %6 %15 %17 0 |
| 76 |
%38 = OpCompositeInsert %6 %16 %17 1 |
| 77 |
%39 = OpCompositeInsert %6 %13 %18 0 |
| 78 |
%40 = OpCompositeInsert %6 %14 %18 1 |
| 79 |
%41 = OpCompositeInsert %7 %13 %19 0 |
| 80 |
%42 = OpCompositeInsert %7 %14 %19 1 |
| 81 |
%43 = OpCompositeInsert %7 %15 %19 2 |
| 82 |
%44 = OpCompositeInsert %8 %13 %20 0 |
| 83 |
%45 = OpCompositeInsert %8 %14 %20 1 |
| 84 |
%46 = OpCompositeInsert %8 %15 %20 2 |
| 85 |
%47 = OpCompositeInsert %8 %16 %20 3 |
| 86 |
%48 = OpCompositeInsert %9 %17 %21 0 |
| 87 |
%49 = OpCompositeInsert %9 %18 %21 1 |
| 88 |
OpReturn |
| 89 |
OpFunctionEnd |
| 90 |
)"; |
| 91 |
|
| 92 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 93 |
const auto consumer = nullptr; |
| 94 |
const auto context = |
| 95 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 96 |
spvtools::ValidatorOptions validator_options; |
| 97 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 98 |
kConsoleMessageConsumer)); |
| 99 |
TransformationContext transformation_context( |
| 100 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 101 |
// Tests undefined instruction. |
| 102 |
auto transformation = TransformationMakeVectorOperationDynamic(50, 10); |
| 103 |
ASSERT_FALSE( |
| 104 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 105 |
|
| 106 |
// Tests non-composite instruction. |
| 107 |
transformation = TransformationMakeVectorOperationDynamic(23, 11); |
| 108 |
ASSERT_FALSE( |
| 109 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 110 |
|
| 111 |
// Tests composite being a matrix. |
| 112 |
transformation = TransformationMakeVectorOperationDynamic(48, 12); |
| 113 |
ASSERT_FALSE( |
| 114 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 115 |
|
| 116 |
// Tests literal not defined as constant. |
| 117 |
transformation = TransformationMakeVectorOperationDynamic(34, 51); |
| 118 |
ASSERT_FALSE( |
| 119 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 120 |
|
| 121 |
// Tests applicable instructions. |
| 122 |
transformation = TransformationMakeVectorOperationDynamic(24, 10); |
| 123 |
ASSERT_TRUE( |
| 124 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 125 |
|
| 126 |
transformation = TransformationMakeVectorOperationDynamic(25, 11); |
| 127 |
ASSERT_TRUE( |
| 128 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 129 |
|
| 130 |
transformation = TransformationMakeVectorOperationDynamic(26, 10); |
| 131 |
ASSERT_TRUE( |
| 132 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 133 |
|
| 134 |
transformation = TransformationMakeVectorOperationDynamic(37, 10); |
| 135 |
ASSERT_TRUE( |
| 136 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 137 |
|
| 138 |
transformation = TransformationMakeVectorOperationDynamic(38, 11); |
| 139 |
ASSERT_TRUE( |
| 140 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 141 |
|
| 142 |
transformation = TransformationMakeVectorOperationDynamic(39, 10); |
| 143 |
ASSERT_TRUE( |
| 144 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 145 |
} |
| 146 |
|
| 147 |
TEST(TransformationMakeVectorOperationDynamicTest, Apply) { |
| 148 |
std::string reference_shader = R"( |
| 149 |
OpCapability Shader |
| 150 |
%1 = OpExtInstImport "GLSL.std.450" |
| 151 |
OpMemoryModel Logical GLSL450 |
| 152 |
OpEntryPoint Vertex %20 "main" |
| 153 |
|
| 154 |
; Types |
| 155 |
%2 = OpTypeVoid |
| 156 |
%3 = OpTypeFunction %2 |
| 157 |
%4 = OpTypeInt 32 0 |
| 158 |
%5 = OpTypeFloat 32 |
| 159 |
%6 = OpTypeVector %5 2 |
| 160 |
%7 = OpTypeVector %5 3 |
| 161 |
%8 = OpTypeVector %5 4 |
| 162 |
|
| 163 |
; Constant scalars |
| 164 |
%9 = OpConstant %4 0 |
| 165 |
%10 = OpConstant %4 1 |
| 166 |
%11 = OpConstant %4 2 |
| 167 |
%12 = OpConstant %4 3 |
| 168 |
%13 = OpConstant %5 0 |
| 169 |
%14 = OpConstant %5 1 |
| 170 |
%15 = OpConstant %5 2 |
| 171 |
%16 = OpConstant %5 3 |
| 172 |
|
| 173 |
; Constant vectors |
| 174 |
%17 = OpConstantComposite %6 %13 %14 |
| 175 |
%18 = OpConstantComposite %7 %13 %14 %15 |
| 176 |
%19 = OpConstantComposite %8 %13 %14 %15 %16 |
| 177 |
|
| 178 |
; main function |
| 179 |
%20 = OpFunction %2 None %3 |
| 180 |
%21 = OpLabel |
| 181 |
%22 = OpCompositeExtract %5 %17 0 |
| 182 |
%23 = OpCompositeExtract %5 %17 1 |
| 183 |
%24 = OpCompositeExtract %5 %18 0 |
| 184 |
%25 = OpCompositeExtract %5 %18 1 |
| 185 |
%26 = OpCompositeExtract %5 %18 2 |
| 186 |
%27 = OpCompositeExtract %5 %19 0 |
| 187 |
%28 = OpCompositeExtract %5 %19 1 |
| 188 |
%29 = OpCompositeExtract %5 %19 2 |
| 189 |
%30 = OpCompositeExtract %5 %19 3 |
| 190 |
%31 = OpCompositeInsert %6 %13 %17 0 |
| 191 |
%32 = OpCompositeInsert %6 %14 %17 1 |
| 192 |
%33 = OpCompositeInsert %7 %13 %18 0 |
| 193 |
%34 = OpCompositeInsert %7 %14 %18 1 |
| 194 |
%35 = OpCompositeInsert %7 %15 %18 2 |
| 195 |
%36 = OpCompositeInsert %8 %13 %19 0 |
| 196 |
%37 = OpCompositeInsert %8 %14 %19 1 |
| 197 |
%38 = OpCompositeInsert %8 %15 %19 2 |
| 198 |
%39 = OpCompositeInsert %8 %16 %19 3 |
| 199 |
OpReturn |
| 200 |
OpFunctionEnd |
| 201 |
)"; |
| 202 |
|
| 203 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 204 |
const auto consumer = nullptr; |
| 205 |
const auto context = |
| 206 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 207 |
spvtools::ValidatorOptions validator_options; |
| 208 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 209 |
kConsoleMessageConsumer)); |
| 210 |
TransformationContext transformation_context( |
| 211 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 212 |
auto transformation = TransformationMakeVectorOperationDynamic(22, 9); |
| 213 |
ASSERT_TRUE( |
| 214 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 215 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 216 |
|
| 217 |
transformation = TransformationMakeVectorOperationDynamic(23, 10); |
| 218 |
ASSERT_TRUE( |
| 219 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 220 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 221 |
|
| 222 |
transformation = TransformationMakeVectorOperationDynamic(24, 9); |
| 223 |
ASSERT_TRUE( |
| 224 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 225 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 226 |
|
| 227 |
transformation = TransformationMakeVectorOperationDynamic(25, 10); |
| 228 |
ASSERT_TRUE( |
| 229 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 230 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 231 |
|
| 232 |
transformation = TransformationMakeVectorOperationDynamic(26, 11); |
| 233 |
ASSERT_TRUE( |
| 234 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 235 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 236 |
|
| 237 |
transformation = TransformationMakeVectorOperationDynamic(27, 9); |
| 238 |
ASSERT_TRUE( |
| 239 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 240 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 241 |
|
| 242 |
transformation = TransformationMakeVectorOperationDynamic(28, 10); |
| 243 |
ASSERT_TRUE( |
| 244 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 245 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 246 |
|
| 247 |
transformation = TransformationMakeVectorOperationDynamic(29, 11); |
| 248 |
ASSERT_TRUE( |
| 249 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 250 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 251 |
|
| 252 |
transformation = TransformationMakeVectorOperationDynamic(30, 12); |
| 253 |
ASSERT_TRUE( |
| 254 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 255 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 256 |
|
| 257 |
transformation = TransformationMakeVectorOperationDynamic(31, 9); |
| 258 |
ASSERT_TRUE( |
| 259 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 260 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 261 |
|
| 262 |
transformation = TransformationMakeVectorOperationDynamic(32, 10); |
| 263 |
ASSERT_TRUE( |
| 264 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 265 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 266 |
|
| 267 |
transformation = TransformationMakeVectorOperationDynamic(33, 9); |
| 268 |
ASSERT_TRUE( |
| 269 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 270 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 271 |
|
| 272 |
transformation = TransformationMakeVectorOperationDynamic(34, 10); |
| 273 |
ASSERT_TRUE( |
| 274 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 275 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 276 |
|
| 277 |
transformation = TransformationMakeVectorOperationDynamic(35, 11); |
| 278 |
ASSERT_TRUE( |
| 279 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 280 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 281 |
|
| 282 |
transformation = TransformationMakeVectorOperationDynamic(36, 9); |
| 283 |
ASSERT_TRUE( |
| 284 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 285 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 286 |
|
| 287 |
transformation = TransformationMakeVectorOperationDynamic(37, 10); |
| 288 |
ASSERT_TRUE( |
| 289 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 290 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 291 |
|
| 292 |
transformation = TransformationMakeVectorOperationDynamic(38, 11); |
| 293 |
ASSERT_TRUE( |
| 294 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 295 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 296 |
|
| 297 |
transformation = TransformationMakeVectorOperationDynamic(39, 12); |
| 298 |
ASSERT_TRUE( |
| 299 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 300 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 301 |
|
| 302 |
std::string variant_shader = R"( |
| 303 |
OpCapability Shader |
| 304 |
%1 = OpExtInstImport "GLSL.std.450" |
| 305 |
OpMemoryModel Logical GLSL450 |
| 306 |
OpEntryPoint Vertex %20 "main" |
| 307 |
|
| 308 |
; Types |
| 309 |
%2 = OpTypeVoid |
| 310 |
%3 = OpTypeFunction %2 |
| 311 |
%4 = OpTypeInt 32 0 |
| 312 |
%5 = OpTypeFloat 32 |
| 313 |
%6 = OpTypeVector %5 2 |
| 314 |
%7 = OpTypeVector %5 3 |
| 315 |
%8 = OpTypeVector %5 4 |
| 316 |
|
| 317 |
; Constant scalars |
| 318 |
%9 = OpConstant %4 0 |
| 319 |
%10 = OpConstant %4 1 |
| 320 |
%11 = OpConstant %4 2 |
| 321 |
%12 = OpConstant %4 3 |
| 322 |
%13 = OpConstant %5 0 |
| 323 |
%14 = OpConstant %5 1 |
| 324 |
%15 = OpConstant %5 2 |
| 325 |
%16 = OpConstant %5 3 |
| 326 |
|
| 327 |
; Constant vectors |
| 328 |
%17 = OpConstantComposite %6 %13 %14 |
| 329 |
%18 = OpConstantComposite %7 %13 %14 %15 |
| 330 |
%19 = OpConstantComposite %8 %13 %14 %15 %16 |
| 331 |
|
| 332 |
; main function |
| 333 |
%20 = OpFunction %2 None %3 |
| 334 |
%21 = OpLabel |
| 335 |
%22 = OpVectorExtractDynamic %5 %17 %9 |
| 336 |
%23 = OpVectorExtractDynamic %5 %17 %10 |
| 337 |
%24 = OpVectorExtractDynamic %5 %18 %9 |
| 338 |
%25 = OpVectorExtractDynamic %5 %18 %10 |
| 339 |
%26 = OpVectorExtractDynamic %5 %18 %11 |
| 340 |
%27 = OpVectorExtractDynamic %5 %19 %9 |
| 341 |
%28 = OpVectorExtractDynamic %5 %19 %10 |
| 342 |
%29 = OpVectorExtractDynamic %5 %19 %11 |
| 343 |
%30 = OpVectorExtractDynamic %5 %19 %12 |
| 344 |
%31 = OpVectorInsertDynamic %6 %17 %13 %9 |
| 345 |
%32 = OpVectorInsertDynamic %6 %17 %14 %10 |
| 346 |
%33 = OpVectorInsertDynamic %7 %18 %13 %9 |
| 347 |
%34 = OpVectorInsertDynamic %7 %18 %14 %10 |
| 348 |
%35 = OpVectorInsertDynamic %7 %18 %15 %11 |
| 349 |
%36 = OpVectorInsertDynamic %8 %19 %13 %9 |
| 350 |
%37 = OpVectorInsertDynamic %8 %19 %14 %10 |
| 351 |
%38 = OpVectorInsertDynamic %8 %19 %15 %11 |
| 352 |
%39 = OpVectorInsertDynamic %8 %19 %16 %12 |
| 353 |
OpReturn |
| 354 |
OpFunctionEnd |
| 355 |
)"; |
| 356 |
|
| 357 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 358 |
kConsoleMessageConsumer)); |
| 359 |
ASSERT_TRUE(IsEqual(env, variant_shader, context.get())); |
| 360 |
} |
| 361 |
|
| 362 |
} // namespace |
| 363 |
} // namespace fuzz |
| 364 |
} // namespace spvtools |