jni_cfi_test.cc revision 93205e395f777c1dd81d3f164cf9a4aec4bde45f
1/*
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"
21#include "base/arena_allocator.h"
22#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.
32#ifndef __ANDROID__
33
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";
46
47    ArenaPool pool;
48    ArenaAllocator arena(&pool);
49
50    std::unique_ptr<JniCallingConvention> jni_conv(
51        JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
52    std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
53        ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa));
54    const int frame_size(jni_conv->FrameSize());
55    const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters();
56
57    // Assemble the method.
58    std::unique_ptr<Assembler> jni_asm(Assembler::Create(&arena, isa));
59    jni_asm->cfi().SetEnabled(true);
60    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);
65    jni_asm->FinalizeCode();
66    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
97#endif  // __ANDROID__
98
99}  // namespace art
100