/src/WasmEdge/lib/driver/fuzzTool.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #ifdef WASMEDGE_BUILD_FUZZING |
5 | | #include "driver/fuzzTool.h" |
6 | | #include "common/configure.h" |
7 | | #include "loader/loader.h" |
8 | | #include "validator/validator.h" |
9 | | #include "llvm/codegen.h" |
10 | | #include "llvm/compiler.h" |
11 | | |
12 | | namespace WasmEdge { |
13 | | namespace Driver { |
14 | | |
15 | 9.06k | int FuzzTool(const uint8_t *Data, size_t Size) noexcept { |
16 | 9.06k | using namespace std::literals; |
17 | 9.06k | std::ios::sync_with_stdio(false); |
18 | 9.06k | spdlog::set_level(spdlog::level::critical); |
19 | | |
20 | 9.06k | Configure Conf; |
21 | 9.06k | Conf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter); |
22 | 9.06k | Loader::Loader Loader(Conf); |
23 | | |
24 | 9.06k | std::unique_ptr<AST::Module> Module; |
25 | 9.06k | if (auto Res = Loader.parseModule(Span<const uint8_t>(Data, Size))) { |
26 | 4.08k | Module = std::move(*Res); |
27 | 4.98k | } else { |
28 | 4.98k | const auto Err = static_cast<uint32_t>(Res.error()); |
29 | 4.98k | spdlog::error("Parse Module failed. Error code: {}"sv, Err); |
30 | 4.98k | return EXIT_FAILURE; |
31 | 4.98k | } |
32 | | |
33 | 4.08k | { |
34 | 4.08k | Validator::Validator ValidatorEngine(Conf); |
35 | 4.08k | if (auto Res = ValidatorEngine.validate(*Module); !Res) { |
36 | 1.85k | const auto Err = static_cast<uint32_t>(Res.error()); |
37 | 1.85k | spdlog::error("Validate Module failed. Error code: {}"sv, Err); |
38 | 1.85k | return EXIT_FAILURE; |
39 | 1.85k | } |
40 | 4.08k | } |
41 | | |
42 | 2.23k | LLVM::Compiler Compiler(Conf); |
43 | 2.23k | if (auto Res = Compiler.checkConfigure(); !Res) { |
44 | 0 | const auto Err = static_cast<uint32_t>(Res.error()); |
45 | 0 | spdlog::error("Compiler Configure failed. Error code: {}"sv, Err); |
46 | 0 | } |
47 | | |
48 | 2.23k | LLVM::CodeGen CodeGen(Conf); |
49 | 2.23k | if (auto Res = Compiler.compile(*Module); !Res) { |
50 | 9 | const auto Err = static_cast<uint32_t>(Res.error()); |
51 | 9 | spdlog::error("Compilation failed. Error code: {}"sv, Err); |
52 | 9 | return EXIT_FAILURE; |
53 | 2.22k | } else if (auto Res2 = CodeGen.codegen(Span<const uint8_t>(Data, Size), |
54 | 2.22k | std::move(*Res), "/dev/null"sv); |
55 | 2.22k | !Res2) { |
56 | 0 | const auto Err = static_cast<uint32_t>(Res2.error()); |
57 | 0 | spdlog::error("Code Generation failed. Error code: {}"sv, Err); |
58 | 0 | return EXIT_FAILURE; |
59 | 0 | } |
60 | | |
61 | 2.22k | return EXIT_SUCCESS; |
62 | 2.23k | } |
63 | | |
64 | | } // namespace Driver |
65 | | } // namespace WasmEdge |
66 | | #endif |