| 89 | |
| 90 | |
| 91 | static std::expected<void, PreformattedMessage> checkValidWasmCode(std::string_view name, std::string_view wasm_code) |
| 92 | { |
| 93 | if (name.empty() || 128 < name.size() || !std::all_of(name.data(), name.data() + name.size(), isWordCharASCII)) |
| 94 | { |
| 95 | return formatUnexpected( |
| 96 | "Name of a WebAssembly module must be a non-empty string of length at most 128 consisting of word characters only, got '{}'", |
| 97 | trimAndEscape(name)); |
| 98 | } |
| 99 | |
| 100 | /// Detect magic number for WebAssembly module |
| 101 | /// Reference: https://webassembly.github.io/spec/core/binary/modules.html#binary-module |
| 102 | constexpr std::array<uint8_t, 8> wasm_magic_number = {0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00}; |
| 103 | if (!std::ranges::equal(wasm_magic_number, std::views::take(wasm_code, wasm_magic_number.size()))) |
| 104 | { |
| 105 | return formatUnexpected( |
| 106 | "Cannot read magic number for WebAssembly module '{}', binary module version 0x1 is expected, got '{}'", |
| 107 | name, trimAndEscape(wasm_code, 16)); |
| 108 | } |
| 109 | |
| 110 | return {}; |
| 111 | } |
| 112 | |
| 113 | static std::expected<String, PreformattedMessage> validateModuleFile(const DiskPtr & user_scripts_disk, const String & path) |
| 114 | { |
no test coverage detected