AbstractMachine.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Enumerate.h>
  7. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  8. #include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
  9. #include <LibWasm/AbstractMachine/Configuration.h>
  10. #include <LibWasm/AbstractMachine/Interpreter.h>
  11. #include <LibWasm/AbstractMachine/Validator.h>
  12. #include <LibWasm/Types.h>
  13. namespace Wasm {
  14. Optional<FunctionAddress> Store::allocate(ModuleInstance& module, Module::Function const& function)
  15. {
  16. FunctionAddress address { m_functions.size() };
  17. if (function.type().value() > module.types().size())
  18. return {};
  19. auto& type = module.types()[function.type().value()];
  20. m_functions.empend(WasmFunction { type, module, function });
  21. return address;
  22. }
  23. Optional<FunctionAddress> Store::allocate(HostFunction&& function)
  24. {
  25. FunctionAddress address { m_functions.size() };
  26. m_functions.empend(HostFunction { move(function) });
  27. return address;
  28. }
  29. Optional<TableAddress> Store::allocate(TableType const& type)
  30. {
  31. TableAddress address { m_tables.size() };
  32. Vector<Reference> elements;
  33. elements.resize(type.limits().min());
  34. m_tables.empend(TableInstance { type, move(elements) });
  35. return address;
  36. }
  37. Optional<MemoryAddress> Store::allocate(MemoryType const& type)
  38. {
  39. MemoryAddress address { m_memories.size() };
  40. auto instance = MemoryInstance::create(type);
  41. if (instance.is_error())
  42. return {};
  43. m_memories.append(instance.release_value());
  44. return address;
  45. }
  46. Optional<GlobalAddress> Store::allocate(GlobalType const& type, Value value)
  47. {
  48. GlobalAddress address { m_globals.size() };
  49. m_globals.append(GlobalInstance { move(value), type.is_mutable() });
  50. return address;
  51. }
  52. Optional<DataAddress> Store::allocate_data(Vector<u8> initializer)
  53. {
  54. DataAddress address { m_datas.size() };
  55. m_datas.append(DataInstance { move(initializer) });
  56. return address;
  57. }
  58. Optional<ElementAddress> Store::allocate(ValueType const& type, Vector<Reference> references)
  59. {
  60. ElementAddress address { m_elements.size() };
  61. m_elements.append(ElementInstance { type, move(references) });
  62. return address;
  63. }
  64. FunctionInstance* Store::get(FunctionAddress address)
  65. {
  66. auto value = address.value();
  67. if (m_functions.size() <= value)
  68. return nullptr;
  69. return &m_functions[value];
  70. }
  71. TableInstance* Store::get(TableAddress address)
  72. {
  73. auto value = address.value();
  74. if (m_tables.size() <= value)
  75. return nullptr;
  76. return &m_tables[value];
  77. }
  78. MemoryInstance* Store::get(MemoryAddress address)
  79. {
  80. auto value = address.value();
  81. if (m_memories.size() <= value)
  82. return nullptr;
  83. return &m_memories[value];
  84. }
  85. GlobalInstance* Store::get(GlobalAddress address)
  86. {
  87. auto value = address.value();
  88. if (m_globals.size() <= value)
  89. return nullptr;
  90. return &m_globals[value];
  91. }
  92. ElementInstance* Store::get(ElementAddress address)
  93. {
  94. auto value = address.value();
  95. if (m_elements.size() <= value)
  96. return nullptr;
  97. return &m_elements[value];
  98. }
  99. DataInstance* Store::get(DataAddress address)
  100. {
  101. auto value = address.value();
  102. if (m_datas.size() <= value)
  103. return nullptr;
  104. return &m_datas[value];
  105. }
  106. ErrorOr<void, ValidationError> AbstractMachine::validate(Module& module)
  107. {
  108. if (module.validation_status() != Module::ValidationStatus::Unchecked) {
  109. if (module.validation_status() == Module::ValidationStatus::Valid)
  110. return {};
  111. return ValidationError { module.validation_error() };
  112. }
  113. auto result = Validator {}.validate(module);
  114. if (result.is_error()) {
  115. module.set_validation_error(result.error().error_string);
  116. return result.release_error();
  117. }
  118. return {};
  119. }
  120. InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<ExternValue> externs)
  121. {
  122. if (auto result = validate(const_cast<Module&>(module)); result.is_error())
  123. return InstantiationError { ByteString::formatted("Validation failed: {}", result.error()) };
  124. auto main_module_instance_pointer = make<ModuleInstance>();
  125. auto& main_module_instance = *main_module_instance_pointer;
  126. Optional<InstantiationResult> instantiation_result;
  127. module.for_each_section_of_type<TypeSection>([&](TypeSection const& section) {
  128. main_module_instance.types() = section.types();
  129. });
  130. Vector<Value> global_values;
  131. Vector<Vector<Reference>> elements;
  132. ModuleInstance auxiliary_instance;
  133. module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
  134. for (auto [i, import_] : enumerate(section.imports())) {
  135. auto extern_ = externs.at(i);
  136. auto is_valid = import_.description().visit(
  137. [&](MemoryType const& mem_type) -> bool {
  138. if (!extern_.has<MemoryAddress>())
  139. return false;
  140. auto other_mem_type = m_store.get(extern_.get<MemoryAddress>())->type();
  141. return other_mem_type.limits().is_subset_of(mem_type.limits());
  142. },
  143. [&](TableType const& table_type) -> bool {
  144. if (!extern_.has<TableAddress>())
  145. return false;
  146. auto other_table_type = m_store.get(extern_.get<TableAddress>())->type();
  147. return table_type.element_type() == other_table_type.element_type()
  148. && other_table_type.limits().is_subset_of(table_type.limits());
  149. },
  150. [&](GlobalType const& global_type) -> bool {
  151. if (!extern_.has<GlobalAddress>())
  152. return false;
  153. auto other_global_type = m_store.get(extern_.get<GlobalAddress>())->type();
  154. return global_type.type() == other_global_type.type()
  155. && global_type.is_mutable() == other_global_type.is_mutable();
  156. },
  157. [&](FunctionType const& type) -> bool {
  158. if (!extern_.has<FunctionAddress>())
  159. return false;
  160. auto other_type = m_store.get(extern_.get<FunctionAddress>())->visit([&](WasmFunction const& wasm_func) { return wasm_func.type(); }, [&](HostFunction const& host_func) { return host_func.type(); });
  161. return type.results() == other_type.results()
  162. && type.parameters() == other_type.parameters();
  163. },
  164. [&](TypeIndex type_index) -> bool {
  165. if (!extern_.has<FunctionAddress>())
  166. return false;
  167. auto other_type = m_store.get(extern_.get<FunctionAddress>())->visit([&](WasmFunction const& wasm_func) { return wasm_func.type(); }, [&](HostFunction const& host_func) { return host_func.type(); });
  168. auto& type = module.type(type_index);
  169. return type.results() == other_type.results()
  170. && type.parameters() == other_type.parameters();
  171. });
  172. if (!is_valid)
  173. instantiation_result = InstantiationError { "Import and extern do not match" };
  174. }
  175. });
  176. if (instantiation_result.has_value())
  177. return instantiation_result.release_value();
  178. for (auto& entry : externs) {
  179. if (auto* ptr = entry.get_pointer<GlobalAddress>())
  180. auxiliary_instance.globals().append(*ptr);
  181. }
  182. Vector<FunctionAddress> module_functions;
  183. module_functions.ensure_capacity(module.functions().size());
  184. for (auto& func : module.functions()) {
  185. auto address = m_store.allocate(main_module_instance, func);
  186. VERIFY(address.has_value());
  187. auxiliary_instance.functions().append(*address);
  188. module_functions.append(*address);
  189. }
  190. BytecodeInterpreter interpreter(m_stack_info);
  191. module.for_each_section_of_type<GlobalSection>([&](auto& global_section) {
  192. for (auto& entry : global_section.entries()) {
  193. Configuration config { m_store };
  194. if (m_should_limit_instruction_count)
  195. config.enable_instruction_count_limit();
  196. config.set_frame(Frame {
  197. auxiliary_instance,
  198. Vector<Value> {},
  199. entry.expression(),
  200. 1,
  201. });
  202. auto result = config.execute(interpreter).assert_wasm_result();
  203. if (result.is_trap())
  204. instantiation_result = InstantiationError { ByteString::formatted("Global value construction trapped: {}", result.trap().reason) };
  205. else
  206. global_values.append(result.values().first());
  207. }
  208. });
  209. if (instantiation_result.has_value())
  210. return instantiation_result.release_value();
  211. if (auto result = allocate_all_initial_phase(module, main_module_instance, externs, global_values, module_functions); result.has_value())
  212. return result.release_value();
  213. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  214. for (auto& segment : section.segments()) {
  215. Vector<Reference> references;
  216. for (auto& entry : segment.init) {
  217. Configuration config { m_store };
  218. if (m_should_limit_instruction_count)
  219. config.enable_instruction_count_limit();
  220. config.set_frame(Frame {
  221. main_module_instance,
  222. Vector<Value> {},
  223. entry,
  224. entry.instructions().size(),
  225. });
  226. auto result = config.execute(interpreter).assert_wasm_result();
  227. if (result.is_trap()) {
  228. instantiation_result = InstantiationError { ByteString::formatted("Element construction trapped: {}", result.trap().reason) };
  229. return IterationDecision::Continue;
  230. }
  231. for (auto& value : result.values()) {
  232. if (!value.type().is_reference()) {
  233. instantiation_result = InstantiationError { "Evaluated element entry is not a reference" };
  234. return IterationDecision::Continue;
  235. }
  236. auto reference = value.to<Reference>();
  237. if (!reference.has_value()) {
  238. instantiation_result = InstantiationError { "Evaluated element entry does not contain a reference" };
  239. return IterationDecision::Continue;
  240. }
  241. // FIXME: type-check the reference.
  242. references.prepend(reference.release_value());
  243. }
  244. }
  245. elements.append(move(references));
  246. }
  247. return IterationDecision::Continue;
  248. });
  249. if (instantiation_result.has_value())
  250. return instantiation_result.release_value();
  251. if (auto result = allocate_all_final_phase(module, main_module_instance, elements); result.has_value())
  252. return result.release_value();
  253. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  254. size_t index = 0;
  255. for (auto& segment : section.segments()) {
  256. auto current_index = index;
  257. ++index;
  258. auto active_ptr = segment.mode.get_pointer<ElementSection::Active>();
  259. if (!active_ptr)
  260. continue;
  261. Configuration config { m_store };
  262. if (m_should_limit_instruction_count)
  263. config.enable_instruction_count_limit();
  264. config.set_frame(Frame {
  265. main_module_instance,
  266. Vector<Value> {},
  267. active_ptr->expression,
  268. 1,
  269. });
  270. auto result = config.execute(interpreter).assert_wasm_result();
  271. if (result.is_trap()) {
  272. instantiation_result = InstantiationError { ByteString::formatted("Element section initialisation trapped: {}", result.trap().reason) };
  273. return IterationDecision::Break;
  274. }
  275. auto d = result.values().first().to<i32>();
  276. if (!d.has_value()) {
  277. instantiation_result = InstantiationError { "Element section initialisation returned invalid table initial offset" };
  278. return IterationDecision::Break;
  279. }
  280. auto table_instance = m_store.get(main_module_instance.tables()[active_ptr->index.value()]);
  281. if (current_index >= main_module_instance.elements().size()) {
  282. instantiation_result = InstantiationError { "Invalid element referenced by active element segment" };
  283. return IterationDecision::Break;
  284. }
  285. auto elem_instance = m_store.get(main_module_instance.elements()[current_index]);
  286. if (!table_instance || !elem_instance) {
  287. instantiation_result = InstantiationError { "Invalid element referenced by active element segment" };
  288. return IterationDecision::Break;
  289. }
  290. Checked<size_t> total_size = elem_instance->references().size();
  291. total_size.saturating_add(d.value());
  292. if (total_size.value() > table_instance->elements().size()) {
  293. instantiation_result = InstantiationError { "Table instantiation out of bounds" };
  294. return IterationDecision::Break;
  295. }
  296. size_t i = 0;
  297. for (auto it = elem_instance->references().begin(); it < elem_instance->references().end(); ++i, ++it) {
  298. table_instance->elements()[i + d.value()] = *it;
  299. }
  300. }
  301. return IterationDecision::Continue;
  302. });
  303. if (instantiation_result.has_value())
  304. return instantiation_result.release_value();
  305. module.for_each_section_of_type<DataSection>([&](DataSection const& data_section) {
  306. for (auto& segment : data_section.data()) {
  307. segment.value().visit(
  308. [&](DataSection::Data::Active const& data) {
  309. Configuration config { m_store };
  310. if (m_should_limit_instruction_count)
  311. config.enable_instruction_count_limit();
  312. config.set_frame(Frame {
  313. main_module_instance,
  314. Vector<Value> {},
  315. data.offset,
  316. 1,
  317. });
  318. auto result = config.execute(interpreter).assert_wasm_result();
  319. if (result.is_trap()) {
  320. instantiation_result = InstantiationError { ByteString::formatted("Data section initialisation trapped: {}", result.trap().reason) };
  321. return;
  322. }
  323. size_t offset = 0;
  324. result.values().first().value().visit(
  325. [&](auto const& value) { offset = value; },
  326. [&](u128 const&) { instantiation_result = InstantiationError { "Data segment offset returned a vector type"sv }; },
  327. [&](Reference const&) { instantiation_result = InstantiationError { "Data segment offset returned a reference"sv }; });
  328. if (instantiation_result.has_value() && instantiation_result->is_error())
  329. return;
  330. if (main_module_instance.memories().size() <= data.index.value()) {
  331. instantiation_result = InstantiationError {
  332. ByteString::formatted("Data segment referenced out-of-bounds memory ({}) of max {} entries",
  333. data.index.value(), main_module_instance.memories().size())
  334. };
  335. return;
  336. }
  337. auto maybe_data_address = m_store.allocate_data(data.init);
  338. if (!maybe_data_address.has_value()) {
  339. instantiation_result = InstantiationError { "Failed to allocate a data instance for an active data segment"sv };
  340. return;
  341. }
  342. main_module_instance.datas().append(*maybe_data_address);
  343. if (data.init.is_empty())
  344. return;
  345. auto address = main_module_instance.memories()[data.index.value()];
  346. auto instance = m_store.get(address);
  347. Checked<size_t> checked_offset = data.init.size();
  348. checked_offset += offset;
  349. if (checked_offset.has_overflow() || checked_offset > instance->size()) {
  350. instantiation_result = InstantiationError {
  351. ByteString::formatted("Data segment attempted to write to out-of-bounds memory ({}) in memory of size {}",
  352. offset, instance->size())
  353. };
  354. return;
  355. }
  356. instance->data().overwrite(offset, data.init.data(), data.init.size());
  357. },
  358. [&](DataSection::Data::Passive const& passive) {
  359. auto maybe_data_address = m_store.allocate_data(passive.init);
  360. if (!maybe_data_address.has_value()) {
  361. instantiation_result = InstantiationError { "Failed to allocate a data instance for a passive data segment"sv };
  362. return;
  363. }
  364. main_module_instance.datas().append(*maybe_data_address);
  365. });
  366. }
  367. });
  368. module.for_each_section_of_type<StartSection>([&](StartSection const& section) {
  369. auto& functions = main_module_instance.functions();
  370. auto index = section.function().index();
  371. if (functions.size() <= index.value()) {
  372. instantiation_result = InstantiationError { ByteString::formatted("Start section function referenced invalid index {} of max {} entries", index.value(), functions.size()) };
  373. return;
  374. }
  375. invoke(functions[index.value()], {});
  376. });
  377. if (instantiation_result.has_value())
  378. return instantiation_result.release_value();
  379. return InstantiationResult { move(main_module_instance_pointer) };
  380. }
  381. Optional<InstantiationError> AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions)
  382. {
  383. Optional<InstantiationError> result;
  384. for (auto& entry : externs) {
  385. entry.visit(
  386. [&](FunctionAddress const& address) { module_instance.functions().append(address); },
  387. [&](TableAddress const& address) { module_instance.tables().append(address); },
  388. [&](MemoryAddress const& address) { module_instance.memories().append(address); },
  389. [&](GlobalAddress const& address) { module_instance.globals().append(address); });
  390. }
  391. module_instance.functions().extend(own_functions);
  392. // FIXME: What if this fails?
  393. module.for_each_section_of_type<TableSection>([&](TableSection const& section) {
  394. for (auto& table : section.tables()) {
  395. auto table_address = m_store.allocate(table.type());
  396. VERIFY(table_address.has_value());
  397. module_instance.tables().append(*table_address);
  398. }
  399. });
  400. module.for_each_section_of_type<MemorySection>([&](MemorySection const& section) {
  401. for (auto& memory : section.memories()) {
  402. auto memory_address = m_store.allocate(memory.type());
  403. VERIFY(memory_address.has_value());
  404. module_instance.memories().append(*memory_address);
  405. }
  406. });
  407. module.for_each_section_of_type<GlobalSection>([&](GlobalSection const& section) {
  408. size_t index = 0;
  409. for (auto& entry : section.entries()) {
  410. auto address = m_store.allocate(entry.type(), move(global_values[index]));
  411. VERIFY(address.has_value());
  412. module_instance.globals().append(*address);
  413. index++;
  414. }
  415. });
  416. module.for_each_section_of_type<ExportSection>([&](ExportSection const& section) {
  417. for (auto& entry : section.entries()) {
  418. Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress, Empty> address {};
  419. entry.description().visit(
  420. [&](FunctionIndex const& index) {
  421. if (module_instance.functions().size() > index.value())
  422. address = FunctionAddress { module_instance.functions()[index.value()] };
  423. else
  424. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.functions().size());
  425. },
  426. [&](TableIndex const& index) {
  427. if (module_instance.tables().size() > index.value())
  428. address = TableAddress { module_instance.tables()[index.value()] };
  429. else
  430. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.tables().size());
  431. },
  432. [&](MemoryIndex const& index) {
  433. if (module_instance.memories().size() > index.value())
  434. address = MemoryAddress { module_instance.memories()[index.value()] };
  435. else
  436. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.memories().size());
  437. },
  438. [&](GlobalIndex const& index) {
  439. if (module_instance.globals().size() > index.value())
  440. address = GlobalAddress { module_instance.globals()[index.value()] };
  441. else
  442. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.globals().size());
  443. });
  444. if (address.has<Empty>()) {
  445. result = InstantiationError { "An export could not be resolved" };
  446. continue;
  447. }
  448. module_instance.exports().append(ExportInstance {
  449. entry.name(),
  450. move(address).downcast<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>(),
  451. });
  452. }
  453. });
  454. return result;
  455. }
  456. Optional<InstantiationError> AbstractMachine::allocate_all_final_phase(Module const& module, ModuleInstance& module_instance, Vector<Vector<Reference>>& elements)
  457. {
  458. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  459. size_t index = 0;
  460. for (auto& segment : section.segments()) {
  461. auto address = m_store.allocate(segment.type, move(elements[index]));
  462. VERIFY(address.has_value());
  463. module_instance.elements().append(*address);
  464. index++;
  465. }
  466. });
  467. return {};
  468. }
  469. Result AbstractMachine::invoke(FunctionAddress address, Vector<Value> arguments)
  470. {
  471. BytecodeInterpreter interpreter(m_stack_info);
  472. return invoke(interpreter, address, move(arguments));
  473. }
  474. Result AbstractMachine::invoke(Interpreter& interpreter, FunctionAddress address, Vector<Value> arguments)
  475. {
  476. Configuration configuration { m_store };
  477. if (m_should_limit_instruction_count)
  478. configuration.enable_instruction_count_limit();
  479. return configuration.call(interpreter, address, move(arguments));
  480. }
  481. void Linker::link(ModuleInstance const& instance)
  482. {
  483. populate();
  484. if (m_unresolved_imports.is_empty())
  485. return;
  486. HashTable<Name> resolved_imports;
  487. for (auto& import_ : m_unresolved_imports) {
  488. auto it = instance.exports().find_if([&](auto& export_) { return export_.name() == import_.name; });
  489. if (!it.is_end()) {
  490. resolved_imports.set(import_);
  491. m_resolved_imports.set(import_, it->value());
  492. }
  493. }
  494. for (auto& entry : resolved_imports)
  495. m_unresolved_imports.remove(entry);
  496. }
  497. void Linker::link(HashMap<Linker::Name, ExternValue> const& exports)
  498. {
  499. populate();
  500. if (m_unresolved_imports.is_empty())
  501. return;
  502. if (exports.is_empty())
  503. return;
  504. HashTable<Name> resolved_imports;
  505. for (auto& import_ : m_unresolved_imports) {
  506. auto export_ = exports.get(import_);
  507. if (export_.has_value()) {
  508. resolved_imports.set(import_);
  509. m_resolved_imports.set(import_, export_.value());
  510. }
  511. }
  512. for (auto& entry : resolved_imports)
  513. m_unresolved_imports.remove(entry);
  514. }
  515. AK::ErrorOr<Vector<ExternValue>, LinkError> Linker::finish()
  516. {
  517. populate();
  518. if (!m_unresolved_imports.is_empty()) {
  519. if (!m_error.has_value())
  520. m_error = LinkError {};
  521. for (auto& entry : m_unresolved_imports)
  522. m_error->missing_imports.append(entry.name);
  523. return *m_error;
  524. }
  525. if (m_error.has_value())
  526. return *m_error;
  527. // Result must be in the same order as the module imports
  528. Vector<ExternValue> exports;
  529. exports.ensure_capacity(m_ordered_imports.size());
  530. for (auto& import_ : m_ordered_imports)
  531. exports.unchecked_append(*m_resolved_imports.get(import_));
  532. return exports;
  533. }
  534. void Linker::populate()
  535. {
  536. if (!m_ordered_imports.is_empty())
  537. return;
  538. // There better be at most one import section!
  539. bool already_seen_an_import_section = false;
  540. m_module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
  541. if (already_seen_an_import_section) {
  542. if (!m_error.has_value())
  543. m_error = LinkError {};
  544. m_error->other_errors.append(LinkError::InvalidImportedModule);
  545. return;
  546. }
  547. already_seen_an_import_section = true;
  548. for (auto& import_ : section.imports()) {
  549. m_ordered_imports.append({ import_.module(), import_.name(), import_.description() });
  550. m_unresolved_imports.set(m_ordered_imports.last());
  551. }
  552. });
  553. }
  554. }