manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
manzagop | 6d14991 | 2016-12-19 20:17:09 | [diff] [blame] | 5 | #include "components/browser_watcher/stability_debugging.h" |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 6 | |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 7 | #include <windows.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | |
manzagop | 6d14991 | 2016-12-19 20:17:09 | [diff] [blame] | 11 | #include "base/debug/activity_tracker.h" |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 12 | #include "build/build_config.h" |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 13 | |
| 14 | namespace browser_watcher { |
| 15 | |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | struct VehUnregisterer { |
| 19 | void operator()(void* handle) const { |
| 20 | ::RemoveVectoredExceptionHandler(handle); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | using VehHandle = std::unique_ptr<void, VehUnregisterer>; |
| 25 | |
| 26 | uintptr_t GetProgramCounter(const CONTEXT& context) { |
| 27 | #if defined(ARCH_CPU_X86) |
| 28 | return context.Eip; |
| 29 | #elif defined(ARCH_CPU_X86_64) |
| 30 | return context.Rip; |
Tom Tan | ab224ff | 2018-11-13 17:51:42 | [diff] [blame] | 31 | #elif defined(ARCH_CPU_ARM64) |
| 32 | return context.Pc; |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 33 | #endif |
| 34 | } |
| 35 | |
| 36 | LONG CALLBACK VectoredExceptionHandler(EXCEPTION_POINTERS* exception_pointers) { |
| 37 | base::debug::GlobalActivityTracker* tracker = |
| 38 | base::debug::GlobalActivityTracker::Get(); |
| 39 | if (tracker) { |
| 40 | EXCEPTION_RECORD* record = exception_pointers->ExceptionRecord; |
| 41 | uintptr_t pc = GetProgramCounter(*exception_pointers->ContextRecord); |
| 42 | tracker->RecordException(reinterpret_cast<void*>(pc), |
| 43 | record->ExceptionAddress, record->ExceptionCode); |
| 44 | } |
| 45 | |
| 46 | return EXCEPTION_CONTINUE_SEARCH; // Continue to the next handler. |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 51 | void SetStabilityDataBool(base::StringPiece name, bool value) { |
| 52 | base::debug::GlobalActivityTracker* global_tracker = |
| 53 | base::debug::GlobalActivityTracker::Get(); |
| 54 | if (!global_tracker) |
| 55 | return; // Activity tracking isn't enabled. |
| 56 | |
| 57 | global_tracker->process_data().SetBool(name, value); |
| 58 | } |
| 59 | |
manzagop | 6d14991 | 2016-12-19 20:17:09 | [diff] [blame] | 60 | void SetStabilityDataInt(base::StringPiece name, int64_t value) { |
| 61 | base::debug::GlobalActivityTracker* global_tracker = |
| 62 | base::debug::GlobalActivityTracker::Get(); |
| 63 | if (!global_tracker) |
| 64 | return; // Activity tracking isn't enabled. |
| 65 | |
bcwhite | 8ffd390 | 2017-04-11 17:10:49 | [diff] [blame] | 66 | global_tracker->process_data().SetInt(name, value); |
manzagop | 6d14991 | 2016-12-19 20:17:09 | [diff] [blame] | 67 | } |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame] | 68 | |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 69 | void RegisterStabilityVEH() { |
Sigurdur Asgeirsson | 69d0bcd | 2018-03-29 21:50:51 | [diff] [blame] | 70 | #if defined(ADDRESS_SANITIZER) |
Etienne Bergeron | 5f4de1c | 2017-11-21 20:08:56 | [diff] [blame] | 71 | // ASAN on windows x64 is dynamically allocating the shadow memory on a |
| 72 | // memory access violation by setting up an vector exception handler. |
| 73 | // When instrumented with ASAN, this code may trigger an exception by |
| 74 | // accessing unallocated shadow memory, which is causing an infinite |
| 75 | // recursion (i.e. infinite memory access violation). |
| 76 | (void)&VectoredExceptionHandler; |
| 77 | #else |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 78 | // Register a vectored exception handler and request it be first. Note that |
| 79 | // subsequent registrations may also request to be first, in which case this |
| 80 | // one will be bumped. |
| 81 | // TODO(manzagop): Depending on observations, it may be necessary to |
| 82 | // consider refreshing the registration, either periodically or at opportune |
| 83 | // (e.g. risky) times. |
| 84 | static VehHandle veh_handler( |
| 85 | ::AddVectoredExceptionHandler(1, &VectoredExceptionHandler)); |
| 86 | DCHECK(veh_handler); |
Etienne Bergeron | 5f4de1c | 2017-11-21 20:08:56 | [diff] [blame] | 87 | #endif // ADDRESS_SANITIZER |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 88 | } |
| 89 | |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 90 | } // namespace browser_watcher |