summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/test-isolate-independent-builtins.cc
blob: eff2017355b52b0eeb0cb908e56ff8ab95551884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "test/cctest/cctest.h"

#include "src/assembler-inl.h"
#include "src/handles-inl.h"
#include "src/isolate.h"
#include "src/macro-assembler-inl.h"
#include "src/simulator.h"
#include "src/snapshot/snapshot.h"

// To generate the binary files for the test function, enable this section and
// run GenerateTestFunctionData once on each arch.
#define GENERATE_TEST_FUNCTION_DATA false

namespace v8 {
namespace internal {
namespace test_isolate_independent_builtins {

#ifdef V8_EMBEDDED_BUILTINS
TEST(VerifyBuiltinsIsolateIndependence) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope handle_scope(isolate);

  Snapshot::EnsureAllBuiltinsAreDeserialized(isolate);

  // Build a white-list of all isolate-independent RelocInfo entry kinds.
  constexpr int all_real_modes_mask =
      (1 << (RelocInfo::LAST_REAL_RELOC_MODE + 1)) - 1;
  constexpr int mode_mask =
      all_real_modes_mask & ~RelocInfo::ModeMask(RelocInfo::COMMENT) &
      ~RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) &
      ~RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED) &
      ~RelocInfo::ModeMask(RelocInfo::CONST_POOL) &
      ~RelocInfo::ModeMask(RelocInfo::VENEER_POOL);
  STATIC_ASSERT(RelocInfo::LAST_REAL_RELOC_MODE == RelocInfo::VENEER_POOL);
  STATIC_ASSERT(RelocInfo::ModeMask(RelocInfo::COMMENT) ==
                (1 << RelocInfo::COMMENT));
  STATIC_ASSERT(
      mode_mask ==
      (RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
       RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
       RelocInfo::ModeMask(RelocInfo::WASM_CONTEXT_REFERENCE) |
       RelocInfo::ModeMask(RelocInfo::WASM_FUNCTION_TABLE_SIZE_REFERENCE) |
       RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_HANDLE) |
       RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
       RelocInfo::ModeMask(RelocInfo::JS_TO_WASM_CALL) |
       RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
       RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE)));

  constexpr bool kVerbose = false;
  bool found_mismatch = false;
  for (int i = 0; i < Builtins::builtin_count; i++) {
    Code* code = isolate->builtins()->builtin(i);

    if (kVerbose) {
      printf("%s %s\n", Builtins::KindNameOf(i), isolate->builtins()->name(i));
    }

    bool is_isolate_independent = true;
    for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
      is_isolate_independent = false;

#ifdef ENABLE_DISASSEMBLER
      if (kVerbose) {
        RelocInfo::Mode mode = it.rinfo()->rmode();
        printf("  %s\n", RelocInfo::RelocModeName(mode));
      }
#endif
    }

    const bool expected_result = Builtins::IsIsolateIndependent(i);
    if (is_isolate_independent != expected_result) {
      found_mismatch = true;
      printf("%s %s expected: %d, is: %d\n", Builtins::KindNameOf(i),
             isolate->builtins()->name(i), expected_result,
             is_isolate_independent);
    }
  }

  CHECK(!found_mismatch);
}

TEST(VerifyBuiltinsOffHeapSafety) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope handle_scope(isolate);

  Snapshot::EnsureAllBuiltinsAreDeserialized(isolate);

  constexpr int all_real_modes_mask =
      (1 << (RelocInfo::LAST_REAL_RELOC_MODE + 1)) - 1;
  constexpr int mode_mask =
      all_real_modes_mask & ~RelocInfo::ModeMask(RelocInfo::COMMENT) &
      ~RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) &
      ~RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED) &
      ~RelocInfo::ModeMask(RelocInfo::CONST_POOL) &
      ~RelocInfo::ModeMask(RelocInfo::VENEER_POOL) &
      ~RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE);
  STATIC_ASSERT(RelocInfo::LAST_REAL_RELOC_MODE == RelocInfo::VENEER_POOL);
  STATIC_ASSERT(RelocInfo::ModeMask(RelocInfo::COMMENT) ==
                (1 << RelocInfo::COMMENT));
  STATIC_ASSERT(
      mode_mask ==
      (RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
       RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
       RelocInfo::ModeMask(RelocInfo::WASM_CONTEXT_REFERENCE) |
       RelocInfo::ModeMask(RelocInfo::WASM_FUNCTION_TABLE_SIZE_REFERENCE) |
       RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_HANDLE) |
       RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
       RelocInfo::ModeMask(RelocInfo::JS_TO_WASM_CALL) |
       RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY)));

  constexpr bool kVerbose = false;
  bool found_mismatch = false;
  for (int i = 0; i < Builtins::builtin_count; i++) {
    Code* code = isolate->builtins()->builtin(i);

    if (kVerbose) {
      printf("%s %s\n", Builtins::KindNameOf(i), isolate->builtins()->name(i));
    }

    bool is_off_heap_safe = true;
    for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
      is_off_heap_safe = false;
#ifdef ENABLE_DISASSEMBLER
      if (kVerbose) {
        RelocInfo::Mode mode = it.rinfo()->rmode();
        printf("  %s\n", RelocInfo::RelocModeName(mode));
      }
#endif
    }

    // TODO(jgruber): Remove once we properly set up the on-heap code
    // trampoline.
    if (Builtins::IsTooShortForOffHeapTrampoline(i)) is_off_heap_safe = false;

    const bool expected_result = Builtins::IsOffHeapSafe(i);
    if (is_off_heap_safe != expected_result) {
      found_mismatch = true;
      printf("%s %s expected: %d, is: %d\n", Builtins::KindNameOf(i),
             isolate->builtins()->name(i), expected_result, is_off_heap_safe);
    }
  }

  CHECK(!found_mismatch);
}
#endif  // V8_EMBEDDED_BUILTINS

// V8_CC_MSVC is true for both MSVC and clang on windows. clang can handle
// __asm__-style inline assembly but MSVC cannot, and thus we need a more
// precise compiler detection that can distinguish between the two. clang on
// windows sets both __clang__ and _MSC_VER, MSVC sets only _MSC_VER.
#if defined(_MSC_VER) && !defined(__clang__)
#define V8_COMPILER_IS_MSVC
#endif

#ifndef V8_COMPILER_IS_MSVC
#if GENERATE_TEST_FUNCTION_DATA

// Arch-specific defines.
#if V8_TARGET_ARCH_IA32
#define TEST_FUNCTION_FILE "f-ia32.bin"
#elif V8_TARGET_ARCH_X64 && _WIN64
#define TEST_FUNCTION_FILE "f-x64-win.bin"
#elif V8_TARGET_ARCH_X64
#define TEST_FUNCTION_FILE "f-x64.bin"
#elif V8_TARGET_ARCH_ARM64
#define TEST_FUNCTION_FILE "f-arm64.bin"
#elif V8_TARGET_ARCH_ARM
#define TEST_FUNCTION_FILE "f-arm.bin"
#elif V8_TARGET_ARCH_PPC
#define TEST_FUNCTION_FILE "f-ppc.bin"
#elif V8_TARGET_ARCH_MIPS
#define TEST_FUNCTION_FILE "f-mips.bin"
#elif V8_TARGET_ARCH_MIPS64
#define TEST_FUNCTION_FILE "f-mips64.bin"
#elif V8_TARGET_ARCH_S390
#define TEST_FUNCTION_FILE "f-s390.bin"
#else
#error "Unknown architecture."
#endif

#define __ masm.

TEST(GenerateTestFunctionData) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

#if V8_TARGET_ARCH_IA32
  v8::internal::byte buffer[256];
  Assembler masm(isolate, buffer, sizeof(buffer));

  __ mov(eax, Operand(esp, 4));
  __ add(eax, Operand(esp, 8));
  __ ret(0);
#elif V8_TARGET_ARCH_X64
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
  Assembler masm(isolate, buffer, static_cast<int>(allocated));

#ifdef _WIN64
  static const Register arg1 = rcx;
  static const Register arg2 = rdx;
#else
  static const Register arg1 = rdi;
  static const Register arg2 = rsi;
#endif

  __ movq(rax, arg2);
  __ addq(rax, arg1);
  __ ret(0);
#elif V8_TARGET_ARCH_ARM64
  MacroAssembler masm(isolate, nullptr, 0,
                      v8::internal::CodeObjectRequired::kYes);

  __ Add(x0, x0, x1);
  __ Ret();
#elif V8_TARGET_ARCH_ARM
  Assembler masm(isolate, nullptr, 0);

  __ add(r0, r0, Operand(r1));
  __ mov(pc, Operand(lr));
#elif V8_TARGET_ARCH_PPC
  Assembler masm(isolate, nullptr, 0);

  __ function_descriptor();
  __ add(r3, r3, r4);
  __ blr();
#elif V8_TARGET_ARCH_MIPS
  MacroAssembler masm(isolate, nullptr, 0,
                      v8::internal::CodeObjectRequired::kYes);

  __ addu(v0, a0, a1);
  __ jr(ra);
  __ nop();
#elif V8_TARGET_ARCH_MIPS64
  MacroAssembler masm(isolate, nullptr, 0,
                      v8::internal::CodeObjectRequired::kYes);

  __ addu(v0, a0, a1);
  __ jr(ra);
  __ nop();
#elif V8_TARGET_ARCH_S390
  Assembler masm(isolate, nullptr, 0);

  __ agr(r2, r3);
  __ b(r14);
#else  // Unknown architecture.
#error "Unknown architecture."
#endif  // Target architecture.

  CodeDesc desc;
  masm.GetCode(isolate, &desc);

  std::ofstream of(TEST_FUNCTION_FILE, std::ios::out | std::ios::binary);
  of.write(reinterpret_cast<char*>(desc.buffer), desc.instr_size);
}
#endif  // GENERATE_TEST_FUNCTION_DATA

#if V8_TARGET_ARCH_IA32
#define FUNCTION_BYTES \
  ".byte 0x8b, 0x44, 0x24, 0x04, 0x03, 0x44, 0x24, 0x08, 0xc3\n"
#elif V8_TARGET_ARCH_X64 && _WIN64
#define FUNCTION_BYTES ".byte 0x48, 0x8b, 0xc2, 0x48, 0x03, 0xc1, 0xc3\n"
#elif V8_TARGET_ARCH_X64
#define FUNCTION_BYTES ".byte 0x48, 0x8b, 0xc6, 0x48, 0x03, 0xc7, 0xc3\n"
#elif V8_TARGET_ARCH_ARM64
#define FUNCTION_BYTES ".byte 0x00, 0x00, 0x01, 0x8b, 0xc0, 0x03, 0x5f, 0xd6\n"
#elif V8_TARGET_ARCH_ARM
#define FUNCTION_BYTES ".byte 0x01, 0x00, 0x80, 0xe0, 0x0e, 0xf0, 0xa0, 0xe1\n"
#elif V8_TARGET_ARCH_PPC
#define FUNCTION_BYTES ".byte 0x14, 0x22, 0x63, 0x7c, 0x20, 0x00, 0x80, 0x4e\n"
#elif V8_TARGET_ARCH_MIPS
#define FUNCTION_BYTES                               \
  ".byte 0x21, 0x10, 0x85, 0x00, 0x08, 0x00, 0xe0, " \
  "0x03, 0x00, 0x00, 0x00, 0x00\n"
#elif V8_TARGET_ARCH_MIPS64
#define FUNCTION_BYTES                               \
  ".byte 0x21, 0x10, 0x85, 0x00, 0x08, 0x00, 0xe0, " \
  "0x03, 0x00, 0x00, 0x00, 0x00\n"
#elif V8_TARGET_ARCH_S390
#define FUNCTION_BYTES                               \
  ".byte 0xb9, 0x08, 0x00, 0x23, 0x07, 0xfe\n"
#else
#error "Unknown architecture."
#endif

// .byte macros to handle small differences across operating systems.

#if defined(V8_OS_MACOSX)
#define ASM_RODATA_SECTION ".const_data\n"
#define ASM_TEXT_SECTION ".text\n"
#define ASM_MANGLE_LABEL "_"
#define ASM_GLOBAL(NAME) ".globl " ASM_MANGLE_LABEL NAME "\n"
#elif defined(V8_OS_WIN)
#define ASM_RODATA_SECTION ".section .rodata\n"
#define ASM_TEXT_SECTION ".section .text\n"
#if defined(V8_TARGET_ARCH_X64)
#define ASM_MANGLE_LABEL ""
#else
#define ASM_MANGLE_LABEL "_"
#endif
#define ASM_GLOBAL(NAME) ".global " ASM_MANGLE_LABEL NAME "\n"
#else
#define ASM_RODATA_SECTION ".section .rodata\n"
#define ASM_TEXT_SECTION ".section .text\n"
#define ASM_MANGLE_LABEL ""
#define ASM_GLOBAL(NAME) ".global " ASM_MANGLE_LABEL NAME "\n"
#endif

// clang-format off
#define EMBED_IN_RODATA_HEADER(LABEL)    \
  __asm__(ASM_RODATA_SECTION             \
          ASM_GLOBAL(#LABEL)             \
          ".balign 16\n"                 \
          ASM_MANGLE_LABEL #LABEL ":\n");

#define EMBED_IN_TEXT_HEADER(LABEL)        \
    __asm__(ASM_TEXT_SECTION               \
            ASM_GLOBAL(#LABEL)             \
            ".balign 16\n"                 \
            ASM_MANGLE_LABEL #LABEL ":\n");

EMBED_IN_RODATA_HEADER(test_string0_bytes)
__asm__(".byte 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37\n"
        ".byte 0x38, 0x39, 0x0a, 0x00\n");
extern "C" V8_ALIGNED(16) const char test_string0_bytes[];

EMBED_IN_TEXT_HEADER(test_function0_bytes)
__asm__(FUNCTION_BYTES);
extern "C" V8_ALIGNED(16) const char test_function0_bytes[];
// clang-format on

// A historical note: We use .byte over .incbin since the latter leads to
// complications involving generation of build-time dependencies.  Goma parses
// #include statements, and clang has -MD/-MMD. Neither recognize .incbin.

TEST(ByteInRodata) {
  CHECK_EQ(0, std::strcmp("0123456789\n", test_string0_bytes));
}

TEST(ByteInText) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  auto f = GeneratedCode<int(int, int)>::FromAddress(
      isolate, const_cast<char*>(test_function0_bytes));
  CHECK_EQ(7, f.Call(3, 4));
  CHECK_EQ(11, f.Call(5, 6));
}
#endif  // #ifndef V8_COMPILER_IS_MSVC
#undef V8_COMPILER_IS_MSVC

#undef __
#undef ASM_GLOBAL
#undef ASM_MANGLE_LABEL
#undef ASM_RODATA_SECTION
#undef ASM_TEXT_SECTION
#undef EMBED_IN_RODATA_HEADER
#undef EMBED_IN_TEXT_HEADER
#undef FUNCTION_BYTES
#undef GENERATE_TEST_FUNCTION_DATA
#undef TEST_FUNCTION_FILE

}  // namespace test_isolate_independent_builtins
}  // namespace internal
}  // namespace v8