Validator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Debug.h>
  8. #include <AK/HashTable.h>
  9. #include <AK/SourceLocation.h>
  10. #include <AK/Tuple.h>
  11. #include <AK/Vector.h>
  12. #include <LibWasm/Forward.h>
  13. #include <LibWasm/Types.h>
  14. namespace Wasm {
  15. struct Context {
  16. Vector<FunctionType> types;
  17. Vector<FunctionType> functions;
  18. Vector<TableType> tables;
  19. Vector<MemoryType> memories;
  20. Vector<GlobalType> globals;
  21. Vector<ValueType> elements;
  22. Vector<bool> datas;
  23. Vector<ValueType> locals;
  24. Vector<ResultType> labels;
  25. Optional<ResultType> return_;
  26. AK::HashTable<FunctionIndex> references;
  27. size_t imported_function_count { 0 };
  28. };
  29. struct ValidationError : public Error {
  30. ValidationError(ByteString error)
  31. : Error(Error::from_string_view(error.view()))
  32. , error_string(move(error))
  33. {
  34. }
  35. ByteString error_string;
  36. };
  37. class Validator {
  38. AK_MAKE_NONCOPYABLE(Validator);
  39. AK_MAKE_NONMOVABLE(Validator);
  40. public:
  41. Validator() = default;
  42. [[nodiscard]] Validator fork() const
  43. {
  44. return Validator { m_context };
  45. }
  46. // Module
  47. ErrorOr<void, ValidationError> validate(Module&);
  48. ErrorOr<void, ValidationError> validate(ImportSection const&);
  49. ErrorOr<void, ValidationError> validate(ExportSection const&);
  50. ErrorOr<void, ValidationError> validate(StartSection const&);
  51. ErrorOr<void, ValidationError> validate(DataSection const&);
  52. ErrorOr<void, ValidationError> validate(ElementSection const&);
  53. ErrorOr<void, ValidationError> validate(GlobalSection const&);
  54. ErrorOr<void, ValidationError> validate(MemorySection const&);
  55. ErrorOr<void, ValidationError> validate(TableSection const&);
  56. ErrorOr<void, ValidationError> validate(CodeSection const&);
  57. ErrorOr<void, ValidationError> validate(FunctionSection const&) { return {}; }
  58. ErrorOr<void, ValidationError> validate(DataCountSection const&) { return {}; }
  59. ErrorOr<void, ValidationError> validate(TypeSection const&) { return {}; }
  60. ErrorOr<void, ValidationError> validate(CustomSection const&) { return {}; }
  61. ErrorOr<void, ValidationError> validate(TypeIndex index) const
  62. {
  63. if (index.value() < m_context.types.size())
  64. return {};
  65. return Errors::invalid("TypeIndex"sv);
  66. }
  67. ErrorOr<void, ValidationError> validate(FunctionIndex index) const
  68. {
  69. if (index.value() < m_context.functions.size())
  70. return {};
  71. return Errors::invalid("FunctionIndex"sv);
  72. }
  73. ErrorOr<void, ValidationError> validate(MemoryIndex index) const
  74. {
  75. if (index.value() < m_context.memories.size())
  76. return {};
  77. return Errors::invalid("MemoryIndex"sv);
  78. }
  79. ErrorOr<void, ValidationError> validate(ElementIndex index) const
  80. {
  81. if (index.value() < m_context.elements.size())
  82. return {};
  83. return Errors::invalid("ElementIndex"sv);
  84. }
  85. ErrorOr<void, ValidationError> validate(DataIndex index) const
  86. {
  87. if (index.value() < m_context.datas.size())
  88. return {};
  89. return Errors::invalid("DataIndex"sv);
  90. }
  91. ErrorOr<void, ValidationError> validate(GlobalIndex index) const
  92. {
  93. if (index.value() < m_context.globals.size())
  94. return {};
  95. return Errors::invalid("GlobalIndex"sv);
  96. }
  97. ErrorOr<void, ValidationError> validate(LabelIndex index) const
  98. {
  99. if (index.value() < m_context.labels.size())
  100. return {};
  101. return Errors::invalid("LabelIndex"sv);
  102. }
  103. ErrorOr<void, ValidationError> validate(LocalIndex index) const
  104. {
  105. if (index.value() < m_context.locals.size())
  106. return {};
  107. return Errors::invalid("LocalIndex"sv);
  108. }
  109. ErrorOr<void, ValidationError> validate(TableIndex index) const
  110. {
  111. if (index.value() < m_context.tables.size())
  112. return {};
  113. return Errors::invalid("TableIndex"sv);
  114. }
  115. // Instructions
  116. struct StackEntry {
  117. StackEntry(ValueType type)
  118. : concrete_type(type)
  119. , is_known(true)
  120. {
  121. }
  122. explicit StackEntry()
  123. : concrete_type(ValueType::I32)
  124. , is_known(false)
  125. {
  126. }
  127. bool is_of_kind(ValueType::Kind kind) const
  128. {
  129. if (is_known)
  130. return concrete_type.kind() == kind;
  131. return true;
  132. }
  133. bool is_numeric() const { return !is_known || concrete_type.is_numeric(); }
  134. bool is_reference() const { return !is_known || concrete_type.is_reference(); }
  135. bool operator==(ValueType const& other) const
  136. {
  137. if (is_known)
  138. return concrete_type == other;
  139. return true;
  140. }
  141. bool operator==(StackEntry const& other) const
  142. {
  143. if (is_known && other.is_known)
  144. return other.concrete_type == concrete_type;
  145. return true;
  146. }
  147. ValueType concrete_type;
  148. bool is_known { true };
  149. };
  150. // This is a wrapper that can model "polymorphic" stacks,
  151. // by treating unknown stack entries as a potentially infinite number of entries
  152. class Stack : private Vector<StackEntry> {
  153. template<typename, typename>
  154. friend struct AK::Formatter;
  155. public:
  156. // The unknown entry will never be popped off, so we can safely use the original `is_empty`.
  157. using Vector<StackEntry>::is_empty;
  158. using Vector<StackEntry>::last;
  159. using Vector<StackEntry>::at;
  160. StackEntry take_last()
  161. {
  162. if (last().is_known)
  163. return Vector<StackEntry>::take_last();
  164. return last();
  165. }
  166. void append(StackEntry entry)
  167. {
  168. if (!entry.is_known)
  169. m_did_insert_unknown_entry = true;
  170. Vector<StackEntry>::append(entry);
  171. }
  172. ErrorOr<void, ValidationError> take(ValueType type, SourceLocation location = SourceLocation::current())
  173. {
  174. if (is_empty())
  175. return Errors::invalid("stack state"sv, type, "<nothing>"sv, location);
  176. auto type_on_stack = take_last();
  177. if (type_on_stack != type)
  178. return Errors::invalid("stack state"sv, type, type_on_stack, location);
  179. return {};
  180. }
  181. template<auto... kinds>
  182. ErrorOr<void, ValidationError> take(SourceLocation location = SourceLocation::current())
  183. {
  184. ErrorOr<void, ValidationError> result;
  185. if (((result = take(Wasm::ValueType(kinds), location)).is_error(), ...)) {
  186. return result;
  187. }
  188. return result;
  189. }
  190. template<auto... kinds>
  191. ErrorOr<void, ValidationError> take_and_put(Wasm::ValueType::Kind kind, SourceLocation location = SourceLocation::current())
  192. {
  193. ErrorOr<void, ValidationError> result;
  194. if (((result = take(Wasm::ValueType(kinds), location)).is_error(), ...)) {
  195. return result;
  196. }
  197. append(Wasm::ValueType(kind));
  198. return result;
  199. }
  200. size_t actual_size() const { return Vector<StackEntry>::size(); }
  201. size_t size() const { return m_did_insert_unknown_entry ? static_cast<size_t>(-1) : actual_size(); }
  202. Vector<StackEntry> release_vector() { return exchange(static_cast<Vector<StackEntry>&>(*this), Vector<StackEntry> {}); }
  203. bool operator==(Stack const& other) const;
  204. private:
  205. bool m_did_insert_unknown_entry { false };
  206. };
  207. struct ExpressionTypeResult {
  208. Vector<StackEntry> result_types;
  209. bool is_constant { false };
  210. };
  211. ErrorOr<ExpressionTypeResult, ValidationError> validate(Expression const&, Vector<ValueType> const&);
  212. ErrorOr<void, ValidationError> validate(Instruction const& instruction, Stack& stack, bool& is_constant);
  213. template<u64 opcode>
  214. ErrorOr<void, ValidationError> validate_instruction(Instruction const&, Stack& stack, bool& is_constant);
  215. // Types
  216. ErrorOr<void, ValidationError> validate(Limits const&, size_t k); // n <= 2^k-1 && m? <= 2^k-1
  217. ErrorOr<FunctionType, ValidationError> validate(BlockType const&);
  218. ErrorOr<void, ValidationError> validate(FunctionType const&) { return {}; }
  219. ErrorOr<void, ValidationError> validate(TableType const&);
  220. ErrorOr<void, ValidationError> validate(MemoryType const&);
  221. ErrorOr<void, ValidationError> validate(GlobalType const&) { return {}; }
  222. private:
  223. explicit Validator(Context context)
  224. : m_context(move(context))
  225. {
  226. }
  227. struct Errors {
  228. static ValidationError invalid(StringView name) { return ByteString::formatted("Invalid {}", name); }
  229. template<typename Expected, typename Given>
  230. static ValidationError invalid(StringView name, Expected expected, Given given, SourceLocation location = SourceLocation::current())
  231. {
  232. if constexpr (WASM_VALIDATOR_DEBUG)
  233. return ByteString::formatted("Invalid {} in {}, expected {} but got {}", name, find_instruction_name(location), expected, given);
  234. else
  235. return ByteString::formatted("Invalid {}, expected {} but got {}", name, expected, given);
  236. }
  237. template<typename... Args>
  238. static ValidationError non_conforming_types(StringView name, Args... args)
  239. {
  240. return ByteString::formatted("Non-conforming types for {}: {}", name, Vector { args... });
  241. }
  242. static ValidationError duplicate_export_name(StringView name) { return ByteString::formatted("Duplicate exported name '{}'", name); }
  243. template<typename T, typename U, typename V>
  244. static ValidationError out_of_bounds(StringView name, V value, T min, U max) { return ByteString::formatted("Value {} for {} is out of bounds ({},{})", value, name, min, max); }
  245. template<typename... Expected>
  246. static ValidationError invalid_stack_state(Stack const& stack, Tuple<Expected...> expected, SourceLocation location = SourceLocation::current())
  247. {
  248. constexpr size_t count = expected.size();
  249. StringBuilder builder;
  250. if constexpr (WASM_VALIDATOR_DEBUG)
  251. builder.appendff("Invalid stack state in {}: ", find_instruction_name(location));
  252. else
  253. builder.appendff("Invalid stack state in <unknown>: ");
  254. builder.append("Expected [ "sv);
  255. expected.apply_as_args([&]<typename... Ts>(Ts const&... args) {
  256. (builder.appendff("{} ", args), ...);
  257. });
  258. builder.append("], but found [ "sv);
  259. auto actual_size = stack.actual_size();
  260. for (size_t i = 1; i <= min(count, actual_size); ++i) {
  261. auto& entry = stack.at(actual_size - i);
  262. if (entry.is_known) {
  263. builder.appendff("{} ", entry.concrete_type);
  264. } else {
  265. builder.appendff("<polymorphic stack>");
  266. break;
  267. }
  268. }
  269. builder.append(']');
  270. return { builder.to_byte_string() };
  271. }
  272. private:
  273. static ByteString find_instruction_name(SourceLocation const&);
  274. };
  275. enum class ChildScopeKind {
  276. Block,
  277. IfWithoutElse,
  278. IfWithElse,
  279. Else,
  280. };
  281. struct BlockDetails {
  282. size_t initial_stack_size { 0 };
  283. struct IfDetails {
  284. Stack initial_stack;
  285. };
  286. Variant<IfDetails, Empty> details;
  287. };
  288. Context m_context;
  289. Vector<Context> m_parent_contexts;
  290. Vector<ChildScopeKind> m_entered_scopes;
  291. Vector<BlockDetails> m_block_details;
  292. Vector<FunctionType> m_entered_blocks;
  293. Vector<GlobalType> m_globals_without_internal_globals;
  294. };
  295. }
  296. template<>
  297. struct AK::Formatter<Wasm::Validator::StackEntry> : public AK::Formatter<StringView> {
  298. ErrorOr<void> format(FormatBuilder& builder, Wasm::Validator::StackEntry const& value)
  299. {
  300. if (value.is_known)
  301. return Formatter<StringView>::format(builder, Wasm::ValueType::kind_name(value.concrete_type.kind()));
  302. return Formatter<StringView>::format(builder, "<unknown>"sv);
  303. }
  304. };
  305. template<>
  306. struct AK::Formatter<Wasm::Validator::Stack> : public AK::Formatter<Vector<Wasm::Validator::StackEntry>> {
  307. ErrorOr<void> format(FormatBuilder& builder, Wasm::Validator::Stack const& value)
  308. {
  309. return Formatter<Vector<Wasm::Validator::StackEntry>>::format(builder, static_cast<Vector<Wasm::Validator::StackEntry> const&>(value));
  310. }
  311. };
  312. template<>
  313. struct AK::Formatter<Wasm::ValueType> : public AK::Formatter<StringView> {
  314. ErrorOr<void> format(FormatBuilder& builder, Wasm::ValueType const& value)
  315. {
  316. return Formatter<StringView>::format(builder, Wasm::ValueType::kind_name(value.kind()));
  317. }
  318. };
  319. template<>
  320. struct AK::Formatter<Wasm::ValidationError> : public AK::Formatter<StringView> {
  321. ErrorOr<void> format(FormatBuilder& builder, Wasm::ValidationError const& error)
  322. {
  323. return Formatter<StringView>::format(builder, error.error_string);
  324. }
  325. };