blob: 524ce4d34e6ab5e9a423cff3939eaa735e95cf1f [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(
Vladimir Marko93205e32016-04-13 11:59:46 +010067 JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010068 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010069 ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010070 const int frame_size(jni_conv->FrameSize());
Vladimir Marko32248382016-05-19 10:37:24 +010071 ArrayRef<const ManagedRegister> callee_save_regs = jni_conv->CalleeSaveRegisters();
David Srbeckydd973932015-04-07 20:29:48 +010072
73 // Assemble the method.
Andreas Gampe3b165bc2016-08-01 22:07:04 -070074 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm(
75 JNIMacroAssembler<kPointerSize>::Create(&arena, isa));
Vladimir Marko10ef6942015-10-22 15:25:54 +010076 jni_asm->cfi().SetEnabled(true);
David Srbeckydd973932015-04-07 20:29:48 +010077 jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
78 callee_save_regs, mr_conv->EntrySpills());
79 jni_asm->IncreaseFrameSize(32);
80 jni_asm->DecreaseFrameSize(32);
81 jni_asm->RemoveFrame(frame_size, callee_save_regs);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000082 jni_asm->FinalizeCode();
David Srbeckydd973932015-04-07 20:29:48 +010083 std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
84 MemoryRegion code(&actual_asm[0], actual_asm.size());
85 jni_asm->FinalizeInstructions(code);
86 ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
87 const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
88
89 if (kGenerateExpected) {
90 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
91 } else {
92 EXPECT_EQ(expected_asm, actual_asm);
93 EXPECT_EQ(expected_cfi, actual_cfi);
94 }
95 }
96};
97
98#define TEST_ISA(isa) \
99 TEST_F(JNICFITest, isa) { \
100 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
101 expected_asm_##isa + arraysize(expected_asm_##isa)); \
102 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
103 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
104 TestImpl(isa, #isa, expected_asm, expected_cfi); \
105 }
106
107TEST_ISA(kThumb2)
108TEST_ISA(kArm64)
109TEST_ISA(kX86)
110TEST_ISA(kX86_64)
111TEST_ISA(kMips)
112TEST_ISA(kMips64)
113
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100114#endif // ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +0100115
116} // namespace art