[builtins] Add tests for embedding with .byte directive

It turns out that .incbin can be problematic for build systems, in
that the included binary file is not detected as a build-time
dependency.

The alternative is .byte inclusion, which we test here.

Cq-Include-Trybots: luci.v8.try:v8_win64_msvc_compile_rel;master.tryserver.chromium.linux:linux_chromium_rel_ng
Bug: v8:6666
Change-Id: Ie24f4191db17c920c617987d1bee730208776b91
Reviewed-on: https://chromium-review.googlesource.com/901352
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51109}
This commit is contained in:
jgruber 2018-02-06 10:08:28 +01:00 committed by Commit Bot
parent fefee7dc1b
commit 7c14a408f9

View File

@ -110,6 +110,51 @@ TEST(VerifyBuiltinsIsolateIndependence) {
// .incbin macros.
#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");
#define INCBIN_RODATA(LABEL, FILE) \
EMBED_IN_RODATA_HEADER(LABEL) \
__asm__(".incbin \"" FILE "\"\n"); \
extern "C" V8_ALIGNED(16) const char LABEL[]
#define INCBIN_TEXT(LABEL, FILE) \
EMBED_IN_TEXT_HEADER(LABEL) \
__asm__(".incbin \"" FILE "\"\n"); \
extern "C" V8_ALIGNED(16) const char LABEL[]
// clang-format on
// V8_CC_MSVC is true for both MSVC and clang on windows. clang can handle
// .incbin but MSVC cannot, and thus we need a more precise compiler detection
// that can distinguish between the two. clang on windows sets both __clang__
@ -119,45 +164,6 @@ TEST(VerifyBuiltinsIsolateIndependence) {
#endif
#ifndef V8_COMPILER_IS_MSVC
#if defined(V8_OS_MACOSX)
#define INCBIN_RODATA_SECTION ".const_data\n"
#define INCBIN_TEXT_SECTION ".text\n"
#define INCBIN_MANGLE "_"
#define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE NAME "\n"
#elif defined(V8_OS_WIN)
#define INCBIN_RODATA_SECTION ".section .rodata\n"
#define INCBIN_TEXT_SECTION ".section .text\n"
#if defined(V8_TARGET_ARCH_X64)
#define INCBIN_MANGLE ""
#else
#define INCBIN_MANGLE "_"
#endif
#define INCBIN_GLOBAL(NAME) ".global " INCBIN_MANGLE NAME "\n"
#else
#define INCBIN_RODATA_SECTION ".section .rodata\n"
#define INCBIN_TEXT_SECTION ".section .text\n"
#define INCBIN_MANGLE ""
#define INCBIN_GLOBAL(NAME) ".global " INCBIN_MANGLE NAME "\n"
#endif
// clang-format off
#define INCBIN_RODATA(LABEL, FILE) \
__asm__(INCBIN_RODATA_SECTION \
INCBIN_GLOBAL(#LABEL) \
".balign 16\n" \
INCBIN_MANGLE #LABEL ":\n" \
".incbin \"" FILE "\"\n"); \
extern "C" V8_ALIGNED(16) const char LABEL[]
#define INCBIN_TEXT(LABEL, FILE) \
__asm__(INCBIN_TEXT_SECTION \
INCBIN_GLOBAL(#LABEL) \
".balign 16\n" \
INCBIN_MANGLE #LABEL ":\n" \
".incbin \"" FILE "\"\n"); \
extern "C" V8_ALIGNED(16) const char LABEL[]
// clang-format on
INCBIN_RODATA(test_string_bytes,
"gen/test-isolate-independent-builtins/string.bin");
INCBIN_TEXT(test_function_bytes,
@ -243,16 +249,6 @@ TEST(GenerateTestFunctionData) {
}
#endif // GENERATE_TEST_FUNCTION_DATA
#undef __
#undef GENERATE_TEST_FUNCTION_DATA
#undef INCBIN_GLOBAL
#undef INCBIN_MANGLE
#undef INCBIN_RODATA
#undef INCBIN_RODATA_SECTION
#undef INCBIN_TEXT
#undef INCBIN_TEXT_SECTION
#undef TEST_FUNCTION_FILE
TEST(IncbinInRodata) {
CHECK_EQ(0, std::strcmp("0123456789\n", test_string_bytes));
}
@ -265,9 +261,77 @@ TEST(IncbinInText) {
CHECK_EQ(7, f.Call(3, 4));
CHECK_EQ(11, f.Call(5, 6));
}
// Repeat the same tests, but using the .byte directive instead of .incbin.
#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 0xa7, 0x18, 0x00, 0x03, 0xc0, 0x2f, 0x00, " \
"0x00, 0x00, 0x04, 0xb9, 0x04, 0x00, 0x22, 0x1a, 0x21, 0x07, 0xfe\n"
#else
#error "Unknown architecture."
#endif
// clang-format off
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
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 INCBIN_RODATA
#undef INCBIN_TEXT
#undef TEST_FUNCTION_FILE
} // namespace test_isolate_independent_builtins
} // namespace internal
} // namespace v8