2014-08-20 18:30:19 -04:00
|
|
|
|
// Copyright 2014 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 "client/capture_context_mac.h"
|
|
|
|
|
|
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "build/build_config.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
2016-11-22 14:27:51 -05:00
|
|
|
|
#include "util/misc/address_sanitizer.h"
|
2015-09-14 11:09:46 -07:00
|
|
|
|
#include "util/misc/implicit_cast.h"
|
2014-08-20 18:30:19 -04:00
|
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
2014-08-20 18:30:19 -04:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// If the context structure has fields that tell whether it’s valid, such as
|
|
|
|
|
// magic numbers or size fields, sanity-checks those fields for validity with
|
|
|
|
|
// fatal gtest assertions. For other fields, where it’s possible to reason about
|
|
|
|
|
// their validity based solely on their contents, sanity-checks via nonfatal
|
|
|
|
|
// gtest assertions.
|
2015-09-30 14:10:08 -04:00
|
|
|
|
void SanityCheckContext(const NativeCPUContext& context) {
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#if defined(ARCH_CPU_X86)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
ASSERT_EQ(x86_THREAD_STATE32, context.tsh.flavor);
|
|
|
|
|
ASSERT_EQ(implicit_cast<int>(x86_THREAD_STATE32_COUNT), context.tsh.count);
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#elif defined(ARCH_CPU_X86_64)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
ASSERT_EQ(x86_THREAD_STATE64, context.tsh.flavor);
|
|
|
|
|
ASSERT_EQ(implicit_cast<int>(x86_THREAD_STATE64_COUNT), context.tsh.count);
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(ARCH_CPU_X86_FAMILY)
|
|
|
|
|
// The segment registers are only capable of storing 16-bit quantities, but
|
|
|
|
|
// the context structure provides native integer-width fields for them. Ensure
|
|
|
|
|
// that the high bits are all clear.
|
|
|
|
|
//
|
|
|
|
|
// Many bit positions in the flags register are reserved and will always read
|
2015-09-30 14:10:08 -04:00
|
|
|
|
// a known value. Most reserved bits are always 0, but bit 1 is always 1.
|
|
|
|
|
// Check that the reserved bits are all set to their expected values. Note
|
|
|
|
|
// that the set of reserved bits may be relaxed over time with newer CPUs, and
|
|
|
|
|
// that this test may need to be changed to reflect these developments. The
|
|
|
|
|
// current set of reserved bits are 1, 3, 5, 15, and 22 and higher. See Intel
|
|
|
|
|
// Software Developer’s Manual, Volume 1: Basic Architecture (253665-051),
|
|
|
|
|
// 3.4.3 “EFLAGS Register”, and AMD Architecture Programmer’s Manual, Volume
|
|
|
|
|
// 2: System Programming (24593-3.24), 3.1.6 “RFLAGS Register”.
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#if defined(ARCH_CPU_X86)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__cs & ~0xffff);
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__ds & ~0xffff);
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__es & ~0xffff);
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__fs & ~0xffff);
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__gs & ~0xffff);
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts32.__ss & ~0xffff);
|
|
|
|
|
EXPECT_EQ(2u, context.uts.ts32.__eflags & 0xffc0802a);
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#elif defined(ARCH_CPU_X86_64)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
EXPECT_EQ(0u, context.uts.ts64.__cs & ~UINT64_C(0xffff));
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts64.__fs & ~UINT64_C(0xffff));
|
|
|
|
|
EXPECT_EQ(0u, context.uts.ts64.__gs & ~UINT64_C(0xffff));
|
|
|
|
|
EXPECT_EQ(2u, context.uts.ts64.__rflags & UINT64_C(0xffffffffffc0802a));
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A CPU-independent function to return the program counter.
|
2015-09-30 14:10:08 -04:00
|
|
|
|
uintptr_t ProgramCounterFromContext(const NativeCPUContext& context) {
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#if defined(ARCH_CPU_X86)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
return context.uts.ts32.__eip;
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#elif defined(ARCH_CPU_X86_64)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
return context.uts.ts64.__rip;
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A CPU-independent function to return the stack pointer.
|
2015-09-30 14:10:08 -04:00
|
|
|
|
uintptr_t StackPointerFromContext(const NativeCPUContext& context) {
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#if defined(ARCH_CPU_X86)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
return context.uts.ts32.__esp;
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#elif defined(ARCH_CPU_X86_64)
|
2015-09-30 14:10:08 -04:00
|
|
|
|
return context.uts.ts64.__rsp;
|
2014-08-20 18:30:19 -04:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestCaptureContext() {
|
|
|
|
|
NativeCPUContext context_1;
|
|
|
|
|
CaptureContext(&context_1);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
SCOPED_TRACE("context_1");
|
2015-09-30 14:10:08 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SanityCheckContext(context_1));
|
2014-08-20 18:30:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The program counter reference value is this function’s address. The
|
|
|
|
|
// captured program counter should be slightly greater than or equal to the
|
|
|
|
|
// reference program counter.
|
2015-09-30 14:10:08 -04:00
|
|
|
|
uintptr_t pc = ProgramCounterFromContext(context_1);
|
2016-11-22 14:27:51 -05:00
|
|
|
|
|
|
|
|
|
#if !defined(ADDRESS_SANITIZER)
|
2015-08-19 18:50:15 -04:00
|
|
|
|
// AddressSanitizer can cause enough code bloat that the “nearby” check would
|
|
|
|
|
// likely fail.
|
2014-08-20 18:30:19 -04:00
|
|
|
|
const uintptr_t kReferencePC =
|
|
|
|
|
reinterpret_cast<uintptr_t>(TestCaptureContext);
|
|
|
|
|
EXPECT_LT(pc - kReferencePC, 64u);
|
2016-11-22 14:27:51 -05:00
|
|
|
|
#endif // !defined(ADDRESS_SANITIZER)
|
2014-08-20 18:30:19 -04:00
|
|
|
|
|
|
|
|
|
// Declare sp and context_2 here because all local variables need to be
|
|
|
|
|
// declared before computing the stack pointer reference value, so that the
|
|
|
|
|
// reference value can be the lowest value possible.
|
|
|
|
|
uintptr_t sp;
|
|
|
|
|
NativeCPUContext context_2;
|
|
|
|
|
|
|
|
|
|
// The stack pointer reference value is the lowest address of a local variable
|
|
|
|
|
// in this function. The captured program counter will be slightly less than
|
|
|
|
|
// or equal to the reference stack pointer.
|
|
|
|
|
const uintptr_t kReferenceSP =
|
|
|
|
|
std::min(std::min(reinterpret_cast<uintptr_t>(&context_1),
|
|
|
|
|
reinterpret_cast<uintptr_t>(&context_2)),
|
|
|
|
|
std::min(reinterpret_cast<uintptr_t>(&pc),
|
|
|
|
|
reinterpret_cast<uintptr_t>(&sp)));
|
2015-09-30 14:10:08 -04:00
|
|
|
|
sp = StackPointerFromContext(context_1);
|
2014-08-20 18:30:19 -04:00
|
|
|
|
EXPECT_LT(kReferenceSP - sp, 512u);
|
|
|
|
|
|
|
|
|
|
// Capture the context again, expecting that the stack pointer stays the same
|
|
|
|
|
// and the program counter increases. Strictly speaking, there’s no guarantee
|
|
|
|
|
// that these conditions will hold, although they do for known compilers even
|
|
|
|
|
// under typical optimization.
|
|
|
|
|
CaptureContext(&context_2);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
SCOPED_TRACE("context_2");
|
2015-09-30 14:10:08 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SanityCheckContext(context_2));
|
2014-08-20 18:30:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 14:10:08 -04:00
|
|
|
|
EXPECT_EQ(sp, StackPointerFromContext(context_2));
|
|
|
|
|
EXPECT_GT(ProgramCounterFromContext(context_2), pc);
|
2014-08-20 18:30:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(CaptureContextMac, CaptureContext) {
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(TestCaptureContext());
|
2014-08-20 18:30:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|