/src/WasmEdge/lib/executor/instantiate/import.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #include "executor/executor.h" |
5 | | |
6 | | #include "common/errinfo.h" |
7 | | #include "common/spdlog.h" |
8 | | |
9 | | #include <cstdint> |
10 | | #include <string_view> |
11 | | #include <utility> |
12 | | |
13 | | using namespace std::literals; |
14 | | |
15 | | namespace WasmEdge { |
16 | | namespace Executor { |
17 | | |
18 | | namespace { |
19 | | template <typename... Args> |
20 | | auto logMatchError(std::string_view ModName, std::string_view ExtName, |
21 | 0 | ExternalType ExtType, Args &&...Values) { |
22 | 0 | spdlog::error(ErrCode::Value::IncompatibleImportType); |
23 | 0 | spdlog::error(ErrInfo::InfoMismatch(std::forward<Args>(Values)...)); |
24 | 0 | spdlog::error(ErrInfo::InfoLinking(ModName, ExtName, ExtType)); |
25 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Import)); |
26 | 0 | return Unexpect(ErrCode::Value::IncompatibleImportType); |
27 | 0 | } Unexecuted instantiation: import.cpp:auto WasmEdge::Executor::(anonymous namespace)::logMatchError<WasmEdge::ExternalType const&, WasmEdge::ExternalType>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, WasmEdge::ExternalType, WasmEdge::ExternalType const&, WasmEdge::ExternalType&&) Unexecuted instantiation: import.cpp:auto WasmEdge::Executor::(anonymous namespace)::logMatchError<std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, WasmEdge::ExternalType, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&, std::__1::vector<WasmEdge::ValType, std::__1::allocator<WasmEdge::ValType> > const&) Unexecuted instantiation: import.cpp:auto WasmEdge::Executor::(anonymous namespace)::logMatchError<WasmEdge::ValType const&, bool, unsigned int, unsigned int, WasmEdge::ValType const&, bool, unsigned int, unsigned int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, WasmEdge::ExternalType, WasmEdge::ValType const&, bool&&, unsigned int&&, unsigned int&&, WasmEdge::ValType const&, bool&&, unsigned int&&, unsigned int&&) Unexecuted instantiation: import.cpp:auto WasmEdge::Executor::(anonymous namespace)::logMatchError<bool, unsigned int, unsigned int, bool, unsigned int, unsigned int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, WasmEdge::ExternalType, bool&&, unsigned int&&, unsigned int&&, bool&&, unsigned int&&, unsigned int&&) Unexecuted instantiation: import.cpp:auto WasmEdge::Executor::(anonymous namespace)::logMatchError<WasmEdge::ValType const&, WasmEdge::ValMut, WasmEdge::ValType const&, WasmEdge::ValMut>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, WasmEdge::ExternalType, WasmEdge::ValType const&, WasmEdge::ValMut&&, WasmEdge::ValType const&, WasmEdge::ValMut&&) |
28 | | |
29 | | auto logUnknownError(std::string_view ModName, std::string_view ExtName, |
30 | 0 | ExternalType ExtType) { |
31 | 0 | spdlog::error(ErrCode::Value::UnknownImport); |
32 | 0 | spdlog::error(ErrInfo::InfoLinking(ModName, ExtName, ExtType)); |
33 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Desc_Import)); |
34 | 0 | return Unexpect(ErrCode::Value::UnknownImport); |
35 | 0 | } |
36 | | |
37 | 0 | bool matchLimit(const AST::Limit &Exp, const AST::Limit &Got) { |
38 | 0 | if (Exp.isShared() != Got.isShared()) { |
39 | 0 | return false; |
40 | 0 | } |
41 | 0 | if ((Got.getMin() < Exp.getMin()) || (Exp.hasMax() && !Got.hasMax())) { |
42 | 0 | return false; |
43 | 0 | } |
44 | 0 | if (Exp.hasMax() && Got.hasMax() && Got.getMax() > Exp.getMax()) { |
45 | 0 | return false; |
46 | 0 | } |
47 | 0 | return true; |
48 | 0 | } |
49 | | |
50 | | Expect<void> |
51 | | checkImportMatched(std::string_view ModName, std::string_view ExtName, |
52 | | const ExternalType ExtType, |
53 | 0 | const Runtime::Instance::ModuleInstance &ModInst) { |
54 | 0 | switch (ExtType) { |
55 | 0 | case ExternalType::Function: |
56 | 0 | if (auto Res = ModInst.findFuncExports(ExtName); likely(Res != nullptr)) { |
57 | 0 | return {}; |
58 | 0 | } |
59 | 0 | break; |
60 | 0 | case ExternalType::Table: |
61 | 0 | if (auto Res = ModInst.findTableExports(ExtName); likely(Res != nullptr)) { |
62 | 0 | return {}; |
63 | 0 | } |
64 | 0 | break; |
65 | 0 | case ExternalType::Memory: |
66 | 0 | if (auto Res = ModInst.findMemoryExports(ExtName); likely(Res != nullptr)) { |
67 | 0 | return {}; |
68 | 0 | } |
69 | 0 | break; |
70 | 0 | case ExternalType::Global: |
71 | 0 | if (auto Res = ModInst.findGlobalExports(ExtName); likely(Res != nullptr)) { |
72 | 0 | return {}; |
73 | 0 | } |
74 | 0 | break; |
75 | 0 | case ExternalType::Tag: |
76 | 0 | if (auto Res = ModInst.findTagExports(ExtName); likely(Res != nullptr)) { |
77 | 0 | return {}; |
78 | 0 | } |
79 | 0 | break; |
80 | 0 | default: |
81 | 0 | return logUnknownError(ModName, ExtName, ExtType); |
82 | 0 | } |
83 | | |
84 | | // Check is error external type or unknown imports. |
85 | 0 | if (ModInst.findFuncExports(ExtName)) { |
86 | 0 | return logMatchError(ModName, ExtName, ExtType, ExtType, |
87 | 0 | ExternalType::Function); |
88 | 0 | } |
89 | 0 | if (ModInst.findTableExports(ExtName)) { |
90 | 0 | return logMatchError(ModName, ExtName, ExtType, ExtType, |
91 | 0 | ExternalType::Table); |
92 | 0 | } |
93 | 0 | if (ModInst.findMemoryExports(ExtName)) { |
94 | 0 | return logMatchError(ModName, ExtName, ExtType, ExtType, |
95 | 0 | ExternalType::Memory); |
96 | 0 | } |
97 | 0 | if (ModInst.findTagExports(ExtName)) { |
98 | 0 | return logMatchError(ModName, ExtName, ExtType, ExtType, ExternalType::Tag); |
99 | 0 | } |
100 | 0 | if (ModInst.findGlobalExports(ExtName)) { |
101 | 0 | return logMatchError(ModName, ExtName, ExtType, ExtType, |
102 | 0 | ExternalType::Global); |
103 | 0 | } |
104 | | |
105 | 0 | return logUnknownError(ModName, ExtName, ExtType); |
106 | 0 | } |
107 | | } // namespace |
108 | | |
109 | | // Instantiate imports. See "include/executor/executor.h". |
110 | | Expect<void> Executor::instantiate(Runtime::StoreManager &StoreMgr, |
111 | | Runtime::Instance::ModuleInstance &ModInst, |
112 | 0 | const AST::ImportSection &ImportSec) { |
113 | | // Iterate and instantiate import descriptions. |
114 | 0 | for (const auto &ImpDesc : ImportSec.getContent()) { |
115 | | // Get data from import description and find import module. |
116 | 0 | auto ExtType = ImpDesc.getExternalType(); |
117 | 0 | auto ModName = ImpDesc.getModuleName(); |
118 | 0 | auto ExtName = ImpDesc.getExternalName(); |
119 | 0 | const auto *ImpModInst = StoreMgr.findModule(ModName); |
120 | 0 | if (unlikely(ImpModInst == nullptr)) { |
121 | 0 | auto Res = logUnknownError(ModName, ExtName, ExtType); |
122 | 0 | if (ModName == "wasi_snapshot_preview1"sv) { |
123 | 0 | spdlog::error(" This is a WASI related import. Please ensure that " |
124 | 0 | "you've turned on the WASI configuration."sv); |
125 | 0 | } else if (ModName == "wasi_nn"sv) { |
126 | 0 | spdlog::error(" This is a WASI-NN related import. Please ensure " |
127 | 0 | "that you've turned on the WASI-NN configuration and " |
128 | 0 | "installed the WASI-NN plug-in."sv); |
129 | 0 | } else if (ModName == "wasi_crypto_common"sv || |
130 | 0 | ModName == "wasi_crypto_asymmetric_common"sv || |
131 | 0 | ModName == "wasi_crypto_kx"sv || |
132 | 0 | ModName == "wasi_crypto_signatures"sv || |
133 | 0 | ModName == "wasi_crypto_symmetric"sv) { |
134 | 0 | spdlog::error(" This is a WASI-Crypto related import. Please ensure " |
135 | 0 | "that you've turned on the WASI-Crypto configuration and " |
136 | 0 | "installed the WASI-Crypto plug-in."); |
137 | 0 | } else if (ModName == "env"sv) { |
138 | 0 | spdlog::error( |
139 | 0 | " This may be the import of host environment like JavaScript or " |
140 | 0 | "Golang. Please check that you've registered the necessary host " |
141 | 0 | "modules from the host programming language."sv); |
142 | 0 | } |
143 | 0 | return Res; |
144 | 0 | } |
145 | 0 | EXPECTED_TRY(checkImportMatched(ModName, ExtName, ExtType, *ImpModInst)); |
146 | | |
147 | | // Add the imports into module instance. |
148 | 0 | switch (ExtType) { |
149 | 0 | case ExternalType::Function: { |
150 | | // Get function type index. External type checked in validation. |
151 | 0 | uint32_t TypeIdx = ImpDesc.getExternalFuncTypeIdx(); |
152 | | // Import matching. |
153 | 0 | auto *ImpInst = ImpModInst->findFuncExports(ExtName); |
154 | | // External function type should match the import function type in |
155 | | // description. |
156 | |
|
157 | 0 | if (!AST::TypeMatcher::matchType(ModInst.getTypeList(), TypeIdx, |
158 | 0 | ImpModInst->getTypeList(), |
159 | 0 | ImpInst->getTypeIndex())) { |
160 | 0 | const auto &ExpDefType = **ModInst.getType(TypeIdx); |
161 | 0 | bool IsMatchV2 = false; |
162 | 0 | const auto &ExpFuncType = ExpDefType.getCompositeType().getFuncType(); |
163 | 0 | const auto &ImpFuncType = ImpInst->getFuncType(); |
164 | 0 | if (ModName == "wasi_snapshot_preview1"sv) { |
165 | | /* |
166 | | * The following functions should provide V1 and V2. |
167 | | "sock_open_v2", |
168 | | "sock_bind_v2", |
169 | | "sock_connect_v2", |
170 | | "sock_listen_v2", |
171 | | "sock_accept_v2", |
172 | | "sock_recv_v2", |
173 | | "sock_recv_from_v2", |
174 | | "sock_send_v2", |
175 | | "sock_send_to_v2", |
176 | | "sock_getlocaladdr_v2", |
177 | | "sock_getpeeraddr_v2" |
178 | | */ |
179 | 0 | std::vector<std::string_view> CompatibleWASISocketAPI = { |
180 | 0 | "sock_open"sv, "sock_bind"sv, "sock_connect"sv, |
181 | 0 | "sock_listen"sv, "sock_accept"sv, "sock_recv"sv, |
182 | 0 | "sock_recv_from"sv, "sock_send"sv, "sock_send_to"sv, |
183 | 0 | "sock_getlocaladdr"sv, "sock_getpeeraddr"sv}; |
184 | 0 | for (auto Iter = CompatibleWASISocketAPI.begin(); |
185 | 0 | Iter != CompatibleWASISocketAPI.end(); Iter++) { |
186 | 0 | if (ExtName == *Iter) { |
187 | 0 | auto *ImpInstV2 = |
188 | 0 | ImpModInst->findFuncExports(std::string(*Iter) + "_v2"); |
189 | 0 | if (!AST::TypeMatcher::matchType( |
190 | 0 | ModInst.getTypeList(), *ExpDefType.getTypeIndex(), |
191 | 0 | ImpModInst->getTypeList(), ImpInst->getTypeIndex())) { |
192 | | // Try to match the new version |
193 | 0 | ImpInst = ImpInstV2; |
194 | 0 | IsMatchV2 = true; |
195 | 0 | break; |
196 | 0 | } |
197 | 0 | } |
198 | 0 | } |
199 | 0 | } |
200 | 0 | if (!IsMatchV2) { |
201 | 0 | return logMatchError( |
202 | 0 | ModName, ExtName, ExtType, ExpFuncType.getParamTypes(), |
203 | 0 | ExpFuncType.getReturnTypes(), ImpFuncType.getParamTypes(), |
204 | 0 | ImpFuncType.getReturnTypes()); |
205 | 0 | } |
206 | 0 | } |
207 | | // Set the matched function address to module instance. |
208 | 0 | ModInst.importFunction(ImpInst); |
209 | 0 | break; |
210 | 0 | } |
211 | 0 | case ExternalType::Table: { |
212 | | // Get table type. External type checked in validation. |
213 | 0 | const auto &TabType = ImpDesc.getExternalTableType(); |
214 | 0 | const auto &TabLim = TabType.getLimit(); |
215 | | // Import matching. External table type should match the one in import |
216 | | // description. |
217 | 0 | auto *ImpInst = ImpModInst->findTableExports(ExtName); |
218 | 0 | const auto &ImpType = ImpInst->getTableType(); |
219 | 0 | const auto &ImpLim = ImpType.getLimit(); |
220 | | // External table reference type should match the import table reference |
221 | | // type in description, and vice versa. |
222 | 0 | if (!AST::TypeMatcher::matchType( |
223 | 0 | ModInst.getTypeList(), TabType.getRefType(), |
224 | 0 | ImpModInst->getTypeList(), ImpType.getRefType()) || |
225 | 0 | !AST::TypeMatcher::matchType( |
226 | 0 | ImpModInst->getTypeList(), ImpType.getRefType(), |
227 | 0 | ModInst.getTypeList(), TabType.getRefType()) || |
228 | 0 | !matchLimit(TabLim, ImpLim)) { |
229 | 0 | return logMatchError(ModName, ExtName, ExtType, TabType.getRefType(), |
230 | 0 | TabLim.hasMax(), TabLim.getMin(), TabLim.getMax(), |
231 | 0 | ImpType.getRefType(), ImpLim.hasMax(), |
232 | 0 | ImpLim.getMin(), ImpLim.getMax()); |
233 | 0 | } |
234 | | // Set the matched table address to module instance. |
235 | 0 | ModInst.importTable(ImpInst); |
236 | 0 | break; |
237 | 0 | } |
238 | 0 | case ExternalType::Memory: { |
239 | | // Get memory type. External type checked in validation. |
240 | 0 | const auto &MemType = ImpDesc.getExternalMemoryType(); |
241 | 0 | const auto &MemLim = MemType.getLimit(); |
242 | | // Import matching. External memory type should match the one in import |
243 | | // description. |
244 | 0 | auto *ImpInst = ImpModInst->findMemoryExports(ExtName); |
245 | 0 | const auto &ImpLim = ImpInst->getMemoryType().getLimit(); |
246 | 0 | if (!matchLimit(MemLim, ImpLim)) { |
247 | 0 | return logMatchError(ModName, ExtName, ExtType, MemLim.hasMax(), |
248 | 0 | MemLim.getMin(), MemLim.getMax(), ImpLim.hasMax(), |
249 | 0 | ImpLim.getMin(), ImpLim.getMax()); |
250 | 0 | } |
251 | | // Set the matched memory address to module instance. |
252 | 0 | ModInst.importMemory(ImpInst); |
253 | 0 | break; |
254 | 0 | } |
255 | 0 | case ExternalType::Tag: { |
256 | | // Get tag type. External type checked in validation. |
257 | 0 | const auto &TagType = ImpDesc.getExternalTagType(); |
258 | | // Import matching. |
259 | 0 | auto *ImpInst = ImpModInst->findTagExports(ExtName); |
260 | 0 | if (!AST::TypeMatcher::matchType( |
261 | 0 | ModInst.getTypeList(), TagType.getTypeIdx(), |
262 | 0 | ImpModInst->getTypeList(), ImpInst->getTagType().getTypeIdx())) { |
263 | 0 | const auto &ExpFuncType = |
264 | 0 | TagType.getDefType().getCompositeType().getFuncType(); |
265 | 0 | const auto &ImpFuncType = |
266 | 0 | ImpInst->getTagType().getDefType().getCompositeType().getFuncType(); |
267 | 0 | return logMatchError( |
268 | 0 | ModName, ExtName, ExtType, ExpFuncType.getParamTypes(), |
269 | 0 | ExpFuncType.getReturnTypes(), ImpFuncType.getParamTypes(), |
270 | 0 | ImpFuncType.getReturnTypes()); |
271 | 0 | } |
272 | 0 | ModInst.importTag(ImpInst); |
273 | 0 | break; |
274 | 0 | } |
275 | 0 | case ExternalType::Global: { |
276 | | // Get global type. External type checked in validation. |
277 | 0 | const auto &GlobType = ImpDesc.getExternalGlobalType(); |
278 | | // Import matching. External global type should match the one in |
279 | | // import description. |
280 | 0 | auto *ImpInst = ImpModInst->findGlobalExports(ExtName); |
281 | 0 | const auto &ImpType = ImpInst->getGlobalType(); |
282 | 0 | bool IsMatch = false; |
283 | 0 | if (ImpType.getValMut() == GlobType.getValMut()) { |
284 | | // For both const or both var: external global value type should match |
285 | | // the import global value type in description. |
286 | 0 | IsMatch = AST::TypeMatcher::matchType( |
287 | 0 | ModInst.getTypeList(), GlobType.getValType(), |
288 | 0 | ImpModInst->getTypeList(), ImpType.getValType()); |
289 | 0 | if (ImpType.getValMut() == ValMut::Var) { |
290 | | // If both var: import global value type in description should also |
291 | | // match the external global value type. |
292 | 0 | IsMatch &= AST::TypeMatcher::matchType( |
293 | 0 | ImpModInst->getTypeList(), ImpType.getValType(), |
294 | 0 | ModInst.getTypeList(), GlobType.getValType()); |
295 | 0 | } |
296 | 0 | } |
297 | 0 | if (!IsMatch) { |
298 | 0 | return logMatchError(ModName, ExtName, ExtType, GlobType.getValType(), |
299 | 0 | GlobType.getValMut(), ImpType.getValType(), |
300 | 0 | ImpType.getValMut()); |
301 | 0 | } |
302 | | // Set the matched global address to module instance. |
303 | 0 | ModInst.importGlobal(ImpInst); |
304 | 0 | break; |
305 | 0 | } |
306 | 0 | default: |
307 | 0 | break; |
308 | 0 | } |
309 | 0 | } |
310 | 0 | return {}; |
311 | 0 | } |
312 | | |
313 | | } // namespace Executor |
314 | | } // namespace WasmEdge |