Coverage Report

Created: 2026-11-09 06:49

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, this,
310
0
                      std::forward<Args>(Values)...);
311
0
  }
312
0
  template <typename... Args> void addMemory(Args &&...Values) {
313
0
    std::unique_lock Lock(Mutex);
314
0
    unsafeAddInstance(OwnedMemInsts, MemInsts, std::forward<Args>(Values)...);
315
0
  }
316
0
  template <typename... Args> void addTag(Args &&...Values) {
317
0
    std::unique_lock Lock(Mutex);
318
0
    unsafeAddInstance(OwnedTagInsts, TagInsts, this,
319
0
                      std::forward<Args>(Values)...);
320
0
  }
321
0
  template <typename... Args> void addGlobal(Args &&...Values) {
322
0
    std::unique_lock Lock(Mutex);
323
0
    unsafeAddInstance(OwnedGlobInsts, GlobInsts, this,
324
0
                      std::forward<Args>(Values)...);
325
0
  }
326
0
  template <typename... Args> void addElem(Args &&...Values) {
327
0
    std::unique_lock Lock(Mutex);
328
0
    unsafeAddInstance(OwnedElemInsts, ElemInsts, std::forward<Args>(Values)...);
329
0
  }
330
0
  template <typename... Args> void addData(Args &&...Values) {
331
0
    std::unique_lock Lock(Mutex);
332
0
    unsafeAddInstance(OwnedDataInsts, DataInsts, std::forward<Args>(Values)...);
333
0
  }
334
0
  template <typename... Args> ArrayInstance *newArray(Args &&...Values) {
335
0
    std::unique_lock Lock(Mutex);
336
0
    OwnedArrayInsts.push_back(
337
0
        std::make_unique<ArrayInstance>(this, std::forward<Args>(Values)...));
338
0
    return OwnedArrayInsts.back().get();
339
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> > >&&)
340
0
  template <typename... Args> StructInstance *newStruct(Args &&...Values) {
341
0
    std::unique_lock Lock(Mutex);
342
0
    OwnedStructInsts.push_back(
343
0
        std::make_unique<StructInstance>(this, std::forward<Args>(Values)...));
344
0
    return OwnedStructInsts.back().get();
345
0
  }
346
  template <typename... Args>
347
0
  ExceptionInstance *newException(Args &&...Values) {
348
0
    std::unique_lock Lock(Mutex);
349
0
    OwnedExceptionInsts.push_back(
350
0
        std::make_unique<ExceptionInstance>(std::forward<Args>(Values)...));
351
0
    return OwnedExceptionInsts.back().get();
352
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> > >&&)
353
354
  /// Import instances into this module instance.
355
0
  void importFunction(FunctionInstance *Func) {
356
0
    std::unique_lock Lock(Mutex);
357
0
    unsafeImportInstance(FuncInsts, Func);
358
0
  }
359
  /// Import a host function (FunctionInstance owned elsewhere). Registers
360
  /// the host function's defined type into this module's Types list so that
361
  /// downstream matchType() walks find it, and re-links the FunctionInstance
362
  /// to point at the new TypeIdx. Use this when a host function is bridged
363
  /// into a different module than the one that originally created it (e.g.,
364
  /// the inline-instance machinery in component instantiation).
365
0
  void importHostFunction(FunctionInstance *Func) {
366
0
    std::unique_lock Lock(Mutex);
367
0
    assuming(Func->isHostFunction());
368
0
    unsafeImportDefinedType(Func->getHostFunc().getDefinedType());
369
0
    Func->linkDefinedType(this, static_cast<uint32_t>(Types.size()) - 1);
370
0
    unsafeImportInstance(FuncInsts, Func);
371
0
  }
372
0
  void importTable(TableInstance *Tab) {
373
0
    std::unique_lock Lock(Mutex);
374
0
    unsafeImportInstance(TabInsts, Tab);
375
0
  }
376
0
  void importMemory(MemoryInstance *Mem) {
377
0
    std::unique_lock Lock(Mutex);
378
0
    unsafeImportInstance(MemInsts, Mem);
379
0
  }
380
0
  void importTag(TagInstance *Tg) {
381
0
    std::unique_lock Lock(Mutex);
382
0
    unsafeImportInstance(TagInsts, Tg);
383
0
  }
384
0
  void importGlobal(GlobalInstance *Glob) {
385
0
    std::unique_lock Lock(Mutex);
386
0
    ImpGlobalNum++;
387
0
    unsafeImportInstance(GlobInsts, Glob);
388
0
  }
389
390
  /// Export instances with name from this module instance.
391
0
  void exportFunction(std::string_view Name, uint32_t Idx) {
392
0
    std::unique_lock Lock(Mutex);
393
0
    ExpFuncs.insert_or_assign(std::string(Name), FuncInsts[Idx]);
394
0
  }
395
0
  void exportTable(std::string_view Name, uint32_t Idx) {
396
0
    std::unique_lock Lock(Mutex);
397
0
    ExpTables.insert_or_assign(std::string(Name), TabInsts[Idx]);
398
0
  }
399
0
  void exportMemory(std::string_view Name, uint32_t Idx) {
400
0
    std::unique_lock Lock(Mutex);
401
0
    ExpMems.insert_or_assign(std::string(Name), MemInsts[Idx]);
402
0
  }
403
0
  void exportGlobal(std::string_view Name, uint32_t Idx) {
404
0
    std::unique_lock Lock(Mutex);
405
0
    ExpGlobals.insert_or_assign(std::string(Name), GlobInsts[Idx]);
406
0
  }
407
0
  void exportTag(std::string_view Name, uint32_t Idx) {
408
0
    std::unique_lock Lock(Mutex);
409
0
    ExpTags.insert_or_assign(std::string(Name), TagInsts[Idx]);
410
0
  }
411
412
  /// Get defined type list.
413
0
  Span<const AST::SubType *const> getTypeList() const noexcept { return Types; }
414
415
  /// Get instance pointer by index.
416
0
  Expect<const AST::SubType *> getType(uint32_t Idx) const noexcept {
417
0
    std::shared_lock Lock(Mutex);
418
0
    if (unlikely(Idx >= Types.size())) {
419
      // Error logging needs to be handled by the caller.
420
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
421
0
    }
422
0
    return unsafeGetType(Idx);
423
0
  }
424
0
  const AST::SubType *unsafeGetType(uint32_t Idx) const noexcept {
425
0
    return Types[Idx];
426
0
  }
427
0
  Expect<FunctionInstance *> getFunc(uint32_t Idx) const noexcept {
428
0
    std::shared_lock Lock(Mutex);
429
0
    if (Idx >= FuncInsts.size()) {
430
0
      // Error logging needs to be handled by the caller.
431
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
432
0
    }
433
0
    return unsafeGetFunction(Idx);
434
0
  }
435
0
  FunctionInstance *unsafeGetFunction(uint32_t Idx) const noexcept {
436
0
    return FuncInsts[Idx];
437
0
  }
438
0
  Expect<TableInstance *> getTable(uint32_t Idx) const noexcept {
439
0
    std::shared_lock Lock(Mutex);
440
0
    if (Idx >= TabInsts.size()) {
441
0
      // Error logging needs to be handled by the caller.
442
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
443
0
    }
444
0
    return unsafeGetTable(Idx);
445
0
  }
446
0
  TableInstance *unsafeGetTable(uint32_t Idx) const noexcept {
447
0
    return TabInsts[Idx];
448
0
  }
449
0
  Expect<MemoryInstance *> getMemory(uint32_t Idx) const noexcept {
450
0
    std::shared_lock Lock(Mutex);
451
0
    if (Idx >= MemInsts.size()) {
452
      // Error logging needs to be handled by the caller.
453
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
454
0
    }
455
0
    return unsafeGetMemory(Idx);
456
0
  }
457
0
  MemoryInstance *unsafeGetMemory(uint32_t Idx) const noexcept {
458
0
    return MemInsts[Idx];
459
0
  }
460
0
  TagInstance *unsafeGetTag(uint32_t Idx) const noexcept {
461
0
    return TagInsts[Idx];
462
0
  }
463
0
  Expect<GlobalInstance *> getGlobal(uint32_t Idx) const noexcept {
464
0
    std::shared_lock Lock(Mutex);
465
0
    if (Idx >= GlobInsts.size()) {
466
      // Error logging needs to be handled by the caller.
467
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
468
0
    }
469
0
    return unsafeGetGlobal(Idx);
470
0
  }
471
0
  GlobalInstance *unsafeGetGlobal(uint32_t Idx) const noexcept {
472
0
    return GlobInsts[Idx];
473
0
  }
474
0
  Expect<ElementInstance *> getElem(uint32_t Idx) const noexcept {
475
0
    std::shared_lock Lock(Mutex);
476
0
    if (Idx >= ElemInsts.size()) {
477
0
      // Error logging needs to be handled by the caller.
478
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
479
0
    }
480
0
    return unsafeGetElem(Idx);
481
0
  }
482
0
  ElementInstance *unsafeGetElem(uint32_t Idx) const noexcept {
483
0
    return ElemInsts[Idx];
484
0
  }
485
0
  Expect<DataInstance *> getData(uint32_t Idx) const noexcept {
486
0
    std::shared_lock Lock(Mutex);
487
0
    if (Idx >= DataInsts.size()) {
488
0
      // Error logging needs to be handled by the caller.
489
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
490
0
    }
491
0
    return unsafeGetData(Idx);
492
0
  }
493
0
  DataInstance *unsafeGetData(uint32_t Idx) const noexcept {
494
0
    return DataInsts[Idx];
495
0
  }
496
497
  /// Get the instances count.
498
0
  uint32_t getFuncNum() const noexcept {
499
0
    std::shared_lock Lock(Mutex);
500
0
    return static_cast<uint32_t>(FuncInsts.size());
501
0
  }
502
0
  uint32_t getTableNum() const noexcept {
503
0
    std::shared_lock Lock(Mutex);
504
0
    return static_cast<uint32_t>(TabInsts.size());
505
0
  }
506
0
  uint32_t getMemoryNum() const noexcept {
507
0
    std::shared_lock Lock(Mutex);
508
0
    return static_cast<uint32_t>(MemInsts.size());
509
0
  }
510
0
  uint32_t getGlobalNum() const noexcept {
511
0
    std::shared_lock Lock(Mutex);
512
0
    return static_cast<uint32_t>(GlobInsts.size());
513
0
  }
514
515
  /// Get imported global instances count.
516
0
  uint32_t getGlobalImportNum() const noexcept { return ImpGlobalNum; }
517
518
  /// Set the start function index and find the function instance.
519
0
  void setStartIdx(uint32_t Idx) noexcept {
520
0
    std::unique_lock Lock(Mutex);
521
0
    StartFunc = FuncInsts[Idx];
522
0
  }
523
524
  /// Get start function address in Store.
525
0
  const FunctionInstance *getStartFunc() const noexcept {
526
0
    std::shared_lock Lock(Mutex);
527
0
    return StartFunc;
528
0
  }
529
530
  /// Set the target imported WASI module during instantiation.
531
0
  void setWASIModule(const ModuleInstance *Mod) noexcept {
532
0
    std::unique_lock Lock(Mutex);
533
0
    WASIModInst = Mod;
534
0
  }
535
536
  /// Get the target imported WASI module during instantiation.
537
0
  const ModuleInstance *getWASIModule() const noexcept {
538
0
    std::shared_lock Lock(Mutex);
539
0
    return WASIModInst;
540
0
  }
541
542
  /// Unsafely import an instance into this module. Only valid before
543
  /// finalization: buildContext() caches the instance vectors' buffers, which a
544
  /// later push_back may reallocate.
545
  template <typename T>
546
  std::enable_if_t<IsEntityV<T>, void>
547
0
  unsafeImportInstance(std::vector<T *> &Vec, T *Ptr) {
548
0
    assuming(!isInstantiateFinalized());
549
0
    Vec.push_back(Ptr);
550
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*)
551
552
  /// Unsafely import a defined type from a host function into this module.
553
0
  void unsafeImportDefinedType(const AST::SubType &SType) {
554
0
    Types.push_back(&SType);
555
0
    const_cast<AST::SubType *>(Types.back())
556
0
        ->setTypeIndex(static_cast<uint32_t>(Types.size()) - 1);
557
0
  }
558
559
  /// Unsafely create and add the instance to this module. Only valid before
560
  /// finalization: buildContext() caches the instance vectors' buffers, which a
561
  /// later push_back may reallocate.
562
  template <typename T, typename... Args>
563
  std::enable_if_t<IsInstanceV<T>, void>
564
  unsafeAddInstance(std::vector<std::unique_ptr<T>> &OwnedInstsVec,
565
0
                    std::vector<T *> &InstsVec, Args &&...Values) {
566
0
    assuming(!isInstantiateFinalized());
567
0
    OwnedInstsVec.push_back(std::make_unique<T>(std::forward<Args>(Values)...));
568
0
    InstsVec.push_back(OwnedInstsVec.back().get());
569
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::Runtime::Instance::ModuleInstance*, 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::Runtime::Instance::ModuleInstance*&&, 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::Runtime::Instance::ModuleInstance*, 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::Runtime::Instance::ModuleInstance*&&, 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::Runtime::Instance::ModuleInstance*, 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::Runtime::Instance::ModuleInstance*&&, WasmEdge::AST::TagType const&, WasmEdge::AST::SubType const*&)
570
571
  /// Unsafely add and export the existing instance to this module.
572
  template <typename T, typename... Args>
573
  std::enable_if_t<IsEntityV<T>, void>
574
  unsafeAddHostInstance(std::string_view Name,
575
                        std::vector<std::unique_ptr<T>> &OwnedInstsVec,
576
                        std::vector<T *> &InstsVec,
577
                        std::map<std::string, T *, std::less<>> &InstsMap,
578
0
                        std::unique_ptr<T> &&Inst) {
579
0
    OwnedInstsVec.push_back(std::move(Inst));
580
0
    InstsVec.push_back(OwnedInstsVec.back().get());
581
0
    InstsMap.insert_or_assign(std::string(Name), InstsVec.back());
582
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> >&&)
583
584
  /// Unsafely find and get the exported instance by name.
585
  template <typename T>
586
  std::enable_if_t<IsEntityV<T>, T *>
587
  unsafeFindExports(const std::map<std::string, T *, std::less<>> &Map,
588
0
                    std::string_view ExtName) const noexcept {
589
0
    auto Iter = Map.find(ExtName);
590
0
    if (likely(Iter != Map.cend())) {
591
0
      return Iter->second;
592
0
    }
593
0
    return nullptr;
594
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
595
596
  /// \name Data for compiled functions.
597
  /// @{
598
#if WASMEDGE_ALLOCATOR_IS_STABLE
599
  std::vector<uint8_t *> MemoryPtrs;
600
#else
601
  std::vector<uint8_t **> MemoryPtrs;
602
#endif
603
  std::vector<const uint64_t *> MemorySizePtrs;
604
  std::vector<const uint64_t *> TableSizePtrs;
605
  std::vector<RefVariant **> TableRefPtrs;
606
  std::vector<ValVariant *> GlobalPtrs;
607
  /// @}
608
609
  struct ModuleContext {
610
#if WASMEDGE_ALLOCATOR_IS_STABLE
611
    uint8_t *const *Memories;
612
#else
613
    uint8_t **const *Memories;
614
#endif
615
    const uint64_t *const *MemorySizes;
616
    RefVariant **const *TableRefs;
617
    const uint64_t *const *TableSizes;
618
    ValVariant *const *Globals;
619
    const void *ModuleInst;
620
    void *const *Tags;
621
  };
622
623
  /// Compiled code reads this struct by field index through the mirrored ModCtx
624
  /// type built in lib/llvm/compiler/context.cpp. Keep both in the same order.
625
  static_assert(sizeof(ModuleContext) == 7 * sizeof(void *));
626
  static_assert(offsetof(ModuleContext, Memories) == 0 * sizeof(void *));
627
  static_assert(offsetof(ModuleContext, MemorySizes) == 1 * sizeof(void *));
628
  static_assert(offsetof(ModuleContext, TableRefs) == 2 * sizeof(void *));
629
  static_assert(offsetof(ModuleContext, TableSizes) == 3 * sizeof(void *));
630
  static_assert(offsetof(ModuleContext, Globals) == 4 * sizeof(void *));
631
  static_assert(offsetof(ModuleContext, ModuleInst) == 5 * sizeof(void *));
632
  static_assert(offsetof(ModuleContext, Tags) == 6 * sizeof(void *));
633
634
  ModuleContext ModCtx{};
635
636
  /// Snapshot the instance vectors' buffers for compiled code. Call once, after
637
  /// the last instance is added and before the start function runs; adding an
638
  /// instance afterwards may reallocate and leave the context dangling.
639
0
  void buildContext() noexcept {
640
0
    ModCtx.Memories = MemoryPtrs.data();
641
0
    ModCtx.MemorySizes = MemorySizePtrs.data();
642
0
    ModCtx.TableRefs = TableRefPtrs.data();
643
0
    ModCtx.TableSizes = TableSizePtrs.data();
644
0
    ModCtx.Globals = GlobalPtrs.data();
645
0
    ModCtx.ModuleInst = this;
646
0
    ModCtx.Tags = reinterpret_cast<void *const *>(TagInsts.data());
647
0
  }
648
649
  friend class Runtime::StoreManager;
650
  using LinkedStoreKey = std::pair<StoreManager *, std::string>;
651
  using BeforeModuleDestroyCallback = void(const LinkedStoreKey &Key,
652
                                           const ModuleInstance *Mod);
653
  void linkStore(StoreManager *Store, std::string_view Name,
654
0
                 BeforeModuleDestroyCallback Callback) {
655
    // Link to the store during registration.
656
0
    std::unique_lock Lock(Mutex);
657
0
    LinkedStore.insert_or_assign(LinkedStoreKey{Store, std::string(Name)},
658
0
                                 Callback);
659
0
  }
660
661
0
  void unlinkStore(StoreManager *Store, std::string_view Name) {
662
    // Unlink a specific (Store, Name) entry.
663
0
    std::unique_lock Lock(Mutex);
664
0
    LinkedStore.erase(LinkedStoreKey{Store, std::string(Name)});
665
0
  }
666
667
0
  void unlinkAllStores() noexcept {
668
0
    std::map<LinkedStoreKey, std::function<BeforeModuleDestroyCallback>>
669
0
        Snapshot;
670
0
    {
671
0
      std::unique_lock Lock(Mutex);
672
0
      Snapshot.swap(LinkedStore);
673
0
    }
674
0
    for (auto &&[Key, Callback] : Snapshot) {
675
0
      assuming(Callback);
676
0
      Callback(Key, this);
677
0
    }
678
0
  }
679
680
0
  void addDependency(ModuleInstance &Provider) {
681
0
    std::unique_lock Lock(Mutex);
682
0
    if (Providers.insert(&Provider).second) {
683
0
      Provider.Life.addDependent();
684
0
    }
685
0
  }
686
687
0
  void takeProviders(std::unordered_set<ModuleInstance *> &Snapshot) noexcept {
688
0
    std::unique_lock Lock(Mutex);
689
0
    Snapshot.swap(Providers);
690
0
  }
691
692
  static void drainPins(std::unordered_set<ModuleInstance *> &Provs,
693
0
                        std::vector<ModuleInstance *> &ToDelete) noexcept {
694
0
    for (auto *Provider : Provs) {
695
0
      if (Provider->Life.releaseDependent()) {
696
0
        ToDelete.push_back(Provider);
697
0
      }
698
0
    }
699
0
    Provs.clear();
700
0
  }
701
702
0
  void releaseProviders() noexcept {
703
0
    std::unordered_set<ModuleInstance *> ProvidersToRelease;
704
0
    takeProviders(ProvidersToRelease);
705
0
    if (ProvidersToRelease.empty()) {
706
0
      return;
707
0
    }
708
0
    std::vector<ModuleInstance *> ToDelete;
709
0
    drainPins(ProvidersToRelease, ToDelete);
710
0
    while (!ToDelete.empty()) {
711
0
      ModuleInstance *Next = ToDelete.back();
712
0
      ToDelete.pop_back();
713
0
      std::unordered_set<ModuleInstance *> NextProviders;
714
0
      Next->takeProviders(NextProviders);
715
0
      delete Next;
716
0
      drainPins(NextProviders, ToDelete);
717
0
    }
718
0
  }
719
720
  /// Mutex.
721
  mutable std::shared_mutex Mutex;
722
723
  /// Module name.
724
  const std::string ModName;
725
726
  /// Defined types.
727
  std::vector<const AST::SubType *> Types;
728
  std::vector<std::unique_ptr<const AST::SubType>> OwnedTypes;
729
730
  /// Owned instances in this module.
731
  std::vector<std::unique_ptr<FunctionInstance>> OwnedFuncInsts;
732
  std::vector<std::unique_ptr<TableInstance>> OwnedTabInsts;
733
  std::vector<std::unique_ptr<MemoryInstance>> OwnedMemInsts;
734
  std::vector<std::unique_ptr<TagInstance>> OwnedTagInsts;
735
  std::vector<std::unique_ptr<GlobalInstance>> OwnedGlobInsts;
736
  std::vector<std::unique_ptr<ElementInstance>> OwnedElemInsts;
737
  std::vector<std::unique_ptr<DataInstance>> OwnedDataInsts;
738
  std::vector<std::unique_ptr<ArrayInstance>> OwnedArrayInsts;
739
  std::vector<std::unique_ptr<StructInstance>> OwnedStructInsts;
740
  std::vector<std::unique_ptr<ExceptionInstance>> OwnedExceptionInsts;
741
742
  /// Imported and added instances in this module.
743
  std::vector<FunctionInstance *> FuncInsts;
744
  std::vector<TableInstance *> TabInsts;
745
  std::vector<MemoryInstance *> MemInsts;
746
  std::vector<TagInstance *> TagInsts;
747
  std::vector<GlobalInstance *> GlobInsts;
748
  std::vector<ElementInstance *> ElemInsts;
749
  std::vector<DataInstance *> DataInsts;
750
751
  /// Imported instance counts.
752
  uint32_t ImpGlobalNum = 0;
753
754
  /// Exported name maps.
755
  std::map<std::string, FunctionInstance *, std::less<>> ExpFuncs;
756
  std::map<std::string, TableInstance *, std::less<>> ExpTables;
757
  std::map<std::string, MemoryInstance *, std::less<>> ExpMems;
758
  std::map<std::string, TagInstance *, std::less<>> ExpTags;
759
  std::map<std::string, GlobalInstance *, std::less<>> ExpGlobals;
760
761
  /// Start function instance.
762
  const FunctionInstance *StartFunc = nullptr;
763
764
  /// Imported WASI module instance during instantiation.
765
  const ModuleInstance *WASIModInst = nullptr;
766
767
  /// Linked store. Key is (StoreManager*, RegisteredName) to support
768
  /// the same module registered under multiple alias names.
769
  std::map<LinkedStoreKey, std::function<BeforeModuleDestroyCallback>>
770
      LinkedStore;
771
772
  /// External data and its finalizer function pointer.
773
  void *HostData;
774
  std::function<void(void *)> HostDataFinalizer;
775
776
  /// Whether this module instance has been finalized (immutable). Once set,
777
  /// the indexed instances will not be mutated anymore.
778
  mutable std::atomic<bool> InstantiateFinalized{false};
779
780
  /// Intrusive lifetime: owner flag plus importer count, packed into one
781
  /// atomic.
782
  RefLifetime Life;
783
  /// Provider instances this module imported from; each holds one dependent
784
  /// pin, released when this module is destroyed.
785
  std::unordered_set<ModuleInstance *> Providers;
786
};
787
788
} // namespace Instance
789
} // namespace Runtime
790
} // namespace WasmEdge