blob: 28b7290bef3df725dab4ab8a47aa34d563a4db67 [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"
26#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070027#include "utils/jni_macro_assembler.h"
David Srbeckydd973932015-04-07 20:29:48 +010028
29#include "jni/jni_cfi_test_expected.inc"
30
31namespace art {
32
33// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010034#ifndef ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +010035
36class JNICFITest : public CFITest {
37 public:
38 // Enable this flag to generate the expected outputs.
39 static constexpr bool kGenerateExpected = false;
40
Andreas Gampe3b165bc2016-08-01 22:07:04 -070041 void TestImpl(InstructionSet isa,
42 const char* isa_str,
David Srbeckydd973932015-04-07 20:29:48 +010043 const std::vector<uint8_t>& expected_asm,
44 const std::vector<uint8_t>& expected_cfi) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045 if (Is64BitInstructionSet(isa)) {
46 TestImplSized<PointerSize::k64>(isa, isa_str, expected_asm, expected_cfi);
47 } else {
48 TestImplSized<PointerSize::k32>(isa, isa_str, expected_asm, expected_cfi);
49 }
50 }
51
52 private:
53 template <PointerSize kPointerSize>
54 void TestImplSized(InstructionSet isa,
55 const char* isa_str,
56 const std::vector<uint8_t>& expected_asm,
57 const std::vector<uint8_t>& expected_cfi) {
David Srbeckydd973932015-04-07 20:29:48 +010058 // Description of simple method.
59 const bool is_static = true;
60 const bool is_synchronized = false;
61 const char* shorty = "IIFII";
Vladimir Marko93205e32016-04-13 11:59:46 +010062
63 ArenaPool pool;
64 ArenaAllocator arena(&pool);
65
David Srbeckydd973932015-04-07 20:29:48 +010066 std::unique_ptr<JniCallingConvention> jni_conv(
Igor Murashkin367f3dd2016-09-01 17:00:24 -070067 JniCallingConvention::Create(&arena,
68 is_static,
69 is_synchronized,
70 /*is_critical_native*/false,
71 shorty,
72 isa));
David Srbeckydd973932015-04-07 20:29:48 +010073 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010074 ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010075 const int frame_size(jni_conv->FrameSize());
Vladimir Marko32248382016-05-19 10:37:24 +010076 ArrayRef<const ManagedRegister> callee_save_regs = jni_conv->CalleeSaveRegisters();
David Srbeckydd973932015-04-07 20:29:48 +010077
78 // Assemble the method.
Andreas Gampe3b165bc2016-08-01 22:07:04 -070079 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm(
80 JNIMacroAssembler<kPointerSize>::Create(&arena, isa));
Vladimir Marko10ef6942015-10-22 15:25:54 +010081 jni_asm->cfi().SetEnabled(true);
David Srbeckydd973932015-04-07 20:29:48 +010082 jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
83 callee_save_regs, mr_conv->EntrySpills());
84 jni_asm->IncreaseFrameSize(32);
85 jni_asm->DecreaseFrameSize(32);
86 jni_asm->RemoveFrame(frame_size, callee_save_regs);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000087 jni_asm->FinalizeCode();
David Srbeckydd973932015-04-07 20:29:48 +010088 std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
89 MemoryRegion code(&actual_asm[0], actual_asm.size());
90 jni_asm->FinalizeInstructions(code);
91 ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
92 const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
93
94 if (kGenerateExpected) {
95 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
96 } else {
97 EXPECT_EQ(expected_asm, actual_asm);
98 EXPECT_EQ(expected_cfi, actual_cfi);
99 }
100 }
101};
102
103#define TEST_ISA(isa) \
104 TEST_F(JNICFITest, isa) { \
105 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
106 expected_asm_##isa + arraysize(expected_asm_##isa)); \
107 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
108 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
109 TestImpl(isa, #isa, expected_asm, expected_cfi); \
110 }
111
Colin Crossa75b01a2016-08-18 13:45:24 -0700112#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckydd973932015-04-07 20:29:48 +0100113TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700114#endif
115#ifdef ART_ENABLE_CODEGEN_arm64
David Srbeckydd973932015-04-07 20:29:48 +0100116TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700117#endif
118#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckydd973932015-04-07 20:29:48 +0100119TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700120#endif
121#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckydd973932015-04-07 20:29:48 +0100122TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700123#endif
124#ifdef ART_ENABLE_CODEGEN_mips
David Srbeckydd973932015-04-07 20:29:48 +0100125TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700126#endif
127#ifdef ART_ENABLE_CODEGEN_mips64
David Srbeckydd973932015-04-07 20:29:48 +0100128TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700129#endif
David Srbeckydd973932015-04-07 20:29:48 +0100130
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100131#endif // ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +0100132
133} // namespace art