/src/WasmEdge/lib/driver/fuzzTool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
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.30k | int FuzzTool(const uint8_t *Data, size_t Size) noexcept { |
16 | 9.30k | using namespace std::literals; |
17 | 9.30k | std::ios::sync_with_stdio(false); |
18 | 9.30k | spdlog::set_level(spdlog::level::critical); |
19 | | |
20 | 9.30k | Configure Conf; |
21 | 9.30k | Conf.getRuntimeConfigure().setForceInterpreter(true); |
22 | 9.30k | Loader::Loader Loader(Conf); |
23 | | |
24 | 9.30k | std::unique_ptr<AST::Module> Module; |
25 | 9.30k | if (auto Res = Loader.parseModule(Span<const uint8_t>(Data, Size))) { |
26 | 3.80k | Module = std::move(*Res); |
27 | 5.50k | } else { |
28 | 5.50k | const auto Err = static_cast<uint32_t>(Res.error()); |
29 | 5.50k | spdlog::error("Parse Module failed. Error code: {}"sv, Err); |
30 | 5.50k | return EXIT_FAILURE; |
31 | 5.50k | } |
32 | | |
33 | 3.80k | { |
34 | 3.80k | Validator::Validator ValidatorEngine(Conf); |
35 | 3.80k | if (auto Res = ValidatorEngine.validate(*Module); !Res) { |
36 | 1.66k | const auto Err = static_cast<uint32_t>(Res.error()); |
37 | 1.66k | spdlog::error("Validate Module failed. Error code: {}"sv, Err); |
38 | 1.66k | return EXIT_FAILURE; |
39 | 1.66k | } |
40 | 3.80k | } |
41 | | |
42 | 2.14k | LLVM::Compiler Compiler(Conf); |
43 | 2.14k | 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.14k | LLVM::CodeGen CodeGen(Conf); |
49 | 2.14k | if (auto Res = Compiler.compile(*Module); !Res) { |
50 | 0 | const auto Err = static_cast<uint32_t>(Res.error()); |
51 | 0 | spdlog::error("Compilation failed. Error code: {}"sv, Err); |
52 | 0 | return EXIT_FAILURE; |
53 | 2.14k | } else if (auto Res2 = CodeGen.codegen(Span<const uint8_t>(Data, Size), |
54 | 2.14k | std::move(*Res), "/dev/null"sv); |
55 | 2.14k | !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.14k | return EXIT_SUCCESS; |
62 | 2.14k | } |
63 | | |
64 | | } // namespace Driver |
65 | | } // namespace WasmEdge |
66 | | #endif |