AbstractMachine.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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.append(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. auto elem_instance = m_store.get(main_module_instance.elements()[current_index]);
  260. if (!active_ptr) {
  261. if (segment.mode.has<ElementSection::Declarative>())
  262. *elem_instance = ElementInstance(elem_instance->type(), {});
  263. continue;
  264. }
  265. Configuration config { m_store };
  266. if (m_should_limit_instruction_count)
  267. config.enable_instruction_count_limit();
  268. config.set_frame(Frame {
  269. main_module_instance,
  270. Vector<Value> {},
  271. active_ptr->expression,
  272. 1,
  273. });
  274. auto result = config.execute(interpreter).assert_wasm_result();
  275. if (result.is_trap()) {
  276. instantiation_result = InstantiationError { ByteString::formatted("Element section initialisation trapped: {}", result.trap().reason) };
  277. return IterationDecision::Break;
  278. }
  279. auto d = result.values().first().to<i32>();
  280. if (!d.has_value()) {
  281. instantiation_result = InstantiationError { "Element section initialisation returned invalid table initial offset" };
  282. return IterationDecision::Break;
  283. }
  284. auto table_instance = m_store.get(main_module_instance.tables()[active_ptr->index.value()]);
  285. if (current_index >= main_module_instance.elements().size()) {
  286. instantiation_result = InstantiationError { "Invalid element referenced by active element segment" };
  287. return IterationDecision::Break;
  288. }
  289. if (!table_instance || !elem_instance) {
  290. instantiation_result = InstantiationError { "Invalid element referenced by active element segment" };
  291. return IterationDecision::Break;
  292. }
  293. Checked<size_t> total_size = elem_instance->references().size();
  294. total_size.saturating_add(d.value());
  295. if (total_size.value() > table_instance->elements().size()) {
  296. instantiation_result = InstantiationError { "Table instantiation out of bounds" };
  297. return IterationDecision::Break;
  298. }
  299. size_t i = 0;
  300. for (auto it = elem_instance->references().begin(); it < elem_instance->references().end(); ++i, ++it) {
  301. table_instance->elements()[i + d.value()] = *it;
  302. }
  303. // Drop element
  304. *m_store.get(main_module_instance.elements()[current_index]) = ElementInstance(elem_instance->type(), {});
  305. }
  306. return IterationDecision::Continue;
  307. });
  308. if (instantiation_result.has_value())
  309. return instantiation_result.release_value();
  310. module.for_each_section_of_type<DataSection>([&](DataSection const& data_section) {
  311. for (auto& segment : data_section.data()) {
  312. segment.value().visit(
  313. [&](DataSection::Data::Active const& data) {
  314. Configuration config { m_store };
  315. if (m_should_limit_instruction_count)
  316. config.enable_instruction_count_limit();
  317. config.set_frame(Frame {
  318. main_module_instance,
  319. Vector<Value> {},
  320. data.offset,
  321. 1,
  322. });
  323. auto result = config.execute(interpreter).assert_wasm_result();
  324. if (result.is_trap()) {
  325. instantiation_result = InstantiationError { ByteString::formatted("Data section initialisation trapped: {}", result.trap().reason) };
  326. return;
  327. }
  328. size_t offset = 0;
  329. result.values().first().value().visit(
  330. [&](auto const& value) { offset = value; },
  331. [&](u128 const&) { instantiation_result = InstantiationError { "Data segment offset returned a vector type"sv }; },
  332. [&](Reference const&) { instantiation_result = InstantiationError { "Data segment offset returned a reference"sv }; });
  333. if (instantiation_result.has_value() && instantiation_result->is_error())
  334. return;
  335. if (main_module_instance.memories().size() <= data.index.value()) {
  336. instantiation_result = InstantiationError {
  337. ByteString::formatted("Data segment referenced out-of-bounds memory ({}) of max {} entries",
  338. data.index.value(), main_module_instance.memories().size())
  339. };
  340. return;
  341. }
  342. auto maybe_data_address = m_store.allocate_data(data.init);
  343. if (!maybe_data_address.has_value()) {
  344. instantiation_result = InstantiationError { "Failed to allocate a data instance for an active data segment"sv };
  345. return;
  346. }
  347. main_module_instance.datas().append(*maybe_data_address);
  348. if (data.init.is_empty())
  349. return;
  350. auto address = main_module_instance.memories()[data.index.value()];
  351. auto instance = m_store.get(address);
  352. Checked<size_t> checked_offset = data.init.size();
  353. checked_offset += offset;
  354. if (checked_offset.has_overflow() || checked_offset > instance->size()) {
  355. instantiation_result = InstantiationError {
  356. ByteString::formatted("Data segment attempted to write to out-of-bounds memory ({}) in memory of size {}",
  357. offset, instance->size())
  358. };
  359. return;
  360. }
  361. instance->data().overwrite(offset, data.init.data(), data.init.size());
  362. },
  363. [&](DataSection::Data::Passive const& passive) {
  364. auto maybe_data_address = m_store.allocate_data(passive.init);
  365. if (!maybe_data_address.has_value()) {
  366. instantiation_result = InstantiationError { "Failed to allocate a data instance for a passive data segment"sv };
  367. return;
  368. }
  369. main_module_instance.datas().append(*maybe_data_address);
  370. });
  371. }
  372. });
  373. module.for_each_section_of_type<StartSection>([&](StartSection const& section) {
  374. auto& functions = main_module_instance.functions();
  375. auto index = section.function().index();
  376. if (functions.size() <= index.value()) {
  377. instantiation_result = InstantiationError { ByteString::formatted("Start section function referenced invalid index {} of max {} entries", index.value(), functions.size()) };
  378. return;
  379. }
  380. invoke(functions[index.value()], {});
  381. });
  382. if (instantiation_result.has_value())
  383. return instantiation_result.release_value();
  384. return InstantiationResult { move(main_module_instance_pointer) };
  385. }
  386. Optional<InstantiationError> AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions)
  387. {
  388. Optional<InstantiationError> result;
  389. for (auto& entry : externs) {
  390. entry.visit(
  391. [&](FunctionAddress const& address) { module_instance.functions().append(address); },
  392. [&](TableAddress const& address) { module_instance.tables().append(address); },
  393. [&](MemoryAddress const& address) { module_instance.memories().append(address); },
  394. [&](GlobalAddress const& address) { module_instance.globals().append(address); });
  395. }
  396. module_instance.functions().extend(own_functions);
  397. // FIXME: What if this fails?
  398. module.for_each_section_of_type<TableSection>([&](TableSection const& section) {
  399. for (auto& table : section.tables()) {
  400. auto table_address = m_store.allocate(table.type());
  401. VERIFY(table_address.has_value());
  402. module_instance.tables().append(*table_address);
  403. }
  404. });
  405. module.for_each_section_of_type<MemorySection>([&](MemorySection const& section) {
  406. for (auto& memory : section.memories()) {
  407. auto memory_address = m_store.allocate(memory.type());
  408. VERIFY(memory_address.has_value());
  409. module_instance.memories().append(*memory_address);
  410. }
  411. });
  412. module.for_each_section_of_type<GlobalSection>([&](GlobalSection const& section) {
  413. size_t index = 0;
  414. for (auto& entry : section.entries()) {
  415. auto address = m_store.allocate(entry.type(), move(global_values[index]));
  416. VERIFY(address.has_value());
  417. module_instance.globals().append(*address);
  418. index++;
  419. }
  420. });
  421. module.for_each_section_of_type<ExportSection>([&](ExportSection const& section) {
  422. for (auto& entry : section.entries()) {
  423. Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress, Empty> address {};
  424. entry.description().visit(
  425. [&](FunctionIndex const& index) {
  426. if (module_instance.functions().size() > index.value())
  427. address = FunctionAddress { module_instance.functions()[index.value()] };
  428. else
  429. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.functions().size());
  430. },
  431. [&](TableIndex const& index) {
  432. if (module_instance.tables().size() > index.value())
  433. address = TableAddress { module_instance.tables()[index.value()] };
  434. else
  435. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.tables().size());
  436. },
  437. [&](MemoryIndex const& index) {
  438. if (module_instance.memories().size() > index.value())
  439. address = MemoryAddress { module_instance.memories()[index.value()] };
  440. else
  441. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.memories().size());
  442. },
  443. [&](GlobalIndex const& index) {
  444. if (module_instance.globals().size() > index.value())
  445. address = GlobalAddress { module_instance.globals()[index.value()] };
  446. else
  447. dbgln("Failed to export '{}', the exported address ({}) was out of bounds (min: 0, max: {})", entry.name(), index.value(), module_instance.globals().size());
  448. });
  449. if (address.has<Empty>()) {
  450. result = InstantiationError { "An export could not be resolved" };
  451. continue;
  452. }
  453. module_instance.exports().append(ExportInstance {
  454. entry.name(),
  455. move(address).downcast<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>(),
  456. });
  457. }
  458. });
  459. return result;
  460. }
  461. Optional<InstantiationError> AbstractMachine::allocate_all_final_phase(Module const& module, ModuleInstance& module_instance, Vector<Vector<Reference>>& elements)
  462. {
  463. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  464. size_t index = 0;
  465. for (auto& segment : section.segments()) {
  466. auto address = m_store.allocate(segment.type, move(elements[index]));
  467. VERIFY(address.has_value());
  468. module_instance.elements().append(*address);
  469. index++;
  470. }
  471. });
  472. return {};
  473. }
  474. Result AbstractMachine::invoke(FunctionAddress address, Vector<Value> arguments)
  475. {
  476. BytecodeInterpreter interpreter(m_stack_info);
  477. return invoke(interpreter, address, move(arguments));
  478. }
  479. Result AbstractMachine::invoke(Interpreter& interpreter, FunctionAddress address, Vector<Value> arguments)
  480. {
  481. Configuration configuration { m_store };
  482. if (m_should_limit_instruction_count)
  483. configuration.enable_instruction_count_limit();
  484. return configuration.call(interpreter, address, move(arguments));
  485. }
  486. void Linker::link(ModuleInstance const& instance)
  487. {
  488. populate();
  489. if (m_unresolved_imports.is_empty())
  490. return;
  491. HashTable<Name> resolved_imports;
  492. for (auto& import_ : m_unresolved_imports) {
  493. auto it = instance.exports().find_if([&](auto& export_) { return export_.name() == import_.name; });
  494. if (!it.is_end()) {
  495. resolved_imports.set(import_);
  496. m_resolved_imports.set(import_, it->value());
  497. }
  498. }
  499. for (auto& entry : resolved_imports)
  500. m_unresolved_imports.remove(entry);
  501. }
  502. void Linker::link(HashMap<Linker::Name, ExternValue> const& exports)
  503. {
  504. populate();
  505. if (m_unresolved_imports.is_empty())
  506. return;
  507. if (exports.is_empty())
  508. return;
  509. HashTable<Name> resolved_imports;
  510. for (auto& import_ : m_unresolved_imports) {
  511. auto export_ = exports.get(import_);
  512. if (export_.has_value()) {
  513. resolved_imports.set(import_);
  514. m_resolved_imports.set(import_, export_.value());
  515. }
  516. }
  517. for (auto& entry : resolved_imports)
  518. m_unresolved_imports.remove(entry);
  519. }
  520. AK::ErrorOr<Vector<ExternValue>, LinkError> Linker::finish()
  521. {
  522. populate();
  523. if (!m_unresolved_imports.is_empty()) {
  524. if (!m_error.has_value())
  525. m_error = LinkError {};
  526. for (auto& entry : m_unresolved_imports)
  527. m_error->missing_imports.append(entry.name);
  528. return *m_error;
  529. }
  530. if (m_error.has_value())
  531. return *m_error;
  532. // Result must be in the same order as the module imports
  533. Vector<ExternValue> exports;
  534. exports.ensure_capacity(m_ordered_imports.size());
  535. for (auto& import_ : m_ordered_imports)
  536. exports.unchecked_append(*m_resolved_imports.get(import_));
  537. return exports;
  538. }
  539. void Linker::populate()
  540. {
  541. if (!m_ordered_imports.is_empty())
  542. return;
  543. // There better be at most one import section!
  544. bool already_seen_an_import_section = false;
  545. m_module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
  546. if (already_seen_an_import_section) {
  547. if (!m_error.has_value())
  548. m_error = LinkError {};
  549. m_error->other_errors.append(LinkError::InvalidImportedModule);
  550. return;
  551. }
  552. already_seen_an_import_section = true;
  553. for (auto& import_ : section.imports()) {
  554. m_ordered_imports.append({ import_.module(), import_.name(), import_.description() });
  555. m_unresolved_imports.set(m_ordered_imports.last());
  556. }
  557. });
  558. }
  559. }