Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2016 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.h>
     18 #include <errno.h>
     19 #include <signal.h>
     20 #include <string.h>
     21 #include <sys/mman.h>
     22 #include <sys/ptrace.h>
     23 #include <sys/types.h>
     24 #include <unistd.h>
     25 
     26 #include <memory>
     27 #include <vector>
     28 
     29 #include <android-base/file.h>
     30 #include <android-base/test_utils.h>
     31 #include <gtest/gtest.h>
     32 
     33 #include <unwindstack/Elf.h>
     34 #include <unwindstack/MapInfo.h>
     35 #include <unwindstack/Memory.h>
     36 
     37 #include "MemoryFake.h"
     38 
     39 namespace unwindstack {
     40 
     41 class MapInfoCreateMemoryTest : public ::testing::Test {
     42  protected:
     43   template <typename Ehdr, typename Shdr>
     44   static void InitElf(int fd, uint64_t file_offset, uint64_t sh_offset, uint8_t class_type) {
     45     std::vector<uint8_t> buffer(20000);
     46     memset(buffer.data(), 0, buffer.size());
     47 
     48     Ehdr ehdr;
     49     memset(&ehdr, 0, sizeof(ehdr));
     50     memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
     51     ehdr.e_ident[EI_CLASS] = class_type;
     52     ehdr.e_shoff = sh_offset;
     53     ehdr.e_shentsize = sizeof(Shdr) + 100;
     54     ehdr.e_shnum = 4;
     55     memcpy(&buffer[file_offset], &ehdr, sizeof(ehdr));
     56 
     57     ASSERT_TRUE(android::base::WriteFully(fd, buffer.data(), buffer.size()));
     58   }
     59 
     60   static void SetUpTestCase() {
     61     std::vector<uint8_t> buffer(1024);
     62     memset(buffer.data(), 0, buffer.size());
     63     memcpy(buffer.data(), ELFMAG, SELFMAG);
     64     buffer[EI_CLASS] = ELFCLASS32;
     65     ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
     66 
     67     memset(buffer.data(), 0, buffer.size());
     68     memcpy(&buffer[0x100], ELFMAG, SELFMAG);
     69     buffer[0x100 + EI_CLASS] = ELFCLASS64;
     70     ASSERT_TRUE(android::base::WriteFully(elf_at_100_.fd, buffer.data(), buffer.size()));
     71 
     72     InitElf<Elf32_Ehdr, Elf32_Shdr>(elf32_at_map_.fd, 0x1000, 0x2000, ELFCLASS32);
     73     InitElf<Elf64_Ehdr, Elf64_Shdr>(elf64_at_map_.fd, 0x2000, 0x3000, ELFCLASS64);
     74   }
     75 
     76   void SetUp() override {
     77     memory_ = new MemoryFake;
     78     process_memory_.reset(memory_);
     79   }
     80 
     81   MemoryFake* memory_;
     82   std::shared_ptr<Memory> process_memory_;
     83 
     84   static TemporaryFile elf_;
     85 
     86   static TemporaryFile elf_at_100_;
     87 
     88   static TemporaryFile elf32_at_map_;
     89   static TemporaryFile elf64_at_map_;
     90 };
     91 TemporaryFile MapInfoCreateMemoryTest::elf_;
     92 TemporaryFile MapInfoCreateMemoryTest::elf_at_100_;
     93 TemporaryFile MapInfoCreateMemoryTest::elf32_at_map_;
     94 TemporaryFile MapInfoCreateMemoryTest::elf64_at_map_;
     95 
     96 TEST_F(MapInfoCreateMemoryTest, end_le_start) {
     97   MapInfo info(0x100, 0x100, 0, 0, elf_.path);
     98 
     99   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    100   ASSERT_TRUE(memory.get() == nullptr);
    101 
    102   info.end = 0xff;
    103   memory.reset(info.CreateMemory(process_memory_));
    104   ASSERT_TRUE(memory.get() == nullptr);
    105 
    106   // Make sure this test is valid.
    107   info.end = 0x101;
    108   memory.reset(info.CreateMemory(process_memory_));
    109   ASSERT_TRUE(memory.get() != nullptr);
    110 }
    111 
    112 // Verify that if the offset is non-zero but there is no elf at the offset,
    113 // that the full file is used.
    114 TEST_F(MapInfoCreateMemoryTest, file_backed_non_zero_offset_full_file) {
    115   MapInfo info(0x100, 0x200, 0x100, 0, elf_.path);
    116 
    117   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    118   ASSERT_TRUE(memory.get() != nullptr);
    119   ASSERT_EQ(0x100U, info.elf_offset);
    120 
    121   // Read the entire file.
    122   std::vector<uint8_t> buffer(1024);
    123   ASSERT_TRUE(memory->ReadFully(0, buffer.data(), 1024));
    124   ASSERT_TRUE(memcmp(buffer.data(), ELFMAG, SELFMAG) == 0);
    125   ASSERT_EQ(ELFCLASS32, buffer[EI_CLASS]);
    126   for (size_t i = EI_CLASS + 1; i < buffer.size(); i++) {
    127     ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
    128   }
    129 
    130   ASSERT_FALSE(memory->ReadFully(1024, buffer.data(), 1));
    131 }
    132 
    133 // Verify that if the offset is non-zero and there is an elf at that
    134 // offset, that only part of the file is used.
    135 TEST_F(MapInfoCreateMemoryTest, file_backed_non_zero_offset_partial_file) {
    136   MapInfo info(0x100, 0x200, 0x100, 0, elf_at_100_.path);
    137 
    138   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    139   ASSERT_TRUE(memory.get() != nullptr);
    140   ASSERT_EQ(0U, info.elf_offset);
    141 
    142   // Read the valid part of the file.
    143   std::vector<uint8_t> buffer(0x100);
    144   ASSERT_TRUE(memory->ReadFully(0, buffer.data(), 0x100));
    145   ASSERT_TRUE(memcmp(buffer.data(), ELFMAG, SELFMAG) == 0);
    146   ASSERT_EQ(ELFCLASS64, buffer[EI_CLASS]);
    147   for (size_t i = EI_CLASS + 1; i < buffer.size(); i++) {
    148     ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
    149   }
    150 
    151   ASSERT_FALSE(memory->ReadFully(0x100, buffer.data(), 1));
    152 }
    153 
    154 // Verify that if the offset is non-zero and there is an elf at that
    155 // offset, that only part of the file is used. Further verify that if the
    156 // embedded elf is bigger than the initial map, the new object is larger
    157 // than the original map size. Do this for a 32 bit elf and a 64 bit elf.
    158 TEST_F(MapInfoCreateMemoryTest, file_backed_non_zero_offset_partial_file_whole_elf32) {
    159   MapInfo info(0x5000, 0x6000, 0x1000, 0, elf32_at_map_.path);
    160 
    161   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    162   ASSERT_TRUE(memory.get() != nullptr);
    163   ASSERT_EQ(0U, info.elf_offset);
    164 
    165   // Verify the memory is a valid elf.
    166   uint8_t e_ident[SELFMAG + 1];
    167   ASSERT_TRUE(memory->ReadFully(0, e_ident, SELFMAG));
    168   ASSERT_EQ(0, memcmp(e_ident, ELFMAG, SELFMAG));
    169 
    170   // Read past the end of what would normally be the size of the map.
    171   ASSERT_TRUE(memory->ReadFully(0x1000, e_ident, 1));
    172 }
    173 
    174 TEST_F(MapInfoCreateMemoryTest, file_backed_non_zero_offset_partial_file_whole_elf64) {
    175   MapInfo info(0x7000, 0x8000, 0x2000, 0, elf64_at_map_.path);
    176 
    177   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    178   ASSERT_TRUE(memory.get() != nullptr);
    179   ASSERT_EQ(0U, info.elf_offset);
    180 
    181   // Verify the memory is a valid elf.
    182   uint8_t e_ident[SELFMAG + 1];
    183   ASSERT_TRUE(memory->ReadFully(0, e_ident, SELFMAG));
    184   ASSERT_EQ(0, memcmp(e_ident, ELFMAG, SELFMAG));
    185 
    186   // Read past the end of what would normally be the size of the map.
    187   ASSERT_TRUE(memory->ReadFully(0x1000, e_ident, 1));
    188 }
    189 
    190 // Verify that device file names will never result in Memory object creation.
    191 TEST_F(MapInfoCreateMemoryTest, check_device_maps) {
    192   // Set up some memory so that a valid local memory object would
    193   // be returned if the file mapping fails, but the device check is incorrect.
    194   std::vector<uint8_t> buffer(1024);
    195   MapInfo info;
    196   info.start = reinterpret_cast<uint64_t>(buffer.data());
    197   info.end = info.start + buffer.size();
    198   info.offset = 0;
    199 
    200   info.flags = 0x8000;
    201   info.name = "/dev/something";
    202   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    203   ASSERT_TRUE(memory.get() == nullptr);
    204 }
    205 
    206 TEST_F(MapInfoCreateMemoryTest, process_memory) {
    207   MapInfo info;
    208   info.start = 0x2000;
    209   info.end = 0x3000;
    210   info.offset = 0;
    211 
    212   // Verify that the the process_memory object is used, so seed it
    213   // with memory.
    214   std::vector<uint8_t> buffer(1024);
    215   for (size_t i = 0; i < buffer.size(); i++) {
    216     buffer[i] = i % 256;
    217   }
    218   memory_->SetMemory(info.start, buffer.data(), buffer.size());
    219 
    220   std::unique_ptr<Memory> memory(info.CreateMemory(process_memory_));
    221   ASSERT_TRUE(memory.get() != nullptr);
    222 
    223   memset(buffer.data(), 0, buffer.size());
    224   ASSERT_TRUE(memory->ReadFully(0, buffer.data(), buffer.size()));
    225   for (size_t i = 0; i < buffer.size(); i++) {
    226     ASSERT_EQ(i % 256, buffer[i]) << "Failed at byte " << i;
    227   }
    228 
    229   // Try to read outside of the map size.
    230   ASSERT_FALSE(memory->ReadFully(buffer.size(), buffer.data(), 1));
    231 }
    232 
    233 }  // namespace unwindstack
    234