|
| 1 | +import { injectable } from "inversify"; |
| 2 | +import { WasmBinary } from "./WasmBinary"; |
| 3 | +import { BytesReader } from "./BytesReader"; |
| 4 | +import { WasmSection, WasmTypeSectionPayload, WasmSectionPayload, WasmExportSectionPayload, WasmCodeSectionPayload } from "./WasmSection"; |
| 5 | +import { WasmSectionType, WasmType, WasmValueType, getWasmValueType, getExternalType } from "./wasmTypes"; |
| 6 | +import { FuncType } from "./FuncType"; |
| 7 | +import { FunctionBody, FunctionLocal } from "./FunctionBody"; |
| 8 | + |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class WasmBinaryParser { |
| 12 | + |
| 13 | + private readonly WASM_MAGIC_NUMBER = '0061736d' |
| 14 | + private readonly WASM_V1 = '01000000' |
| 15 | + |
| 16 | + private sectionParsers: Map<number, Function> = new Map<number, Function>() |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.sectionParsers.set(WasmSectionType.Type, this.parseTypeSection) |
| 20 | + this.sectionParsers.set(WasmSectionType.Export, this.parseExportSection) |
| 21 | + this.sectionParsers.set(WasmSectionType.Code, this.parseCodeSection) |
| 22 | + } |
| 23 | + |
| 24 | + parse(binary: Buffer): WasmBinary { |
| 25 | + const reader: BytesReader = new BytesReader(binary) |
| 26 | + |
| 27 | + const magicNumberRead: string = reader.readBytesToHex(4) |
| 28 | + |
| 29 | + if(magicNumberRead !== this.WASM_MAGIC_NUMBER) { |
| 30 | + throw new Error(`WASM Magic number not found`) |
| 31 | + } |
| 32 | + const version = reader.readBytesToHex(4) |
| 33 | + |
| 34 | + if(version !== this.WASM_V1) { |
| 35 | + throw new Error(`WASM binary version=${version}, supported=${this.WASM_V1}`) |
| 36 | + } |
| 37 | + |
| 38 | + const wasmSections: WasmSection[] = [] |
| 39 | + while(!reader.finished()) { |
| 40 | + const sectionId = reader.readBytesToNumber(1) |
| 41 | + const sectionType = WasmSectionType[sectionId.toString()] |
| 42 | + const payloadLength = reader.readVarUint32() |
| 43 | + const payloadData = reader.readBytes(payloadLength) |
| 44 | + const payloadHex = payloadData.toString('hex') |
| 45 | + const payload = this.parseSectionPayload(payloadData, sectionId) |
| 46 | + wasmSections.push({ |
| 47 | + sectionType, |
| 48 | + payloadLength, |
| 49 | + payloadData, |
| 50 | + payloadHex, |
| 51 | + payload |
| 52 | + }) |
| 53 | + } |
| 54 | + console.log(JSON.stringify(wasmSections)) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + parseSectionPayload(payload: Buffer, sectionId: number): WasmSectionPayload { |
| 59 | + const sectionParser = this.sectionParsers.get(sectionId) |
| 60 | + if(!sectionParser) { |
| 61 | + return |
| 62 | + } |
| 63 | + return sectionParser(payload, this) |
| 64 | + } |
| 65 | + |
| 66 | + parseTypeSection(payload: Buffer, self: any): WasmTypeSectionPayload { |
| 67 | + const reader = new BytesReader(payload) |
| 68 | + |
| 69 | + // parsing Vector - refactor |
| 70 | + const sectionPayload: WasmTypeSectionPayload = { |
| 71 | + functions : [] |
| 72 | + } |
| 73 | + const numberOfElements = reader.readVarUint32() |
| 74 | + let index = 0; |
| 75 | + while(index < numberOfElements) { |
| 76 | + const funcType: FuncType = self.parseFuncType(reader, index) |
| 77 | + sectionPayload.functions.push(funcType) |
| 78 | + index++ |
| 79 | + } |
| 80 | + return sectionPayload |
| 81 | + } |
| 82 | + |
| 83 | + parseExportSection(payload: Buffer, self: any): WasmExportSectionPayload { |
| 84 | + const reader = new BytesReader(payload) |
| 85 | + // parsing exports vector |
| 86 | + const numberOfElements = reader.readVarUint32() |
| 87 | + let exportsCounter = 0 |
| 88 | + const exportSection: WasmExportSectionPayload = { |
| 89 | + exports: [] |
| 90 | + } |
| 91 | + while(exportsCounter < numberOfElements) { |
| 92 | + const strLength = reader.readVarUint32() |
| 93 | + const name = reader.readBytesToUtf8String(strLength) |
| 94 | + const exportKind = reader.readBytesToNumber(1) |
| 95 | + const kind = getExternalType(exportKind.toString()) |
| 96 | + const index = reader.readVarUint32() |
| 97 | + exportSection.exports.push({ |
| 98 | + name, |
| 99 | + index, |
| 100 | + kind |
| 101 | + }) |
| 102 | + exportsCounter++ |
| 103 | + } |
| 104 | + return exportSection |
| 105 | + } |
| 106 | + |
| 107 | + parseCodeSection(payload: Buffer, self: any): WasmCodeSectionPayload { |
| 108 | + const reader = new BytesReader(payload) |
| 109 | + const numberOfElements = reader.readVarUint32() |
| 110 | + const codeSection: WasmCodeSectionPayload = { |
| 111 | + functions: [] |
| 112 | + } |
| 113 | + console.log(`ParsingCodeSection, noElements=${numberOfElements}`) |
| 114 | + let bodiesCounter = 0 |
| 115 | + while(bodiesCounter < numberOfElements) { |
| 116 | + const functionBody = self.parseFunctionCode(reader) |
| 117 | + codeSection.functions.push(functionBody) |
| 118 | + bodiesCounter++ |
| 119 | + } |
| 120 | + return codeSection |
| 121 | + } |
| 122 | + |
| 123 | + parseFunctionCode(reader: BytesReader): FunctionBody { |
| 124 | + |
| 125 | + const bodySize = reader.readVarUint32() |
| 126 | + const body = reader.readBytes(bodySize) |
| 127 | + return this.parseFunctionBody(body) |
| 128 | + } |
| 129 | + |
| 130 | + parseFunctionBody(body: Buffer): FunctionBody { |
| 131 | + const reader = new BytesReader(body) |
| 132 | + const numberOfLocals = reader.readVarUint32() |
| 133 | + const locals: FunctionLocal[] = [] |
| 134 | + let localsCounter = 0 |
| 135 | + while(localsCounter < numberOfLocals) { |
| 136 | + const count = reader.readVarUint32() |
| 137 | + const valueType = getWasmValueType(reader.readBytesToNumber(1).toString()) |
| 138 | + |
| 139 | + locals.push({ |
| 140 | + count, |
| 141 | + valueType |
| 142 | + }) |
| 143 | + localsCounter++ |
| 144 | + } |
| 145 | + const functionInstructions = reader.readBytes(body.length - reader.getPointer()) |
| 146 | + const bytecodeHex = functionInstructions.toString('hex') |
| 147 | + return { |
| 148 | + bytecodeHex, |
| 149 | + locals |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + parseFuncType(reader: BytesReader, index: number): FuncType { |
| 154 | + const typeByte = reader.readBytesToNumber(1) |
| 155 | + if(typeByte !== WasmType.FunctionType) { |
| 156 | + throw new Error(`Error parsing FuncType - type=${typeByte} not function `) |
| 157 | + } |
| 158 | + // reading params |
| 159 | + const params: WasmValueType[] = [] |
| 160 | + const numParams = reader.readVarUint32() |
| 161 | + let paramsCounter = 0 |
| 162 | + while(paramsCounter < numParams) { |
| 163 | + const paramByte = reader.readBytesToNumber(1) |
| 164 | + params.push(getWasmValueType(paramByte.toString())) |
| 165 | + paramsCounter++ |
| 166 | + } |
| 167 | + // reading results |
| 168 | + const results: WasmValueType[] = [] |
| 169 | + const numResults = reader.readVarUint32() |
| 170 | + let resultsCounter = 0 |
| 171 | + while(resultsCounter < numResults) { |
| 172 | + const resultByte = reader.readBytesToNumber(1) |
| 173 | + results.push(getWasmValueType(resultByte.toString())) |
| 174 | + resultsCounter++ |
| 175 | + } |
| 176 | + return { |
| 177 | + index, |
| 178 | + params, |
| 179 | + results |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments