| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- // Copyright (c) 2020 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.
- #include "source/fuzz/transformation_composite_insert.h"
- #include "gtest/gtest.h"
- #include "source/fuzz/data_descriptor.h"
- #include "source/fuzz/fuzzer_util.h"
- #include "source/fuzz/instruction_descriptor.h"
- #include "test/fuzz/fuzz_test_util.h"
- namespace spvtools {
- namespace fuzz {
- namespace {
- TEST(TransformationCompositeInsertTest, NotApplicableScenarios) {
- // This test handles cases where IsApplicable() returns false.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- OpName %18 "level_1"
- OpMemberName %18 0 "b1"
- OpMemberName %18 1 "b2"
- OpName %20 "l1"
- OpName %24 "level_2"
- OpMemberName %24 0 "c1"
- OpMemberName %24 1 "c2"
- OpName %26 "l2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %18 = OpTypeStruct %12 %12
- %19 = OpTypePointer Function %18
- %24 = OpTypeStruct %18 %18
- %25 = OpTypePointer Function %24
- %30 = OpTypeBool
- %31 = OpConstantTrue %30
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %20 = OpVariable %19 Function
- %26 = OpVariable %25 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %21 = OpLoad %12 %14
- %22 = OpLoad %12 %14
- %23 = OpCompositeConstruct %18 %21 %22
- OpStore %20 %23
- %27 = OpLoad %18 %20
- %28 = OpLoad %18 %20
- %29 = OpCompositeConstruct %24 %27 %28
- OpStore %26 %29
- OpSelectionMerge %33 None
- OpBranchConditional %31 %32 %33
- %32 = OpLabel
- OpBranch %33
- %33 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Bad: |fresh_id| is not fresh.
- auto transformation_bad_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 20, 29, 11,
- {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_1.IsApplicable(context.get(), transformation_context));
- // Bad: |composite_id| does not refer to a existing instruction.
- auto transformation_bad_2 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 40, 11,
- {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_2.IsApplicable(context.get(), transformation_context));
- // Bad: |composite_id| does not refer to a composite value.
- auto transformation_bad_3 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 9, 11, {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_3.IsApplicable(context.get(), transformation_context));
- // Bad: |object_id| does not refer to a defined instruction.
- auto transformation_bad_4 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 29, 40,
- {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_4.IsApplicable(context.get(), transformation_context));
- // Bad: |object_id| cannot refer to a pointer.
- auto transformation_bad_5 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 29, 8, {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_5.IsApplicable(context.get(), transformation_context));
- // Bad: |index| is not a correct index.
- auto transformation_bad_6 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 29, 11,
- {2, 0, 0});
- ASSERT_FALSE(
- transformation_bad_6.IsApplicable(context.get(), transformation_context));
- // Bad: Type id of the object to be inserted and the type id of the
- // component at |index| are not the same.
- auto transformation_bad_7 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 29, 11, {1, 0});
- ASSERT_FALSE(
- transformation_bad_7.IsApplicable(context.get(), transformation_context));
- // Bad: |instruction_to_insert_before| does not refer to a defined
- // instruction.
- auto transformation_bad_8 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpIMul, 0), 50, 29, 11, {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_8.IsApplicable(context.get(), transformation_context));
- // Bad: OpCompositeInsert cannot be inserted before OpBranchConditional with
- // OpSelectionMerge above it.
- auto transformation_bad_9 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpBranchConditional, 0), 50, 29,
- 11, {1, 0, 0});
- ASSERT_FALSE(
- transformation_bad_9.IsApplicable(context.get(), transformation_context));
- // Bad: |composite_id| does not have a type_id.
- auto transformation_bad_10 = TransformationCompositeInsert(
- MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 50, 1, 11, {1, 0, 0});
- ASSERT_FALSE(transformation_bad_10.IsApplicable(context.get(),
- transformation_context));
- }
- TEST(TransformationCompositeInsertTest, EmptyCompositeScenarios) {
- // This test handles cases where either the composite is empty or the
- // composite contains an empty composite.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- %2 = OpTypeVoid
- %60 = OpTypeStruct
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %61 = OpConstantComposite %60
- %62 = OpConstantComposite %60
- %12 = OpTypeStruct %6 %6
- %63 = OpTypeStruct %6 %60
- %13 = OpTypePointer Function %12
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- %64 = OpCompositeConstruct %63 %15 %61
- OpStore %14 %17
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Bad: The composite with |composite_id| cannot be empty.
- auto transformation_bad_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(64, spv::Op::OpStore, 0), 50, 61, 62, {1});
- ASSERT_FALSE(
- transformation_bad_1.IsApplicable(context.get(), transformation_context));
- // Good: It is possible to insert into a composite an element which is an
- // empty composite.
- auto transformation_good_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(64, spv::Op::OpStore, 0), 50, 64, 62, {1});
- ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
- transformation_context));
- ApplyAndCheckFreshIds(transformation_good_1, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformations = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- %2 = OpTypeVoid
- %60 = OpTypeStruct
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %61 = OpConstantComposite %60
- %62 = OpConstantComposite %60
- %12 = OpTypeStruct %6 %6
- %63 = OpTypeStruct %6 %60
- %13 = OpTypePointer Function %12
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- %64 = OpCompositeConstruct %63 %15 %61
- %50 = OpCompositeInsert %63 %62 %64 1
- OpStore %14 %17
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
- }
- TEST(TransformationCompositeInsertTest, IrrelevantCompositeNoSynonyms) {
- // This test handles cases where either |composite| is irrelevant.
- // The transformation shouldn't create any synonyms.
- // The member composite has a different number of elements than the parent
- // composite.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- OpName %18 "level_1"
- OpMemberName %18 0 "b1"
- OpMemberName %18 1 "b2"
- OpMemberName %18 2 "b3"
- OpName %20 "l1"
- OpName %25 "level_2"
- OpMemberName %25 0 "c1"
- OpMemberName %25 1 "c2"
- OpName %27 "l2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %18 = OpTypeStruct %12 %12 %12
- %19 = OpTypePointer Function %18
- %25 = OpTypeStruct %18 %18
- %26 = OpTypePointer Function %25
- %31 = OpTypeBool
- %32 = OpConstantTrue %31
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %20 = OpVariable %19 Function
- %27 = OpVariable %26 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %21 = OpLoad %12 %14
- %22 = OpLoad %12 %14
- %23 = OpLoad %12 %14
- %24 = OpCompositeConstruct %18 %21 %22 %23
- OpStore %20 %24
- %28 = OpLoad %18 %20
- %29 = OpLoad %18 %20
- %30 = OpCompositeConstruct %25 %28 %29
- OpStore %27 %30
- OpSelectionMerge %34 None
- OpBranchConditional %32 %33 %34
- %33 = OpLabel
- OpBranch %34
- %34 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Add fact that the composite is irrelevant.
- transformation_context.GetFactManager()->AddFactIdIsIrrelevant(30);
- auto transformation_good_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(30, spv::Op::OpStore, 0), 50, 30, 11,
- {1, 0, 0});
- ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
- transformation_context));
- ApplyAndCheckFreshIds(transformation_good_1, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // No synonyms that involve the original object - %30 - should have been
- // added.
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {0}), MakeDataDescriptor(50, {0})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 1}), MakeDataDescriptor(50, {1, 1})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 2}), MakeDataDescriptor(50, {1, 2})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 0, 1}), MakeDataDescriptor(50, {1, 0, 1})));
- // We *should* have a synonym between %11 and the component of %50 into which
- // it has been inserted.
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {1, 0, 0}), MakeDataDescriptor(11, {})));
- }
- TEST(TransformationCompositeInsertTest, IrrelevantObjectNoSynonyms) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- OpName %18 "level_1"
- OpMemberName %18 0 "b1"
- OpMemberName %18 1 "b2"
- OpMemberName %18 2 "b3"
- OpName %20 "l1"
- OpName %25 "level_2"
- OpMemberName %25 0 "c1"
- OpMemberName %25 1 "c2"
- OpName %27 "l2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %18 = OpTypeStruct %12 %12 %12
- %19 = OpTypePointer Function %18
- %25 = OpTypeStruct %18 %18
- %26 = OpTypePointer Function %25
- %31 = OpTypeBool
- %32 = OpConstantTrue %31
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %20 = OpVariable %19 Function
- %27 = OpVariable %26 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %21 = OpLoad %12 %14
- %22 = OpLoad %12 %14
- %23 = OpLoad %12 %14
- %24 = OpCompositeConstruct %18 %21 %22 %23
- OpStore %20 %24
- %28 = OpLoad %18 %20
- %29 = OpLoad %18 %20
- %30 = OpCompositeConstruct %25 %28 %29
- OpStore %27 %30
- OpSelectionMerge %34 None
- OpBranchConditional %32 %33 %34
- %33 = OpLabel
- OpBranch %34
- %34 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Add fact that the object is irrelevant.
- transformation_context.GetFactManager()->AddFactIdIsIrrelevant(11);
- auto transformation_good_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(30, spv::Op::OpStore, 0), 50, 30, 11,
- {1, 0, 0});
- ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
- transformation_context));
- ApplyAndCheckFreshIds(transformation_good_1, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Since %30 and %50 are not irrelevant, they should be synonymous at all
- // indices unaffected by the insertion.
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {0}), MakeDataDescriptor(50, {0})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 1}), MakeDataDescriptor(50, {1, 1})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 2}), MakeDataDescriptor(50, {1, 2})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 0, 1}), MakeDataDescriptor(50, {1, 0, 1})));
- // Since %11 is irrelevant it should not be synonymous with the component into
- // which it has been inserted.
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {1, 0, 0}), MakeDataDescriptor(11, {})));
- }
- TEST(TransformationCompositeInsertTest, ApplicableCreatedSynonyms) {
- // This test handles cases where neither |composite| nor |object| is
- // irrelevant. The transformation should create synonyms.
- // The member composite has a different number of elements than the parent
- // composite.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- OpName %18 "level_1"
- OpMemberName %18 0 "b1"
- OpMemberName %18 1 "b2"
- OpMemberName %18 2 "b3"
- OpName %20 "l1"
- OpName %25 "level_2"
- OpMemberName %25 0 "c1"
- OpMemberName %25 1 "c2"
- OpName %27 "l2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %18 = OpTypeStruct %12 %12 %12
- %19 = OpTypePointer Function %18
- %25 = OpTypeStruct %18 %18
- %26 = OpTypePointer Function %25
- %31 = OpTypeBool
- %32 = OpConstantTrue %31
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %20 = OpVariable %19 Function
- %27 = OpVariable %26 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %21 = OpLoad %12 %14
- %22 = OpLoad %12 %14
- %23 = OpLoad %12 %14
- %24 = OpCompositeConstruct %18 %21 %22 %23
- OpStore %20 %24
- %28 = OpLoad %18 %20
- %29 = OpLoad %18 %20
- %30 = OpCompositeConstruct %25 %28 %29
- OpStore %27 %30
- OpSelectionMerge %34 None
- OpBranchConditional %32 %33 %34
- %33 = OpLabel
- OpBranch %34
- %34 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- auto transformation_good_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(30, spv::Op::OpStore, 0), 50, 30, 11,
- {1, 0, 0});
- ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
- transformation_context));
- ApplyAndCheckFreshIds(transformation_good_1, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // These synonyms should have been added.
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {0}), MakeDataDescriptor(50, {0})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 1}), MakeDataDescriptor(50, {1, 1})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 2}), MakeDataDescriptor(50, {1, 2})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 0, 1}), MakeDataDescriptor(50, {1, 0, 1})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {1, 0, 0}), MakeDataDescriptor(11, {})));
- // These synonyms should not have been added.
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1}), MakeDataDescriptor(50, {1})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 0}), MakeDataDescriptor(50, {1, 0})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(30, {1, 0, 0}), MakeDataDescriptor(50, {1, 0, 0})));
- auto transformation_good_2 = TransformationCompositeInsert(
- MakeInstructionDescriptor(50, spv::Op::OpStore, 0), 51, 50, 11,
- {0, 1, 1});
- ASSERT_TRUE(transformation_good_2.IsApplicable(context.get(),
- transformation_context));
- ApplyAndCheckFreshIds(transformation_good_2, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // These synonyms should have been added.
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {1}), MakeDataDescriptor(51, {1})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0, 0}), MakeDataDescriptor(51, {0, 0})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0, 2}), MakeDataDescriptor(51, {0, 2})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0, 1, 0}), MakeDataDescriptor(51, {0, 1, 0})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(51, {0, 1, 1}), MakeDataDescriptor(11, {})));
- // These synonyms should not have been added.
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0}), MakeDataDescriptor(51, {0})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0, 1}), MakeDataDescriptor(51, {0, 1})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(50, {0, 1, 1}), MakeDataDescriptor(51, {0, 1, 1})));
- std::string after_transformations = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b"
- OpName %18 "level_1"
- OpMemberName %18 0 "b1"
- OpMemberName %18 1 "b2"
- OpMemberName %18 2 "b3"
- OpName %20 "l1"
- OpName %25 "level_2"
- OpMemberName %25 0 "c1"
- OpMemberName %25 1 "c2"
- OpName %27 "l2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %18 = OpTypeStruct %12 %12 %12
- %19 = OpTypePointer Function %18
- %25 = OpTypeStruct %18 %18
- %26 = OpTypePointer Function %25
- %31 = OpTypeBool
- %32 = OpConstantTrue %31
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %20 = OpVariable %19 Function
- %27 = OpVariable %26 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %21 = OpLoad %12 %14
- %22 = OpLoad %12 %14
- %23 = OpLoad %12 %14
- %24 = OpCompositeConstruct %18 %21 %22 %23
- OpStore %20 %24
- %28 = OpLoad %18 %20
- %29 = OpLoad %18 %20
- %30 = OpCompositeConstruct %25 %28 %29
- %50 = OpCompositeInsert %25 %11 %30 1 0 0
- %51 = OpCompositeInsert %25 %11 %50 0 1 1
- OpStore %27 %30
- OpSelectionMerge %34 None
- OpBranchConditional %32 %33 %34
- %33 = OpLabel
- OpBranch %34
- %34 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
- }
- TEST(TransformationCompositeInsertTest, IdNotAvailableScenarios) {
- // This test handles cases where either the composite or the object is not
- // available before the |instruction_to_insert_before|.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %8 "i1"
- OpName %10 "i2"
- OpName %12 "base"
- OpMemberName %12 0 "a1"
- OpMemberName %12 1 "a2"
- OpName %14 "b1"
- OpName %18 "b2"
- OpName %22 "lvl1"
- OpMemberName %22 0 "b1"
- OpMemberName %22 1 "b2"
- OpName %24 "l1"
- OpName %28 "i3"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 1
- %11 = OpConstant %6 2
- %12 = OpTypeStruct %6 %6
- %13 = OpTypePointer Function %12
- %22 = OpTypeStruct %12 %12
- %23 = OpTypePointer Function %22
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %14 = OpVariable %13 Function
- %18 = OpVariable %13 Function
- %24 = OpVariable %23 Function
- %28 = OpVariable %7 Function
- OpStore %8 %9
- OpStore %10 %11
- %15 = OpLoad %6 %8
- %16 = OpLoad %6 %10
- %17 = OpCompositeConstruct %12 %15 %16
- OpStore %14 %17
- %19 = OpLoad %6 %10
- %20 = OpLoad %6 %8
- %21 = OpCompositeConstruct %12 %19 %20
- OpStore %18 %21
- %25 = OpLoad %12 %14
- %26 = OpLoad %12 %18
- %27 = OpCompositeConstruct %22 %25 %26
- OpStore %24 %27
- %29 = OpLoad %6 %8
- %30 = OpLoad %6 %10
- %31 = OpIMul %6 %29 %30
- OpStore %28 %31
- %60 = OpCompositeConstruct %12 %20 %19
- %61 = OpCompositeConstruct %22 %26 %25
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_4;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Bad: The object with |object_id| is not available at
- // |instruction_to_insert_before|.
- auto transformation_bad_1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(31, spv::Op::OpIMul, 0), 50, 27, 60, {1});
- ASSERT_FALSE(
- transformation_bad_1.IsApplicable(context.get(), transformation_context));
- // Bad: The composite with |composite_id| is not available at
- // |instruction_to_insert_before|.
- auto transformation_bad_2 = TransformationCompositeInsert(
- MakeInstructionDescriptor(31, spv::Op::OpIMul, 0), 50, 61, 21, {1});
- ASSERT_FALSE(
- transformation_bad_2.IsApplicable(context.get(), transformation_context));
- // Bad: The |instruction_to_insert_before| is the composite itself and is
- // available.
- auto transformation_bad_3 = TransformationCompositeInsert(
- MakeInstructionDescriptor(61, spv::Op::OpCompositeConstruct, 0), 50, 61,
- 21, {1});
- ASSERT_FALSE(
- transformation_bad_3.IsApplicable(context.get(), transformation_context));
- // Bad: The |instruction_to_insert_before| is the object itself and is not
- // available.
- auto transformation_bad_4 = TransformationCompositeInsert(
- MakeInstructionDescriptor(60, spv::Op::OpCompositeConstruct, 0), 50, 27,
- 60, {1});
- ASSERT_FALSE(
- transformation_bad_4.IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationCompositeInsertTest, CompositeInsertionWithIrrelevantIds) {
- // This checks that we do *not* get data synonym facts when we do composite
- // insertion using irrelevant ids or in dead blocks.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %12 "main"
- OpExecutionMode %12 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeVector %6 2
- %8 = OpConstant %6 0
- %9 = OpConstantComposite %7 %8 %8
- %10 = OpTypeBool
- %11 = OpConstantFalse %10
- %16 = OpConstant %6 0
- %17 = OpConstant %6 1
- %18 = OpConstantComposite %7 %8 %8
- %12 = OpFunction %2 None %3
- %13 = OpLabel
- OpSelectionMerge %15 None
- OpBranchConditional %11 %14 %15
- %14 = OpLabel
- OpBranch %15
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- transformation_context.GetFactManager()->AddFactBlockIsDead(14);
- transformation_context.GetFactManager()->AddFactIdIsIrrelevant(16);
- transformation_context.GetFactManager()->AddFactIdIsIrrelevant(18);
- // Leads to synonyms - nothing is irrelevant.
- auto transformation1 = TransformationCompositeInsert(
- MakeInstructionDescriptor(13, spv::Op::OpSelectionMerge, 0), 100, 9, 17,
- {0});
- ASSERT_TRUE(
- transformation1.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation1, context.get(),
- &transformation_context);
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(100, {0}), MakeDataDescriptor(17, {})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(100, {1}), MakeDataDescriptor(9, {1})));
- // Because %16 is irrelevant, we don't get a synonym with the component to
- // which it has been inserted (but we do for the other component).
- auto transformation2 = TransformationCompositeInsert(
- MakeInstructionDescriptor(13, spv::Op::OpSelectionMerge, 0), 101, 9, 16,
- {0});
- ASSERT_TRUE(
- transformation2.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation2, context.get(),
- &transformation_context);
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(101, {0}), MakeDataDescriptor(16, {})));
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(101, {1}), MakeDataDescriptor(9, {1})));
- // Because %18 is irrelevant we only get a synonym for the component into
- // which insertion has taken place.
- auto transformation3 = TransformationCompositeInsert(
- MakeInstructionDescriptor(13, spv::Op::OpSelectionMerge, 0), 102, 18, 17,
- {0});
- ASSERT_TRUE(
- transformation3.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation3, context.get(),
- &transformation_context);
- ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(102, {0}), MakeDataDescriptor(17, {})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(102, {1}), MakeDataDescriptor(18, {1})));
- // Does not lead to synonyms as block %14 is dead.
- auto transformation4 = TransformationCompositeInsert(
- MakeInstructionDescriptor(14, spv::Op::OpBranch, 0), 103, 9, 17, {0});
- ASSERT_TRUE(
- transformation4.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation4, context.get(),
- &transformation_context);
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(103, {0}), MakeDataDescriptor(17, {})));
- ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
- MakeDataDescriptor(103, {1}), MakeDataDescriptor(9, {1})));
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- }
- } // namespace
- } // namespace fuzz
- } // namespace spvtools
|