blob: 371019a22550f587a70d5cb3a22d461296cbbee3 [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"
David Srbeckydd973932015-04-07 20:29:48 +010022#include "cfi_test.h"
23#include "gtest/gtest.h"
24#include "jni/quick/calling_convention.h"
25#include "utils/assembler.h"
26
27#include "jni/jni_cfi_test_expected.inc"
28
29namespace art {
30
31// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010032#ifndef ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +010033
34class JNICFITest : public CFITest {
35 public:
36 // Enable this flag to generate the expected outputs.
37 static constexpr bool kGenerateExpected = false;
38
39 void TestImpl(InstructionSet isa, const char* isa_str,
40 const std::vector<uint8_t>& expected_asm,
41 const std::vector<uint8_t>& expected_cfi) {
42 // Description of simple method.
43 const bool is_static = true;
44 const bool is_synchronized = false;
45 const char* shorty = "IIFII";
Vladimir Marko93205e32016-04-13 11:59:46 +010046
47 ArenaPool pool;
48 ArenaAllocator arena(&pool);
49
David Srbeckydd973932015-04-07 20:29:48 +010050 std::unique_ptr<JniCallingConvention> jni_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010051 JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010052 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010053 ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
David Srbeckydd973932015-04-07 20:29:48 +010054 const int frame_size(jni_conv->FrameSize());
55 const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters();
56
57 // Assemble the method.
Vladimir Marko93205e32016-04-13 11:59:46 +010058 std::unique_ptr<Assembler> jni_asm(Assembler::Create(&arena, isa));
Vladimir Marko10ef6942015-10-22 15:25:54 +010059 jni_asm->cfi().SetEnabled(true);
David Srbeckydd973932015-04-07 20:29:48 +010060 jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
61 callee_save_regs, mr_conv->EntrySpills());
62 jni_asm->IncreaseFrameSize(32);
63 jni_asm->DecreaseFrameSize(32);
64 jni_asm->RemoveFrame(frame_size, callee_save_regs);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000065 jni_asm->FinalizeCode();
David Srbeckydd973932015-04-07 20:29:48 +010066 std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
67 MemoryRegion code(&actual_asm[0], actual_asm.size());
68 jni_asm->FinalizeInstructions(code);
69 ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
70 const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
71
72 if (kGenerateExpected) {
73 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
74 } else {
75 EXPECT_EQ(expected_asm, actual_asm);
76 EXPECT_EQ(expected_cfi, actual_cfi);
77 }
78 }
79};
80
81#define TEST_ISA(isa) \
82 TEST_F(JNICFITest, isa) { \
83 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
84 expected_asm_##isa + arraysize(expected_asm_##isa)); \
85 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
86 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
87 TestImpl(isa, #isa, expected_asm, expected_cfi); \
88 }
89
90TEST_ISA(kThumb2)
91TEST_ISA(kArm64)
92TEST_ISA(kX86)
93TEST_ISA(kX86_64)
94TEST_ISA(kMips)
95TEST_ISA(kMips64)
96
Bilyan Borisovbb661c02016-04-04 16:27:32 +010097#endif // ART_TARGET_ANDROID
David Srbeckydd973932015-04-07 20:29:48 +010098
99} // namespace art