Coverage Report

Created: 2026-09-28 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/runtime/instance/module.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
//===-- wasmedge/runtime/instance/module.h - Module Instance definition ---===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the module instance definition in store manager.
12
///
13
//===----------------------------------------------------------------------===//
14
#pragma once
15
16
#include "ast/component/component.h"
17
#include "ast/module.h"
18
#include "common/errcode.h"
19
#include "runtime/hostfunc.h"
20
#include "runtime/instance/array.h"
21
#include "runtime/instance/data.h"
22
#include "runtime/instance/elem.h"
23
#include "runtime/instance/exception.h"
24
#include "runtime/instance/function.h"
25
#include "runtime/instance/global.h"
26
#include "runtime/instance/memory.h"
27
#include "runtime/instance/reflifetime.h"
28
#include "runtime/instance/struct.h"
29
#include "runtime/instance/table.h"
30
#include "runtime/instance/tag.h"
31
32
#include <atomic>
33
#include <cstddef>
34
#include <functional>
35
#include <map>
36
#include <memory>
37
#include <mutex>
38
#include <set>
39
#include <shared_mutex>
40
#include <string>
41
#include <type_traits>
42
#include <unordered_set>
43
#include <vector>
44
45
namespace WasmEdge {
46
47
namespace Executor {
48
class Executor;
49
}
50
51
namespace Runtime {
52
53
class StoreManager;
54
class CallingFrame;
55
56
namespace Instance {
57
58
namespace {
59
/// Return true if T is an entity that can be exported or imported.
60
template <typename T>
61
inline constexpr const bool IsEntityV =
62
    std::is_same_v<T, Instance::FunctionInstance> ||
63
    std::is_same_v<T, Instance::TableInstance> ||
64
    std::is_same_v<T, Instance::MemoryInstance> ||
65
    std::is_same_v<T, Instance::GlobalInstance> ||
66
    std::is_same_v<T, Instance::TagInstance>;
67
68
/// Return true if T is an instance.
69
template <typename T>
70
inline constexpr const bool IsInstanceV =
71
    IsEntityV<T> || std::is_same_v<T, Instance::ElementInstance> ||
72
    std::is_same_v<T, Instance::DataInstance>;
73
} // namespace
74
75
class ComponentInstance;
76
77
/// Module instances use a dependency-pinned lifetime: each importer pins the
78
/// instances it imports from (see RefLifetime) and releases them when
79
/// destroyed, so a heap instance torn down via terminate() is deleted only
80
/// after its owner and all importers release.
81
///
82
/// A stack- or member-allocated provider cannot be deferred by a pin, so it
83
/// must outlive its importers: declare a host module before the VM or
84
/// StoreManager that imports it. The destructor enforces this with
85
/// assuming(!hasDependents()).
86
class ModuleInstance {
87
public:
88
  ModuleInstance(std::string_view Name, void *Data = nullptr,
89
                 std::function<void(void *)> Finalizer = nullptr)
90
0
      : ModName(Name), HostData(Data), HostDataFinalizer(Finalizer) {}
91
0
  virtual ~ModuleInstance() noexcept {
92
    // Fallback for instances torn down outside terminate() (e.g.
93
    // stack-allocated).
94
0
    unlinkAllStores();
95
0
    assuming(!Life.hasDependents());
96
0
    if (HostDataFinalizer.operator bool()) {
97
0
      HostDataFinalizer(HostData);
98
0
    }
99
0
    releaseProviders();
100
0
  }
101
102
0
  void terminate() noexcept {
103
0
    unlinkAllStores();
104
0
    if (Life.releaseOwner()) {
105
0
      delete this;
106
0
    }
107
0
  }
108
109
  /// Mark this module instance finalized (immutable). Idempotent, thread-safe.
110
0
  void finalizeInstantiation() const noexcept {
111
0
    if (InstantiateFinalized.load(std::memory_order_acquire)) {
112
0
      return;
113
0
    }
114
0
    std::unique_lock Lock(Mutex);
115
0
    InstantiateFinalized.store(true, std::memory_order_release);
116
0
  }
117
118
  /// Return true if this module instance has been finalized (immutable).
119
0
  bool isInstantiateFinalized() const noexcept {
120
0
    return InstantiateFinalized.load(std::memory_order_acquire);
121
0
  }
122
123
0
  std::string_view getModuleName() const noexcept {
124
0
    std::shared_lock Lock(Mutex);
125
0
    return ModName;
126
0
  }
127
128
0
  void *getHostData() const noexcept { return HostData; }
129
130
0
  Span<const FunctionInstance *const> getFunctionInstances() const noexcept {
131
0
    return Span<const FunctionInstance *const>(
132
0
        const_cast<const FunctionInstance *const *>(FuncInsts.data()),
133
0
        FuncInsts.size());
134
0
  }
135
136
0
  Span<const MemoryInstance *const> getMemoryInstances() const noexcept {
137
0
    return Span<const MemoryInstance *const>(
138
0
        const_cast<const MemoryInstance *const *>(MemInsts.data()),
139
0
        MemInsts.size());
140
0
  }
141
142
0
  Span<const GlobalInstance *const> getGlobalInstances() const noexcept {
143
0
    return Span<const GlobalInstance *const>(
144
0
        const_cast<const GlobalInstance *const *>(GlobInsts.data()),
145
0
        GlobInsts.size());
146
0
  }
147
148
0
  Span<const DataInstance *const> getOwnedDataInstances() const noexcept {
149
0
    return Span<const DataInstance *const>(
150
0
        const_cast<const DataInstance *const *>(DataInsts.data()),
151
0
        DataInsts.size());
152
0
  }
153
154
  /// Add existing instances and move ownership with the export name.
155
  ///
156
  /// These functions fail with `ErrCode::Value::WrongVMWorkflow` if the module
157
  /// instance has already been finalized, i.e. it has been used during
158
  /// execution and become immutable. On failure the passed-in `unique_ptr` is
159
  /// not moved from, so the caller retains ownership of the instance.
160
  Expect<void> addHostFunc(std::string_view Name,
161
0
                           std::unique_ptr<HostFunctionBase> &&Func) {
162
0
    std::unique_lock Lock(Mutex);
163
0
    if (unlikely(InstantiateFinalized.load(std::memory_order_acquire))) {
164
0
      return Unexpect(ErrCode::Value::WrongVMWorkflow);
165
0
    }
166
0
    unsafeImportDefinedType(Func->getDefinedType());
167
0
    unsafeAddHostInstance(
168
0
        Name, OwnedFuncInsts, FuncInsts, ExpFuncs,
169
0
        std::make_unique<FunctionInstance>(
170
0
            this, static_cast<uint32_t>(Types.size()) - 1, std::move(Func)));
171
0
    return {};
172
0
  }
173
  Expect<void> addHostFunc(std::string_view Name,
174
0
                           std::unique_ptr<FunctionInstance> &&Func) {
175
0
    std::unique_lock Lock(Mutex);
176
0
    if (unlikely(InstantiateFinalized.load(std::memory_order_acquire))) {
177
0
      return Unexpect(ErrCode::Value::WrongVMWorkflow);
178
0
    }
179
0
    assuming(Func->isHostFunction());
180
0
    unsafeImportDefinedType(Func->getHostFunc().getDefinedType());
181
0
    Func->linkDefinedType(this, static_cast<uint32_t>(Types.size()) - 1);
182
0
    unsafeAddHostInstance(Name, OwnedFuncInsts, FuncInsts, ExpFuncs,
183
0
                          std::move(Func));
184
0
    return {};
185
0
  }
186
187
  Expect<void> addHostTable(std::string_view Name,
188
0
                            std::unique_ptr<TableInstance> &&Tab) {
189
0
    std::unique_lock Lock(Mutex);
190
0
    if (unlikely(InstantiateFinalized.load(std::memory_order_acquire))) {
191
0
      return Unexpect(ErrCode::Value::WrongVMWorkflow);
192
0
    }
193
0
    unsafeAddHostInstance(Name, OwnedTabInsts, TabInsts, ExpTables,
194
0
                          std::move(Tab));
195
0
    return {};
196
0
  }
197
  Expect<void> addHostMemory(std::string_view Name,
198
0
                             std::unique_ptr<MemoryInstance> &&Mem) {
199
0
    std::unique_lock Lock(Mutex);
200
0
    if (unlikely(InstantiateFinalized.load(std::memory_order_acquire))) {
201
0
      return Unexpect(ErrCode::Value::WrongVMWorkflow);
202
0
    }
203
0
    unsafeAddHostInstance(Name, OwnedMemInsts, MemInsts, ExpMems,
204
0
                          std::move(Mem));
205
0
    return {};
206
0
  }
207
  Expect<void> addHostGlobal(std::string_view Name,
208
0
                             std::unique_ptr<GlobalInstance> &&Glob) {
209
0
    std::unique_lock Lock(Mutex);
210
0
    if (unlikely(InstantiateFinalized.load(std::memory_order_acquire))) {
211
0
      return Unexpect(ErrCode::Value::WrongVMWorkflow);
212
0
    }
213
0
    unsafeAddHostInstance(Name, OwnedGlobInsts, GlobInsts, ExpGlobals,
214
0
                          std::move(Glob));
215
0
    return {};
216
0
  }
217
218
  /// Find and get the exported instance by name.
219
0
  FunctionInstance *findFuncExports(std::string_view ExtName) const noexcept {
220
0
    std::shared_lock Lock(Mutex);
221
0
    return unsafeFindExports(ExpFuncs, ExtName);
222
0
  }
223
0
  TableInstance *findTableExports(std::string_view ExtName) const noexcept {
224
0
    std::shared_lock Lock(Mutex);
225
0
    return unsafeFindExports(ExpTables, ExtName);
226
0
  }
227
0
  MemoryInstance *findMemoryExports(std::string_view ExtName) const noexcept {
228
0
    std::shared_lock Lock(Mutex);
229
0
    return unsafeFindExports(ExpMems, ExtName);
230
0
  }
231
0
  TagInstance *findTagExports(std::string_view ExtName) const noexcept {
232
0
    std::shared_lock Lock(Mutex);
233
0
    return unsafeFindExports(ExpTags, ExtName);
234
0
  }
235
0
  GlobalInstance *findGlobalExports(std::string_view ExtName) const noexcept {
236
0
    std::shared_lock Lock(Mutex);
237
0
    return unsafeFindExports(ExpGlobals, ExtName);
238
0
  }
239
240
  /// Get the exported instances count.
241
0
  uint32_t getFuncExportNum() const noexcept {
242
0
    std::shared_lock Lock(Mutex);
243
0
    return static_cast<uint32_t>(ExpFuncs.size());
244
0
  }
245
0
  uint32_t getTableExportNum() const noexcept {
246
0
    std::shared_lock Lock(Mutex);
247
0
    return static_cast<uint32_t>(ExpTables.size());
248
0
  }
249
0
  uint32_t getMemoryExportNum() const noexcept {
250
0
    std::shared_lock Lock(Mutex);
251
0
    return static_cast<uint32_t>(ExpMems.size());
252
0
  }
253
0
  uint32_t getTagExportNum() const noexcept {
254
0
    std::shared_lock Lock(Mutex);
255
0
    return static_cast<uint32_t>(ExpTags.size());
256
0
  }
257
0
  uint32_t getGlobalExportNum() const noexcept {
258
0
    std::shared_lock Lock(Mutex);
259
0
    return static_cast<uint32_t>(ExpGlobals.size());
260
0
  }
261
262
  /// Get the exported instances maps.
263
  template <typename CallbackT>
264
0
  auto getFuncExports(CallbackT &&CallBack) const noexcept {
265
0
    std::shared_lock Lock(Mutex);
266
0
    return std::forward<CallbackT>(CallBack)(ExpFuncs);
267
0
  }
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge_ModuleInstanceListFunction::$_0>(WasmEdge_ModuleInstanceListFunction::$_0&&) const
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge_VMGetFunctionList::$_0>(WasmEdge_VMGetFunctionList::$_0&&) const
Unexecuted instantiation: vm.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge::VM::VM::unsafeGetFunctionList() const::$_0>(WasmEdge::VM::VM::unsafeGetFunctionList() const::$_0&&) const
Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
268
  template <typename CallbackT>
269
0
  auto getTableExports(CallbackT &&CallBack) const noexcept {
270
0
    std::shared_lock Lock(Mutex);
271
0
    return std::forward<CallbackT>(CallBack)(ExpTables);
272
0
  }
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTableExports<WasmEdge_ModuleInstanceListTable::$_0>(WasmEdge_ModuleInstanceListTable::$_0&&) const
Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTableExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
273
  template <typename CallbackT>
274
0
  auto getMemoryExports(CallbackT &&CallBack) const noexcept {
275
0
    std::shared_lock Lock(Mutex);
276
0
    return std::forward<CallbackT>(CallBack)(ExpMems);
277
0
  }
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getMemoryExports<WasmEdge_ModuleInstanceListMemory::$_0>(WasmEdge_ModuleInstanceListMemory::$_0&&) const
Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getMemoryExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
278
  template <typename CallbackT>
279
0
  auto getTagExports(CallbackT &&CallBack) const noexcept {
280
0
    std::shared_lock Lock(Mutex);
281
0
    return std::forward<CallbackT>(CallBack)(ExpTags);
282
0
  }
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTagExports<WasmEdge_ModuleInstanceListTag::$_0>(WasmEdge_ModuleInstanceListTag::$_0&&) const
Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTagExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
283
  template <typename CallbackT>
284
0
  auto getGlobalExports(CallbackT &&CallBack) const noexcept {
285
0
    std::shared_lock Lock(Mutex);
286
0
    return std::forward<CallbackT>(CallBack)(ExpGlobals);
287
0
  }
Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getGlobalExports<WasmEdge_ModuleInstanceListGlobal::$_0>(WasmEdge_ModuleInstanceListGlobal::$_0&&) const
Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getGlobalExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
288
289
protected:
290
  friend class Executor::Executor;
291
  friend class ComponentInstance;
292
  friend class Runtime::CallingFrame;
293
294
  /// Create and copy the defined type to this module instance.
295
0
  void addDefinedType(const AST::SubType &SType) {
296
0
    std::unique_lock Lock(Mutex);
297
0
    OwnedTypes.push_back(std::make_unique<AST::SubType>(SType));
298
0
    Types.push_back(OwnedTypes.back().get());
299
0
  }
300
301
  /// Create and add instances to this module instance.
302
0
  template <typename... Args> void addFunc(Args &&...Values) {
303
0
    std::unique_lock Lock(Mutex);
304
0
    unsafeAddInstance(OwnedFuncInsts, FuncInsts, this,
305
0
                      std::forward<Args>(Values)...);
306
0
  }
Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addFunc<unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void> >(unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void>&&)
Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addFunc<unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul> >(unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>&&, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul>&&)
307
0
  template <typename... Args> void addTable(Args &&...Values) {
308
0
    std::unique_lock Lock(Mutex);
309
0
    unsafeAddInstance(OwnedTabInsts, TabInsts, std::forward<Args>(Values)...);
310
0
  }
311
0
  template <typename... Args> void addMemory(Args &&...Values) {
312
0
    std::unique_lock Lock(Mutex);
313
0
    unsafeAddInstance(OwnedMemInsts, MemInsts, std::forward<Args>(Values)...);
314
0
  }
315
0
  template <typename... Args> void addTag(Args &&...Values) {
316
0
    std::unique_lock Lock(Mutex);
317
0
    unsafeAddInstance(OwnedTagInsts, TagInsts, std::forward<Args>(Values)...);
318
0
  }
319
0
  template <typename... Args> void addGlobal(Args &&...Values) {
320
0
    std::unique_lock Lock(Mutex);
321
0
    unsafeAddInstance(OwnedGlobInsts, GlobInsts, std::forward<Args>(Values)...);
322
0
  }
323
0
  template <typename... Args> void addElem(Args &&...Values) {
324
0
    std::unique_lock Lock(Mutex);
325
0
    unsafeAddInstance(OwnedElemInsts, ElemInsts, std::forward<Args>(Values)...);
326
0
  }
327
0
  template <typename... Args> void addData(Args &&...Values) {
328
0
    std::unique_lock Lock(Mutex);
329
0
    unsafeAddInstance(OwnedDataInsts, DataInsts, std::forward<Args>(Values)...);
330
0
  }
331
0
  template <typename... Args> ArrayInstance *newArray(Args &&...Values) {
332
0
    std::unique_lock Lock(Mutex);
333
0
    OwnedArrayInsts.push_back(
334
0
        std::make_unique<ArrayInstance>(this, std::forward<Args>(Values)...));
335
0
    return OwnedArrayInsts.back().get();
336
0
  }
Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, unsigned int 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>&>(unsigned int const&, unsigned int 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>&)
Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, unsigned int 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> >(unsigned int const&, unsigned int 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>&&)
Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, std::__1::vector<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>, std::__1::allocator<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> > > >(unsigned int const&, std::__1::vector<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>, std::__1::allocator<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> > >&&)
337
0
  template <typename... Args> StructInstance *newStruct(Args &&...Values) {
338
0
    std::unique_lock Lock(Mutex);
339
0
    OwnedStructInsts.push_back(
340
0
        std::make_unique<StructInstance>(this, std::forward<Args>(Values)...));
341
0
    return OwnedStructInsts.back().get();
342
0
  }
343
  template <typename... Args>
344
0
  ExceptionInstance *newException(Args &&...Values) {
345
0
    std::unique_lock Lock(Mutex);
346
0
    OwnedExceptionInsts.push_back(
347
0
        std::make_unique<ExceptionInstance>(std::forward<Args>(Values)...));
348
0
    return OwnedExceptionInsts.back().get();
349
0
  }
Unexecuted instantiation: WasmEdge::Runtime::Instance::ExceptionInstance* WasmEdge::Runtime::Instance::ModuleInstance::newException<WasmEdge::Runtime::Instance::TagInstance*&, std::__1::vector<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>, std::__1::allocator<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::Runtime::Instance::TagInstance*&, std::__1::vector<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>, std::__1::allocator<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> > >&&)
Unexecuted instantiation: WasmEdge::Runtime::Instance::ExceptionInstance* WasmEdge::Runtime::Instance::ModuleInstance::newException<WasmEdge::Runtime::Instance::TagInstance*, std::__1::vector<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>, std::__1::allocator<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::Runtime::Instance::TagInstance*&&, std::__1::vector<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>, std::__1::allocator<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> > >&&)
350
351
  /// Import instances into this module instance.
352
0
  void importFunction(FunctionInstance *Func) {
353
0
    std::unique_lock Lock(Mutex);
354
0
    unsafeImportInstance(FuncInsts, Func);
355
0
  }
356
  /// Import a host function (FunctionInstance owned elsewhere). Registers
357
  /// the host function's defined type into this module's Types list so that
358
  /// downstream matchType() walks find it, and re-links the FunctionInstance
359
  /// to point at the new TypeIdx. Use this when a host function is bridged
360
  /// into a different module than the one that originally created it (e.g.,
361
  /// the inline-instance machinery in component instantiation).
362
0
  void importHostFunction(FunctionInstance *Func) {
363
0
    std::unique_lock Lock(Mutex);
364
0
    assuming(Func->isHostFunction());
365
0
    unsafeImportDefinedType(Func->getHostFunc().getDefinedType());
366
0
    Func->linkDefinedType(this, static_cast<uint32_t>(Types.size()) - 1);
367
0
    unsafeImportInstance(FuncInsts, Func);
368
0
  }
369
0
  void importTable(TableInstance *Tab) {
370
0
    std::unique_lock Lock(Mutex);
371
0
    unsafeImportInstance(TabInsts, Tab);
372
0
  }
373
0
  void importMemory(MemoryInstance *Mem) {
374
0
    std::unique_lock Lock(Mutex);
375
0
    unsafeImportInstance(MemInsts, Mem);
376
0
  }
377
0
  void importTag(TagInstance *Tg) {
378
0
    std::unique_lock Lock(Mutex);
379
0
    unsafeImportInstance(TagInsts, Tg);
380
0
  }
381
0
  void importGlobal(GlobalInstance *Glob) {
382
0
    std::unique_lock Lock(Mutex);
383
0
    ImpGlobalNum++;
384
0
    unsafeImportInstance(GlobInsts, Glob);
385
0
  }
386
387
  /// Export instances with name from this module instance.
388
0
  void exportFunction(std::string_view Name, uint32_t Idx) {
389
0
    std::unique_lock Lock(Mutex);
390
0
    ExpFuncs.insert_or_assign(std::string(Name), FuncInsts[Idx]);
391
0
  }
392
0
  void exportTable(std::string_view Name, uint32_t Idx) {
393
0
    std::unique_lock Lock(Mutex);
394
0
    ExpTables.insert_or_assign(std::string(Name), TabInsts[Idx]);
395
0
  }
396
0
  void exportMemory(std::string_view Name, uint32_t Idx) {
397
0
    std::unique_lock Lock(Mutex);
398
0
    ExpMems.insert_or_assign(std::string(Name), MemInsts[Idx]);
399
0
  }
400
0
  void exportGlobal(std::string_view Name, uint32_t Idx) {
401
0
    std::unique_lock Lock(Mutex);
402
0
    ExpGlobals.insert_or_assign(std::string(Name), GlobInsts[Idx]);
403
0
  }
404
0
  void exportTag(std::string_view Name, uint32_t Idx) {
405
0
    std::unique_lock Lock(Mutex);
406
0
    ExpTags.insert_or_assign(std::string(Name), TagInsts[Idx]);
407
0
  }
408
409
  /// Get defined type list.
410
0
  Span<const AST::SubType *const> getTypeList() const noexcept { return Types; }
411
412
  /// Get instance pointer by index.
413
0
  Expect<const AST::SubType *> getType(uint32_t Idx) const noexcept {
414
0
    std::shared_lock Lock(Mutex);
415
0
    if (unlikely(Idx >= Types.size())) {
416
      // Error logging needs to be handled by the caller.
417
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
418
0
    }
419
0
    return unsafeGetType(Idx);
420
0
  }
421
0
  const AST::SubType *unsafeGetType(uint32_t Idx) const noexcept {
422
0
    return Types[Idx];
423
0
  }
424
0
  Expect<FunctionInstance *> getFunc(uint32_t Idx) const noexcept {
425
0
    std::shared_lock Lock(Mutex);
426
0
    if (Idx >= FuncInsts.size()) {
427
0
      // Error logging needs to be handled by the caller.
428
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
429
0
    }
430
0
    return unsafeGetFunction(Idx);
431
0
  }
432
0
  FunctionInstance *unsafeGetFunction(uint32_t Idx) const noexcept {
433
0
    return FuncInsts[Idx];
434
0
  }
435
0
  Expect<TableInstance *> getTable(uint32_t Idx) const noexcept {
436
0
    std::shared_lock Lock(Mutex);
437
0
    if (Idx >= TabInsts.size()) {
438
0
      // Error logging needs to be handled by the caller.
439
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
440
0
    }
441
0
    return unsafeGetTable(Idx);
442
0
  }
443
0
  TableInstance *unsafeGetTable(uint32_t Idx) const noexcept {
444
0
    return TabInsts[Idx];
445
0
  }
446
0
  Expect<MemoryInstance *> getMemory(uint32_t Idx) const noexcept {
447
0
    std::shared_lock Lock(Mutex);
448
0
    if (Idx >= MemInsts.size()) {
449
      // Error logging needs to be handled by the caller.
450
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
451
0
    }
452
0
    return unsafeGetMemory(Idx);
453
0
  }
454
0
  MemoryInstance *unsafeGetMemory(uint32_t Idx) const noexcept {
455
0
    return MemInsts[Idx];
456
0
  }
457
0
  TagInstance *unsafeGetTag(uint32_t Idx) const noexcept {
458
0
    return TagInsts[Idx];
459
0
  }
460
0
  Expect<GlobalInstance *> getGlobal(uint32_t Idx) const noexcept {
461
0
    std::shared_lock Lock(Mutex);
462
0
    if (Idx >= GlobInsts.size()) {
463
      // Error logging needs to be handled by the caller.
464
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
465
0
    }
466
0
    return unsafeGetGlobal(Idx);
467
0
  }
468
0
  GlobalInstance *unsafeGetGlobal(uint32_t Idx) const noexcept {
469
0
    return GlobInsts[Idx];
470
0
  }
471
0
  Expect<ElementInstance *> getElem(uint32_t Idx) const noexcept {
472
0
    std::shared_lock Lock(Mutex);
473
0
    if (Idx >= ElemInsts.size()) {
474
0
      // Error logging needs to be handled by the caller.
475
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
476
0
    }
477
0
    return unsafeGetElem(Idx);
478
0
  }
479
0
  ElementInstance *unsafeGetElem(uint32_t Idx) const noexcept {
480
0
    return ElemInsts[Idx];
481
0
  }
482
0
  Expect<DataInstance *> getData(uint32_t Idx) const noexcept {
483
0
    std::shared_lock Lock(Mutex);
484
0
    if (Idx >= DataInsts.size()) {
485
0
      // Error logging needs to be handled by the caller.
486
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
487
0
    }
488
0
    return unsafeGetData(Idx);
489
0
  }
490
0
  DataInstance *unsafeGetData(uint32_t Idx) const noexcept {
491
0
    return DataInsts[Idx];
492
0
  }
493
494
  /// Get the instances count.
495
0
  uint32_t getFuncNum() const noexcept {
496
0
    std::shared_lock Lock(Mutex);
497
0
    return static_cast<uint32_t>(FuncInsts.size());
498
0
  }
499
0
  uint32_t getTableNum() const noexcept {
500
0
    std::shared_lock Lock(Mutex);
501
0
    return static_cast<uint32_t>(TabInsts.size());
502
0
  }
503
0
  uint32_t getMemoryNum() const noexcept {
504
0
    std::shared_lock Lock(Mutex);
505
0
    return static_cast<uint32_t>(MemInsts.size());
506
0
  }
507
0
  uint32_t getGlobalNum() const noexcept {
508
0
    std::shared_lock Lock(Mutex);
509
0
    return static_cast<uint32_t>(GlobInsts.size());
510
0
  }
511
512
  /// Get imported global instances count.
513
0
  uint32_t getGlobalImportNum() const noexcept { return ImpGlobalNum; }
514
515
  /// Set the start function index and find the function instance.
516
0
  void setStartIdx(uint32_t Idx) noexcept {
517
0
    std::unique_lock Lock(Mutex);
518
0
    StartFunc = FuncInsts[Idx];
519
0
  }
520
521
  /// Get start function address in Store.
522
0
  const FunctionInstance *getStartFunc() const noexcept {
523
0
    std::shared_lock Lock(Mutex);
524
0
    return StartFunc;
525
0
  }
526
527
  /// Set the target imported WASI module during instantiation.
528
0
  void setWASIModule(const ModuleInstance *Mod) noexcept {
529
0
    std::unique_lock Lock(Mutex);
530
0
    WASIModInst = Mod;
531
0
  }
532
533
  /// Get the target imported WASI module during instantiation.
534
0
  const ModuleInstance *getWASIModule() const noexcept {
535
0
    std::shared_lock Lock(Mutex);
536
0
    return WASIModInst;
537
0
  }
538
539
  /// Unsafely import an instance into this module. Only valid before
540
  /// finalization: buildContext() caches the instance vectors' buffers, which a
541
  /// later push_back may reallocate.
542
  template <typename T>
543
  std::enable_if_t<IsEntityV<T>, void>
544
0
  unsafeImportInstance(std::vector<T *> &Vec, T *Ptr) {
545
0
    assuming(!isInstantiateFinalized());
546
0
    Vec.push_back(Ptr);
547
0
  }
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::FunctionInstance*)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::GlobalInstance*> >&, WasmEdge::Runtime::Instance::GlobalInstance*)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::TableInstance>(std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, WasmEdge::Runtime::Instance::TableInstance*)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::MemoryInstance*> >&, WasmEdge::Runtime::Instance::MemoryInstance*)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TagInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::TagInstance>(std::__1::vector<WasmEdge::Runtime::Instance::TagInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TagInstance*> >&, WasmEdge::Runtime::Instance::TagInstance*)
548
549
  /// Unsafely import a defined type from a host function into this module.
550
0
  void unsafeImportDefinedType(const AST::SubType &SType) {
551
0
    Types.push_back(&SType);
552
0
    const_cast<AST::SubType *>(Types.back())
553
0
        ->setTypeIndex(static_cast<uint32_t>(Types.size()) - 1);
554
0
  }
555
556
  /// Unsafely create and add the instance to this module. Only valid before
557
  /// finalization: buildContext() caches the instance vectors' buffers, which a
558
  /// later push_back may reallocate.
559
  template <typename T, typename... Args>
560
  std::enable_if_t<IsInstanceV<T>, void>
561
  unsafeAddInstance(std::vector<std::unique_ptr<T>> &OwnedInstsVec,
562
0
                    std::vector<T *> &InstsVec, Args &&...Values) {
563
0
    assuming(!isInstantiateFinalized());
564
0
    OwnedInstsVec.push_back(std::make_unique<T>(std::forward<Args>(Values)...));
565
0
    InstsVec.push_back(OwnedInstsVec.back().get());
566
0
  }
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::FunctionInstance, WasmEdge::Runtime::Instance::ModuleInstance*, unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::ModuleInstance*&&, unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void>&&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::FunctionInstance, WasmEdge::Runtime::Instance::ModuleInstance*, unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::ModuleInstance*&&, unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>&&, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul>&&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::GlobalInstance, WasmEdge::AST::GlobalType 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>&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::GlobalInstance*> >&, WasmEdge::AST::GlobalType 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>&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::TableInstance, WasmEdge::AST::TableType const&, WasmEdge::RefVariant&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, WasmEdge::AST::TableType const&, WasmEdge::RefVariant&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::MemoryInstance, WasmEdge::AST::MemoryType const&, unsigned long>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::MemoryInstance*> >&, WasmEdge::AST::MemoryType const&, unsigned long&&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::ElementInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::ElementInstance, unsigned long&, WasmEdge::ValType const&, std::__1::vector<WasmEdge::RefVariant, std::__1::allocator<WasmEdge::RefVariant> >&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::ElementInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ElementInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::ElementInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ElementInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::ElementInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::ElementInstance*> >&, unsigned long&, WasmEdge::ValType const&, std::__1::vector<WasmEdge::RefVariant, std::__1::allocator<WasmEdge::RefVariant> >&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::DataInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::DataInstance, unsigned long&, cxx20::span<unsigned char const, 18446744073709551615ul> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::DataInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::DataInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::DataInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::DataInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::DataInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::DataInstance*> >&, unsigned long&, cxx20::span<unsigned char const, 18446744073709551615ul>&&)
Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::TagInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::TagInstance, WasmEdge::AST::TagType const&, WasmEdge::AST::SubType const*&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TagInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TagInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TagInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TagInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TagInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TagInstance*> >&, WasmEdge::AST::TagType const&, WasmEdge::AST::SubType const*&)
567
568
  /// Unsafely add and export the existing instance to this module.
569
  template <typename T, typename... Args>
570
  std::enable_if_t<IsEntityV<T>, void>
571
  unsafeAddHostInstance(std::string_view Name,
572
                        std::vector<std::unique_ptr<T>> &OwnedInstsVec,
573
                        std::vector<T *> &InstsVec,
574
                        std::map<std::string, T *, std::less<>> &InstsMap,
575
0
                        std::unique_ptr<T> &&Inst) {
576
0
    OwnedInstsVec.push_back(std::move(Inst));
577
0
    InstsVec.push_back(OwnedInstsVec.back().get());
578
0
    InstsMap.insert_or_assign(std::string(Name), InstsVec.back());
579
0
  }
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::FunctionInstance*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >&&)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::TableInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TableInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TableInstance*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >&&)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::MemoryInstance*> >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::MemoryInstance*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >&&)
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::GlobalInstance*> >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::GlobalInstance*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >&&)
580
581
  /// Unsafely find and get the exported instance by name.
582
  template <typename T>
583
  std::enable_if_t<IsEntityV<T>, T *>
584
  unsafeFindExports(const std::map<std::string, T *, std::less<>> &Map,
585
0
                    std::string_view ExtName) const noexcept {
586
0
    auto Iter = Map.find(ExtName);
587
0
    if (likely(Iter != Map.cend())) {
588
0
      return Iter->second;
589
0
    }
590
0
    return nullptr;
591
0
  }
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, WasmEdge::Runtime::Instance::FunctionInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::FunctionInstance*> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, WasmEdge::Runtime::Instance::GlobalInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::GlobalInstance*> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, WasmEdge::Runtime::Instance::TableInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<WasmEdge::Runtime::Instance::TableInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TableInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TableInstance*> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, WasmEdge::Runtime::Instance::MemoryInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::MemoryInstance*> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const
Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TagInstance>, WasmEdge::Runtime::Instance::TagInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<WasmEdge::Runtime::Instance::TagInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TagInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TagInstance*> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const
592
593
  /// \name Data for compiled functions.
594
  /// @{
595
#if WASMEDGE_ALLOCATOR_IS_STABLE
596
  std::vector<uint8_t *> MemoryPtrs;
597
#else
598
  std::vector<uint8_t **> MemoryPtrs;
599
#endif
600
  std::vector<const uint64_t *> MemorySizePtrs;
601
  std::vector<const uint64_t *> TableSizePtrs;
602
  std::vector<RefVariant **> TableRefPtrs;
603
  std::vector<ValVariant *> GlobalPtrs;
604
  /// @}
605
606
  struct ModuleContext {
607
#if WASMEDGE_ALLOCATOR_IS_STABLE
608
    uint8_t *const *Memories;
609
#else
610
    uint8_t **const *Memories;
611
#endif
612
    const uint64_t *const *MemorySizes;
613
    RefVariant **const *TableRefs;
614
    const uint64_t *const *TableSizes;
615
    ValVariant *const *Globals;
616
    const void *ModuleInst;
617
    void *const *Tags;
618
  };
619
620
  /// Compiled code reads this struct by field index through the mirrored ModCtx
621
  /// type built in lib/llvm/compiler/context.cpp. Keep both in the same order.
622
  static_assert(sizeof(ModuleContext) == 7 * sizeof(void *));
623
  static_assert(offsetof(ModuleContext, Memories) == 0 * sizeof(void *));
624
  static_assert(offsetof(ModuleContext, MemorySizes) == 1 * sizeof(void *));
625
  static_assert(offsetof(ModuleContext, TableRefs) == 2 * sizeof(void *));
626
  static_assert(offsetof(ModuleContext, TableSizes) == 3 * sizeof(void *));
627
  static_assert(offsetof(ModuleContext, Globals) == 4 * sizeof(void *));
628
  static_assert(offsetof(ModuleContext, ModuleInst) == 5 * sizeof(void *));
629
  static_assert(offsetof(ModuleContext, Tags) == 6 * sizeof(void *));
630
631
  ModuleContext ModCtx{};
632
633
  /// Snapshot the instance vectors' buffers for compiled code. Call once, after
634
  /// the last instance is added and before the start function runs; adding an
635
  /// instance afterwards may reallocate and leave the context dangling.
636
0
  void buildContext() noexcept {
637
0
    ModCtx.Memories = MemoryPtrs.data();
638
0
    ModCtx.MemorySizes = MemorySizePtrs.data();
639
0
    ModCtx.TableRefs = TableRefPtrs.data();
640
0
    ModCtx.TableSizes = TableSizePtrs.data();
641
0
    ModCtx.Globals = GlobalPtrs.data();
642
0
    ModCtx.ModuleInst = this;
643
0
    ModCtx.Tags = reinterpret_cast<void *const *>(TagInsts.data());
644
0
  }
645
646
  friend class Runtime::StoreManager;
647
  using LinkedStoreKey = std::pair<StoreManager *, std::string>;
648
  using BeforeModuleDestroyCallback = void(const LinkedStoreKey &Key,
649
                                           const ModuleInstance *Mod);
650
  void linkStore(StoreManager *Store, std::string_view Name,
651
0
                 BeforeModuleDestroyCallback Callback) {
652
    // Link to the store during registration.
653
0
    std::unique_lock Lock(Mutex);
654
0
    LinkedStore.insert_or_assign(LinkedStoreKey{Store, std::string(Name)},
655
0
                                 Callback);
656
0
  }
657
658
0
  void unlinkStore(StoreManager *Store, std::string_view Name) {
659
    // Unlink a specific (Store, Name) entry.
660
0
    std::unique_lock Lock(Mutex);
661
0
    LinkedStore.erase(LinkedStoreKey{Store, std::string(Name)});
662
0
  }
663
664
0
  void unlinkAllStores() noexcept {
665
0
    std::map<LinkedStoreKey, std::function<BeforeModuleDestroyCallback>>
666
0
        Snapshot;
667
0
    {
668
0
      std::unique_lock Lock(Mutex);
669
0
      Snapshot.swap(LinkedStore);
670
0
    }
671
0
    for (auto &&[Key, Callback] : Snapshot) {
672
0
      assuming(Callback);
673
0
      Callback(Key, this);
674
0
    }
675
0
  }
676
677
0
  void addDependency(ModuleInstance &Provider) {
678
0
    std::unique_lock Lock(Mutex);
679
0
    if (Providers.insert(&Provider).second) {
680
0
      Provider.Life.addDependent();
681
0
    }
682
0
  }
683
684
0
  void takeProviders(std::unordered_set<ModuleInstance *> &Snapshot) noexcept {
685
0
    std::unique_lock Lock(Mutex);
686
0
    Snapshot.swap(Providers);
687
0
  }
688
689
  static void drainPins(std::unordered_set<ModuleInstance *> &Provs,
690
0
                        std::vector<ModuleInstance *> &ToDelete) noexcept {
691
0
    for (auto *Provider : Provs) {
692
0
      if (Provider->Life.releaseDependent()) {
693
0
        ToDelete.push_back(Provider);
694
0
      }
695
0
    }
696
0
    Provs.clear();
697
0
  }
698
699
0
  void releaseProviders() noexcept {
700
0
    std::unordered_set<ModuleInstance *> ProvidersToRelease;
701
0
    takeProviders(ProvidersToRelease);
702
0
    if (ProvidersToRelease.empty()) {
703
0
      return;
704
0
    }
705
0
    std::vector<ModuleInstance *> ToDelete;
706
0
    drainPins(ProvidersToRelease, ToDelete);
707
0
    while (!ToDelete.empty()) {
708
0
      ModuleInstance *Next = ToDelete.back();
709
0
      ToDelete.pop_back();
710
0
      std::unordered_set<ModuleInstance *> NextProviders;
711
0
      Next->takeProviders(NextProviders);
712
0
      delete Next;
713
0
      drainPins(NextProviders, ToDelete);
714
0
    }
715
0
  }
716
717
  /// Mutex.
718
  mutable std::shared_mutex Mutex;
719
720
  /// Module name.
721
  const std::string ModName;
722
723
  /// Defined types.
724
  std::vector<const AST::SubType *> Types;
725
  std::vector<std::unique_ptr<const AST::SubType>> OwnedTypes;
726
727
  /// Owned instances in this module.
728
  std::vector<std::unique_ptr<FunctionInstance>> OwnedFuncInsts;
729
  std::vector<std::unique_ptr<TableInstance>> OwnedTabInsts;
730
  std::vector<std::unique_ptr<MemoryInstance>> OwnedMemInsts;
731
  std::vector<std::unique_ptr<TagInstance>> OwnedTagInsts;
732
  std::vector<std::unique_ptr<GlobalInstance>> OwnedGlobInsts;
733
  std::vector<std::unique_ptr<ElementInstance>> OwnedElemInsts;
734
  std::vector<std::unique_ptr<DataInstance>> OwnedDataInsts;
735
  std::vector<std::unique_ptr<ArrayInstance>> OwnedArrayInsts;
736
  std::vector<std::unique_ptr<StructInstance>> OwnedStructInsts;
737
  std::vector<std::unique_ptr<ExceptionInstance>> OwnedExceptionInsts;
738
739
  /// Imported and added instances in this module.
740
  std::vector<FunctionInstance *> FuncInsts;
741
  std::vector<TableInstance *> TabInsts;
742
  std::vector<MemoryInstance *> MemInsts;
743
  std::vector<TagInstance *> TagInsts;
744
  std::vector<GlobalInstance *> GlobInsts;
745
  std::vector<ElementInstance *> ElemInsts;
746
  std::vector<DataInstance *> DataInsts;
747
748
  /// Imported instance counts.
749
  uint32_t ImpGlobalNum = 0;
750
751
  /// Exported name maps.
752
  std::map<std::string, FunctionInstance *, std::less<>> ExpFuncs;
753
  std::map<std::string, TableInstance *, std::less<>> ExpTables;
754
  std::map<std::string, MemoryInstance *, std::less<>> ExpMems;
755
  std::map<std::string, TagInstance *, std::less<>> ExpTags;
756
  std::map<std::string, GlobalInstance *, std::less<>> ExpGlobals;
757
758
  /// Start function instance.
759
  const FunctionInstance *StartFunc = nullptr;
760
761
  /// Imported WASI module instance during instantiation.
762
  const ModuleInstance *WASIModInst = nullptr;
763
764
  /// Linked store. Key is (StoreManager*, RegisteredName) to support
765
  /// the same module registered under multiple alias names.
766
  std::map<LinkedStoreKey, std::function<BeforeModuleDestroyCallback>>
767
      LinkedStore;
768
769
  /// External data and its finalizer function pointer.
770
  void *HostData;
771
  std::function<void(void *)> HostDataFinalizer;
772
773
  /// Whether this module instance has been finalized (immutable). Once set,
774
  /// the indexed instances will not be mutated anymore.
775
  mutable std::atomic<bool> InstantiateFinalized{false};
776
777
  /// Intrusive lifetime: owner flag plus importer count, packed into one
778
  /// atomic.
779
  RefLifetime Life;
780
  /// Provider instances this module imported from; each holds one dependent
781
  /// pin, released when this module is destroyed.
782
  std::unordered_set<ModuleInstance *> Providers;
783
};
784
785
} // namespace Instance
786
} // namespace Runtime
787
} // namespace WasmEdge