| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WasmNameSectionParser.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "IdentifierInlines.h" |
| 32 | #include "WasmNameSection.h" |
| 33 | |
| 34 | namespace JSC { namespace Wasm { |
| 35 | |
| 36 | auto NameSectionParser::parse() -> Result |
| 37 | { |
| 38 | Ref<NameSection> nameSection = NameSection::create(); |
| 39 | WASM_PARSER_FAIL_IF(!nameSection->functionNames.tryReserveCapacity(m_info.functionIndexSpaceSize()), "can't allocate enough memory for function names" ); |
| 40 | nameSection->functionNames.resize(m_info.functionIndexSpaceSize()); |
| 41 | |
| 42 | for (size_t payloadNumber = 0; m_offset < length(); ++payloadNumber) { |
| 43 | uint8_t nameType; |
| 44 | uint32_t payloadLength; |
| 45 | WASM_PARSER_FAIL_IF(!parseUInt7(nameType), "can't get name type for payload " , payloadNumber); |
| 46 | WASM_PARSER_FAIL_IF(!parseVarUInt32(payloadLength), "can't get payload length for payload " , payloadNumber); |
| 47 | WASM_PARSER_FAIL_IF(payloadLength > length() - m_offset, "payload length is too big for payload " , payloadNumber); |
| 48 | const auto payloadStart = m_offset; |
| 49 | |
| 50 | if (!isValidNameType(nameType)) { |
| 51 | // Unknown name section entries are simply ignored. This allows us to support newer toolchains without breaking older features. |
| 52 | m_offset += payloadLength; |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | switch (static_cast<NameType>(nameType)) { |
| 57 | case NameType::Module: { |
| 58 | uint32_t nameLen; |
| 59 | Name nameString; |
| 60 | WASM_PARSER_FAIL_IF(!parseVarUInt32(nameLen), "can't get module's name length for payload " , payloadNumber); |
| 61 | WASM_PARSER_FAIL_IF(!consumeUTF8String(nameString, nameLen), "can't get module's name of length " , nameLen, " for payload " , payloadNumber); |
| 62 | nameSection->moduleName = WTFMove(nameString); |
| 63 | break; |
| 64 | } |
| 65 | case NameType::Function: { |
| 66 | uint32_t count; |
| 67 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get function count for payload " , payloadNumber); |
| 68 | for (uint32_t function = 0; function < count; ++function) { |
| 69 | uint32_t index; |
| 70 | uint32_t nameLen; |
| 71 | Name nameString; |
| 72 | WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get function " , function, " index for payload " , payloadNumber); |
| 73 | WASM_PARSER_FAIL_IF(m_info.functionIndexSpaceSize() <= index, "function " , function, " index " , index, " is larger than function index space " , m_info.functionIndexSpaceSize(), " for payload " , payloadNumber); |
| 74 | WASM_PARSER_FAIL_IF(!parseVarUInt32(nameLen), "can't get functions " , function, "'s name length for payload " , payloadNumber); |
| 75 | WASM_PARSER_FAIL_IF(!consumeUTF8String(nameString, nameLen), "can't get function " , function, "'s name of length " , nameLen, " for payload " , payloadNumber); |
| 76 | nameSection->functionNames[index] = WTFMove(nameString); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | case NameType::Local: { |
| 81 | // Ignore local names for now, we don't do anything with them but we still need to parse them in order to properly ignore them. |
| 82 | uint32_t functionIndex; |
| 83 | uint32_t count; |
| 84 | WASM_PARSER_FAIL_IF(!parseVarUInt32(functionIndex), "can't get local's function index for payload " , payloadNumber); |
| 85 | WASM_PARSER_FAIL_IF(!parseVarUInt32(count), "can't get local count for payload " , payloadNumber); |
| 86 | for (uint32_t local = 0; local < count; ++local) { |
| 87 | uint32_t index; |
| 88 | uint32_t nameLen; |
| 89 | Name nameString; |
| 90 | WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get local " , local, " index for payload " , payloadNumber); |
| 91 | WASM_PARSER_FAIL_IF(!parseVarUInt32(nameLen), "can't get local " , local, "'s name length for payload " , payloadNumber); |
| 92 | WASM_PARSER_FAIL_IF(!consumeUTF8String(nameString, nameLen), "can't get local " , local, "'s name of length " , nameLen, " for payload " , payloadNumber); |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | WASM_PARSER_FAIL_IF(payloadStart + payloadLength != m_offset); |
| 98 | } |
| 99 | return nameSection; |
| 100 | } |
| 101 | |
| 102 | } } // namespace JSC::Wasm |
| 103 | |
| 104 | #endif // ENABLE(WEBASSEMBLY) |
| 105 | |