| 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_early_terminator_wrapper.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 |
|
| 25 |
TEST(TransformationAddEarlyTerminatorWrapperTest, NoVoidType) { |
| 26 |
std::string shader = R"( |
| 27 |
OpCapability Shader |
| 28 |
OpCapability Linkage |
| 29 |
%1 = OpExtInstImport "GLSL.std.450" |
| 30 |
OpMemoryModel Logical GLSL450 |
| 31 |
OpSource ESSL 320 |
| 32 |
)"; |
| 33 |
|
| 34 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 35 |
const auto consumer = nullptr; |
| 36 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 37 |
spvtools::ValidatorOptions validator_options; |
| 38 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 39 |
kConsoleMessageConsumer)); |
| 40 |
TransformationContext transformation_context( |
| 41 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 42 |
|
| 43 |
ASSERT_FALSE( |
| 44 |
TransformationAddEarlyTerminatorWrapper(100, 101, spv::Op::OpKill) |
| 45 |
.IsApplicable(context.get(), transformation_context)); |
| 46 |
} |
| 47 |
|
| 48 |
TEST(TransformationAddEarlyTerminatorWrapperTest, NoVoidFunctionType) { |
| 49 |
std::string shader = R"( |
| 50 |
OpCapability Shader |
| 51 |
OpCapability Linkage |
| 52 |
%1 = OpExtInstImport "GLSL.std.450" |
| 53 |
OpMemoryModel Logical GLSL450 |
| 54 |
OpSource ESSL 320 |
| 55 |
%2 = OpTypeVoid |
| 56 |
)"; |
| 57 |
|
| 58 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 59 |
const auto consumer = nullptr; |
| 60 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 61 |
spvtools::ValidatorOptions validator_options; |
| 62 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 63 |
kConsoleMessageConsumer)); |
| 64 |
TransformationContext transformation_context( |
| 65 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 66 |
|
| 67 |
ASSERT_FALSE( |
| 68 |
TransformationAddEarlyTerminatorWrapper(100, 101, spv::Op::OpKill) |
| 69 |
.IsApplicable(context.get(), transformation_context)); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(TransformationAddEarlyTerminatorWrapperTest, BasicTest) { |
| 73 |
std::string shader = R"( |
| 74 |
OpCapability Shader |
| 75 |
OpExtension "SPV_KHR_terminate_invocation" |
| 76 |
%1 = OpExtInstImport "GLSL.std.450" |
| 77 |
OpMemoryModel Logical GLSL450 |
| 78 |
OpEntryPoint Fragment %4 "main" |
| 79 |
OpExecutionMode %4 OriginUpperLeft |
| 80 |
OpSource ESSL 320 |
| 81 |
%2 = OpTypeVoid |
| 82 |
%3 = OpTypeFunction %2 |
| 83 |
%4 = OpFunction %2 None %3 |
| 84 |
%5 = OpLabel |
| 85 |
OpReturn |
| 86 |
OpFunctionEnd |
| 87 |
)"; |
| 88 |
|
| 89 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 90 |
const auto consumer = nullptr; |
| 91 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 92 |
spvtools::ValidatorOptions validator_options; |
| 93 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 94 |
kConsoleMessageConsumer)); |
| 95 |
TransformationContext transformation_context( |
| 96 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 97 |
|
| 98 |
ASSERT_FALSE(TransformationAddEarlyTerminatorWrapper(2, 101, spv::Op::OpKill) |
| 99 |
.IsApplicable(context.get(), transformation_context)); |
| 100 |
ASSERT_FALSE(TransformationAddEarlyTerminatorWrapper(100, 4, spv::Op::OpKill) |
| 101 |
.IsApplicable(context.get(), transformation_context)); |
| 102 |
ASSERT_FALSE( |
| 103 |
TransformationAddEarlyTerminatorWrapper(100, 100, spv::Op::OpKill) |
| 104 |
.IsApplicable(context.get(), transformation_context)); |
| 105 |
|
| 106 |
#ifndef NDEBUG |
| 107 |
ASSERT_DEATH( |
| 108 |
TransformationAddEarlyTerminatorWrapper(100, 101, spv::Op::OpReturn) |
| 109 |
.IsApplicable(context.get(), transformation_context), |
| 110 |
"Invalid opcode."); |
| 111 |
#endif |
| 112 |
|
| 113 |
auto transformation1 = |
| 114 |
TransformationAddEarlyTerminatorWrapper(100, 101, spv::Op::OpKill); |
| 115 |
auto transformation2 = |
| 116 |
TransformationAddEarlyTerminatorWrapper(102, 103, spv::Op::OpUnreachable); |
| 117 |
auto transformation3 = TransformationAddEarlyTerminatorWrapper( |
| 118 |
104, 105, spv::Op::OpTerminateInvocation); |
| 119 |
|
| 120 |
ASSERT_TRUE( |
| 121 |
transformation1.IsApplicable(context.get(), transformation_context)); |
| 122 |
ApplyAndCheckFreshIds(transformation1, context.get(), |
| 123 |
&transformation_context); |
| 124 |
ASSERT_TRUE( |
| 125 |
transformation2.IsApplicable(context.get(), transformation_context)); |
| 126 |
ApplyAndCheckFreshIds(transformation2, context.get(), |
| 127 |
&transformation_context); |
| 128 |
ASSERT_TRUE( |
| 129 |
transformation3.IsApplicable(context.get(), transformation_context)); |
| 130 |
ApplyAndCheckFreshIds(transformation3, context.get(), |
| 131 |
&transformation_context); |
| 132 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 133 |
kConsoleMessageConsumer)); |
| 134 |
|
| 135 |
std::string after_transformation = R"( |
| 136 |
OpCapability Shader |
| 137 |
OpExtension "SPV_KHR_terminate_invocation" |
| 138 |
%1 = OpExtInstImport "GLSL.std.450" |
| 139 |
OpMemoryModel Logical GLSL450 |
| 140 |
OpEntryPoint Fragment %4 "main" |
| 141 |
OpExecutionMode %4 OriginUpperLeft |
| 142 |
OpSource ESSL 320 |
| 143 |
%2 = OpTypeVoid |
| 144 |
%3 = OpTypeFunction %2 |
| 145 |
%4 = OpFunction %2 None %3 |
| 146 |
%5 = OpLabel |
| 147 |
OpReturn |
| 148 |
OpFunctionEnd |
| 149 |
%100 = OpFunction %2 None %3 |
| 150 |
%101 = OpLabel |
| 151 |
OpKill |
| 152 |
OpFunctionEnd |
| 153 |
%102 = OpFunction %2 None %3 |
| 154 |
%103 = OpLabel |
| 155 |
OpUnreachable |
| 156 |
OpFunctionEnd |
| 157 |
%104 = OpFunction %2 None %3 |
| 158 |
%105 = OpLabel |
| 159 |
OpTerminateInvocation |
| 160 |
OpFunctionEnd |
| 161 |
)"; |
| 162 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 163 |
} |
| 164 |
|
| 165 |
} // namespace |
| 166 |
} // namespace fuzz |
| 167 |
} // namespace spvtools |