Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/wasm/wasm-debug.h" | |
| 6 | |
| 7 #include "src/assert-scope.h" | |
| 8 #include "src/debug/debug.h" | |
| 9 #include "src/factory.h" | |
| 10 #include "src/isolate.h" | |
| 11 #include "src/wasm/module-decoder.h" | |
| 12 #include "src/wasm/wasm-module.h" | |
| 13 | |
| 14 using namespace v8::internal; | |
| 15 using namespace v8::internal::wasm; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 enum { | |
| 20 kWasmDebugInfoWasmObj, | |
| 21 kWasmDebugInfoWasmBytesHash, | |
| 22 kWasmDebugInfoFunctionByteOffsets, | |
| 23 kWasmDebugInfoNumEntries | |
| 24 }; | |
| 25 | |
| 26 ByteArray *GetOrCreateFunctionOffsetTable(WasmDebugInfo *debug_info) { | |
| 27 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); | |
| 28 Isolate *isolate = debug_info->GetIsolate(); | |
| 29 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); | |
| 30 | |
| 31 FunctionOffsetsResult function_offsets; | |
| 32 { | |
| 33 DisallowHeapAllocation no_gc; | |
| 34 SeqOneByteString *wasm_bytes = | |
| 35 wasm::GetWasmBytes(debug_info->wasm_object()); | |
| 36 const byte *bytes_start = wasm_bytes->GetChars(); | |
| 37 const byte *bytes_end = bytes_start + wasm_bytes->length(); | |
| 38 function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end); | |
| 39 } | |
| 40 DCHECK(function_offsets.ok()); | |
| 41 size_t array_size = 2 * sizeof(int) * function_offsets.val.size(); | |
| 42 CHECK_LE(array_size, static_cast<size_t>(std::numeric_limits<int>::max())); | |
| 43 ByteArray *arr = | |
| 44 *isolate->factory()->NewByteArray(static_cast<int>(array_size)); | |
| 45 int idx = 0; | |
| 46 for (std::pair<int, int> p : function_offsets.val) { | |
| 47 arr->set_int(idx++, p.first); | |
| 48 arr->set_int(idx++, p.second); | |
| 49 } | |
| 50 DCHECK_EQ(arr->length(), idx * sizeof(int)); | |
|
ahaas
2016/06/16 12:55:09
ByteArray uses kIntSize internally instead of size
clemensh
2016/06/16 15:59:50
Thanks for the hint. I also found kMaxInt in globa
| |
| 51 debug_info->set(kWasmDebugInfoFunctionByteOffsets, arr); | |
| 52 | |
| 53 return arr; | |
| 54 } | |
| 55 | |
| 56 std::pair<int, int> GetFunctionOffsetAndLength(WasmDebugInfo *debug_info, | |
| 57 int func_index) { | |
| 58 ByteArray *arr = GetOrCreateFunctionOffsetTable(debug_info); | |
| 59 | |
| 60 int funcs_in_arr = arr->length() / sizeof(int) / 2; | |
|
ahaas
2016/06/16 12:55:09
same here.
clemensh
2016/06/16 15:59:50
Done.
| |
| 61 if (func_index < 0 || func_index >= funcs_in_arr) return {0, 0}; | |
|
ahaas
2016/06/16 12:55:09
Are func_index < 0 and func_index >= funcs_in_arra
clemensh
2016/06/16 15:59:50
Yeah, that's always the question. func_index someh
| |
| 62 | |
| 63 int offset = arr->get_int(2 * func_index); | |
| 64 int length = arr->get_int(2 * func_index + 1); | |
| 65 // Assert that it's distinguishable from the "illegal function index" return. | |
| 66 DCHECK(offset > 0 && length > 0); | |
| 67 return {offset, length}; | |
| 68 } | |
| 69 | |
| 70 Vector<const uint8_t> GetFunctionBytes(WasmDebugInfo *debug_info, | |
|
ahaas
2016/06/16 12:55:09
Why don't you make this a private function of Wasm
clemensh
2016/06/16 15:59:50
Because I would have to add it to the header, and
ahaas
2016/06/16 19:42:19
Isn't that what private methods are for?
| |
| 71 int func_index) { | |
| 72 DCHECK(!AllowHeapAllocation::IsAllowed()); | |
| 73 SeqOneByteString *module_bytes = | |
| 74 wasm::GetWasmBytes(debug_info->wasm_object()); | |
| 75 std::pair<int, int> offset_and_length = | |
| 76 GetFunctionOffsetAndLength(debug_info, func_index); | |
| 77 return Vector<const uint8_t>( | |
| 78 module_bytes->GetChars() + offset_and_length.first, | |
| 79 offset_and_length.second); | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) { | |
| 85 Isolate *isolate = wasm->GetIsolate(); | |
| 86 Factory *factory = isolate->factory(); | |
| 87 Handle<FixedArray> arr = | |
| 88 factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED); | |
| 89 arr->set(kWasmDebugInfoWasmObj, *wasm); | |
| 90 int hash = 0; | |
| 91 Handle<SeqOneByteString> wasm_bytes(GetWasmBytes(*wasm), isolate); | |
| 92 { | |
| 93 DisallowHeapAllocation no_gc; | |
| 94 hash = StringHasher::HashSequentialString( | |
| 95 wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed); | |
| 96 } | |
| 97 Handle<Object> hash_obj = factory->NewNumberFromInt(hash, TENURED); | |
|
ahaas
2016/06/16 12:55:09
Is it guaranteed that {hash} is a SMI? If not, the
clemensh
2016/06/16 15:59:50
If a Smi has less than 32 bit, then it could not f
ahaas
2016/06/16 19:42:18
True, I missed the ending scope.
| |
| 98 arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj); | |
| 99 | |
| 100 return Handle<WasmDebugInfo>::cast(arr); | |
| 101 } | |
| 102 | |
| 103 bool WasmDebugInfo::IsDebugInfo(Object *object) { | |
| 104 if (!object->IsFixedArray()) return false; | |
| 105 FixedArray *arr = FixedArray::cast(object); | |
| 106 Isolate *isolate = arr->GetIsolate(); | |
| 107 return arr->length() == kWasmDebugInfoNumEntries && | |
| 108 IsWasmObject(arr->get(kWasmDebugInfoWasmObj)) && | |
| 109 arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber() && | |
| 110 (arr->get(kWasmDebugInfoFunctionByteOffsets)->IsUndefined(isolate) || | |
| 111 arr->get(kWasmDebugInfoFunctionByteOffsets)->IsByteArray()); | |
| 112 } | |
| 113 | |
| 114 WasmDebugInfo *WasmDebugInfo::cast(Object *object) { | |
| 115 DCHECK(IsDebugInfo(object)); | |
| 116 return static_cast<WasmDebugInfo *>(object); | |
|
ahaas
2016/06/16 12:55:09
For consistency, could you use a reinterpret_cast
clemensh
2016/06/16 15:59:50
Done.
| |
| 117 } | |
| 118 | |
| 119 JSObject *WasmDebugInfo::wasm_object() { | |
| 120 return JSObject::cast(get(kWasmDebugInfoWasmObj)); | |
| 121 } | |
| 122 | |
| 123 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { return false; } | |
|
ahaas
2016/06/16 12:55:09
Add a to-be-implemented here?
clemensh
2016/06/16 15:59:50
Done.
| |
| 124 | |
| 125 Handle<String> WasmDebugInfo::DisassembleFunction(int func_index) { | |
| 126 std::ostringstream disassembly_os; | |
| 127 | |
| 128 { | |
| 129 DisallowHeapAllocation no_gc; | |
| 130 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); | |
| 131 | |
| 132 base::AccountingAllocator allocator; | |
| 133 bool ok = PrintAst( | |
| 134 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), | |
| 135 disassembly_os, nullptr); | |
| 136 DCHECK(ok); | |
| 137 USE(ok); | |
| 138 } | |
| 139 | |
| 140 // Unfortunately, we have to copy the string here. | |
| 141 std::string code_str = disassembly_os.str(); | |
| 142 CHECK_LE(code_str.length(), | |
| 143 static_cast<size_t>(std::numeric_limits<int>::max())); | |
| 144 return GetIsolate() | |
| 145 ->factory() | |
| 146 ->NewStringFromAscii(Vector<const char>( | |
| 147 code_str.data(), static_cast<int>(code_str.length()))) | |
| 148 .ToHandleChecked(); | |
| 149 } | |
| 150 | |
| 151 Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable(int func_index) { | |
| 152 class NullBuf : public std::streambuf {}; | |
| 153 NullBuf null_buf; | |
| 154 std::ostream null_stream(&null_buf); | |
| 155 | |
| 156 std::vector<std::tuple<uint32_t, int, int>> offset_table_vec; | |
| 157 | |
| 158 { | |
| 159 DisallowHeapAllocation no_gc; | |
| 160 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); | |
| 161 | |
| 162 v8::base::AccountingAllocator allocator; | |
| 163 bool ok = PrintAst( | |
|
ahaas
2016/06/16 12:55:09
Is PrintAst supposed to be used like this?
clemensh
2016/06/16 15:59:50
What do you mean by "like this"?
It is the functio
ahaas
2016/06/16 19:42:19
I was just surprised that a function called PrintA
| |
| 164 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), | |
| 165 null_stream, &offset_table_vec); | |
| 166 DCHECK(ok); | |
| 167 USE(ok); | |
| 168 } | |
| 169 | |
| 170 size_t arr_size = 3 * offset_table_vec.size(); | |
| 171 CHECK_LE(arr_size, static_cast<size_t>(std::numeric_limits<int>::max())); | |
| 172 Handle<FixedArray> offset_table = GetIsolate()->factory()->NewFixedArray( | |
| 173 static_cast<int>(arr_size), TENURED); | |
| 174 | |
| 175 int idx = 0; | |
| 176 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { | |
| 177 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); | |
| 178 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); | |
| 179 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); | |
| 180 } | |
| 181 DCHECK_EQ(idx, offset_table->length()); | |
| 182 | |
| 183 return offset_table; | |
| 184 } | |
| OLD | NEW |