• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/transformation_add_global_undef.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(TransformationAddGlobalUndefTest,BasicTest)25 TEST(TransformationAddGlobalUndefTest, BasicTest) {
26   std::string shader = R"(
27                OpCapability Shader
28           %1 = OpExtInstImport "GLSL.std.450"
29                OpMemoryModel Logical GLSL450
30                OpEntryPoint Fragment %4 "main"
31                OpExecutionMode %4 OriginUpperLeft
32                OpSource ESSL 310
33           %2 = OpTypeVoid
34           %3 = OpTypeFunction %2
35           %6 = OpTypeFloat 32
36           %7 = OpTypeInt 32 1
37           %8 = OpTypeVector %6 2
38           %9 = OpTypeVector %6 3
39          %10 = OpTypeVector %6 4
40          %11 = OpTypeVector %7 2
41           %4 = OpFunction %2 None %3
42           %5 = OpLabel
43                OpReturn
44                OpFunctionEnd
45   )";
46 
47   const auto env = SPV_ENV_UNIVERSAL_1_4;
48   const auto consumer = nullptr;
49   const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
50   spvtools::ValidatorOptions validator_options;
51   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
52                                                kConsoleMessageConsumer));
53   TransformationContext transformation_context(
54       MakeUnique<FactManager>(context.get()), validator_options);
55   // Id already in use
56   ASSERT_FALSE(TransformationAddGlobalUndef(4, 11).IsApplicable(
57       context.get(), transformation_context));
58   // %1 is not a type
59   ASSERT_FALSE(TransformationAddGlobalUndef(100, 1).IsApplicable(
60       context.get(), transformation_context));
61 
62   // %3 is a function type
63   ASSERT_FALSE(TransformationAddGlobalUndef(100, 3).IsApplicable(
64       context.get(), transformation_context));
65 
66   {
67     // %100 = OpUndef %6
68     TransformationAddGlobalUndef transformation(100, 6);
69     ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(100));
70     ASSERT_TRUE(
71         transformation.IsApplicable(context.get(), transformation_context));
72     ApplyAndCheckFreshIds(transformation, context.get(),
73                           &transformation_context);
74     ASSERT_EQ(SpvOpUndef, context->get_def_use_mgr()->GetDef(100)->opcode());
75   }
76 
77   TransformationAddGlobalUndef transformations[] = {
78 
79       // %101 = OpUndef %7
80       TransformationAddGlobalUndef(101, 7),
81 
82       // %102 = OpUndef %8
83       TransformationAddGlobalUndef(102, 8),
84 
85       // %103 = OpUndef %9
86       TransformationAddGlobalUndef(103, 9),
87 
88       // %104 = OpUndef %10
89       TransformationAddGlobalUndef(104, 10),
90 
91       // %105 = OpUndef %11
92       TransformationAddGlobalUndef(105, 11)};
93 
94   for (auto& transformation : transformations) {
95     ASSERT_TRUE(
96         transformation.IsApplicable(context.get(), transformation_context));
97     ApplyAndCheckFreshIds(transformation, context.get(),
98                           &transformation_context);
99   }
100   ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
101                                                kConsoleMessageConsumer));
102 
103   std::string after_transformation = R"(
104                OpCapability Shader
105           %1 = OpExtInstImport "GLSL.std.450"
106                OpMemoryModel Logical GLSL450
107                OpEntryPoint Fragment %4 "main"
108                OpExecutionMode %4 OriginUpperLeft
109                OpSource ESSL 310
110           %2 = OpTypeVoid
111           %3 = OpTypeFunction %2
112           %6 = OpTypeFloat 32
113           %7 = OpTypeInt 32 1
114           %8 = OpTypeVector %6 2
115           %9 = OpTypeVector %6 3
116          %10 = OpTypeVector %6 4
117          %11 = OpTypeVector %7 2
118         %100 = OpUndef %6
119         %101 = OpUndef %7
120         %102 = OpUndef %8
121         %103 = OpUndef %9
122         %104 = OpUndef %10
123         %105 = OpUndef %11
124           %4 = OpFunction %2 None %3
125           %5 = OpLabel
126                OpReturn
127                OpFunctionEnd
128   )";
129   ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
130 }
131 
132 }  // namespace
133 }  // namespace fuzz
134 }  // namespace spvtools
135