Parser.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ConstrainedStream.h>
  7. #include <AK/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/LEB128.h>
  10. #include <AK/MemoryStream.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/ScopeLogger.h>
  13. #include <AK/UFixedBigInt.h>
  14. #include <LibWasm/Types.h>
  15. namespace Wasm {
  16. #define TRY_READ(stream, type, error) \
  17. ({ \
  18. /* Ignore -Wshadow to allow nesting the macro. */ \
  19. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  20. auto&& _temporary_result = stream.read_value<type>()); \
  21. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  22. "Do not return a reference from a fallible expression"); \
  23. if (_temporary_result.is_error()) [[unlikely]] \
  24. return with_eof_check(stream, error); \
  25. _temporary_result.release_value(); \
  26. })
  27. ParseError with_eof_check(Stream const& stream, ParseError error_if_not_eof)
  28. {
  29. if (stream.is_eof())
  30. return ParseError::UnexpectedEof;
  31. return error_if_not_eof;
  32. }
  33. template<typename T>
  34. static auto parse_vector(Stream& stream)
  35. {
  36. ScopeLogger<WASM_BINPARSER_DEBUG> logger;
  37. if constexpr (requires { T::parse(stream); }) {
  38. using ResultT = typename decltype(T::parse(stream))::ResultType;
  39. auto count_or_error = stream.read_value<LEB128<u32>>();
  40. if (count_or_error.is_error())
  41. return ParseResult<Vector<ResultT>> { with_eof_check(stream, ParseError::ExpectedSize) };
  42. size_t count = count_or_error.release_value();
  43. Vector<ResultT> entries;
  44. entries.ensure_capacity(count);
  45. for (size_t i = 0; i < count; ++i) {
  46. auto result = T::parse(stream);
  47. if (result.is_error())
  48. return ParseResult<Vector<ResultT>> { result.error() };
  49. entries.append(result.release_value());
  50. }
  51. return ParseResult<Vector<ResultT>> { move(entries) };
  52. } else {
  53. auto count_or_error = stream.read_value<LEB128<u32>>();
  54. if (count_or_error.is_error())
  55. return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
  56. size_t count = count_or_error.release_value();
  57. Vector<T> entries;
  58. entries.ensure_capacity(count);
  59. for (size_t i = 0; i < count; ++i) {
  60. if constexpr (IsSame<T, u32>) {
  61. auto value_or_error = stream.read_value<LEB128<u32>>();
  62. if (value_or_error.is_error())
  63. return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
  64. size_t value = value_or_error.release_value();
  65. entries.append(value);
  66. } else if constexpr (IsSame<T, ssize_t>) {
  67. auto value_or_error = stream.read_value<LEB128<ssize_t>>();
  68. if (value_or_error.is_error())
  69. return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::ExpectedSize) };
  70. ssize_t value = value_or_error.release_value();
  71. entries.append(value);
  72. } else if constexpr (IsSame<T, u8>) {
  73. if (count > Constants::max_allowed_vector_size)
  74. return ParseResult<Vector<T>> { ParseError::HugeAllocationRequested };
  75. entries.resize(count);
  76. if (stream.read_until_filled({ entries.data(), entries.size() }).is_error())
  77. return ParseResult<Vector<T>> { with_eof_check(stream, ParseError::InvalidInput) };
  78. break; // Note: We read this all in one go!
  79. }
  80. }
  81. return ParseResult<Vector<T>> { move(entries) };
  82. }
  83. }
  84. static ParseResult<ByteString> parse_name(Stream& stream)
  85. {
  86. ScopeLogger<WASM_BINPARSER_DEBUG> logger;
  87. auto data = TRY(parse_vector<u8>(stream));
  88. auto string = ByteString::copy(data);
  89. if (!Utf8View(string).validate(Utf8View::AllowSurrogates::No))
  90. return ParseError::InvalidUtf8;
  91. return string;
  92. }
  93. ParseResult<ValueType> ValueType::parse(Stream& stream)
  94. {
  95. ScopeLogger<WASM_BINPARSER_DEBUG> logger("ValueType"sv);
  96. auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  97. switch (tag) {
  98. case Constants::i32_tag:
  99. return ValueType(I32);
  100. case Constants::i64_tag:
  101. return ValueType(I64);
  102. case Constants::f32_tag:
  103. return ValueType(F32);
  104. case Constants::f64_tag:
  105. return ValueType(F64);
  106. case Constants::v128_tag:
  107. return ValueType(V128);
  108. case Constants::function_reference_tag:
  109. return ValueType(FunctionReference);
  110. case Constants::extern_reference_tag:
  111. return ValueType(ExternReference);
  112. default:
  113. return ParseError::InvalidTag;
  114. }
  115. }
  116. ParseResult<ResultType> ResultType::parse(Stream& stream)
  117. {
  118. ScopeLogger<WASM_BINPARSER_DEBUG> logger("ResultType"sv);
  119. auto types = TRY(parse_vector<ValueType>(stream));
  120. return ResultType { types };
  121. }
  122. ParseResult<FunctionType> FunctionType::parse(Stream& stream)
  123. {
  124. ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionType"sv);
  125. auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  126. if (tag != Constants::function_signature_tag) {
  127. dbgln("Expected 0x60, but found {:#x}", tag);
  128. return with_eof_check(stream, ParseError::InvalidTag);
  129. }
  130. auto parameters_result = TRY(parse_vector<ValueType>(stream));
  131. auto results_result = TRY(parse_vector<ValueType>(stream));
  132. return FunctionType { parameters_result, results_result };
  133. }
  134. ParseResult<Limits> Limits::parse(Stream& stream)
  135. {
  136. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Limits"sv);
  137. auto flag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  138. if (flag > 1)
  139. return with_eof_check(stream, ParseError::InvalidTag);
  140. auto min_or_error = stream.read_value<LEB128<u32>>();
  141. if (min_or_error.is_error())
  142. return with_eof_check(stream, ParseError::ExpectedSize);
  143. size_t min = min_or_error.release_value();
  144. Optional<u32> max;
  145. if (flag) {
  146. auto value_or_error = stream.read_value<LEB128<u32>>();
  147. if (value_or_error.is_error())
  148. return with_eof_check(stream, ParseError::ExpectedSize);
  149. max = value_or_error.release_value();
  150. }
  151. return Limits { static_cast<u32>(min), move(max) };
  152. }
  153. ParseResult<MemoryType> MemoryType::parse(Stream& stream)
  154. {
  155. ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemoryType"sv);
  156. auto limits_result = TRY(Limits::parse(stream));
  157. return MemoryType { limits_result };
  158. }
  159. ParseResult<TableType> TableType::parse(Stream& stream)
  160. {
  161. ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableType"sv);
  162. auto type_result = TRY(ValueType::parse(stream));
  163. if (!type_result.is_reference())
  164. return ParseError::InvalidType;
  165. auto limits_result = TRY(Limits::parse(stream));
  166. return TableType { type_result, limits_result };
  167. }
  168. ParseResult<GlobalType> GlobalType::parse(Stream& stream)
  169. {
  170. ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalType"sv);
  171. auto type_result = TRY(ValueType::parse(stream));
  172. auto mutable_ = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  173. if (mutable_ > 1)
  174. return with_eof_check(stream, ParseError::InvalidTag);
  175. return GlobalType { type_result, mutable_ == 0x01 };
  176. }
  177. ParseResult<BlockType> BlockType::parse(Stream& stream)
  178. {
  179. ScopeLogger<WASM_BINPARSER_DEBUG> logger("BlockType"sv);
  180. auto kind = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  181. if (kind == Constants::empty_block_tag)
  182. return BlockType {};
  183. {
  184. FixedMemoryStream value_stream { ReadonlyBytes { &kind, 1 } };
  185. if (auto value_type = ValueType::parse(value_stream); !value_type.is_error())
  186. return BlockType { value_type.release_value() };
  187. }
  188. ReconsumableStream new_stream { stream };
  189. new_stream.unread({ &kind, 1 });
  190. // FIXME: should be an i33. Right now, we're missing a potential last bit at
  191. // the end. See https://webassembly.github.io/spec/core/binary/instructions.html#binary-blocktype
  192. i32 index_value = TRY_READ(new_stream, LEB128<i32>, ParseError::ExpectedIndex);
  193. if (index_value < 0) {
  194. dbgln("Invalid type index {}", index_value);
  195. return with_eof_check(stream, ParseError::InvalidIndex);
  196. }
  197. return BlockType { TypeIndex(index_value) };
  198. }
  199. ParseResult<Instruction> Instruction::parse(Stream& stream)
  200. {
  201. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Instruction"sv);
  202. auto byte = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  203. OpCode opcode { byte };
  204. switch (opcode.value()) {
  205. case Instructions::block.value():
  206. case Instructions::loop.value():
  207. case Instructions::if_.value(): {
  208. auto block_type = TRY(BlockType::parse(stream));
  209. return Instruction {
  210. opcode, StructuredInstructionArgs { block_type, {}, {} }
  211. };
  212. }
  213. case Instructions::br.value():
  214. case Instructions::br_if.value(): {
  215. // branches with a single label immediate
  216. auto index = TRY(GenericIndexParser<LabelIndex>::parse(stream));
  217. return Instruction { opcode, index };
  218. }
  219. case Instructions::br_table.value(): {
  220. // br_table label* label
  221. auto labels = TRY(parse_vector<GenericIndexParser<LabelIndex>>(stream));
  222. auto default_label = TRY(GenericIndexParser<LabelIndex>::parse(stream));
  223. return Instruction { opcode, TableBranchArgs { labels, default_label } };
  224. }
  225. case Instructions::call.value(): {
  226. // call function
  227. auto function_index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
  228. return Instruction { opcode, function_index };
  229. }
  230. case Instructions::call_indirect.value(): {
  231. // call_indirect type table
  232. auto type_index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
  233. auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
  234. return Instruction { opcode, IndirectCallArgs { type_index, table_index } };
  235. }
  236. case Instructions::i32_load.value():
  237. case Instructions::i64_load.value():
  238. case Instructions::f32_load.value():
  239. case Instructions::f64_load.value():
  240. case Instructions::i32_load8_s.value():
  241. case Instructions::i32_load8_u.value():
  242. case Instructions::i32_load16_s.value():
  243. case Instructions::i32_load16_u.value():
  244. case Instructions::i64_load8_s.value():
  245. case Instructions::i64_load8_u.value():
  246. case Instructions::i64_load16_s.value():
  247. case Instructions::i64_load16_u.value():
  248. case Instructions::i64_load32_s.value():
  249. case Instructions::i64_load32_u.value():
  250. case Instructions::i32_store.value():
  251. case Instructions::i64_store.value():
  252. case Instructions::f32_store.value():
  253. case Instructions::f64_store.value():
  254. case Instructions::i32_store8.value():
  255. case Instructions::i32_store16.value():
  256. case Instructions::i64_store8.value():
  257. case Instructions::i64_store16.value():
  258. case Instructions::i64_store32.value(): {
  259. // op (align [multi-memory: memindex] offset)
  260. u32 align = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  261. // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
  262. auto memory_index = 0;
  263. if ((align & 0x40) != 0) {
  264. align &= ~0x40;
  265. memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  266. }
  267. auto offset = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  268. return Instruction { opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } };
  269. }
  270. case Instructions::local_get.value():
  271. case Instructions::local_set.value():
  272. case Instructions::local_tee.value(): {
  273. auto index = TRY(GenericIndexParser<LocalIndex>::parse(stream));
  274. return Instruction { opcode, index };
  275. }
  276. case Instructions::global_get.value():
  277. case Instructions::global_set.value(): {
  278. auto index = TRY(GenericIndexParser<GlobalIndex>::parse(stream));
  279. return Instruction { opcode, index };
  280. }
  281. case Instructions::memory_size.value():
  282. case Instructions::memory_grow.value(): {
  283. // op [multi-memory: memindex]|0x00
  284. auto memory_index = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  285. return Instruction { opcode, MemoryIndexArgument { MemoryIndex(memory_index) } };
  286. }
  287. case Instructions::i32_const.value(): {
  288. auto value = TRY_READ(stream, LEB128<i32>, ParseError::ExpectedSignedImmediate);
  289. return Instruction { opcode, value };
  290. }
  291. case Instructions::i64_const.value(): {
  292. // op literal
  293. auto value = TRY_READ(stream, LEB128<i64>, ParseError::ExpectedSignedImmediate);
  294. return Instruction { opcode, value };
  295. }
  296. case Instructions::f32_const.value(): {
  297. // op literal
  298. auto value = TRY_READ(stream, LittleEndian<u32>, ParseError::ExpectedFloatingImmediate);
  299. auto floating = bit_cast<float>(static_cast<u32>(value));
  300. return Instruction { opcode, floating };
  301. }
  302. case Instructions::f64_const.value(): {
  303. // op literal
  304. auto value = TRY_READ(stream, LittleEndian<u64>, ParseError::ExpectedFloatingImmediate);
  305. auto floating = bit_cast<double>(static_cast<u64>(value));
  306. return Instruction { opcode, floating };
  307. }
  308. case Instructions::table_get.value():
  309. case Instructions::table_set.value(): {
  310. auto index = TRY(GenericIndexParser<TableIndex>::parse(stream));
  311. return Instruction { opcode, index };
  312. }
  313. case Instructions::select_typed.value(): {
  314. auto types = TRY(parse_vector<ValueType>(stream));
  315. return Instruction { opcode, types };
  316. }
  317. case Instructions::ref_null.value(): {
  318. auto type = TRY(ValueType::parse(stream));
  319. if (!type.is_reference())
  320. return ParseError::InvalidType;
  321. return Instruction { opcode, type };
  322. }
  323. case Instructions::ref_func.value(): {
  324. auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
  325. return Instruction { opcode, index };
  326. }
  327. case Instructions::structured_end.value():
  328. case Instructions::structured_else.value():
  329. case Instructions::ref_is_null.value():
  330. case Instructions::unreachable.value():
  331. case Instructions::nop.value():
  332. case Instructions::return_.value():
  333. case Instructions::drop.value():
  334. case Instructions::select.value():
  335. case Instructions::i32_eqz.value():
  336. case Instructions::i32_eq.value():
  337. case Instructions::i32_ne.value():
  338. case Instructions::i32_lts.value():
  339. case Instructions::i32_ltu.value():
  340. case Instructions::i32_gts.value():
  341. case Instructions::i32_gtu.value():
  342. case Instructions::i32_les.value():
  343. case Instructions::i32_leu.value():
  344. case Instructions::i32_ges.value():
  345. case Instructions::i32_geu.value():
  346. case Instructions::i64_eqz.value():
  347. case Instructions::i64_eq.value():
  348. case Instructions::i64_ne.value():
  349. case Instructions::i64_lts.value():
  350. case Instructions::i64_ltu.value():
  351. case Instructions::i64_gts.value():
  352. case Instructions::i64_gtu.value():
  353. case Instructions::i64_les.value():
  354. case Instructions::i64_leu.value():
  355. case Instructions::i64_ges.value():
  356. case Instructions::i64_geu.value():
  357. case Instructions::f32_eq.value():
  358. case Instructions::f32_ne.value():
  359. case Instructions::f32_lt.value():
  360. case Instructions::f32_gt.value():
  361. case Instructions::f32_le.value():
  362. case Instructions::f32_ge.value():
  363. case Instructions::f64_eq.value():
  364. case Instructions::f64_ne.value():
  365. case Instructions::f64_lt.value():
  366. case Instructions::f64_gt.value():
  367. case Instructions::f64_le.value():
  368. case Instructions::f64_ge.value():
  369. case Instructions::i32_clz.value():
  370. case Instructions::i32_ctz.value():
  371. case Instructions::i32_popcnt.value():
  372. case Instructions::i32_add.value():
  373. case Instructions::i32_sub.value():
  374. case Instructions::i32_mul.value():
  375. case Instructions::i32_divs.value():
  376. case Instructions::i32_divu.value():
  377. case Instructions::i32_rems.value():
  378. case Instructions::i32_remu.value():
  379. case Instructions::i32_and.value():
  380. case Instructions::i32_or.value():
  381. case Instructions::i32_xor.value():
  382. case Instructions::i32_shl.value():
  383. case Instructions::i32_shrs.value():
  384. case Instructions::i32_shru.value():
  385. case Instructions::i32_rotl.value():
  386. case Instructions::i32_rotr.value():
  387. case Instructions::i64_clz.value():
  388. case Instructions::i64_ctz.value():
  389. case Instructions::i64_popcnt.value():
  390. case Instructions::i64_add.value():
  391. case Instructions::i64_sub.value():
  392. case Instructions::i64_mul.value():
  393. case Instructions::i64_divs.value():
  394. case Instructions::i64_divu.value():
  395. case Instructions::i64_rems.value():
  396. case Instructions::i64_remu.value():
  397. case Instructions::i64_and.value():
  398. case Instructions::i64_or.value():
  399. case Instructions::i64_xor.value():
  400. case Instructions::i64_shl.value():
  401. case Instructions::i64_shrs.value():
  402. case Instructions::i64_shru.value():
  403. case Instructions::i64_rotl.value():
  404. case Instructions::i64_rotr.value():
  405. case Instructions::f32_abs.value():
  406. case Instructions::f32_neg.value():
  407. case Instructions::f32_ceil.value():
  408. case Instructions::f32_floor.value():
  409. case Instructions::f32_trunc.value():
  410. case Instructions::f32_nearest.value():
  411. case Instructions::f32_sqrt.value():
  412. case Instructions::f32_add.value():
  413. case Instructions::f32_sub.value():
  414. case Instructions::f32_mul.value():
  415. case Instructions::f32_div.value():
  416. case Instructions::f32_min.value():
  417. case Instructions::f32_max.value():
  418. case Instructions::f32_copysign.value():
  419. case Instructions::f64_abs.value():
  420. case Instructions::f64_neg.value():
  421. case Instructions::f64_ceil.value():
  422. case Instructions::f64_floor.value():
  423. case Instructions::f64_trunc.value():
  424. case Instructions::f64_nearest.value():
  425. case Instructions::f64_sqrt.value():
  426. case Instructions::f64_add.value():
  427. case Instructions::f64_sub.value():
  428. case Instructions::f64_mul.value():
  429. case Instructions::f64_div.value():
  430. case Instructions::f64_min.value():
  431. case Instructions::f64_max.value():
  432. case Instructions::f64_copysign.value():
  433. case Instructions::i32_wrap_i64.value():
  434. case Instructions::i32_trunc_sf32.value():
  435. case Instructions::i32_trunc_uf32.value():
  436. case Instructions::i32_trunc_sf64.value():
  437. case Instructions::i32_trunc_uf64.value():
  438. case Instructions::i64_extend_si32.value():
  439. case Instructions::i64_extend_ui32.value():
  440. case Instructions::i64_trunc_sf32.value():
  441. case Instructions::i64_trunc_uf32.value():
  442. case Instructions::i64_trunc_sf64.value():
  443. case Instructions::i64_trunc_uf64.value():
  444. case Instructions::f32_convert_si32.value():
  445. case Instructions::f32_convert_ui32.value():
  446. case Instructions::f32_convert_si64.value():
  447. case Instructions::f32_convert_ui64.value():
  448. case Instructions::f32_demote_f64.value():
  449. case Instructions::f64_convert_si32.value():
  450. case Instructions::f64_convert_ui32.value():
  451. case Instructions::f64_convert_si64.value():
  452. case Instructions::f64_convert_ui64.value():
  453. case Instructions::f64_promote_f32.value():
  454. case Instructions::i32_reinterpret_f32.value():
  455. case Instructions::i64_reinterpret_f64.value():
  456. case Instructions::f32_reinterpret_i32.value():
  457. case Instructions::f64_reinterpret_i64.value():
  458. case Instructions::i32_extend8_s.value():
  459. case Instructions::i32_extend16_s.value():
  460. case Instructions::i64_extend8_s.value():
  461. case Instructions::i64_extend16_s.value():
  462. case Instructions::i64_extend32_s.value():
  463. return Instruction { opcode };
  464. case 0xfc:
  465. case 0xfd: {
  466. // These are multibyte instructions.
  467. auto selector = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  468. OpCode full_opcode = static_cast<u64>(opcode.value()) << 56 | selector;
  469. switch (full_opcode.value()) {
  470. case Instructions::i32_trunc_sat_f32_s.value():
  471. case Instructions::i32_trunc_sat_f32_u.value():
  472. case Instructions::i32_trunc_sat_f64_s.value():
  473. case Instructions::i32_trunc_sat_f64_u.value():
  474. case Instructions::i64_trunc_sat_f32_s.value():
  475. case Instructions::i64_trunc_sat_f32_u.value():
  476. case Instructions::i64_trunc_sat_f64_s.value():
  477. case Instructions::i64_trunc_sat_f64_u.value():
  478. return Instruction { full_opcode };
  479. case Instructions::memory_init.value(): {
  480. auto index = TRY(GenericIndexParser<DataIndex>::parse(stream));
  481. // Proposal "multi-memory", literal 0x00 is replaced with a memory index.
  482. auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput);
  483. return Instruction { full_opcode, MemoryInitArgs { index, MemoryIndex(memory_index) } };
  484. }
  485. case Instructions::data_drop.value(): {
  486. auto index = TRY(GenericIndexParser<DataIndex>::parse(stream));
  487. return Instruction { full_opcode, index };
  488. }
  489. case Instructions::memory_copy.value(): {
  490. // Proposal "multi-memory", literal 0x00 is replaced with two memory indices, destination and source, respectively.
  491. MemoryIndex indices[] = { 0, 0 };
  492. for (size_t i = 0; i < 2; ++i) {
  493. auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput);
  494. indices[i] = memory_index;
  495. }
  496. return Instruction { full_opcode, MemoryCopyArgs { indices[1], indices[0] } };
  497. }
  498. case Instructions::memory_fill.value(): {
  499. // Proposal "multi-memory", literal 0x00 is replaced with a memory index.
  500. auto memory_index = TRY_READ(stream, u8, ParseError::InvalidInput);
  501. return Instruction { full_opcode, MemoryIndexArgument { MemoryIndex { memory_index } } };
  502. }
  503. case Instructions::table_init.value(): {
  504. auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream));
  505. auto table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
  506. return Instruction { full_opcode, TableElementArgs { element_index, table_index } };
  507. }
  508. case Instructions::elem_drop.value(): {
  509. auto element_index = TRY(GenericIndexParser<ElementIndex>::parse(stream));
  510. return Instruction { full_opcode, element_index };
  511. }
  512. case Instructions::table_copy.value(): {
  513. auto lhs = TRY(GenericIndexParser<TableIndex>::parse(stream));
  514. auto rhs = TRY(GenericIndexParser<TableIndex>::parse(stream));
  515. return Instruction { full_opcode, TableTableArgs { lhs, rhs } };
  516. }
  517. case Instructions::table_grow.value():
  518. case Instructions::table_size.value():
  519. case Instructions::table_fill.value(): {
  520. auto index = TRY(GenericIndexParser<TableIndex>::parse(stream));
  521. return Instruction { full_opcode, index };
  522. }
  523. case Instructions::v128_load.value():
  524. case Instructions::v128_load8x8_s.value():
  525. case Instructions::v128_load8x8_u.value():
  526. case Instructions::v128_load16x4_s.value():
  527. case Instructions::v128_load16x4_u.value():
  528. case Instructions::v128_load32x2_s.value():
  529. case Instructions::v128_load32x2_u.value():
  530. case Instructions::v128_load8_splat.value():
  531. case Instructions::v128_load16_splat.value():
  532. case Instructions::v128_load32_splat.value():
  533. case Instructions::v128_load64_splat.value():
  534. case Instructions::v128_load32_zero.value():
  535. case Instructions::v128_load64_zero.value():
  536. case Instructions::v128_store.value(): {
  537. // op (align [multi-memory memindex] offset)
  538. u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
  539. // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
  540. auto memory_index = 0;
  541. if ((align & 0x20) != 0) {
  542. align &= ~0x20;
  543. memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  544. }
  545. auto offset = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
  546. return Instruction { full_opcode, MemoryArgument { align, offset, MemoryIndex(memory_index) } };
  547. }
  548. case Instructions::v128_load8_lane.value():
  549. case Instructions::v128_load16_lane.value():
  550. case Instructions::v128_load32_lane.value():
  551. case Instructions::v128_load64_lane.value():
  552. case Instructions::v128_store8_lane.value():
  553. case Instructions::v128_store16_lane.value():
  554. case Instructions::v128_store32_lane.value():
  555. case Instructions::v128_store64_lane.value(): {
  556. // op (align [multi-memory: memindex] offset) (index)
  557. u32 align = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
  558. // Proposal "multi-memory", if bit 6 of alignment is set, then a memory index follows the alignment.
  559. auto memory_index = 0;
  560. if ((align & 0x20) != 0) {
  561. align &= ~0x20;
  562. memory_index = TRY_READ(stream, LEB128<u32>, ParseError::InvalidInput);
  563. }
  564. auto offset = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
  565. auto index = TRY_READ(stream, u8, ParseError::InvalidInput);
  566. return Instruction { full_opcode, MemoryAndLaneArgument { { align, offset, MemoryIndex(memory_index) }, index } };
  567. }
  568. case Instructions::v128_const.value(): {
  569. // op (literal:16)
  570. auto value = TRY_READ(stream, LittleEndian<u128>, ParseError::InvalidImmediate);
  571. return Instruction { full_opcode, value };
  572. }
  573. case Instructions::i8x16_shuffle.value(): {
  574. // op 16x(lane)
  575. u8 lanes[16];
  576. for (size_t i = 0; i < 16; ++i) {
  577. auto value = TRY_READ(stream, u8, ParseError::InvalidInput);
  578. lanes[i] = value;
  579. }
  580. return Instruction { full_opcode, ShuffleArgument(lanes) };
  581. }
  582. case Instructions::i8x16_extract_lane_s.value():
  583. case Instructions::i8x16_extract_lane_u.value():
  584. case Instructions::i8x16_replace_lane.value():
  585. case Instructions::i16x8_extract_lane_s.value():
  586. case Instructions::i16x8_extract_lane_u.value():
  587. case Instructions::i16x8_replace_lane.value():
  588. case Instructions::i32x4_extract_lane.value():
  589. case Instructions::i32x4_replace_lane.value():
  590. case Instructions::i64x2_extract_lane.value():
  591. case Instructions::i64x2_replace_lane.value():
  592. case Instructions::f32x4_extract_lane.value():
  593. case Instructions::f32x4_replace_lane.value():
  594. case Instructions::f64x2_extract_lane.value():
  595. case Instructions::f64x2_replace_lane.value(): {
  596. // op (lane)
  597. auto lane = TRY_READ(stream, u8, ParseError::InvalidInput);
  598. return Instruction { full_opcode, LaneIndex { lane } };
  599. }
  600. case Instructions::i8x16_swizzle.value():
  601. case Instructions::i8x16_splat.value():
  602. case Instructions::i16x8_splat.value():
  603. case Instructions::i32x4_splat.value():
  604. case Instructions::i64x2_splat.value():
  605. case Instructions::f32x4_splat.value():
  606. case Instructions::f64x2_splat.value():
  607. case Instructions::i8x16_eq.value():
  608. case Instructions::i8x16_ne.value():
  609. case Instructions::i8x16_lt_s.value():
  610. case Instructions::i8x16_lt_u.value():
  611. case Instructions::i8x16_gt_s.value():
  612. case Instructions::i8x16_gt_u.value():
  613. case Instructions::i8x16_le_s.value():
  614. case Instructions::i8x16_le_u.value():
  615. case Instructions::i8x16_ge_s.value():
  616. case Instructions::i8x16_ge_u.value():
  617. case Instructions::i16x8_eq.value():
  618. case Instructions::i16x8_ne.value():
  619. case Instructions::i16x8_lt_s.value():
  620. case Instructions::i16x8_lt_u.value():
  621. case Instructions::i16x8_gt_s.value():
  622. case Instructions::i16x8_gt_u.value():
  623. case Instructions::i16x8_le_s.value():
  624. case Instructions::i16x8_le_u.value():
  625. case Instructions::i16x8_ge_s.value():
  626. case Instructions::i16x8_ge_u.value():
  627. case Instructions::i32x4_eq.value():
  628. case Instructions::i32x4_ne.value():
  629. case Instructions::i32x4_lt_s.value():
  630. case Instructions::i32x4_lt_u.value():
  631. case Instructions::i32x4_gt_s.value():
  632. case Instructions::i32x4_gt_u.value():
  633. case Instructions::i32x4_le_s.value():
  634. case Instructions::i32x4_le_u.value():
  635. case Instructions::i32x4_ge_s.value():
  636. case Instructions::i32x4_ge_u.value():
  637. case Instructions::f32x4_eq.value():
  638. case Instructions::f32x4_ne.value():
  639. case Instructions::f32x4_lt.value():
  640. case Instructions::f32x4_gt.value():
  641. case Instructions::f32x4_le.value():
  642. case Instructions::f32x4_ge.value():
  643. case Instructions::f64x2_eq.value():
  644. case Instructions::f64x2_ne.value():
  645. case Instructions::f64x2_lt.value():
  646. case Instructions::f64x2_gt.value():
  647. case Instructions::f64x2_le.value():
  648. case Instructions::f64x2_ge.value():
  649. case Instructions::v128_not.value():
  650. case Instructions::v128_and.value():
  651. case Instructions::v128_andnot.value():
  652. case Instructions::v128_or.value():
  653. case Instructions::v128_xor.value():
  654. case Instructions::v128_bitselect.value():
  655. case Instructions::v128_any_true.value():
  656. case Instructions::f32x4_demote_f64x2_zero.value():
  657. case Instructions::f64x2_promote_low_f32x4.value():
  658. case Instructions::i8x16_abs.value():
  659. case Instructions::i8x16_neg.value():
  660. case Instructions::i8x16_popcnt.value():
  661. case Instructions::i8x16_all_true.value():
  662. case Instructions::i8x16_bitmask.value():
  663. case Instructions::i8x16_narrow_i16x8_s.value():
  664. case Instructions::i8x16_narrow_i16x8_u.value():
  665. case Instructions::f32x4_ceil.value():
  666. case Instructions::f32x4_floor.value():
  667. case Instructions::f32x4_trunc.value():
  668. case Instructions::f32x4_nearest.value():
  669. case Instructions::i8x16_shl.value():
  670. case Instructions::i8x16_shr_s.value():
  671. case Instructions::i8x16_shr_u.value():
  672. case Instructions::i8x16_add.value():
  673. case Instructions::i8x16_add_sat_s.value():
  674. case Instructions::i8x16_add_sat_u.value():
  675. case Instructions::i8x16_sub.value():
  676. case Instructions::i8x16_sub_sat_s.value():
  677. case Instructions::i8x16_sub_sat_u.value():
  678. case Instructions::f64x2_ceil.value():
  679. case Instructions::f64x2_floor.value():
  680. case Instructions::i8x16_min_s.value():
  681. case Instructions::i8x16_min_u.value():
  682. case Instructions::i8x16_max_s.value():
  683. case Instructions::i8x16_max_u.value():
  684. case Instructions::f64x2_trunc.value():
  685. case Instructions::i8x16_avgr_u.value():
  686. case Instructions::i16x8_extadd_pairwise_i8x16_s.value():
  687. case Instructions::i16x8_extadd_pairwise_i8x16_u.value():
  688. case Instructions::i32x4_extadd_pairwise_i16x8_s.value():
  689. case Instructions::i32x4_extadd_pairwise_i16x8_u.value():
  690. case Instructions::i16x8_abs.value():
  691. case Instructions::i16x8_neg.value():
  692. case Instructions::i16x8_q15mulr_sat_s.value():
  693. case Instructions::i16x8_all_true.value():
  694. case Instructions::i16x8_bitmask.value():
  695. case Instructions::i16x8_narrow_i32x4_s.value():
  696. case Instructions::i16x8_narrow_i32x4_u.value():
  697. case Instructions::i16x8_extend_low_i8x16_s.value():
  698. case Instructions::i16x8_extend_high_i8x16_s.value():
  699. case Instructions::i16x8_extend_low_i8x16_u.value():
  700. case Instructions::i16x8_extend_high_i8x16_u.value():
  701. case Instructions::i16x8_shl.value():
  702. case Instructions::i16x8_shr_s.value():
  703. case Instructions::i16x8_shr_u.value():
  704. case Instructions::i16x8_add.value():
  705. case Instructions::i16x8_add_sat_s.value():
  706. case Instructions::i16x8_add_sat_u.value():
  707. case Instructions::i16x8_sub.value():
  708. case Instructions::i16x8_sub_sat_s.value():
  709. case Instructions::i16x8_sub_sat_u.value():
  710. case Instructions::f64x2_nearest.value():
  711. case Instructions::i16x8_mul.value():
  712. case Instructions::i16x8_min_s.value():
  713. case Instructions::i16x8_min_u.value():
  714. case Instructions::i16x8_max_s.value():
  715. case Instructions::i16x8_max_u.value():
  716. case Instructions::i16x8_avgr_u.value():
  717. case Instructions::i16x8_extmul_low_i8x16_s.value():
  718. case Instructions::i16x8_extmul_high_i8x16_s.value():
  719. case Instructions::i16x8_extmul_low_i8x16_u.value():
  720. case Instructions::i16x8_extmul_high_i8x16_u.value():
  721. case Instructions::i32x4_abs.value():
  722. case Instructions::i32x4_neg.value():
  723. case Instructions::i32x4_all_true.value():
  724. case Instructions::i32x4_bitmask.value():
  725. case Instructions::i32x4_extend_low_i16x8_s.value():
  726. case Instructions::i32x4_extend_high_i16x8_s.value():
  727. case Instructions::i32x4_extend_low_i16x8_u.value():
  728. case Instructions::i32x4_extend_high_i16x8_u.value():
  729. case Instructions::i32x4_shl.value():
  730. case Instructions::i32x4_shr_s.value():
  731. case Instructions::i32x4_shr_u.value():
  732. case Instructions::i32x4_add.value():
  733. case Instructions::i32x4_sub.value():
  734. case Instructions::i32x4_mul.value():
  735. case Instructions::i32x4_min_s.value():
  736. case Instructions::i32x4_min_u.value():
  737. case Instructions::i32x4_max_s.value():
  738. case Instructions::i32x4_max_u.value():
  739. case Instructions::i32x4_dot_i16x8_s.value():
  740. case Instructions::i32x4_extmul_low_i16x8_s.value():
  741. case Instructions::i32x4_extmul_high_i16x8_s.value():
  742. case Instructions::i32x4_extmul_low_i16x8_u.value():
  743. case Instructions::i32x4_extmul_high_i16x8_u.value():
  744. case Instructions::i64x2_abs.value():
  745. case Instructions::i64x2_neg.value():
  746. case Instructions::i64x2_all_true.value():
  747. case Instructions::i64x2_bitmask.value():
  748. case Instructions::i64x2_extend_low_i32x4_s.value():
  749. case Instructions::i64x2_extend_high_i32x4_s.value():
  750. case Instructions::i64x2_extend_low_i32x4_u.value():
  751. case Instructions::i64x2_extend_high_i32x4_u.value():
  752. case Instructions::i64x2_shl.value():
  753. case Instructions::i64x2_shr_s.value():
  754. case Instructions::i64x2_shr_u.value():
  755. case Instructions::i64x2_add.value():
  756. case Instructions::i64x2_sub.value():
  757. case Instructions::i64x2_mul.value():
  758. case Instructions::i64x2_eq.value():
  759. case Instructions::i64x2_ne.value():
  760. case Instructions::i64x2_lt_s.value():
  761. case Instructions::i64x2_gt_s.value():
  762. case Instructions::i64x2_le_s.value():
  763. case Instructions::i64x2_ge_s.value():
  764. case Instructions::i64x2_extmul_low_i32x4_s.value():
  765. case Instructions::i64x2_extmul_high_i32x4_s.value():
  766. case Instructions::i64x2_extmul_low_i32x4_u.value():
  767. case Instructions::i64x2_extmul_high_i32x4_u.value():
  768. case Instructions::f32x4_abs.value():
  769. case Instructions::f32x4_neg.value():
  770. case Instructions::f32x4_sqrt.value():
  771. case Instructions::f32x4_add.value():
  772. case Instructions::f32x4_sub.value():
  773. case Instructions::f32x4_mul.value():
  774. case Instructions::f32x4_div.value():
  775. case Instructions::f32x4_min.value():
  776. case Instructions::f32x4_max.value():
  777. case Instructions::f32x4_pmin.value():
  778. case Instructions::f32x4_pmax.value():
  779. case Instructions::f64x2_abs.value():
  780. case Instructions::f64x2_neg.value():
  781. case Instructions::f64x2_sqrt.value():
  782. case Instructions::f64x2_add.value():
  783. case Instructions::f64x2_sub.value():
  784. case Instructions::f64x2_mul.value():
  785. case Instructions::f64x2_div.value():
  786. case Instructions::f64x2_min.value():
  787. case Instructions::f64x2_max.value():
  788. case Instructions::f64x2_pmin.value():
  789. case Instructions::f64x2_pmax.value():
  790. case Instructions::i32x4_trunc_sat_f32x4_s.value():
  791. case Instructions::i32x4_trunc_sat_f32x4_u.value():
  792. case Instructions::f32x4_convert_i32x4_s.value():
  793. case Instructions::f32x4_convert_i32x4_u.value():
  794. case Instructions::i32x4_trunc_sat_f64x2_s_zero.value():
  795. case Instructions::i32x4_trunc_sat_f64x2_u_zero.value():
  796. case Instructions::f64x2_convert_low_i32x4_s.value():
  797. case Instructions::f64x2_convert_low_i32x4_u.value():
  798. // op
  799. return Instruction { full_opcode };
  800. default:
  801. return ParseError::UnknownInstruction;
  802. }
  803. }
  804. }
  805. return ParseError::UnknownInstruction;
  806. }
  807. ParseResult<CustomSection> CustomSection::parse(Stream& stream)
  808. {
  809. ScopeLogger<WASM_BINPARSER_DEBUG> logger("CustomSection"sv);
  810. auto name = TRY(parse_name(stream));
  811. ByteBuffer data_buffer;
  812. if (data_buffer.try_resize(64).is_error())
  813. return ParseError::OutOfMemory;
  814. while (!stream.is_eof()) {
  815. char buf[16];
  816. auto span_or_error = stream.read_some({ buf, 16 });
  817. if (span_or_error.is_error())
  818. break;
  819. auto size = span_or_error.release_value().size();
  820. if (size == 0)
  821. break;
  822. if (data_buffer.try_append(buf, size).is_error())
  823. return ParseError::HugeAllocationRequested;
  824. }
  825. return CustomSection(name, move(data_buffer));
  826. }
  827. ParseResult<TypeSection> TypeSection::parse(Stream& stream)
  828. {
  829. ScopeLogger<WASM_BINPARSER_DEBUG> logger("TypeSection"sv);
  830. auto types = TRY(parse_vector<FunctionType>(stream));
  831. return TypeSection { types };
  832. }
  833. ParseResult<ImportSection::Import> ImportSection::Import::parse(Stream& stream)
  834. {
  835. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Import"sv);
  836. auto module = TRY(parse_name(stream));
  837. auto name = TRY(parse_name(stream));
  838. auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  839. switch (tag) {
  840. case Constants::extern_function_tag: {
  841. auto index = TRY(GenericIndexParser<TypeIndex>::parse(stream));
  842. return Import { module, name, index };
  843. }
  844. case Constants::extern_table_tag:
  845. return parse_with_type<TableType>(stream, module, name);
  846. case Constants::extern_memory_tag:
  847. return parse_with_type<MemoryType>(stream, module, name);
  848. case Constants::extern_global_tag:
  849. return parse_with_type<GlobalType>(stream, module, name);
  850. default:
  851. return ParseError::InvalidTag;
  852. }
  853. }
  854. ParseResult<ImportSection> ImportSection::parse(Stream& stream)
  855. {
  856. ScopeLogger<WASM_BINPARSER_DEBUG> logger("ImportSection"sv);
  857. auto imports = TRY(parse_vector<Import>(stream));
  858. return ImportSection { imports };
  859. }
  860. ParseResult<FunctionSection> FunctionSection::parse(Stream& stream)
  861. {
  862. ScopeLogger<WASM_BINPARSER_DEBUG> logger("FunctionSection"sv);
  863. auto indices = TRY(parse_vector<u32>(stream));
  864. Vector<TypeIndex> typed_indices;
  865. typed_indices.ensure_capacity(indices.size());
  866. for (auto entry : indices)
  867. typed_indices.append(entry);
  868. return FunctionSection { move(typed_indices) };
  869. }
  870. ParseResult<TableSection::Table> TableSection::Table::parse(Stream& stream)
  871. {
  872. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Table"sv);
  873. auto type = TRY(TableType::parse(stream));
  874. return Table { type };
  875. }
  876. ParseResult<TableSection> TableSection::parse(Stream& stream)
  877. {
  878. ScopeLogger<WASM_BINPARSER_DEBUG> logger("TableSection"sv);
  879. auto tables = TRY(parse_vector<Table>(stream));
  880. return TableSection { tables };
  881. }
  882. ParseResult<MemorySection::Memory> MemorySection::Memory::parse(Stream& stream)
  883. {
  884. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Memory"sv);
  885. auto type = TRY(MemoryType::parse(stream));
  886. return Memory { type };
  887. }
  888. ParseResult<MemorySection> MemorySection::parse(Stream& stream)
  889. {
  890. ScopeLogger<WASM_BINPARSER_DEBUG> logger("MemorySection"sv);
  891. auto memories = TRY(parse_vector<Memory>(stream));
  892. return MemorySection { memories };
  893. }
  894. ParseResult<Expression> Expression::parse(Stream& stream, Optional<size_t> size_hint)
  895. {
  896. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv);
  897. InstructionPointer ip { 0 };
  898. Vector<InstructionPointer> stack;
  899. Vector<Instruction> instructions;
  900. if (size_hint.has_value())
  901. instructions.ensure_capacity(size_hint.release_value());
  902. while (true) {
  903. auto instruction = TRY(Instruction::parse(stream));
  904. switch (instruction.opcode().value()) {
  905. case Instructions::block.value():
  906. case Instructions::loop.value():
  907. case Instructions::if_.value():
  908. stack.append(ip);
  909. break;
  910. case Instructions::structured_end.value(): {
  911. if (stack.is_empty())
  912. return Expression { move(instructions) };
  913. auto entry = stack.take_last();
  914. auto& args = instructions[entry.value()].arguments().get<Instruction::StructuredInstructionArgs>();
  915. // Patch the end_ip of the last structured instruction
  916. args.end_ip = ip + (args.else_ip.has_value() ? 1 : 0);
  917. break;
  918. }
  919. case Instructions::structured_else.value(): {
  920. if (stack.is_empty())
  921. return ParseError::UnknownInstruction;
  922. auto entry = stack.last();
  923. auto& args = instructions[entry.value()].arguments().get<Instruction::StructuredInstructionArgs>();
  924. args.else_ip = ip + 1;
  925. break;
  926. }
  927. }
  928. instructions.append(move(instruction));
  929. ++ip;
  930. }
  931. return Expression { move(instructions) };
  932. }
  933. ParseResult<GlobalSection::Global> GlobalSection::Global::parse(Stream& stream)
  934. {
  935. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Global"sv);
  936. auto type = TRY(GlobalType::parse(stream));
  937. auto exprs = TRY(Expression::parse(stream));
  938. return Global { type, exprs };
  939. }
  940. ParseResult<GlobalSection> GlobalSection::parse(Stream& stream)
  941. {
  942. ScopeLogger<WASM_BINPARSER_DEBUG> logger("GlobalSection"sv);
  943. auto result = TRY(parse_vector<Global>(stream));
  944. return GlobalSection { result };
  945. }
  946. ParseResult<ExportSection::Export> ExportSection::Export::parse(Stream& stream)
  947. {
  948. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Export"sv);
  949. auto name = TRY(parse_name(stream));
  950. auto tag = TRY_READ(stream, u8, ParseError::ExpectedKindTag);
  951. auto index = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedIndex);
  952. switch (tag) {
  953. case Constants::extern_function_tag:
  954. return Export { name, ExportDesc { FunctionIndex { index } } };
  955. case Constants::extern_table_tag:
  956. return Export { name, ExportDesc { TableIndex { index } } };
  957. case Constants::extern_memory_tag:
  958. return Export { name, ExportDesc { MemoryIndex { index } } };
  959. case Constants::extern_global_tag:
  960. return Export { name, ExportDesc { GlobalIndex { index } } };
  961. default:
  962. return ParseError::InvalidTag;
  963. }
  964. }
  965. ParseResult<ExportSection> ExportSection::parse(Stream& stream)
  966. {
  967. ScopeLogger<WASM_BINPARSER_DEBUG> logger("ExportSection"sv);
  968. auto result = TRY(parse_vector<Export>(stream));
  969. return ExportSection { result };
  970. }
  971. ParseResult<StartSection::StartFunction> StartSection::StartFunction::parse(Stream& stream)
  972. {
  973. ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartFunction"sv);
  974. auto index = TRY(GenericIndexParser<FunctionIndex>::parse(stream));
  975. return StartFunction { index };
  976. }
  977. ParseResult<StartSection> StartSection::parse(Stream& stream)
  978. {
  979. ScopeLogger<WASM_BINPARSER_DEBUG> logger("StartSection"sv);
  980. auto result = TRY(StartFunction::parse(stream));
  981. return StartSection { result };
  982. }
  983. ParseResult<ElementSection::Element> ElementSection::Element::parse(Stream& stream)
  984. {
  985. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Element"sv);
  986. auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag);
  987. if (tag > 0x07)
  988. return ParseError::InvalidTag;
  989. auto has_passive = (tag & 0x01) != 0;
  990. auto has_explicit_index = (tag & 0x02) != 0;
  991. auto has_exprs = (tag & 0x04) != 0;
  992. Variant<Active, Passive, Declarative> mode = Passive {};
  993. if (has_passive) {
  994. if (has_explicit_index) {
  995. mode = Declarative {};
  996. } else {
  997. mode = Passive {};
  998. }
  999. } else {
  1000. TableIndex table_index = 0;
  1001. if (has_explicit_index)
  1002. table_index = TRY(GenericIndexParser<TableIndex>::parse(stream));
  1003. auto expression = TRY(Expression::parse(stream));
  1004. mode = Active { table_index, expression };
  1005. }
  1006. auto type = ValueType(ValueType::FunctionReference);
  1007. if (has_passive || has_explicit_index) {
  1008. if (has_exprs) {
  1009. type = TRY(ValueType::parse(stream));
  1010. } else {
  1011. auto extern_ = TRY_READ(stream, u8, ParseError::InvalidType);
  1012. // Make sure that this is a function, as it's technically only the
  1013. // allowed one.
  1014. if (extern_ != 0x00) {
  1015. return ParseError::InvalidType;
  1016. }
  1017. type = ValueType(ValueType::FunctionReference);
  1018. }
  1019. }
  1020. Vector<Expression> items;
  1021. if (!has_exprs) {
  1022. auto indices = TRY(parse_vector<GenericIndexParser<FunctionIndex>>(stream));
  1023. for (auto& index : indices) {
  1024. Vector<Instruction> instructions { Instruction(Instructions::ref_func, index) };
  1025. items.empend(move(instructions));
  1026. }
  1027. } else {
  1028. items = TRY(parse_vector<Expression>(stream));
  1029. }
  1030. return Element { type, move(items), move(mode) };
  1031. }
  1032. ParseResult<ElementSection> ElementSection::parse(Stream& stream)
  1033. {
  1034. ScopeLogger<WASM_BINPARSER_DEBUG> logger("ElementSection"sv);
  1035. auto result = TRY(parse_vector<Element>(stream));
  1036. return ElementSection { result };
  1037. }
  1038. ParseResult<Locals> Locals::parse(Stream& stream)
  1039. {
  1040. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Locals"sv);
  1041. auto count = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize);
  1042. if (count > Constants::max_allowed_function_locals_per_type)
  1043. return ParseError::HugeAllocationRequested;
  1044. auto type = TRY(ValueType::parse(stream));
  1045. return Locals { count, type };
  1046. }
  1047. ParseResult<CodeSection::Func> CodeSection::Func::parse(Stream& stream, size_t size_hint)
  1048. {
  1049. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv);
  1050. auto locals = TRY(parse_vector<Locals>(stream));
  1051. auto body = TRY(Expression::parse(stream, size_hint));
  1052. return Func { move(locals), move(body) };
  1053. }
  1054. ParseResult<CodeSection::Code> CodeSection::Code::parse(Stream& stream)
  1055. {
  1056. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Code"sv);
  1057. auto size = TRY_READ(stream, LEB128<u32>, ParseError::InvalidSize);
  1058. // Emprically, if there are `size` bytes to be read, then there's around
  1059. // `size / 2` instructions, so we pass that as our size hint.
  1060. auto func = TRY(Func::parse(stream, size / 2));
  1061. return Code { size, move(func) };
  1062. }
  1063. ParseResult<CodeSection> CodeSection::parse(Stream& stream)
  1064. {
  1065. ScopeLogger<WASM_BINPARSER_DEBUG> logger("CodeSection"sv);
  1066. auto result = TRY(parse_vector<Code>(stream));
  1067. return CodeSection { move(result) };
  1068. }
  1069. ParseResult<DataSection::Data> DataSection::Data::parse(Stream& stream)
  1070. {
  1071. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Data"sv);
  1072. auto tag = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedKindTag);
  1073. if (tag > 0x02)
  1074. return ParseError::InvalidTag;
  1075. if (tag == 0x00) {
  1076. auto expr = TRY(Expression::parse(stream));
  1077. auto init = TRY(parse_vector<u8>(stream));
  1078. return Data { Active { init, { 0 }, expr } };
  1079. }
  1080. if (tag == 0x01) {
  1081. auto init = TRY(parse_vector<u8>(stream));
  1082. return Data { Passive { init } };
  1083. }
  1084. if (tag == 0x02) {
  1085. auto index = TRY(GenericIndexParser<MemoryIndex>::parse(stream));
  1086. auto expr = TRY(Expression::parse(stream));
  1087. auto init = TRY(parse_vector<u8>(stream));
  1088. return Data { Active { init, index, expr } };
  1089. }
  1090. VERIFY_NOT_REACHED();
  1091. }
  1092. ParseResult<DataSection> DataSection::parse(Stream& stream)
  1093. {
  1094. ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataSection"sv);
  1095. auto data = TRY(parse_vector<Data>(stream));
  1096. return DataSection { data };
  1097. }
  1098. ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] Stream& stream)
  1099. {
  1100. ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataCountSection"sv);
  1101. auto value_or_error = stream.read_value<LEB128<u32>>();
  1102. if (value_or_error.is_error()) {
  1103. if (stream.is_eof()) {
  1104. // The section simply didn't contain anything.
  1105. return DataCountSection { {} };
  1106. }
  1107. return ParseError::ExpectedSize;
  1108. }
  1109. u32 value = value_or_error.release_value();
  1110. return DataCountSection { value };
  1111. }
  1112. ParseResult<SectionId> SectionId::parse(Stream& stream)
  1113. {
  1114. u8 id = TRY_READ(stream, u8, ParseError::ExpectedIndex);
  1115. switch (id) {
  1116. case 0x00:
  1117. return SectionId(SectionIdKind::Custom);
  1118. case 0x01:
  1119. return SectionId(SectionIdKind::Type);
  1120. case 0x02:
  1121. return SectionId(SectionIdKind::Import);
  1122. case 0x03:
  1123. return SectionId(SectionIdKind::Function);
  1124. case 0x04:
  1125. return SectionId(SectionIdKind::Table);
  1126. case 0x05:
  1127. return SectionId(SectionIdKind::Memory);
  1128. case 0x06:
  1129. return SectionId(SectionIdKind::Global);
  1130. case 0x07:
  1131. return SectionId(SectionIdKind::Export);
  1132. case 0x08:
  1133. return SectionId(SectionIdKind::Start);
  1134. case 0x09:
  1135. return SectionId(SectionIdKind::Element);
  1136. case 0x0a:
  1137. return SectionId(SectionIdKind::Code);
  1138. case 0x0b:
  1139. return SectionId(SectionIdKind::Data);
  1140. case 0x0c:
  1141. return SectionId(SectionIdKind::DataCount);
  1142. default:
  1143. return ParseError::InvalidIndex;
  1144. }
  1145. }
  1146. ParseResult<NonnullRefPtr<Module>> Module::parse(Stream& stream)
  1147. {
  1148. ScopeLogger<WASM_BINPARSER_DEBUG> logger("Module"sv);
  1149. u8 buf[4];
  1150. if (stream.read_until_filled({ buf, 4 }).is_error())
  1151. return with_eof_check(stream, ParseError::InvalidInput);
  1152. if (Bytes { buf, 4 } != wasm_magic.span())
  1153. return with_eof_check(stream, ParseError::InvalidModuleMagic);
  1154. if (stream.read_until_filled({ buf, 4 }).is_error())
  1155. return with_eof_check(stream, ParseError::InvalidInput);
  1156. if (Bytes { buf, 4 } != wasm_version.span())
  1157. return with_eof_check(stream, ParseError::InvalidModuleVersion);
  1158. auto last_section_id = SectionId::SectionIdKind::Custom;
  1159. auto module_ptr = make_ref_counted<Module>();
  1160. auto& module = *module_ptr;
  1161. while (!stream.is_eof()) {
  1162. auto section_id = TRY(SectionId::parse(stream));
  1163. size_t section_size = TRY_READ(stream, LEB128<u32>, ParseError::ExpectedSize);
  1164. auto section_stream = ConstrainedStream { MaybeOwned<Stream>(stream), section_size };
  1165. if (section_id.kind() != SectionId::SectionIdKind::Custom && section_id.kind() == last_section_id)
  1166. return ParseError::DuplicateSection;
  1167. switch (section_id.kind()) {
  1168. case SectionId::SectionIdKind::Custom:
  1169. module.custom_sections().append(TRY(CustomSection::parse(section_stream)));
  1170. break;
  1171. case SectionId::SectionIdKind::Type:
  1172. module.type_section() = TRY(TypeSection::parse(section_stream));
  1173. break;
  1174. case SectionId::SectionIdKind::Import:
  1175. module.import_section() = TRY(ImportSection::parse(section_stream));
  1176. break;
  1177. case SectionId::SectionIdKind::Function:
  1178. module.function_section() = TRY(FunctionSection::parse(section_stream));
  1179. break;
  1180. case SectionId::SectionIdKind::Table:
  1181. module.table_section() = TRY(TableSection::parse(section_stream));
  1182. break;
  1183. case SectionId::SectionIdKind::Memory:
  1184. module.memory_section() = TRY(MemorySection::parse(section_stream));
  1185. break;
  1186. case SectionId::SectionIdKind::Global:
  1187. module.global_section() = TRY(GlobalSection::parse(section_stream));
  1188. break;
  1189. case SectionId::SectionIdKind::Export:
  1190. module.export_section() = TRY(ExportSection::parse(section_stream));
  1191. break;
  1192. case SectionId::SectionIdKind::Start:
  1193. module.start_section() = TRY(StartSection::parse(section_stream));
  1194. break;
  1195. case SectionId::SectionIdKind::Element:
  1196. module.element_section() = TRY(ElementSection::parse(section_stream));
  1197. break;
  1198. case SectionId::SectionIdKind::Code:
  1199. module.code_section() = TRY(CodeSection::parse(section_stream));
  1200. break;
  1201. case SectionId::SectionIdKind::Data:
  1202. module.data_section() = TRY(DataSection::parse(section_stream));
  1203. break;
  1204. case SectionId::SectionIdKind::DataCount:
  1205. module.data_count_section() = TRY(DataCountSection::parse(section_stream));
  1206. break;
  1207. default:
  1208. return ParseError::InvalidIndex;
  1209. }
  1210. if (section_id.kind() != SectionId::SectionIdKind::Custom) {
  1211. if (section_id.kind() < last_section_id)
  1212. return ParseError::SectionOutOfOrder;
  1213. last_section_id = section_id.kind();
  1214. }
  1215. if (section_stream.remaining() != 0)
  1216. return ParseError::SectionSizeMismatch;
  1217. }
  1218. return module_ptr;
  1219. }
  1220. ByteString parse_error_to_byte_string(ParseError error)
  1221. {
  1222. switch (error) {
  1223. case ParseError::UnexpectedEof:
  1224. return "Unexpected end-of-file";
  1225. case ParseError::ExpectedIndex:
  1226. return "Expected a valid index value";
  1227. case ParseError::ExpectedKindTag:
  1228. return "Expected a valid kind tag";
  1229. case ParseError::ExpectedSize:
  1230. return "Expected a valid LEB128-encoded size";
  1231. case ParseError::ExpectedValueOrTerminator:
  1232. return "Expected either a terminator or a value";
  1233. case ParseError::InvalidIndex:
  1234. return "An index parsed was semantically invalid";
  1235. case ParseError::InvalidInput:
  1236. return "Input data contained invalid bytes";
  1237. case ParseError::InvalidModuleMagic:
  1238. return "Incorrect module magic (did not match \\0asm)";
  1239. case ParseError::InvalidModuleVersion:
  1240. return "Incorrect module version";
  1241. case ParseError::InvalidSize:
  1242. return "A parsed size did not make sense in context";
  1243. case ParseError::InvalidTag:
  1244. return "A parsed tag did not make sense in context";
  1245. case ParseError::InvalidType:
  1246. return "A parsed type did not make sense in context";
  1247. case ParseError::HugeAllocationRequested:
  1248. return "Parsing caused an attempt to allocate a very big chunk of memory, likely malformed data";
  1249. case ParseError::OutOfMemory:
  1250. return "The parser hit an OOM condition";
  1251. case ParseError::ExpectedFloatingImmediate:
  1252. return "Expected a floating point immediate";
  1253. case ParseError::ExpectedSignedImmediate:
  1254. return "Expected a signed integer immediate";
  1255. case ParseError::InvalidImmediate:
  1256. return "A parsed instruction immediate was invalid for the instruction it was used for";
  1257. case ParseError::SectionSizeMismatch:
  1258. return "A parsed section did not fulfill its expected size";
  1259. case ParseError::InvalidUtf8:
  1260. return "A parsed string was not valid UTF-8";
  1261. case ParseError::UnknownInstruction:
  1262. return "A parsed instruction was not known to this parser";
  1263. case ParseError::DuplicateSection:
  1264. return "Two sections of the same type were encountered";
  1265. case ParseError::SectionOutOfOrder:
  1266. return "A section encountered was not in the correct ordering";
  1267. }
  1268. return "Unknown error";
  1269. }
  1270. }