Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/include/loader/shared_library.h
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
//===-- wasmedge/loader/shared_library.h - Shared library definition ------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the declaration of the SharedLibrary, which holds handle
12
/// to loaded library.
13
///
14
//===----------------------------------------------------------------------===//
15
#pragma once
16
17
#include "ast/section.h"
18
#include "common/executable.h"
19
#include "common/filesystem.h"
20
#include "system/winapi.h"
21
22
#include <cstdint>
23
#include <memory>
24
#include <vector>
25
26
namespace WasmEdge {
27
namespace Loader {
28
29
/// Holder class for library handle
30
class SharedLibrary : public Executable {
31
public:
32
#if WASMEDGE_OS_WINDOWS
33
  using NativeHandle = winapi::HMODULE_;
34
#else
35
  using NativeHandle = void *;
36
#endif
37
38
0
  SharedLibrary() noexcept = default;
39
0
  ~SharedLibrary() noexcept override { unload(); }
40
  Expect<void> load(const std::filesystem::path &Path) noexcept;
41
  void unload() noexcept;
42
43
0
  Symbol<const IntrinsicsTable *> getIntrinsics() noexcept override {
44
0
    return get<const IntrinsicsTable *>("intrinsics");
45
0
  }
46
47
0
  std::vector<Symbol<Wrapper>> getTypes(size_t Size) noexcept override {
48
0
    using namespace std::literals;
49
0
    std::vector<Symbol<Wrapper>> Result;
50
0
    Result.reserve(Size);
51
0
    for (size_t I = 0; I < Size; ++I) {
52
      // "t" prefix is for type helper function
53
0
      const std::string Name = fmt::format("t{}"sv, I);
54
0
      if (auto Symbol = get<Wrapper>(Name.c_str())) {
55
0
        Result.push_back(std::move(Symbol));
56
0
      }
57
0
    }
58
59
0
    return Result;
60
0
  }
61
62
  std::vector<Symbol<void>> getCodes(size_t Offset,
63
0
                                     size_t Size) noexcept override {
64
0
    using namespace std::literals;
65
0
    std::vector<Symbol<void>> Result;
66
0
    Result.reserve(Size);
67
0
    for (size_t I = 0; I < Size; ++I) {
68
      // "f" prefix is for code function
69
0
      const std::string Name = fmt::format("f{}"sv, I + Offset);
70
0
      if (auto Symbol = get<void>(Name.c_str())) {
71
0
        Result.push_back(std::move(Symbol));
72
0
      }
73
0
    }
74
75
0
    return Result;
76
0
  }
77
78
  /// Read embedded Wasm binary.
79
0
  Expect<std::vector<Byte>> getWasm() noexcept {
80
0
    const auto Size = get<uint32_t>("wasm.size");
81
0
    if (unlikely(!Size)) {
82
0
      spdlog::error(ErrCode::Value::IllegalGrammar);
83
0
      return Unexpect(ErrCode::Value::IllegalGrammar);
84
0
    }
85
0
    const auto Code = get<uint8_t>("wasm.code");
86
0
    if (unlikely(!Code)) {
87
0
      spdlog::error(ErrCode::Value::IllegalGrammar);
88
0
      return Unexpect(ErrCode::Value::IllegalGrammar);
89
0
    }
90
91
0
    return std::vector<Byte>(Code.get(), Code.get() + *Size);
92
0
  }
93
94
  /// Read wasmedge version.
95
0
  Expect<uint32_t> getVersion() noexcept {
96
0
    const auto Version = get<uint32_t>("version");
97
0
    if (unlikely(!Version)) {
98
0
      spdlog::error(ErrCode::Value::IllegalGrammar);
99
0
      return Unexpect(ErrCode::Value::IllegalGrammar);
100
0
    }
101
0
    return *Version;
102
0
  }
103
104
0
  template <typename T> Symbol<T> get(const char *Name) {
105
0
    return createSymbol<T>(reinterpret_cast<T *>(getSymbolAddr(Name)));
106
0
  }
Unexecuted instantiation: WasmEdge::Symbol<void* const (*) [38]> WasmEdge::Loader::SharedLibrary::get<void* const (*) [38]>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<void (void*, void*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>*)> WasmEdge::Loader::SharedLibrary::get<void (void*, void*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>*)>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<void> WasmEdge::Loader::SharedLibrary::get<void>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<unsigned int> WasmEdge::Loader::SharedLibrary::get<unsigned int>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<unsigned char> WasmEdge::Loader::SharedLibrary::get<unsigned char>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<WasmEdge::Plugin::Plugin::PluginDescriptor const* ()> WasmEdge::Loader::SharedLibrary::get<WasmEdge::Plugin::Plugin::PluginDescriptor const* ()>(char const*)
Unexecuted instantiation: WasmEdge::Symbol<WasmEdge_PluginDescriptor const* ()> WasmEdge::Loader::SharedLibrary::get<WasmEdge_PluginDescriptor const* ()>(char const*)
107
108
private:
109
  void *getSymbolAddr(const char *Name) const noexcept;
110
  NativeHandle Handle{};
111
};
112
113
} // namespace Loader
114
} // namespace WasmEdge