| 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_swap_conditional_branch_operands.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(TransformationSwapConditionalBranchOperandsTest, 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 = OpTypePointer Function %6 |
| 38 |
%9 = OpConstant %6 0 |
| 39 |
%11 = OpConstant %6 1 |
| 40 |
%14 = OpTypeBool |
| 41 |
%4 = OpFunction %2 None %3 |
| 42 |
%5 = OpLabel |
| 43 |
%8 = OpVariable %7 Function |
| 44 |
%10 = OpVariable %7 Function |
| 45 |
OpStore %8 %9 |
| 46 |
OpStore %10 %11 |
| 47 |
%12 = OpLoad %6 %8 |
| 48 |
%13 = OpLoad %6 %10 |
| 49 |
%15 = OpSLessThan %14 %12 %13 |
| 50 |
OpSelectionMerge %17 None |
| 51 |
OpBranchConditional %15 %16 %21 10 20 |
| 52 |
%16 = OpLabel |
| 53 |
%18 = OpLoad %6 %10 |
| 54 |
%19 = OpLoad %6 %8 |
| 55 |
%20 = OpIAdd %6 %19 %18 |
| 56 |
OpBranch %17 |
| 57 |
%21 = OpLabel |
| 58 |
%22 = OpLoad %6 %10 |
| 59 |
%23 = OpLoad %6 %8 |
| 60 |
%24 = OpISub %6 %23 %22 |
| 61 |
OpBranch %17 |
| 62 |
%17 = OpLabel |
| 63 |
%25 = OpPhi %6 %20 %16 %24 %21 |
| 64 |
OpStore %8 %25 |
| 65 |
OpReturn |
| 66 |
OpFunctionEnd |
| 67 |
)"; |
| 68 |
|
| 69 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 70 |
const auto consumer = nullptr; |
| 71 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 72 |
spvtools::ValidatorOptions validator_options; |
| 73 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 74 |
kConsoleMessageConsumer)); |
| 75 |
TransformationContext transformation_context( |
| 76 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 77 |
// Invalid instruction descriptor. |
| 78 |
ASSERT_FALSE(TransformationSwapConditionalBranchOperands( |
| 79 |
MakeInstructionDescriptor(26, SpvOpPhi, 0), 26) |
| 80 |
.IsApplicable(context.get(), transformation_context)); |
| 81 |
|
| 82 |
// Descriptor for a wrong instruction. |
| 83 |
ASSERT_FALSE(TransformationSwapConditionalBranchOperands( |
| 84 |
MakeInstructionDescriptor(25, SpvOpPhi, 0), 26) |
| 85 |
.IsApplicable(context.get(), transformation_context)); |
| 86 |
|
| 87 |
// Fresh id is not fresh. |
| 88 |
ASSERT_FALSE(TransformationSwapConditionalBranchOperands( |
| 89 |
MakeInstructionDescriptor(15, SpvOpBranchConditional, 0), 25) |
| 90 |
.IsApplicable(context.get(), transformation_context)); |
| 91 |
|
| 92 |
TransformationSwapConditionalBranchOperands transformation( |
| 93 |
MakeInstructionDescriptor(15, SpvOpBranchConditional, 0), 26); |
| 94 |
ASSERT_TRUE( |
| 95 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 96 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 97 |
|
| 98 |
std::string after_transformation = R"( |
| 99 |
OpCapability Shader |
| 100 |
%1 = OpExtInstImport "GLSL.std.450" |
| 101 |
OpMemoryModel Logical GLSL450 |
| 102 |
OpEntryPoint Fragment %4 "main" |
| 103 |
OpExecutionMode %4 OriginUpperLeft |
| 104 |
OpSource ESSL 310 |
| 105 |
%2 = OpTypeVoid |
| 106 |
%3 = OpTypeFunction %2 |
| 107 |
%6 = OpTypeInt 32 1 |
| 108 |
%7 = OpTypePointer Function %6 |
| 109 |
%9 = OpConstant %6 0 |
| 110 |
%11 = OpConstant %6 1 |
| 111 |
%14 = OpTypeBool |
| 112 |
%4 = OpFunction %2 None %3 |
| 113 |
%5 = OpLabel |
| 114 |
%8 = OpVariable %7 Function |
| 115 |
%10 = OpVariable %7 Function |
| 116 |
OpStore %8 %9 |
| 117 |
OpStore %10 %11 |
| 118 |
%12 = OpLoad %6 %8 |
| 119 |
%13 = OpLoad %6 %10 |
| 120 |
%15 = OpSLessThan %14 %12 %13 |
| 121 |
%26 = OpLogicalNot %14 %15 |
| 122 |
OpSelectionMerge %17 None |
| 123 |
OpBranchConditional %26 %21 %16 20 10 |
| 124 |
%16 = OpLabel |
| 125 |
%18 = OpLoad %6 %10 |
| 126 |
%19 = OpLoad %6 %8 |
| 127 |
%20 = OpIAdd %6 %19 %18 |
| 128 |
OpBranch %17 |
| 129 |
%21 = OpLabel |
| 130 |
%22 = OpLoad %6 %10 |
| 131 |
%23 = OpLoad %6 %8 |
| 132 |
%24 = OpISub %6 %23 %22 |
| 133 |
OpBranch %17 |
| 134 |
%17 = OpLabel |
| 135 |
%25 = OpPhi %6 %20 %16 %24 %21 |
| 136 |
OpStore %8 %25 |
| 137 |
OpReturn |
| 138 |
OpFunctionEnd |
| 139 |
)"; |
| 140 |
|
| 141 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 142 |
} |
| 143 |
|
| 144 |
} // namespace |
| 145 |
} // namespace fuzz |
| 146 |
} // namespace spvtools |