Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 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 | |
| 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 | |
| 34 | uint32_t func_index = 0; |
| 35 | for (Vector<const char> name : names) { |
| 36 | size_t name_offset = all_names.size(); |
| 37 | all_names.insert(all_names.end(), name.start(), |
| 38 | name.start() + name.length()); |
| 39 | // Make every second function name null-terminated. |
| 40 | if (func_index % 2) all_names.push_back('\0'); |
| 41 | module.functions.push_back( |
| 42 | {nullptr, 0, 0, static_cast<uint32_t>(name_offset), |
| 43 | static_cast<uint32_t>(name.length()), 0, 0, false}); |
| 44 | ++func_index; |
| 45 | } |
| 46 | |
| 47 | module.module_start = reinterpret_cast<byte *>(all_names.data()); |
| 48 | module.module_end = module.module_start + all_names.size(); |
| 49 | |
| 50 | Handle<Object> wasm_function_name_table = |
| 51 | BuildFunctionNamesTable(isolate, &module); |
| 52 | CHECK(wasm_function_name_table->IsByteArray()); |
| 53 | |
| 54 | func_index = 0; |
| 55 | for (Vector<const char> name : names) { |
| 56 | Handle<Object> string_obj = GetWasmFunctionNameFromTable( |
| 57 | Handle<ByteArray>::cast(wasm_function_name_table), func_index); |
| 58 | CHECK(!string_obj.is_null()); |
| 59 | CHECK(string_obj->IsString()); |
| 60 | Handle<String> string = Handle<String>::cast(string_obj); |
| 61 | CHECK(string->IsUtf8EqualTo(name)); |
| 62 | ++func_index; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void testFunctionNameTable(Vector<const char *> names) { |
| 67 | std::vector<Vector<const char>> names_vec; |
| 68 | for (const char *name : names) names_vec.push_back(CStrVector(name)); |
| 69 | testFunctionNameTable(Vector<Vector<const char>>( |
| 70 | names_vec.data(), static_cast<int>(names_vec.size()))); |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | TEST(NoFunctions) { testFunctionNameTable(Vector<Vector<const char>>()); } |
| 76 | |
| 77 | TEST(OneFunctions) { |
| 78 | const char *names[] = {"foo"}; |
| 79 | testFunctionNameTable(ArrayVector(names)); |
| 80 | } |
| 81 | |
| 82 | TEST(ThreeFunctions) { |
| 83 | const char *names[] = {"foo", "bar", "baz"}; |
| 84 | testFunctionNameTable(ArrayVector(names)); |
| 85 | } |
| 86 | |
| 87 | TEST(OneUnnamedFunction) { |
| 88 | const char *names[] = {""}; |
| 89 | testFunctionNameTable(ArrayVector(names)); |
| 90 | } |
| 91 | |
| 92 | TEST(UnnamedFirstFunction) { |
| 93 | const char *names[] = {"", "bar", "baz"}; |
| 94 | testFunctionNameTable(ArrayVector(names)); |
| 95 | } |
| 96 | |
| 97 | TEST(UnnamedLastFunction) { |
| 98 | const char *names[] = {"bar", "baz", ""}; |
| 99 | testFunctionNameTable(ArrayVector(names)); |
| 100 | } |
| 101 | |
| 102 | TEST(ThreeUnnamedFunctions) { |
| 103 | const char *names[] = {"", "", ""}; |
| 104 | testFunctionNameTable(ArrayVector(names)); |
| 105 | } |
| 106 | |
| 107 | TEST(UTF8Names) { |
| 108 | const char *names[] = {"↱fun↰", "↺", "alpha:α beta:β"}; |
| 109 | testFunctionNameTable(ArrayVector(names)); |
| 110 | } |