Coverage Report

Created: 2025-11-16 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/driver/fuzzTool.cpp
Line
Count
Source
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.39k
int FuzzTool(const uint8_t *Data, size_t Size) noexcept {
16
9.39k
  using namespace std::literals;
17
9.39k
  std::ios::sync_with_stdio(false);
18
9.39k
  spdlog::set_level(spdlog::level::critical);
19
20
9.39k
  Configure Conf;
21
9.39k
  Conf.getRuntimeConfigure().setForceInterpreter(true);
22
9.39k
  Loader::Loader Loader(Conf);
23
24
9.39k
  std::unique_ptr<AST::Module> Module;
25
9.39k
  if (auto Res = Loader.parseModule(Span<const uint8_t>(Data, Size))) {
26
4.30k
    Module = std::move(*Res);
27
5.09k
  } else {
28
5.09k
    const auto Err = static_cast<uint32_t>(Res.error());
29
5.09k
    spdlog::error("Parse Module failed. Error code: {}"sv, Err);
30
5.09k
    return EXIT_FAILURE;
31
5.09k
  }
32
33
4.30k
  {
34
4.30k
    Validator::Validator ValidatorEngine(Conf);
35
4.30k
    if (auto Res = ValidatorEngine.validate(*Module); !Res) {
36
1.99k
      const auto Err = static_cast<uint32_t>(Res.error());
37
1.99k
      spdlog::error("Validate Module failed. Error code: {}"sv, Err);
38
1.99k
      return EXIT_FAILURE;
39
1.99k
    }
40
4.30k
  }
41
42
2.30k
  LLVM::Compiler Compiler(Conf);
43
2.30k
  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.30k
  LLVM::CodeGen CodeGen(Conf);
49
2.30k
  if (auto Res = Compiler.compile(*Module); !Res) {
50
7
    const auto Err = static_cast<uint32_t>(Res.error());
51
7
    spdlog::error("Compilation failed. Error code: {}"sv, Err);
52
7
    return EXIT_FAILURE;
53
2.29k
  } else if (auto Res2 = CodeGen.codegen(Span<const uint8_t>(Data, Size),
54
2.29k
                                         std::move(*Res), "/dev/null"sv);
55
2.29k
             !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.29k
  return EXIT_SUCCESS;
62
2.30k
}
63
64
} // namespace Driver
65
} // namespace WasmEdge
66
#endif