Validator.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  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. stack.append(ValueType(ValueType::I32));
  1547. return {};
  1548. }
  1549. VALIDATE_INSTRUCTION(memory_fill)
  1550. {
  1551. TRY(validate(MemoryIndex { 0 }));
  1552. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1553. return {};
  1554. }
  1555. VALIDATE_INSTRUCTION(memory_init)
  1556. {
  1557. TRY(validate(MemoryIndex { 0 }));
  1558. auto index = instruction.arguments().get<DataIndex>();
  1559. TRY(validate(index));
  1560. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1561. return {};
  1562. }
  1563. VALIDATE_INSTRUCTION(data_drop)
  1564. {
  1565. auto index = instruction.arguments().get<DataIndex>();
  1566. TRY(validate(index));
  1567. return {};
  1568. }
  1569. // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
  1570. VALIDATE_INSTRUCTION(nop)
  1571. {
  1572. return {};
  1573. }
  1574. VALIDATE_INSTRUCTION(unreachable)
  1575. {
  1576. // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
  1577. stack.append(StackEntry());
  1578. return {};
  1579. }
  1580. // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
  1581. VALIDATE_INSTRUCTION(structured_end)
  1582. {
  1583. if (m_entered_scopes.is_empty())
  1584. return Errors::invalid("usage of structured end");
  1585. auto last_scope = m_entered_scopes.take_last();
  1586. m_context = m_parent_contexts.take_last();
  1587. auto last_block_type = m_entered_blocks.take_last();
  1588. switch (last_scope) {
  1589. case ChildScopeKind::Block:
  1590. case ChildScopeKind::IfWithoutElse:
  1591. case ChildScopeKind::Else:
  1592. m_block_details.take_last();
  1593. break;
  1594. case ChildScopeKind::IfWithElse:
  1595. return Errors::invalid("usage of if without an else clause that appears to have one anyway");
  1596. }
  1597. auto& results = last_block_type.results();
  1598. for (size_t i = 1; i <= results.size(); ++i)
  1599. TRY(stack.take(results[results.size() - i]));
  1600. for (auto& result : results)
  1601. stack.append(result);
  1602. return {};
  1603. }
  1604. // Note: This is *not* from the spec.
  1605. VALIDATE_INSTRUCTION(structured_else)
  1606. {
  1607. if (m_entered_scopes.is_empty())
  1608. return Errors::invalid("usage of structured else");
  1609. if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
  1610. return Errors::invalid("usage of structured else");
  1611. auto& block_type = m_entered_blocks.last();
  1612. auto& results = block_type.results();
  1613. for (size_t i = 1; i <= results.size(); ++i)
  1614. TRY(stack.take(results[results.size() - i]));
  1615. auto& details = m_block_details.last().details.get<BlockDetails::IfDetails>();
  1616. m_entered_scopes.last() = ChildScopeKind::Else;
  1617. stack = move(details.initial_stack);
  1618. return {};
  1619. }
  1620. VALIDATE_INSTRUCTION(block)
  1621. {
  1622. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1623. auto block_type = TRY(validate(args.block_type));
  1624. auto& parameters = block_type.parameters();
  1625. for (size_t i = 1; i <= parameters.size(); ++i)
  1626. TRY(stack.take(parameters[parameters.size() - i]));
  1627. for (auto& parameter : parameters)
  1628. stack.append(parameter);
  1629. m_entered_scopes.append(ChildScopeKind::Block);
  1630. m_block_details.empend(stack.actual_size(), Empty {});
  1631. m_parent_contexts.append(m_context);
  1632. m_entered_blocks.append(block_type);
  1633. m_context.labels.prepend(ResultType { block_type.results() });
  1634. return {};
  1635. }
  1636. VALIDATE_INSTRUCTION(loop)
  1637. {
  1638. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1639. auto block_type = TRY(validate(args.block_type));
  1640. auto& parameters = block_type.parameters();
  1641. for (size_t i = 1; i <= parameters.size(); ++i)
  1642. TRY(stack.take(parameters[parameters.size() - i]));
  1643. for (auto& parameter : parameters)
  1644. stack.append(parameter);
  1645. m_entered_scopes.append(ChildScopeKind::Block);
  1646. m_block_details.empend(stack.actual_size(), Empty {});
  1647. m_parent_contexts.append(m_context);
  1648. m_entered_blocks.append(block_type);
  1649. m_context.labels.prepend(ResultType { block_type.parameters() });
  1650. return {};
  1651. }
  1652. VALIDATE_INSTRUCTION(if_)
  1653. {
  1654. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1655. auto block_type = TRY(validate(args.block_type));
  1656. TRY(stack.take<ValueType::I32>());
  1657. auto stack_snapshot = stack;
  1658. auto& parameters = block_type.parameters();
  1659. for (size_t i = 1; i <= parameters.size(); ++i)
  1660. TRY(stack.take(parameters[parameters.size() - i]));
  1661. for (auto& parameter : parameters)
  1662. stack.append(parameter);
  1663. m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
  1664. m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { move(stack_snapshot) });
  1665. m_parent_contexts.append(m_context);
  1666. m_entered_blocks.append(block_type);
  1667. m_context.labels.prepend(ResultType { block_type.results() });
  1668. return {};
  1669. }
  1670. VALIDATE_INSTRUCTION(br)
  1671. {
  1672. auto label = instruction.arguments().get<LabelIndex>();
  1673. TRY(validate(label));
  1674. auto& type = m_context.labels[label.value()];
  1675. for (size_t i = 1; i <= type.types().size(); ++i)
  1676. TRY(stack.take(type.types()[type.types().size() - i]));
  1677. stack.append(StackEntry());
  1678. return {};
  1679. }
  1680. VALIDATE_INSTRUCTION(br_if)
  1681. {
  1682. auto label = instruction.arguments().get<LabelIndex>();
  1683. TRY(validate(label));
  1684. TRY(stack.take<ValueType::I32>());
  1685. auto& type = m_context.labels[label.value()];
  1686. Vector<StackEntry> entries;
  1687. entries.ensure_capacity(type.types().size());
  1688. for (size_t i = 0; i < type.types().size(); ++i) {
  1689. auto& entry = type.types()[type.types().size() - i - 1];
  1690. TRY(stack.take(entry));
  1691. entries.append(entry);
  1692. }
  1693. for (size_t i = 0; i < entries.size(); ++i)
  1694. stack.append(entries[entries.size() - i - 1]);
  1695. return {};
  1696. }
  1697. VALIDATE_INSTRUCTION(br_table)
  1698. {
  1699. auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
  1700. TRY(validate(args.default_));
  1701. for (auto& label : args.labels)
  1702. TRY(validate(label));
  1703. TRY(stack.take<ValueType::I32>());
  1704. auto& default_types = m_context.labels[args.default_.value()].types();
  1705. auto arity = default_types.size();
  1706. auto stack_snapshot = stack;
  1707. auto stack_to_check = stack_snapshot;
  1708. for (auto& label : args.labels) {
  1709. auto& label_types = m_context.labels[label.value()].types();
  1710. for (size_t i = 0; i < arity; ++i)
  1711. TRY(stack_to_check.take(label_types[label_types.size() - i - 1]));
  1712. stack_to_check = stack_snapshot;
  1713. }
  1714. for (size_t i = 0; i < arity; ++i) {
  1715. auto expected = default_types[default_types.size() - i - 1];
  1716. TRY((stack.take(expected)));
  1717. }
  1718. stack.append(StackEntry());
  1719. return {};
  1720. }
  1721. VALIDATE_INSTRUCTION(return_)
  1722. {
  1723. if (!m_context.return_.has_value())
  1724. return Errors::invalid("use of return outside function");
  1725. auto& return_types = m_context.return_->types();
  1726. for (size_t i = 0; i < return_types.size(); ++i)
  1727. TRY((stack.take(return_types[return_types.size() - i - 1])));
  1728. stack.append(StackEntry());
  1729. return {};
  1730. }
  1731. VALIDATE_INSTRUCTION(call)
  1732. {
  1733. auto index = instruction.arguments().get<FunctionIndex>();
  1734. TRY(validate(index));
  1735. auto& function_type = m_context.functions[index.value()];
  1736. for (size_t i = 0; i < function_type.parameters().size(); ++i)
  1737. TRY(stack.take(function_type.parameters()[function_type.parameters().size() - i - 1]));
  1738. for (auto& type : function_type.results())
  1739. stack.append(type);
  1740. return {};
  1741. }
  1742. VALIDATE_INSTRUCTION(call_indirect)
  1743. {
  1744. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  1745. TRY(validate(args.table));
  1746. TRY(validate(args.type));
  1747. auto& table = m_context.tables[args.table.value()];
  1748. if (!table.element_type().is_reference())
  1749. return Errors::invalid("table element type for call.indirect", "a reference type", table.element_type());
  1750. auto& type = m_context.types[args.type.value()];
  1751. TRY(stack.take<ValueType::I32>());
  1752. for (size_t i = 0; i < type.parameters().size(); ++i)
  1753. TRY(stack.take(type.parameters()[type.parameters().size() - i - 1]));
  1754. for (auto& type : type.results())
  1755. stack.append(type);
  1756. return {};
  1757. }
  1758. ErrorOr<void, ValidationError> Validator::validate(Instruction const& instruction, Stack& stack, bool& is_constant)
  1759. {
  1760. switch (instruction.opcode().value()) {
  1761. #define M(name, integer_value) \
  1762. case Instructions::name.value(): \
  1763. dbgln_if(WASM_VALIDATOR_DEBUG, "checking {}, stack = {}", #name, stack); \
  1764. return validate_instruction<integer_value>(instruction, stack, is_constant);
  1765. ENUMERATE_WASM_OPCODES(M)
  1766. #undef M
  1767. default:
  1768. is_constant = false;
  1769. return Errors::invalid("instruction opcode");
  1770. }
  1771. }
  1772. ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
  1773. {
  1774. Stack stack;
  1775. bool is_constant_expression = true;
  1776. for (auto& instruction : expression.instructions()) {
  1777. bool is_constant = false;
  1778. TRY(validate(instruction, stack, is_constant));
  1779. is_constant_expression &= is_constant;
  1780. }
  1781. auto expected_result_types = result_types;
  1782. while (!expected_result_types.is_empty())
  1783. TRY(stack.take(expected_result_types.take_last()));
  1784. for (auto& type : result_types)
  1785. stack.append(type);
  1786. return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
  1787. }
  1788. bool Validator::Stack::operator==(Stack const& other) const
  1789. {
  1790. if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
  1791. return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
  1792. Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
  1793. auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
  1794. auto own_size = Vector<StackEntry>::size();
  1795. for (size_t i = 0; i < own_size; ++i) {
  1796. if (other_size <= i)
  1797. break;
  1798. auto own_entry = at(own_size - i - 1);
  1799. auto other_entry = other.at(other_size - i - 1);
  1800. if (!own_entry.is_known) {
  1801. own_last_unknown_entry_index_from_end = i;
  1802. break;
  1803. }
  1804. if (!other_entry.is_known) {
  1805. other_last_unknown_entry_index_from_end = i;
  1806. break;
  1807. }
  1808. }
  1809. if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
  1810. if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
  1811. return true;
  1812. dbgln("Equality check internal error between");
  1813. dbgln("stack:");
  1814. for (auto& entry : *this)
  1815. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1816. dbgln("and stack:");
  1817. for (auto& entry : other)
  1818. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  1819. VERIFY_NOT_REACHED();
  1820. }
  1821. 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));
  1822. for (size_t i = 0; i < index_from_end; ++i) {
  1823. if (at(own_size - i - 1) != other.at(other_size - i - 1))
  1824. return false;
  1825. }
  1826. return true;
  1827. }
  1828. String Validator::Errors::find_instruction_name(SourceLocation const& location)
  1829. {
  1830. auto index = location.function_name().find('<');
  1831. auto end_index = location.function_name().find('>');
  1832. if (!index.has_value() || !end_index.has_value())
  1833. return String::formatted("{}", location);
  1834. auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_uint();
  1835. if (!opcode.has_value())
  1836. return String::formatted("{}", location);
  1837. return instruction_name(OpCode { *opcode });
  1838. }
  1839. }