Validator.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <AK/Result.h>
  8. #include <AK/SourceLocation.h>
  9. #include <AK/Try.h>
  10. #include <LibWasm/AbstractMachine/Validator.h>
  11. #include <LibWasm/Printer/Printer.h>
  12. namespace Wasm {
  13. ErrorOr<void, ValidationError> Validator::validate(Module& module)
  14. {
  15. ErrorOr<void, ValidationError> result {};
  16. // Note: The spec performs this after populating the context, but there's no real reason to do so,
  17. // as this has no dependency.
  18. HashTable<StringView> seen_export_names;
  19. module.for_each_section_of_type<ExportSection>([&result, &seen_export_names](ExportSection const& section) {
  20. if (result.is_error())
  21. return;
  22. for (auto& export_ : section.entries()) {
  23. if (seen_export_names.try_set(export_.name()).release_value_but_fixme_should_propagate_errors() != AK::HashSetResult::InsertedNewEntry)
  24. result = Errors::duplicate_export_name(export_.name());
  25. return;
  26. }
  27. });
  28. if (result.is_error()) {
  29. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  30. return result;
  31. }
  32. m_context = {};
  33. module.for_each_section_of_type<TypeSection>([this](TypeSection const& section) {
  34. m_context.types = section.types();
  35. });
  36. module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
  37. for (auto& import_ : section.imports()) {
  38. import_.description().visit(
  39. [this, &result](TypeIndex const& index) {
  40. if (m_context.types.size() > index.value())
  41. m_context.functions.append(m_context.types[index.value()]);
  42. else
  43. result = Errors::invalid("TypeIndex"sv);
  44. m_context.imported_function_count++;
  45. },
  46. [this](FunctionType const& type) {
  47. m_context.functions.append(type);
  48. m_context.imported_function_count++;
  49. },
  50. [this](TableType const& type) { m_context.tables.append(type); },
  51. [this](MemoryType const& type) { m_context.memories.append(type); },
  52. [this](GlobalType const& type) { m_context.globals.append(type); });
  53. }
  54. });
  55. if (result.is_error()) {
  56. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  57. return result;
  58. }
  59. module.for_each_section_of_type<FunctionSection>([this, &result](FunctionSection const& section) {
  60. if (result.is_error())
  61. return;
  62. m_context.functions.ensure_capacity(section.types().size() + m_context.functions.size());
  63. for (auto& index : section.types()) {
  64. if (m_context.types.size() > index.value()) {
  65. m_context.functions.append(m_context.types[index.value()]);
  66. } else {
  67. result = Errors::invalid("TypeIndex");
  68. break;
  69. }
  70. }
  71. });
  72. if (result.is_error()) {
  73. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  74. return result;
  75. }
  76. module.for_each_section_of_type<TableSection>([this](TableSection const& section) {
  77. m_context.tables.ensure_capacity(m_context.tables.size() + section.tables().size());
  78. for (auto& table : section.tables())
  79. m_context.tables.unchecked_append(table.type());
  80. });
  81. module.for_each_section_of_type<MemorySection>([this](MemorySection const& section) {
  82. m_context.memories.ensure_capacity(m_context.memories.size() + section.memories().size());
  83. for (auto& memory : section.memories())
  84. m_context.memories.unchecked_append(memory.type());
  85. });
  86. module.for_each_section_of_type<GlobalSection>([this](GlobalSection const& section) {
  87. m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size());
  88. for (auto& global : section.entries())
  89. m_context.globals.unchecked_append(global.type());
  90. });
  91. module.for_each_section_of_type<ElementSection>([this](ElementSection const& section) {
  92. m_context.elements.ensure_capacity(section.segments().size());
  93. for (auto& segment : section.segments())
  94. m_context.elements.unchecked_append(segment.type);
  95. });
  96. module.for_each_section_of_type<DataSection>([this](DataSection const& section) {
  97. m_context.datas.resize(section.data().size());
  98. });
  99. // FIXME: C.refs is the set funcidx(module with funcs=ϵ with start=ϵ),
  100. // i.e., the set of function indices occurring in the module, except in its functions or start function.
  101. // This is rather weird, it seems to ultimately be checking that `ref.func` uses a specific set of predetermined functions:
  102. // The only place where this is accessed is in validate_instruction<ref_func>(), but we *populate* this from the ref.func instructions occurring outside regular functions,
  103. // which limits it to only functions referenced from the elements section.
  104. // so the only reason for this (as I see) is to ensure that ref.func only hands out references that occur within the elements and global sections
  105. // _if_ that is indeed the case, then this should be much more specific about where the "valid" references are, and about the actual purpose of this field.
  106. //
  107. // For now, we simply assume that we need to scan the aforementioned section initializers for (ref.func f).
  108. auto scan_expression_for_function_indices = [&](auto& expression) {
  109. for (auto& instruction : expression.instructions()) {
  110. if (instruction.opcode() == Instructions::ref_func)
  111. m_context.references.set(instruction.arguments().template get<FunctionIndex>());
  112. }
  113. };
  114. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  115. for (auto& segment : section.segments()) {
  116. for (auto& expression : segment.init)
  117. scan_expression_for_function_indices(expression);
  118. }
  119. });
  120. module.for_each_section_of_type<GlobalSection>([&](GlobalSection const& section) {
  121. for (auto& segment : section.entries())
  122. scan_expression_for_function_indices(segment.expression());
  123. });
  124. for (auto& section : module.sections()) {
  125. section.visit([this, &result](auto& section) {
  126. result = validate(section);
  127. });
  128. if (result.is_error()) {
  129. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  130. return result;
  131. }
  132. }
  133. if (m_context.memories.size() > 1) {
  134. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  135. return Errors::out_of_bounds("memory section count"sv, m_context.memories.size(), 1, 1);
  136. }
  137. module.set_validation_status(Module::ValidationStatus::Valid, {});
  138. return {};
  139. }
  140. ErrorOr<void, ValidationError> Validator::validate(ImportSection const& section)
  141. {
  142. for (auto& import_ : section.imports())
  143. TRY(import_.description().visit([&](auto& entry) { return validate(entry); }));
  144. return {};
  145. }
  146. ErrorOr<void, ValidationError> Validator::validate(ExportSection const& section)
  147. {
  148. for (auto& export_ : section.entries())
  149. TRY(export_.description().visit([&](auto& entry) { return validate(entry); }));
  150. return {};
  151. }
  152. ErrorOr<void, ValidationError> Validator::validate(StartSection const& section)
  153. {
  154. TRY(validate(section.function().index()));
  155. FunctionType const& type = m_context.functions[section.function().index().value()];
  156. if (!type.parameters().is_empty() || !type.results().is_empty())
  157. return Errors::invalid("start function signature");
  158. return {};
  159. }
  160. ErrorOr<void, ValidationError> Validator::validate(DataSection const& section)
  161. {
  162. for (auto& entry : section.data()) {
  163. TRY(entry.value().visit(
  164. [](DataSection::Data::Passive const&) { return ErrorOr<void, ValidationError> {}; },
  165. [&](DataSection::Data::Active const& active) -> ErrorOr<void, ValidationError> {
  166. TRY(validate(active.index));
  167. auto expression_result = TRY(validate(active.offset, { ValueType(ValueType::I32) }));
  168. if (!expression_result.is_constant)
  169. return Errors::invalid("active data initializer");
  170. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
  171. return Errors::invalid("active data initializer type", ValueType(ValueType::I32), expression_result.result_types);
  172. return {};
  173. }));
  174. }
  175. return {};
  176. }
  177. ErrorOr<void, ValidationError> Validator::validate(ElementSection const& section)
  178. {
  179. for (auto& segment : section.segments()) {
  180. TRY(segment.mode.visit(
  181. [](ElementSection::Declarative const&) -> ErrorOr<void, ValidationError> { return {}; },
  182. [](ElementSection::Passive const&) -> ErrorOr<void, ValidationError> { return {}; },
  183. [&](ElementSection::Active const& active) -> ErrorOr<void, ValidationError> {
  184. TRY(validate(active.index));
  185. auto expression_result = TRY(validate(active.expression, { ValueType(ValueType::I32) }));
  186. if (!expression_result.is_constant)
  187. return Errors::invalid("active element initializer");
  188. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
  189. return Errors::invalid("active element initializer type", ValueType(ValueType::I32), expression_result.result_types);
  190. return {};
  191. }));
  192. }
  193. return {};
  194. }
  195. ErrorOr<void, ValidationError> Validator::validate(GlobalSection const& section)
  196. {
  197. for (auto& entry : section.entries()) {
  198. auto& type = entry.type();
  199. TRY(validate(type));
  200. auto expression_result = TRY(validate(entry.expression(), { type.type() }));
  201. if (!expression_result.is_constant)
  202. return Errors::invalid("global variable initializer");
  203. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(type.type().kind()))
  204. return Errors::invalid("global variable initializer type", ValueType(ValueType::I32), expression_result.result_types);
  205. }
  206. return {};
  207. }
  208. ErrorOr<void, ValidationError> Validator::validate(MemorySection const& section)
  209. {
  210. for (auto& entry : section.memories())
  211. TRY(validate(entry.type()));
  212. return {};
  213. }
  214. ErrorOr<void, ValidationError> Validator::validate(TableSection const& section)
  215. {
  216. for (auto& entry : section.tables())
  217. TRY(validate(entry.type()));
  218. return {};
  219. }
  220. ErrorOr<void, ValidationError> Validator::validate(CodeSection const& section)
  221. {
  222. size_t index = m_context.imported_function_count;
  223. for (auto& entry : section.functions()) {
  224. auto function_index = index++;
  225. TRY(validate(FunctionIndex { function_index }));
  226. auto& function_type = m_context.functions[function_index];
  227. auto& function = entry.func();
  228. auto function_validator = fork();
  229. function_validator.m_context.locals = {};
  230. function_validator.m_context.locals.extend(function_type.parameters());
  231. for (auto& local : function.locals()) {
  232. for (size_t i = 0; i < local.n(); ++i)
  233. function_validator.m_context.locals.append(local.type());
  234. }
  235. function_validator.m_context.labels = { ResultType { function_type.results() } };
  236. function_validator.m_context.return_ = ResultType { function_type.results() };
  237. TRY(function_validator.validate(function.body(), function_type.results()));
  238. }
  239. return {};
  240. }
  241. ErrorOr<void, ValidationError> Validator::validate(TableType const& type)
  242. {
  243. return validate(type.limits(), 32);
  244. }
  245. ErrorOr<void, ValidationError> Validator::validate(MemoryType const& type)
  246. {
  247. return validate(type.limits(), 16);
  248. }
  249. ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type)
  250. {
  251. if (type.kind() == BlockType::Index) {
  252. TRY(validate(type.type_index()));
  253. return m_context.types[type.type_index().value()];
  254. }
  255. if (type.kind() == BlockType::Type) {
  256. FunctionType function_type { {}, { type.value_type() } };
  257. TRY(validate(function_type));
  258. return function_type;
  259. }
  260. if (type.kind() == BlockType::Empty)
  261. return FunctionType { {}, {} };
  262. return Errors::invalid("BlockType"sv);
  263. }
  264. ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, size_t k)
  265. {
  266. auto bound = (1ull << k) - 1;
  267. auto check_bound = [bound](auto value) {
  268. return static_cast<u64>(value) <= bound;
  269. };
  270. if (!check_bound(limits.min()))
  271. return Errors::out_of_bounds("limit minimum"sv, limits.min(), 0, bound);
  272. if (limits.max().has_value() && (limits.max().value() < limits.min() || !check_bound(*limits.max())))
  273. return Errors::out_of_bounds("limit maximum"sv, limits.max().value(), limits.min(), bound);
  274. return {};
  275. }
  276. template<u32 opcode>
  277. ErrorOr<void, ValidationError> Validator::validate_instruction(Instruction const&, Stack&, bool&)
  278. {
  279. return Errors::invalid("instruction opcode"sv);
  280. }
  281. #define VALIDATE_INSTRUCTION(name) \
  282. template<> \
  283. ErrorOr<void, ValidationError> Validator::validate_instruction<Instructions::name.value()>([[maybe_unused]] Instruction const& instruction, [[maybe_unused]] Stack& stack, [[maybe_unused]] bool& is_constant)
  284. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-instr-numericmathsfconstc
  285. VALIDATE_INSTRUCTION(i32_const)
  286. {
  287. is_constant = true;
  288. stack.append(ValueType(ValueType::I32));
  289. return {};
  290. }
  291. VALIDATE_INSTRUCTION(i64_const)
  292. {
  293. is_constant = true;
  294. stack.append(ValueType(ValueType::I64));
  295. return {};
  296. }
  297. VALIDATE_INSTRUCTION(f32_const)
  298. {
  299. is_constant = true;
  300. stack.append(ValueType(ValueType::F32));
  301. return {};
  302. }
  303. VALIDATE_INSTRUCTION(f64_const)
  304. {
  305. is_constant = true;
  306. stack.append(ValueType(ValueType::F64));
  307. return {};
  308. }
  309. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-unopmathitunop
  310. VALIDATE_INSTRUCTION(i32_clz)
  311. {
  312. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  313. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  314. return {};
  315. }
  316. VALIDATE_INSTRUCTION(i32_ctz)
  317. {
  318. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  319. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  320. return {};
  321. }
  322. VALIDATE_INSTRUCTION(i32_popcnt)
  323. {
  324. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  325. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  326. return {};
  327. }
  328. VALIDATE_INSTRUCTION(i64_clz)
  329. {
  330. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  331. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  332. return {};
  333. }
  334. VALIDATE_INSTRUCTION(i64_ctz)
  335. {
  336. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  337. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  338. return {};
  339. }
  340. VALIDATE_INSTRUCTION(i64_popcnt)
  341. {
  342. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  343. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  344. return {};
  345. }
  346. VALIDATE_INSTRUCTION(f32_abs)
  347. {
  348. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  349. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  350. return {};
  351. }
  352. VALIDATE_INSTRUCTION(f32_neg)
  353. {
  354. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  355. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  356. return {};
  357. }
  358. VALIDATE_INSTRUCTION(f32_sqrt)
  359. {
  360. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  361. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  362. return {};
  363. }
  364. VALIDATE_INSTRUCTION(f32_ceil)
  365. {
  366. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  367. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  368. return {};
  369. }
  370. VALIDATE_INSTRUCTION(f32_floor)
  371. {
  372. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  373. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  374. return {};
  375. }
  376. VALIDATE_INSTRUCTION(f32_trunc)
  377. {
  378. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  379. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  380. return {};
  381. }
  382. VALIDATE_INSTRUCTION(f32_nearest)
  383. {
  384. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  385. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  386. return {};
  387. }
  388. VALIDATE_INSTRUCTION(f64_abs)
  389. {
  390. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  391. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  392. return {};
  393. }
  394. VALIDATE_INSTRUCTION(f64_neg)
  395. {
  396. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  397. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  398. return {};
  399. }
  400. VALIDATE_INSTRUCTION(f64_sqrt)
  401. {
  402. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  403. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  404. return {};
  405. }
  406. VALIDATE_INSTRUCTION(f64_ceil)
  407. {
  408. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  409. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  410. return {};
  411. }
  412. VALIDATE_INSTRUCTION(f64_floor)
  413. {
  414. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  415. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  416. return {};
  417. }
  418. VALIDATE_INSTRUCTION(f64_trunc)
  419. {
  420. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  421. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  422. return {};
  423. }
  424. VALIDATE_INSTRUCTION(f64_nearest)
  425. {
  426. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  427. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  428. return {};
  429. }
  430. VALIDATE_INSTRUCTION(i32_extend16_s)
  431. {
  432. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  433. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  434. return {};
  435. }
  436. VALIDATE_INSTRUCTION(i32_extend8_s)
  437. {
  438. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  439. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  440. return {};
  441. }
  442. VALIDATE_INSTRUCTION(i64_extend32_s)
  443. {
  444. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  445. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  446. return {};
  447. }
  448. VALIDATE_INSTRUCTION(i64_extend16_s)
  449. {
  450. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  451. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  452. return {};
  453. }
  454. VALIDATE_INSTRUCTION(i64_extend8_s)
  455. {
  456. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  457. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  458. return {};
  459. }
  460. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-binopmathitbinop
  461. VALIDATE_INSTRUCTION(i32_add)
  462. {
  463. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  464. stack.append(ValueType(ValueType::I32));
  465. return {};
  466. }
  467. VALIDATE_INSTRUCTION(i32_sub)
  468. {
  469. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  470. stack.append(ValueType(ValueType::I32));
  471. return {};
  472. }
  473. VALIDATE_INSTRUCTION(i32_mul)
  474. {
  475. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  476. stack.append(ValueType(ValueType::I32));
  477. return {};
  478. }
  479. VALIDATE_INSTRUCTION(i32_divs)
  480. {
  481. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  482. stack.append(ValueType(ValueType::I32));
  483. return {};
  484. }
  485. VALIDATE_INSTRUCTION(i32_divu)
  486. {
  487. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  488. stack.append(ValueType(ValueType::I32));
  489. return {};
  490. }
  491. VALIDATE_INSTRUCTION(i32_rems)
  492. {
  493. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  494. stack.append(ValueType(ValueType::I32));
  495. return {};
  496. }
  497. VALIDATE_INSTRUCTION(i32_remu)
  498. {
  499. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  500. stack.append(ValueType(ValueType::I32));
  501. return {};
  502. }
  503. VALIDATE_INSTRUCTION(i32_and)
  504. {
  505. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  506. stack.append(ValueType(ValueType::I32));
  507. return {};
  508. }
  509. VALIDATE_INSTRUCTION(i32_or)
  510. {
  511. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  512. stack.append(ValueType(ValueType::I32));
  513. return {};
  514. }
  515. VALIDATE_INSTRUCTION(i32_xor)
  516. {
  517. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  518. stack.append(ValueType(ValueType::I32));
  519. return {};
  520. }
  521. VALIDATE_INSTRUCTION(i32_shl)
  522. {
  523. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  524. stack.append(ValueType(ValueType::I32));
  525. return {};
  526. }
  527. VALIDATE_INSTRUCTION(i32_shrs)
  528. {
  529. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  530. stack.append(ValueType(ValueType::I32));
  531. return {};
  532. }
  533. VALIDATE_INSTRUCTION(i32_shru)
  534. {
  535. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  536. stack.append(ValueType(ValueType::I32));
  537. return {};
  538. }
  539. VALIDATE_INSTRUCTION(i32_rotl)
  540. {
  541. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  542. stack.append(ValueType(ValueType::I32));
  543. return {};
  544. }
  545. VALIDATE_INSTRUCTION(i32_rotr)
  546. {
  547. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  548. stack.append(ValueType(ValueType::I32));
  549. return {};
  550. }
  551. VALIDATE_INSTRUCTION(i64_add)
  552. {
  553. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  554. stack.append(ValueType(ValueType::I64));
  555. return {};
  556. }
  557. VALIDATE_INSTRUCTION(i64_sub)
  558. {
  559. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  560. stack.append(ValueType(ValueType::I64));
  561. return {};
  562. }
  563. VALIDATE_INSTRUCTION(i64_mul)
  564. {
  565. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  566. stack.append(ValueType(ValueType::I64));
  567. return {};
  568. }
  569. VALIDATE_INSTRUCTION(i64_divs)
  570. {
  571. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  572. stack.append(ValueType(ValueType::I64));
  573. return {};
  574. }
  575. VALIDATE_INSTRUCTION(i64_divu)
  576. {
  577. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  578. stack.append(ValueType(ValueType::I64));
  579. return {};
  580. }
  581. VALIDATE_INSTRUCTION(i64_rems)
  582. {
  583. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  584. stack.append(ValueType(ValueType::I64));
  585. return {};
  586. }
  587. VALIDATE_INSTRUCTION(i64_remu)
  588. {
  589. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  590. stack.append(ValueType(ValueType::I64));
  591. return {};
  592. }
  593. VALIDATE_INSTRUCTION(i64_and)
  594. {
  595. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  596. stack.append(ValueType(ValueType::I64));
  597. return {};
  598. }
  599. VALIDATE_INSTRUCTION(i64_or)
  600. {
  601. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  602. stack.append(ValueType(ValueType::I64));
  603. return {};
  604. }
  605. VALIDATE_INSTRUCTION(i64_xor)
  606. {
  607. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  608. stack.append(ValueType(ValueType::I64));
  609. return {};
  610. }
  611. VALIDATE_INSTRUCTION(i64_shl)
  612. {
  613. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  614. stack.append(ValueType(ValueType::I64));
  615. return {};
  616. }
  617. VALIDATE_INSTRUCTION(i64_shrs)
  618. {
  619. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  620. stack.append(ValueType(ValueType::I64));
  621. return {};
  622. }
  623. VALIDATE_INSTRUCTION(i64_shru)
  624. {
  625. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  626. stack.append(ValueType(ValueType::I64));
  627. return {};
  628. }
  629. VALIDATE_INSTRUCTION(i64_rotl)
  630. {
  631. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  632. stack.append(ValueType(ValueType::I64));
  633. return {};
  634. }
  635. VALIDATE_INSTRUCTION(i64_rotr)
  636. {
  637. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  638. stack.append(ValueType(ValueType::I64));
  639. return {};
  640. }
  641. VALIDATE_INSTRUCTION(f32_add)
  642. {
  643. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  644. stack.append(ValueType(ValueType::F32));
  645. return {};
  646. }
  647. VALIDATE_INSTRUCTION(f32_sub)
  648. {
  649. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  650. stack.append(ValueType(ValueType::F32));
  651. return {};
  652. }
  653. VALIDATE_INSTRUCTION(f32_mul)
  654. {
  655. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  656. stack.append(ValueType(ValueType::F32));
  657. return {};
  658. }
  659. VALIDATE_INSTRUCTION(f32_div)
  660. {
  661. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  662. stack.append(ValueType(ValueType::F32));
  663. return {};
  664. }
  665. VALIDATE_INSTRUCTION(f32_min)
  666. {
  667. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  668. stack.append(ValueType(ValueType::F32));
  669. return {};
  670. }
  671. VALIDATE_INSTRUCTION(f32_max)
  672. {
  673. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  674. stack.append(ValueType(ValueType::F32));
  675. return {};
  676. }
  677. VALIDATE_INSTRUCTION(f32_copysign)
  678. {
  679. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  680. stack.append(ValueType(ValueType::F32));
  681. return {};
  682. }
  683. VALIDATE_INSTRUCTION(f64_add)
  684. {
  685. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  686. stack.append(ValueType(ValueType::F64));
  687. return {};
  688. }
  689. VALIDATE_INSTRUCTION(f64_sub)
  690. {
  691. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  692. stack.append(ValueType(ValueType::F64));
  693. return {};
  694. }
  695. VALIDATE_INSTRUCTION(f64_mul)
  696. {
  697. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  698. stack.append(ValueType(ValueType::F64));
  699. return {};
  700. }
  701. VALIDATE_INSTRUCTION(f64_div)
  702. {
  703. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  704. stack.append(ValueType(ValueType::F64));
  705. return {};
  706. }
  707. VALIDATE_INSTRUCTION(f64_min)
  708. {
  709. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  710. stack.append(ValueType(ValueType::F64));
  711. return {};
  712. }
  713. VALIDATE_INSTRUCTION(f64_max)
  714. {
  715. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  716. stack.append(ValueType(ValueType::F64));
  717. return {};
  718. }
  719. VALIDATE_INSTRUCTION(f64_copysign)
  720. {
  721. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  722. stack.append(ValueType(ValueType::F64));
  723. return {};
  724. }
  725. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-testopmathittestop
  726. VALIDATE_INSTRUCTION(i32_eqz)
  727. {
  728. TRY((stack.take<ValueType::I32>()));
  729. stack.append(ValueType(ValueType::I32));
  730. return {};
  731. }
  732. VALIDATE_INSTRUCTION(i64_eqz)
  733. {
  734. TRY((stack.take<ValueType::I64>()));
  735. stack.append(ValueType(ValueType::I32));
  736. return {};
  737. }
  738. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-relopmathitrelop
  739. VALIDATE_INSTRUCTION(i32_eq)
  740. {
  741. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  742. stack.append(ValueType(ValueType::I32));
  743. return {};
  744. }
  745. VALIDATE_INSTRUCTION(i32_ne)
  746. {
  747. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  748. stack.append(ValueType(ValueType::I32));
  749. return {};
  750. }
  751. VALIDATE_INSTRUCTION(i32_lts)
  752. {
  753. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  754. stack.append(ValueType(ValueType::I32));
  755. return {};
  756. }
  757. VALIDATE_INSTRUCTION(i32_ltu)
  758. {
  759. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  760. stack.append(ValueType(ValueType::I32));
  761. return {};
  762. }
  763. VALIDATE_INSTRUCTION(i32_gts)
  764. {
  765. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  766. stack.append(ValueType(ValueType::I32));
  767. return {};
  768. }
  769. VALIDATE_INSTRUCTION(i32_gtu)
  770. {
  771. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  772. stack.append(ValueType(ValueType::I32));
  773. return {};
  774. }
  775. VALIDATE_INSTRUCTION(i32_les)
  776. {
  777. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  778. stack.append(ValueType(ValueType::I32));
  779. return {};
  780. }
  781. VALIDATE_INSTRUCTION(i32_leu)
  782. {
  783. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  784. stack.append(ValueType(ValueType::I32));
  785. return {};
  786. }
  787. VALIDATE_INSTRUCTION(i32_ges)
  788. {
  789. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  790. stack.append(ValueType(ValueType::I32));
  791. return {};
  792. }
  793. VALIDATE_INSTRUCTION(i32_geu)
  794. {
  795. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  796. stack.append(ValueType(ValueType::I32));
  797. return {};
  798. }
  799. VALIDATE_INSTRUCTION(i64_eq)
  800. {
  801. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  802. stack.append(ValueType(ValueType::I32));
  803. return {};
  804. }
  805. VALIDATE_INSTRUCTION(i64_ne)
  806. {
  807. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  808. stack.append(ValueType(ValueType::I32));
  809. return {};
  810. }
  811. VALIDATE_INSTRUCTION(i64_lts)
  812. {
  813. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  814. stack.append(ValueType(ValueType::I32));
  815. return {};
  816. }
  817. VALIDATE_INSTRUCTION(i64_ltu)
  818. {
  819. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  820. stack.append(ValueType(ValueType::I32));
  821. return {};
  822. }
  823. VALIDATE_INSTRUCTION(i64_gts)
  824. {
  825. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  826. stack.append(ValueType(ValueType::I32));
  827. return {};
  828. }
  829. VALIDATE_INSTRUCTION(i64_gtu)
  830. {
  831. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  832. stack.append(ValueType(ValueType::I32));
  833. return {};
  834. }
  835. VALIDATE_INSTRUCTION(i64_les)
  836. {
  837. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  838. stack.append(ValueType(ValueType::I32));
  839. return {};
  840. }
  841. VALIDATE_INSTRUCTION(i64_leu)
  842. {
  843. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  844. stack.append(ValueType(ValueType::I32));
  845. return {};
  846. }
  847. VALIDATE_INSTRUCTION(i64_ges)
  848. {
  849. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  850. stack.append(ValueType(ValueType::I32));
  851. return {};
  852. }
  853. VALIDATE_INSTRUCTION(i64_geu)
  854. {
  855. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  856. stack.append(ValueType(ValueType::I32));
  857. return {};
  858. }
  859. VALIDATE_INSTRUCTION(f32_eq)
  860. {
  861. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  862. stack.append(ValueType(ValueType::I32));
  863. return {};
  864. }
  865. VALIDATE_INSTRUCTION(f32_ne)
  866. {
  867. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  868. stack.append(ValueType(ValueType::I32));
  869. return {};
  870. }
  871. VALIDATE_INSTRUCTION(f32_lt)
  872. {
  873. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  874. stack.append(ValueType(ValueType::I32));
  875. return {};
  876. }
  877. VALIDATE_INSTRUCTION(f32_le)
  878. {
  879. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  880. stack.append(ValueType(ValueType::I32));
  881. return {};
  882. }
  883. VALIDATE_INSTRUCTION(f32_gt)
  884. {
  885. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  886. stack.append(ValueType(ValueType::I32));
  887. return {};
  888. }
  889. VALIDATE_INSTRUCTION(f32_ge)
  890. {
  891. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  892. stack.append(ValueType(ValueType::I32));
  893. return {};
  894. }
  895. VALIDATE_INSTRUCTION(f64_eq)
  896. {
  897. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  898. stack.append(ValueType(ValueType::I32));
  899. return {};
  900. }
  901. VALIDATE_INSTRUCTION(f64_ne)
  902. {
  903. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  904. stack.append(ValueType(ValueType::I32));
  905. return {};
  906. }
  907. VALIDATE_INSTRUCTION(f64_lt)
  908. {
  909. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  910. stack.append(ValueType(ValueType::I32));
  911. return {};
  912. }
  913. VALIDATE_INSTRUCTION(f64_le)
  914. {
  915. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  916. stack.append(ValueType(ValueType::I32));
  917. return {};
  918. }
  919. VALIDATE_INSTRUCTION(f64_gt)
  920. {
  921. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  922. stack.append(ValueType(ValueType::I32));
  923. return {};
  924. }
  925. VALIDATE_INSTRUCTION(f64_ge)
  926. {
  927. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  928. stack.append(ValueType(ValueType::I32));
  929. return {};
  930. }
  931. // https://webassembly.github.io/spec/core/bikeshed/#-t_2mathsfhrefsyntax-cvtopmathitcvtopmathsf_t_1mathsf_hrefsyntax-sxmathitsx
  932. VALIDATE_INSTRUCTION(i32_wrap_i64)
  933. {
  934. TRY(stack.take<ValueType::I64>());
  935. stack.append(ValueType(ValueType::I32));
  936. return {};
  937. }
  938. VALIDATE_INSTRUCTION(i64_extend_si32)
  939. {
  940. TRY(stack.take<ValueType::I32>());
  941. stack.append(ValueType(ValueType::I64));
  942. return {};
  943. }
  944. VALIDATE_INSTRUCTION(i64_extend_ui32)
  945. {
  946. TRY(stack.take<ValueType::I32>());
  947. stack.append(ValueType(ValueType::I64));
  948. return {};
  949. }
  950. VALIDATE_INSTRUCTION(i32_trunc_sf32)
  951. {
  952. TRY(stack.take<ValueType::F32>());
  953. stack.append(ValueType(ValueType::I32));
  954. return {};
  955. }
  956. VALIDATE_INSTRUCTION(i32_trunc_uf32)
  957. {
  958. TRY(stack.take<ValueType::F32>());
  959. stack.append(ValueType(ValueType::I32));
  960. return {};
  961. }
  962. VALIDATE_INSTRUCTION(i32_trunc_sf64)
  963. {
  964. TRY(stack.take<ValueType::F64>());
  965. stack.append(ValueType(ValueType::I32));
  966. return {};
  967. }
  968. VALIDATE_INSTRUCTION(i32_trunc_uf64)
  969. {
  970. TRY(stack.take<ValueType::F64>());
  971. stack.append(ValueType(ValueType::I32));
  972. return {};
  973. }
  974. VALIDATE_INSTRUCTION(i64_trunc_sf32)
  975. {
  976. TRY(stack.take<ValueType::F32>());
  977. stack.append(ValueType(ValueType::I64));
  978. return {};
  979. }
  980. VALIDATE_INSTRUCTION(i64_trunc_uf32)
  981. {
  982. TRY(stack.take<ValueType::F32>());
  983. stack.append(ValueType(ValueType::I64));
  984. return {};
  985. }
  986. VALIDATE_INSTRUCTION(i64_trunc_sf64)
  987. {
  988. TRY(stack.take<ValueType::F64>());
  989. stack.append(ValueType(ValueType::I64));
  990. return {};
  991. }
  992. VALIDATE_INSTRUCTION(i64_trunc_uf64)
  993. {
  994. TRY(stack.take<ValueType::F64>());
  995. stack.append(ValueType(ValueType::I64));
  996. return {};
  997. }
  998. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_s)
  999. {
  1000. TRY(stack.take<ValueType::F32>());
  1001. stack.append(ValueType(ValueType::I32));
  1002. return {};
  1003. }
  1004. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_u)
  1005. {
  1006. TRY(stack.take<ValueType::F32>());
  1007. stack.append(ValueType(ValueType::I32));
  1008. return {};
  1009. }
  1010. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_s)
  1011. {
  1012. TRY(stack.take<ValueType::F64>());
  1013. stack.append(ValueType(ValueType::I32));
  1014. return {};
  1015. }
  1016. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_u)
  1017. {
  1018. TRY(stack.take<ValueType::F64>());
  1019. stack.append(ValueType(ValueType::I32));
  1020. return {};
  1021. }
  1022. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_s)
  1023. {
  1024. TRY(stack.take<ValueType::F32>());
  1025. stack.append(ValueType(ValueType::I64));
  1026. return {};
  1027. }
  1028. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_u)
  1029. {
  1030. TRY(stack.take<ValueType::F32>());
  1031. stack.append(ValueType(ValueType::I64));
  1032. return {};
  1033. }
  1034. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_s)
  1035. {
  1036. TRY(stack.take<ValueType::F64>());
  1037. stack.append(ValueType(ValueType::I64));
  1038. return {};
  1039. }
  1040. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_u)
  1041. {
  1042. TRY(stack.take<ValueType::F64>());
  1043. stack.append(ValueType(ValueType::I64));
  1044. return {};
  1045. }
  1046. VALIDATE_INSTRUCTION(f32_convert_si32)
  1047. {
  1048. TRY(stack.take<ValueType::I32>());
  1049. stack.append(ValueType(ValueType::F32));
  1050. return {};
  1051. }
  1052. VALIDATE_INSTRUCTION(f32_convert_ui32)
  1053. {
  1054. TRY(stack.take<ValueType::I32>());
  1055. stack.append(ValueType(ValueType::F32));
  1056. return {};
  1057. }
  1058. VALIDATE_INSTRUCTION(f32_convert_si64)
  1059. {
  1060. TRY(stack.take<ValueType::I64>());
  1061. stack.append(ValueType(ValueType::F32));
  1062. return {};
  1063. }
  1064. VALIDATE_INSTRUCTION(f32_convert_ui64)
  1065. {
  1066. TRY(stack.take<ValueType::I64>());
  1067. stack.append(ValueType(ValueType::F32));
  1068. return {};
  1069. }
  1070. VALIDATE_INSTRUCTION(f64_convert_si32)
  1071. {
  1072. TRY(stack.take<ValueType::I32>());
  1073. stack.append(ValueType(ValueType::F64));
  1074. return {};
  1075. }
  1076. VALIDATE_INSTRUCTION(f64_convert_ui32)
  1077. {
  1078. TRY(stack.take<ValueType::I32>());
  1079. stack.append(ValueType(ValueType::F64));
  1080. return {};
  1081. }
  1082. VALIDATE_INSTRUCTION(f64_convert_si64)
  1083. {
  1084. TRY(stack.take<ValueType::I64>());
  1085. stack.append(ValueType(ValueType::F64));
  1086. return {};
  1087. }
  1088. VALIDATE_INSTRUCTION(f64_convert_ui64)
  1089. {
  1090. TRY(stack.take<ValueType::I64>());
  1091. stack.append(ValueType(ValueType::F64));
  1092. return {};
  1093. }
  1094. VALIDATE_INSTRUCTION(f32_demote_f64)
  1095. {
  1096. TRY(stack.take<ValueType::F64>());
  1097. stack.append(ValueType(ValueType::F32));
  1098. return {};
  1099. }
  1100. VALIDATE_INSTRUCTION(f64_promote_f32)
  1101. {
  1102. TRY(stack.take<ValueType::F32>());
  1103. stack.append(ValueType(ValueType::F64));
  1104. return {};
  1105. }
  1106. VALIDATE_INSTRUCTION(f32_reinterpret_i32)
  1107. {
  1108. TRY(stack.take<ValueType::I32>());
  1109. stack.append(ValueType(ValueType::F32));
  1110. return {};
  1111. }
  1112. VALIDATE_INSTRUCTION(f64_reinterpret_i64)
  1113. {
  1114. TRY(stack.take<ValueType::I64>());
  1115. stack.append(ValueType(ValueType::F64));
  1116. return {};
  1117. }
  1118. VALIDATE_INSTRUCTION(i32_reinterpret_f32)
  1119. {
  1120. TRY(stack.take<ValueType::F32>());
  1121. stack.append(ValueType(ValueType::I32));
  1122. return {};
  1123. }
  1124. VALIDATE_INSTRUCTION(i64_reinterpret_f64)
  1125. {
  1126. TRY(stack.take<ValueType::F64>());
  1127. stack.append(ValueType(ValueType::I64));
  1128. return {};
  1129. }
  1130. // https://webassembly.github.io/spec/core/bikeshed/#reference-instructions%E2%91%A2
  1131. VALIDATE_INSTRUCTION(ref_null)
  1132. {
  1133. is_constant = true;
  1134. stack.append(instruction.arguments().get<ValueType>());
  1135. return {};
  1136. }
  1137. VALIDATE_INSTRUCTION(ref_is_null)
  1138. {
  1139. if (stack.is_empty() || !stack.last().is_reference())
  1140. return Errors::invalid_stack_state(stack, Tuple { "reference" });
  1141. stack.take_last();
  1142. stack.append(ValueType(ValueType::I32));
  1143. return {};
  1144. }
  1145. VALIDATE_INSTRUCTION(ref_func)
  1146. {
  1147. auto index = instruction.arguments().get<FunctionIndex>();
  1148. TRY(validate(index));
  1149. if (!m_context.references.contains(index))
  1150. return Errors::invalid("function reference");
  1151. is_constant = true;
  1152. stack.append(ValueType(ValueType::FunctionReference));
  1153. return {};
  1154. }
  1155. // https://webassembly.github.io/spec/core/bikeshed/#parametric-instructions%E2%91%A2
  1156. VALIDATE_INSTRUCTION(drop)
  1157. {
  1158. if (stack.is_empty())
  1159. return Errors::invalid_stack_state(stack, Tuple { "any" });
  1160. stack.take_last();
  1161. return {};
  1162. }
  1163. VALIDATE_INSTRUCTION(select)
  1164. {
  1165. if (stack.size() < 3)
  1166. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), "any", "any" });
  1167. auto index_type = stack.take_last();
  1168. auto arg0_type = stack.take_last();
  1169. auto& arg1_type = stack.last();
  1170. if (!index_type.is_of_kind(ValueType::I32))
  1171. return Errors::invalid("select index type", ValueType(ValueType::I32), index_type);
  1172. if (arg0_type != arg1_type)
  1173. return Errors::invalid("select argument types", Vector { arg0_type, arg0_type }, Vector { arg0_type, arg1_type });
  1174. return {};
  1175. }
  1176. VALIDATE_INSTRUCTION(select_typed)
  1177. {
  1178. auto& required_types = instruction.arguments().get<Vector<ValueType>>();
  1179. if (required_types.size() != 1)
  1180. return Errors::invalid("select types", "exactly one type", required_types);
  1181. if (stack.size() < 3)
  1182. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), required_types.first(), required_types.first() });
  1183. auto index_type = stack.take_last();
  1184. auto arg0_type = stack.take_last();
  1185. auto& arg1_type = stack.last();
  1186. if (!index_type.is_of_kind(ValueType::I32))
  1187. return Errors::invalid("select index type", ValueType(ValueType::I32), index_type);
  1188. if (arg0_type != arg1_type || arg0_type != required_types.first())
  1189. return Errors::invalid("select argument types", Vector { required_types.first(), required_types.first() }, Vector { arg0_type, arg1_type });
  1190. return {};
  1191. }
  1192. // https://webassembly.github.io/spec/core/bikeshed/#variable-instructions%E2%91%A2
  1193. VALIDATE_INSTRUCTION(local_get)
  1194. {
  1195. auto index = instruction.arguments().get<LocalIndex>();
  1196. TRY(validate(index));
  1197. stack.append(m_context.locals[index.value()]);
  1198. return {};
  1199. }
  1200. VALIDATE_INSTRUCTION(local_set)
  1201. {
  1202. auto index = instruction.arguments().get<LocalIndex>();
  1203. TRY(validate(index));
  1204. auto& value_type = m_context.locals[index.value()];
  1205. TRY(stack.take(value_type));
  1206. return {};
  1207. }
  1208. VALIDATE_INSTRUCTION(local_tee)
  1209. {
  1210. auto index = instruction.arguments().get<LocalIndex>();
  1211. TRY(validate(index));
  1212. auto& value_type = m_context.locals[index.value()];
  1213. TRY(stack.take(value_type));
  1214. return {};
  1215. }
  1216. VALIDATE_INSTRUCTION(global_get)
  1217. {
  1218. auto index = instruction.arguments().get<GlobalIndex>();
  1219. TRY(validate(index));
  1220. auto& global = m_context.globals[index.value()];
  1221. is_constant = !global.is_mutable();
  1222. stack.append(global.type());
  1223. return {};
  1224. }
  1225. VALIDATE_INSTRUCTION(global_set)
  1226. {
  1227. auto index = instruction.arguments().get<GlobalIndex>();
  1228. TRY(validate(index));
  1229. auto& global = m_context.globals[index.value()];
  1230. if (!global.is_mutable())
  1231. return Errors::invalid("global variable for global.set");
  1232. TRY(stack.take(global.type()));
  1233. return {};
  1234. }
  1235. // https://webassembly.github.io/spec/core/bikeshed/#table-instructions%E2%91%A2
  1236. VALIDATE_INSTRUCTION(table_get)
  1237. {
  1238. auto index = instruction.arguments().get<TableIndex>();
  1239. TRY(validate(index));
  1240. auto& table = m_context.tables[index.value()];
  1241. TRY(stack.take<ValueType::I32>());
  1242. stack.append(table.element_type());
  1243. return {};
  1244. }
  1245. VALIDATE_INSTRUCTION(table_set)
  1246. {
  1247. auto index = instruction.arguments().get<TableIndex>();
  1248. TRY(validate(index));
  1249. auto& table = m_context.tables[index.value()];
  1250. TRY(stack.take(table.element_type()));
  1251. TRY(stack.take<ValueType::I32>());
  1252. return {};
  1253. }
  1254. VALIDATE_INSTRUCTION(table_size)
  1255. {
  1256. auto index = instruction.arguments().get<TableIndex>();
  1257. TRY(validate(index));
  1258. stack.append(ValueType(ValueType::I32));
  1259. return {};
  1260. }
  1261. VALIDATE_INSTRUCTION(table_grow)
  1262. {
  1263. auto index = instruction.arguments().get<TableIndex>();
  1264. TRY(validate(index));
  1265. auto& table = m_context.tables[index.value()];
  1266. TRY(stack.take<ValueType::I32>());
  1267. TRY(stack.take(table.element_type()));
  1268. stack.append(ValueType(ValueType::I32));
  1269. return {};
  1270. }
  1271. VALIDATE_INSTRUCTION(table_fill)
  1272. {
  1273. auto index = instruction.arguments().get<TableIndex>();
  1274. TRY(validate(index));
  1275. auto& table = m_context.tables[index.value()];
  1276. TRY(stack.take<ValueType::I32>());
  1277. TRY(stack.take(table.element_type()));
  1278. TRY(stack.take<ValueType::I32>());
  1279. return {};
  1280. }
  1281. VALIDATE_INSTRUCTION(table_copy)
  1282. {
  1283. auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
  1284. TRY(validate(args.lhs));
  1285. TRY(validate(args.rhs));
  1286. auto& lhs_table = m_context.tables[args.lhs.value()];
  1287. auto& rhs_table = m_context.tables[args.rhs.value()];
  1288. if (lhs_table.element_type() != rhs_table.element_type())
  1289. return Errors::non_conforming_types("table.copy", lhs_table.element_type(), rhs_table.element_type());
  1290. if (!lhs_table.element_type().is_reference())
  1291. return Errors::invalid("table.copy element type", "a reference type", lhs_table.element_type());
  1292. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1293. return {};
  1294. }
  1295. VALIDATE_INSTRUCTION(table_init)
  1296. {
  1297. auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
  1298. TRY(validate(args.table_index));
  1299. TRY(validate(args.element_index));
  1300. auto& table = m_context.tables[args.table_index.value()];
  1301. auto& element_type = m_context.elements[args.element_index.value()];
  1302. if (table.element_type() != element_type)
  1303. return Errors::non_conforming_types("table.init", table.element_type(), element_type);
  1304. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1305. return {};
  1306. }
  1307. VALIDATE_INSTRUCTION(elem_drop)
  1308. {
  1309. auto index = instruction.arguments().get<ElementIndex>();
  1310. TRY(validate(index));
  1311. return {};
  1312. }
  1313. // https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A2
  1314. VALIDATE_INSTRUCTION(i32_load)
  1315. {
  1316. TRY(validate(MemoryIndex { 0 }));
  1317. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1318. if ((1ull << arg.align) > sizeof(i32))
  1319. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1320. TRY((stack.take<ValueType::I32>()));
  1321. stack.append(ValueType(ValueType::I32));
  1322. return {};
  1323. }
  1324. VALIDATE_INSTRUCTION(i64_load)
  1325. {
  1326. TRY(validate(MemoryIndex { 0 }));
  1327. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1328. if ((1ull << arg.align) > sizeof(i64))
  1329. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1330. TRY((stack.take<ValueType::I32>()));
  1331. stack.append(ValueType(ValueType::I64));
  1332. return {};
  1333. }
  1334. VALIDATE_INSTRUCTION(f32_load)
  1335. {
  1336. TRY(validate(MemoryIndex { 0 }));
  1337. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1338. if ((1ull << arg.align) > sizeof(float))
  1339. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1340. TRY((stack.take<ValueType::I32>()));
  1341. stack.append(ValueType(ValueType::F32));
  1342. return {};
  1343. }
  1344. VALIDATE_INSTRUCTION(f64_load)
  1345. {
  1346. TRY(validate(MemoryIndex { 0 }));
  1347. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1348. if ((1ull << arg.align) > sizeof(double))
  1349. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1350. TRY((stack.take<ValueType::I32>()));
  1351. stack.append(ValueType(ValueType::F64));
  1352. return {};
  1353. }
  1354. VALIDATE_INSTRUCTION(i32_load16_s)
  1355. {
  1356. TRY(validate(MemoryIndex { 0 }));
  1357. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1358. if ((1ull << arg.align) > 16 / 8)
  1359. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1360. TRY((stack.take<ValueType::I32>()));
  1361. stack.append(ValueType(ValueType::I32));
  1362. return {};
  1363. }
  1364. VALIDATE_INSTRUCTION(i32_load16_u)
  1365. {
  1366. TRY(validate(MemoryIndex { 0 }));
  1367. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1368. if ((1ull << arg.align) > 16 / 8)
  1369. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1370. TRY((stack.take<ValueType::I32>()));
  1371. stack.append(ValueType(ValueType::I32));
  1372. return {};
  1373. }
  1374. VALIDATE_INSTRUCTION(i32_load8_s)
  1375. {
  1376. TRY(validate(MemoryIndex { 0 }));
  1377. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1378. if ((1ull << arg.align) > 8 / 8)
  1379. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1380. TRY((stack.take<ValueType::I32>()));
  1381. stack.append(ValueType(ValueType::I32));
  1382. return {};
  1383. }
  1384. VALIDATE_INSTRUCTION(i32_load8_u)
  1385. {
  1386. TRY(validate(MemoryIndex { 0 }));
  1387. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1388. if ((1ull << arg.align) > 8 / 8)
  1389. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1390. TRY((stack.take<ValueType::I32>()));
  1391. stack.append(ValueType(ValueType::I32));
  1392. return {};
  1393. }
  1394. VALIDATE_INSTRUCTION(i64_load32_s)
  1395. {
  1396. TRY(validate(MemoryIndex { 0 }));
  1397. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1398. if ((1ull << arg.align) > 32 / 8)
  1399. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1400. TRY((stack.take<ValueType::I32>()));
  1401. stack.append(ValueType(ValueType::I64));
  1402. return {};
  1403. }
  1404. VALIDATE_INSTRUCTION(i64_load32_u)
  1405. {
  1406. TRY(validate(MemoryIndex { 0 }));
  1407. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1408. if ((1ull << arg.align) > 32 / 8)
  1409. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1410. TRY((stack.take<ValueType::I32>()));
  1411. stack.append(ValueType(ValueType::I64));
  1412. return {};
  1413. }
  1414. VALIDATE_INSTRUCTION(i64_load16_s)
  1415. {
  1416. TRY(validate(MemoryIndex { 0 }));
  1417. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1418. if ((1ull << arg.align) > 16 / 8)
  1419. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1420. TRY((stack.take<ValueType::I32>()));
  1421. stack.append(ValueType(ValueType::I64));
  1422. return {};
  1423. }
  1424. VALIDATE_INSTRUCTION(i64_load16_u)
  1425. {
  1426. TRY(validate(MemoryIndex { 0 }));
  1427. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1428. if ((1ull << arg.align) > 16 / 8)
  1429. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1430. TRY((stack.take<ValueType::I32>()));
  1431. stack.append(ValueType(ValueType::I64));
  1432. return {};
  1433. }
  1434. VALIDATE_INSTRUCTION(i64_load8_s)
  1435. {
  1436. TRY(validate(MemoryIndex { 0 }));
  1437. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1438. if ((1ull << arg.align) > 8 / 8)
  1439. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1440. TRY((stack.take<ValueType::I32>()));
  1441. stack.append(ValueType(ValueType::I64));
  1442. return {};
  1443. }
  1444. VALIDATE_INSTRUCTION(i64_load8_u)
  1445. {
  1446. TRY(validate(MemoryIndex { 0 }));
  1447. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1448. if ((1ull << arg.align) > 8 / 8)
  1449. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1450. TRY((stack.take<ValueType::I32>()));
  1451. stack.append(ValueType(ValueType::I64));
  1452. return {};
  1453. }
  1454. VALIDATE_INSTRUCTION(i32_store)
  1455. {
  1456. TRY(validate(MemoryIndex { 0 }));
  1457. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1458. if ((1ull << arg.align) > sizeof(i32))
  1459. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1460. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1461. return {};
  1462. }
  1463. VALIDATE_INSTRUCTION(i64_store)
  1464. {
  1465. TRY(validate(MemoryIndex { 0 }));
  1466. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1467. if ((1ull << arg.align) > sizeof(i64))
  1468. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1469. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1470. return {};
  1471. }
  1472. VALIDATE_INSTRUCTION(f32_store)
  1473. {
  1474. TRY(validate(MemoryIndex { 0 }));
  1475. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1476. if ((1ull << arg.align) > sizeof(float))
  1477. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1478. TRY((stack.take<ValueType::F32, ValueType::I32>()));
  1479. return {};
  1480. }
  1481. VALIDATE_INSTRUCTION(f64_store)
  1482. {
  1483. TRY(validate(MemoryIndex { 0 }));
  1484. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1485. if ((1ull << arg.align) > sizeof(double))
  1486. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1487. TRY((stack.take<ValueType::F64, ValueType::I32>()));
  1488. return {};
  1489. }
  1490. VALIDATE_INSTRUCTION(i32_store16)
  1491. {
  1492. TRY(validate(MemoryIndex { 0 }));
  1493. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1494. if ((1ull << arg.align) > 16 / 8)
  1495. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1496. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1497. return {};
  1498. }
  1499. VALIDATE_INSTRUCTION(i32_store8)
  1500. {
  1501. TRY(validate(MemoryIndex { 0 }));
  1502. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1503. if ((1ull << arg.align) > 8 / 8)
  1504. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1505. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1506. return {};
  1507. }
  1508. VALIDATE_INSTRUCTION(i64_store32)
  1509. {
  1510. TRY(validate(MemoryIndex { 0 }));
  1511. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1512. if ((1ull << arg.align) > 32 / 8)
  1513. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1514. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1515. return {};
  1516. }
  1517. VALIDATE_INSTRUCTION(i64_store16)
  1518. {
  1519. TRY(validate(MemoryIndex { 0 }));
  1520. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1521. if ((1ull << arg.align) > 16 / 8)
  1522. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1523. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1524. return {};
  1525. }
  1526. VALIDATE_INSTRUCTION(i64_store8)
  1527. {
  1528. TRY(validate(MemoryIndex { 0 }));
  1529. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1530. if ((1ull << arg.align) > 8 / 8)
  1531. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1532. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1533. return {};
  1534. }
  1535. VALIDATE_INSTRUCTION(memory_size)
  1536. {
  1537. TRY(validate(MemoryIndex { 0 }));
  1538. stack.append(ValueType(ValueType::I32));
  1539. return {};
  1540. }
  1541. VALIDATE_INSTRUCTION(memory_grow)
  1542. {
  1543. TRY(validate(MemoryIndex { 0 }));
  1544. TRY((stack.take<ValueType::I32>()));
  1545. return {};
  1546. }
  1547. VALIDATE_INSTRUCTION(memory_fill)
  1548. {
  1549. TRY(validate(MemoryIndex { 0 }));
  1550. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1551. return {};
  1552. }
  1553. VALIDATE_INSTRUCTION(memory_init)
  1554. {
  1555. TRY(validate(MemoryIndex { 0 }));
  1556. auto index = instruction.arguments().get<DataIndex>();
  1557. TRY(validate(index));
  1558. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1559. return {};
  1560. }
  1561. VALIDATE_INSTRUCTION(data_drop)
  1562. {
  1563. auto index = instruction.arguments().get<DataIndex>();
  1564. TRY(validate(index));
  1565. return {};
  1566. }
  1567. // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
  1568. VALIDATE_INSTRUCTION(nop)
  1569. {
  1570. return {};
  1571. }
  1572. VALIDATE_INSTRUCTION(unreachable)
  1573. {
  1574. // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
  1575. stack.append(StackEntry());
  1576. return {};
  1577. }
  1578. // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
  1579. VALIDATE_INSTRUCTION(structured_end)
  1580. {
  1581. if (m_entered_scopes.is_empty())
  1582. return Errors::invalid("usage of structured end");
  1583. auto last_scope = m_entered_scopes.take_last();
  1584. m_context = m_parent_contexts.take_last();
  1585. auto last_block_type = m_entered_blocks.take_last();
  1586. switch (last_scope) {
  1587. case ChildScopeKind::Block:
  1588. case ChildScopeKind::IfWithoutElse:
  1589. case ChildScopeKind::Else:
  1590. m_block_details.take_last();
  1591. break;
  1592. case ChildScopeKind::IfWithElse:
  1593. return Errors::invalid("usage of if without an else clause that appears to have one anyway");
  1594. }
  1595. auto& results = last_block_type.results();
  1596. for (size_t i = 1; i <= results.size(); ++i)
  1597. TRY(stack.take(results[results.size() - i]));
  1598. for (auto& result : results)
  1599. stack.append(result);
  1600. return {};
  1601. }
  1602. // Note: This is *not* from the spec.
  1603. VALIDATE_INSTRUCTION(structured_else)
  1604. {
  1605. if (m_entered_scopes.is_empty())
  1606. return Errors::invalid("usage of structured else");
  1607. if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
  1608. return Errors::invalid("usage of structured else");
  1609. auto& block_type = m_entered_blocks.last();
  1610. auto& results = block_type.results();
  1611. for (size_t i = 1; i <= results.size(); ++i)
  1612. TRY(stack.take(results[results.size() - i]));
  1613. auto& details = m_block_details.last().details.get<BlockDetails::IfDetails>();
  1614. m_entered_scopes.last() = ChildScopeKind::Else;
  1615. stack = move(details.initial_stack);
  1616. return {};
  1617. }
  1618. VALIDATE_INSTRUCTION(block)
  1619. {
  1620. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1621. auto block_type = TRY(validate(args.block_type));
  1622. auto& parameters = block_type.parameters();
  1623. for (size_t i = 1; i <= parameters.size(); ++i)
  1624. TRY(stack.take(parameters[parameters.size() - i]));
  1625. for (auto& parameter : parameters)
  1626. stack.append(parameter);
  1627. m_entered_scopes.append(ChildScopeKind::Block);
  1628. m_block_details.empend(stack.actual_size(), Empty {});
  1629. m_parent_contexts.append(m_context);
  1630. m_entered_blocks.append(block_type);
  1631. m_context.labels.prepend(ResultType { block_type.results() });
  1632. return {};
  1633. }
  1634. VALIDATE_INSTRUCTION(loop)
  1635. {
  1636. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1637. auto block_type = TRY(validate(args.block_type));
  1638. auto& parameters = block_type.parameters();
  1639. for (size_t i = 1; i <= parameters.size(); ++i)
  1640. TRY(stack.take(parameters[parameters.size() - i]));
  1641. for (auto& parameter : parameters)
  1642. stack.append(parameter);
  1643. m_entered_scopes.append(ChildScopeKind::Block);
  1644. m_block_details.empend(stack.actual_size(), Empty {});
  1645. m_parent_contexts.append(m_context);
  1646. m_entered_blocks.append(block_type);
  1647. m_context.labels.prepend(ResultType { block_type.parameters() });
  1648. return {};
  1649. }
  1650. VALIDATE_INSTRUCTION(if_)
  1651. {
  1652. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1653. auto block_type = TRY(validate(args.block_type));
  1654. TRY(stack.take<ValueType::I32>());
  1655. auto stack_snapshot = stack;
  1656. auto& parameters = block_type.parameters();
  1657. for (size_t i = 1; i <= parameters.size(); ++i)
  1658. TRY(stack.take(parameters[parameters.size() - i]));
  1659. for (auto& parameter : parameters)
  1660. stack.append(parameter);
  1661. m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
  1662. m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { move(stack_snapshot) });
  1663. m_parent_contexts.append(m_context);
  1664. m_entered_blocks.append(block_type);
  1665. m_context.labels.prepend(ResultType { block_type.results() });
  1666. return {};
  1667. }
  1668. VALIDATE_INSTRUCTION(br)
  1669. {
  1670. auto label = instruction.arguments().get<LabelIndex>();
  1671. TRY(validate(label));
  1672. auto& type = m_context.labels[label.value()];
  1673. for (size_t i = 1; i <= type.types().size(); ++i)
  1674. TRY(stack.take(type.types()[type.types().size() - i]));
  1675. stack.append(StackEntry());
  1676. return {};
  1677. }
  1678. VALIDATE_INSTRUCTION(br_if)
  1679. {
  1680. auto label = instruction.arguments().get<LabelIndex>();
  1681. TRY(validate(label));
  1682. TRY(stack.take<ValueType::I32>());
  1683. auto& type = m_context.labels[label.value()];
  1684. Vector<StackEntry> entries;
  1685. entries.ensure_capacity(type.types().size());
  1686. for (size_t i = 0; i < type.types().size(); ++i) {
  1687. auto& entry = type.types()[type.types().size() - i - 1];
  1688. TRY(stack.take(entry));
  1689. entries.append(entry);
  1690. }
  1691. for (size_t i = 0; i < entries.size(); ++i)
  1692. stack.append(entries[entries.size() - i - 1]);
  1693. return {};
  1694. }
  1695. VALIDATE_INSTRUCTION(br_table)
  1696. {
  1697. auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
  1698. TRY(validate(args.default_));
  1699. for (auto& label : args.labels)
  1700. TRY(validate(label));
  1701. TRY(stack.take<ValueType::I32>());
  1702. auto& default_types = m_context.labels[args.default_.value()].types();
  1703. auto arity = default_types.size();
  1704. auto stack_snapshot = stack;
  1705. auto stack_to_check = stack_snapshot;
  1706. for (auto& label : args.labels) {
  1707. auto& label_types = m_context.labels[label.value()].types();
  1708. for (size_t i = 0; i < arity; ++i)
  1709. TRY(stack_to_check.take(label_types[label_types.size() - i - 1]));
  1710. stack_to_check = stack_snapshot;
  1711. }
  1712. for (size_t i = 0; i < arity; ++i) {
  1713. auto expected = default_types[default_types.size() - i - 1];
  1714. TRY((stack.take(expected)));
  1715. }
  1716. stack.append(StackEntry());
  1717. return {};
  1718. }
  1719. VALIDATE_INSTRUCTION(return_)
  1720. {
  1721. if (!m_context.return_.has_value())
  1722. return Errors::invalid("use of return outside function");
  1723. auto& return_types = m_context.return_->types();
  1724. for (size_t i = 0; i < return_types.size(); ++i)
  1725. TRY((stack.take(return_types[return_types.size() - i - 1])));
  1726. stack.append(StackEntry());
  1727. return {};
  1728. }
  1729. VALIDATE_INSTRUCTION(call)
  1730. {
  1731. auto index = instruction.arguments().get<FunctionIndex>();
  1732. TRY(validate(index));
  1733. auto& function_type = m_context.functions[index.value()];
  1734. for (size_t i = 0; i < function_type.parameters().size(); ++i)
  1735. TRY(stack.take(function_type.parameters()[function_type.parameters().size() - i - 1]));
  1736. for (auto& type : function_type.results())
  1737. stack.append(type);
  1738. return {};
  1739. }
  1740. VALIDATE_INSTRUCTION(call_indirect)
  1741. {
  1742. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  1743. TRY(validate(args.table));
  1744. TRY(validate(args.type));
  1745. auto& table = m_context.tables[args.table.value()];
  1746. if (!table.element_type().is_reference())
  1747. return Errors::invalid("table element type for call.indirect", "a reference type", table.element_type());
  1748. auto& type = m_context.types[args.type.value()];
  1749. TRY(stack.take<ValueType::I32>());
  1750. for (size_t i = 0; i < type.parameters().size(); ++i)
  1751. TRY(stack.take(type.parameters()[type.parameters().size() - i - 1]));
  1752. for (auto& type : type.results())
  1753. stack.append(type);
  1754. return {};
  1755. }
  1756. ErrorOr<void, ValidationError> Validator::validate(const Instruction& instruction, Stack& stack, bool& is_constant)
  1757. {
  1758. switch (instruction.opcode().value()) {
  1759. #define M(name, integer_value) \
  1760. case Instructions::name.value(): \
  1761. dbgln_if(WASM_VALIDATOR_DEBUG, "checking {}, stack = {}", #name, stack); \
  1762. return validate_instruction<integer_value>(instruction, stack, is_constant);
  1763. ENUMERATE_WASM_OPCODES(M)
  1764. #undef M
  1765. default:
  1766. is_constant = false;
  1767. return Errors::invalid("instruction opcode");
  1768. }
  1769. }
  1770. ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
  1771. {
  1772. Stack stack;
  1773. bool is_constant_expression = true;
  1774. for (auto& instruction : expression.instructions()) {
  1775. bool is_constant = false;
  1776. TRY(validate(instruction, stack, is_constant));
  1777. is_constant_expression &= is_constant;
  1778. }
  1779. auto expected_result_types = result_types;
  1780. while (!expected_result_types.is_empty())
  1781. TRY(stack.take(expected_result_types.take_last()));
  1782. for (auto& type : result_types)
  1783. stack.append(type);
  1784. return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
  1785. }
  1786. bool Validator::Stack::operator==(const Stack& other) const
  1787. {
  1788. if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
  1789. return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
  1790. Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
  1791. auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
  1792. auto own_size = Vector<StackEntry>::size();
  1793. for (size_t i = 0; i < own_size; ++i) {
  1794. if (other_size <= i)
  1795. break;
  1796. auto own_entry = at(own_size - i - 1);
  1797. auto other_entry = other.at(other_size - i - 1);
  1798. if (!own_entry.is_known) {
  1799. own_last_unknown_entry_index_from_end = i;
  1800. break;
  1801. }
  1802. if (!other_entry.is_known) {
  1803. other_last_unknown_entry_index_from_end = i;
  1804. break;
  1805. }
  1806. }
  1807. if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
  1808. if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
  1809. return true;
  1810. dbgln("Equality check internal error between");
  1811. dbgln("stack:");
  1812. for (auto& entry : *this)
  1813. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1814. dbgln("and stack:");
  1815. for (auto& entry : other)
  1816. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1817. VERIFY_NOT_REACHED();
  1818. }
  1819. auto index_from_end = max(own_last_unknown_entry_index_from_end.value_or(0), other_last_unknown_entry_index_from_end.value_or(0));
  1820. for (size_t i = 0; i < index_from_end; ++i) {
  1821. if (at(own_size - i - 1) != other.at(other_size - i - 1))
  1822. return false;
  1823. }
  1824. return true;
  1825. }
  1826. String Validator::Errors::find_instruction_name(SourceLocation const& location)
  1827. {
  1828. auto index = location.function_name().find('<');
  1829. auto end_index = location.function_name().find('>');
  1830. if (!index.has_value() || !end_index.has_value())
  1831. return String::formatted("{}", location);
  1832. auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_uint();
  1833. if (!opcode.has_value())
  1834. return String::formatted("{}", location);
  1835. return instruction_name(OpCode { *opcode });
  1836. }
  1837. }