OSDN Git Service

Merge "Rename options for shorter names." am: bc58b020c8 am: b4bfd7f529
[android-x86/art.git] / compiler / elf_writer_test.cc
1 /*
2  * Copyright (C) 2011 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 "elf_file.h"
18
19 #include "base/stringprintf.h"
20 #include "base/unix_file/fd_file.h"
21 #include "common_compiler_test.h"
22 #include "elf_file.h"
23 #include "elf_file_impl.h"
24 #include "elf_builder.h"
25 #include "elf_writer_quick.h"
26 #include "oat.h"
27 #include "utils.h"
28
29 namespace art {
30
31 class ElfWriterTest : public CommonCompilerTest {
32  protected:
33   virtual void SetUp() {
34     ReserveImageSpace();
35     CommonCompilerTest::SetUp();
36   }
37 };
38
39 #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
40   do { \
41     void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
42                                                                symbol_name, \
43                                                                build_map)); \
44     EXPECT_NE(nullptr, addr); \
45     EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
46     if (expected_value == nullptr) { \
47       expected_value = addr; \
48     }                        \
49     EXPECT_EQ(expected_value, addr); \
50     EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
51   } while (false)
52
53 TEST_F(ElfWriterTest, dlsym) {
54   std::string elf_location = GetCoreOatLocation();
55   std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
56   LOG(INFO) << "elf_filename=" << elf_filename;
57
58   UnreserveImageSpace();
59   void* dl_oatdata = nullptr;
60   void* dl_oatexec = nullptr;
61   void* dl_oatlastword = nullptr;
62
63   std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
64   ASSERT_TRUE(file.get() != nullptr);
65   {
66     std::string error_msg;
67     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
68     CHECK(ef.get() != nullptr) << error_msg;
69     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
70     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
71     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
72   }
73   {
74     std::string error_msg;
75     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
76     CHECK(ef.get() != nullptr) << error_msg;
77     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
78     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
79     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
80   }
81   {
82     std::string error_msg;
83     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
84     CHECK(ef.get() != nullptr) << error_msg;
85     CHECK(ef->Load(false, &error_msg)) << error_msg;
86     EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
87     EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
88     EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
89   }
90 }
91
92 TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
93   const std::vector<std::vector<uintptr_t>> test_data {
94       { 0, 4, 8, 15, 128, 200 },
95       { 8, 8 + 127 },
96       { 8, 8 + 128 },
97       { },
98   };
99   for (const auto& patch_locations : test_data) {
100     constexpr int32_t delta = 0x11235813;
101
102     // Encode patch locations.
103     std::vector<uint8_t> oat_patches;
104     ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations),
105                                              &oat_patches);
106
107     // Create buffer to be patched.
108     std::vector<uint8_t> initial_data(256);
109     for (size_t i = 0; i < initial_data.size(); i++) {
110       initial_data[i] = i;
111     }
112
113     // Patch manually.
114     std::vector<uint8_t> expected = initial_data;
115     for (uintptr_t location : patch_locations) {
116       typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress;
117       *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta;
118     }
119
120     // Decode and apply patch locations.
121     std::vector<uint8_t> actual = initial_data;
122     ElfFileImpl32::ApplyOatPatches(
123         oat_patches.data(), oat_patches.data() + oat_patches.size(), delta,
124         actual.data(), actual.data() + actual.size());
125
126     EXPECT_EQ(expected, actual);
127   }
128 }
129
130 }  // namespace art