OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/wasm/wasm-function-name-table.h" | 5 #include "src/wasm/wasm-function-name-table.h" |
6 | 6 |
7 #include "src/wasm/wasm-module.h" | 7 #include "src/wasm/wasm-module.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
11 namespace wasm { | 11 namespace wasm { |
12 | 12 |
13 // Build an array with all function names. If there are N functions in the | 13 // Build an array with all function names. If there are N functions in the |
14 // module, then the first (kIntSize * (N+1)) bytes are integer entries. | 14 // module, then the first (kIntSize * (N+1)) bytes are integer entries. |
15 // The first integer entry encodes the number of functions in the module. | 15 // The first integer entry encodes the number of functions in the module. |
16 // The entries 1 to N contain offsets into the second part of this array. | 16 // The entries 1 to N contain offsets into the second part of this array. |
| 17 // If a function is unnamed (not to be confused with an empty name), then the |
| 18 // integer entry is the negative offset of the next function name. |
17 // After these N+1 integer entries, the second part begins, which holds a | 19 // After these N+1 integer entries, the second part begins, which holds a |
18 // concatenation of all function names. | 20 // concatenation of all function names. |
19 // | 21 // |
20 // Returns undefined if the array length would not fit in an integer value | 22 // Returns undefined if the array length would not fit in an integer value. |
21 Handle<Object> BuildFunctionNamesTable(Isolate* isolate, WasmModule* module) { | 23 Handle<Object> BuildFunctionNamesTable(Isolate* isolate, WasmModule* module) { |
22 uint64_t func_names_length = 0; | 24 uint64_t func_names_length = 0; |
23 for (auto& func : module->functions) func_names_length += func.name_length; | 25 for (auto& func : module->functions) func_names_length += func.name_length; |
24 int num_funcs_int = static_cast<int>(module->functions.size()); | 26 int num_funcs_int = static_cast<int>(module->functions.size()); |
25 int current_offset = (num_funcs_int + 1) * kIntSize; | 27 int current_offset = (num_funcs_int + 1) * kIntSize; |
26 uint64_t total_array_length = current_offset + func_names_length; | 28 uint64_t total_array_length = current_offset + func_names_length; |
27 int total_array_length_int = static_cast<int>(total_array_length); | 29 int total_array_length_int = static_cast<int>(total_array_length); |
28 // Check for overflow. Just skip function names if it happens. | 30 // Check for overflow. Just skip function names if it happens. |
29 if (total_array_length_int != total_array_length || num_funcs_int < 0 || | 31 if (total_array_length_int != total_array_length || num_funcs_int < 0 || |
30 num_funcs_int != module->functions.size()) | 32 num_funcs_int != module->functions.size()) |
31 return isolate->factory()->undefined_value(); | 33 return isolate->factory()->undefined_value(); |
32 Handle<ByteArray> func_names_array = | 34 Handle<ByteArray> func_names_array = |
33 isolate->factory()->NewByteArray(total_array_length_int, TENURED); | 35 isolate->factory()->NewByteArray(total_array_length_int, TENURED); |
34 if (func_names_array.is_null()) return isolate->factory()->undefined_value(); | 36 if (func_names_array.is_null()) return isolate->factory()->undefined_value(); |
35 func_names_array->set_int(0, num_funcs_int); | 37 func_names_array->set_int(0, num_funcs_int); |
36 int func_index = 0; | 38 int func_index = 0; |
37 for (WasmFunction& fun : module->functions) { | 39 for (WasmFunction& fun : module->functions) { |
38 WasmName name = module->GetNameOrNull(&fun); | 40 WasmName name = module->GetNameOrNull(&fun); |
39 func_names_array->copy_in(current_offset, | 41 if (name.start() == nullptr) { |
40 reinterpret_cast<const byte*>(name.start()), | 42 func_names_array->set_int(func_index + 1, -current_offset); |
41 name.length()); | 43 } else { |
42 func_names_array->set_int(func_index + 1, current_offset); | 44 func_names_array->copy_in(current_offset, |
43 current_offset += name.length(); | 45 reinterpret_cast<const byte*>(name.start()), |
| 46 name.length()); |
| 47 func_names_array->set_int(func_index + 1, current_offset); |
| 48 current_offset += name.length(); |
| 49 } |
44 ++func_index; | 50 ++func_index; |
45 } | 51 } |
46 return func_names_array; | 52 return func_names_array; |
47 } | 53 } |
48 | 54 |
| 55 // Extract the function name for the given function index from the function name |
| 56 // table. |
| 57 // |
| 58 // Returns undefined if the function index is invalid, the respective function |
| 59 // is unnamed (not to be confused with empty names), or the function name is not |
| 60 // a valid UTF-8 string. |
49 Handle<Object> GetWasmFunctionNameFromTable(Handle<ByteArray> func_names_array, | 61 Handle<Object> GetWasmFunctionNameFromTable(Handle<ByteArray> func_names_array, |
50 uint32_t func_index) { | 62 uint32_t func_index) { |
51 uint32_t num_funcs = static_cast<uint32_t>(func_names_array->get_int(0)); | 63 uint32_t num_funcs = static_cast<uint32_t>(func_names_array->get_int(0)); |
52 DCHECK(static_cast<int>(num_funcs) >= 0); | 64 DCHECK(static_cast<int>(num_funcs) >= 0); |
53 auto undefined = [&func_names_array]() -> Handle<Object> { | 65 Factory* factory = func_names_array->GetIsolate()->factory(); |
54 return func_names_array->GetIsolate()->factory()->undefined_value(); | 66 if (func_index >= num_funcs) return factory->undefined_value(); |
55 }; | |
56 if (func_index >= num_funcs) return undefined(); | |
57 int offset = func_names_array->get_int(func_index + 1); | 67 int offset = func_names_array->get_int(func_index + 1); |
| 68 if (offset < 0) return factory->undefined_value(); |
58 int next_offset = func_index == num_funcs - 1 | 69 int next_offset = func_index == num_funcs - 1 |
59 ? func_names_array->length() | 70 ? func_names_array->length() |
60 : func_names_array->get_int(func_index + 2); | 71 : abs(func_names_array->get_int(func_index + 2)); |
61 ScopedVector<byte> buffer(next_offset - offset); | 72 ScopedVector<byte> buffer(next_offset - offset); |
62 func_names_array->copy_out(offset, buffer.start(), next_offset - offset); | 73 func_names_array->copy_out(offset, buffer.start(), next_offset - offset); |
63 if (!unibrow::Utf8::Validate(buffer.start(), buffer.length())) { | 74 if (!unibrow::Utf8::Validate(buffer.start(), buffer.length())) { |
64 return undefined(); | 75 return factory->undefined_value(); |
65 } | 76 } |
66 MaybeHandle<Object> maybe_name = | 77 Handle<Object> maybe_name; |
67 func_names_array->GetIsolate()->factory()->NewStringFromUtf8( | 78 if (!factory->NewStringFromUtf8(Vector<const char>::cast(buffer)) |
68 Vector<const char>::cast(buffer)); | 79 .ToHandle(&maybe_name)) { |
69 return maybe_name.is_null() ? undefined() : maybe_name.ToHandleChecked(); | 80 maybe_name = factory->undefined_value(); |
| 81 } |
| 82 return maybe_name; |
70 } | 83 } |
71 | 84 |
72 } // namespace wasm | 85 } // namespace wasm |
73 } // namespace internal | 86 } // namespace internal |
74 } // namespace v8 | 87 } // namespace v8 |
OLD | NEW |