| 1 |
// Copyright (c) 2020 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_add_relaxed_decoration.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "test/fuzz/fuzz_test_util.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
namespace { |
| 24 |
TEST(TransformationAddRelaxedDecorationTest, BasicScenarios) { |
| 25 |
// This is a simple transformation and this test handles the main cases. |
| 26 |
|
| 27 |
std::string shader = R"( |
| 28 |
OpCapability Shader |
| 29 |
%1 = OpExtInstImport "GLSL.std.450" |
| 30 |
OpMemoryModel Logical GLSL450 |
| 31 |
OpEntryPoint Fragment %4 "main" |
| 32 |
OpExecutionMode %4 OriginUpperLeft |
| 33 |
OpSource ESSL 310 |
| 34 |
OpName %4 "main" |
| 35 |
OpName %8 "a" |
| 36 |
OpName %10 "b" |
| 37 |
OpName %14 "c" |
| 38 |
%2 = OpTypeVoid |
| 39 |
%3 = OpTypeFunction %2 |
| 40 |
%6 = OpTypeInt 32 1 |
| 41 |
%7 = OpTypePointer Function %6 |
| 42 |
%9 = OpConstant %6 4 |
| 43 |
%11 = OpConstant %6 6 |
| 44 |
%12 = OpTypeBool |
| 45 |
%13 = OpTypePointer Function %12 |
| 46 |
%15 = OpConstantTrue %12 |
| 47 |
%4 = OpFunction %2 None %3 |
| 48 |
%5 = OpLabel |
| 49 |
%8 = OpVariable %7 Function |
| 50 |
%10 = OpVariable %7 Function |
| 51 |
%14 = OpVariable %13 Function |
| 52 |
OpStore %8 %9 |
| 53 |
OpStore %10 %11 |
| 54 |
OpStore %14 %15 |
| 55 |
OpSelectionMerge %19 None |
| 56 |
OpBranchConditional %15 %19 %100 |
| 57 |
%100 = OpLabel |
| 58 |
%25 = OpISub %6 %9 %11 |
| 59 |
%28 = OpLogicalNot %12 %15 |
| 60 |
OpBranch %19 |
| 61 |
%19 = OpLabel |
| 62 |
%27 = OpISub %6 %9 %11 |
| 63 |
OpReturn |
| 64 |
OpFunctionEnd |
| 65 |
)"; |
| 66 |
|
| 67 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 68 |
const auto consumer = nullptr; |
| 69 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 70 |
spvtools::ValidatorOptions validator_options; |
| 71 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 72 |
kConsoleMessageConsumer)); |
| 73 |
TransformationContext transformation_context( |
| 74 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 75 |
transformation_context.GetFactManager()->AddFactBlockIsDead(100); |
| 76 |
|
| 77 |
// Invalid: 200 is not an id. |
| 78 |
ASSERT_FALSE(TransformationAddRelaxedDecoration(200).IsApplicable( |
| 79 |
context.get(), transformation_context)); |
| 80 |
// Invalid: 1 is not in a block. |
| 81 |
ASSERT_FALSE(TransformationAddRelaxedDecoration(1).IsApplicable( |
| 82 |
context.get(), transformation_context)); |
| 83 |
// Invalid: 27 is not in a dead block. |
| 84 |
ASSERT_FALSE(TransformationAddRelaxedDecoration(27).IsApplicable( |
| 85 |
context.get(), transformation_context)); |
| 86 |
// Invalid: 28 is in a dead block, but returns bool (not numeric). |
| 87 |
ASSERT_FALSE(TransformationAddRelaxedDecoration(28).IsApplicable( |
| 88 |
context.get(), transformation_context)); |
| 89 |
// It is valid to add RelaxedPrecision to 25 |
| 90 |
for (uint32_t result_id : {25u}) { |
| 91 |
TransformationAddRelaxedDecoration transformation(result_id); |
| 92 |
ASSERT_TRUE( |
| 93 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 94 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 95 |
&transformation_context); |
| 96 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 97 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 98 |
} |
| 99 |
|
| 100 |
std::string after_transformation = R"( |
| 101 |
OpCapability Shader |
| 102 |
%1 = OpExtInstImport "GLSL.std.450" |
| 103 |
OpMemoryModel Logical GLSL450 |
| 104 |
OpEntryPoint Fragment %4 "main" |
| 105 |
OpExecutionMode %4 OriginUpperLeft |
| 106 |
OpSource ESSL 310 |
| 107 |
OpName %4 "main" |
| 108 |
OpName %8 "a" |
| 109 |
OpName %10 "b" |
| 110 |
OpName %14 "c" |
| 111 |
OpDecorate %25 RelaxedPrecision |
| 112 |
%2 = OpTypeVoid |
| 113 |
%3 = OpTypeFunction %2 |
| 114 |
%6 = OpTypeInt 32 1 |
| 115 |
%7 = OpTypePointer Function %6 |
| 116 |
%9 = OpConstant %6 4 |
| 117 |
%11 = OpConstant %6 6 |
| 118 |
%12 = OpTypeBool |
| 119 |
%13 = OpTypePointer Function %12 |
| 120 |
%15 = OpConstantTrue %12 |
| 121 |
%4 = OpFunction %2 None %3 |
| 122 |
%5 = OpLabel |
| 123 |
%8 = OpVariable %7 Function |
| 124 |
%10 = OpVariable %7 Function |
| 125 |
%14 = OpVariable %13 Function |
| 126 |
OpStore %8 %9 |
| 127 |
OpStore %10 %11 |
| 128 |
OpStore %14 %15 |
| 129 |
OpSelectionMerge %19 None |
| 130 |
OpBranchConditional %15 %19 %100 |
| 131 |
%100 = OpLabel |
| 132 |
%25 = OpISub %6 %9 %11 |
| 133 |
%28 = OpLogicalNot %12 %15 |
| 134 |
OpBranch %19 |
| 135 |
%19 = OpLabel |
| 136 |
%27 = OpISub %6 %9 %11 |
| 137 |
OpReturn |
| 138 |
OpFunctionEnd |
| 139 |
)"; |
| 140 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace |
| 144 |
} // namespace fuzz |
| 145 |
} // namespace spvtools |