|
| 1 | +// Copyright 2024 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +// |
| 5 | +// Flags: --allow-natives-syntax |
| 6 | + |
| 7 | +var kWasmH0 = 0; |
| 8 | +var kWasmH1 = 0x61; |
| 9 | +var kWasmH2 = 0x73; |
| 10 | +var kWasmH3 = 0x6d; |
| 11 | +var kWasmV0 = 0x1; |
| 12 | +var kWasmV1 = 0; |
| 13 | +var kWasmV2 = 0; |
| 14 | +var kWasmV3 = 0; |
| 15 | +let kTypeSectionCode = 1; // Function signature declarations |
| 16 | +let kFunctionSectionCode = 3; // Function declarations |
| 17 | +let kExportSectionCode = 7; // Exports |
| 18 | +let kCodeSectionCode = 10; // Function code |
| 19 | +let kWasmFunctionTypeForm = 0x60; |
| 20 | +let kWasmStructTypeForm = 0x5f; |
| 21 | +let kNoSuperType = 0xFFFFFFFF; |
| 22 | +let kWasmI32 = 0x7f; |
| 23 | +let kWasmExternRef = -0x11; |
| 24 | +let kLeb128Mask = 0x7f; |
| 25 | +let kExternalFunction = 0; |
| 26 | +function makeSig(params, results) { |
| 27 | + return {params: params, results: results}; |
| 28 | +} |
| 29 | +const kWasmOpcodes = { |
| 30 | + 'End': 0x0b, |
| 31 | + 'I32Const': 0x41, |
| 32 | +}; |
| 33 | +function defineWasmOpcode(name, value) { |
| 34 | + Object.defineProperty(globalThis, name, {value: value}); |
| 35 | +} |
| 36 | +for (let name in kWasmOpcodes) { |
| 37 | + defineWasmOpcode(`kExpr${name}`, kWasmOpcodes[name]); |
| 38 | +} |
| 39 | +const kPrefixOpcodes = { |
| 40 | + 'GC': 0xfb, |
| 41 | +}; |
| 42 | +for (let prefix in kPrefixOpcodes) { |
| 43 | + defineWasmOpcode(`k${prefix}Prefix`, kPrefixOpcodes[prefix]); |
| 44 | +} |
| 45 | +let kExprStructNew = 0x00; |
| 46 | +let kExprExternConvertAny = 0x1b; |
| 47 | +class Binary { |
| 48 | + constructor() { |
| 49 | + this.length = 0; |
| 50 | + this.buffer = new Uint8Array(8192); |
| 51 | + } |
| 52 | + trunc_buffer() { |
| 53 | + return new Uint8Array(this.buffer.buffer, 0, this.length); |
| 54 | + } |
| 55 | + emit_u8(val) { |
| 56 | + this.buffer[this.length++] = val; |
| 57 | + } |
| 58 | + emit_leb_u(val) { |
| 59 | + let v = val & 0xff; |
| 60 | + this.buffer[this.length++] = v; |
| 61 | + } |
| 62 | + emit_u32v(val) { |
| 63 | + this.emit_leb_u(val); |
| 64 | + } |
| 65 | + emit_bytes(data) { |
| 66 | + this.buffer.set(data, this.length); |
| 67 | + this.length += data.length; |
| 68 | + } |
| 69 | + emit_string(string) { |
| 70 | + let string_utf8 = string; |
| 71 | + this.emit_u32v(string_utf8.length); |
| 72 | + for (let i = 0; i < string_utf8.length; i++) { |
| 73 | + this.emit_u8(string_utf8.charCodeAt(i)); |
| 74 | + } |
| 75 | + } |
| 76 | + emit_type(type) { |
| 77 | + this.emit_u8(type >= 0 ? type : type & kLeb128Mask); |
| 78 | + } |
| 79 | + emit_header() { |
| 80 | + this.emit_bytes([ |
| 81 | + kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3 |
| 82 | + ]); |
| 83 | + } |
| 84 | + emit_section(section_code, content_generator) { |
| 85 | + this.emit_u8(section_code); |
| 86 | + const section = new Binary; |
| 87 | + content_generator(section); |
| 88 | + this.emit_u32v(section.length); |
| 89 | + this.emit_bytes(section.trunc_buffer()); |
| 90 | + } |
| 91 | +} |
| 92 | +class WasmFunctionBuilder { |
| 93 | + constructor(module, name, type_index, arg_names) { |
| 94 | + this.module = module; |
| 95 | + this.name = name; |
| 96 | + this.type_index = type_index; |
| 97 | + } |
| 98 | + exportAs(name) { |
| 99 | + this.module.addExport(name, this.index); |
| 100 | + } |
| 101 | + exportFunc() { |
| 102 | + this.exportAs(this.name); |
| 103 | + return this; |
| 104 | + } |
| 105 | + addBody(body) { |
| 106 | + this.body = body.concat([kExprEnd]); |
| 107 | + } |
| 108 | +} |
| 109 | +function makeField(type, mutability) { |
| 110 | + return {type: type, mutability: mutability}; |
| 111 | +} |
| 112 | +class WasmStruct { |
| 113 | + constructor(fields) { |
| 114 | + this.fields = fields; |
| 115 | + } |
| 116 | +} |
| 117 | +class WasmModuleBuilder { |
| 118 | + constructor() { |
| 119 | + this.types = []; |
| 120 | + this.exports = []; |
| 121 | + this.functions = []; |
| 122 | + } |
| 123 | + addType(type, supertype_idx = kNoSuperType, is_final = true, |
| 124 | + is_shared = false) { |
| 125 | + var type_copy = {params: type.params, results: type.results, |
| 126 | + is_final: is_final, is_shared: is_shared, |
| 127 | + supertype: supertype_idx}; |
| 128 | + this.types.push(type_copy); |
| 129 | + return this.types.length - 1; |
| 130 | + } |
| 131 | + addStruct(fields = kNoSuperType = false, is_shared = false) { |
| 132 | + this.types.push(new WasmStruct(fields)); |
| 133 | + } |
| 134 | + addFunction(name, type, arg_names) { |
| 135 | + let type_index =typeof type == 'number' ? type : this.addType(type); |
| 136 | + let func = new WasmFunctionBuilder(this, name, type_index); |
| 137 | + this.functions.push(func); |
| 138 | + return func; |
| 139 | + } |
| 140 | + addExport(name, index) { |
| 141 | + this.exports.push({name: name, kind: kExternalFunction, index: index}); |
| 142 | + } |
| 143 | + toBuffer() { |
| 144 | + let binary = new Binary; |
| 145 | + let wasm = this; |
| 146 | + binary.emit_header(); |
| 147 | + binary.emit_section(kTypeSectionCode, section => { |
| 148 | + let length_with_groups = wasm.types.length; |
| 149 | + section.emit_u32v(length_with_groups); |
| 150 | + for (let i = 0; i < wasm.types.length; i++) { |
| 151 | + let type = wasm.types[i]; |
| 152 | + if (type instanceof WasmStruct) { |
| 153 | + section.emit_u8(kWasmStructTypeForm); |
| 154 | + section.emit_u32v(type.fields.length); |
| 155 | + for (let field of type.fields) { |
| 156 | + section.emit_type(field.type); |
| 157 | + section.emit_u8(); |
| 158 | + } |
| 159 | + } else { |
| 160 | + section.emit_u8(kWasmFunctionTypeForm); |
| 161 | + section.emit_u32v(); |
| 162 | + section.emit_u32v(type.results.length); |
| 163 | + for (let result of type.results) { |
| 164 | + section.emit_type(result); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + }); |
| 169 | + binary.emit_section(kFunctionSectionCode, section => { |
| 170 | + section.emit_u32v(wasm.functions.length); |
| 171 | + for (let func of wasm.functions) { |
| 172 | + section.emit_u32v(func.type_index); |
| 173 | + } |
| 174 | + }); |
| 175 | + var exports_count = wasm.exports.length; |
| 176 | + binary.emit_section(kExportSectionCode, section => { |
| 177 | + section.emit_u32v(exports_count); |
| 178 | + for (let exp of wasm.exports) { |
| 179 | + section.emit_string(exp.name); |
| 180 | + section.emit_u8(); |
| 181 | + section.emit_u32v(); |
| 182 | + } |
| 183 | + }); |
| 184 | + binary.emit_section(kCodeSectionCode, section => { |
| 185 | + section.emit_u32v(wasm.functions.length); |
| 186 | + for (let func of wasm.functions) { |
| 187 | + section.emit_u32v(func.body.length + 1); |
| 188 | + section.emit_u8(); // 0 locals. |
| 189 | + section.emit_bytes(func.body); |
| 190 | + } |
| 191 | + }); |
| 192 | + return binary.trunc_buffer(); |
| 193 | + } |
| 194 | + instantiate() { |
| 195 | + let module = this.toModule(); |
| 196 | + let instance = new WebAssembly.Instance(module); |
| 197 | + return instance; |
| 198 | + } |
| 199 | + toModule() { |
| 200 | + return new WebAssembly.Module(this.toBuffer()); |
| 201 | + } |
| 202 | +} |
| 203 | +let builder = new WasmModuleBuilder(); |
| 204 | +let struct_type = builder.addStruct([makeField(kWasmI32)]); |
| 205 | +builder.addFunction('MakeStruct', makeSig([], [kWasmExternRef])).exportFunc() |
| 206 | + .addBody([kExprI32Const, 42, kGCPrefix, kExprStructNew, struct_type, |
| 207 | + kGCPrefix, kExprExternConvertAny]); |
| 208 | +let instance = builder.instantiate(); |
| 209 | +let evil_wasm_object = instance.exports.MakeStruct(); |
| 210 | +function evil_ctor(){ |
| 211 | +} |
| 212 | +function evil_cast_jit(evil_o){ |
| 213 | + global_collect_node_info = evil_o; // get nodeinfo from PropertyCellStore |
| 214 | + return evil_o instanceof evil_ctor; |
| 215 | +} |
| 216 | +evil_ctor.prototype = evil_wasm_object; |
| 217 | +%PrepareFunctionForOptimization(evil_cast_jit); |
| 218 | +evil_cast_jit(new evil_ctor()); |
| 219 | +evil_cast_jit(new evil_ctor()); |
| 220 | +%OptimizeFunctionOnNextCall(evil_cast_jit); |
| 221 | +evil_cast_jit(); |
0 commit comments