crashpad/util/linux/ptracer_test.cc
Joshua Peraza 59c5d848e5 linux: Refactor ptrace usage.
1) Add PtraceConnection which serves as the base class for specific
types of connections Crashpad uses to trace processes.
2) Add DirectPtraceConnection which is used when the handler process
has `ptrace` capabilities for the target process.
3) Move `ptrace` logic into Ptracer. This class isolates `ptrace` call
logic for use by various PtraceConnection implementations.

Bug: crashpad:30
Change-Id: I98083134a9f7d9f085e4cc816d2b85ffd6d73162
Reviewed-on: https://chromium-review.googlesource.com/671659
Commit-Queue: Joshua Peraza <jperaza@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Reviewed-by: Leonard Mosescu <mosescu@chromium.org>
2017-09-22 16:25:32 +00:00

99 lines
2.9 KiB
C++

// 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 "util/linux/ptracer.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "test/multiprocess.h"
#include "util/file/file_io.h"
#include "util/linux/scoped_ptrace_attach.h"
#include "util/misc/from_pointer_cast.h"
namespace crashpad {
namespace test {
namespace {
class SameBitnessTest : public Multiprocess {
public:
SameBitnessTest() : Multiprocess() {}
~SameBitnessTest() {}
private:
void MultiprocessParent() override {
LinuxVMAddress expected_tls;
CheckedReadFileExactly(
ReadPipeHandle(), &expected_tls, sizeof(expected_tls));
#if defined(ARCH_CPU_64_BITS)
constexpr bool am_64_bit = true;
#else
constexpr bool am_64_bit = false;
#endif // ARCH_CPU_64_BITS
ScopedPtraceAttach attach;
ASSERT_TRUE(attach.ResetAttach(ChildPID()));
Ptracer ptracer(am_64_bit);
EXPECT_EQ(ptracer.Is64Bit(), am_64_bit);
ThreadInfo thread_info;
ASSERT_TRUE(ptracer.GetThreadInfo(ChildPID(), &thread_info));
#if defined(ARCH_CPU_X86_64)
EXPECT_EQ(thread_info.thread_context.t64.fs_base, expected_tls);
#endif // ARCH_CPU_X86_64
EXPECT_EQ(thread_info.thread_specific_data_address, expected_tls);
}
void MultiprocessChild() override {
LinuxVMAddress expected_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);
expected_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"(expected_tls));
#elif defined(ARCH_CPU_X86)
uint32_t expected_tls_32;
asm("movl %%gs:0x0, %0" : "=r"(expected_tls_32));
expected_tls = expected_tls_32;
#elif defined(ARCH_CPU_X86_64)
asm("movq %%fs:0x0, %0" : "=r"(expected_tls));
#else
#error Port.
#endif // ARCH_CPU_ARMEL
CheckedWriteFile(WritePipeHandle(), &expected_tls, sizeof(expected_tls));
CheckedReadFileAtEOF(ReadPipeHandle());
}
DISALLOW_COPY_AND_ASSIGN(SameBitnessTest);
};
TEST(Ptracer, SameBitness) {
SameBitnessTest test;
test.Run();
}
// TODO(jperaza): Test against a process with different bitness.
} // namespace
} // namespace test
} // namespace crashpad