Validator.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  458. return {};
  459. }
  460. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-binopmathitbinop
  461. VALIDATE_INSTRUCTION(i32_add)
  462. {
  463. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  464. return Errors::invalid_stack_state();
  465. stack.take_last();
  466. stack.append(ValueType(ValueType::I32));
  467. return {};
  468. }
  469. VALIDATE_INSTRUCTION(i32_sub)
  470. {
  471. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  472. return Errors::invalid_stack_state();
  473. stack.take_last();
  474. stack.append(ValueType(ValueType::I32));
  475. return {};
  476. }
  477. VALIDATE_INSTRUCTION(i32_mul)
  478. {
  479. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  480. return Errors::invalid_stack_state();
  481. stack.take_last();
  482. stack.append(ValueType(ValueType::I32));
  483. return {};
  484. }
  485. VALIDATE_INSTRUCTION(i32_divs)
  486. {
  487. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  488. return Errors::invalid_stack_state();
  489. stack.take_last();
  490. stack.append(ValueType(ValueType::I32));
  491. return {};
  492. }
  493. VALIDATE_INSTRUCTION(i32_divu)
  494. {
  495. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  496. return Errors::invalid_stack_state();
  497. stack.take_last();
  498. stack.append(ValueType(ValueType::I32));
  499. return {};
  500. }
  501. VALIDATE_INSTRUCTION(i32_rems)
  502. {
  503. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  504. return Errors::invalid_stack_state();
  505. stack.take_last();
  506. stack.append(ValueType(ValueType::I32));
  507. return {};
  508. }
  509. VALIDATE_INSTRUCTION(i32_remu)
  510. {
  511. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  512. return Errors::invalid_stack_state();
  513. stack.take_last();
  514. stack.append(ValueType(ValueType::I32));
  515. return {};
  516. }
  517. VALIDATE_INSTRUCTION(i32_and)
  518. {
  519. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  520. return Errors::invalid_stack_state();
  521. stack.take_last();
  522. stack.append(ValueType(ValueType::I32));
  523. return {};
  524. }
  525. VALIDATE_INSTRUCTION(i32_or)
  526. {
  527. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  528. return Errors::invalid_stack_state();
  529. stack.take_last();
  530. stack.append(ValueType(ValueType::I32));
  531. return {};
  532. }
  533. VALIDATE_INSTRUCTION(i32_xor)
  534. {
  535. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  536. return Errors::invalid_stack_state();
  537. stack.take_last();
  538. stack.append(ValueType(ValueType::I32));
  539. return {};
  540. }
  541. VALIDATE_INSTRUCTION(i32_shl)
  542. {
  543. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  544. return Errors::invalid_stack_state();
  545. stack.take_last();
  546. stack.append(ValueType(ValueType::I32));
  547. return {};
  548. }
  549. VALIDATE_INSTRUCTION(i32_shrs)
  550. {
  551. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  552. return Errors::invalid_stack_state();
  553. stack.take_last();
  554. stack.append(ValueType(ValueType::I32));
  555. return {};
  556. }
  557. VALIDATE_INSTRUCTION(i32_shru)
  558. {
  559. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  560. return Errors::invalid_stack_state();
  561. stack.take_last();
  562. stack.append(ValueType(ValueType::I32));
  563. return {};
  564. }
  565. VALIDATE_INSTRUCTION(i32_rotl)
  566. {
  567. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  568. return Errors::invalid_stack_state();
  569. stack.take_last();
  570. stack.append(ValueType(ValueType::I32));
  571. return {};
  572. }
  573. VALIDATE_INSTRUCTION(i32_rotr)
  574. {
  575. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  576. return Errors::invalid_stack_state();
  577. stack.take_last();
  578. stack.append(ValueType(ValueType::I32));
  579. return {};
  580. }
  581. VALIDATE_INSTRUCTION(i64_add)
  582. {
  583. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  584. return Errors::invalid_stack_state();
  585. stack.take_last();
  586. stack.append(ValueType(ValueType::I64));
  587. return {};
  588. }
  589. VALIDATE_INSTRUCTION(i64_sub)
  590. {
  591. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  592. return Errors::invalid_stack_state();
  593. stack.take_last();
  594. stack.append(ValueType(ValueType::I64));
  595. return {};
  596. }
  597. VALIDATE_INSTRUCTION(i64_mul)
  598. {
  599. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  600. return Errors::invalid_stack_state();
  601. stack.take_last();
  602. stack.append(ValueType(ValueType::I64));
  603. return {};
  604. }
  605. VALIDATE_INSTRUCTION(i64_divs)
  606. {
  607. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  608. return Errors::invalid_stack_state();
  609. stack.take_last();
  610. stack.append(ValueType(ValueType::I64));
  611. return {};
  612. }
  613. VALIDATE_INSTRUCTION(i64_divu)
  614. {
  615. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  616. return Errors::invalid_stack_state();
  617. stack.take_last();
  618. stack.append(ValueType(ValueType::I64));
  619. return {};
  620. }
  621. VALIDATE_INSTRUCTION(i64_rems)
  622. {
  623. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  624. return Errors::invalid_stack_state();
  625. stack.take_last();
  626. stack.append(ValueType(ValueType::I64));
  627. return {};
  628. }
  629. VALIDATE_INSTRUCTION(i64_remu)
  630. {
  631. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  632. return Errors::invalid_stack_state();
  633. stack.take_last();
  634. stack.append(ValueType(ValueType::I64));
  635. return {};
  636. }
  637. VALIDATE_INSTRUCTION(i64_and)
  638. {
  639. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  640. return Errors::invalid_stack_state();
  641. stack.take_last();
  642. stack.append(ValueType(ValueType::I64));
  643. return {};
  644. }
  645. VALIDATE_INSTRUCTION(i64_or)
  646. {
  647. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  648. return Errors::invalid_stack_state();
  649. stack.take_last();
  650. stack.append(ValueType(ValueType::I64));
  651. return {};
  652. }
  653. VALIDATE_INSTRUCTION(i64_xor)
  654. {
  655. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  656. return Errors::invalid_stack_state();
  657. stack.take_last();
  658. stack.append(ValueType(ValueType::I64));
  659. return {};
  660. }
  661. VALIDATE_INSTRUCTION(i64_shl)
  662. {
  663. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  664. return Errors::invalid_stack_state();
  665. stack.take_last();
  666. stack.append(ValueType(ValueType::I64));
  667. return {};
  668. }
  669. VALIDATE_INSTRUCTION(i64_shrs)
  670. {
  671. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  672. return Errors::invalid_stack_state();
  673. stack.take_last();
  674. stack.append(ValueType(ValueType::I64));
  675. return {};
  676. }
  677. VALIDATE_INSTRUCTION(i64_shru)
  678. {
  679. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  680. return Errors::invalid_stack_state();
  681. stack.take_last();
  682. stack.append(ValueType(ValueType::I64));
  683. return {};
  684. }
  685. VALIDATE_INSTRUCTION(i64_rotl)
  686. {
  687. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  688. return Errors::invalid_stack_state();
  689. stack.take_last();
  690. stack.append(ValueType(ValueType::I64));
  691. return {};
  692. }
  693. VALIDATE_INSTRUCTION(i64_rotr)
  694. {
  695. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  696. return Errors::invalid_stack_state();
  697. stack.take_last();
  698. stack.append(ValueType(ValueType::I64));
  699. return {};
  700. }
  701. VALIDATE_INSTRUCTION(f32_add)
  702. {
  703. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  704. return Errors::invalid_stack_state();
  705. stack.take_last();
  706. stack.append(ValueType(ValueType::F32));
  707. return {};
  708. }
  709. VALIDATE_INSTRUCTION(f32_sub)
  710. {
  711. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  712. return Errors::invalid_stack_state();
  713. stack.take_last();
  714. stack.append(ValueType(ValueType::F32));
  715. return {};
  716. }
  717. VALIDATE_INSTRUCTION(f32_mul)
  718. {
  719. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  720. return Errors::invalid_stack_state();
  721. stack.take_last();
  722. stack.append(ValueType(ValueType::F32));
  723. return {};
  724. }
  725. VALIDATE_INSTRUCTION(f32_div)
  726. {
  727. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  728. return Errors::invalid_stack_state();
  729. stack.take_last();
  730. stack.append(ValueType(ValueType::F32));
  731. return {};
  732. }
  733. VALIDATE_INSTRUCTION(f32_min)
  734. {
  735. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  736. return Errors::invalid_stack_state();
  737. stack.take_last();
  738. stack.append(ValueType(ValueType::F32));
  739. return {};
  740. }
  741. VALIDATE_INSTRUCTION(f32_max)
  742. {
  743. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  744. return Errors::invalid_stack_state();
  745. stack.take_last();
  746. stack.append(ValueType(ValueType::F32));
  747. return {};
  748. }
  749. VALIDATE_INSTRUCTION(f32_copysign)
  750. {
  751. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  752. return Errors::invalid_stack_state();
  753. stack.take_last();
  754. stack.append(ValueType(ValueType::F32));
  755. return {};
  756. }
  757. VALIDATE_INSTRUCTION(f64_add)
  758. {
  759. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  760. return Errors::invalid_stack_state();
  761. stack.take_last();
  762. stack.append(ValueType(ValueType::F64));
  763. return {};
  764. }
  765. VALIDATE_INSTRUCTION(f64_sub)
  766. {
  767. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  768. return Errors::invalid_stack_state();
  769. stack.take_last();
  770. stack.append(ValueType(ValueType::F64));
  771. return {};
  772. }
  773. VALIDATE_INSTRUCTION(f64_mul)
  774. {
  775. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  776. return Errors::invalid_stack_state();
  777. stack.take_last();
  778. stack.append(ValueType(ValueType::F64));
  779. return {};
  780. }
  781. VALIDATE_INSTRUCTION(f64_div)
  782. {
  783. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  784. return Errors::invalid_stack_state();
  785. stack.take_last();
  786. stack.append(ValueType(ValueType::F64));
  787. return {};
  788. }
  789. VALIDATE_INSTRUCTION(f64_min)
  790. {
  791. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  792. return Errors::invalid_stack_state();
  793. stack.take_last();
  794. stack.append(ValueType(ValueType::F64));
  795. return {};
  796. }
  797. VALIDATE_INSTRUCTION(f64_max)
  798. {
  799. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  800. return Errors::invalid_stack_state();
  801. stack.take_last();
  802. stack.append(ValueType(ValueType::F64));
  803. return {};
  804. }
  805. VALIDATE_INSTRUCTION(f64_copysign)
  806. {
  807. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  808. return Errors::invalid_stack_state();
  809. stack.take_last();
  810. stack.append(ValueType(ValueType::F64));
  811. return {};
  812. }
  813. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-testopmathittestop
  814. VALIDATE_INSTRUCTION(i32_eqz)
  815. {
  816. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  817. return Errors::invalid_stack_state();
  818. return {};
  819. }
  820. VALIDATE_INSTRUCTION(i64_eqz)
  821. {
  822. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  823. return Errors::invalid_stack_state();
  824. stack.take_last();
  825. stack.append(ValueType(ValueType::I32));
  826. return {};
  827. }
  828. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-relopmathitrelop
  829. VALIDATE_INSTRUCTION(i32_eq)
  830. {
  831. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  832. return Errors::invalid_stack_state();
  833. stack.take_last();
  834. stack.append(ValueType(ValueType::I32));
  835. return {};
  836. }
  837. VALIDATE_INSTRUCTION(i32_ne)
  838. {
  839. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  840. return Errors::invalid_stack_state();
  841. stack.take_last();
  842. stack.append(ValueType(ValueType::I32));
  843. return {};
  844. }
  845. VALIDATE_INSTRUCTION(i32_lts)
  846. {
  847. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  848. return Errors::invalid_stack_state();
  849. stack.take_last();
  850. stack.append(ValueType(ValueType::I32));
  851. return {};
  852. }
  853. VALIDATE_INSTRUCTION(i32_ltu)
  854. {
  855. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  856. return Errors::invalid_stack_state();
  857. stack.take_last();
  858. stack.append(ValueType(ValueType::I32));
  859. return {};
  860. }
  861. VALIDATE_INSTRUCTION(i32_gts)
  862. {
  863. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  864. return Errors::invalid_stack_state();
  865. stack.take_last();
  866. stack.append(ValueType(ValueType::I32));
  867. return {};
  868. }
  869. VALIDATE_INSTRUCTION(i32_gtu)
  870. {
  871. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  872. return Errors::invalid_stack_state();
  873. stack.take_last();
  874. stack.append(ValueType(ValueType::I32));
  875. return {};
  876. }
  877. VALIDATE_INSTRUCTION(i32_les)
  878. {
  879. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  880. return Errors::invalid_stack_state();
  881. stack.take_last();
  882. stack.append(ValueType(ValueType::I32));
  883. return {};
  884. }
  885. VALIDATE_INSTRUCTION(i32_leu)
  886. {
  887. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  888. return Errors::invalid_stack_state();
  889. stack.take_last();
  890. stack.append(ValueType(ValueType::I32));
  891. return {};
  892. }
  893. VALIDATE_INSTRUCTION(i32_ges)
  894. {
  895. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  896. return Errors::invalid_stack_state();
  897. stack.take_last();
  898. stack.append(ValueType(ValueType::I32));
  899. return {};
  900. }
  901. VALIDATE_INSTRUCTION(i32_geu)
  902. {
  903. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I32))
  904. return Errors::invalid_stack_state();
  905. stack.take_last();
  906. stack.append(ValueType(ValueType::I32));
  907. return {};
  908. }
  909. VALIDATE_INSTRUCTION(i64_eq)
  910. {
  911. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  912. return Errors::invalid_stack_state();
  913. stack.take_last();
  914. stack.append(ValueType(ValueType::I32));
  915. return {};
  916. }
  917. VALIDATE_INSTRUCTION(i64_ne)
  918. {
  919. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  920. return Errors::invalid_stack_state();
  921. stack.take_last();
  922. stack.append(ValueType(ValueType::I32));
  923. return {};
  924. }
  925. VALIDATE_INSTRUCTION(i64_lts)
  926. {
  927. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  928. return Errors::invalid_stack_state();
  929. stack.take_last();
  930. stack.append(ValueType(ValueType::I32));
  931. return {};
  932. }
  933. VALIDATE_INSTRUCTION(i64_ltu)
  934. {
  935. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  936. return Errors::invalid_stack_state();
  937. stack.take_last();
  938. stack.append(ValueType(ValueType::I32));
  939. return {};
  940. }
  941. VALIDATE_INSTRUCTION(i64_gts)
  942. {
  943. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  944. return Errors::invalid_stack_state();
  945. stack.take_last();
  946. stack.append(ValueType(ValueType::I32));
  947. return {};
  948. }
  949. VALIDATE_INSTRUCTION(i64_gtu)
  950. {
  951. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  952. return Errors::invalid_stack_state();
  953. stack.take_last();
  954. stack.append(ValueType(ValueType::I32));
  955. return {};
  956. }
  957. VALIDATE_INSTRUCTION(i64_les)
  958. {
  959. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  960. return Errors::invalid_stack_state();
  961. stack.take_last();
  962. stack.append(ValueType(ValueType::I32));
  963. return {};
  964. }
  965. VALIDATE_INSTRUCTION(i64_leu)
  966. {
  967. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  968. return Errors::invalid_stack_state();
  969. stack.take_last();
  970. stack.append(ValueType(ValueType::I32));
  971. return {};
  972. }
  973. VALIDATE_INSTRUCTION(i64_ges)
  974. {
  975. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  976. return Errors::invalid_stack_state();
  977. stack.take_last();
  978. stack.append(ValueType(ValueType::I32));
  979. return {};
  980. }
  981. VALIDATE_INSTRUCTION(i64_geu)
  982. {
  983. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::I64))
  984. return Errors::invalid_stack_state();
  985. stack.take_last();
  986. stack.append(ValueType(ValueType::I32));
  987. return {};
  988. }
  989. VALIDATE_INSTRUCTION(f32_eq)
  990. {
  991. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  992. return Errors::invalid_stack_state();
  993. stack.take_last();
  994. stack.append(ValueType(ValueType::I32));
  995. return {};
  996. }
  997. VALIDATE_INSTRUCTION(f32_ne)
  998. {
  999. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  1000. return Errors::invalid_stack_state();
  1001. stack.take_last();
  1002. stack.append(ValueType(ValueType::I32));
  1003. return {};
  1004. }
  1005. VALIDATE_INSTRUCTION(f32_lt)
  1006. {
  1007. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  1008. return Errors::invalid_stack_state();
  1009. stack.take_last();
  1010. stack.append(ValueType(ValueType::I32));
  1011. return {};
  1012. }
  1013. VALIDATE_INSTRUCTION(f32_le)
  1014. {
  1015. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  1016. return Errors::invalid_stack_state();
  1017. stack.take_last();
  1018. stack.append(ValueType(ValueType::I32));
  1019. return {};
  1020. }
  1021. VALIDATE_INSTRUCTION(f32_gt)
  1022. {
  1023. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  1024. return Errors::invalid_stack_state();
  1025. stack.take_last();
  1026. stack.append(ValueType(ValueType::I32));
  1027. return {};
  1028. }
  1029. VALIDATE_INSTRUCTION(f32_ge)
  1030. {
  1031. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F32))
  1032. return Errors::invalid_stack_state();
  1033. stack.take_last();
  1034. stack.append(ValueType(ValueType::I32));
  1035. return {};
  1036. }
  1037. VALIDATE_INSTRUCTION(f64_eq)
  1038. {
  1039. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1040. return Errors::invalid_stack_state();
  1041. stack.take_last();
  1042. stack.append(ValueType(ValueType::I32));
  1043. return {};
  1044. }
  1045. VALIDATE_INSTRUCTION(f64_ne)
  1046. {
  1047. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1048. return Errors::invalid_stack_state();
  1049. stack.take_last();
  1050. stack.append(ValueType(ValueType::I32));
  1051. return {};
  1052. }
  1053. VALIDATE_INSTRUCTION(f64_lt)
  1054. {
  1055. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1056. return Errors::invalid_stack_state();
  1057. stack.take_last();
  1058. stack.append(ValueType(ValueType::I32));
  1059. return {};
  1060. }
  1061. VALIDATE_INSTRUCTION(f64_le)
  1062. {
  1063. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1064. return Errors::invalid_stack_state();
  1065. stack.take_last();
  1066. stack.append(ValueType(ValueType::I32));
  1067. return {};
  1068. }
  1069. VALIDATE_INSTRUCTION(f64_gt)
  1070. {
  1071. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1072. return Errors::invalid_stack_state();
  1073. stack.take_last();
  1074. stack.append(ValueType(ValueType::I32));
  1075. return {};
  1076. }
  1077. VALIDATE_INSTRUCTION(f64_ge)
  1078. {
  1079. if (stack.size() < 2 || stack.take_last() != stack.last() || !stack.last().is_of_kind(ValueType::F64))
  1080. return Errors::invalid_stack_state();
  1081. stack.take_last();
  1082. stack.append(ValueType(ValueType::I32));
  1083. return {};
  1084. }
  1085. // https://webassembly.github.io/spec/core/bikeshed/#-t_2mathsfhrefsyntax-cvtopmathitcvtopmathsf_t_1mathsf_hrefsyntax-sxmathitsx
  1086. VALIDATE_INSTRUCTION(i32_wrap_i64)
  1087. {
  1088. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1089. return Errors::invalid_stack_state();
  1090. stack.take_last();
  1091. stack.append(ValueType(ValueType::I32));
  1092. return {};
  1093. }
  1094. VALIDATE_INSTRUCTION(i64_extend_si32)
  1095. {
  1096. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1097. return Errors::invalid_stack_state();
  1098. stack.take_last();
  1099. stack.append(ValueType(ValueType::I64));
  1100. return {};
  1101. }
  1102. VALIDATE_INSTRUCTION(i64_extend_ui32)
  1103. {
  1104. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1105. return Errors::invalid_stack_state();
  1106. stack.take_last();
  1107. stack.append(ValueType(ValueType::I64));
  1108. return {};
  1109. }
  1110. VALIDATE_INSTRUCTION(i32_trunc_sf32)
  1111. {
  1112. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1113. return Errors::invalid_stack_state();
  1114. stack.take_last();
  1115. stack.append(ValueType(ValueType::I32));
  1116. return {};
  1117. }
  1118. VALIDATE_INSTRUCTION(i32_trunc_uf32)
  1119. {
  1120. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1121. return Errors::invalid_stack_state();
  1122. stack.take_last();
  1123. stack.append(ValueType(ValueType::I32));
  1124. return {};
  1125. }
  1126. VALIDATE_INSTRUCTION(i32_trunc_sf64)
  1127. {
  1128. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1129. return Errors::invalid_stack_state();
  1130. stack.take_last();
  1131. stack.append(ValueType(ValueType::I32));
  1132. return {};
  1133. }
  1134. VALIDATE_INSTRUCTION(i32_trunc_uf64)
  1135. {
  1136. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1137. return Errors::invalid_stack_state();
  1138. stack.take_last();
  1139. stack.append(ValueType(ValueType::I32));
  1140. return {};
  1141. }
  1142. VALIDATE_INSTRUCTION(i64_trunc_sf32)
  1143. {
  1144. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1145. return Errors::invalid_stack_state();
  1146. stack.take_last();
  1147. stack.append(ValueType(ValueType::I64));
  1148. return {};
  1149. }
  1150. VALIDATE_INSTRUCTION(i64_trunc_uf32)
  1151. {
  1152. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1153. return Errors::invalid_stack_state();
  1154. stack.take_last();
  1155. stack.append(ValueType(ValueType::I64));
  1156. return {};
  1157. }
  1158. VALIDATE_INSTRUCTION(i64_trunc_sf64)
  1159. {
  1160. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1161. return Errors::invalid_stack_state();
  1162. stack.take_last();
  1163. stack.append(ValueType(ValueType::I64));
  1164. return {};
  1165. }
  1166. VALIDATE_INSTRUCTION(i64_trunc_uf64)
  1167. {
  1168. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1169. return Errors::invalid_stack_state();
  1170. stack.take_last();
  1171. stack.append(ValueType(ValueType::I64));
  1172. return {};
  1173. }
  1174. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_s)
  1175. {
  1176. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1177. return Errors::invalid_stack_state();
  1178. stack.take_last();
  1179. stack.append(ValueType(ValueType::I32));
  1180. return {};
  1181. }
  1182. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_u)
  1183. {
  1184. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1185. return Errors::invalid_stack_state();
  1186. stack.take_last();
  1187. stack.append(ValueType(ValueType::I32));
  1188. return {};
  1189. }
  1190. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_s)
  1191. {
  1192. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1193. return Errors::invalid_stack_state();
  1194. stack.take_last();
  1195. stack.append(ValueType(ValueType::I32));
  1196. return {};
  1197. }
  1198. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_u)
  1199. {
  1200. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1201. return Errors::invalid_stack_state();
  1202. stack.take_last();
  1203. stack.append(ValueType(ValueType::I32));
  1204. return {};
  1205. }
  1206. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_s)
  1207. {
  1208. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1209. return Errors::invalid_stack_state();
  1210. stack.take_last();
  1211. stack.append(ValueType(ValueType::I64));
  1212. return {};
  1213. }
  1214. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_u)
  1215. {
  1216. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1217. return Errors::invalid_stack_state();
  1218. stack.take_last();
  1219. stack.append(ValueType(ValueType::I64));
  1220. return {};
  1221. }
  1222. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_s)
  1223. {
  1224. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1225. return Errors::invalid_stack_state();
  1226. stack.take_last();
  1227. stack.append(ValueType(ValueType::I64));
  1228. return {};
  1229. }
  1230. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_u)
  1231. {
  1232. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1233. return Errors::invalid_stack_state();
  1234. stack.take_last();
  1235. stack.append(ValueType(ValueType::I64));
  1236. return {};
  1237. }
  1238. VALIDATE_INSTRUCTION(f32_convert_si32)
  1239. {
  1240. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1241. return Errors::invalid_stack_state();
  1242. stack.take_last();
  1243. stack.append(ValueType(ValueType::F32));
  1244. return {};
  1245. }
  1246. VALIDATE_INSTRUCTION(f32_convert_ui32)
  1247. {
  1248. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1249. return Errors::invalid_stack_state();
  1250. stack.take_last();
  1251. stack.append(ValueType(ValueType::F32));
  1252. return {};
  1253. }
  1254. VALIDATE_INSTRUCTION(f32_convert_si64)
  1255. {
  1256. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1257. return Errors::invalid_stack_state();
  1258. stack.take_last();
  1259. stack.append(ValueType(ValueType::F32));
  1260. return {};
  1261. }
  1262. VALIDATE_INSTRUCTION(f32_convert_ui64)
  1263. {
  1264. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1265. return Errors::invalid_stack_state();
  1266. stack.take_last();
  1267. stack.append(ValueType(ValueType::F32));
  1268. return {};
  1269. }
  1270. VALIDATE_INSTRUCTION(f64_convert_si32)
  1271. {
  1272. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1273. return Errors::invalid_stack_state();
  1274. stack.take_last();
  1275. stack.append(ValueType(ValueType::F64));
  1276. return {};
  1277. }
  1278. VALIDATE_INSTRUCTION(f64_convert_ui32)
  1279. {
  1280. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1281. return Errors::invalid_stack_state();
  1282. stack.take_last();
  1283. stack.append(ValueType(ValueType::F64));
  1284. return {};
  1285. }
  1286. VALIDATE_INSTRUCTION(f64_convert_si64)
  1287. {
  1288. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1289. return Errors::invalid_stack_state();
  1290. stack.take_last();
  1291. stack.append(ValueType(ValueType::F64));
  1292. return {};
  1293. }
  1294. VALIDATE_INSTRUCTION(f64_convert_ui64)
  1295. {
  1296. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1297. return Errors::invalid_stack_state();
  1298. stack.take_last();
  1299. stack.append(ValueType(ValueType::F64));
  1300. return {};
  1301. }
  1302. VALIDATE_INSTRUCTION(f32_demote_f64)
  1303. {
  1304. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1305. return Errors::invalid_stack_state();
  1306. stack.take_last();
  1307. stack.append(ValueType(ValueType::F32));
  1308. return {};
  1309. }
  1310. VALIDATE_INSTRUCTION(f64_promote_f32)
  1311. {
  1312. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  1313. return Errors::invalid_stack_state();
  1314. stack.take_last();
  1315. stack.append(ValueType(ValueType::F64));
  1316. return {};
  1317. }
  1318. VALIDATE_INSTRUCTION(f32_reinterpret_i32)
  1319. {
  1320. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1321. return Errors::invalid_stack_state();
  1322. stack.take_last();
  1323. stack.append(ValueType(ValueType::F32));
  1324. return {};
  1325. }
  1326. VALIDATE_INSTRUCTION(f64_reinterpret_i64)
  1327. {
  1328. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  1329. return Errors::invalid_stack_state();
  1330. stack.take_last();
  1331. stack.append(ValueType(ValueType::F64));
  1332. return {};
  1333. }
  1334. VALIDATE_INSTRUCTION(i32_reinterpret_f32)
  1335. {
  1336. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1337. return Errors::invalid_stack_state();
  1338. stack.take_last();
  1339. stack.append(ValueType(ValueType::F32));
  1340. return {};
  1341. }
  1342. VALIDATE_INSTRUCTION(i64_reinterpret_f64)
  1343. {
  1344. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  1345. return Errors::invalid_stack_state();
  1346. stack.take_last();
  1347. stack.append(ValueType(ValueType::I64));
  1348. return {};
  1349. }
  1350. // https://webassembly.github.io/spec/core/bikeshed/#reference-instructions%E2%91%A2
  1351. VALIDATE_INSTRUCTION(ref_null)
  1352. {
  1353. is_constant = true;
  1354. stack.append(instruction.arguments().get<ValueType>());
  1355. return {};
  1356. }
  1357. VALIDATE_INSTRUCTION(ref_is_null)
  1358. {
  1359. if (stack.is_empty() || !stack.last().is_reference())
  1360. return Errors::invalid_stack_state();
  1361. stack.take_last();
  1362. stack.append(ValueType(ValueType::I32));
  1363. return {};
  1364. }
  1365. VALIDATE_INSTRUCTION(ref_func)
  1366. {
  1367. auto index = instruction.arguments().get<FunctionIndex>();
  1368. TRY(validate(index));
  1369. if (!m_context.references.contains(index))
  1370. return Errors::invalid("function reference");
  1371. is_constant = true;
  1372. stack.append(ValueType(ValueType::FunctionReference));
  1373. return {};
  1374. }
  1375. // https://webassembly.github.io/spec/core/bikeshed/#parametric-instructions%E2%91%A2
  1376. VALIDATE_INSTRUCTION(drop)
  1377. {
  1378. if (stack.is_empty())
  1379. return Errors::invalid_stack_state();
  1380. stack.take_last();
  1381. return {};
  1382. }
  1383. VALIDATE_INSTRUCTION(select)
  1384. {
  1385. if (stack.size() < 3)
  1386. return Errors::invalid_stack_state();
  1387. auto index_type = stack.take_last();
  1388. auto arg0_type = stack.take_last();
  1389. auto& arg1_type = stack.last();
  1390. if (!index_type.is_of_kind(ValueType::I32))
  1391. return Errors::invalid("select index type", ValueType(ValueType::I32), index_type);
  1392. if (arg0_type != arg1_type)
  1393. return Errors::invalid("select argument types", Vector { arg0_type, arg0_type }, Vector { arg0_type, arg1_type });
  1394. return {};
  1395. }
  1396. VALIDATE_INSTRUCTION(select_typed)
  1397. {
  1398. if (stack.size() < 3)
  1399. return Errors::invalid_stack_state();
  1400. auto& required_types = instruction.arguments().get<Vector<ValueType>>();
  1401. if (required_types.size() != 1)
  1402. return Errors::invalid("select types", "exactly one type", required_types);
  1403. auto index_type = stack.take_last();
  1404. auto arg0_type = stack.take_last();
  1405. auto& arg1_type = stack.last();
  1406. if (!index_type.is_of_kind(ValueType::I32))
  1407. return Errors::invalid("select index type", ValueType(ValueType::I32), index_type);
  1408. if (arg0_type != arg1_type || arg0_type != required_types.first())
  1409. return Errors::invalid("select argument types", Vector { required_types.first(), required_types.first() }, Vector { arg0_type, arg1_type });
  1410. return {};
  1411. }
  1412. // https://webassembly.github.io/spec/core/bikeshed/#variable-instructions%E2%91%A2
  1413. VALIDATE_INSTRUCTION(local_get)
  1414. {
  1415. auto index = instruction.arguments().get<LocalIndex>();
  1416. TRY(validate(index));
  1417. stack.append(m_context.locals[index.value()]);
  1418. return {};
  1419. }
  1420. VALIDATE_INSTRUCTION(local_set)
  1421. {
  1422. auto index = instruction.arguments().get<LocalIndex>();
  1423. TRY(validate(index));
  1424. auto& value_type = m_context.locals[index.value()];
  1425. if (stack.take_last() != value_type)
  1426. return Errors::invalid_stack_state();
  1427. return {};
  1428. }
  1429. VALIDATE_INSTRUCTION(local_tee)
  1430. {
  1431. auto index = instruction.arguments().get<LocalIndex>();
  1432. TRY(validate(index));
  1433. auto& value_type = m_context.locals[index.value()];
  1434. if (stack.last() != value_type)
  1435. return Errors::invalid_stack_state();
  1436. return {};
  1437. }
  1438. VALIDATE_INSTRUCTION(global_get)
  1439. {
  1440. auto index = instruction.arguments().get<GlobalIndex>();
  1441. TRY(validate(index));
  1442. auto& global = m_context.globals[index.value()];
  1443. is_constant = !global.is_mutable();
  1444. stack.append(global.type());
  1445. return {};
  1446. }
  1447. VALIDATE_INSTRUCTION(global_set)
  1448. {
  1449. auto index = instruction.arguments().get<GlobalIndex>();
  1450. TRY(validate(index));
  1451. auto& global = m_context.globals[index.value()];
  1452. if (!global.is_mutable())
  1453. return Errors::invalid("global variable for global.set");
  1454. stack.append(global.type());
  1455. return {};
  1456. }
  1457. // https://webassembly.github.io/spec/core/bikeshed/#table-instructions%E2%91%A2
  1458. VALIDATE_INSTRUCTION(table_get)
  1459. {
  1460. auto index = instruction.arguments().get<TableIndex>();
  1461. TRY(validate(index));
  1462. auto& table = m_context.tables[index.value()];
  1463. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1464. return Errors::invalid_stack_state();
  1465. stack.take_last();
  1466. stack.append(table.element_type());
  1467. return {};
  1468. }
  1469. VALIDATE_INSTRUCTION(table_set)
  1470. {
  1471. auto index = instruction.arguments().get<TableIndex>();
  1472. TRY(validate(index));
  1473. auto& table = m_context.tables[index.value()];
  1474. if (stack.is_empty())
  1475. return Errors::invalid_stack_state();
  1476. if (stack.take_last() != table.element_type())
  1477. return Errors::invalid_stack_state();
  1478. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1479. return Errors::invalid_stack_state();
  1480. return {};
  1481. }
  1482. VALIDATE_INSTRUCTION(table_size)
  1483. {
  1484. auto index = instruction.arguments().get<TableIndex>();
  1485. TRY(validate(index));
  1486. stack.append(ValueType(ValueType::I32));
  1487. return {};
  1488. }
  1489. VALIDATE_INSTRUCTION(table_grow)
  1490. {
  1491. auto index = instruction.arguments().get<TableIndex>();
  1492. TRY(validate(index));
  1493. auto& table = m_context.tables[index.value()];
  1494. if (stack.is_empty())
  1495. return Errors::invalid_stack_state();
  1496. if (!stack.take_last().is_of_kind(ValueType::I32))
  1497. return Errors::invalid_stack_state();
  1498. if (stack.is_empty() || stack.take_last() != table.element_type())
  1499. return Errors::invalid_stack_state();
  1500. stack.append(ValueType(ValueType::I32));
  1501. return {};
  1502. }
  1503. VALIDATE_INSTRUCTION(table_fill)
  1504. {
  1505. auto index = instruction.arguments().get<TableIndex>();
  1506. TRY(validate(index));
  1507. auto& table = m_context.tables[index.value()];
  1508. if (stack.is_empty())
  1509. return Errors::invalid_stack_state();
  1510. if (!stack.take_last().is_of_kind(ValueType::I32))
  1511. return Errors::invalid_stack_state();
  1512. if (stack.is_empty() || stack.take_last() != table.element_type())
  1513. return Errors::invalid_stack_state();
  1514. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1515. return Errors::invalid_stack_state();
  1516. return {};
  1517. }
  1518. VALIDATE_INSTRUCTION(table_copy)
  1519. {
  1520. auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
  1521. TRY(validate(args.lhs));
  1522. TRY(validate(args.rhs));
  1523. auto& lhs_table = m_context.tables[args.lhs.value()];
  1524. auto& rhs_table = m_context.tables[args.rhs.value()];
  1525. if (lhs_table.element_type() != rhs_table.element_type())
  1526. return Errors::non_conforming_types("table.copy", lhs_table.element_type(), rhs_table.element_type());
  1527. if (!lhs_table.element_type().is_reference())
  1528. return Errors::invalid("table.copy element type", "a reference type", lhs_table.element_type());
  1529. if (stack.size() < 3)
  1530. return Errors::invalid_stack_state();
  1531. for (size_t i = 0; i < 3; ++i) {
  1532. if (!stack.take_last().is_of_kind(ValueType::I32))
  1533. return Errors::invalid_stack_state();
  1534. }
  1535. return {};
  1536. }
  1537. VALIDATE_INSTRUCTION(table_init)
  1538. {
  1539. auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
  1540. TRY(validate(args.table_index));
  1541. TRY(validate(args.element_index));
  1542. auto& table = m_context.tables[args.table_index.value()];
  1543. auto& element_type = m_context.elements[args.element_index.value()];
  1544. if (table.element_type() != element_type)
  1545. return Errors::non_conforming_types("table.init", table.element_type(), element_type);
  1546. if (stack.size() < 3)
  1547. return Errors::invalid_stack_state();
  1548. for (size_t i = 0; i < 3; ++i) {
  1549. if (!stack.take_last().is_of_kind(ValueType::I32))
  1550. return Errors::invalid_stack_state();
  1551. }
  1552. return {};
  1553. }
  1554. VALIDATE_INSTRUCTION(elem_drop)
  1555. {
  1556. auto index = instruction.arguments().get<ElementIndex>();
  1557. TRY(validate(index));
  1558. return {};
  1559. }
  1560. // https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A2
  1561. VALIDATE_INSTRUCTION(i32_load)
  1562. {
  1563. TRY(validate(MemoryIndex { 0 }));
  1564. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1565. if ((1ull << arg.align) > sizeof(i32))
  1566. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1567. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1568. return Errors::invalid_stack_state();
  1569. return {};
  1570. }
  1571. VALIDATE_INSTRUCTION(i64_load)
  1572. {
  1573. TRY(validate(MemoryIndex { 0 }));
  1574. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1575. if ((1ull << arg.align) > sizeof(i64))
  1576. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1577. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1578. return Errors::invalid_stack_state();
  1579. stack.take_last();
  1580. stack.append(ValueType(ValueType::I64));
  1581. return {};
  1582. }
  1583. VALIDATE_INSTRUCTION(f32_load)
  1584. {
  1585. TRY(validate(MemoryIndex { 0 }));
  1586. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1587. if ((1ull << arg.align) > sizeof(float))
  1588. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1589. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1590. return Errors::invalid_stack_state();
  1591. stack.take_last();
  1592. stack.append(ValueType(ValueType::F32));
  1593. return {};
  1594. }
  1595. VALIDATE_INSTRUCTION(f64_load)
  1596. {
  1597. TRY(validate(MemoryIndex { 0 }));
  1598. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1599. if ((1ull << arg.align) > sizeof(double))
  1600. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1601. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1602. return Errors::invalid_stack_state();
  1603. stack.take_last();
  1604. stack.append(ValueType(ValueType::F64));
  1605. return {};
  1606. }
  1607. VALIDATE_INSTRUCTION(i32_load16_s)
  1608. {
  1609. TRY(validate(MemoryIndex { 0 }));
  1610. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1611. if ((1ull << arg.align) > 16 / 8)
  1612. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1613. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1614. return Errors::invalid_stack_state();
  1615. stack.take_last();
  1616. stack.append(ValueType(ValueType::I32));
  1617. return {};
  1618. }
  1619. VALIDATE_INSTRUCTION(i32_load16_u)
  1620. {
  1621. TRY(validate(MemoryIndex { 0 }));
  1622. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1623. if ((1ull << arg.align) > 16 / 8)
  1624. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1625. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1626. return Errors::invalid_stack_state();
  1627. stack.take_last();
  1628. stack.append(ValueType(ValueType::I32));
  1629. return {};
  1630. }
  1631. VALIDATE_INSTRUCTION(i32_load8_s)
  1632. {
  1633. TRY(validate(MemoryIndex { 0 }));
  1634. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1635. if ((1ull << arg.align) > 8 / 8)
  1636. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1637. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1638. return Errors::invalid_stack_state();
  1639. stack.take_last();
  1640. stack.append(ValueType(ValueType::I32));
  1641. return {};
  1642. }
  1643. VALIDATE_INSTRUCTION(i32_load8_u)
  1644. {
  1645. TRY(validate(MemoryIndex { 0 }));
  1646. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1647. if ((1ull << arg.align) > 8 / 8)
  1648. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1649. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1650. return Errors::invalid_stack_state();
  1651. stack.take_last();
  1652. stack.append(ValueType(ValueType::I32));
  1653. return {};
  1654. }
  1655. VALIDATE_INSTRUCTION(i64_load32_s)
  1656. {
  1657. TRY(validate(MemoryIndex { 0 }));
  1658. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1659. if ((1ull << arg.align) > 32 / 8)
  1660. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1661. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1662. return Errors::invalid_stack_state();
  1663. stack.take_last();
  1664. stack.append(ValueType(ValueType::I64));
  1665. return {};
  1666. }
  1667. VALIDATE_INSTRUCTION(i64_load32_u)
  1668. {
  1669. TRY(validate(MemoryIndex { 0 }));
  1670. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1671. if ((1ull << arg.align) > 32 / 8)
  1672. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1673. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1674. return Errors::invalid_stack_state();
  1675. stack.take_last();
  1676. stack.append(ValueType(ValueType::I64));
  1677. return {};
  1678. }
  1679. VALIDATE_INSTRUCTION(i64_load16_s)
  1680. {
  1681. TRY(validate(MemoryIndex { 0 }));
  1682. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1683. if ((1ull << arg.align) > 16 / 8)
  1684. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1685. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1686. return Errors::invalid_stack_state();
  1687. stack.take_last();
  1688. stack.append(ValueType(ValueType::I64));
  1689. return {};
  1690. }
  1691. VALIDATE_INSTRUCTION(i64_load16_u)
  1692. {
  1693. TRY(validate(MemoryIndex { 0 }));
  1694. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1695. if ((1ull << arg.align) > 16 / 8)
  1696. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1697. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1698. return Errors::invalid_stack_state();
  1699. stack.take_last();
  1700. stack.append(ValueType(ValueType::I64));
  1701. return {};
  1702. }
  1703. VALIDATE_INSTRUCTION(i64_load8_s)
  1704. {
  1705. TRY(validate(MemoryIndex { 0 }));
  1706. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1707. if ((1ull << arg.align) > 8 / 8)
  1708. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1709. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1710. return Errors::invalid_stack_state();
  1711. stack.take_last();
  1712. stack.append(ValueType(ValueType::I64));
  1713. return {};
  1714. }
  1715. VALIDATE_INSTRUCTION(i64_load8_u)
  1716. {
  1717. TRY(validate(MemoryIndex { 0 }));
  1718. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1719. if ((1ull << arg.align) > 8 / 8)
  1720. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1721. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1722. return Errors::invalid_stack_state();
  1723. stack.take_last();
  1724. stack.append(ValueType(ValueType::I64));
  1725. return {};
  1726. }
  1727. VALIDATE_INSTRUCTION(i32_store)
  1728. {
  1729. TRY(validate(MemoryIndex { 0 }));
  1730. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1731. if ((1ull << arg.align) > sizeof(i32))
  1732. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i32));
  1733. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1734. return Errors::invalid_stack_state();
  1735. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1736. return Errors::invalid_stack_state();
  1737. return {};
  1738. }
  1739. VALIDATE_INSTRUCTION(i64_store)
  1740. {
  1741. TRY(validate(MemoryIndex { 0 }));
  1742. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1743. if ((1ull << arg.align) > sizeof(i64))
  1744. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(i64));
  1745. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I64))
  1746. return Errors::invalid_stack_state();
  1747. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1748. return Errors::invalid_stack_state();
  1749. return {};
  1750. }
  1751. VALIDATE_INSTRUCTION(f32_store)
  1752. {
  1753. TRY(validate(MemoryIndex { 0 }));
  1754. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1755. if ((1ull << arg.align) > sizeof(float))
  1756. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(float));
  1757. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::F32))
  1758. return Errors::invalid_stack_state();
  1759. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1760. return Errors::invalid_stack_state();
  1761. return {};
  1762. }
  1763. VALIDATE_INSTRUCTION(f64_store)
  1764. {
  1765. TRY(validate(MemoryIndex { 0 }));
  1766. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1767. if ((1ull << arg.align) > sizeof(double))
  1768. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, sizeof(double));
  1769. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::F64))
  1770. return Errors::invalid_stack_state();
  1771. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1772. return Errors::invalid_stack_state();
  1773. return {};
  1774. }
  1775. VALIDATE_INSTRUCTION(i32_store16)
  1776. {
  1777. TRY(validate(MemoryIndex { 0 }));
  1778. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1779. if ((1ull << arg.align) > 16 / 8)
  1780. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1781. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1782. return Errors::invalid_stack_state();
  1783. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1784. return Errors::invalid_stack_state();
  1785. return {};
  1786. }
  1787. VALIDATE_INSTRUCTION(i32_store8)
  1788. {
  1789. TRY(validate(MemoryIndex { 0 }));
  1790. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1791. if ((1ull << arg.align) > 8 / 8)
  1792. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1793. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1794. return Errors::invalid_stack_state();
  1795. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1796. return Errors::invalid_stack_state();
  1797. return {};
  1798. }
  1799. VALIDATE_INSTRUCTION(i64_store32)
  1800. {
  1801. TRY(validate(MemoryIndex { 0 }));
  1802. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1803. if ((1ull << arg.align) > 32 / 8)
  1804. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 32 / 8);
  1805. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I64))
  1806. return Errors::invalid_stack_state();
  1807. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1808. return Errors::invalid_stack_state();
  1809. return {};
  1810. }
  1811. VALIDATE_INSTRUCTION(i64_store16)
  1812. {
  1813. TRY(validate(MemoryIndex { 0 }));
  1814. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1815. if ((1ull << arg.align) > 16 / 8)
  1816. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 16 / 8);
  1817. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I64))
  1818. return Errors::invalid_stack_state();
  1819. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1820. return Errors::invalid_stack_state();
  1821. return {};
  1822. }
  1823. VALIDATE_INSTRUCTION(i64_store8)
  1824. {
  1825. TRY(validate(MemoryIndex { 0 }));
  1826. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1827. if ((1ull << arg.align) > 8 / 8)
  1828. return Errors::out_of_bounds("memory op alignment", 1ull << arg.align, 0, 8 / 8);
  1829. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I64))
  1830. return Errors::invalid_stack_state();
  1831. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1832. return Errors::invalid_stack_state();
  1833. return {};
  1834. }
  1835. VALIDATE_INSTRUCTION(memory_size)
  1836. {
  1837. TRY(validate(MemoryIndex { 0 }));
  1838. stack.append(ValueType(ValueType::I32));
  1839. return {};
  1840. }
  1841. VALIDATE_INSTRUCTION(memory_grow)
  1842. {
  1843. TRY(validate(MemoryIndex { 0 }));
  1844. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  1845. return Errors::invalid_stack_state();
  1846. return {};
  1847. }
  1848. VALIDATE_INSTRUCTION(memory_fill)
  1849. {
  1850. TRY(validate(MemoryIndex { 0 }));
  1851. if (stack.size() < 3)
  1852. return Errors::invalid_stack_state();
  1853. for (size_t i = 0; i < 3; ++i) {
  1854. if (!stack.take_last().is_of_kind(ValueType::I32))
  1855. return Errors::invalid_stack_state();
  1856. }
  1857. return {};
  1858. }
  1859. VALIDATE_INSTRUCTION(memory_init)
  1860. {
  1861. TRY(validate(MemoryIndex { 0 }));
  1862. auto index = instruction.arguments().get<DataIndex>();
  1863. TRY(validate(index));
  1864. if (stack.size() < 3)
  1865. return Errors::invalid_stack_state();
  1866. for (size_t i = 0; i < 3; ++i) {
  1867. if (!stack.take_last().is_of_kind(ValueType::I32))
  1868. return Errors::invalid_stack_state();
  1869. }
  1870. return {};
  1871. }
  1872. VALIDATE_INSTRUCTION(data_drop)
  1873. {
  1874. auto index = instruction.arguments().get<DataIndex>();
  1875. TRY(validate(index));
  1876. return {};
  1877. }
  1878. // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
  1879. VALIDATE_INSTRUCTION(nop)
  1880. {
  1881. return {};
  1882. }
  1883. VALIDATE_INSTRUCTION(unreachable)
  1884. {
  1885. // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
  1886. stack.append(StackEntry());
  1887. return {};
  1888. }
  1889. // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
  1890. VALIDATE_INSTRUCTION(structured_end)
  1891. {
  1892. if (m_entered_scopes.is_empty())
  1893. return Errors::invalid("usage of structured end");
  1894. auto last_scope = m_entered_scopes.take_last();
  1895. m_context = m_parent_contexts.take_last();
  1896. auto last_block_type = m_entered_blocks.take_last();
  1897. if (last_scope == ChildScopeKind::Block) {
  1898. auto details = m_block_details.take_last();
  1899. // FIXME: Validate the returns.
  1900. return {};
  1901. }
  1902. if (last_scope == ChildScopeKind::Else) {
  1903. auto details = m_block_details.take_last().details.get<BlockDetails::IfDetails>();
  1904. if (details.true_branch_stack != stack)
  1905. return Errors::invalid("stack configuration after if-else", details.true_branch_stack.release_vector(), stack.release_vector());
  1906. return {};
  1907. }
  1908. return {};
  1909. }
  1910. // Note: This is *not* from the spec.
  1911. VALIDATE_INSTRUCTION(structured_else)
  1912. {
  1913. if (m_entered_scopes.is_empty())
  1914. return Errors::invalid("usage of structured else");
  1915. if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
  1916. return Errors::invalid("usage of structured else");
  1917. m_entered_scopes.last() = ChildScopeKind::Else;
  1918. auto& if_details = m_block_details.last().details.get<BlockDetails::IfDetails>();
  1919. if_details.true_branch_stack = exchange(stack, move(if_details.initial_stack));
  1920. m_context = m_parent_contexts.last();
  1921. return {};
  1922. }
  1923. VALIDATE_INSTRUCTION(block)
  1924. {
  1925. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1926. auto block_type = TRY(validate(args.block_type));
  1927. auto& parameters = block_type.parameters();
  1928. if (stack.size() < parameters.size())
  1929. return Errors::invalid_stack_state();
  1930. for (size_t i = 0; i < parameters.size(); ++i) {
  1931. if (stack.take_last() != parameters[parameters.size() - i])
  1932. return Errors::invalid_stack_state();
  1933. }
  1934. m_entered_scopes.append(ChildScopeKind::Block);
  1935. m_block_details.empend(stack.actual_size(), Empty {});
  1936. m_parent_contexts.append(m_context);
  1937. m_entered_blocks.append(block_type);
  1938. m_context.labels.prepend(ResultType { block_type.results() });
  1939. return {};
  1940. }
  1941. VALIDATE_INSTRUCTION(loop)
  1942. {
  1943. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1944. auto block_type = TRY(validate(args.block_type));
  1945. auto& parameters = block_type.parameters();
  1946. if (stack.size() < parameters.size())
  1947. return Errors::invalid_stack_state();
  1948. for (size_t i = 0; i < parameters.size(); ++i) {
  1949. if (stack.take_last() != parameters[parameters.size() - i - 1])
  1950. return Errors::invalid_stack_state();
  1951. }
  1952. m_entered_scopes.append(ChildScopeKind::Block);
  1953. m_block_details.empend(stack.actual_size(), Empty {});
  1954. m_parent_contexts.append(m_context);
  1955. m_entered_blocks.append(block_type);
  1956. m_context.labels.prepend(ResultType { block_type.results() });
  1957. return {};
  1958. }
  1959. VALIDATE_INSTRUCTION(if_)
  1960. {
  1961. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1962. auto block_type = TRY(validate(args.block_type));
  1963. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  1964. return Errors::invalid_stack_state();
  1965. auto& parameters = block_type.parameters();
  1966. if (stack.size() < parameters.size())
  1967. return Errors::invalid_stack_state();
  1968. for (size_t i = 0; i < parameters.size(); ++i) {
  1969. if (stack.take_last() != parameters[parameters.size() - i])
  1970. return Errors::invalid_stack_state();
  1971. }
  1972. m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
  1973. m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { stack, {} });
  1974. m_parent_contexts.append(m_context);
  1975. m_entered_blocks.append(block_type);
  1976. m_context.labels.prepend(ResultType { block_type.results() });
  1977. return {};
  1978. }
  1979. VALIDATE_INSTRUCTION(br)
  1980. {
  1981. auto label = instruction.arguments().get<LabelIndex>();
  1982. TRY(validate(label));
  1983. auto& type = m_context.labels[label.value()];
  1984. if (stack.size() < type.types().size())
  1985. return Errors::invalid_stack_state();
  1986. for (size_t i = 0; i < type.types().size(); ++i) {
  1987. if (stack.take_last() != type.types()[type.types().size() - i - 1])
  1988. return Errors::invalid_stack_state();
  1989. }
  1990. stack.append(StackEntry());
  1991. return {};
  1992. }
  1993. VALIDATE_INSTRUCTION(br_if)
  1994. {
  1995. auto label = instruction.arguments().get<LabelIndex>();
  1996. TRY(validate(label));
  1997. auto& type = m_context.labels[label.value()];
  1998. if (stack.size() < type.types().size())
  1999. return Errors::invalid_stack_state();
  2000. Vector<StackEntry> entries;
  2001. entries.ensure_capacity(type.types().size());
  2002. for (size_t i = 0; i < type.types().size(); ++i) {
  2003. auto entry = stack.take_last();
  2004. if (entry != type.types()[type.types().size() - i - 1])
  2005. return Errors::invalid_stack_state();
  2006. entries.append(entry);
  2007. }
  2008. for (size_t i = 0; i < entries.size(); ++i)
  2009. stack.append(entries[entries.size() - i - 1]);
  2010. return {};
  2011. }
  2012. VALIDATE_INSTRUCTION(br_table)
  2013. {
  2014. auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
  2015. TRY(validate(args.default_));
  2016. for (auto& label : args.labels)
  2017. TRY(validate(label));
  2018. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  2019. return Errors::invalid_stack_state();
  2020. auto& default_types = m_context.labels[args.default_.value()].types();
  2021. auto arity = default_types.size();
  2022. if (stack.size() < arity)
  2023. return Errors::invalid_stack_state();
  2024. for (auto& label : args.labels) {
  2025. auto& label_types = m_context.labels[label.value()].types();
  2026. if (label_types.size() != arity)
  2027. return Errors::invalid_stack_state();
  2028. for (size_t i = 0; i < arity; ++i) {
  2029. if (stack.at(stack.actual_size() - i - 1) != label_types[label_types.size() - i - 1])
  2030. return Errors::invalid_stack_state();
  2031. }
  2032. }
  2033. for (size_t i = 0; i < arity; ++i) {
  2034. if (stack.take_last() != default_types[default_types.size() - i - 1])
  2035. return Errors::invalid_stack_state();
  2036. }
  2037. return {};
  2038. }
  2039. VALIDATE_INSTRUCTION(return_)
  2040. {
  2041. if (!m_context.return_.has_value())
  2042. return Errors::invalid("use of return outside function");
  2043. auto& return_types = m_context.return_->types();
  2044. for (size_t i = 0; i < return_types.size(); ++i) {
  2045. if (stack.is_empty() || stack.take_last() != return_types[return_types.size() - i - 1])
  2046. return Errors::invalid_stack_state();
  2047. }
  2048. stack.append(StackEntry());
  2049. return {};
  2050. }
  2051. VALIDATE_INSTRUCTION(call)
  2052. {
  2053. auto index = instruction.arguments().get<FunctionIndex>();
  2054. TRY(validate(index));
  2055. auto& function_type = m_context.functions[index.value()];
  2056. for (size_t i = 0; i < function_type.parameters().size(); ++i) {
  2057. if (stack.is_empty() || stack.take_last() != function_type.parameters()[function_type.parameters().size() - i - 1])
  2058. return Errors::invalid_stack_state();
  2059. }
  2060. for (auto& type : function_type.results())
  2061. stack.append(type);
  2062. return {};
  2063. }
  2064. VALIDATE_INSTRUCTION(call_indirect)
  2065. {
  2066. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  2067. TRY(validate(args.table));
  2068. TRY(validate(args.type));
  2069. auto& table = m_context.tables[args.table.value()];
  2070. if (!table.element_type().is_reference())
  2071. return Errors::invalid("table element type for call.indirect", "a reference type", table.element_type());
  2072. auto& type = m_context.types[args.type.value()];
  2073. if (stack.is_empty() || !stack.take_last().is_of_kind(ValueType::I32))
  2074. return Errors::invalid_stack_state();
  2075. for (size_t i = 0; i < type.parameters().size(); ++i) {
  2076. if (stack.is_empty() || stack.take_last() != type.parameters()[type.parameters().size() - i - 1])
  2077. return Errors::invalid_stack_state();
  2078. }
  2079. for (auto& type : type.results())
  2080. stack.append(type);
  2081. return {};
  2082. }
  2083. ErrorOr<void, ValidationError> Validator::validate(const Instruction& instruction, Stack& stack, bool& is_constant)
  2084. {
  2085. switch (instruction.opcode().value()) {
  2086. #define M(name, integer_value) \
  2087. case Instructions::name.value(): \
  2088. return validate_instruction<integer_value>(instruction, stack, is_constant);
  2089. ENUMERATE_WASM_OPCODES(M)
  2090. #undef M
  2091. default:
  2092. is_constant = false;
  2093. return Errors::invalid("instruction opcode");
  2094. }
  2095. }
  2096. ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
  2097. {
  2098. Stack stack;
  2099. bool is_constant_expression = true;
  2100. for (auto& instruction : expression.instructions()) {
  2101. bool is_constant = false;
  2102. TRY(validate(instruction, stack, is_constant));
  2103. is_constant_expression &= is_constant;
  2104. }
  2105. auto expected_result_types = result_types;
  2106. while (!expected_result_types.is_empty()) {
  2107. if (stack.is_empty())
  2108. return Errors::invalid_stack_state();
  2109. auto stack_type = stack.take_last();
  2110. auto expected_type = expected_result_types.take_last();
  2111. if (stack_type != expected_type)
  2112. return Errors::invalid_stack_state();
  2113. }
  2114. for (auto& type : result_types)
  2115. stack.append(type);
  2116. return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
  2117. }
  2118. bool Validator::Stack::operator==(const Stack& other) const
  2119. {
  2120. if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
  2121. return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
  2122. Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
  2123. auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
  2124. auto own_size = Vector<StackEntry>::size();
  2125. for (size_t i = 0; i < own_size; ++i) {
  2126. if (other_size <= i)
  2127. break;
  2128. auto own_entry = at(own_size - i - 1);
  2129. auto other_entry = other.at(other_size - i - 1);
  2130. if (!own_entry.is_known) {
  2131. own_last_unknown_entry_index_from_end = i;
  2132. break;
  2133. }
  2134. if (!other_entry.is_known) {
  2135. other_last_unknown_entry_index_from_end = i;
  2136. break;
  2137. }
  2138. }
  2139. if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
  2140. if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
  2141. return true;
  2142. dbgln("Equality check internal error between");
  2143. dbgln("stack:");
  2144. for (auto& entry : *this)
  2145. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  2146. dbgln("and stack:");
  2147. for (auto& entry : other)
  2148. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  2149. VERIFY_NOT_REACHED();
  2150. }
  2151. 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));
  2152. for (size_t i = 0; i < index_from_end; ++i) {
  2153. if (at(own_size - i - 1) != other.at(other_size - i - 1))
  2154. return false;
  2155. }
  2156. return true;
  2157. }
  2158. #if WASM_VALIDATOR_DEBUG
  2159. ValidationError Validator::Errors::invalid_stack_state(SourceLocation location)
  2160. {
  2161. auto index = location.function_name().find('<');
  2162. auto end_index = location.function_name().find('>');
  2163. if (!index.has_value() || !end_index.has_value())
  2164. return ValidationError { "Invalid stack state"sv };
  2165. auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_uint();
  2166. if (!opcode.has_value())
  2167. return ValidationError { "Invalid stack state"sv };
  2168. auto name = instruction_name(OpCode { *opcode });
  2169. return String::formatted("Invalid stack state for {}", name);
  2170. }
  2171. #else
  2172. ValidationError Validator::Errors::invalid_stack_state()
  2173. {
  2174. return ValidationError { "Invalid stack state"sv };
  2175. }
  2176. #endif
  2177. }