From 29c5378e91720b7befabc91fbab0f25d59a23bf1 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 26 Sep 2017 14:11:37 -0700 Subject: [PATCH] unwindstack: add Memory::ReadPartially. Add a way to read while allowing for partial reads. Test: new tests added to libunwindstack_test, ran 32/64 on hikey960, sailfish Test: ran unwind on hikey960/sailfish Change-Id: I8b11d9230fcd3122148ef3f980863ac1404ad70a --- libunwindstack/MapInfo.cpp | 2 +- libunwindstack/Memory.cpp | 238 ++++++++++---------- libunwindstack/include/unwindstack/Memory.h | 52 +++-- libunwindstack/tests/MemoryBufferTest.cpp | 20 ++ libunwindstack/tests/MemoryFake.cpp | 6 +- libunwindstack/tests/MemoryFake.h | 18 +- libunwindstack/tests/MemoryLocalTest.cpp | 41 ++++ libunwindstack/tests/MemoryRangeTest.cpp | 21 +- libunwindstack/tests/MemoryRemoteTest.cpp | 74 +++++- 9 files changed, 301 insertions(+), 171 deletions(-) diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp index 140d05a92..6239c8138 100644 --- a/libunwindstack/MapInfo.cpp +++ b/libunwindstack/MapInfo.cpp @@ -101,7 +101,7 @@ Memory* MapInfo::CreateMemory(const std::shared_ptr& process_memory) { if (!(flags & PROT_READ)) { return nullptr; } - return new MemoryRange(process_memory, start, end); + return new MemoryRange(process_memory, start, end - start, 0); } Elf* MapInfo::GetElf(const std::shared_ptr& process_memory, bool init_gnu_debugdata) { diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp index 32753df6f..f27a350d4 100644 --- a/libunwindstack/Memory.cpp +++ b/libunwindstack/Memory.cpp @@ -32,8 +32,63 @@ #include "Check.h" +static size_t ProcessVmRead(pid_t pid, void* dst, uint64_t remote_src, size_t len) { + struct iovec dst_iov = { + .iov_base = dst, + .iov_len = len, + }; + + // Split up the remote read across page boundaries. + // From the manpage: + // A partial read/write may result if one of the remote_iov elements points to an invalid + // memory region in the remote process. + // + // Partial transfers apply at the granularity of iovec elements. These system calls won't + // perform a partial transfer that splits a single iovec element. + constexpr size_t kMaxIovecs = 64; + struct iovec src_iovs[kMaxIovecs]; + size_t iovecs_used = 0; + + uint64_t cur = remote_src; + while (len > 0) { + if (iovecs_used == kMaxIovecs) { + errno = EINVAL; + return 0; + } + + // struct iovec uses void* for iov_base. + if (cur >= UINTPTR_MAX) { + errno = EFAULT; + return 0; + } + + src_iovs[iovecs_used].iov_base = reinterpret_cast(cur); + + uintptr_t misalignment = cur & (getpagesize() - 1); + size_t iov_len = getpagesize() - misalignment; + iov_len = std::min(iov_len, len); + + len -= iov_len; + if (__builtin_add_overflow(cur, iov_len, &cur)) { + errno = EFAULT; + return 0; + } + + src_iovs[iovecs_used].iov_len = iov_len; + ++iovecs_used; + } + + ssize_t rc = process_vm_readv(pid, &dst_iov, 1, src_iovs, iovecs_used, 0); + return rc == -1 ? 0 : rc; +} + namespace unwindstack { +bool Memory::Read(uint64_t addr, void* dst, size_t size) { + size_t rc = ReadPartially(addr, dst, size); + return rc == size; +} + bool Memory::ReadString(uint64_t addr, std::string* string, uint64_t max_read) { string->clear(); uint64_t bytes_read = 0; @@ -59,16 +114,17 @@ std::shared_ptr Memory::CreateProcessMemory(pid_t pid) { return std::shared_ptr(new MemoryRemote(pid)); } -bool MemoryBuffer::Read(uint64_t addr, void* dst, size_t size) { - uint64_t last_read_byte; - if (__builtin_add_overflow(size, addr, &last_read_byte)) { - return false; +size_t MemoryBuffer::ReadPartially(uint64_t addr, void* dst, size_t size) { + if (addr >= raw_.size()) { + return 0; } - if (last_read_byte > raw_.size()) { - return false; - } - memcpy(dst, &raw_[addr], size); - return true; + + size_t bytes_left = raw_.size() - static_cast(addr); + const unsigned char* actual_base = static_cast(raw_.data()) + addr; + size_t actual_len = std::min(bytes_left, size); + + memcpy(dst, actual_base, actual_len); + return actual_len; } uint8_t* MemoryBuffer::GetPtr(size_t offset) { @@ -129,145 +185,77 @@ bool MemoryFileAtOffset::Init(const std::string& file, uint64_t offset, uint64_t return true; } -bool MemoryFileAtOffset::Read(uint64_t addr, void* dst, size_t size) { - uint64_t max_size; - if (__builtin_add_overflow(addr, size, &max_size) || max_size > size_) { - return false; +size_t MemoryFileAtOffset::ReadPartially(uint64_t addr, void* dst, size_t size) { + if (addr >= size_) { + return 0; } - memcpy(dst, &data_[addr], size); - return true; + + size_t bytes_left = size_ - static_cast(addr); + const unsigned char* actual_base = static_cast(data_) + addr; + size_t actual_len = std::min(bytes_left, size); + + memcpy(dst, actual_base, actual_len); + return actual_len; } -bool MemoryRemote::PtraceRead(uint64_t addr, long* value) { -#if !defined(__LP64__) - // Cannot read an address greater than 32 bits. - if (addr > UINT32_MAX) { - return false; - } -#endif - // ptrace() returns -1 and sets errno when the operation fails. - // To disambiguate -1 from a valid result, we clear errno beforehand. - errno = 0; - *value = ptrace(PTRACE_PEEKTEXT, pid_, reinterpret_cast(addr), nullptr); - if (*value == -1 && errno) { - return false; - } - return true; +size_t MemoryRemote::ReadPartially(uint64_t addr, void* dst, size_t size) { + return ProcessVmRead(pid_, dst, addr, size); } -bool MemoryRemote::Read(uint64_t addr, void* dst, size_t bytes) { - // Make sure that there is no overflow. - uint64_t max_size; - if (__builtin_add_overflow(addr, bytes, &max_size)) { - return false; - } - - size_t bytes_read = 0; - long data; - size_t align_bytes = addr & (sizeof(long) - 1); - if (align_bytes != 0) { - if (!PtraceRead(addr & ~(sizeof(long) - 1), &data)) { - return false; - } - size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes); - memcpy(dst, reinterpret_cast(&data) + align_bytes, copy_bytes); - addr += copy_bytes; - dst = reinterpret_cast(reinterpret_cast(dst) + copy_bytes); - bytes -= copy_bytes; - bytes_read += copy_bytes; - } - - for (size_t i = 0; i < bytes / sizeof(long); i++) { - if (!PtraceRead(addr, &data)) { - return false; - } - memcpy(dst, &data, sizeof(long)); - dst = reinterpret_cast(reinterpret_cast(dst) + sizeof(long)); - addr += sizeof(long); - bytes_read += sizeof(long); - } - - size_t left_over = bytes & (sizeof(long) - 1); - if (left_over) { - if (!PtraceRead(addr, &data)) { - return false; - } - memcpy(dst, &data, left_over); - bytes_read += left_over; - } - return true; +size_t MemoryLocal::ReadPartially(uint64_t addr, void* dst, size_t size) { + return ProcessVmRead(getpid(), dst, addr, size); } -bool MemoryLocal::Read(uint64_t addr, void* dst, size_t size) { - // Make sure that there is no overflow. - uint64_t max_size; - if (__builtin_add_overflow(addr, size, &max_size)) { - return false; +MemoryRange::MemoryRange(const std::shared_ptr& memory, uint64_t begin, uint64_t length, + uint64_t offset) + : memory_(memory), begin_(begin), length_(length), offset_(offset) {} + +size_t MemoryRange::ReadPartially(uint64_t addr, void* dst, size_t size) { + if (addr < offset_) { + return 0; } - // The process_vm_readv call will not always work on remote - // processes, so only use it for reads from the current pid. - // Use this method to avoid crashes if an address is invalid since - // unwind data could try to access any part of the address space. - struct iovec local_io; - local_io.iov_base = dst; - local_io.iov_len = size; - - struct iovec remote_io; - remote_io.iov_base = reinterpret_cast(static_cast(addr)); - remote_io.iov_len = size; - - ssize_t bytes_read = process_vm_readv(getpid(), &local_io, 1, &remote_io, 1, 0); - if (bytes_read == -1) { - return false; + uint64_t read_offset = addr - offset_; + if (read_offset >= length_) { + return 0; } - return static_cast(bytes_read) == size; + + uint64_t read_length = std::min(static_cast(size), length_ - read_offset); + uint64_t read_addr; + if (__builtin_add_overflow(read_offset, begin_, &read_addr)) { + return 0; + } + + return memory_->ReadPartially(read_addr, dst, read_length); } bool MemoryOffline::Init(const std::string& file, uint64_t offset) { - if (!MemoryFileAtOffset::Init(file, offset)) { + auto memory_file = std::make_shared(); + if (!memory_file->Init(file, offset)) { return false; } + // The first uint64_t value is the start of memory. - if (!MemoryFileAtOffset::Read(0, &start_, sizeof(start_))) { + uint64_t start; + if (!memory_file->Read(0, &start, sizeof(start))) { return false; } - // Subtract the first 64 bit value from the total size. - size_ -= sizeof(start_); + + uint64_t size = memory_file->Size(); + if (__builtin_sub_overflow(size, sizeof(start), &size)) { + return false; + } + + memory_ = std::make_unique(memory_file, sizeof(start), size, start); return true; } -bool MemoryOffline::Read(uint64_t addr, void* dst, size_t size) { - uint64_t max_size; - if (__builtin_add_overflow(addr, size, &max_size)) { - return false; +size_t MemoryOffline::ReadPartially(uint64_t addr, void* dst, size_t size) { + if (!memory_) { + return 0; } - uint64_t real_size; - if (__builtin_add_overflow(start_, offset_, &real_size) || - __builtin_add_overflow(real_size, size_, &real_size)) { - return false; - } - - if (addr < start_ || max_size > real_size) { - return false; - } - memcpy(dst, &data_[addr + offset_ - start_ + sizeof(start_)], size); - return true; -} - -MemoryRange::MemoryRange(const std::shared_ptr& memory, uint64_t begin, uint64_t end) - : memory_(memory), begin_(begin), length_(end - begin) { - CHECK(end > begin); -} - -bool MemoryRange::Read(uint64_t addr, void* dst, size_t size) { - uint64_t max_read; - if (__builtin_add_overflow(addr, size, &max_read) || max_read > length_) { - return false; - } - // The check above guarantees that addr + begin_ will not overflow. - return memory_->Read(addr + begin_, dst, size); + return memory_->ReadPartially(addr, dst, size); } } // namespace unwindstack diff --git a/libunwindstack/include/unwindstack/Memory.h b/libunwindstack/include/unwindstack/Memory.h index 183b8993a..d747fa7ae 100644 --- a/libunwindstack/include/unwindstack/Memory.h +++ b/libunwindstack/include/unwindstack/Memory.h @@ -36,7 +36,9 @@ class Memory { virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX); - virtual bool Read(uint64_t addr, void* dst, size_t size) = 0; + virtual size_t ReadPartially(uint64_t addr, void* dst, size_t size) = 0; + + bool Read(uint64_t addr, void* dst, size_t size); inline bool ReadField(uint64_t addr, void* start, void* field, size_t size) { if (reinterpret_cast(field) < reinterpret_cast(start)) { @@ -60,7 +62,7 @@ class MemoryBuffer : public Memory { MemoryBuffer() = default; virtual ~MemoryBuffer() = default; - bool Read(uint64_t addr, void* dst, size_t size) override; + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; uint8_t* GetPtr(size_t offset); @@ -79,7 +81,9 @@ class MemoryFileAtOffset : public Memory { bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX); - bool Read(uint64_t addr, void* dst, size_t size) override; + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; + + size_t Size() { return size_; } void Clear(); @@ -89,31 +93,15 @@ class MemoryFileAtOffset : public Memory { uint8_t* data_ = nullptr; }; -class MemoryOffline : public MemoryFileAtOffset { - public: - MemoryOffline() = default; - virtual ~MemoryOffline() = default; - - bool Init(const std::string& file, uint64_t offset); - - bool Read(uint64_t addr, void* dst, size_t size) override; - - private: - uint64_t start_; -}; - class MemoryRemote : public Memory { public: MemoryRemote(pid_t pid) : pid_(pid) {} virtual ~MemoryRemote() = default; - bool Read(uint64_t addr, void* dst, size_t size) override; + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; pid_t pid() { return pid_; } - protected: - virtual bool PtraceRead(uint64_t addr, long* value); - private: pid_t pid_; }; @@ -123,20 +111,38 @@ class MemoryLocal : public Memory { MemoryLocal() = default; virtual ~MemoryLocal() = default; - bool Read(uint64_t addr, void* dst, size_t size) override; + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; }; +// MemoryRange maps one address range onto another. +// The range [src_begin, src_begin + length) in the underlying Memory is mapped onto offset, +// such that range.read(offset) is equivalent to underlying.read(src_begin). class MemoryRange : public Memory { public: - MemoryRange(const std::shared_ptr& memory, uint64_t begin, uint64_t end); + MemoryRange(const std::shared_ptr& memory, uint64_t begin, uint64_t length, + uint64_t offset); virtual ~MemoryRange() = default; - bool Read(uint64_t addr, void* dst, size_t size) override; + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; private: std::shared_ptr memory_; uint64_t begin_; uint64_t length_; + uint64_t offset_; +}; + +class MemoryOffline : public Memory { + public: + MemoryOffline() = default; + virtual ~MemoryOffline() = default; + + bool Init(const std::string& file, uint64_t offset); + + size_t ReadPartially(uint64_t addr, void* dst, size_t size) override; + + private: + std::unique_ptr memory_; }; } // namespace unwindstack diff --git a/libunwindstack/tests/MemoryBufferTest.cpp b/libunwindstack/tests/MemoryBufferTest.cpp index 50a8a1b6c..be5a52003 100644 --- a/libunwindstack/tests/MemoryBufferTest.cpp +++ b/libunwindstack/tests/MemoryBufferTest.cpp @@ -78,4 +78,24 @@ TEST_F(MemoryBufferTest, read_failure_overflow) { ASSERT_FALSE(memory_->Read(UINT64_MAX - 100, buffer.data(), 200)); } +TEST_F(MemoryBufferTest, ReadPartially) { + memory_->Resize(256); + ASSERT_EQ(256U, memory_->Size()); + ASSERT_TRUE(memory_->GetPtr(0) != nullptr); + ASSERT_TRUE(memory_->GetPtr(1) != nullptr); + ASSERT_TRUE(memory_->GetPtr(255) != nullptr); + ASSERT_TRUE(memory_->GetPtr(256) == nullptr); + + uint8_t* data = memory_->GetPtr(0); + for (size_t i = 0; i < memory_->Size(); i++) { + data[i] = i; + } + + std::vector buffer(memory_->Size()); + ASSERT_EQ(128U, memory_->ReadPartially(128, buffer.data(), buffer.size())); + for (size_t i = 0; i < 128; i++) { + ASSERT_EQ(128 + i, buffer[i]) << "Failed at byte " << i; + } +} + } // namespace unwindstack diff --git a/libunwindstack/tests/MemoryFake.cpp b/libunwindstack/tests/MemoryFake.cpp index 2026acc00..e340dba4d 100644 --- a/libunwindstack/tests/MemoryFake.cpp +++ b/libunwindstack/tests/MemoryFake.cpp @@ -35,16 +35,16 @@ void MemoryFake::SetMemory(uint64_t addr, const void* memory, size_t length) { } } -bool MemoryFake::Read(uint64_t addr, void* memory, size_t size) { +size_t MemoryFake::ReadPartially(uint64_t addr, void* memory, size_t size) { uint8_t* dst = reinterpret_cast(memory); for (size_t i = 0; i < size; i++, addr++) { auto value = data_.find(addr); if (value == data_.end()) { - return false; + return i; } dst[i] = value->second; } - return true; + return size; } } // namespace unwindstack diff --git a/libunwindstack/tests/MemoryFake.h b/libunwindstack/tests/MemoryFake.h index d37426155..2b0297cb4 100644 --- a/libunwindstack/tests/MemoryFake.h +++ b/libunwindstack/tests/MemoryFake.h @@ -32,7 +32,7 @@ class MemoryFake : public Memory { MemoryFake() = default; virtual ~MemoryFake() = default; - bool Read(uint64_t addr, void* buffer, size_t size) override; + size_t ReadPartially(uint64_t addr, void* buffer, size_t size) override; void SetMemory(uint64_t addr, const void* memory, size_t length); @@ -71,21 +71,9 @@ class MemoryFakeAlwaysReadZero : public Memory { MemoryFakeAlwaysReadZero() = default; virtual ~MemoryFakeAlwaysReadZero() = default; - bool Read(uint64_t, void* buffer, size_t size) override { + size_t ReadPartially(uint64_t, void* buffer, size_t size) override { memset(buffer, 0, size); - return true; - } -}; - -class MemoryFakeRemote : public MemoryRemote { - public: - MemoryFakeRemote() : MemoryRemote(0) {} - virtual ~MemoryFakeRemote() = default; - - protected: - bool PtraceRead(uint64_t, long* value) override { - *value = 0; - return true; + return size; } }; diff --git a/libunwindstack/tests/MemoryLocalTest.cpp b/libunwindstack/tests/MemoryLocalTest.cpp index 73eebdd8c..957b239a6 100644 --- a/libunwindstack/tests/MemoryLocalTest.cpp +++ b/libunwindstack/tests/MemoryLocalTest.cpp @@ -16,6 +16,7 @@ #include #include +#include #include @@ -67,4 +68,44 @@ TEST(MemoryLocalTest, read_overflow) { ASSERT_FALSE(local.Read(reinterpret_cast(&value), dst.data(), SIZE_MAX)); } +TEST(MemoryLocalTest, ReadPartially) { + char* mapping = static_cast( + mmap(nullptr, 2 * getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + + ASSERT_NE(MAP_FAILED, mapping); + + mprotect(mapping + getpagesize(), getpagesize(), PROT_NONE); + memset(mapping + getpagesize() - 1024, 0x4c, 1024); + + MemoryLocal local; + + std::vector dst(4096); + ASSERT_EQ(1024U, local.ReadPartially(reinterpret_cast(mapping + getpagesize() - 1024), + dst.data(), 4096)); + for (size_t i = 0; i < 1024; i++) { + ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i; + } + + ASSERT_EQ(0, munmap(mapping, 2 * getpagesize())); +} + +TEST(MemoryLocalTest, read_hole) { + void* mapping = + mmap(nullptr, 3 * 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(MAP_FAILED, mapping); + memset(mapping, 0xFF, 3 * 4096); + mprotect(static_cast(mapping) + 4096, 4096, PROT_NONE); + + MemoryLocal local; + std::vector dst(4096 * 3, 0xCC); + ASSERT_EQ(4096U, local.ReadPartially(reinterpret_cast(mapping), dst.data(), 4096 * 3)); + for (size_t i = 0; i < 4096; ++i) { + ASSERT_EQ(0xFF, dst[i]); + } + for (size_t i = 4096; i < 4096 * 3; ++i) { + ASSERT_EQ(0xCC, dst[i]); + } + ASSERT_EQ(0, munmap(mapping, 3 * 4096)); +} + } // namespace unwindstack diff --git a/libunwindstack/tests/MemoryRangeTest.cpp b/libunwindstack/tests/MemoryRangeTest.cpp index 680fae9c1..4a764f59f 100644 --- a/libunwindstack/tests/MemoryRangeTest.cpp +++ b/libunwindstack/tests/MemoryRangeTest.cpp @@ -35,7 +35,7 @@ TEST(MemoryRangeTest, read) { std::shared_ptr process_memory(memory_fake); memory_fake->SetMemory(9001, src); - MemoryRange range(process_memory, 9001, 9001 + src.size()); + MemoryRange range(process_memory, 9001, src.size(), 0); std::vector dst(1024); ASSERT_TRUE(range.Read(0, dst.data(), src.size())); @@ -51,7 +51,7 @@ TEST(MemoryRangeTest, read_near_limit) { std::shared_ptr process_memory(memory_fake); memory_fake->SetMemory(1000, src); - MemoryRange range(process_memory, 1000, 2024); + MemoryRange range(process_memory, 1000, 1024, 0); std::vector dst(1024); ASSERT_TRUE(range.Read(1020, dst.data(), 4)); @@ -72,8 +72,23 @@ TEST(MemoryRangeTest, read_overflow) { std::vector buffer(100); std::shared_ptr process_memory(new MemoryFakeAlwaysReadZero); - std::unique_ptr overflow(new MemoryRange(process_memory, 100, 200)); + std::unique_ptr overflow(new MemoryRange(process_memory, 100, 200, 0)); ASSERT_FALSE(overflow->Read(UINT64_MAX - 10, buffer.data(), 100)); } +TEST(MemoryRangeTest, ReadPartially) { + std::vector src(4096); + memset(src.data(), 0x4c, 4096); + MemoryFake* memory_fake = new MemoryFake; + std::shared_ptr process_memory(memory_fake); + memory_fake->SetMemory(1000, src); + + MemoryRange range(process_memory, 1000, 1024, 0); + std::vector dst(1024); + ASSERT_EQ(4U, range.ReadPartially(1020, dst.data(), 1024)); + for (size_t i = 0; i < 4; i++) { + ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i; + } +} + } // namespace unwindstack diff --git a/libunwindstack/tests/MemoryRemoteTest.cpp b/libunwindstack/tests/MemoryRemoteTest.cpp index a66d0c550..8ab806819 100644 --- a/libunwindstack/tests/MemoryRemoteTest.cpp +++ b/libunwindstack/tests/MemoryRemoteTest.cpp @@ -79,6 +79,38 @@ TEST_F(MemoryRemoteTest, read) { ASSERT_TRUE(Detach(pid)); } +TEST_F(MemoryRemoteTest, ReadPartially) { + char* mapping = static_cast( + mmap(nullptr, 2 * getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + + ASSERT_NE(MAP_FAILED, mapping); + + mprotect(mapping + getpagesize(), getpagesize(), PROT_NONE); + memset(mapping + getpagesize() - 1024, 0x4c, 1024); + + pid_t pid; + if ((pid = fork()) == 0) { + while (true); + exit(1); + } + ASSERT_LT(0, pid); + TestScopedPidReaper reap(pid); + + ASSERT_TRUE(Attach(pid)); + + MemoryRemote remote(pid); + + std::vector dst(4096); + ASSERT_EQ(1024U, remote.ReadPartially(reinterpret_cast(mapping + getpagesize() - 1024), + dst.data(), 4096)); + for (size_t i = 0; i < 1024; i++) { + ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i; + } + + ASSERT_TRUE(Detach(pid)); + ASSERT_EQ(0, munmap(mapping, 2 * getpagesize())); +} + TEST_F(MemoryRemoteTest, read_fail) { int pagesize = getpagesize(); void* src = mmap(nullptr, pagesize * 2, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,-1, 0); @@ -119,11 +151,23 @@ TEST_F(MemoryRemoteTest, read_fail) { } TEST_F(MemoryRemoteTest, read_overflow) { - MemoryFakeRemote remote; + pid_t pid; + if ((pid = fork()) == 0) { + while (true); + exit(1); + } + ASSERT_LT(0, pid); + TestScopedPidReaper reap(pid); + + ASSERT_TRUE(Attach(pid)); + + MemoryRemote remote(pid); // Check overflow condition is caught properly. std::vector dst(200); ASSERT_FALSE(remote.Read(UINT64_MAX - 100, dst.data(), 200)); + + ASSERT_TRUE(Detach(pid)); } TEST_F(MemoryRemoteTest, read_illegal) { @@ -146,4 +190,32 @@ TEST_F(MemoryRemoteTest, read_illegal) { ASSERT_TRUE(Detach(pid)); } +TEST_F(MemoryRemoteTest, read_hole) { + void* mapping = + mmap(nullptr, 3 * 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(MAP_FAILED, mapping); + memset(mapping, 0xFF, 3 * 4096); + mprotect(static_cast(mapping) + 4096, 4096, PROT_NONE); + + pid_t pid; + if ((pid = fork()) == 0) { + while (true); + exit(1); + } + ASSERT_LT(0, pid); + TestScopedPidReaper reap(pid); + + ASSERT_TRUE(Attach(pid)); + + MemoryRemote remote(pid); + std::vector dst(4096 * 3, 0xCC); + ASSERT_EQ(4096U, remote.ReadPartially(reinterpret_cast(mapping), dst.data(), 4096 * 3)); + for (size_t i = 0; i < 4096; ++i) { + ASSERT_EQ(0xFF, dst[i]); + } + for (size_t i = 4096; i < 4096 * 3; ++i) { + ASSERT_EQ(0xCC, dst[i]); + } +} + } // namespace unwindstack