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