| 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/transformation_store.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(TransformationStoreTest, BasicTest) { |
| 27 |
std::string shader = R"( |
| 28 |
OpCapability Shader |
| 29 |
OpCapability VariablePointers |
| 30 |
%1 = OpExtInstImport "GLSL.std.450" |
| 31 |
OpMemoryModel Logical GLSL450 |
| 32 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 33 |
OpExecutionMode %4 OriginUpperLeft |
| 34 |
OpSource ESSL 310 |
| 35 |
OpDecorate %92 BuiltIn FragCoord |
| 36 |
%2 = OpTypeVoid |
| 37 |
%3 = OpTypeFunction %2 |
| 38 |
%6 = OpTypeInt 32 1 |
| 39 |
%7 = OpTypeFloat 32 |
| 40 |
%8 = OpTypeStruct %6 %7 |
| 41 |
%9 = OpTypePointer Function %8 |
| 42 |
%10 = OpTypeFunction %6 %9 |
| 43 |
%14 = OpConstant %6 0 |
| 44 |
%15 = OpTypePointer Function %6 |
| 45 |
%51 = OpTypePointer Private %6 |
| 46 |
%21 = OpConstant %6 2 |
| 47 |
%23 = OpConstant %6 1 |
| 48 |
%24 = OpConstant %7 1 |
| 49 |
%25 = OpTypePointer Function %7 |
| 50 |
%50 = OpTypePointer Private %7 |
| 51 |
%34 = OpTypeBool |
| 52 |
%35 = OpConstantFalse %34 |
| 53 |
%60 = OpConstantNull %50 |
| 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 ; irrelevant |
| 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 ; irrelevant |
| 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 ; irrelevant |
| 80 |
OpBranch %37 |
| 81 |
%37 = OpLabel |
| 82 |
OpReturn |
| 83 |
OpFunctionEnd |
| 84 |
%12 = OpFunction %6 None %10 |
| 85 |
%11 = OpFunctionParameter %9 ; irrelevant |
| 86 |
%13 = OpLabel |
| 87 |
%46 = OpCopyObject %9 %11 ; irrelevant |
| 88 |
%16 = OpAccessChain %15 %11 %14 ; irrelevant |
| 89 |
%95 = OpCopyObject %8 %80 |
| 90 |
OpReturnValue %21 |
| 91 |
OpFunctionEnd |
| 92 |
)"; |
| 93 |
|
| 94 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 95 |
const auto consumer = nullptr; |
| 96 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 97 |
spvtools::ValidatorOptions validator_options; |
| 98 |
validator_options.SetRelaxLogicalPointer(true); |
| 99 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 100 |
kConsoleMessageConsumer)); |
| 101 |
TransformationContext transformation_context( |
| 102 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 103 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 104 |
27); |
| 105 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 106 |
11); |
| 107 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 108 |
46); |
| 109 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 110 |
16); |
| 111 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 112 |
52); |
| 113 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 114 |
81); |
| 115 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 116 |
82); |
| 117 |
|
| 118 |
transformation_context.GetFactManager()->AddFactBlockIsDead(36); |
| 119 |
|
| 120 |
// Variables with pointee types: |
| 121 |
// 52 - ptr_to(7) |
| 122 |
// 53 - ptr_to(6) |
| 123 |
// 20 - ptr_to(8) |
| 124 |
// 27 - ptr_to(8) - irrelevant |
| 125 |
// 92 - ptr_to(90) - read only |
| 126 |
|
| 127 |
// Access chains with pointee type: |
| 128 |
// 22 - ptr_to(6) |
| 129 |
// 26 - ptr_to(6) |
| 130 |
// 30 - ptr_to(6) |
| 131 |
// 33 - ptr_to(6) |
| 132 |
// 38 - ptr_to(6) |
| 133 |
// 40 - ptr_to(6) |
| 134 |
// 43 - ptr_to(6) |
| 135 |
// 16 - ptr_to(6) - irrelevant |
| 136 |
|
| 137 |
// Copied object with pointee type: |
| 138 |
// 44 - ptr_to(8) |
| 139 |
// 45 - ptr_to(6) |
| 140 |
// 46 - ptr_to(8) - irrelevant |
| 141 |
// 81 - ptr_to(8) - irrelevant |
| 142 |
// 82 - ptr_to(8) - irrelevant |
| 143 |
|
| 144 |
// Function parameters with pointee type: |
| 145 |
// 11 - ptr_to(8) - irrelevant |
| 146 |
|
| 147 |
// Pointers that cannot be used: |
| 148 |
// 60 - null |
| 149 |
// 61 - undefined |
| 150 |
|
| 151 |
// Bad: attempt to store to 11 from outside its function |
| 152 |
ASSERT_FALSE(TransformationStore( |
| 153 |
11, false, 0, 0, 80, |
| 154 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 155 |
.IsApplicable(context.get(), transformation_context)); |
| 156 |
|
| 157 |
// Bad: pointer is not available |
| 158 |
ASSERT_FALSE(TransformationStore( |
| 159 |
81, false, 0, 0, 80, |
| 160 |
MakeInstructionDescriptor(45, spv::Op::OpCopyObject, 0)) |
| 161 |
.IsApplicable(context.get(), transformation_context)); |
| 162 |
|
| 163 |
// Bad: attempt to insert before OpVariable |
| 164 |
ASSERT_FALSE( |
| 165 |
TransformationStore(52, false, 0, 0, 24, |
| 166 |
MakeInstructionDescriptor(27, spv::Op::OpVariable, 0)) |
| 167 |
.IsApplicable(context.get(), transformation_context)); |
| 168 |
|
| 169 |
// Bad: pointer id does not exist |
| 170 |
ASSERT_FALSE(TransformationStore( |
| 171 |
1000, false, 0, 0, 24, |
| 172 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 173 |
.IsApplicable(context.get(), transformation_context)); |
| 174 |
|
| 175 |
// Bad: pointer id exists but does not have a type |
| 176 |
ASSERT_FALSE(TransformationStore( |
| 177 |
5, false, 0, 0, 24, |
| 178 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 179 |
.IsApplicable(context.get(), transformation_context)); |
| 180 |
|
| 181 |
// Bad: pointer id exists and has a type, but is not a pointer |
| 182 |
ASSERT_FALSE(TransformationStore( |
| 183 |
24, false, 0, 0, 24, |
| 184 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 185 |
.IsApplicable(context.get(), transformation_context)); |
| 186 |
|
| 187 |
// Bad: attempt to store to a null pointer |
| 188 |
ASSERT_FALSE(TransformationStore( |
| 189 |
60, false, 0, 0, 24, |
| 190 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 191 |
.IsApplicable(context.get(), transformation_context)); |
| 192 |
|
| 193 |
// Bad: attempt to store to an undefined pointer |
| 194 |
ASSERT_FALSE(TransformationStore( |
| 195 |
61, false, 0, 0, 21, |
| 196 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 197 |
.IsApplicable(context.get(), transformation_context)); |
| 198 |
|
| 199 |
// Bad: %82 is not available at the program point |
| 200 |
ASSERT_FALSE( |
| 201 |
TransformationStore(82, false, 0, 0, 80, |
| 202 |
MakeInstructionDescriptor(37, spv::Op::OpReturn, 0)) |
| 203 |
.IsApplicable(context.get(), transformation_context)); |
| 204 |
|
| 205 |
// Bad: value id does not exist |
| 206 |
ASSERT_FALSE(TransformationStore( |
| 207 |
27, false, 0, 0, 1000, |
| 208 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 209 |
.IsApplicable(context.get(), transformation_context)); |
| 210 |
|
| 211 |
// Bad: value id exists but does not have a type |
| 212 |
ASSERT_FALSE(TransformationStore( |
| 213 |
27, false, 0, 0, 15, |
| 214 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 215 |
.IsApplicable(context.get(), transformation_context)); |
| 216 |
|
| 217 |
// Bad: value id exists but has the wrong type |
| 218 |
ASSERT_FALSE(TransformationStore( |
| 219 |
27, false, 0, 0, 14, |
| 220 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)) |
| 221 |
.IsApplicable(context.get(), transformation_context)); |
| 222 |
|
| 223 |
// Bad: attempt to store to read-only variable |
| 224 |
ASSERT_FALSE(TransformationStore( |
| 225 |
92, false, 0, 0, 93, |
| 226 |
MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0)) |
| 227 |
.IsApplicable(context.get(), transformation_context)); |
| 228 |
|
| 229 |
// Bad: value is not available |
| 230 |
ASSERT_FALSE(TransformationStore( |
| 231 |
27, false, 0, 0, 95, |
| 232 |
MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0)) |
| 233 |
.IsApplicable(context.get(), transformation_context)); |
| 234 |
|
| 235 |
// Bad: variable being stored to does not have an irrelevant pointee value, |
| 236 |
// and the store is not in a dead block. |
| 237 |
ASSERT_FALSE(TransformationStore( |
| 238 |
20, false, 0, 0, 95, |
| 239 |
MakeInstructionDescriptor(45, spv::Op::OpCopyObject, 0)) |
| 240 |
.IsApplicable(context.get(), transformation_context)); |
| 241 |
|
| 242 |
// The described instruction does not exist. |
| 243 |
ASSERT_FALSE(TransformationStore( |
| 244 |
27, false, 0, 0, 80, |
| 245 |
MakeInstructionDescriptor(1000, spv::Op::OpAccessChain, 0)) |
| 246 |
.IsApplicable(context.get(), transformation_context)); |
| 247 |
|
| 248 |
{ |
| 249 |
// Store to irrelevant variable from dead block. |
| 250 |
TransformationStore transformation( |
| 251 |
27, false, 0, 0, 80, |
| 252 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)); |
| 253 |
ASSERT_TRUE( |
| 254 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 255 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 256 |
&transformation_context); |
| 257 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 258 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 259 |
} |
| 260 |
|
| 261 |
{ |
| 262 |
// Store to irrelevant variable from live block. |
| 263 |
TransformationStore transformation( |
| 264 |
11, false, 0, 0, 95, |
| 265 |
MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0)); |
| 266 |
ASSERT_TRUE( |
| 267 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 268 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 269 |
&transformation_context); |
| 270 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 271 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 272 |
} |
| 273 |
|
| 274 |
{ |
| 275 |
// Store to irrelevant variable from live block. |
| 276 |
TransformationStore transformation( |
| 277 |
46, false, 0, 0, 80, |
| 278 |
MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0)); |
| 279 |
ASSERT_TRUE( |
| 280 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 281 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 282 |
&transformation_context); |
| 283 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 284 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 285 |
} |
| 286 |
|
| 287 |
{ |
| 288 |
// Store to irrelevant variable from live block. |
| 289 |
TransformationStore transformation( |
| 290 |
16, false, 0, 0, 21, |
| 291 |
MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0)); |
| 292 |
ASSERT_TRUE( |
| 293 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 294 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 295 |
&transformation_context); |
| 296 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 297 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 298 |
} |
| 299 |
|
| 300 |
{ |
| 301 |
// Store to non-irrelevant variable from dead block. |
| 302 |
TransformationStore transformation( |
| 303 |
53, false, 0, 0, 21, |
| 304 |
MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0)); |
| 305 |
ASSERT_TRUE( |
| 306 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 307 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 308 |
&transformation_context); |
| 309 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 310 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 311 |
} |
| 312 |
|
| 313 |
std::string after_transformation = R"( |
| 314 |
OpCapability Shader |
| 315 |
OpCapability VariablePointers |
| 316 |
%1 = OpExtInstImport "GLSL.std.450" |
| 317 |
OpMemoryModel Logical GLSL450 |
| 318 |
OpEntryPoint Fragment %4 "main" %92 %52 %53 |
| 319 |
OpExecutionMode %4 OriginUpperLeft |
| 320 |
OpSource ESSL 310 |
| 321 |
OpDecorate %92 BuiltIn FragCoord |
| 322 |
%2 = OpTypeVoid |
| 323 |
%3 = OpTypeFunction %2 |
| 324 |
%6 = OpTypeInt 32 1 |
| 325 |
%7 = OpTypeFloat 32 |
| 326 |
%8 = OpTypeStruct %6 %7 |
| 327 |
%9 = OpTypePointer Function %8 |
| 328 |
%10 = OpTypeFunction %6 %9 |
| 329 |
%14 = OpConstant %6 0 |
| 330 |
%15 = OpTypePointer Function %6 |
| 331 |
%51 = OpTypePointer Private %6 |
| 332 |
%21 = OpConstant %6 2 |
| 333 |
%23 = OpConstant %6 1 |
| 334 |
%24 = OpConstant %7 1 |
| 335 |
%25 = OpTypePointer Function %7 |
| 336 |
%50 = OpTypePointer Private %7 |
| 337 |
%34 = OpTypeBool |
| 338 |
%35 = OpConstantFalse %34 |
| 339 |
%60 = OpConstantNull %50 |
| 340 |
%52 = OpVariable %50 Private |
| 341 |
%53 = OpVariable %51 Private |
| 342 |
%80 = OpConstantComposite %8 %21 %24 |
| 343 |
%90 = OpTypeVector %7 4 |
| 344 |
%91 = OpTypePointer Input %90 |
| 345 |
%92 = OpVariable %91 Input |
| 346 |
%93 = OpConstantComposite %90 %24 %24 %24 %24 |
| 347 |
%4 = OpFunction %2 None %3 |
| 348 |
%5 = OpLabel |
| 349 |
%20 = OpVariable %9 Function |
| 350 |
%27 = OpVariable %9 Function ; irrelevant |
| 351 |
%22 = OpAccessChain %15 %20 %14 |
| 352 |
%44 = OpCopyObject %9 %20 |
| 353 |
%26 = OpAccessChain %25 %20 %23 |
| 354 |
%29 = OpFunctionCall %6 %12 %27 |
| 355 |
%30 = OpAccessChain %15 %20 %14 |
| 356 |
%45 = OpCopyObject %15 %30 |
| 357 |
%81 = OpCopyObject %9 %27 ; irrelevant |
| 358 |
%33 = OpAccessChain %15 %20 %14 |
| 359 |
OpSelectionMerge %37 None |
| 360 |
OpBranchConditional %35 %36 %37 |
| 361 |
%36 = OpLabel |
| 362 |
OpStore %27 %80 |
| 363 |
OpStore %53 %21 |
| 364 |
%38 = OpAccessChain %15 %20 %14 |
| 365 |
%40 = OpAccessChain %15 %20 %14 |
| 366 |
%43 = OpAccessChain %15 %20 %14 |
| 367 |
%82 = OpCopyObject %9 %27 ; irrelevant |
| 368 |
OpBranch %37 |
| 369 |
%37 = OpLabel |
| 370 |
OpReturn |
| 371 |
OpFunctionEnd |
| 372 |
%12 = OpFunction %6 None %10 |
| 373 |
%11 = OpFunctionParameter %9 ; irrelevant |
| 374 |
%13 = OpLabel |
| 375 |
%46 = OpCopyObject %9 %11 ; irrelevant |
| 376 |
%16 = OpAccessChain %15 %11 %14 ; irrelevant |
| 377 |
%95 = OpCopyObject %8 %80 |
| 378 |
OpStore %11 %95 |
| 379 |
OpStore %46 %80 |
| 380 |
OpStore %16 %21 |
| 381 |
OpReturnValue %21 |
| 382 |
OpFunctionEnd |
| 383 |
)"; |
| 384 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 385 |
} |
| 386 |
|
| 387 |
TEST(TransformationStoreTest, DoNotAllowStoresToReadOnlyMemory) { |
| 388 |
std::string shader = R"( |
| 389 |
OpCapability Shader |
| 390 |
%1 = OpExtInstImport "GLSL.std.450" |
| 391 |
OpMemoryModel Logical GLSL450 |
| 392 |
OpEntryPoint Fragment %4 "main" |
| 393 |
OpExecutionMode %4 OriginUpperLeft |
| 394 |
OpSource ESSL 320 |
| 395 |
OpMemberDecorate %10 0 Offset 0 |
| 396 |
OpMemberDecorate %10 1 Offset 4 |
| 397 |
OpDecorate %10 Block |
| 398 |
OpMemberDecorate %23 0 Offset 0 |
| 399 |
OpDecorate %23 Block |
| 400 |
OpDecorate %25 DescriptorSet 0 |
| 401 |
OpDecorate %25 Binding 0 |
| 402 |
%2 = OpTypeVoid |
| 403 |
%3 = OpTypeFunction %2 |
| 404 |
%6 = OpTypeInt 32 1 |
| 405 |
%7 = OpTypePointer Function %6 |
| 406 |
%9 = OpTypeFloat 32 |
| 407 |
%10 = OpTypeStruct %6 %9 |
| 408 |
%11 = OpTypePointer PushConstant %10 |
| 409 |
%12 = OpVariable %11 PushConstant |
| 410 |
%13 = OpConstant %6 0 |
| 411 |
%14 = OpTypePointer PushConstant %6 |
| 412 |
%17 = OpConstant %6 1 |
| 413 |
%18 = OpTypePointer PushConstant %9 |
| 414 |
%23 = OpTypeStruct %9 |
| 415 |
%24 = OpTypePointer UniformConstant %23 |
| 416 |
%25 = OpVariable %24 UniformConstant |
| 417 |
%26 = OpTypePointer UniformConstant %9 |
| 418 |
%50 = OpConstant %9 0 |
| 419 |
%4 = OpFunction %2 None %3 |
| 420 |
%5 = OpLabel |
| 421 |
%15 = OpAccessChain %14 %12 %13 |
| 422 |
%19 = OpAccessChain %18 %12 %17 |
| 423 |
%27 = OpAccessChain %26 %25 %13 |
| 424 |
OpReturn |
| 425 |
OpFunctionEnd |
| 426 |
)"; |
| 427 |
|
| 428 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 429 |
const auto consumer = nullptr; |
| 430 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 431 |
spvtools::ValidatorOptions validator_options; |
| 432 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 433 |
kConsoleMessageConsumer)); |
| 434 |
TransformationContext transformation_context( |
| 435 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 436 |
transformation_context.GetFactManager()->AddFactBlockIsDead(5); |
| 437 |
|
| 438 |
ASSERT_FALSE( |
| 439 |
TransformationStore(15, false, 0, 0, 13, |
| 440 |
MakeInstructionDescriptor(27, spv::Op::OpReturn, 0)) |
| 441 |
.IsApplicable(context.get(), transformation_context)); |
| 442 |
ASSERT_FALSE( |
| 443 |
TransformationStore(19, false, 0, 0, 50, |
| 444 |
MakeInstructionDescriptor(27, spv::Op::OpReturn, 0)) |
| 445 |
.IsApplicable(context.get(), transformation_context)); |
| 446 |
ASSERT_FALSE( |
| 447 |
TransformationStore(27, false, 0, 0, 50, |
| 448 |
MakeInstructionDescriptor(27, spv::Op::OpReturn, 0)) |
| 449 |
.IsApplicable(context.get(), transformation_context)); |
| 450 |
} |
| 451 |
|
| 452 |
TEST(TransformationStoreTest, SupportAtomicStore) { |
| 453 |
const std::string shader = R"( |
| 454 |
OpCapability Shader |
| 455 |
OpCapability Int8 |
| 456 |
%1 = OpExtInstImport "GLSL.std.450" |
| 457 |
OpMemoryModel Logical GLSL450 |
| 458 |
OpEntryPoint Fragment %4 "main" |
| 459 |
OpExecutionMode %4 OriginUpperLeft |
| 460 |
OpSource ESSL 320 |
| 461 |
%2 = OpTypeVoid |
| 462 |
%3 = OpTypeFunction %2 |
| 463 |
%6 = OpTypeInt 32 1 |
| 464 |
%7 = OpTypeInt 8 1 |
| 465 |
%9 = OpTypeInt 32 0 |
| 466 |
%26 = OpTypeFloat 32 |
| 467 |
%8 = OpTypeStruct %6 |
| 468 |
%10 = OpTypePointer StorageBuffer %8 |
| 469 |
%11 = OpVariable %10 StorageBuffer |
| 470 |
%19 = OpConstant %26 0 |
| 471 |
%18 = OpConstant %9 1 |
| 472 |
%12 = OpConstant %6 0 |
| 473 |
%13 = OpTypePointer StorageBuffer %6 |
| 474 |
%15 = OpConstant %6 4 |
| 475 |
%16 = OpConstant %6 7 |
| 476 |
%17 = OpConstant %7 4 |
| 477 |
%20 = OpConstant %9 64 |
| 478 |
%21 = OpConstant %6 15 |
| 479 |
%4 = OpFunction %2 None %3 |
| 480 |
%5 = OpLabel |
| 481 |
%14 = OpAccessChain %13 %11 %12 |
| 482 |
%24 = OpAccessChain %13 %11 %12 |
| 483 |
OpReturn |
| 484 |
OpFunctionEnd |
| 485 |
)"; |
| 486 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 487 |
const auto consumer = nullptr; |
| 488 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 489 |
spvtools::ValidatorOptions validator_options; |
| 490 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 491 |
kConsoleMessageConsumer)); |
| 492 |
TransformationContext transformation_context( |
| 493 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 494 |
|
| 495 |
transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant( |
| 496 |
14); |
| 497 |
|
| 498 |
// Bad: id 100 of memory scope instruction does not exist. |
| 499 |
ASSERT_FALSE(TransformationStore( |
| 500 |
14, true, 100, 20, 21, |
| 501 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 502 |
.IsApplicable(context.get(), transformation_context)); |
| 503 |
// Bad: id 100 of memory semantics instruction does not exist. |
| 504 |
ASSERT_FALSE(TransformationStore( |
| 505 |
14, true, 15, 100, 21, |
| 506 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 507 |
.IsApplicable(context.get(), transformation_context)); |
| 508 |
// Bad: memory scope should be |OpConstant| opcode. |
| 509 |
ASSERT_FALSE(TransformationStore( |
| 510 |
14, true, 5, 20, 21, |
| 511 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 512 |
.IsApplicable(context.get(), transformation_context)); |
| 513 |
// Bad: memory semantics should be |OpConstant| opcode. |
| 514 |
ASSERT_FALSE(TransformationStore( |
| 515 |
14, true, 15, 5, 21, |
| 516 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 517 |
.IsApplicable(context.get(), transformation_context)); |
| 518 |
|
| 519 |
// Bad: The memory scope instruction must have an Integer operand. |
| 520 |
ASSERT_FALSE(TransformationStore( |
| 521 |
14, true, 15, 19, 21, |
| 522 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 523 |
.IsApplicable(context.get(), transformation_context)); |
| 524 |
// Bad: The memory memory semantics instruction must have an Integer operand. |
| 525 |
ASSERT_FALSE(TransformationStore( |
| 526 |
14, true, 19, 20, 21, |
| 527 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 528 |
.IsApplicable(context.get(), transformation_context)); |
| 529 |
|
| 530 |
// Bad: Integer size of the memory scope must be equal to 32 bits. |
| 531 |
ASSERT_FALSE(TransformationStore( |
| 532 |
14, true, 17, 20, 21, |
| 533 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 534 |
.IsApplicable(context.get(), transformation_context)); |
| 535 |
|
| 536 |
// Bad: Integer size of memory semantics must be equal to 32 bits. |
| 537 |
ASSERT_FALSE(TransformationStore( |
| 538 |
14, true, 15, 17, 21, |
| 539 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 540 |
.IsApplicable(context.get(), transformation_context)); |
| 541 |
|
| 542 |
// Bad: memory scope value must be 4 (spv::Scope::Invocation). |
| 543 |
ASSERT_FALSE(TransformationStore( |
| 544 |
14, true, 16, 20, 21, |
| 545 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 546 |
.IsApplicable(context.get(), transformation_context)); |
| 547 |
|
| 548 |
// Bad: memory semantics value must be either: |
| 549 |
// 64 (SpvMemorySemanticsUniformMemoryMask) |
| 550 |
// 256 (SpvMemorySemanticsWorkgroupMemoryMask) |
| 551 |
ASSERT_FALSE(TransformationStore( |
| 552 |
14, true, 15, 16, 21, |
| 553 |
MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0)) |
| 554 |
.IsApplicable(context.get(), transformation_context)); |
| 555 |
// Bad: The described instruction does not exist |
| 556 |
ASSERT_FALSE(TransformationStore( |
| 557 |
14, true, 15, 20, 21, |
| 558 |
MakeInstructionDescriptor(150, spv::Op::OpAccessChain, 0)) |
| 559 |
.IsApplicable(context.get(), transformation_context)); |
| 560 |
|
| 561 |
// Bad: Can't insert OpAccessChain before the id 15 of memory scope. |
| 562 |
ASSERT_FALSE(TransformationStore( |
| 563 |
14, true, 15, 20, 21, |
| 564 |
MakeInstructionDescriptor(15, spv::Op::OpAccessChain, 0)) |
| 565 |
.IsApplicable(context.get(), transformation_context)); |
| 566 |
|
| 567 |
// Bad: Can't insert OpAccessChain before the id 20 of memory semantics. |
| 568 |
ASSERT_FALSE(TransformationStore( |
| 569 |
14, true, 15, 20, 21, |
| 570 |
MakeInstructionDescriptor(20, spv::Op::OpAccessChain, 0)) |
| 571 |
.IsApplicable(context.get(), transformation_context)); |
| 572 |
|
| 573 |
// Successful transformations. |
| 574 |
{ |
| 575 |
TransformationStore transformation( |
| 576 |
14, true, 15, 20, 21, |
| 577 |
MakeInstructionDescriptor(24, spv::Op::OpReturn, 0)); |
| 578 |
ASSERT_TRUE( |
| 579 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 580 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 581 |
&transformation_context); |
| 582 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 583 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 584 |
} |
| 585 |
|
| 586 |
const std::string after_transformation = R"( |
| 587 |
OpCapability Shader |
| 588 |
OpCapability Int8 |
| 589 |
%1 = OpExtInstImport "GLSL.std.450" |
| 590 |
OpMemoryModel Logical GLSL450 |
| 591 |
OpEntryPoint Fragment %4 "main" |
| 592 |
OpExecutionMode %4 OriginUpperLeft |
| 593 |
OpSource ESSL 320 |
| 594 |
%2 = OpTypeVoid |
| 595 |
%3 = OpTypeFunction %2 |
| 596 |
%6 = OpTypeInt 32 1 |
| 597 |
%7 = OpTypeInt 8 1 |
| 598 |
%9 = OpTypeInt 32 0 |
| 599 |
%26 = OpTypeFloat 32 |
| 600 |
%8 = OpTypeStruct %6 |
| 601 |
%10 = OpTypePointer StorageBuffer %8 |
| 602 |
%11 = OpVariable %10 StorageBuffer |
| 603 |
%19 = OpConstant %26 0 |
| 604 |
%18 = OpConstant %9 1 |
| 605 |
%12 = OpConstant %6 0 |
| 606 |
%13 = OpTypePointer StorageBuffer %6 |
| 607 |
%15 = OpConstant %6 4 |
| 608 |
%16 = OpConstant %6 7 |
| 609 |
%17 = OpConstant %7 4 |
| 610 |
%20 = OpConstant %9 64 |
| 611 |
%21 = OpConstant %6 15 |
| 612 |
%4 = OpFunction %2 None %3 |
| 613 |
%5 = OpLabel |
| 614 |
%14 = OpAccessChain %13 %11 %12 |
| 615 |
%24 = OpAccessChain %13 %11 %12 |
| 616 |
OpAtomicStore %14 %15 %20 %21 |
| 617 |
OpReturn |
| 618 |
OpFunctionEnd |
| 619 |
)"; |
| 620 |
|
| 621 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 622 |
} |
| 623 |
|
| 624 |
} // namespace |
| 625 |
} // namespace fuzz |
| 626 |
} // namespace spvtools |