blob: 347f4ea9d4c4c695fd01cb8089c34426bff9cfda [file] [log] [blame]
David Srbeckydd973932015-04-07 20:29:48 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <memory>
18#include <vector>
19
20#include "arch/instruction_set.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010021#include "base/arena_allocator.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070022#include "base/enums.h"
David Srbeckydd973932015-04-07 20:29:48 +010023#include "cfi_test.h"
24#include "gtest/gtest.h"
25#include "jni/quick/calling_convention.h"
Andreas Gampe217488a2017-09-18 08:34:42 -070026#include "read_barrier_config.h"
David Srbeckydd973932015-04-07 20:29:48 +010027#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070028#include "utils/jni_macro_assembler.h"
David Srbeckydd973932015-04-07 20:29:48 +010029
30#include "jni/jni_cfi_test_expected.inc"
31
32namespace art {
33
34// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010035#ifndef ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +010036
37class JNICFITest : public CFITest {
38 public:
39 // Enable this flag to generate the expected outputs.
40 static constexpr bool kGenerateExpected = false;
41
Andreas Gampe3b165bc2016-08-01 22:07:04 -070042 void TestImpl(InstructionSet isa,
43 const char* isa_str,
David Srbeckydd973932015-04-07 20:29:48 +010044 const std::vector<uint8_t>& expected_asm,
45 const std::vector<uint8_t>& expected_cfi) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070046 if (Is64BitInstructionSet(isa)) {
47 TestImplSized<PointerSize::k64>(isa, isa_str, expected_asm, expected_cfi);
48 } else {
49 TestImplSized<PointerSize::k32>(isa, isa_str, expected_asm, expected_cfi);
50 }
51 }
52
53 private:
54 template <PointerSize kPointerSize>
55 void TestImplSized(InstructionSet isa,
56 const char* isa_str,
57 const std::vector<uint8_t>& expected_asm,
58 const std::vector<uint8_t>& expected_cfi) {
David Srbeckydd973932015-04-07 20:29:48 +010059 // Description of simple method.
60 const bool is_static = true;
61 const bool is_synchronized = false;
62 const char* shorty = "IIFII";
Vladimir Marko93205e32016-04-13 11:59:46 +010063
64 ArenaPool pool;
65 ArenaAllocator arena(&pool);
66
David Srbeckydd973932015-04-07 20:29:48 +010067 std::unique_ptr<JniCallingConvention> jni_conv(
Igor Murashkin367f3dd2016-09-01 17:00:24 -070068 JniCallingConvention::Create(&arena,
69 is_static,
70 is_synchronized,
71 /*is_critical_native*/false,
72 shorty,
73 isa));
David Srbeckydd973932015-04-07 20:29:48 +010074 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010075 ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010076 const int frame_size(jni_conv->FrameSize());
Vladimir Marko32248382016-05-19 10:37:24 +010077 ArrayRef<const ManagedRegister> callee_save_regs = jni_conv->CalleeSaveRegisters();
David Srbeckydd973932015-04-07 20:29:48 +010078
79 // Assemble the method.
Andreas Gampe3b165bc2016-08-01 22:07:04 -070080 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm(
81 JNIMacroAssembler<kPointerSize>::Create(&arena, isa));
Vladimir Marko10ef6942015-10-22 15:25:54 +010082 jni_asm->cfi().SetEnabled(true);
David Srbeckydd973932015-04-07 20:29:48 +010083 jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
84 callee_save_regs, mr_conv->EntrySpills());
85 jni_asm->IncreaseFrameSize(32);
86 jni_asm->DecreaseFrameSize(32);
87 jni_asm->RemoveFrame(frame_size, callee_save_regs);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000088 jni_asm->FinalizeCode();
David Srbeckydd973932015-04-07 20:29:48 +010089 std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
90 MemoryRegion code(&actual_asm[0], actual_asm.size());
91 jni_asm->FinalizeInstructions(code);
92 ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
93 const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
94
95 if (kGenerateExpected) {
96 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
97 } else {
98 EXPECT_EQ(expected_asm, actual_asm);
99 EXPECT_EQ(expected_cfi, actual_cfi);
100 }
101 }
102};
103
104#define TEST_ISA(isa) \
105 TEST_F(JNICFITest, isa) { \
106 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
107 expected_asm_##isa + arraysize(expected_asm_##isa)); \
108 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
109 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
110 TestImpl(isa, #isa, expected_asm, expected_cfi); \
111 }
112
Colin Crossa75b01a2016-08-18 13:45:24 -0700113#ifdef ART_ENABLE_CODEGEN_arm
Roland Levillain6d729a72017-06-30 18:34:01 +0100114// Run the tests for ARM only with Baker read barriers, as the
115// expected generated code contains a Marking Register refresh
116// instruction.
117#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckydd973932015-04-07 20:29:48 +0100118TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700119#endif
Roland Levillain6d729a72017-06-30 18:34:01 +0100120#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100121
Colin Crossa75b01a2016-08-18 13:45:24 -0700122#ifdef ART_ENABLE_CODEGEN_arm64
Roland Levillainaf24def2017-07-12 13:18:01 +0100123// Run the tests for ARM64 only with Baker read barriers, as the
124// expected generated code contains a Marking Register refresh
125// instruction.
126#if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER)
David Srbeckydd973932015-04-07 20:29:48 +0100127TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700128#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100129#endif
130
Colin Crossa75b01a2016-08-18 13:45:24 -0700131#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckydd973932015-04-07 20:29:48 +0100132TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700133#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100134
Colin Crossa75b01a2016-08-18 13:45:24 -0700135#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckydd973932015-04-07 20:29:48 +0100136TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700137#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100138
Colin Crossa75b01a2016-08-18 13:45:24 -0700139#ifdef ART_ENABLE_CODEGEN_mips
David Srbeckydd973932015-04-07 20:29:48 +0100140TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700141#endif
Roland Levillainaf24def2017-07-12 13:18:01 +0100142
Colin Crossa75b01a2016-08-18 13:45:24 -0700143#ifdef ART_ENABLE_CODEGEN_mips64
David Srbeckydd973932015-04-07 20:29:48 +0100144TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700145#endif
David Srbeckydd973932015-04-07 20:29:48 +0100146
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100147#endif // ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +0100148
149} // namespace art