blob: 1ae78dcb4e6d8322dc1efb06861bb1e92d49b537 [file] [log] [blame]
Ben Murdochc5610432016-08-08 18:44:38 +01001// 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
10using namespace v8::internal;
11using namespace v8::internal::wasm;
12
13namespace {
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
27void testFunctionNameTable(Vector<Vector<const char>> names) {
28 Isolate *isolate = CcTest::InitIsolateOnce();
29 HandleAndZoneScope scope;
30
31 WasmModule module;
32 std::vector<char> all_names;
Ben Murdoch61f157c2016-09-16 13:49:30 +010033 // 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');
Ben Murdochc5610432016-08-08 18:44:38 +010036
37 uint32_t func_index = 0;
38 for (Vector<const char> name : names) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010039 size_t name_offset = name.start() ? all_names.size() : 0;
Ben Murdochc5610432016-08-08 18:44:38 +010040 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');
Ben Murdoch61f157c2016-09-16 13:49:30 +010044 module.functions.push_back({nullptr, 0, 0,
45 static_cast<uint32_t>(name_offset),
46 static_cast<uint32_t>(name.length()), 0, 0});
Ben Murdochc5610432016-08-08 18:44:38 +010047 ++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) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010059 MaybeHandle<String> string = GetWasmFunctionNameFromTable(
Ben Murdochc5610432016-08-08 18:44:38 +010060 Handle<ByteArray>::cast(wasm_function_name_table), func_index);
Ben Murdoch61f157c2016-09-16 13:49:30 +010061 if (name.start()) {
62 CHECK(string.ToHandleChecked()->IsUtf8EqualTo(name));
63 } else {
64 CHECK(string.is_null());
65 }
Ben Murdochc5610432016-08-08 18:44:38 +010066 ++func_index;
67 }
68}
69
70void testFunctionNameTable(Vector<const char *> names) {
71 std::vector<Vector<const char>> names_vec;
Ben Murdoch61f157c2016-09-16 13:49:30 +010072 for (const char *name : names)
73 names_vec.push_back(name ? CStrVector(name) : Vector<const char>());
Ben Murdochc5610432016-08-08 18:44:38 +010074 testFunctionNameTable(Vector<Vector<const char>>(
75 names_vec.data(), static_cast<int>(names_vec.size())));
76}
77
78} // namespace
79
80TEST(NoFunctions) { testFunctionNameTable(Vector<Vector<const char>>()); }
81
82TEST(OneFunctions) {
83 const char *names[] = {"foo"};
84 testFunctionNameTable(ArrayVector(names));
85}
86
87TEST(ThreeFunctions) {
88 const char *names[] = {"foo", "bar", "baz"};
89 testFunctionNameTable(ArrayVector(names));
90}
91
92TEST(OneUnnamedFunction) {
93 const char *names[] = {""};
94 testFunctionNameTable(ArrayVector(names));
95}
96
97TEST(UnnamedFirstFunction) {
98 const char *names[] = {"", "bar", "baz"};
99 testFunctionNameTable(ArrayVector(names));
100}
101
102TEST(UnnamedLastFunction) {
103 const char *names[] = {"bar", "baz", ""};
104 testFunctionNameTable(ArrayVector(names));
105}
106
107TEST(ThreeUnnamedFunctions) {
108 const char *names[] = {"", "", ""};
109 testFunctionNameTable(ArrayVector(names));
110}
111
112TEST(UTF8Names) {
113 const char *names[] = {"↱fun↰", "↺", "alpha:α beta:β"};
114 testFunctionNameTable(ArrayVector(names));
115}
Ben Murdoch61f157c2016-09-16 13:49:30 +0100116
117TEST(UnnamedVsEmptyNames) {
118 const char *names[] = {"", nullptr, nullptr, ""};
119 testFunctionNameTable(ArrayVector(names));
120}