jni_cfi_test.cc revision 10ef6941648aad04d54527d4a7a6070bf7065e88
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 "cfi_test.h"
22#include "gtest/gtest.h"
23#include "jni/quick/calling_convention.h"
24#include "utils/assembler.h"
25
26#include "jni/jni_cfi_test_expected.inc"
27
28namespace art {
29
30// Run the tests only on host.
31#ifndef __ANDROID__
32
33class JNICFITest : public CFITest {
34 public:
35  // Enable this flag to generate the expected outputs.
36  static constexpr bool kGenerateExpected = false;
37
38  void TestImpl(InstructionSet isa, const char* isa_str,
39                const std::vector<uint8_t>& expected_asm,
40                const std::vector<uint8_t>& expected_cfi) {
41    // Description of simple method.
42    const bool is_static = true;
43    const bool is_synchronized = false;
44    const char* shorty = "IIFII";
45    std::unique_ptr<JniCallingConvention> jni_conv(
46        JniCallingConvention::Create(is_static, is_synchronized, shorty, isa));
47    std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
48        ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, isa));
49    const int frame_size(jni_conv->FrameSize());
50    const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters();
51
52    // Assemble the method.
53    std::unique_ptr<Assembler> jni_asm(Assembler::Create(isa));
54    jni_asm->cfi().SetEnabled(true);
55    jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
56                        callee_save_regs, mr_conv->EntrySpills());
57    jni_asm->IncreaseFrameSize(32);
58    jni_asm->DecreaseFrameSize(32);
59    jni_asm->RemoveFrame(frame_size, callee_save_regs);
60    jni_asm->FinalizeCode();
61    std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
62    MemoryRegion code(&actual_asm[0], actual_asm.size());
63    jni_asm->FinalizeInstructions(code);
64    ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
65    const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
66
67    if (kGenerateExpected) {
68      GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
69    } else {
70      EXPECT_EQ(expected_asm, actual_asm);
71      EXPECT_EQ(expected_cfi, actual_cfi);
72    }
73  }
74};
75
76#define TEST_ISA(isa) \
77  TEST_F(JNICFITest, isa) { \
78    std::vector<uint8_t> expected_asm(expected_asm_##isa, \
79        expected_asm_##isa + arraysize(expected_asm_##isa)); \
80    std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
81        expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
82    TestImpl(isa, #isa, expected_asm, expected_cfi); \
83  }
84
85TEST_ISA(kThumb2)
86TEST_ISA(kArm64)
87TEST_ISA(kX86)
88TEST_ISA(kX86_64)
89TEST_ISA(kMips)
90TEST_ISA(kMips64)
91
92#endif  // __ANDROID__
93
94}  // namespace art
95