// Copyright 2016 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/wasm/wasm-debug.h" #include "src/assert-scope.h" #include "src/debug/debug.h" #include "src/factory.h" #include "src/isolate.h" #include "src/wasm/module-decoder.h" #include "src/wasm/wasm-module.h" using namespace v8::internal; using namespace v8::internal::wasm; namespace { enum { kWasmDebugInfoWasmObj, kWasmDebugInfoWasmBytesHash, kWasmDebugInfoFunctionByteOffsets, kWasmDebugInfoFunctionScripts, kWasmDebugInfoNumEntries }; ByteArray *GetOrCreateFunctionOffsetTable(Handle debug_info) { Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); Isolate *isolate = debug_info->GetIsolate(); if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); FunctionOffsetsResult function_offsets; { DisallowHeapAllocation no_gc; SeqOneByteString *wasm_bytes = wasm::GetWasmBytes(debug_info->wasm_object()); const byte *bytes_start = wasm_bytes->GetChars(); const byte *bytes_end = bytes_start + wasm_bytes->length(); function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end); } DCHECK(function_offsets.ok()); size_t array_size = 2 * kIntSize * function_offsets.val.size(); CHECK_LE(array_size, static_cast(kMaxInt)); ByteArray *arr = *isolate->factory()->NewByteArray(static_cast(array_size)); int idx = 0; for (std::pair p : function_offsets.val) { arr->set_int(idx++, p.first); arr->set_int(idx++, p.second); } DCHECK_EQ(arr->length(), idx * kIntSize); debug_info->set(kWasmDebugInfoFunctionByteOffsets, arr); return arr; } std::pair GetFunctionOffsetAndLength(Handle debug_info, int func_index) { ByteArray *arr = GetOrCreateFunctionOffsetTable(debug_info); DCHECK(func_index >= 0 && func_index < arr->length() / kIntSize / 2); int offset = arr->get_int(2 * func_index); int length = arr->get_int(2 * func_index + 1); // Assert that it's distinguishable from the "illegal function index" return. DCHECK(offset > 0 && length > 0); return {offset, length}; } Vector GetFunctionBytes(Handle debug_info, int func_index) { SeqOneByteString *module_bytes = wasm::GetWasmBytes(debug_info->wasm_object()); std::pair offset_and_length = GetFunctionOffsetAndLength(debug_info, func_index); return Vector( module_bytes->GetChars() + offset_and_length.first, offset_and_length.second); } } // namespace Handle WasmDebugInfo::New(Handle wasm) { Isolate *isolate = wasm->GetIsolate(); Factory *factory = isolate->factory(); Handle arr = factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED); arr->set(kWasmDebugInfoWasmObj, *wasm); int hash = 0; Handle wasm_bytes(GetWasmBytes(*wasm), isolate); { DisallowHeapAllocation no_gc; hash = StringHasher::HashSequentialString( wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed); } Handle hash_obj = factory->NewNumberFromInt(hash, TENURED); arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj); return Handle::cast(arr); } bool WasmDebugInfo::IsDebugInfo(Object *object) { if (!object->IsFixedArray()) return false; FixedArray *arr = FixedArray::cast(object); Isolate *isolate = arr->GetIsolate(); return arr->length() == kWasmDebugInfoNumEntries && IsWasmObject(arr->get(kWasmDebugInfoWasmObj)) && arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber() && (arr->get(kWasmDebugInfoFunctionByteOffsets)->IsUndefined(isolate) || arr->get(kWasmDebugInfoFunctionByteOffsets)->IsByteArray()) && (arr->get(kWasmDebugInfoFunctionScripts)->IsUndefined(isolate) || arr->get(kWasmDebugInfoFunctionScripts)->IsFixedArray()); } WasmDebugInfo *WasmDebugInfo::cast(Object *object) { DCHECK(IsDebugInfo(object)); return reinterpret_cast(object); } JSObject *WasmDebugInfo::wasm_object() { return JSObject::cast(get(kWasmDebugInfoWasmObj)); } Script *WasmDebugInfo::GetFunctionScript(Handle debug_info, int func_index) { Isolate *isolate = debug_info->GetIsolate(); Object *scripts_obj = debug_info->get(kWasmDebugInfoFunctionScripts); Handle scripts; if (scripts_obj->IsUndefined(isolate)) { int num_functions = wasm::GetNumberOfFunctions(debug_info->wasm_object()); scripts = isolate->factory()->NewFixedArray(num_functions, TENURED); debug_info->set(kWasmDebugInfoFunctionScripts, *scripts); } else { scripts = handle(FixedArray::cast(scripts_obj), isolate); } DCHECK(func_index >= 0 && func_index < scripts->length()); Object *script_or_undef = scripts->get(func_index); if (!script_or_undef->IsUndefined(isolate)) { return Script::cast(script_or_undef); } Handle