Create common utility class for fuzzing
This moves the usage of Tint in the fuzzers into a single class. This
will be expanded in the future to support emitting shaders, so fuzzing
tests, just need to indicate which reader & writer they want to use,
instead of having each test implement all of the API usage logic
directly.
BUG=tint:199
Change-Id: Id081a374014b7640a07b267e544ddeba3e6329dd
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/36760
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 547b74a..2943ab0 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1308,6 +1308,10 @@
source_set("tint_fuzzer_common") {
configs += [ ":tint_common_config" ]
public_configs = [ ":tint_public_config" ]
+ sources = [
+ "fuzzers/tint_common_fuzzer.cc",
+ "fuzzers/tint_common_fuzzer.h",
+ ]
}
if (tint_build_wgsl_reader) {
diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt
index bfa50e7..dc585f1 100644
--- a/fuzzers/CMakeLists.txt
+++ b/fuzzers/CMakeLists.txt
@@ -13,7 +13,9 @@
# limitations under the License.
function(add_tint_fuzzer NAME)
- add_executable(${NAME} ${NAME}.cc)
+ add_executable(${NAME}
+ ${NAME}.cc
+ tint_common_fuzzer.cc)
target_link_libraries(${NAME} libtint-fuzz)
tint_default_compile_options(${NAME})
target_compile_options(${NAME} PRIVATE -Wno-missing-prototypes)
diff --git a/fuzzers/tint_common_fuzzer.cc b/fuzzers/tint_common_fuzzer.cc
new file mode 100644
index 0000000..f211b49
--- /dev/null
+++ b/fuzzers/tint_common_fuzzer.cc
@@ -0,0 +1,69 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace tint {
+namespace fuzzers {
+
+CommonFuzzer::CommonFuzzer(InputFormat input) : input_(input) {}
+CommonFuzzer::~CommonFuzzer() = default;
+
+int CommonFuzzer::Run(const uint8_t* data, size_t size) {
+ std::unique_ptr<tint::reader::Reader> parser;
+#if TINT_BUILD_WGSL_READER
+ std::unique_ptr<tint::Source::File> file;
+#endif // TINT_BUILD_WGSL_READER
+
+ switch (input_) {
+ case InputFormat::kWGSL:
+#if TINT_BUILD_WGSL_READER
+ {
+ std::string str(reinterpret_cast<const char*>(data), size);
+
+ file = std::make_unique<tint::Source::File>("test.wgsl", str);
+ parser = std::make_unique<tint::reader::wgsl::Parser>(file.get());
+ }
+#endif // TINT_BUILD_WGSL_READER
+ break;
+ case InputFormat::kSpv:
+#if TINT_BUILD_SPV_READER
+ {
+ size_t sizeInU32 = size / sizeof(uint32_t);
+ const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data);
+ std::vector<uint32_t> input(u32Data, u32Data + sizeInU32);
+
+ if (input.size() != 0) {
+ parser = std::make_unique<tint::reader::spirv::Parser>(input);
+ }
+ }
+#endif // TINT_BUILD_WGSL_READER
+ break;
+ default:
+ break;
+ }
+
+ if (parser) {
+ parser->Parse();
+ }
+
+ return 0;
+}
+
+} // namespace fuzzers
+} // namespace tint
diff --git a/fuzzers/tint_common_fuzzer.h b/fuzzers/tint_common_fuzzer.h
new file mode 100644
index 0000000..6e1f2b4
--- /dev/null
+++ b/fuzzers/tint_common_fuzzer.h
@@ -0,0 +1,39 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef FUZZERS_TINT_COMMON_FUZZER_H_
+#define FUZZERS_TINT_COMMON_FUZZER_H_
+
+#include "include/tint/tint.h"
+
+namespace tint {
+namespace fuzzers {
+
+enum class InputFormat { kWGSL, kSpv, kNone };
+
+class CommonFuzzer {
+ public:
+ explicit CommonFuzzer(InputFormat input);
+ ~CommonFuzzer();
+
+ int Run(const uint8_t* data, size_t size);
+
+ private:
+ InputFormat input_;
+};
+
+} // namespace fuzzers
+} // namespace tint
+
+#endif // FUZZERS_TINT_COMMON_FUZZER_H_
diff --git a/fuzzers/tint_spv_reader_fuzzer.cc b/fuzzers/tint_spv_reader_fuzzer.cc
index e809e55..aeb6ae7 100644
--- a/fuzzers/tint_spv_reader_fuzzer.cc
+++ b/fuzzers/tint_spv_reader_fuzzer.cc
@@ -14,17 +14,9 @@
#include <vector>
-#include "src/reader/spirv/parser.h"
+#include "fuzzers/tint_common_fuzzer.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
- size_t sizeInU32 = size / sizeof(uint32_t);
- const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data);
- std::vector<uint32_t> input(u32Data, u32Data + sizeInU32);
-
- if (input.size() != 0) {
- tint::reader::spirv::Parser parser(input);
- parser.Parse();
- }
-
- return 0;
+ tint::fuzzers::CommonFuzzer fuzzer(tint::fuzzers::InputFormat::kSpv);
+ return fuzzer.Run(data, size);
}
diff --git a/fuzzers/tint_wgsl_reader_fuzzer.cc b/fuzzers/tint_wgsl_reader_fuzzer.cc
index d1a7507..99edc29 100644
--- a/fuzzers/tint_wgsl_reader_fuzzer.cc
+++ b/fuzzers/tint_wgsl_reader_fuzzer.cc
@@ -14,14 +14,9 @@
#include <string>
-#include "src/reader/wgsl/parser.h"
+#include "fuzzers/tint_common_fuzzer.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
- std::string str(reinterpret_cast<const char*>(data), size);
-
- tint::Source::File file("test.wgsl", str);
- tint::reader::wgsl::Parser parser(&file);
- parser.Parse();
-
- return 0;
+ tint::fuzzers::CommonFuzzer fuzzer(tint::fuzzers::InputFormat::kWGSL);
+ return fuzzer.Run(data, size);
}