Coverage Report

Created: 2026-06-30 06:10

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