1 // Copyright 2016 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 #include "src/assert-scope.h"
6 #include "src/debug/debug.h"
7 #include "src/factory.h"
8 #include "src/isolate.h"
9 #include "src/wasm/module-decoder.h"
10 #include "src/wasm/wasm-module.h"
11 #include "src/wasm/wasm-objects.h"
12 
13 using namespace v8::internal;
14 using namespace v8::internal::wasm;
15 
16 namespace {
17 
18 enum {
19   kWasmDebugInfoWasmObj,
20   kWasmDebugInfoWasmBytesHash,
21   kWasmDebugInfoAsmJsOffsets,
22   kWasmDebugInfoNumEntries
23 };
24 
25 // TODO(clemensh): Move asm.js offset tables to the compiled module.
GetAsmJsOffsetTables(Handle<WasmDebugInfo> debug_info,Isolate * isolate)26 FixedArray *GetAsmJsOffsetTables(Handle<WasmDebugInfo> debug_info,
27                                  Isolate *isolate) {
28   Object *offset_tables = debug_info->get(kWasmDebugInfoAsmJsOffsets);
29   if (!offset_tables->IsUndefined(isolate)) {
30     return FixedArray::cast(offset_tables);
31   }
32 
33   Handle<JSObject> wasm_instance(debug_info->wasm_instance(), isolate);
34   Handle<WasmCompiledModule> compiled_module(GetCompiledModule(*wasm_instance),
35                                              isolate);
36   DCHECK(compiled_module->has_asm_js_offset_tables());
37 
38   AsmJsOffsetsResult asm_offsets;
39   {
40     Handle<ByteArray> asm_offset_tables =
41         compiled_module->asm_js_offset_tables();
42     DisallowHeapAllocation no_gc;
43     const byte *bytes_start = asm_offset_tables->GetDataStartAddress();
44     const byte *bytes_end = bytes_start + asm_offset_tables->length();
45     asm_offsets = wasm::DecodeAsmJsOffsets(bytes_start, bytes_end);
46   }
47   // Wasm bytes must be valid and must contain asm.js offset table.
48   DCHECK(asm_offsets.ok());
49   DCHECK_GE(static_cast<size_t>(kMaxInt), asm_offsets.val.size());
50   int num_functions = static_cast<int>(asm_offsets.val.size());
51   DCHECK_EQ(
52       wasm::GetNumberOfFunctions(handle(debug_info->wasm_instance())),
53       static_cast<int>(num_functions +
54                        compiled_module->module()->num_imported_functions));
55   Handle<FixedArray> all_tables =
56       isolate->factory()->NewFixedArray(num_functions);
57   debug_info->set(kWasmDebugInfoAsmJsOffsets, *all_tables);
58   for (int func = 0; func < num_functions; ++func) {
59     std::vector<std::pair<int, int>> &func_asm_offsets = asm_offsets.val[func];
60     if (func_asm_offsets.empty()) continue;
61     size_t array_size = 2 * kIntSize * func_asm_offsets.size();
62     CHECK_LE(array_size, static_cast<size_t>(kMaxInt));
63     ByteArray *arr =
64         *isolate->factory()->NewByteArray(static_cast<int>(array_size));
65     all_tables->set(func, arr);
66     int idx = 0;
67     for (std::pair<int, int> p : func_asm_offsets) {
68       // Byte offsets must be strictly monotonously increasing:
69       DCHECK(idx == 0 || p.first > arr->get_int(idx - 2));
70       arr->set_int(idx++, p.first);
71       arr->set_int(idx++, p.second);
72     }
73     DCHECK_EQ(arr->length(), idx * kIntSize);
74   }
75   return *all_tables;
76 }
77 }  // namespace
78 
New(Handle<JSObject> wasm)79 Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) {
80   Isolate *isolate = wasm->GetIsolate();
81   Factory *factory = isolate->factory();
82   Handle<FixedArray> arr =
83       factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED);
84   arr->set(kWasmDebugInfoWasmObj, *wasm);
85   int hash = 0;
86   Handle<SeqOneByteString> wasm_bytes = GetWasmBytes(wasm);
87   {
88     DisallowHeapAllocation no_gc;
89     hash = StringHasher::HashSequentialString(
90         wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed);
91   }
92   Handle<Object> hash_obj = factory->NewNumberFromInt(hash, TENURED);
93   arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj);
94 
95   return Handle<WasmDebugInfo>::cast(arr);
96 }
97 
IsDebugInfo(Object * object)98 bool WasmDebugInfo::IsDebugInfo(Object *object) {
99   if (!object->IsFixedArray()) return false;
100   FixedArray *arr = FixedArray::cast(object);
101   return arr->length() == kWasmDebugInfoNumEntries &&
102          IsWasmInstance(arr->get(kWasmDebugInfoWasmObj)) &&
103          arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber();
104 }
105 
cast(Object * object)106 WasmDebugInfo *WasmDebugInfo::cast(Object *object) {
107   DCHECK(IsDebugInfo(object));
108   return reinterpret_cast<WasmDebugInfo *>(object);
109 }
110 
wasm_instance()111 JSObject *WasmDebugInfo::wasm_instance() {
112   return JSObject::cast(get(kWasmDebugInfoWasmObj));
113 }
114 
GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info,int func_index,int byte_offset)115 int WasmDebugInfo::GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info,
116                                           int func_index, int byte_offset) {
117   Isolate *isolate = debug_info->GetIsolate();
118   Handle<JSObject> instance(debug_info->wasm_instance(), isolate);
119   FixedArray *offset_tables = GetAsmJsOffsetTables(debug_info, isolate);
120 
121   WasmCompiledModule *compiled_module = wasm::GetCompiledModule(*instance);
122   int num_imported_functions =
123       compiled_module->module()->num_imported_functions;
124   DCHECK_LE(num_imported_functions, func_index);
125   func_index -= num_imported_functions;
126   DCHECK_LT(func_index, offset_tables->length());
127   ByteArray *offset_table = ByteArray::cast(offset_tables->get(func_index));
128 
129   // Binary search for the current byte offset.
130   int left = 0;                                       // inclusive
131   int right = offset_table->length() / kIntSize / 2;  // exclusive
132   DCHECK_LT(left, right);
133   while (right - left > 1) {
134     int mid = left + (right - left) / 2;
135     if (offset_table->get_int(2 * mid) <= byte_offset) {
136       left = mid;
137     } else {
138       right = mid;
139     }
140   }
141   // There should be an entry for each position that could show up on the stack
142   // trace:
143   DCHECK_EQ(byte_offset, offset_table->get_int(2 * left));
144   return offset_table->get_int(2 * left + 1);
145 }
146