| 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_push_id_through_variable.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(TransformationPushIdThroughVariableTest, IsApplicable) { |
| 27 |
std::string reference_shader = R"( |
| 28 |
OpCapability Shader |
| 29 |
%1 = OpExtInstImport "GLSL.std.450" |
| 30 |
OpMemoryModel Logical GLSL450 |
| 31 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 32 |
OpExecutionMode %4 OriginUpperLeft |
| 33 |
OpSource ESSL 310 |
| 34 |
OpDecorate %92 BuiltIn FragCoord |
| 35 |
%2 = OpTypeVoid |
| 36 |
%3 = OpTypeFunction %2 |
| 37 |
%6 = OpTypeInt 32 1 |
| 38 |
%7 = OpTypeFloat 32 |
| 39 |
%8 = OpTypeStruct %6 %7 |
| 40 |
%9 = OpTypePointer Function %8 |
| 41 |
%10 = OpTypeFunction %6 %9 |
| 42 |
%14 = OpConstant %6 0 |
| 43 |
%15 = OpTypePointer Function %6 |
| 44 |
%51 = OpTypePointer Private %6 |
| 45 |
%21 = OpConstant %6 2 |
| 46 |
%23 = OpConstant %6 1 |
| 47 |
%24 = OpConstant %7 1 |
| 48 |
%25 = OpTypePointer Function %7 |
| 49 |
%50 = OpTypePointer Private %7 |
| 50 |
%34 = OpTypeBool |
| 51 |
%35 = OpConstantFalse %34 |
| 52 |
%60 = OpConstantNull %50 |
| 53 |
%61 = OpUndef %51 |
| 54 |
%52 = OpVariable %50 Private |
| 55 |
%53 = OpVariable %51 Private |
| 56 |
%80 = OpConstantComposite %8 %21 %24 |
| 57 |
%90 = OpTypeVector %7 4 |
| 58 |
%91 = OpTypePointer Input %90 |
| 59 |
%92 = OpVariable %91 Input |
| 60 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 61 |
%4 = OpFunction %2 None %3 |
| 62 |
%5 = OpLabel |
| 63 |
%20 = OpVariable %9 Function |
| 64 |
%27 = OpVariable %9 Function |
| 65 |
%22 = OpAccessChain %15 %20 %14 |
| 66 |
%44 = OpCopyObject %9 %20 |
| 67 |
%26 = OpAccessChain %25 %20 %23 |
| 68 |
%29 = OpFunctionCall %6 %12 %27 |
| 69 |
%30 = OpAccessChain %15 %20 %14 |
| 70 |
%45 = OpCopyObject %15 %30 |
| 71 |
%81 = OpCopyObject %9 %27 |
| 72 |
%33 = OpAccessChain %15 %20 %14 |
| 73 |
OpSelectionMerge %37 None |
| 74 |
OpBranchConditional %35 %36 %37 |
| 75 |
%36 = OpLabel |
| 76 |
%38 = OpAccessChain %15 %20 %14 |
| 77 |
%40 = OpAccessChain %15 %20 %14 |
| 78 |
%43 = OpAccessChain %15 %20 %14 |
| 79 |
%82 = OpCopyObject %9 %27 |
| 80 |
OpBranch %37 |
| 81 |
%37 = OpLabel |
| 82 |
OpReturn |
| 83 |
OpFunctionEnd |
| 84 |
%12 = OpFunction %6 None %10 |
| 85 |
%11 = OpFunctionParameter %9 |
| 86 |
%13 = OpLabel |
| 87 |
%46 = OpCopyObject %9 %11 |
| 88 |
%16 = OpAccessChain %15 %11 %14 |
| 89 |
%95 = OpCopyObject %8 %80 |
| 90 |
OpReturnValue %21 |
| 91 |
%100 = OpLabel |
| 92 |
OpUnreachable |
| 93 |
OpFunctionEnd |
| 94 |
)"; |
| 95 |
|
| 96 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 97 |
const auto consumer = nullptr; |
| 98 |
const auto context = |
| 99 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 100 |
|
| 101 |
spvtools::ValidatorOptions validator_options; |
| 102 |
TransformationContext transformation_context( |
| 103 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 104 |
// Tests the reference shader validity. |
| 105 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 106 |
kConsoleMessageConsumer)); |
| 107 |
|
| 108 |
// Tests |value_synonym_id| and |variable_id| are fresh ids. |
| 109 |
uint32_t value_id = 21; |
| 110 |
uint32_t value_synonym_id = 62; |
| 111 |
uint32_t variable_id = 63; |
| 112 |
uint32_t initializer_id = 23; |
| 113 |
uint32_t variable_storage_class = SpvStorageClassPrivate; |
| 114 |
auto instruction_descriptor = |
| 115 |
MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 116 |
auto transformation = TransformationPushIdThroughVariable( |
| 117 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 118 |
initializer_id, instruction_descriptor); |
| 119 |
ASSERT_TRUE( |
| 120 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 121 |
|
| 122 |
// Tests |value_synonym_id| and |variable_id| are non-fresh ids. |
| 123 |
value_id = 80; |
| 124 |
value_synonym_id = 60; |
| 125 |
variable_id = 61; |
| 126 |
initializer_id = 80; |
| 127 |
variable_storage_class = SpvStorageClassFunction; |
| 128 |
instruction_descriptor = MakeInstructionDescriptor(38, SpvOpAccessChain, 0); |
| 129 |
transformation = TransformationPushIdThroughVariable( |
| 130 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 131 |
initializer_id, instruction_descriptor); |
| 132 |
ASSERT_FALSE( |
| 133 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 134 |
|
| 135 |
// The instruction to insert before is not defined. |
| 136 |
value_id = 80; |
| 137 |
value_synonym_id = 62; |
| 138 |
variable_id = 63; |
| 139 |
initializer_id = 80; |
| 140 |
variable_storage_class = SpvStorageClassFunction; |
| 141 |
instruction_descriptor = MakeInstructionDescriptor(64, SpvOpAccessChain, 0); |
| 142 |
transformation = TransformationPushIdThroughVariable( |
| 143 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 144 |
initializer_id, instruction_descriptor); |
| 145 |
ASSERT_FALSE( |
| 146 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 147 |
|
| 148 |
// Attempting to insert the store and load instructions |
| 149 |
// before an OpVariable instruction. |
| 150 |
value_id = 24; |
| 151 |
value_synonym_id = 62; |
| 152 |
variable_id = 63; |
| 153 |
initializer_id = 24; |
| 154 |
variable_storage_class = SpvStorageClassFunction; |
| 155 |
instruction_descriptor = MakeInstructionDescriptor(27, SpvOpVariable, 0); |
| 156 |
transformation = TransformationPushIdThroughVariable( |
| 157 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 158 |
initializer_id, instruction_descriptor); |
| 159 |
ASSERT_FALSE( |
| 160 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 161 |
|
| 162 |
// The block containing instruction descriptor must be reachable. |
| 163 |
value_id = 80; |
| 164 |
value_synonym_id = 62; |
| 165 |
variable_id = 63; |
| 166 |
initializer_id = 80; |
| 167 |
variable_storage_class = SpvStorageClassFunction; |
| 168 |
instruction_descriptor = MakeInstructionDescriptor(100, SpvOpUnreachable, 0); |
| 169 |
transformation = TransformationPushIdThroughVariable( |
| 170 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 171 |
initializer_id, instruction_descriptor); |
| 172 |
ASSERT_FALSE( |
| 173 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 174 |
|
| 175 |
// Tests value instruction not available. |
| 176 |
value_id = 64; |
| 177 |
value_synonym_id = 62; |
| 178 |
variable_id = 63; |
| 179 |
initializer_id = 23; |
| 180 |
variable_storage_class = SpvStorageClassFunction; |
| 181 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 182 |
transformation = TransformationPushIdThroughVariable( |
| 183 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 184 |
initializer_id, instruction_descriptor); |
| 185 |
ASSERT_FALSE( |
| 186 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 187 |
|
| 188 |
// Tests pointer type not available. |
| 189 |
value_id = 80; |
| 190 |
value_synonym_id = 62; |
| 191 |
variable_id = 63; |
| 192 |
initializer_id = 80; |
| 193 |
variable_storage_class = SpvStorageClassPrivate; |
| 194 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 195 |
transformation = TransformationPushIdThroughVariable( |
| 196 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 197 |
initializer_id, instruction_descriptor); |
| 198 |
ASSERT_FALSE( |
| 199 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 200 |
|
| 201 |
// Tests not a private nor function storage class. |
| 202 |
value_id = 93; |
| 203 |
value_synonym_id = 62; |
| 204 |
variable_id = 63; |
| 205 |
initializer_id = 93; |
| 206 |
variable_storage_class = SpvStorageClassInput; |
| 207 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 208 |
transformation = TransformationPushIdThroughVariable( |
| 209 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 210 |
initializer_id, instruction_descriptor); |
| 211 |
#ifndef NDEBUG |
| 212 |
ASSERT_DEATH( |
| 213 |
transformation.IsApplicable(context.get(), transformation_context), |
| 214 |
"The variable storage class must be private or function"); |
| 215 |
#endif |
| 216 |
|
| 217 |
// Tests value instruction not available before instruction. |
| 218 |
value_id = 95; |
| 219 |
value_synonym_id = 62; |
| 220 |
variable_id = 63; |
| 221 |
initializer_id = 80; |
| 222 |
variable_storage_class = SpvStorageClassFunction; |
| 223 |
instruction_descriptor = MakeInstructionDescriptor(40, SpvOpAccessChain, 0); |
| 224 |
transformation = TransformationPushIdThroughVariable( |
| 225 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 226 |
initializer_id, instruction_descriptor); |
| 227 |
ASSERT_FALSE( |
| 228 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 229 |
|
| 230 |
// Variable initializer is not constant. |
| 231 |
value_id = 95; |
| 232 |
value_synonym_id = 62; |
| 233 |
variable_id = 63; |
| 234 |
initializer_id = 95; |
| 235 |
variable_storage_class = SpvStorageClassFunction; |
| 236 |
instruction_descriptor = MakeInstructionDescriptor(40, SpvOpAccessChain, 0); |
| 237 |
transformation = TransformationPushIdThroughVariable( |
| 238 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 239 |
initializer_id, instruction_descriptor); |
| 240 |
ASSERT_FALSE( |
| 241 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 242 |
|
| 243 |
// Variable initializer has wrong type. |
| 244 |
value_id = 95; |
| 245 |
value_synonym_id = 62; |
| 246 |
variable_id = 63; |
| 247 |
initializer_id = 93; |
| 248 |
variable_storage_class = SpvStorageClassFunction; |
| 249 |
instruction_descriptor = MakeInstructionDescriptor(40, SpvOpAccessChain, 0); |
| 250 |
transformation = TransformationPushIdThroughVariable( |
| 251 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 252 |
initializer_id, instruction_descriptor); |
| 253 |
ASSERT_FALSE( |
| 254 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 255 |
} |
| 256 |
|
| 257 |
TEST(TransformationPushIdThroughVariableTest, Apply) { |
| 258 |
std::string reference_shader = R"( |
| 259 |
OpCapability Shader |
| 260 |
%1 = OpExtInstImport "GLSL.std.450" |
| 261 |
OpMemoryModel Logical GLSL450 |
| 262 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 263 |
OpExecutionMode %4 OriginUpperLeft |
| 264 |
OpSource ESSL 310 |
| 265 |
OpDecorate %92 BuiltIn FragCoord |
| 266 |
%2 = OpTypeVoid |
| 267 |
%3 = OpTypeFunction %2 |
| 268 |
%6 = OpTypeInt 32 1 |
| 269 |
%7 = OpTypeFloat 32 |
| 270 |
%8 = OpTypeStruct %6 %7 |
| 271 |
%9 = OpTypePointer Function %8 |
| 272 |
%10 = OpTypeFunction %6 %9 |
| 273 |
%14 = OpConstant %6 0 |
| 274 |
%15 = OpTypePointer Function %6 |
| 275 |
%51 = OpTypePointer Private %6 |
| 276 |
%21 = OpConstant %6 2 |
| 277 |
%23 = OpConstant %6 1 |
| 278 |
%24 = OpConstant %7 1 |
| 279 |
%25 = OpTypePointer Function %7 |
| 280 |
%50 = OpTypePointer Private %7 |
| 281 |
%34 = OpTypeBool |
| 282 |
%35 = OpConstantFalse %34 |
| 283 |
%60 = OpConstantNull %50 |
| 284 |
%61 = OpUndef %51 |
| 285 |
%52 = OpVariable %50 Private |
| 286 |
%53 = OpVariable %51 Private |
| 287 |
%80 = OpConstantComposite %8 %21 %24 |
| 288 |
%90 = OpTypeVector %7 4 |
| 289 |
%91 = OpTypePointer Input %90 |
| 290 |
%92 = OpVariable %91 Input |
| 291 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 292 |
%4 = OpFunction %2 None %3 |
| 293 |
%5 = OpLabel |
| 294 |
%20 = OpVariable %9 Function |
| 295 |
%27 = OpVariable %9 Function |
| 296 |
OpStore %53 %21 |
| 297 |
%22 = OpAccessChain %15 %20 %14 |
| 298 |
%44 = OpCopyObject %9 %20 |
| 299 |
%26 = OpAccessChain %25 %20 %23 |
| 300 |
%29 = OpFunctionCall %6 %12 %27 |
| 301 |
%30 = OpAccessChain %15 %20 %14 |
| 302 |
%45 = OpCopyObject %15 %30 |
| 303 |
%81 = OpCopyObject %9 %27 |
| 304 |
%33 = OpAccessChain %15 %20 %14 |
| 305 |
OpSelectionMerge %37 None |
| 306 |
OpBranchConditional %35 %36 %37 |
| 307 |
%36 = OpLabel |
| 308 |
%38 = OpAccessChain %15 %20 %14 |
| 309 |
%40 = OpAccessChain %15 %20 %14 |
| 310 |
%43 = OpAccessChain %15 %20 %14 |
| 311 |
%82 = OpCopyObject %9 %27 |
| 312 |
OpBranch %37 |
| 313 |
%37 = OpLabel |
| 314 |
OpReturn |
| 315 |
OpFunctionEnd |
| 316 |
%12 = OpFunction %6 None %10 |
| 317 |
%11 = OpFunctionParameter %9 |
| 318 |
%13 = OpLabel |
| 319 |
%46 = OpCopyObject %9 %11 |
| 320 |
%16 = OpAccessChain %15 %11 %14 |
| 321 |
%95 = OpCopyObject %8 %80 |
| 322 |
OpReturnValue %21 |
| 323 |
OpFunctionEnd |
| 324 |
)"; |
| 325 |
|
| 326 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 327 |
const auto consumer = nullptr; |
| 328 |
const auto context = |
| 329 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 330 |
|
| 331 |
spvtools::ValidatorOptions validator_options; |
| 332 |
TransformationContext transformation_context( |
| 333 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 334 |
uint32_t value_id = 80; |
| 335 |
uint32_t value_synonym_id = 100; |
| 336 |
uint32_t variable_id = 101; |
| 337 |
uint32_t initializer_id = 80; |
| 338 |
uint32_t variable_storage_class = SpvStorageClassFunction; |
| 339 |
auto instruction_descriptor = |
| 340 |
MakeInstructionDescriptor(38, SpvOpAccessChain, 0); |
| 341 |
auto transformation = TransformationPushIdThroughVariable( |
| 342 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 343 |
initializer_id, instruction_descriptor); |
| 344 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 345 |
|
| 346 |
value_id = 21; |
| 347 |
value_synonym_id = 102; |
| 348 |
variable_id = 103; |
| 349 |
initializer_id = 21; |
| 350 |
variable_storage_class = SpvStorageClassFunction; |
| 351 |
instruction_descriptor = MakeInstructionDescriptor(38, SpvOpAccessChain, 0); |
| 352 |
transformation = TransformationPushIdThroughVariable( |
| 353 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 354 |
initializer_id, instruction_descriptor); |
| 355 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 356 |
|
| 357 |
value_id = 95; |
| 358 |
value_synonym_id = 104; |
| 359 |
variable_id = 105; |
| 360 |
initializer_id = 80; |
| 361 |
variable_storage_class = SpvStorageClassFunction; |
| 362 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 363 |
transformation = TransformationPushIdThroughVariable( |
| 364 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 365 |
initializer_id, instruction_descriptor); |
| 366 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 367 |
|
| 368 |
value_id = 80; |
| 369 |
value_synonym_id = 106; |
| 370 |
variable_id = 107; |
| 371 |
initializer_id = 80; |
| 372 |
variable_storage_class = SpvStorageClassFunction; |
| 373 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 374 |
transformation = TransformationPushIdThroughVariable( |
| 375 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 376 |
initializer_id, instruction_descriptor); |
| 377 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 378 |
|
| 379 |
value_id = 21; |
| 380 |
value_synonym_id = 108; |
| 381 |
variable_id = 109; |
| 382 |
initializer_id = 21; |
| 383 |
variable_storage_class = SpvStorageClassPrivate; |
| 384 |
instruction_descriptor = MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 385 |
transformation = TransformationPushIdThroughVariable( |
| 386 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 387 |
initializer_id, instruction_descriptor); |
| 388 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 389 |
|
| 390 |
value_id = 23; |
| 391 |
value_synonym_id = 110; |
| 392 |
variable_id = 111; |
| 393 |
initializer_id = 21; |
| 394 |
variable_storage_class = SpvStorageClassPrivate; |
| 395 |
instruction_descriptor = MakeInstructionDescriptor(27, SpvOpStore, 0); |
| 396 |
transformation = TransformationPushIdThroughVariable( |
| 397 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 398 |
initializer_id, instruction_descriptor); |
| 399 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 400 |
|
| 401 |
std::string variant_shader = R"( |
| 402 |
OpCapability Shader |
| 403 |
%1 = OpExtInstImport "GLSL.std.450" |
| 404 |
OpMemoryModel Logical GLSL450 |
| 405 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 %109 %111 |
| 406 |
OpExecutionMode %4 OriginUpperLeft |
| 407 |
OpSource ESSL 310 |
| 408 |
OpDecorate %92 BuiltIn FragCoord |
| 409 |
%2 = OpTypeVoid |
| 410 |
%3 = OpTypeFunction %2 |
| 411 |
%6 = OpTypeInt 32 1 |
| 412 |
%7 = OpTypeFloat 32 |
| 413 |
%8 = OpTypeStruct %6 %7 |
| 414 |
%9 = OpTypePointer Function %8 |
| 415 |
%10 = OpTypeFunction %6 %9 |
| 416 |
%14 = OpConstant %6 0 |
| 417 |
%15 = OpTypePointer Function %6 |
| 418 |
%51 = OpTypePointer Private %6 |
| 419 |
%21 = OpConstant %6 2 |
| 420 |
%23 = OpConstant %6 1 |
| 421 |
%24 = OpConstant %7 1 |
| 422 |
%25 = OpTypePointer Function %7 |
| 423 |
%50 = OpTypePointer Private %7 |
| 424 |
%34 = OpTypeBool |
| 425 |
%35 = OpConstantFalse %34 |
| 426 |
%60 = OpConstantNull %50 |
| 427 |
%61 = OpUndef %51 |
| 428 |
%52 = OpVariable %50 Private |
| 429 |
%53 = OpVariable %51 Private |
| 430 |
%80 = OpConstantComposite %8 %21 %24 |
| 431 |
%90 = OpTypeVector %7 4 |
| 432 |
%91 = OpTypePointer Input %90 |
| 433 |
%92 = OpVariable %91 Input |
| 434 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 435 |
%109 = OpVariable %51 Private %21 |
| 436 |
%111 = OpVariable %51 Private %21 |
| 437 |
%4 = OpFunction %2 None %3 |
| 438 |
%5 = OpLabel |
| 439 |
%103 = OpVariable %15 Function %21 |
| 440 |
%101 = OpVariable %9 Function %80 |
| 441 |
%20 = OpVariable %9 Function |
| 442 |
%27 = OpVariable %9 Function |
| 443 |
OpStore %111 %23 |
| 444 |
%110 = OpLoad %6 %111 |
| 445 |
OpStore %53 %21 |
| 446 |
%22 = OpAccessChain %15 %20 %14 |
| 447 |
%44 = OpCopyObject %9 %20 |
| 448 |
%26 = OpAccessChain %25 %20 %23 |
| 449 |
%29 = OpFunctionCall %6 %12 %27 |
| 450 |
%30 = OpAccessChain %15 %20 %14 |
| 451 |
%45 = OpCopyObject %15 %30 |
| 452 |
%81 = OpCopyObject %9 %27 |
| 453 |
%33 = OpAccessChain %15 %20 %14 |
| 454 |
OpSelectionMerge %37 None |
| 455 |
OpBranchConditional %35 %36 %37 |
| 456 |
%36 = OpLabel |
| 457 |
OpStore %101 %80 |
| 458 |
%100 = OpLoad %8 %101 |
| 459 |
OpStore %103 %21 |
| 460 |
%102 = OpLoad %6 %103 |
| 461 |
%38 = OpAccessChain %15 %20 %14 |
| 462 |
%40 = OpAccessChain %15 %20 %14 |
| 463 |
%43 = OpAccessChain %15 %20 %14 |
| 464 |
%82 = OpCopyObject %9 %27 |
| 465 |
OpBranch %37 |
| 466 |
%37 = OpLabel |
| 467 |
OpReturn |
| 468 |
OpFunctionEnd |
| 469 |
%12 = OpFunction %6 None %10 |
| 470 |
%11 = OpFunctionParameter %9 |
| 471 |
%13 = OpLabel |
| 472 |
%107 = OpVariable %9 Function %80 |
| 473 |
%105 = OpVariable %9 Function %80 |
| 474 |
%46 = OpCopyObject %9 %11 |
| 475 |
%16 = OpAccessChain %15 %11 %14 |
| 476 |
%95 = OpCopyObject %8 %80 |
| 477 |
OpStore %105 %95 |
| 478 |
%104 = OpLoad %8 %105 |
| 479 |
OpStore %107 %80 |
| 480 |
%106 = OpLoad %8 %107 |
| 481 |
OpStore %109 %21 |
| 482 |
%108 = OpLoad %6 %109 |
| 483 |
OpReturnValue %21 |
| 484 |
OpFunctionEnd |
| 485 |
)"; |
| 486 |
|
| 487 |
ASSERT_TRUE(IsEqual(env, variant_shader, context.get())); |
| 488 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 489 |
MakeDataDescriptor(80, {}), MakeDataDescriptor(100, {}))); |
| 490 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 491 |
MakeDataDescriptor(21, {}), MakeDataDescriptor(102, {}))); |
| 492 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 493 |
MakeDataDescriptor(95, {}), MakeDataDescriptor(104, {}))); |
| 494 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 495 |
MakeDataDescriptor(80, {}), MakeDataDescriptor(106, {}))); |
| 496 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 497 |
MakeDataDescriptor(21, {}), MakeDataDescriptor(108, {}))); |
| 498 |
} |
| 499 |
|
| 500 |
TEST(TransformationPushIdThroughVariableTest, AddSynonymsForRelevantIds) { |
| 501 |
std::string reference_shader = R"( |
| 502 |
OpCapability Shader |
| 503 |
%1 = OpExtInstImport "GLSL.std.450" |
| 504 |
OpMemoryModel Logical GLSL450 |
| 505 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 506 |
OpExecutionMode %4 OriginUpperLeft |
| 507 |
OpSource ESSL 310 |
| 508 |
OpDecorate %92 BuiltIn FragCoord |
| 509 |
%2 = OpTypeVoid |
| 510 |
%3 = OpTypeFunction %2 |
| 511 |
%6 = OpTypeInt 32 1 |
| 512 |
%7 = OpTypeFloat 32 |
| 513 |
%8 = OpTypeStruct %6 %7 |
| 514 |
%9 = OpTypePointer Function %8 |
| 515 |
%10 = OpTypeFunction %6 %9 |
| 516 |
%14 = OpConstant %6 0 |
| 517 |
%15 = OpTypePointer Function %6 |
| 518 |
%51 = OpTypePointer Private %6 |
| 519 |
%21 = OpConstant %6 2 |
| 520 |
%23 = OpConstant %6 1 |
| 521 |
%24 = OpConstant %7 1 |
| 522 |
%25 = OpTypePointer Function %7 |
| 523 |
%50 = OpTypePointer Private %7 |
| 524 |
%34 = OpTypeBool |
| 525 |
%35 = OpConstantFalse %34 |
| 526 |
%60 = OpConstantNull %50 |
| 527 |
%61 = OpUndef %51 |
| 528 |
%52 = OpVariable %50 Private |
| 529 |
%53 = OpVariable %51 Private |
| 530 |
%80 = OpConstantComposite %8 %21 %24 |
| 531 |
%90 = OpTypeVector %7 4 |
| 532 |
%91 = OpTypePointer Input %90 |
| 533 |
%92 = OpVariable %91 Input |
| 534 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 535 |
%4 = OpFunction %2 None %3 |
| 536 |
%5 = OpLabel |
| 537 |
%20 = OpVariable %9 Function |
| 538 |
%27 = OpVariable %9 Function |
| 539 |
%22 = OpAccessChain %15 %20 %14 |
| 540 |
%44 = OpCopyObject %9 %20 |
| 541 |
%26 = OpAccessChain %25 %20 %23 |
| 542 |
%29 = OpFunctionCall %6 %12 %27 |
| 543 |
%30 = OpAccessChain %15 %20 %14 |
| 544 |
%45 = OpCopyObject %15 %30 |
| 545 |
%81 = OpCopyObject %9 %27 |
| 546 |
%33 = OpAccessChain %15 %20 %14 |
| 547 |
OpSelectionMerge %37 None |
| 548 |
OpBranchConditional %35 %36 %37 |
| 549 |
%36 = OpLabel |
| 550 |
%38 = OpAccessChain %15 %20 %14 |
| 551 |
%40 = OpAccessChain %15 %20 %14 |
| 552 |
%43 = OpAccessChain %15 %20 %14 |
| 553 |
%82 = OpCopyObject %9 %27 |
| 554 |
OpBranch %37 |
| 555 |
%37 = OpLabel |
| 556 |
OpReturn |
| 557 |
OpFunctionEnd |
| 558 |
%12 = OpFunction %6 None %10 |
| 559 |
%11 = OpFunctionParameter %9 |
| 560 |
%13 = OpLabel |
| 561 |
%46 = OpCopyObject %9 %11 |
| 562 |
%16 = OpAccessChain %15 %11 %14 |
| 563 |
%95 = OpCopyObject %8 %80 |
| 564 |
OpReturnValue %21 |
| 565 |
%100 = OpLabel |
| 566 |
OpUnreachable |
| 567 |
OpFunctionEnd |
| 568 |
)"; |
| 569 |
|
| 570 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 571 |
const auto consumer = nullptr; |
| 572 |
const auto context = |
| 573 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 574 |
|
| 575 |
spvtools::ValidatorOptions validator_options; |
| 576 |
TransformationContext transformation_context( |
| 577 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 578 |
// Tests the reference shader validity. |
| 579 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 580 |
kConsoleMessageConsumer)); |
| 581 |
|
| 582 |
uint32_t value_id = 21; |
| 583 |
uint32_t value_synonym_id = 62; |
| 584 |
uint32_t variable_id = 63; |
| 585 |
uint32_t initializer_id = 23; |
| 586 |
uint32_t variable_storage_class = SpvStorageClassPrivate; |
| 587 |
auto instruction_descriptor = |
| 588 |
MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 589 |
auto transformation = TransformationPushIdThroughVariable( |
| 590 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 591 |
initializer_id, instruction_descriptor); |
| 592 |
ASSERT_TRUE( |
| 593 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 594 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 595 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 596 |
kConsoleMessageConsumer)); |
| 597 |
ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous( |
| 598 |
MakeDataDescriptor(21, {}), MakeDataDescriptor(62, {}))); |
| 599 |
} |
| 600 |
|
| 601 |
TEST(TransformationPushIdThroughVariableTest, DontAddSynonymsForIrrelevantIds) { |
| 602 |
std::string reference_shader = R"( |
| 603 |
OpCapability Shader |
| 604 |
%1 = OpExtInstImport "GLSL.std.450" |
| 605 |
OpMemoryModel Logical GLSL450 |
| 606 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 607 |
OpExecutionMode %4 OriginUpperLeft |
| 608 |
OpSource ESSL 310 |
| 609 |
OpDecorate %92 BuiltIn FragCoord |
| 610 |
%2 = OpTypeVoid |
| 611 |
%3 = OpTypeFunction %2 |
| 612 |
%6 = OpTypeInt 32 1 |
| 613 |
%7 = OpTypeFloat 32 |
| 614 |
%8 = OpTypeStruct %6 %7 |
| 615 |
%9 = OpTypePointer Function %8 |
| 616 |
%10 = OpTypeFunction %6 %9 |
| 617 |
%14 = OpConstant %6 0 |
| 618 |
%15 = OpTypePointer Function %6 |
| 619 |
%51 = OpTypePointer Private %6 |
| 620 |
%21 = OpConstant %6 2 |
| 621 |
%23 = OpConstant %6 1 |
| 622 |
%24 = OpConstant %7 1 |
| 623 |
%25 = OpTypePointer Function %7 |
| 624 |
%50 = OpTypePointer Private %7 |
| 625 |
%34 = OpTypeBool |
| 626 |
%35 = OpConstantFalse %34 |
| 627 |
%60 = OpConstantNull %50 |
| 628 |
%61 = OpUndef %51 |
| 629 |
%52 = OpVariable %50 Private |
| 630 |
%53 = OpVariable %51 Private |
| 631 |
%80 = OpConstantComposite %8 %21 %24 |
| 632 |
%90 = OpTypeVector %7 4 |
| 633 |
%91 = OpTypePointer Input %90 |
| 634 |
%92 = OpVariable %91 Input |
| 635 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 636 |
%4 = OpFunction %2 None %3 |
| 637 |
%5 = OpLabel |
| 638 |
%20 = OpVariable %9 Function |
| 639 |
%27 = OpVariable %9 Function |
| 640 |
%22 = OpAccessChain %15 %20 %14 |
| 641 |
%44 = OpCopyObject %9 %20 |
| 642 |
%26 = OpAccessChain %25 %20 %23 |
| 643 |
%29 = OpFunctionCall %6 %12 %27 |
| 644 |
%30 = OpAccessChain %15 %20 %14 |
| 645 |
%45 = OpCopyObject %15 %30 |
| 646 |
%81 = OpCopyObject %9 %27 |
| 647 |
%33 = OpAccessChain %15 %20 %14 |
| 648 |
OpSelectionMerge %37 None |
| 649 |
OpBranchConditional %35 %36 %37 |
| 650 |
%36 = OpLabel |
| 651 |
%38 = OpAccessChain %15 %20 %14 |
| 652 |
%40 = OpAccessChain %15 %20 %14 |
| 653 |
%43 = OpAccessChain %15 %20 %14 |
| 654 |
%82 = OpCopyObject %9 %27 |
| 655 |
OpBranch %37 |
| 656 |
%37 = OpLabel |
| 657 |
OpReturn |
| 658 |
OpFunctionEnd |
| 659 |
%12 = OpFunction %6 None %10 |
| 660 |
%11 = OpFunctionParameter %9 |
| 661 |
%13 = OpLabel |
| 662 |
%46 = OpCopyObject %9 %11 |
| 663 |
%16 = OpAccessChain %15 %11 %14 |
| 664 |
%95 = OpCopyObject %8 %80 |
| 665 |
OpReturnValue %21 |
| 666 |
%100 = OpLabel |
| 667 |
OpUnreachable |
| 668 |
OpFunctionEnd |
| 669 |
)"; |
| 670 |
|
| 671 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 672 |
const auto consumer = nullptr; |
| 673 |
const auto context = |
| 674 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 675 |
|
| 676 |
spvtools::ValidatorOptions validator_options; |
| 677 |
TransformationContext transformation_context( |
| 678 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 679 |
// Tests the reference shader validity. |
| 680 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 681 |
kConsoleMessageConsumer)); |
| 682 |
|
| 683 |
transformation_context.GetFactManager()->AddFactIdIsIrrelevant(21); |
| 684 |
|
| 685 |
uint32_t value_id = 21; |
| 686 |
uint32_t value_synonym_id = 62; |
| 687 |
uint32_t variable_id = 63; |
| 688 |
uint32_t initializer_id = 23; |
| 689 |
uint32_t variable_storage_class = SpvStorageClassPrivate; |
| 690 |
auto instruction_descriptor = |
| 691 |
MakeInstructionDescriptor(95, SpvOpReturnValue, 0); |
| 692 |
auto transformation = TransformationPushIdThroughVariable( |
| 693 |
value_id, value_synonym_id, variable_id, variable_storage_class, |
| 694 |
initializer_id, instruction_descriptor); |
| 695 |
ASSERT_TRUE( |
| 696 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 697 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 698 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 699 |
kConsoleMessageConsumer)); |
| 700 |
ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous( |
| 701 |
MakeDataDescriptor(21, {}), MakeDataDescriptor(62, {}))); |
| 702 |
} |
| 703 |
|
| 704 |
TEST(TransformationPushIdThroughVariableTest, DontAddSynonymsInDeadBlocks) { |
| 705 |
std::string reference_shader = R"( |
| 706 |
OpCapability Shader |
| 707 |
%1 = OpExtInstImport "GLSL.std.450" |
| 708 |
OpMemoryModel Logical GLSL450 |
| 709 |
OpEntryPoint Fragment %4 "main" |
| 710 |
OpExecutionMode %4 OriginUpperLeft |
| 711 |
OpSource ESSL 320 |
| 712 |
%2 = OpTypeVoid |
| 713 |
%3 = OpTypeFunction %2 |
| 714 |
%6 = OpTypeInt 32 1 |
| 715 |
%7 = OpTypeVector %6 2 |
| 716 |
%8 = OpTypePointer Function %7 |
| 717 |
%10 = OpConstant %6 0 |
| 718 |
%11 = OpConstant %6 1 |
| 719 |
%12 = OpConstantComposite %7 %10 %11 |
| 720 |
%13 = OpTypeBool |
| 721 |
%50 = OpTypePointer Function %13 |
| 722 |
%14 = OpConstantFalse %13 |
| 723 |
%4 = OpFunction %2 None %3 |
| 724 |
%5 = OpLabel |
| 725 |
%9 = OpVariable %8 Function |
| 726 |
OpStore %9 %12 |
| 727 |
OpSelectionMerge %16 None |
| 728 |
OpBranchConditional %14 %15 %16 |
| 729 |
%15 = OpLabel |
| 730 |
OpBranch %16 |
| 731 |
%16 = OpLabel |
| 732 |
OpReturn |
| 733 |
OpFunctionEnd |
| 734 |
)"; |
| 735 |
|
| 736 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 737 |
const auto consumer = nullptr; |
| 738 |
const auto context = |
| 739 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 740 |
|
| 741 |
spvtools::ValidatorOptions validator_options; |
| 742 |
TransformationContext transformation_context( |
| 743 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 744 |
// Tests the reference shader validity. |
| 745 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 746 |
kConsoleMessageConsumer)); |
| 747 |
|
| 748 |
transformation_context.GetFactManager()->AddFactBlockIsDead(15); |
| 749 |
auto transformation = TransformationPushIdThroughVariable( |
| 750 |
14, 100, 101, SpvStorageClassFunction, 14, |
| 751 |
MakeInstructionDescriptor(15, SpvOpBranch, 0)); |
| 752 |
ASSERT_TRUE( |
| 753 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 754 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 755 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 756 |
kConsoleMessageConsumer)); |
| 757 |
ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous( |
| 758 |
MakeDataDescriptor(14, {}), MakeDataDescriptor(100, {}))); |
| 759 |
} |
| 760 |
|
| 761 |
} // namespace |
| 762 |
} // namespace fuzz |
| 763 |
} // namespace spvtools |