Validator.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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. stack.append(value_type);
  1215. return {};
  1216. }
  1217. VALIDATE_INSTRUCTION(global_get)
  1218. {
  1219. auto index = instruction.arguments().get<GlobalIndex>();
  1220. TRY(validate(index));
  1221. auto& global = m_context.globals[index.value()];
  1222. is_constant = !global.is_mutable();
  1223. stack.append(global.type());
  1224. return {};
  1225. }
  1226. VALIDATE_INSTRUCTION(global_set)
  1227. {
  1228. auto index = instruction.arguments().get<GlobalIndex>();
  1229. TRY(validate(index));
  1230. auto& global = m_context.globals[index.value()];
  1231. if (!global.is_mutable())
  1232. return Errors::invalid("global variable for global.set");
  1233. TRY(stack.take(global.type()));
  1234. return {};
  1235. }
  1236. // https://webassembly.github.io/spec/core/bikeshed/#table-instructions%E2%91%A2
  1237. VALIDATE_INSTRUCTION(table_get)
  1238. {
  1239. auto index = instruction.arguments().get<TableIndex>();
  1240. TRY(validate(index));
  1241. auto& table = m_context.tables[index.value()];
  1242. TRY(stack.take<ValueType::I32>());
  1243. stack.append(table.element_type());
  1244. return {};
  1245. }
  1246. VALIDATE_INSTRUCTION(table_set)
  1247. {
  1248. auto index = instruction.arguments().get<TableIndex>();
  1249. TRY(validate(index));
  1250. auto& table = m_context.tables[index.value()];
  1251. TRY(stack.take(table.element_type()));
  1252. TRY(stack.take<ValueType::I32>());
  1253. return {};
  1254. }
  1255. VALIDATE_INSTRUCTION(table_size)
  1256. {
  1257. auto index = instruction.arguments().get<TableIndex>();
  1258. TRY(validate(index));
  1259. stack.append(ValueType(ValueType::I32));
  1260. return {};
  1261. }
  1262. VALIDATE_INSTRUCTION(table_grow)
  1263. {
  1264. auto index = instruction.arguments().get<TableIndex>();
  1265. TRY(validate(index));
  1266. auto& table = m_context.tables[index.value()];
  1267. TRY(stack.take<ValueType::I32>());
  1268. TRY(stack.take(table.element_type()));
  1269. stack.append(ValueType(ValueType::I32));
  1270. return {};
  1271. }
  1272. VALIDATE_INSTRUCTION(table_fill)
  1273. {
  1274. auto index = instruction.arguments().get<TableIndex>();
  1275. TRY(validate(index));
  1276. auto& table = m_context.tables[index.value()];
  1277. TRY(stack.take<ValueType::I32>());
  1278. TRY(stack.take(table.element_type()));
  1279. TRY(stack.take<ValueType::I32>());
  1280. return {};
  1281. }
  1282. VALIDATE_INSTRUCTION(table_copy)
  1283. {
  1284. auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
  1285. TRY(validate(args.lhs));
  1286. TRY(validate(args.rhs));
  1287. auto& lhs_table = m_context.tables[args.lhs.value()];
  1288. auto& rhs_table = m_context.tables[args.rhs.value()];
  1289. if (lhs_table.element_type() != rhs_table.element_type())
  1290. return Errors::non_conforming_types("table.copy", lhs_table.element_type(), rhs_table.element_type());
  1291. if (!lhs_table.element_type().is_reference())
  1292. return Errors::invalid("table.copy element type", "a reference type", lhs_table.element_type());
  1293. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1294. return {};
  1295. }
  1296. VALIDATE_INSTRUCTION(table_init)
  1297. {
  1298. auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
  1299. TRY(validate(args.table_index));
  1300. TRY(validate(args.element_index));
  1301. auto& table = m_context.tables[args.table_index.value()];
  1302. auto& element_type = m_context.elements[args.element_index.value()];
  1303. if (table.element_type() != element_type)
  1304. return Errors::non_conforming_types("table.init", table.element_type(), element_type);
  1305. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1306. return {};
  1307. }
  1308. VALIDATE_INSTRUCTION(elem_drop)
  1309. {
  1310. auto index = instruction.arguments().get<ElementIndex>();
  1311. TRY(validate(index));
  1312. return {};
  1313. }
  1314. // https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A2
  1315. VALIDATE_INSTRUCTION(i32_load)
  1316. {
  1317. TRY(validate(MemoryIndex { 0 }));
  1318. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1319. if ((1ull << arg.align) > sizeof(i32))
  1320. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1321. TRY((stack.take<ValueType::I32>()));
  1322. stack.append(ValueType(ValueType::I32));
  1323. return {};
  1324. }
  1325. VALIDATE_INSTRUCTION(i64_load)
  1326. {
  1327. TRY(validate(MemoryIndex { 0 }));
  1328. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1329. if ((1ull << arg.align) > sizeof(i64))
  1330. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1331. TRY((stack.take<ValueType::I32>()));
  1332. stack.append(ValueType(ValueType::I64));
  1333. return {};
  1334. }
  1335. VALIDATE_INSTRUCTION(f32_load)
  1336. {
  1337. TRY(validate(MemoryIndex { 0 }));
  1338. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1339. if ((1ull << arg.align) > sizeof(float))
  1340. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1341. TRY((stack.take<ValueType::I32>()));
  1342. stack.append(ValueType(ValueType::F32));
  1343. return {};
  1344. }
  1345. VALIDATE_INSTRUCTION(f64_load)
  1346. {
  1347. TRY(validate(MemoryIndex { 0 }));
  1348. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1349. if ((1ull << arg.align) > sizeof(double))
  1350. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1351. TRY((stack.take<ValueType::I32>()));
  1352. stack.append(ValueType(ValueType::F64));
  1353. return {};
  1354. }
  1355. VALIDATE_INSTRUCTION(i32_load16_s)
  1356. {
  1357. TRY(validate(MemoryIndex { 0 }));
  1358. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1359. if ((1ull << arg.align) > 16 / 8)
  1360. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1361. TRY((stack.take<ValueType::I32>()));
  1362. stack.append(ValueType(ValueType::I32));
  1363. return {};
  1364. }
  1365. VALIDATE_INSTRUCTION(i32_load16_u)
  1366. {
  1367. TRY(validate(MemoryIndex { 0 }));
  1368. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1369. if ((1ull << arg.align) > 16 / 8)
  1370. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1371. TRY((stack.take<ValueType::I32>()));
  1372. stack.append(ValueType(ValueType::I32));
  1373. return {};
  1374. }
  1375. VALIDATE_INSTRUCTION(i32_load8_s)
  1376. {
  1377. TRY(validate(MemoryIndex { 0 }));
  1378. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1379. if ((1ull << arg.align) > 8 / 8)
  1380. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1381. TRY((stack.take<ValueType::I32>()));
  1382. stack.append(ValueType(ValueType::I32));
  1383. return {};
  1384. }
  1385. VALIDATE_INSTRUCTION(i32_load8_u)
  1386. {
  1387. TRY(validate(MemoryIndex { 0 }));
  1388. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1389. if ((1ull << arg.align) > 8 / 8)
  1390. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1391. TRY((stack.take<ValueType::I32>()));
  1392. stack.append(ValueType(ValueType::I32));
  1393. return {};
  1394. }
  1395. VALIDATE_INSTRUCTION(i64_load32_s)
  1396. {
  1397. TRY(validate(MemoryIndex { 0 }));
  1398. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1399. if ((1ull << arg.align) > 32 / 8)
  1400. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1401. TRY((stack.take<ValueType::I32>()));
  1402. stack.append(ValueType(ValueType::I64));
  1403. return {};
  1404. }
  1405. VALIDATE_INSTRUCTION(i64_load32_u)
  1406. {
  1407. TRY(validate(MemoryIndex { 0 }));
  1408. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1409. if ((1ull << arg.align) > 32 / 8)
  1410. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1411. TRY((stack.take<ValueType::I32>()));
  1412. stack.append(ValueType(ValueType::I64));
  1413. return {};
  1414. }
  1415. VALIDATE_INSTRUCTION(i64_load16_s)
  1416. {
  1417. TRY(validate(MemoryIndex { 0 }));
  1418. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1419. if ((1ull << arg.align) > 16 / 8)
  1420. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1421. TRY((stack.take<ValueType::I32>()));
  1422. stack.append(ValueType(ValueType::I64));
  1423. return {};
  1424. }
  1425. VALIDATE_INSTRUCTION(i64_load16_u)
  1426. {
  1427. TRY(validate(MemoryIndex { 0 }));
  1428. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1429. if ((1ull << arg.align) > 16 / 8)
  1430. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1431. TRY((stack.take<ValueType::I32>()));
  1432. stack.append(ValueType(ValueType::I64));
  1433. return {};
  1434. }
  1435. VALIDATE_INSTRUCTION(i64_load8_s)
  1436. {
  1437. TRY(validate(MemoryIndex { 0 }));
  1438. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1439. if ((1ull << arg.align) > 8 / 8)
  1440. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1441. TRY((stack.take<ValueType::I32>()));
  1442. stack.append(ValueType(ValueType::I64));
  1443. return {};
  1444. }
  1445. VALIDATE_INSTRUCTION(i64_load8_u)
  1446. {
  1447. TRY(validate(MemoryIndex { 0 }));
  1448. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1449. if ((1ull << arg.align) > 8 / 8)
  1450. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1451. TRY((stack.take<ValueType::I32>()));
  1452. stack.append(ValueType(ValueType::I64));
  1453. return {};
  1454. }
  1455. VALIDATE_INSTRUCTION(i32_store)
  1456. {
  1457. TRY(validate(MemoryIndex { 0 }));
  1458. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1459. if ((1ull << arg.align) > sizeof(i32))
  1460. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1461. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1462. return {};
  1463. }
  1464. VALIDATE_INSTRUCTION(i64_store)
  1465. {
  1466. TRY(validate(MemoryIndex { 0 }));
  1467. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1468. if ((1ull << arg.align) > sizeof(i64))
  1469. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1470. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1471. return {};
  1472. }
  1473. VALIDATE_INSTRUCTION(f32_store)
  1474. {
  1475. TRY(validate(MemoryIndex { 0 }));
  1476. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1477. if ((1ull << arg.align) > sizeof(float))
  1478. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1479. TRY((stack.take<ValueType::F32, ValueType::I32>()));
  1480. return {};
  1481. }
  1482. VALIDATE_INSTRUCTION(f64_store)
  1483. {
  1484. TRY(validate(MemoryIndex { 0 }));
  1485. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1486. if ((1ull << arg.align) > sizeof(double))
  1487. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1488. TRY((stack.take<ValueType::F64, ValueType::I32>()));
  1489. return {};
  1490. }
  1491. VALIDATE_INSTRUCTION(i32_store16)
  1492. {
  1493. TRY(validate(MemoryIndex { 0 }));
  1494. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1495. if ((1ull << arg.align) > 16 / 8)
  1496. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1497. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1498. return {};
  1499. }
  1500. VALIDATE_INSTRUCTION(i32_store8)
  1501. {
  1502. TRY(validate(MemoryIndex { 0 }));
  1503. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1504. if ((1ull << arg.align) > 8 / 8)
  1505. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1506. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1507. return {};
  1508. }
  1509. VALIDATE_INSTRUCTION(i64_store32)
  1510. {
  1511. TRY(validate(MemoryIndex { 0 }));
  1512. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1513. if ((1ull << arg.align) > 32 / 8)
  1514. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1515. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1516. return {};
  1517. }
  1518. VALIDATE_INSTRUCTION(i64_store16)
  1519. {
  1520. TRY(validate(MemoryIndex { 0 }));
  1521. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1522. if ((1ull << arg.align) > 16 / 8)
  1523. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1524. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1525. return {};
  1526. }
  1527. VALIDATE_INSTRUCTION(i64_store8)
  1528. {
  1529. TRY(validate(MemoryIndex { 0 }));
  1530. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1531. if ((1ull << arg.align) > 8 / 8)
  1532. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1533. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1534. return {};
  1535. }
  1536. VALIDATE_INSTRUCTION(memory_size)
  1537. {
  1538. TRY(validate(MemoryIndex { 0 }));
  1539. stack.append(ValueType(ValueType::I32));
  1540. return {};
  1541. }
  1542. VALIDATE_INSTRUCTION(memory_grow)
  1543. {
  1544. TRY(validate(MemoryIndex { 0 }));
  1545. TRY((stack.take<ValueType::I32>()));
  1546. return {};
  1547. }
  1548. VALIDATE_INSTRUCTION(memory_fill)
  1549. {
  1550. TRY(validate(MemoryIndex { 0 }));
  1551. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1552. return {};
  1553. }
  1554. VALIDATE_INSTRUCTION(memory_init)
  1555. {
  1556. TRY(validate(MemoryIndex { 0 }));
  1557. auto index = instruction.arguments().get<DataIndex>();
  1558. TRY(validate(index));
  1559. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1560. return {};
  1561. }
  1562. VALIDATE_INSTRUCTION(data_drop)
  1563. {
  1564. auto index = instruction.arguments().get<DataIndex>();
  1565. TRY(validate(index));
  1566. return {};
  1567. }
  1568. // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
  1569. VALIDATE_INSTRUCTION(nop)
  1570. {
  1571. return {};
  1572. }
  1573. VALIDATE_INSTRUCTION(unreachable)
  1574. {
  1575. // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
  1576. stack.append(StackEntry());
  1577. return {};
  1578. }
  1579. // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
  1580. VALIDATE_INSTRUCTION(structured_end)
  1581. {
  1582. if (m_entered_scopes.is_empty())
  1583. return Errors::invalid("usage of structured end");
  1584. auto last_scope = m_entered_scopes.take_last();
  1585. m_context = m_parent_contexts.take_last();
  1586. auto last_block_type = m_entered_blocks.take_last();
  1587. switch (last_scope) {
  1588. case ChildScopeKind::Block:
  1589. case ChildScopeKind::IfWithoutElse:
  1590. case ChildScopeKind::Else:
  1591. m_block_details.take_last();
  1592. break;
  1593. case ChildScopeKind::IfWithElse:
  1594. return Errors::invalid("usage of if without an else clause that appears to have one anyway");
  1595. }
  1596. auto& results = last_block_type.results();
  1597. for (size_t i = 1; i <= results.size(); ++i)
  1598. TRY(stack.take(results[results.size() - i]));
  1599. for (auto& result : results)
  1600. stack.append(result);
  1601. return {};
  1602. }
  1603. // Note: This is *not* from the spec.
  1604. VALIDATE_INSTRUCTION(structured_else)
  1605. {
  1606. if (m_entered_scopes.is_empty())
  1607. return Errors::invalid("usage of structured else");
  1608. if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
  1609. return Errors::invalid("usage of structured else");
  1610. auto& block_type = m_entered_blocks.last();
  1611. auto& results = block_type.results();
  1612. for (size_t i = 1; i <= results.size(); ++i)
  1613. TRY(stack.take(results[results.size() - i]));
  1614. auto& details = m_block_details.last().details.get<BlockDetails::IfDetails>();
  1615. m_entered_scopes.last() = ChildScopeKind::Else;
  1616. stack = move(details.initial_stack);
  1617. return {};
  1618. }
  1619. VALIDATE_INSTRUCTION(block)
  1620. {
  1621. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1622. auto block_type = TRY(validate(args.block_type));
  1623. auto& parameters = block_type.parameters();
  1624. for (size_t i = 1; i <= parameters.size(); ++i)
  1625. TRY(stack.take(parameters[parameters.size() - i]));
  1626. for (auto& parameter : parameters)
  1627. stack.append(parameter);
  1628. m_entered_scopes.append(ChildScopeKind::Block);
  1629. m_block_details.empend(stack.actual_size(), Empty {});
  1630. m_parent_contexts.append(m_context);
  1631. m_entered_blocks.append(block_type);
  1632. m_context.labels.prepend(ResultType { block_type.results() });
  1633. return {};
  1634. }
  1635. VALIDATE_INSTRUCTION(loop)
  1636. {
  1637. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1638. auto block_type = TRY(validate(args.block_type));
  1639. auto& parameters = block_type.parameters();
  1640. for (size_t i = 1; i <= parameters.size(); ++i)
  1641. TRY(stack.take(parameters[parameters.size() - i]));
  1642. for (auto& parameter : parameters)
  1643. stack.append(parameter);
  1644. m_entered_scopes.append(ChildScopeKind::Block);
  1645. m_block_details.empend(stack.actual_size(), Empty {});
  1646. m_parent_contexts.append(m_context);
  1647. m_entered_blocks.append(block_type);
  1648. m_context.labels.prepend(ResultType { block_type.parameters() });
  1649. return {};
  1650. }
  1651. VALIDATE_INSTRUCTION(if_)
  1652. {
  1653. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1654. auto block_type = TRY(validate(args.block_type));
  1655. TRY(stack.take<ValueType::I32>());
  1656. auto stack_snapshot = stack;
  1657. auto& parameters = block_type.parameters();
  1658. for (size_t i = 1; i <= parameters.size(); ++i)
  1659. TRY(stack.take(parameters[parameters.size() - i]));
  1660. for (auto& parameter : parameters)
  1661. stack.append(parameter);
  1662. m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
  1663. m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { move(stack_snapshot) });
  1664. m_parent_contexts.append(m_context);
  1665. m_entered_blocks.append(block_type);
  1666. m_context.labels.prepend(ResultType { block_type.results() });
  1667. return {};
  1668. }
  1669. VALIDATE_INSTRUCTION(br)
  1670. {
  1671. auto label = instruction.arguments().get<LabelIndex>();
  1672. TRY(validate(label));
  1673. auto& type = m_context.labels[label.value()];
  1674. for (size_t i = 1; i <= type.types().size(); ++i)
  1675. TRY(stack.take(type.types()[type.types().size() - i]));
  1676. stack.append(StackEntry());
  1677. return {};
  1678. }
  1679. VALIDATE_INSTRUCTION(br_if)
  1680. {
  1681. auto label = instruction.arguments().get<LabelIndex>();
  1682. TRY(validate(label));
  1683. TRY(stack.take<ValueType::I32>());
  1684. auto& type = m_context.labels[label.value()];
  1685. Vector<StackEntry> entries;
  1686. entries.ensure_capacity(type.types().size());
  1687. for (size_t i = 0; i < type.types().size(); ++i) {
  1688. auto& entry = type.types()[type.types().size() - i - 1];
  1689. TRY(stack.take(entry));
  1690. entries.append(entry);
  1691. }
  1692. for (size_t i = 0; i < entries.size(); ++i)
  1693. stack.append(entries[entries.size() - i - 1]);
  1694. return {};
  1695. }
  1696. VALIDATE_INSTRUCTION(br_table)
  1697. {
  1698. auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
  1699. TRY(validate(args.default_));
  1700. for (auto& label : args.labels)
  1701. TRY(validate(label));
  1702. TRY(stack.take<ValueType::I32>());
  1703. auto& default_types = m_context.labels[args.default_.value()].types();
  1704. auto arity = default_types.size();
  1705. auto stack_snapshot = stack;
  1706. auto stack_to_check = stack_snapshot;
  1707. for (auto& label : args.labels) {
  1708. auto& label_types = m_context.labels[label.value()].types();
  1709. for (size_t i = 0; i < arity; ++i)
  1710. TRY(stack_to_check.take(label_types[label_types.size() - i - 1]));
  1711. stack_to_check = stack_snapshot;
  1712. }
  1713. for (size_t i = 0; i < arity; ++i) {
  1714. auto expected = default_types[default_types.size() - i - 1];
  1715. TRY((stack.take(expected)));
  1716. }
  1717. stack.append(StackEntry());
  1718. return {};
  1719. }
  1720. VALIDATE_INSTRUCTION(return_)
  1721. {
  1722. if (!m_context.return_.has_value())
  1723. return Errors::invalid("use of return outside function");
  1724. auto& return_types = m_context.return_->types();
  1725. for (size_t i = 0; i < return_types.size(); ++i)
  1726. TRY((stack.take(return_types[return_types.size() - i - 1])));
  1727. stack.append(StackEntry());
  1728. return {};
  1729. }
  1730. VALIDATE_INSTRUCTION(call)
  1731. {
  1732. auto index = instruction.arguments().get<FunctionIndex>();
  1733. TRY(validate(index));
  1734. auto& function_type = m_context.functions[index.value()];
  1735. for (size_t i = 0; i < function_type.parameters().size(); ++i)
  1736. TRY(stack.take(function_type.parameters()[function_type.parameters().size() - i - 1]));
  1737. for (auto& type : function_type.results())
  1738. stack.append(type);
  1739. return {};
  1740. }
  1741. VALIDATE_INSTRUCTION(call_indirect)
  1742. {
  1743. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  1744. TRY(validate(args.table));
  1745. TRY(validate(args.type));
  1746. auto& table = m_context.tables[args.table.value()];
  1747. if (!table.element_type().is_reference())
  1748. return Errors::invalid("table element type for call.indirect", "a reference type", table.element_type());
  1749. auto& type = m_context.types[args.type.value()];
  1750. TRY(stack.take<ValueType::I32>());
  1751. for (size_t i = 0; i < type.parameters().size(); ++i)
  1752. TRY(stack.take(type.parameters()[type.parameters().size() - i - 1]));
  1753. for (auto& type : type.results())
  1754. stack.append(type);
  1755. return {};
  1756. }
  1757. ErrorOr<void, ValidationError> Validator::validate(Instruction const& instruction, Stack& stack, bool& is_constant)
  1758. {
  1759. switch (instruction.opcode().value()) {
  1760. #define M(name, integer_value) \
  1761. case Instructions::name.value(): \
  1762. dbgln_if(WASM_VALIDATOR_DEBUG, "checking {}, stack = {}", #name, stack); \
  1763. return validate_instruction<integer_value>(instruction, stack, is_constant);
  1764. ENUMERATE_WASM_OPCODES(M)
  1765. #undef M
  1766. default:
  1767. is_constant = false;
  1768. return Errors::invalid("instruction opcode");
  1769. }
  1770. }
  1771. ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
  1772. {
  1773. Stack stack;
  1774. bool is_constant_expression = true;
  1775. for (auto& instruction : expression.instructions()) {
  1776. bool is_constant = false;
  1777. TRY(validate(instruction, stack, is_constant));
  1778. is_constant_expression &= is_constant;
  1779. }
  1780. auto expected_result_types = result_types;
  1781. while (!expected_result_types.is_empty())
  1782. TRY(stack.take(expected_result_types.take_last()));
  1783. for (auto& type : result_types)
  1784. stack.append(type);
  1785. return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
  1786. }
  1787. bool Validator::Stack::operator==(Stack const& other) const
  1788. {
  1789. if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
  1790. return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
  1791. Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
  1792. auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
  1793. auto own_size = Vector<StackEntry>::size();
  1794. for (size_t i = 0; i < own_size; ++i) {
  1795. if (other_size <= i)
  1796. break;
  1797. auto own_entry = at(own_size - i - 1);
  1798. auto other_entry = other.at(other_size - i - 1);
  1799. if (!own_entry.is_known) {
  1800. own_last_unknown_entry_index_from_end = i;
  1801. break;
  1802. }
  1803. if (!other_entry.is_known) {
  1804. other_last_unknown_entry_index_from_end = i;
  1805. break;
  1806. }
  1807. }
  1808. if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
  1809. if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
  1810. return true;
  1811. dbgln("Equality check internal error between");
  1812. dbgln("stack:");
  1813. for (auto& entry : *this)
  1814. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1815. dbgln("and stack:");
  1816. for (auto& entry : other)
  1817. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1818. VERIFY_NOT_REACHED();
  1819. }
  1820. 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));
  1821. for (size_t i = 0; i < index_from_end; ++i) {
  1822. if (at(own_size - i - 1) != other.at(other_size - i - 1))
  1823. return false;
  1824. }
  1825. return true;
  1826. }
  1827. String Validator::Errors::find_instruction_name(SourceLocation const& location)
  1828. {
  1829. auto index = location.function_name().find('<');
  1830. auto end_index = location.function_name().find('>');
  1831. if (!index.has_value() || !end_index.has_value())
  1832. return String::formatted("{}", location);
  1833. auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_uint();
  1834. if (!opcode.has_value())
  1835. return String::formatted("{}", location);
  1836. return instruction_name(OpCode { *opcode });
  1837. }
  1838. }