linux: Add ProcessReader

ProcessReader is responsible for collecting information needed to build
a snapshot of the target process, independent of the Snapshot
interface. This CL includes implementation and tests for collecting
thread information, but does not yet collect module information.

Bug: crashpad:30
Change-Id: I911f155c953129a5fa8c031e923c0de2bd740ce0
Reviewed-on: https://chromium-review.googlesource.com/488162
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Joshua Peraza 2017-07-19 10:56:20 -07:00
parent 5536baff13
commit 6b5f139d88
6 changed files with 866 additions and 0 deletions

View File

@ -0,0 +1,25 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// 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.
#ifndef CRASHPAD_COMPAT_ANDROID_SYS_SYSCALL_H_
#define CRASHPAD_COMPAT_ANDROID_SYS_SYSCALL_H_
#include_next <sys/syscall.h>
// Android 5.0.0 (API 21) NDK
#if !defined(SYS_gettid)
#define SYS_gettid __NR_gettid
#endif
#endif // CRASHPAD_COMPAT_ANDROID_SYS_SYSCALL_H_

View File

@ -0,0 +1,281 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// 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 "snapshot/linux/process_reader.h"
#include <dirent.h>
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <algorithm>
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "util/posix/scoped_dir.h"
namespace crashpad {
namespace {
bool ShouldMergeStackMappings(const MemoryMap::Mapping& stack_mapping,
const MemoryMap::Mapping& adj_mapping) {
DCHECK(stack_mapping.readable);
return adj_mapping.readable && stack_mapping.device == adj_mapping.device &&
stack_mapping.inode == adj_mapping.inode;
}
} // namespace
ProcessReader::Thread::Thread()
: thread_context(),
float_context(),
thread_specific_data_address(0),
stack_region_address(0),
stack_region_size(0),
tid(-1),
static_priority(-1),
nice_value(-1) {}
ProcessReader::Thread::~Thread() {}
bool ProcessReader::Thread::InitializePtrace() {
ThreadInfo thread_info;
if (!thread_info.Initialize(tid)) {
return false;
}
thread_info.GetGeneralPurposeRegisters(&thread_context);
if (!thread_info.GetFloatingPointRegisters(&float_context)) {
return false;
}
if (!thread_info.GetThreadArea(&thread_specific_data_address)) {
return false;
}
// TODO(jperaza): Starting with Linux 3.14, scheduling policy, static
// priority, and nice value can be collected all in one call with
// sched_getattr().
int res = sched_getscheduler(tid);
if (res < 0) {
PLOG(ERROR) << "sched_getscheduler";
return false;
}
sched_policy = res;
sched_param param;
if (sched_getparam(tid, &param) != 0) {
PLOG(ERROR) << "sched_getparam";
return false;
}
static_priority = param.sched_priority;
errno = 0;
res = getpriority(PRIO_PROCESS, tid);
if (res == -1 && errno) {
PLOG(ERROR) << "getpriority";
return false;
}
nice_value = res;
return true;
}
void ProcessReader::Thread::InitializeStack(ProcessReader* reader) {
LinuxVMAddress stack_pointer;
#if defined(ARCH_CPU_X86_FAMILY)
stack_pointer =
reader->Is64Bit() ? thread_context.t64.rsp : thread_context.t32.esp;
#elif defined(ARCH_CPU_ARM_FAMILY)
stack_pointer =
reader->Is64Bit() ? thread_context.t64.sp : thread_context.t32.sp;
#else
#error Port.
#endif
const MemoryMap* memory_map = reader->GetMemoryMap();
// If we can't find the mapping, it's probably a bad stack pointer
const MemoryMap::Mapping* mapping = memory_map->FindMapping(stack_pointer);
if (!mapping) {
LOG(WARNING) << "no stack mapping";
return;
}
LinuxVMAddress stack_region_start = stack_pointer;
// We've hit what looks like a guard page; skip to the end and check for a
// mapped stack region.
if (!mapping->readable) {
stack_region_start = mapping->range.End();
mapping = memory_map->FindMapping(stack_region_start);
if (!mapping) {
LOG(WARNING) << "no stack mapping";
return;
}
} else {
#if defined(ARCH_CPU_X86_FAMILY)
// Adjust start address to include the red zone
if (reader->Is64Bit()) {
const LinuxVMSize kRedZoneSize = 128;
LinuxVMAddress red_zone_base =
stack_region_start - std::min(kRedZoneSize, stack_region_start);
// Only include the red zone if it is part of a valid mapping
if (red_zone_base >= mapping->range.Base()) {
stack_region_start = red_zone_base;
} else {
const MemoryMap::Mapping* rz_mapping =
memory_map->FindMapping(red_zone_base);
if (rz_mapping && ShouldMergeStackMappings(*mapping, *rz_mapping)) {
stack_region_start = red_zone_base;
} else {
stack_region_start = mapping->range.Base();
}
}
}
#endif
}
stack_region_address = stack_region_start;
// If there are more mappings at the end of this one, they may be a
// continuation of the stack.
LinuxVMAddress stack_end = mapping->range.End();
const MemoryMap::Mapping* next_mapping;
while ((next_mapping = memory_map->FindMapping(stack_end)) &&
ShouldMergeStackMappings(*mapping, *next_mapping)) {
stack_end = next_mapping->range.End();
}
// The main thread should have an entry in the maps file just for its stack,
// so we'll assume the base of the stack is at the end of the region. Other
// threads' stacks may not have their own entries in the maps file if they
// were user-allocated within a larger mapping, but pthreads places the TLS
// at the high-address end of the stack so we can try using that to shrink
// the stack region.
stack_region_size = stack_end - stack_region_address;
if (tid != reader->ProcessID() &&
thread_specific_data_address > stack_region_address &&
thread_specific_data_address < stack_end) {
stack_region_size = thread_specific_data_address - stack_region_address;
}
}
ProcessReader::ProcessReader()
: process_info_(),
memory_map_(),
threads_(),
process_memory_(),
is_64_bit_(false),
initialized_threads_(false),
initialized_() {}
ProcessReader::~ProcessReader() {}
bool ProcessReader::Initialize(pid_t pid) {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
if (!process_info_.Initialize(pid)) {
return false;
}
if (!memory_map_.Initialize(pid)) {
return false;
}
process_memory_.reset(new ProcessMemory());
if (!process_memory_->Initialize(pid)) {
return false;
}
if (!process_info_.Is64Bit(&is_64_bit_)) {
return false;
}
INITIALIZATION_STATE_SET_VALID(initialized_);
return true;
}
const std::vector<ProcessReader::Thread>& ProcessReader::Threads() {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
if (!initialized_threads_) {
InitializeThreads();
}
return threads_;
}
void ProcessReader::InitializeThreads() {
DCHECK(threads_.empty());
pid_t pid = ProcessID();
if (pid == getpid()) {
// TODO(jperaza): ptrace can't be used on threads in the same thread group.
// Using clone to create a new thread in it's own thread group doesn't work
// because glibc doesn't support threads it didn't create via pthreads.
// Fork a new process to snapshot us and copy the data back?
LOG(ERROR) << "not implemented";
return;
}
char path[32];
snprintf(path, arraysize(path), "/proc/%d/task", pid);
DIR* dir = opendir(path);
if (!dir) {
PLOG(ERROR) << "opendir";
return;
}
ScopedDIR scoped_dir(dir);
Thread main_thread;
main_thread.tid = pid;
if (main_thread.InitializePtrace()) {
main_thread.InitializeStack(this);
threads_.push_back(main_thread);
} else {
LOG(WARNING) << "Couldn't initialize main thread.";
}
bool main_thread_found = false;
dirent* dir_entry;
while ((dir_entry = readdir(scoped_dir.get()))) {
if (strncmp(dir_entry->d_name, ".", arraysize(dir_entry->d_name)) == 0 ||
strncmp(dir_entry->d_name, "..", arraysize(dir_entry->d_name)) == 0) {
continue;
}
pid_t tid;
if (!base::StringToInt(dir_entry->d_name, &tid)) {
LOG(ERROR) << "format error";
continue;
}
if (tid == pid) {
DCHECK(!main_thread_found);
main_thread_found = true;
continue;
}
Thread thread;
thread.tid = tid;
if (thread.InitializePtrace()) {
thread.InitializeStack(this);
threads_.push_back(thread);
}
}
DCHECK(main_thread_found);
}
} // namespace crashpad

View File

@ -0,0 +1,107 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// 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.
#ifndef CRASHPAD_SNAPSHOT_LINUX_PROCESS_READER_H_
#define CRASHPAD_SNAPSHOT_LINUX_PROCESS_READER_H_
#include <sys/types.h>
#include <memory>
#include <vector>
#include "base/macros.h"
#include "util/linux/address_types.h"
#include "util/linux/memory_map.h"
#include "util/linux/process_memory.h"
#include "util/linux/thread_info.h"
#include "util/posix/process_info.h"
#include "util/misc/initialization_state_dcheck.h"
namespace crashpad {
//! \brief Accesses information about another process, identified by a process
//! ID.
class ProcessReader {
public:
//! \brief Contains information about a thread that belongs to a process.
struct Thread {
Thread();
~Thread();
ThreadContext thread_context;
FloatContext float_context;
LinuxVMAddress thread_specific_data_address;
LinuxVMAddress stack_region_address;
LinuxVMSize stack_region_size;
pid_t tid;
int sched_policy;
int static_priority;
int nice_value;
private:
friend class ProcessReader;
bool InitializePtrace();
void InitializeStack(ProcessReader* reader);
};
ProcessReader();
~ProcessReader();
//! \brief Initializes this object.
//!
//! This method must be successfully called before calling any other method in
//! this class.
//!
//! \param[in] pid The process ID of the target process.
//! \return `true` on success. `false` on failure with a message logged.
bool Initialize(pid_t pid);
//! \brief Return `true` if the target task is a 64-bit process.
bool Is64Bit() const { return is_64_bit_; }
//! \brief Return the target process' process ID.
pid_t ProcessID() const { return process_info_.ProcessID(); }
//! \brief Return the target process' parent process ID.
pid_t ParentProcessID() const { return process_info_.ParentProcessID(); }
//! \brief Return a memory reader for the target process.
ProcessMemory* Memory() { return process_memory_.get(); }
//! \brief Return a memory map of the target process.
MemoryMap* GetMemoryMap() { return &memory_map_; }
//! \brief Return a vector of threads that are in the task process. If the
//! main thread is able to be identified and traced, it will be placed at
//! index `0`.
const std::vector<Thread>& Threads();
private:
void InitializeThreads();
ProcessInfo process_info_;
class MemoryMap memory_map_;
std::vector<Thread> threads_;
std::unique_ptr<ProcessMemory> process_memory_;
bool is_64_bit_;
bool initialized_threads_;
InitializationStateDcheck initialized_;
DISALLOW_COPY_AND_ASSIGN(ProcessReader);
};
} // namespace crashpad
#endif // CRASHPAD_SNAPSHOT_LINUX_PROCESS_READER_H_

View File

@ -0,0 +1,450 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// 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 "snapshot/linux/process_reader.h"
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <map>
#include <string>
#include "base/format_macros.h"
#include "base/memory/free_deleter.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "test/errors.h"
#include "test/multiprocess.h"
#include "util/file/file_io.h"
#include "util/misc/from_pointer_cast.h"
#include "util/stdlib/pointer_container.h"
#include "util/synchronization/semaphore.h"
namespace crashpad {
namespace test {
namespace {
pid_t gettid() {
return syscall(SYS_gettid);
}
LinuxVMAddress GetTLS() {
LinuxVMAddress tls;
#if defined(ARCH_CPU_ARMEL)
// 0xffff0fe0 is the address of the kernel user helper __kuser_get_tls().
auto kuser_get_tls = reinterpret_cast<void* (*)()>(0xffff0fe0);
tls = FromPointerCast<LinuxVMAddress>(kuser_get_tls());
#elif defined(ARCH_CPU_ARM64)
// Linux/aarch64 places the tls address in system register tpidr_el0.
asm("mrs %0, tpidr_el0" : "=r"(tls));
#elif defined(ARCH_CPU_X86)
uint32_t tls_32;
asm("movl %%gs:0x0, %0" : "=r"(tls_32));
tls = tls_32;
#elif defined(ARCH_CPU_X86_64)
asm("movq %%fs:0x0, %0" : "=r"(tls));
#else
#error Port.
#endif // ARCH_CPU_ARMEL
return tls;
}
TEST(ProcessReader, SelfBasic) {
ProcessReader process_reader;
ASSERT_TRUE(process_reader.Initialize(getpid()));
#if !defined(ARCH_CPU_64_BITS)
EXPECT_FALSE(process_reader.Is64Bit());
#else
EXPECT_TRUE(process_reader.Is64Bit());
#endif
EXPECT_EQ(process_reader.ProcessID(), getpid());
EXPECT_EQ(process_reader.ParentProcessID(), getppid());
const char kTestMemory[] = "Some test memory";
char buffer[arraysize(kTestMemory)];
ASSERT_TRUE(process_reader.Memory()->Read(
reinterpret_cast<LinuxVMAddress>(kTestMemory),
sizeof(kTestMemory),
&buffer));
EXPECT_STREQ(kTestMemory, buffer);
}
const char kTestMemory[] = "Read me from another process";
class BasicChildTest : public Multiprocess {
public:
BasicChildTest() : Multiprocess() {}
~BasicChildTest() {}
private:
void MultiprocessParent() override {
ProcessReader process_reader;
ASSERT_TRUE(process_reader.Initialize(ChildPID()));
#if !defined(ARCH_CPU_64_BITS)
EXPECT_FALSE(process_reader.Is64Bit());
#else
EXPECT_TRUE(process_reader.Is64Bit());
#endif
EXPECT_EQ(process_reader.ParentProcessID(), getpid());
EXPECT_EQ(process_reader.ProcessID(), ChildPID());
std::string read_string;
ASSERT_TRUE(process_reader.Memory()->ReadCString(
reinterpret_cast<LinuxVMAddress>(kTestMemory), &read_string));
EXPECT_EQ(read_string, kTestMemory);
}
void MultiprocessChild() override { CheckedReadFileAtEOF(ReadPipeHandle()); }
DISALLOW_COPY_AND_ASSIGN(BasicChildTest);
};
TEST(ProcessReader, ChildBasic) {
BasicChildTest test;
test.Run();
}
class TestThreadPool {
public:
struct ThreadExpectation {
LinuxVMAddress tls = 0;
LinuxVMAddress stack_address = 0;
LinuxVMSize max_stack_size = 0;
int sched_policy = 0;
int static_priority = 0;
int nice_value = 0;
};
TestThreadPool() : threads_() {}
~TestThreadPool() {
for (Thread* thread : threads_) {
thread->exit_semaphore.Signal();
}
for (const Thread* thread : threads_) {
EXPECT_EQ(pthread_join(thread->pthread, nullptr), 0)
<< ErrnoMessage("pthread_join");
}
}
void StartThreads(size_t thread_count, size_t stack_size = 0) {
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
Thread* thread = new Thread();
threads_.push_back(thread);
pthread_attr_t attr;
ASSERT_EQ(pthread_attr_init(&attr), 0)
<< ErrnoMessage("pthread_attr_init");
if (stack_size > 0) {
void* stack_ptr;
errno = posix_memalign(&stack_ptr, getpagesize(), stack_size);
ASSERT_EQ(errno, 0) << ErrnoMessage("posix_memalign");
thread->stack.reset(reinterpret_cast<char*>(stack_ptr));
ASSERT_EQ(pthread_attr_setstack(&attr, thread->stack.get(), stack_size),
0)
<< ErrnoMessage("pthread_attr_setstack");
thread->expectation.max_stack_size = stack_size;
}
ASSERT_EQ(pthread_attr_setschedpolicy(&attr, SCHED_OTHER), 0)
<< ErrnoMessage("pthread_attr_setschedpolicy");
thread->expectation.sched_policy = SCHED_OTHER;
sched_param param;
param.sched_priority = 0;
ASSERT_EQ(pthread_attr_setschedparam(&attr, &param), 0)
<< ErrnoMessage("pthread_attr_setschedparam");
thread->expectation.static_priority = 0;
thread->expectation.nice_value = thread_index % 20;
ASSERT_EQ(pthread_create(&thread->pthread, &attr, ThreadMain, thread), 0)
<< ErrnoMessage("pthread_create");
}
for (Thread* thread : threads_) {
thread->ready_semaphore.Wait();
}
}
pid_t GetThreadExpectation(size_t thread_index,
ThreadExpectation* expectation) {
CHECK_LT(thread_index, threads_.size());
const Thread* thread = threads_[thread_index];
*expectation = thread->expectation;
return thread->tid;
}
private:
struct Thread {
Thread()
: pthread(),
expectation(),
ready_semaphore(0),
exit_semaphore(0),
tid(-1) {}
~Thread() {}
pthread_t pthread;
ThreadExpectation expectation;
std::unique_ptr<char[], base::FreeDeleter> stack;
Semaphore ready_semaphore;
Semaphore exit_semaphore;
pid_t tid;
};
static void* ThreadMain(void* argument) {
Thread* thread = static_cast<Thread*>(argument);
CHECK_EQ(setpriority(PRIO_PROCESS, 0, thread->expectation.nice_value), 0)
<< ErrnoMessage("setpriority");
thread->expectation.tls = GetTLS();
thread->expectation.stack_address =
reinterpret_cast<LinuxVMAddress>(&thread);
thread->tid = gettid();
thread->ready_semaphore.Signal();
thread->exit_semaphore.Wait();
CHECK_EQ(pthread_self(), thread->pthread);
return nullptr;
}
PointerVector<Thread> threads_;
DISALLOW_COPY_AND_ASSIGN(TestThreadPool);
};
using ThreadMap = std::map<pid_t, TestThreadPool::ThreadExpectation>;
void ExpectThreads(const ThreadMap& thread_map,
const std::vector<ProcessReader::Thread>& threads,
const pid_t pid) {
ASSERT_EQ(threads.size(), thread_map.size());
MemoryMap memory_map;
ASSERT_TRUE(memory_map.Initialize(pid));
for (const auto& thread : threads) {
SCOPED_TRACE(base::StringPrintf("Thread id %d, tls 0x%" PRIx64
", stack addr 0x%" PRIx64
", stack size 0x%" PRIx64,
thread.tid,
thread.thread_specific_data_address,
thread.stack_region_address,
thread.stack_region_size));
const auto& iterator = thread_map.find(thread.tid);
ASSERT_NE(iterator, thread_map.end());
EXPECT_EQ(thread.thread_specific_data_address, iterator->second.tls);
ASSERT_TRUE(memory_map.FindMapping(thread.stack_region_address));
EXPECT_LE(thread.stack_region_address, iterator->second.stack_address);
ASSERT_TRUE(memory_map.FindMapping(thread.stack_region_address +
thread.stack_region_size - 1));
EXPECT_GE(thread.stack_region_address + thread.stack_region_size,
iterator->second.stack_address);
if (iterator->second.max_stack_size) {
EXPECT_LT(thread.stack_region_size, iterator->second.max_stack_size);
}
EXPECT_EQ(thread.sched_policy, iterator->second.sched_policy);
EXPECT_EQ(thread.static_priority, iterator->second.static_priority);
EXPECT_EQ(thread.nice_value, iterator->second.nice_value);
}
}
class ChildThreadTest : public Multiprocess {
public:
ChildThreadTest(size_t stack_size = 0)
: Multiprocess(), stack_size_(stack_size) {}
~ChildThreadTest() {}
private:
void MultiprocessParent() override {
ThreadMap thread_map;
for (size_t thread_index = 0; thread_index < kThreadCount + 1;
++thread_index) {
pid_t tid;
TestThreadPool::ThreadExpectation expectation;
CheckedReadFileExactly(ReadPipeHandle(), &tid, sizeof(tid));
CheckedReadFileExactly(
ReadPipeHandle(), &expectation, sizeof(expectation));
thread_map[tid] = expectation;
}
ProcessReader process_reader;
ASSERT_TRUE(process_reader.Initialize(ChildPID()));
const std::vector<ProcessReader::Thread>& threads =
process_reader.Threads();
ExpectThreads(thread_map, threads, ChildPID());
}
void MultiprocessChild() override {
TestThreadPool thread_pool;
thread_pool.StartThreads(kThreadCount, stack_size_);
TestThreadPool::ThreadExpectation expectation;
expectation.tls = GetTLS();
expectation.stack_address = reinterpret_cast<LinuxVMAddress>(&thread_pool);
int res = sched_getscheduler(0);
ASSERT_GE(res, 0) << ErrnoMessage("sched_getscheduler");
expectation.sched_policy = res;
sched_param param;
ASSERT_EQ(sched_getparam(0, &param), 0) << ErrnoMessage("sched_getparam");
expectation.static_priority = param.sched_priority;
errno = 0;
res = getpriority(PRIO_PROCESS, 0);
ASSERT_FALSE(res == -1 && errno) << ErrnoMessage("getpriority");
expectation.nice_value = res;
pid_t tid = gettid();
CheckedWriteFile(WritePipeHandle(), &tid, sizeof(tid));
CheckedWriteFile(WritePipeHandle(), &expectation, sizeof(expectation));
for (size_t thread_index = 0; thread_index < kThreadCount; ++thread_index) {
tid = thread_pool.GetThreadExpectation(thread_index, &expectation);
CheckedWriteFile(WritePipeHandle(), &tid, sizeof(tid));
CheckedWriteFile(WritePipeHandle(), &expectation, sizeof(expectation));
}
CheckedReadFileAtEOF(ReadPipeHandle());
}
static constexpr size_t kThreadCount = 3;
const size_t stack_size_;
DISALLOW_COPY_AND_ASSIGN(ChildThreadTest);
};
TEST(ProcessReader, ChildWithThreads) {
ChildThreadTest test;
test.Run();
}
TEST(ProcessReader, ChildThreadsWithSmallUserStacks) {
ChildThreadTest test(PTHREAD_STACK_MIN);
test.Run();
}
// Tests a thread with a stack that spans multiple mappings.
class ChildWithSplitStackTest : public Multiprocess {
public:
ChildWithSplitStackTest() : Multiprocess(), page_size_(getpagesize()) {}
~ChildWithSplitStackTest() {}
private:
void MultiprocessParent() override {
LinuxVMAddress stack_addr1;
LinuxVMAddress stack_addr2;
LinuxVMAddress stack_addr3;
CheckedReadFileExactly(ReadPipeHandle(), &stack_addr1, sizeof(stack_addr1));
CheckedReadFileExactly(ReadPipeHandle(), &stack_addr2, sizeof(stack_addr2));
CheckedReadFileExactly(ReadPipeHandle(), &stack_addr3, sizeof(stack_addr3));
ProcessReader process_reader;
ASSERT_TRUE(process_reader.Initialize(ChildPID()));
const std::vector<ProcessReader::Thread>& threads =
process_reader.Threads();
ASSERT_EQ(threads.size(), 1u);
LinuxVMAddress thread_stack_start = threads[0].stack_region_address;
EXPECT_LE(thread_stack_start, stack_addr1);
EXPECT_LE(thread_stack_start, stack_addr2);
EXPECT_LE(thread_stack_start, stack_addr3);
LinuxVMAddress thread_stack_end =
thread_stack_start + threads[0].stack_region_size;
EXPECT_GE(thread_stack_end, stack_addr1);
EXPECT_GE(thread_stack_end, stack_addr2);
EXPECT_GE(thread_stack_end, stack_addr3);
}
void MultiprocessChild() override {
const LinuxVMSize stack_size = page_size_ * 3;
GrowStack(stack_size, reinterpret_cast<LinuxVMAddress>(&stack_size));
}
void GrowStack(LinuxVMSize stack_size, LinuxVMAddress bottom_of_stack) {
char stack_contents[4096];
auto stack_address = reinterpret_cast<LinuxVMAddress>(&stack_contents);
if (bottom_of_stack - stack_address < stack_size) {
GrowStack(stack_size, bottom_of_stack);
} else {
// Write-protect a page on our stack to split up the mapping
LinuxVMAddress page_addr =
stack_address - (stack_address % page_size_) + page_size_;
ASSERT_EQ(
mprotect(reinterpret_cast<void*>(page_addr), page_size_, PROT_READ),
0)
<< ErrnoMessage("mprotect");
CheckedWriteFile(
WritePipeHandle(), &bottom_of_stack, sizeof(bottom_of_stack));
CheckedWriteFile(WritePipeHandle(), &page_addr, sizeof(page_addr));
CheckedWriteFile(
WritePipeHandle(), &stack_address, sizeof(stack_address));
// Wait for parent to read us
CheckedReadFileAtEOF(ReadPipeHandle());
ASSERT_EQ(mprotect(reinterpret_cast<void*>(page_addr),
page_size_,
PROT_READ | PROT_WRITE),
0)
<< ErrnoMessage("mprotect");
}
}
const size_t page_size_;
DISALLOW_COPY_AND_ASSIGN(ChildWithSplitStackTest);
};
TEST(ProcessReader, ChildWithSplitStack) {
ChildWithSplitStackTest test;
test.Run();
}
} // namespace
} // namespace test
} // namespace crashpad

View File

@ -48,6 +48,8 @@
'linux/elf_image_reader.h',
'linux/elf_symbol_table_reader.cc',
'linux/elf_symbol_table_reader.h',
'linux/process_reader.cc',
'linux/process_reader.h',
'mac/cpu_context_mac.cc',
'mac/cpu_context_mac.h',
'mac/exception_snapshot_mac.cc',

View File

@ -72,6 +72,7 @@
'api/module_annotations_win_test.cc',
'linux/debug_rendezvous_test.cc',
'linux/elf_image_reader_test.cc',
'linux/process_reader_test.cc',
'mac/cpu_context_mac_test.cc',
'mac/mach_o_image_annotations_reader_test.cc',
'mac/mach_o_image_reader_test.cc',