1 // Copyright (c) 2019 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/fact_manager/fact_manager.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(DeadBlockFactsTest,BlockIsDead)25 TEST(DeadBlockFactsTest, BlockIsDead) {
26   std::string shader = R"(
27                OpCapability Shader
28           %1 = OpExtInstImport "GLSL.std.450"
29                OpMemoryModel Logical GLSL450
30                OpEntryPoint Fragment %2 "main"
31                OpExecutionMode %2 OriginUpperLeft
32                OpSource ESSL 310
33           %3 = OpTypeVoid
34           %4 = OpTypeFunction %3
35           %5 = OpTypeBool
36           %6 = OpConstantTrue %5
37           %7 = OpTypeInt 32 1
38           %8 = OpTypePointer Function %7
39           %2 = OpFunction %3 None %4
40           %9 = OpLabel
41                OpSelectionMerge %10 None
42                OpBranchConditional %6 %11 %12
43          %11 = OpLabel
44                OpBranch %10
45          %12 = OpLabel
46                OpBranch %10
47          %10 = OpLabel
48                OpReturn
49                OpFunctionEnd
50 )";
51 
52   const auto env = SPV_ENV_UNIVERSAL_1_5;
53   const auto consumer = nullptr;
54   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
55   spvtools::ValidatorOptions validator_options;
56   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
57                                                kConsoleMessageConsumer));
58 
59   FactManager fact_manager(context.get());
60 
61   ASSERT_FALSE(fact_manager.BlockIsDead(9));
62   ASSERT_FALSE(fact_manager.BlockIsDead(11));
63   ASSERT_FALSE(fact_manager.BlockIsDead(12));
64 
65   fact_manager.AddFactBlockIsDead(12);
66 
67   ASSERT_FALSE(fact_manager.BlockIsDead(9));
68   ASSERT_FALSE(fact_manager.BlockIsDead(11));
69   ASSERT_TRUE(fact_manager.BlockIsDead(12));
70 }
71 
72 }  // namespace
73 }  // namespace fuzz
74 }  // namespace spvtools
75