| 1 | /* |
| 2 | * Copyright (C) 2016-2018 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2018 Yusuke Suzuki <yusukesuzuki@slowstart.org>. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "WasmSectionParser.h" |
| 29 | |
| 30 | #if ENABLE(WEBASSEMBLY) |
| 31 | |
| 32 | #include "IdentifierInlines.h" |
| 33 | #include "WasmMemoryInformation.h" |
| 34 | #include "WasmNameSectionParser.h" |
| 35 | #include "WasmOps.h" |
| 36 | #include "WasmSections.h" |
| 37 | #include "WasmSignatureInlines.h" |
| 38 | #include <wtf/Optional.h> |
| 39 | |
| 40 | namespace JSC { namespace Wasm { |
| 41 | |
| 42 | auto SectionParser::parseType() -> PartialResult |
| 43 | { |
| 44 | uint32_t count; |
| 45 | |
| 46 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Type section's count" ); |
| 47 | WASM_PARSER_FAIL_IF(count > maxTypes, "Type section's count is too big " , count, " maximum " , maxTypes); |
| 48 | WASM_PARSER_FAIL_IF(!m_info->usedSignatures.tryReserveCapacity(count), "can't allocate enough memory for Type section's " , count, " entries" ); |
| 49 | |
| 50 | for (uint32_t i = 0; i < count; ++i) { |
| 51 | int8_t type; |
| 52 | uint32_t argumentCount; |
| 53 | Vector<Type> argumentTypes; |
| 54 | |
| 55 | WASM_PARSER_FAIL_IF(!parseInt7(type), "can't get " , i, "th Type's type" ); |
| 56 | WASM_PARSER_FAIL_IF(type != Func, i, "th Type is non-Func " , type); |
| 57 | WASM_PARSER_FAIL_IF(!parseVarUInt32(argumentCount), "can't get " , i, "th Type's argument count" ); |
| 58 | WASM_PARSER_FAIL_IF(argumentCount > maxFunctionParams, i, "th argument count is too big " , argumentCount, " maximum " , maxFunctionParams); |
| 59 | RefPtr<Signature> maybeSignature = Signature::tryCreate(argumentCount); |
| 60 | WASM_PARSER_FAIL_IF(!maybeSignature, "can't allocate enough memory for Type section's " , i, "th signature" ); |
| 61 | Ref<Signature> signature = maybeSignature.releaseNonNull(); |
| 62 | |
| 63 | for (unsigned i = 0; i < argumentCount; ++i) { |
| 64 | Type argumentType; |
| 65 | WASM_PARSER_FAIL_IF(!parseValueType(argumentType), "can't get " , i, "th argument Type" ); |
| 66 | signature->argument(i) = argumentType; |
| 67 | } |
| 68 | |
| 69 | uint8_t returnCount; |
| 70 | WASM_PARSER_FAIL_IF(!parseVarUInt1(returnCount), "can't get " , i, "th Type's return count" ); |
| 71 | Type returnType; |
| 72 | if (returnCount) { |
| 73 | Type value; |
| 74 | WASM_PARSER_FAIL_IF(!parseValueType(value), "can't get " , i, "th Type's return value" ); |
| 75 | returnType = static_cast<Type>(value); |
| 76 | } else |
| 77 | returnType = Type::Void; |
| 78 | signature->returnType() = returnType; |
| 79 | |
| 80 | m_info->usedSignatures.uncheckedAppend(SignatureInformation::adopt(WTFMove(signature))); |
| 81 | } |
| 82 | return { }; |
| 83 | } |
| 84 | |
| 85 | auto SectionParser::parseImport() -> PartialResult |
| 86 | { |
| 87 | uint32_t importCount; |
| 88 | WASM_PARSER_FAIL_IF(!parseVarUInt32(importCount), "can't get Import section's count" ); |
| 89 | WASM_PARSER_FAIL_IF(importCount > maxImports, "Import section's count is too big " , importCount, " maximum " , maxImports); |
| 90 | WASM_PARSER_FAIL_IF(!m_info->globals.tryReserveCapacity(importCount), "can't allocate enough memory for " , importCount, " globals" ); // FIXME this over-allocates when we fix the FIXMEs below. |
| 91 | WASM_PARSER_FAIL_IF(!m_info->imports.tryReserveCapacity(importCount), "can't allocate enough memory for " , importCount, " imports" ); // FIXME this over-allocates when we fix the FIXMEs below. |
| 92 | WASM_PARSER_FAIL_IF(!m_info->importFunctionSignatureIndices.tryReserveCapacity(importCount), "can't allocate enough memory for " , importCount, " import function signatures" ); // FIXME this over-allocates when we fix the FIXMEs below. |
| 93 | |
| 94 | for (uint32_t importNumber = 0; importNumber < importCount; ++importNumber) { |
| 95 | uint32_t moduleLen; |
| 96 | uint32_t fieldLen; |
| 97 | Name moduleString; |
| 98 | Name fieldString; |
| 99 | ExternalKind kind; |
| 100 | unsigned kindIndex { 0 }; |
| 101 | |
| 102 | WASM_PARSER_FAIL_IF(!parseVarUInt32(moduleLen), "can't get " , importNumber, "th Import's module name length" ); |
| 103 | WASM_PARSER_FAIL_IF(!consumeUTF8String(moduleString, moduleLen), "can't get " , importNumber, "th Import's module name of length " , moduleLen); |
| 104 | |
| 105 | WASM_PARSER_FAIL_IF(!parseVarUInt32(fieldLen), "can't get " , importNumber, "th Import's field name length in module '" , moduleString, "'" ); |
| 106 | WASM_PARSER_FAIL_IF(!consumeUTF8String(fieldString, fieldLen), "can't get " , importNumber, "th Import's field name of length " , moduleLen, " in module '" , moduleString, "'" ); |
| 107 | |
| 108 | WASM_PARSER_FAIL_IF(!parseExternalKind(kind), "can't get " , importNumber, "th Import's kind in module '" , moduleString, "' field '" , fieldString, "'" ); |
| 109 | switch (kind) { |
| 110 | case ExternalKind::Function: { |
| 111 | uint32_t functionSignatureIndex; |
| 112 | WASM_PARSER_FAIL_IF(!parseVarUInt32(functionSignatureIndex), "can't get " , importNumber, "th Import's function signature in module '" , moduleString, "' field '" , fieldString, "'" ); |
| 113 | WASM_PARSER_FAIL_IF(functionSignatureIndex >= m_info->usedSignatures.size(), "invalid function signature for " , importNumber, "th Import, " , functionSignatureIndex, " is out of range of " , m_info->usedSignatures.size(), " in module '" , moduleString, "' field '" , fieldString, "'" ); |
| 114 | kindIndex = m_info->importFunctionSignatureIndices.size(); |
| 115 | SignatureIndex signatureIndex = SignatureInformation::get(m_info->usedSignatures[functionSignatureIndex]); |
| 116 | m_info->importFunctionSignatureIndices.uncheckedAppend(signatureIndex); |
| 117 | break; |
| 118 | } |
| 119 | case ExternalKind::Table: { |
| 120 | bool isImport = true; |
| 121 | PartialResult result = parseTableHelper(isImport); |
| 122 | if (UNLIKELY(!result)) |
| 123 | return makeUnexpected(WTFMove(result.error())); |
| 124 | break; |
| 125 | } |
| 126 | case ExternalKind::Memory: { |
| 127 | bool isImport = true; |
| 128 | PartialResult result = parseMemoryHelper(isImport); |
| 129 | if (UNLIKELY(!result)) |
| 130 | return makeUnexpected(WTFMove(result.error())); |
| 131 | break; |
| 132 | } |
| 133 | case ExternalKind::Global: { |
| 134 | Global global; |
| 135 | WASM_FAIL_IF_HELPER_FAILS(parseGlobalType(global)); |
| 136 | WASM_PARSER_FAIL_IF(global.mutability == Global::Mutable, "Mutable Globals aren't supported" ); |
| 137 | |
| 138 | kindIndex = m_info->globals.size(); |
| 139 | m_info->globals.uncheckedAppend(WTFMove(global)); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | m_info->imports.uncheckedAppend({ WTFMove(moduleString), WTFMove(fieldString), kind, kindIndex }); |
| 145 | } |
| 146 | |
| 147 | m_info->firstInternalGlobal = m_info->globals.size(); |
| 148 | return { }; |
| 149 | } |
| 150 | |
| 151 | auto SectionParser::parseFunction() -> PartialResult |
| 152 | { |
| 153 | uint32_t count; |
| 154 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Function section's count" ); |
| 155 | WASM_PARSER_FAIL_IF(count > maxFunctions, "Function section's count is too big " , count, " maximum " , maxFunctions); |
| 156 | WASM_PARSER_FAIL_IF(!m_info->internalFunctionSignatureIndices.tryReserveCapacity(count), "can't allocate enough memory for " , count, " Function signatures" ); |
| 157 | WASM_PARSER_FAIL_IF(!m_info->functions.tryReserveCapacity(count), "can't allocate enough memory for " , count, "Function locations" ); |
| 158 | |
| 159 | for (uint32_t i = 0; i < count; ++i) { |
| 160 | uint32_t typeNumber; |
| 161 | WASM_PARSER_FAIL_IF(!parseVarUInt32(typeNumber), "can't get " , i, "th Function's type number" ); |
| 162 | WASM_PARSER_FAIL_IF(typeNumber >= m_info->usedSignatures.size(), i, "th Function type number is invalid " , typeNumber); |
| 163 | |
| 164 | SignatureIndex signatureIndex = SignatureInformation::get(m_info->usedSignatures[typeNumber]); |
| 165 | // The Code section fixes up start and end. |
| 166 | size_t start = 0; |
| 167 | size_t end = 0; |
| 168 | m_info->internalFunctionSignatureIndices.uncheckedAppend(signatureIndex); |
| 169 | m_info->functions.uncheckedAppend({ start, end, Vector<uint8_t>() }); |
| 170 | } |
| 171 | |
| 172 | return { }; |
| 173 | } |
| 174 | |
| 175 | auto SectionParser::parseResizableLimits(uint32_t& initial, Optional<uint32_t>& maximum) -> PartialResult |
| 176 | { |
| 177 | ASSERT(!maximum); |
| 178 | |
| 179 | uint8_t flags; |
| 180 | WASM_PARSER_FAIL_IF(!parseVarUInt1(flags), "can't parse resizable limits flags" ); |
| 181 | WASM_PARSER_FAIL_IF(!parseVarUInt32(initial), "can't parse resizable limits initial page count" ); |
| 182 | |
| 183 | if (flags) { |
| 184 | uint32_t maximumInt; |
| 185 | WASM_PARSER_FAIL_IF(!parseVarUInt32(maximumInt), "can't parse resizable limits maximum page count" ); |
| 186 | WASM_PARSER_FAIL_IF(initial > maximumInt, "resizable limits has a initial page count of " , initial, " which is greater than its maximum " , maximumInt); |
| 187 | maximum = maximumInt; |
| 188 | } |
| 189 | |
| 190 | return { }; |
| 191 | } |
| 192 | |
| 193 | auto SectionParser::parseTableHelper(bool isImport) -> PartialResult |
| 194 | { |
| 195 | WASM_PARSER_FAIL_IF(m_info->tableCount() > 0, "Cannot have more than one Table for now" ); |
| 196 | |
| 197 | int8_t type; |
| 198 | WASM_PARSER_FAIL_IF(!parseInt7(type), "can't parse Table type" ); |
| 199 | WASM_PARSER_FAIL_IF(type != Wasm::Anyfunc, "Table type should be anyfunc, got " , type); |
| 200 | |
| 201 | uint32_t initial; |
| 202 | Optional<uint32_t> maximum; |
| 203 | PartialResult limits = parseResizableLimits(initial, maximum); |
| 204 | if (UNLIKELY(!limits)) |
| 205 | return makeUnexpected(WTFMove(limits.error())); |
| 206 | WASM_PARSER_FAIL_IF(initial > maxTableEntries, "Table's initial page count of " , initial, " is too big, maximum " , maxTableEntries); |
| 207 | |
| 208 | ASSERT(!maximum || *maximum >= initial); |
| 209 | |
| 210 | m_info->tableInformation = TableInformation(initial, maximum, isImport); |
| 211 | |
| 212 | return { }; |
| 213 | } |
| 214 | |
| 215 | auto SectionParser::parseTable() -> PartialResult |
| 216 | { |
| 217 | uint32_t count; |
| 218 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Table's count" ); |
| 219 | WASM_PARSER_FAIL_IF(count > 1, "Table count of " , count, " is invalid, at most 1 is allowed for now" ); |
| 220 | |
| 221 | if (!count) |
| 222 | return { }; |
| 223 | |
| 224 | bool isImport = false; |
| 225 | PartialResult result = parseTableHelper(isImport); |
| 226 | if (UNLIKELY(!result)) |
| 227 | return makeUnexpected(WTFMove(result.error())); |
| 228 | |
| 229 | return { }; |
| 230 | } |
| 231 | |
| 232 | auto SectionParser::parseMemoryHelper(bool isImport) -> PartialResult |
| 233 | { |
| 234 | WASM_PARSER_FAIL_IF(m_info->memoryCount(), "there can at most be one Memory section for now" ); |
| 235 | |
| 236 | PageCount initialPageCount; |
| 237 | PageCount maximumPageCount; |
| 238 | { |
| 239 | uint32_t initial; |
| 240 | Optional<uint32_t> maximum; |
| 241 | PartialResult limits = parseResizableLimits(initial, maximum); |
| 242 | if (UNLIKELY(!limits)) |
| 243 | return makeUnexpected(WTFMove(limits.error())); |
| 244 | ASSERT(!maximum || *maximum >= initial); |
| 245 | WASM_PARSER_FAIL_IF(!PageCount::isValid(initial), "Memory's initial page count of " , initial, " is invalid" ); |
| 246 | |
| 247 | initialPageCount = PageCount(initial); |
| 248 | |
| 249 | if (maximum) { |
| 250 | WASM_PARSER_FAIL_IF(!PageCount::isValid(*maximum), "Memory's maximum page count of " , *maximum, " is invalid" ); |
| 251 | maximumPageCount = PageCount(*maximum); |
| 252 | } |
| 253 | } |
| 254 | ASSERT(initialPageCount); |
| 255 | ASSERT(!maximumPageCount || maximumPageCount >= initialPageCount); |
| 256 | |
| 257 | m_info->memory = MemoryInformation(initialPageCount, maximumPageCount, isImport); |
| 258 | return { }; |
| 259 | } |
| 260 | |
| 261 | auto SectionParser::parseMemory() -> PartialResult |
| 262 | { |
| 263 | uint8_t count; |
| 264 | WASM_PARSER_FAIL_IF(!parseVarUInt1(count), "can't parse Memory section's count" ); |
| 265 | |
| 266 | if (!count) |
| 267 | return { }; |
| 268 | |
| 269 | WASM_PARSER_FAIL_IF(count != 1, "Memory section has more than one memory, WebAssembly currently only allows zero or one" ); |
| 270 | |
| 271 | bool isImport = false; |
| 272 | return parseMemoryHelper(isImport); |
| 273 | } |
| 274 | |
| 275 | auto SectionParser::parseGlobal() -> PartialResult |
| 276 | { |
| 277 | uint32_t globalCount; |
| 278 | WASM_PARSER_FAIL_IF(!parseVarUInt32(globalCount), "can't get Global section's count" ); |
| 279 | WASM_PARSER_FAIL_IF(globalCount > maxGlobals, "Global section's count is too big " , globalCount, " maximum " , maxGlobals); |
| 280 | size_t totalBytes = globalCount + m_info->firstInternalGlobal; |
| 281 | WASM_PARSER_FAIL_IF((static_cast<uint32_t>(totalBytes) < globalCount) || !m_info->globals.tryReserveCapacity(totalBytes), "can't allocate memory for " , totalBytes, " globals" ); |
| 282 | |
| 283 | for (uint32_t globalIndex = 0; globalIndex < globalCount; ++globalIndex) { |
| 284 | Global global; |
| 285 | uint8_t initOpcode; |
| 286 | |
| 287 | WASM_FAIL_IF_HELPER_FAILS(parseGlobalType(global)); |
| 288 | Type typeForInitOpcode; |
| 289 | WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, global.initialBitsOrImportNumber, typeForInitOpcode)); |
| 290 | if (initOpcode == GetGlobal) |
| 291 | global.initializationType = Global::FromGlobalImport; |
| 292 | else |
| 293 | global.initializationType = Global::FromExpression; |
| 294 | WASM_PARSER_FAIL_IF(typeForInitOpcode != global.type, "Global init_expr opcode of type " , typeForInitOpcode, " doesn't match global's type " , global.type); |
| 295 | |
| 296 | m_info->globals.uncheckedAppend(WTFMove(global)); |
| 297 | } |
| 298 | |
| 299 | return { }; |
| 300 | } |
| 301 | |
| 302 | auto SectionParser::parseExport() -> PartialResult |
| 303 | { |
| 304 | uint32_t exportCount; |
| 305 | WASM_PARSER_FAIL_IF(!parseVarUInt32(exportCount), "can't get Export section's count" ); |
| 306 | WASM_PARSER_FAIL_IF(exportCount > maxExports, "Export section's count is too big " , exportCount, " maximum " , maxExports); |
| 307 | WASM_PARSER_FAIL_IF(!m_info->exports.tryReserveCapacity(exportCount), "can't allocate enough memory for " , exportCount, " exports" ); |
| 308 | |
| 309 | HashSet<String> exportNames; |
| 310 | for (uint32_t exportNumber = 0; exportNumber < exportCount; ++exportNumber) { |
| 311 | uint32_t fieldLen; |
| 312 | Name fieldString; |
| 313 | ExternalKind kind; |
| 314 | unsigned kindIndex; |
| 315 | |
| 316 | WASM_PARSER_FAIL_IF(!parseVarUInt32(fieldLen), "can't get " , exportNumber, "th Export's field name length" ); |
| 317 | WASM_PARSER_FAIL_IF(!consumeUTF8String(fieldString, fieldLen), "can't get " , exportNumber, "th Export's field name of length " , fieldLen); |
| 318 | String fieldName = String::fromUTF8(fieldString); |
| 319 | WASM_PARSER_FAIL_IF(exportNames.contains(fieldName), "duplicate export: '" , fieldString, "'" ); |
| 320 | exportNames.add(fieldName); |
| 321 | |
| 322 | WASM_PARSER_FAIL_IF(!parseExternalKind(kind), "can't get " , exportNumber, "th Export's kind, named '" , fieldString, "'" ); |
| 323 | WASM_PARSER_FAIL_IF(!parseVarUInt32(kindIndex), "can't get " , exportNumber, "th Export's kind index, named '" , fieldString, "'" ); |
| 324 | switch (kind) { |
| 325 | case ExternalKind::Function: { |
| 326 | WASM_PARSER_FAIL_IF(kindIndex >= m_info->functionIndexSpaceSize(), exportNumber, "th Export has invalid function number " , kindIndex, " it exceeds the function index space " , m_info->functionIndexSpaceSize(), ", named '" , fieldString, "'" ); |
| 327 | break; |
| 328 | } |
| 329 | case ExternalKind::Table: { |
| 330 | WASM_PARSER_FAIL_IF(kindIndex >= m_info->tableCount(), "can't export Table " , kindIndex, " there are " , m_info->tableCount(), " Tables" ); |
| 331 | break; |
| 332 | } |
| 333 | case ExternalKind::Memory: { |
| 334 | WASM_PARSER_FAIL_IF(!m_info->memory, "can't export a non-existent Memory" ); |
| 335 | WASM_PARSER_FAIL_IF(kindIndex, "can't export Memory " , kindIndex, " only one Table is currently supported" ); |
| 336 | break; |
| 337 | } |
| 338 | case ExternalKind::Global: { |
| 339 | WASM_PARSER_FAIL_IF(kindIndex >= m_info->globals.size(), exportNumber, "th Export has invalid global number " , kindIndex, " it exceeds the globals count " , m_info->globals.size(), ", named '" , fieldString, "'" ); |
| 340 | WASM_PARSER_FAIL_IF(m_info->globals[kindIndex].mutability != Global::Immutable, exportNumber, "th Export isn't immutable, named '" , fieldString, "'" ); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | m_info->exports.uncheckedAppend({ WTFMove(fieldString), kind, kindIndex }); |
| 346 | } |
| 347 | |
| 348 | return { }; |
| 349 | } |
| 350 | |
| 351 | auto SectionParser::parseStart() -> PartialResult |
| 352 | { |
| 353 | uint32_t startFunctionIndex; |
| 354 | WASM_PARSER_FAIL_IF(!parseVarUInt32(startFunctionIndex), "can't get Start index" ); |
| 355 | WASM_PARSER_FAIL_IF(startFunctionIndex >= m_info->functionIndexSpaceSize(), "Start index " , startFunctionIndex, " exceeds function index space " , m_info->functionIndexSpaceSize()); |
| 356 | SignatureIndex signatureIndex = m_info->signatureIndexFromFunctionIndexSpace(startFunctionIndex); |
| 357 | const Signature& signature = SignatureInformation::get(signatureIndex); |
| 358 | WASM_PARSER_FAIL_IF(signature.argumentCount(), "Start function can't have arguments" ); |
| 359 | WASM_PARSER_FAIL_IF(signature.returnType() != Void, "Start function can't return a value" ); |
| 360 | m_info->startFunctionIndexSpace = startFunctionIndex; |
| 361 | return { }; |
| 362 | } |
| 363 | |
| 364 | auto SectionParser::parseElement() -> PartialResult |
| 365 | { |
| 366 | uint32_t elementCount; |
| 367 | WASM_PARSER_FAIL_IF(!parseVarUInt32(elementCount), "can't get Element section's count" ); |
| 368 | WASM_PARSER_FAIL_IF(elementCount > maxTableEntries, "Element section's count is too big " , elementCount, " maximum " , maxTableEntries); |
| 369 | WASM_PARSER_FAIL_IF(!m_info->elements.tryReserveCapacity(elementCount), "can't allocate memory for " , elementCount, " Elements" ); |
| 370 | for (unsigned elementNum = 0; elementNum < elementCount; ++elementNum) { |
| 371 | uint32_t tableIndex; |
| 372 | uint64_t initExprBits; |
| 373 | uint8_t initOpcode; |
| 374 | uint32_t indexCount; |
| 375 | |
| 376 | WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), "can't get " , elementNum, "th Element table index" ); |
| 377 | WASM_PARSER_FAIL_IF(tableIndex >= m_info->tableCount(), "Element section for Table " , tableIndex, " exceeds available Table " , m_info->tableCount()); |
| 378 | Type initExprType; |
| 379 | WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, initExprBits, initExprType)); |
| 380 | WASM_PARSER_FAIL_IF(initExprType != I32, "Element init_expr must produce an i32" ); |
| 381 | WASM_PARSER_FAIL_IF(!parseVarUInt32(indexCount), "can't get " , elementNum, "th index count for Element section" ); |
| 382 | WASM_PARSER_FAIL_IF(indexCount == std::numeric_limits<uint32_t>::max(), "Element section's " , elementNum, "th index count is too big " , indexCount); |
| 383 | |
| 384 | ASSERT(!!m_info->tableInformation); |
| 385 | |
| 386 | Element element(makeI32InitExpr(initOpcode, initExprBits)); |
| 387 | WASM_PARSER_FAIL_IF(!element.functionIndices.tryReserveCapacity(indexCount), "can't allocate memory for " , indexCount, " Element indices" ); |
| 388 | |
| 389 | for (unsigned index = 0; index < indexCount; ++index) { |
| 390 | uint32_t functionIndex; |
| 391 | WASM_PARSER_FAIL_IF(!parseVarUInt32(functionIndex), "can't get Element section's " , elementNum, "th element's " , index, "th index" ); |
| 392 | WASM_PARSER_FAIL_IF(functionIndex >= m_info->functionIndexSpaceSize(), "Element section's " , elementNum, "th element's " , index, "th index is " , functionIndex, " which exceeds the function index space size of " , m_info->functionIndexSpaceSize()); |
| 393 | |
| 394 | element.functionIndices.uncheckedAppend(functionIndex); |
| 395 | } |
| 396 | |
| 397 | m_info->elements.uncheckedAppend(WTFMove(element)); |
| 398 | } |
| 399 | |
| 400 | return { }; |
| 401 | } |
| 402 | |
| 403 | // This function will be changed to be RELEASE_ASSERT_NOT_REACHED once we switch our parsing infrastructure to the streaming parser. |
| 404 | auto SectionParser::parseCode() -> PartialResult |
| 405 | { |
| 406 | uint32_t count; |
| 407 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get Code section's count" ); |
| 408 | WASM_PARSER_FAIL_IF(count == std::numeric_limits<uint32_t>::max(), "Code section's count is too big " , count); |
| 409 | WASM_PARSER_FAIL_IF(count != m_info->functions.size(), "Code section count " , count, " exceeds the declared number of functions " , m_info->functions.size()); |
| 410 | |
| 411 | for (uint32_t i = 0; i < count; ++i) { |
| 412 | uint32_t functionSize; |
| 413 | WASM_PARSER_FAIL_IF(!parseVarUInt32(functionSize), "can't get " , i, "th Code function's size" ); |
| 414 | WASM_PARSER_FAIL_IF(functionSize > length(), "Code function's size " , functionSize, " exceeds the module's size " , length()); |
| 415 | WASM_PARSER_FAIL_IF(functionSize > length() - m_offset, "Code function's size " , functionSize, " exceeds the module's remaining size" , length() - m_offset); |
| 416 | WASM_PARSER_FAIL_IF(functionSize > maxFunctionSize, "Code function's size " , functionSize, " is too big" ); |
| 417 | |
| 418 | Vector<uint8_t> data(functionSize); |
| 419 | std::memcpy(data.data(), source() + m_offset, functionSize); |
| 420 | m_info->functions[i].start = m_offsetInSource + m_offset; |
| 421 | m_info->functions[i].end = m_offsetInSource + m_offset + functionSize; |
| 422 | m_info->functions[i].data = WTFMove(data); |
| 423 | m_offset += functionSize; |
| 424 | } |
| 425 | |
| 426 | return { }; |
| 427 | } |
| 428 | |
| 429 | auto SectionParser::parseInitExpr(uint8_t& opcode, uint64_t& bitsOrImportNumber, Type& resultType) -> PartialResult |
| 430 | { |
| 431 | WASM_PARSER_FAIL_IF(!parseUInt8(opcode), "can't get init_expr's opcode" ); |
| 432 | |
| 433 | switch (opcode) { |
| 434 | case I32Const: { |
| 435 | int32_t constant; |
| 436 | WASM_PARSER_FAIL_IF(!parseVarInt32(constant), "can't get constant value for init_expr's i32.const" ); |
| 437 | bitsOrImportNumber = static_cast<uint64_t>(constant); |
| 438 | resultType = I32; |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | case I64Const: { |
| 443 | int64_t constant; |
| 444 | WASM_PARSER_FAIL_IF(!parseVarInt64(constant), "can't get constant value for init_expr's i64.const" ); |
| 445 | bitsOrImportNumber = constant; |
| 446 | resultType = I64; |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | case F32Const: { |
| 451 | uint32_t constant; |
| 452 | WASM_PARSER_FAIL_IF(!parseUInt32(constant), "can't get constant value for init_expr's f32.const" ); |
| 453 | bitsOrImportNumber = constant; |
| 454 | resultType = F32; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | case F64Const: { |
| 459 | uint64_t constant; |
| 460 | WASM_PARSER_FAIL_IF(!parseUInt64(constant), "can't get constant value for init_expr's f64.const" ); |
| 461 | bitsOrImportNumber = constant; |
| 462 | resultType = F64; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | case GetGlobal: { |
| 467 | uint32_t index; |
| 468 | WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get get_global's index" ); |
| 469 | |
| 470 | WASM_PARSER_FAIL_IF(index >= m_info->globals.size(), "get_global's index " , index, " exceeds the number of globals " , m_info->globals.size()); |
| 471 | WASM_PARSER_FAIL_IF(index >= m_info->firstInternalGlobal, "get_global import kind index " , index, " exceeds the first internal global " , m_info->firstInternalGlobal); |
| 472 | |
| 473 | ASSERT(m_info->globals[index].mutability == Global::Immutable); |
| 474 | resultType = m_info->globals[index].type; |
| 475 | bitsOrImportNumber = index; |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | default: |
| 480 | WASM_PARSER_FAIL_IF(true, "unknown init_expr opcode " , opcode); |
| 481 | } |
| 482 | |
| 483 | uint8_t endOpcode; |
| 484 | WASM_PARSER_FAIL_IF(!parseUInt8(endOpcode), "can't get init_expr's end opcode" ); |
| 485 | WASM_PARSER_FAIL_IF(endOpcode != OpType::End, "init_expr should end with end, ended with " , endOpcode); |
| 486 | |
| 487 | return { }; |
| 488 | } |
| 489 | |
| 490 | auto SectionParser::parseGlobalType(Global& global) -> PartialResult |
| 491 | { |
| 492 | uint8_t mutability; |
| 493 | WASM_PARSER_FAIL_IF(!parseValueType(global.type), "can't get Global's value type" ); |
| 494 | WASM_PARSER_FAIL_IF(!parseVarUInt1(mutability), "can't get Global type's mutability" ); |
| 495 | global.mutability = static_cast<Global::Mutability>(mutability); |
| 496 | return { }; |
| 497 | } |
| 498 | |
| 499 | auto SectionParser::parseData() -> PartialResult |
| 500 | { |
| 501 | uint32_t segmentCount; |
| 502 | WASM_PARSER_FAIL_IF(!parseVarUInt32(segmentCount), "can't get Data section's count" ); |
| 503 | WASM_PARSER_FAIL_IF(segmentCount > maxDataSegments, "Data section's count is too big " , segmentCount, " maximum " , maxDataSegments); |
| 504 | WASM_PARSER_FAIL_IF(!m_info->data.tryReserveCapacity(segmentCount), "can't allocate enough memory for Data section's " , segmentCount, " segments" ); |
| 505 | |
| 506 | for (uint32_t segmentNumber = 0; segmentNumber < segmentCount; ++segmentNumber) { |
| 507 | uint32_t memoryIndex; |
| 508 | uint64_t initExprBits; |
| 509 | uint8_t initOpcode; |
| 510 | uint32_t dataByteLength; |
| 511 | |
| 512 | WASM_PARSER_FAIL_IF(!parseVarUInt32(memoryIndex), "can't get " , segmentNumber, "th Data segment's index" ); |
| 513 | WASM_PARSER_FAIL_IF(memoryIndex >= m_info->memoryCount(), segmentNumber, "th Data segment has index " , memoryIndex, " which exceeds the number of Memories " , m_info->memoryCount()); |
| 514 | Type initExprType; |
| 515 | WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, initExprBits, initExprType)); |
| 516 | WASM_PARSER_FAIL_IF(initExprType != I32, segmentNumber, "th Data segment's init_expr must produce an i32" ); |
| 517 | WASM_PARSER_FAIL_IF(!parseVarUInt32(dataByteLength), "can't get " , segmentNumber, "th Data segment's data byte length" ); |
| 518 | WASM_PARSER_FAIL_IF(dataByteLength > maxModuleSize, segmentNumber, "th Data segment's data byte length is too big " , dataByteLength, " maximum " , maxModuleSize); |
| 519 | |
| 520 | Segment* segment = Segment::create(makeI32InitExpr(initOpcode, initExprBits), dataByteLength); |
| 521 | WASM_PARSER_FAIL_IF(!segment, "can't allocate enough memory for " , segmentNumber, "th Data segment of size " , dataByteLength); |
| 522 | m_info->data.uncheckedAppend(Segment::adoptPtr(segment)); |
| 523 | for (uint32_t dataByte = 0; dataByte < dataByteLength; ++dataByte) { |
| 524 | uint8_t byte; |
| 525 | WASM_PARSER_FAIL_IF(!parseUInt8(byte), "can't get " , dataByte, "th data byte from " , segmentNumber, "th Data segment" ); |
| 526 | segment->byte(dataByte) = byte; |
| 527 | } |
| 528 | } |
| 529 | return { }; |
| 530 | } |
| 531 | |
| 532 | auto SectionParser::parseCustom() -> PartialResult |
| 533 | { |
| 534 | CustomSection section; |
| 535 | uint32_t customSectionNumber = m_info->customSections.size() + 1; |
| 536 | uint32_t nameLen; |
| 537 | WASM_PARSER_FAIL_IF(!m_info->customSections.tryReserveCapacity(customSectionNumber), "can't allocate enough memory for " , customSectionNumber, "th custom section" ); |
| 538 | WASM_PARSER_FAIL_IF(!parseVarUInt32(nameLen), "can't get " , customSectionNumber, "th custom section's name length" ); |
| 539 | WASM_PARSER_FAIL_IF(!consumeUTF8String(section.name, nameLen), "nameLen get " , customSectionNumber, "th custom section's name of length " , nameLen); |
| 540 | |
| 541 | uint32_t payloadBytes = length() - m_offset; |
| 542 | WASM_PARSER_FAIL_IF(!section.payload.tryReserveCapacity(payloadBytes), "can't allocate enough memory for " , customSectionNumber, "th custom section's " , payloadBytes, " bytes" ); |
| 543 | for (uint32_t byteNumber = 0; byteNumber < payloadBytes; ++byteNumber) { |
| 544 | uint8_t byte; |
| 545 | WASM_PARSER_FAIL_IF(!parseUInt8(byte), "can't get " , byteNumber, "th data byte from " , customSectionNumber, "th custom section" ); |
| 546 | section.payload.uncheckedAppend(byte); |
| 547 | } |
| 548 | |
| 549 | Name nameName = { 'n', 'a', 'm', 'e' }; |
| 550 | if (section.name == nameName) { |
| 551 | NameSectionParser nameSectionParser(section.payload.begin(), section.payload.size(), m_info); |
| 552 | if (auto nameSection = nameSectionParser.parse()) |
| 553 | m_info->nameSection = WTFMove(*nameSection); |
| 554 | } |
| 555 | |
| 556 | m_info->customSections.uncheckedAppend(WTFMove(section)); |
| 557 | |
| 558 | return { }; |
| 559 | } |
| 560 | |
| 561 | } } // namespace JSC::Wasm |
| 562 | |
| 563 | #endif // ENABLE(WEBASSEMBLY) |
| 564 | |