Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 <stdint.h>
     18 #include <string.h>
     19 #include <sys/mman.h>
     20 
     21 #include <vector>
     22 
     23 #include <gtest/gtest.h>
     24 
     25 #include <unwindstack/Memory.h>
     26 
     27 namespace unwindstack {
     28 
     29 TEST(MemoryLocalTest, read) {
     30   std::vector<uint8_t> src(1024);
     31   memset(src.data(), 0x4c, 1024);
     32 
     33   MemoryLocal local;
     34 
     35   std::vector<uint8_t> dst(1024);
     36   ASSERT_TRUE(local.ReadFully(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
     37   ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
     38   for (size_t i = 0; i < 1024; i++) {
     39     ASSERT_EQ(0x4cU, dst[i]);
     40   }
     41 
     42   memset(src.data(), 0x23, 512);
     43   ASSERT_TRUE(local.ReadFully(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
     44   ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
     45   for (size_t i = 0; i < 512; i++) {
     46     ASSERT_EQ(0x23U, dst[i]);
     47   }
     48   for (size_t i = 512; i < 1024; i++) {
     49     ASSERT_EQ(0x4cU, dst[i]);
     50   }
     51 }
     52 
     53 TEST(MemoryLocalTest, read_illegal) {
     54   MemoryLocal local;
     55 
     56   std::vector<uint8_t> dst(100);
     57   ASSERT_FALSE(local.ReadFully(0, dst.data(), 1));
     58   ASSERT_FALSE(local.ReadFully(0, dst.data(), 100));
     59 }
     60 
     61 TEST(MemoryLocalTest, read_overflow) {
     62   MemoryLocal local;
     63 
     64   // On 32 bit this test doesn't necessarily cause an overflow. The 64 bit
     65   // version will always go through the overflow check.
     66   std::vector<uint8_t> dst(100);
     67   uint64_t value;
     68   ASSERT_FALSE(local.ReadFully(reinterpret_cast<uint64_t>(&value), dst.data(), SIZE_MAX));
     69 }
     70 
     71 TEST(MemoryLocalTest, Read) {
     72   char* mapping = static_cast<char*>(
     73       mmap(nullptr, 2 * getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
     74 
     75   ASSERT_NE(MAP_FAILED, mapping);
     76 
     77   mprotect(mapping + getpagesize(), getpagesize(), PROT_NONE);
     78   memset(mapping + getpagesize() - 1024, 0x4c, 1024);
     79 
     80   MemoryLocal local;
     81 
     82   std::vector<uint8_t> dst(4096);
     83   ASSERT_EQ(1024U, local.Read(reinterpret_cast<uint64_t>(mapping + getpagesize() - 1024),
     84                               dst.data(), 4096));
     85   for (size_t i = 0; i < 1024; i++) {
     86     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
     87   }
     88 
     89   ASSERT_EQ(0, munmap(mapping, 2 * getpagesize()));
     90 }
     91 
     92 TEST(MemoryLocalTest, read_hole) {
     93   void* mapping =
     94       mmap(nullptr, 3 * 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
     95   ASSERT_NE(MAP_FAILED, mapping);
     96   memset(mapping, 0xFF, 3 * 4096);
     97   mprotect(static_cast<char*>(mapping) + 4096, 4096, PROT_NONE);
     98 
     99   MemoryLocal local;
    100   std::vector<uint8_t> dst(4096 * 3, 0xCC);
    101   ASSERT_EQ(4096U, local.Read(reinterpret_cast<uintptr_t>(mapping), dst.data(), 4096 * 3));
    102   for (size_t i = 0; i < 4096; ++i) {
    103     ASSERT_EQ(0xFF, dst[i]);
    104   }
    105   for (size_t i = 4096; i < 4096 * 3; ++i) {
    106     ASSERT_EQ(0xCC, dst[i]);
    107   }
    108   ASSERT_EQ(0, munmap(mapping, 3 * 4096));
    109 }
    110 
    111 }  // namespace unwindstack
    112