| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 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_invert_comparison_operator.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(TransformationInvertComparisonOperatorTest, BasicTest) { |
| 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 |
%2 = OpTypeVoid |
| 35 |
%3 = OpTypeFunction %2 |
| 36 |
%6 = OpTypeInt 32 1 |
| 37 |
%7 = OpTypeInt 32 0 |
| 38 |
%8 = OpTypeBool |
| 39 |
%9 = OpConstant %6 3 |
| 40 |
%10 = OpConstant %6 4 |
| 41 |
%11 = OpConstant %7 3 |
| 42 |
%12 = OpConstant %7 4 |
| 43 |
%4 = OpFunction %2 None %3 |
| 44 |
%5 = OpLabel |
| 45 |
%13 = OpSLessThan %8 %9 %10 |
| 46 |
%14 = OpSLessThanEqual %8 %9 %10 |
| 47 |
%15 = OpSGreaterThan %8 %9 %10 |
| 48 |
%16 = OpSGreaterThanEqual %8 %9 %10 |
| 49 |
%17 = OpULessThan %8 %11 %12 |
| 50 |
%18 = OpULessThanEqual %8 %11 %12 |
| 51 |
%19 = OpUGreaterThan %8 %11 %12 |
| 52 |
%20 = OpUGreaterThanEqual %8 %11 %12 |
| 53 |
%21 = OpIEqual %8 %9 %10 |
| 54 |
%22 = OpINotEqual %8 %9 %10 |
| 55 |
OpReturn |
| 56 |
OpFunctionEnd |
| 57 |
)"; |
| 58 |
|
| 59 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 60 |
const auto consumer = nullptr; |
| 61 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 62 |
spvtools::ValidatorOptions validator_options; |
| 63 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 64 |
kConsoleMessageConsumer)); |
| 65 |
TransformationContext transformation_context( |
| 66 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 67 |
// Operator id is not valid. |
| 68 |
ASSERT_FALSE(TransformationInvertComparisonOperator(23, 23).IsApplicable( |
| 69 |
context.get(), transformation_context)); |
| 70 |
|
| 71 |
// Operator instruction is not supported. |
| 72 |
ASSERT_FALSE(TransformationInvertComparisonOperator(5, 23).IsApplicable( |
| 73 |
context.get(), transformation_context)); |
| 74 |
|
| 75 |
// Fresh id is not fresh. |
| 76 |
ASSERT_FALSE(TransformationInvertComparisonOperator(13, 22).IsApplicable( |
| 77 |
context.get(), transformation_context)); |
| 78 |
|
| 79 |
for (uint32_t fresh_id = 23, operator_id = 13; operator_id <= 22; |
| 80 |
++fresh_id, ++operator_id) { |
| 81 |
TransformationInvertComparisonOperator transformation(operator_id, |
| 82 |
fresh_id); |
| 83 |
ASSERT_TRUE( |
| 84 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 85 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 86 |
&transformation_context); |
| 87 |
} |
| 88 |
|
| 89 |
std::string expected = R"( |
| 90 |
OpCapability Shader |
| 91 |
%1 = OpExtInstImport "GLSL.std.450" |
| 92 |
OpMemoryModel Logical GLSL450 |
| 93 |
OpEntryPoint Fragment %4 "main" |
| 94 |
OpExecutionMode %4 OriginUpperLeft |
| 95 |
OpSource ESSL 310 |
| 96 |
%2 = OpTypeVoid |
| 97 |
%3 = OpTypeFunction %2 |
| 98 |
%6 = OpTypeInt 32 1 |
| 99 |
%7 = OpTypeInt 32 0 |
| 100 |
%8 = OpTypeBool |
| 101 |
%9 = OpConstant %6 3 |
| 102 |
%10 = OpConstant %6 4 |
| 103 |
%11 = OpConstant %7 3 |
| 104 |
%12 = OpConstant %7 4 |
| 105 |
%4 = OpFunction %2 None %3 |
| 106 |
%5 = OpLabel |
| 107 |
%23 = OpSGreaterThanEqual %8 %9 %10 |
| 108 |
%13 = OpLogicalNot %8 %23 |
| 109 |
%24 = OpSGreaterThan %8 %9 %10 |
| 110 |
%14 = OpLogicalNot %8 %24 |
| 111 |
%25 = OpSLessThanEqual %8 %9 %10 |
| 112 |
%15 = OpLogicalNot %8 %25 |
| 113 |
%26 = OpSLessThan %8 %9 %10 |
| 114 |
%16 = OpLogicalNot %8 %26 |
| 115 |
%27 = OpUGreaterThanEqual %8 %11 %12 |
| 116 |
%17 = OpLogicalNot %8 %27 |
| 117 |
%28 = OpUGreaterThan %8 %11 %12 |
| 118 |
%18 = OpLogicalNot %8 %28 |
| 119 |
%29 = OpULessThanEqual %8 %11 %12 |
| 120 |
%19 = OpLogicalNot %8 %29 |
| 121 |
%30 = OpULessThan %8 %11 %12 |
| 122 |
%20 = OpLogicalNot %8 %30 |
| 123 |
%31 = OpINotEqual %8 %9 %10 |
| 124 |
%21 = OpLogicalNot %8 %31 |
| 125 |
%32 = OpIEqual %8 %9 %10 |
| 126 |
%22 = OpLogicalNot %8 %32 |
| 127 |
OpReturn |
| 128 |
OpFunctionEnd |
| 129 |
)"; |
| 130 |
|
| 131 |
ASSERT_TRUE(IsEqual(env, expected, context.get())); |
| 132 |
} |
| 133 |
|
| 134 |
} // namespace |
| 135 |
} // namespace fuzz |
| 136 |
} // namespace spvtools |