2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "base/stringprintf.h"
20 #include "base/unix_file/fd_file.h"
21 #include "common_compiler_test.h"
23 #include "elf_file_impl.h"
24 #include "elf_builder.h"
25 #include "elf_writer_quick.h"
31 class ElfWriterTest : public CommonCompilerTest {
33 virtual void SetUp() {
35 CommonCompilerTest::SetUp();
39 #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
41 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
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; \
49 EXPECT_EQ(expected_value, addr); \
50 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
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;
58 UnreserveImageSpace();
59 void* dl_oatdata = nullptr;
60 void* dl_oatexec = nullptr;
61 void* dl_oatlastword = nullptr;
63 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
64 ASSERT_TRUE(file.get() != nullptr);
66 std::string error_msg;
67 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
72 CHECK(ef.get() != nullptr) << error_msg;
73 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
74 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
75 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
78 std::string error_msg;
79 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
84 CHECK(ef.get() != nullptr) << error_msg;
85 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
86 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
87 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
90 std::string error_msg;
91 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
96 CHECK(ef.get() != nullptr) << error_msg;
97 CHECK(ef->Load(false, /*low_4gb*/false, &error_msg)) << error_msg;
98 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
99 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
100 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
104 TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
105 const std::vector<std::vector<uintptr_t>> test_data {
106 { 0, 4, 8, 15, 128, 200 },
111 for (const auto& patch_locations : test_data) {
112 constexpr int32_t delta = 0x11235813;
114 // Encode patch locations.
115 std::vector<uint8_t> oat_patches;
116 ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations),
119 // Create buffer to be patched.
120 std::vector<uint8_t> initial_data(256);
121 for (size_t i = 0; i < initial_data.size(); i++) {
126 std::vector<uint8_t> expected = initial_data;
127 for (uintptr_t location : patch_locations) {
128 typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress;
129 *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta;
132 // Decode and apply patch locations.
133 std::vector<uint8_t> actual = initial_data;
134 ElfFileImpl32::ApplyOatPatches(
135 oat_patches.data(), oat_patches.data() + oat_patches.size(), delta,
136 actual.data(), actual.data() + actual.size());
138 EXPECT_EQ(expected, actual);