1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <elf.h>
#include <unistd.h>
#include <android-base/file.h>
#include <gtest/gtest.h>
#include <unwindstack/Elf.h>
#include <unwindstack/MapInfo.h>
#include "ElfTestUtils.h"
#include "MemoryFake.h"
namespace unwindstack {
class ElfCacheTest : public ::testing::Test {
protected:
static void SetUpTestCase() { memory_.reset(new MemoryFake); }
void SetUp() override { Elf::SetCachingEnabled(true); }
void TearDown() override { Elf::SetCachingEnabled(false); }
void WriteElfFile(uint64_t offset, TemporaryFile* tf, uint32_t type) {
ASSERT_TRUE(type == EM_ARM || type == EM_386 || type == EM_X86_64);
size_t ehdr_size;
Elf32_Ehdr ehdr32;
Elf64_Ehdr ehdr64;
void* ptr;
if (type == EM_ARM || type == EM_386) {
ehdr_size = sizeof(ehdr32);
ptr = &ehdr32;
TestInitEhdr(&ehdr32, ELFCLASS32, type);
} else {
ehdr_size = sizeof(ehdr64);
ptr = &ehdr64;
TestInitEhdr(&ehdr64, ELFCLASS64, type);
}
ASSERT_EQ(offset, static_cast<uint64_t>(lseek(tf->fd, offset, SEEK_SET)));
ASSERT_TRUE(android::base::WriteFully(tf->fd, ptr, ehdr_size));
}
void VerifyWithinSameMap(bool cache_enabled);
void VerifySameMap(bool cache_enabled);
void VerifyWithinSameMapNeverReadAtZero(bool cache_enabled);
static std::shared_ptr<Memory> memory_;
};
std::shared_ptr<Memory> ElfCacheTest::memory_;
void ElfCacheTest::VerifySameMap(bool cache_enabled) {
if (!cache_enabled) {
Elf::SetCachingEnabled(false);
}
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
WriteElfFile(0, &tf, EM_ARM);
close(tf.fd);
uint64_t start = 0x1000;
uint64_t end = 0x20000;
MapInfo info1(nullptr, start, end, 0, 0x5, tf.path);
MapInfo info2(nullptr, start, end, 0, 0x5, tf.path);
Elf* elf1 = info1.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf1->valid());
Elf* elf2 = info2.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf2->valid());
if (cache_enabled) {
EXPECT_EQ(elf1, elf2);
} else {
EXPECT_NE(elf1, elf2);
}
}
TEST_F(ElfCacheTest, no_caching) {
VerifySameMap(false);
}
TEST_F(ElfCacheTest, caching_invalid_elf) {
VerifySameMap(true);
}
void ElfCacheTest::VerifyWithinSameMap(bool cache_enabled) {
if (!cache_enabled) {
Elf::SetCachingEnabled(false);
}
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
WriteElfFile(0, &tf, EM_ARM);
WriteElfFile(0x100, &tf, EM_386);
WriteElfFile(0x200, &tf, EM_X86_64);
lseek(tf.fd, 0x500, SEEK_SET);
uint8_t value = 0;
write(tf.fd, &value, 1);
close(tf.fd);
uint64_t start = 0x1000;
uint64_t end = 0x20000;
// Will have an elf at offset 0 in file.
MapInfo info0_1(nullptr, start, end, 0, 0x5, tf.path);
MapInfo info0_2(nullptr, start, end, 0, 0x5, tf.path);
// Will have an elf at offset 0x100 in file.
MapInfo info100_1(nullptr, start, end, 0x100, 0x5, tf.path);
MapInfo info100_2(nullptr, start, end, 0x100, 0x5, tf.path);
// Will have an elf at offset 0x200 in file.
MapInfo info200_1(nullptr, start, end, 0x200, 0x5, tf.path);
MapInfo info200_2(nullptr, start, end, 0x200, 0x5, tf.path);
// Will have an elf at offset 0 in file.
MapInfo info300_1(nullptr, start, end, 0x300, 0x5, tf.path);
MapInfo info300_2(nullptr, start, end, 0x300, 0x5, tf.path);
Elf* elf0_1 = info0_1.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf0_1->valid());
EXPECT_EQ(ARCH_ARM, elf0_1->arch());
Elf* elf0_2 = info0_2.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf0_2->valid());
EXPECT_EQ(ARCH_ARM, elf0_2->arch());
EXPECT_EQ(0U, info0_1.elf_offset);
EXPECT_EQ(0U, info0_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf0_1, elf0_2);
} else {
EXPECT_NE(elf0_1, elf0_2);
}
Elf* elf100_1 = info100_1.GetElf(memory_, ARCH_X86);
ASSERT_TRUE(elf100_1->valid());
EXPECT_EQ(ARCH_X86, elf100_1->arch());
Elf* elf100_2 = info100_2.GetElf(memory_, ARCH_X86);
ASSERT_TRUE(elf100_2->valid());
EXPECT_EQ(ARCH_X86, elf100_2->arch());
EXPECT_EQ(0U, info100_1.elf_offset);
EXPECT_EQ(0U, info100_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf100_1, elf100_2);
} else {
EXPECT_NE(elf100_1, elf100_2);
}
Elf* elf200_1 = info200_1.GetElf(memory_, ARCH_X86_64);
ASSERT_TRUE(elf200_1->valid());
EXPECT_EQ(ARCH_X86_64, elf200_1->arch());
Elf* elf200_2 = info200_2.GetElf(memory_, ARCH_X86_64);
ASSERT_TRUE(elf200_2->valid());
EXPECT_EQ(ARCH_X86_64, elf200_2->arch());
EXPECT_EQ(0U, info200_1.elf_offset);
EXPECT_EQ(0U, info200_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf200_1, elf200_2);
} else {
EXPECT_NE(elf200_1, elf200_2);
}
Elf* elf300_1 = info300_1.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf300_1->valid());
EXPECT_EQ(ARCH_ARM, elf300_1->arch());
Elf* elf300_2 = info300_2.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf300_2->valid());
EXPECT_EQ(ARCH_ARM, elf300_2->arch());
EXPECT_EQ(0x300U, info300_1.elf_offset);
EXPECT_EQ(0x300U, info300_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf300_1, elf300_2);
EXPECT_EQ(elf0_1, elf300_1);
} else {
EXPECT_NE(elf300_1, elf300_2);
EXPECT_NE(elf0_1, elf300_1);
}
}
TEST_F(ElfCacheTest, no_caching_valid_elf_offset_non_zero) {
VerifyWithinSameMap(false);
}
TEST_F(ElfCacheTest, caching_valid_elf_offset_non_zero) {
VerifyWithinSameMap(true);
}
// Verify that when reading from multiple non-zero offsets in the same map
// that when cached, all of the elf objects are the same.
void ElfCacheTest::VerifyWithinSameMapNeverReadAtZero(bool cache_enabled) {
if (!cache_enabled) {
Elf::SetCachingEnabled(false);
}
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
WriteElfFile(0, &tf, EM_ARM);
lseek(tf.fd, 0x500, SEEK_SET);
uint8_t value = 0;
write(tf.fd, &value, 1);
close(tf.fd);
uint64_t start = 0x1000;
uint64_t end = 0x20000;
// Multiple info sections at different offsets will have non-zero elf offsets.
MapInfo info300_1(nullptr, start, end, 0x300, 0x5, tf.path);
MapInfo info300_2(nullptr, start, end, 0x300, 0x5, tf.path);
MapInfo info400_1(nullptr, start, end, 0x400, 0x5, tf.path);
MapInfo info400_2(nullptr, start, end, 0x400, 0x5, tf.path);
Elf* elf300_1 = info300_1.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf300_1->valid());
EXPECT_EQ(ARCH_ARM, elf300_1->arch());
Elf* elf300_2 = info300_2.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf300_2->valid());
EXPECT_EQ(ARCH_ARM, elf300_2->arch());
EXPECT_EQ(0x300U, info300_1.elf_offset);
EXPECT_EQ(0x300U, info300_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf300_1, elf300_2);
} else {
EXPECT_NE(elf300_1, elf300_2);
}
Elf* elf400_1 = info400_1.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf400_1->valid());
EXPECT_EQ(ARCH_ARM, elf400_1->arch());
Elf* elf400_2 = info400_2.GetElf(memory_, ARCH_ARM);
ASSERT_TRUE(elf400_2->valid());
EXPECT_EQ(ARCH_ARM, elf400_2->arch());
EXPECT_EQ(0x400U, info400_1.elf_offset);
EXPECT_EQ(0x400U, info400_2.elf_offset);
if (cache_enabled) {
EXPECT_EQ(elf400_1, elf400_2);
EXPECT_EQ(elf300_1, elf400_1);
} else {
EXPECT_NE(elf400_1, elf400_2);
EXPECT_NE(elf300_1, elf400_1);
}
}
TEST_F(ElfCacheTest, no_caching_valid_elf_offset_non_zero_never_read_at_zero) {
VerifyWithinSameMapNeverReadAtZero(false);
}
TEST_F(ElfCacheTest, caching_valid_elf_offset_non_zero_never_read_at_zero) {
VerifyWithinSameMapNeverReadAtZero(true);
}
} // namespace unwindstack
|