OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/win/capture_context.h" |
| 16 |
| 17 #include <stdint.h> |
| 18 |
| 19 #include <algorithm> |
| 20 |
| 21 #include "base/basictypes.h" |
| 22 #include "build/build_config.h" |
| 23 #include "gtest/gtest.h" |
| 24 |
| 25 namespace crashpad { |
| 26 namespace test { |
| 27 namespace { |
| 28 |
| 29 // If the context structure has fields that tell whether it’s valid, such as |
| 30 // magic numbers or size fields, sanity-checks those fields for validity with |
| 31 // fatal gtest assertions. For other fields, where it’s possible to reason about |
| 32 // their validity based solely on their contents, sanity-checks via nonfatal |
| 33 // gtest assertions. |
| 34 void SanityCheckContext(const CONTEXT& context) { |
| 35 #if defined(ARCH_CPU_X86) |
| 36 const uint32_t must_have = CONTEXT_i386 | |
| 37 CONTEXT_CONTROL | |
| 38 CONTEXT_INTEGER | |
| 39 CONTEXT_SEGMENTS | |
| 40 CONTEXT_FLOATING_POINT; |
| 41 ASSERT_EQ(must_have, context.ContextFlags & must_have); |
| 42 const uint32_t may_have = CONTEXT_EXTENDED_REGISTERS; |
| 43 ASSERT_EQ(0, context.ContextFlags & ~(must_have | may_have)); |
| 44 #elif defined(ARCH_CPU_X86_64) |
| 45 ASSERT_EQ(CONTEXT_AMD64 | |
| 46 CONTEXT_CONTROL | |
| 47 CONTEXT_INTEGER | |
| 48 CONTEXT_SEGMENTS | |
| 49 CONTEXT_FLOATING_POINT, |
| 50 context.ContextFlags); |
| 51 #endif |
| 52 |
| 53 #if defined(ARCH_CPU_X86_FAMILY) |
| 54 // Many bit positions in the flags register are reserved and will always read |
| 55 // a known value. Most reserved bits are always 0, but bit 1 is always 1. |
| 56 // Check that the reserved bits are all set to their expected values. Note |
| 57 // that the set of reserved bits may be relaxed over time with newer CPUs, and |
| 58 // that this test may need to be changed to reflect these developments. The |
| 59 // current set of reserved bits are 1, 3, 5, 15, and 22 and higher. See Intel |
| 60 // Software Developer’s Manual, Volume 1: Basic Architecture (253665-055), |
| 61 // 3.4.3 “EFLAGS Register”, and AMD Architecture Programmer’s Manual, Volume |
| 62 // 2: System Programming (24593-3.25), 3.1.6 “RFLAGS Register”. |
| 63 EXPECT_EQ(2u, context.EFlags & 0xffc0802a); |
| 64 |
| 65 // CaptureContext() doesn’t capture debug registers, so make sure they read 0. |
| 66 EXPECT_EQ(0, context.Dr0); |
| 67 EXPECT_EQ(0, context.Dr1); |
| 68 EXPECT_EQ(0, context.Dr2); |
| 69 EXPECT_EQ(0, context.Dr3); |
| 70 EXPECT_EQ(0, context.Dr6); |
| 71 EXPECT_EQ(0, context.Dr7); |
| 72 #endif |
| 73 |
| 74 #if defined(ARCH_CPU_X86) |
| 75 // fxsave doesn’t write these bytes. |
| 76 for (size_t i = 464; i < arraysize(context.ExtendedRegisters); ++i) { |
| 77 SCOPED_TRACE(i); |
| 78 EXPECT_EQ(0, context.ExtendedRegisters[i]); |
| 79 } |
| 80 #elif defined(ARCH_CPU_X86_64) |
| 81 // mxcsr shows up twice in the context structure. Make sure the values are |
| 82 // identical. |
| 83 EXPECT_EQ(context.MxCsr, context.FltSave.MxCsr); |
| 84 |
| 85 // fxsave doesn’t write these bytes. |
| 86 for (size_t i = 0; i < arraysize(context.FltSave.Reserved4); ++i) { |
| 87 SCOPED_TRACE(i); |
| 88 EXPECT_EQ(0, context.FltSave.Reserved4[i]); |
| 89 } |
| 90 |
| 91 // CaptureContext() doesn’t use these fields. |
| 92 EXPECT_EQ(0, context.P1Home); |
| 93 EXPECT_EQ(0, context.P2Home); |
| 94 EXPECT_EQ(0, context.P3Home); |
| 95 EXPECT_EQ(0, context.P4Home); |
| 96 EXPECT_EQ(0, context.P5Home); |
| 97 EXPECT_EQ(0, context.P6Home); |
| 98 for (size_t i = 0; i < arraysize(context.VectorRegister); ++i) { |
| 99 SCOPED_TRACE(i); |
| 100 EXPECT_EQ(0, context.VectorRegister[i].Low); |
| 101 EXPECT_EQ(0, context.VectorRegister[i].High); |
| 102 } |
| 103 EXPECT_EQ(0, context.VectorControl); |
| 104 EXPECT_EQ(0, context.DebugControl); |
| 105 EXPECT_EQ(0, context.LastBranchToRip); |
| 106 EXPECT_EQ(0, context.LastBranchFromRip); |
| 107 EXPECT_EQ(0, context.LastExceptionToRip); |
| 108 EXPECT_EQ(0, context.LastExceptionFromRip); |
| 109 #endif |
| 110 } |
| 111 |
| 112 // A CPU-independent function to return the program counter. |
| 113 uintptr_t ProgramCounterFromContext(const CONTEXT& context) { |
| 114 #if defined(ARCH_CPU_X86) |
| 115 return context.Eip; |
| 116 #elif defined(ARCH_CPU_X86_64) |
| 117 return context.Rip; |
| 118 #endif |
| 119 } |
| 120 |
| 121 // A CPU-independent function to return the stack pointer. |
| 122 uintptr_t StackPointerFromContext(const CONTEXT& context) { |
| 123 #if defined(ARCH_CPU_X86) |
| 124 return context.Esp; |
| 125 #elif defined(ARCH_CPU_X86_64) |
| 126 return context.Rsp; |
| 127 #endif |
| 128 } |
| 129 |
| 130 void TestCaptureContext() { |
| 131 CONTEXT context_1; |
| 132 CaptureContext(&context_1); |
| 133 |
| 134 { |
| 135 SCOPED_TRACE("context_1"); |
| 136 ASSERT_NO_FATAL_FAILURE(SanityCheckContext(context_1)); |
| 137 } |
| 138 |
| 139 // The program counter reference value is this function’s address. The |
| 140 // captured program counter should be slightly greater than or equal to the |
| 141 // reference program counter. |
| 142 uintptr_t pc = ProgramCounterFromContext(context_1); |
| 143 |
| 144 // Declare sp and context_2 here because all local variables need to be |
| 145 // declared before computing the stack pointer reference value, so that the |
| 146 // reference value can be the lowest value possible. |
| 147 uintptr_t sp; |
| 148 CONTEXT context_2; |
| 149 |
| 150 // The stack pointer reference value is the lowest address of a local variable |
| 151 // in this function. The captured program counter will be slightly less than |
| 152 // or equal to the reference stack pointer. |
| 153 const uintptr_t kReferenceSP = |
| 154 std::min(std::min(reinterpret_cast<uintptr_t>(&context_1), |
| 155 reinterpret_cast<uintptr_t>(&context_2)), |
| 156 std::min(reinterpret_cast<uintptr_t>(&pc), |
| 157 reinterpret_cast<uintptr_t>(&sp))); |
| 158 sp = StackPointerFromContext(context_1); |
| 159 EXPECT_LT(kReferenceSP - sp, 512u); |
| 160 |
| 161 // Capture the context again, expecting that the stack pointer stays the same |
| 162 // and the program counter increases. Strictly speaking, there’s no guarantee |
| 163 // that these conditions will hold, although they do for known compilers even |
| 164 // under typical optimization. |
| 165 CaptureContext(&context_2); |
| 166 |
| 167 { |
| 168 SCOPED_TRACE("context_2"); |
| 169 ASSERT_NO_FATAL_FAILURE(SanityCheckContext(context_2)); |
| 170 } |
| 171 |
| 172 EXPECT_EQ(sp, StackPointerFromContext(context_2)); |
| 173 EXPECT_GT(ProgramCounterFromContext(context_2), pc); |
| 174 } |
| 175 |
| 176 TEST(CaptureContextWin, CaptureContext) { |
| 177 ASSERT_NO_FATAL_FAILURE(TestCaptureContext()); |
| 178 } |
| 179 |
| 180 } // namespace |
| 181 } // namespace test |
| 182 } // namespace crashpad |
OLD | NEW |