| 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/instruction_descriptor.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(InstructionDescriptorTest, 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 = OpTypeInt 32 0 |
| 36 |
%7 = OpTypePointer Function %6 |
| 37 |
%9 = OpConstant %6 0 |
| 38 |
%10 = OpTypeInt 32 1 |
| 39 |
%11 = OpTypePointer Function %10 |
| 40 |
%13 = OpConstant %10 2 |
| 41 |
%32 = OpConstant %10 0 |
| 42 |
%4 = OpFunction %2 None %3 |
| 43 |
%5 = OpLabel |
| 44 |
%164 = OpVariable %11 Function |
| 45 |
%165 = OpVariable %11 Function |
| 46 |
OpBranch %16 |
| 47 |
%16 = OpLabel |
| 48 |
OpStore %164 %32 |
| 49 |
OpStore %165 %13 |
| 50 |
OpReturn |
| 51 |
OpFunctionEnd |
| 52 |
)"; |
| 53 |
|
| 54 |
const auto env = SPV_ENV_UNIVERSAL_1_4; |
| 55 |
const auto consumer = nullptr; |
| 56 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 57 |
spvtools::ValidatorOptions validator_options; |
| 58 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 59 |
kConsoleMessageConsumer)); |
| 60 |
|
| 61 |
for (auto& function : *context->module()) { |
| 62 |
for (auto& block : function) { |
| 63 |
for (auto inst_it = block.cbegin(); inst_it != block.cend(); ++inst_it) { |
| 64 |
ASSERT_EQ(&*inst_it, |
| 65 |
FindInstruction(MakeInstructionDescriptor(block, inst_it), |
| 66 |
context.get())); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace |
| 73 |
} // namespace fuzz |
| 74 |
} // namespace spvtools |