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-function-name-table.h"
6 #include "src/wasm/wasm-module.h"
7
8 #include "test/cctest/cctest.h"
9
10 using namespace v8::internal;
11 using namespace v8::internal::wasm;
12
13 namespace {
14
15 #define CHECK_STREQ(exp, found) \
16 do { \
17 Vector<const char> exp_ = (exp); \
18 Vector<const char> found_ = (found); \
19 if (V8_UNLIKELY(exp_.length() != found_.length() || \
20 memcmp(exp_.start(), found_.start(), exp_.length()))) { \
21 V8_Fatal(__FILE__, __LINE__, \
22 "Check failed: (%s) != (%s) ('%.*s' vs '%.*s').", #exp, #found, \
23 exp_.length(), exp_.start(), found_.length(), found_.start()); \
24 } \
25 } while (0)
26
testFunctionNameTable(Vector<Vector<const char>> names)27 void testFunctionNameTable(Vector<Vector<const char>> names) {
28 Isolate *isolate = CcTest::InitIsolateOnce();
29 HandleAndZoneScope scope;
30
31 WasmModule module;
32 std::vector<char> all_names;
33 // No name should have offset 0, because that encodes unnamed functions.
34 // In real wasm binary, offset 0 is impossible anyway.
35 all_names.push_back('\0');
36
37 uint32_t func_index = 0;
38 for (Vector<const char> name : names) {
39 size_t name_offset = name.start() ? all_names.size() : 0;
40 all_names.insert(all_names.end(), name.start(),
41 name.start() + name.length());
42 // Make every second function name null-terminated.
43 if (func_index % 2) all_names.push_back('\0');
44 module.functions.push_back({nullptr, 0, 0,
45 static_cast<uint32_t>(name_offset),
46 static_cast<uint32_t>(name.length()), 0, 0});
47 ++func_index;
48 }
49
50 module.module_start = reinterpret_cast<byte *>(all_names.data());
51 module.module_end = module.module_start + all_names.size();
52
53 Handle<Object> wasm_function_name_table =
54 BuildFunctionNamesTable(isolate, &module);
55 CHECK(wasm_function_name_table->IsByteArray());
56
57 func_index = 0;
58 for (Vector<const char> name : names) {
59 MaybeHandle<String> string = GetWasmFunctionNameFromTable(
60 Handle<ByteArray>::cast(wasm_function_name_table), func_index);
61 if (name.start()) {
62 CHECK(string.ToHandleChecked()->IsUtf8EqualTo(name));
63 } else {
64 CHECK(string.is_null());
65 }
66 ++func_index;
67 }
68 }
69
testFunctionNameTable(Vector<const char * > names)70 void testFunctionNameTable(Vector<const char *> names) {
71 std::vector<Vector<const char>> names_vec;
72 for (const char *name : names)
73 names_vec.push_back(name ? CStrVector(name) : Vector<const char>());
74 testFunctionNameTable(Vector<Vector<const char>>(
75 names_vec.data(), static_cast<int>(names_vec.size())));
76 }
77
78 } // namespace
79
TEST(NoFunctions)80 TEST(NoFunctions) { testFunctionNameTable(Vector<Vector<const char>>()); }
81
TEST(OneFunctions)82 TEST(OneFunctions) {
83 const char *names[] = {"foo"};
84 testFunctionNameTable(ArrayVector(names));
85 }
86
TEST(ThreeFunctions)87 TEST(ThreeFunctions) {
88 const char *names[] = {"foo", "bar", "baz"};
89 testFunctionNameTable(ArrayVector(names));
90 }
91
TEST(OneUnnamedFunction)92 TEST(OneUnnamedFunction) {
93 const char *names[] = {""};
94 testFunctionNameTable(ArrayVector(names));
95 }
96
TEST(UnnamedFirstFunction)97 TEST(UnnamedFirstFunction) {
98 const char *names[] = {"", "bar", "baz"};
99 testFunctionNameTable(ArrayVector(names));
100 }
101
TEST(UnnamedLastFunction)102 TEST(UnnamedLastFunction) {
103 const char *names[] = {"bar", "baz", ""};
104 testFunctionNameTable(ArrayVector(names));
105 }
106
TEST(ThreeUnnamedFunctions)107 TEST(ThreeUnnamedFunctions) {
108 const char *names[] = {"", "", ""};
109 testFunctionNameTable(ArrayVector(names));
110 }
111
TEST(UTF8Names)112 TEST(UTF8Names) {
113 const char *names[] = {"↱fun↰", "↺", "alpha:α beta:β"};
114 testFunctionNameTable(ArrayVector(names));
115 }
116
TEST(UnnamedVsEmptyNames)117 TEST(UnnamedVsEmptyNames) {
118 const char *names[] = {"", nullptr, nullptr, ""};
119 testFunctionNameTable(ArrayVector(names));
120 }
121