jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 1 | // Copyright 2015 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 | |
| 5 | #include "content/browser/bad_message.h" |
| 6 | |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 7 | #include "base/bind.h" |
rockot | f050c89 | 2016-10-05 01:28:28 | [diff] [blame] | 8 | #include "base/debug/dump_without_crashing.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 9 | #include "base/logging.h" |
Ilya Sherman | 1c811db | 2017-12-14 10:36:18 | [diff] [blame] | 10 | #include "base/metrics/histogram_functions.h" |
creis | 37a5d6ee | 2016-06-21 17:33:52 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 12 | #include "base/task/post_task.h" |
nick | 1679482 | 2015-06-02 23:23:31 | [diff] [blame] | 13 | #include "content/public/browser/browser_message_filter.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 14 | #include "content/public/browser/browser_task_traits.h" |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 16 | #include "content/public/browser/render_process_host.h" |
| 17 | |
| 18 | namespace content { |
| 19 | namespace bad_message { |
| 20 | |
nick | 1679482 | 2015-06-02 23:23:31 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | void LogBadMessage(BadMessageReason reason) { |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 24 | static auto* bad_message_reason = base::debug::AllocateCrashKeyString( |
| 25 | "bad_message_reason", base::debug::CrashKeySize::Size32); |
| 26 | |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 27 | LOG(ERROR) << "Terminating renderer for bad IPC message, reason " << reason; |
Ilya Sherman | 1c811db | 2017-12-14 10:36:18 | [diff] [blame] | 28 | base::UmaHistogramSparse("Stability.BadMessageTerminated.Content", reason); |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 29 | base::debug::SetCrashKeyString(bad_message_reason, base::IntToString(reason)); |
nick | 1679482 | 2015-06-02 23:23:31 | [diff] [blame] | 30 | } |
| 31 | |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 32 | void ReceivedBadMessageOnUIThread(int render_process_id, |
| 33 | BadMessageReason reason) { |
| 34 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 35 | RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
rockot | f050c89 | 2016-10-05 01:28:28 | [diff] [blame] | 36 | if (!host) |
| 37 | return; |
| 38 | |
rockot | f050c89 | 2016-10-05 01:28:28 | [diff] [blame] | 39 | // A dump has already been generated by the caller. Don't generate another. |
| 40 | host->ShutdownForBadMessage( |
| 41 | RenderProcessHost::CrashReportMode::NO_CRASH_DUMP); |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 42 | } |
| 43 | |
nick | 1679482 | 2015-06-02 23:23:31 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | void ReceivedBadMessage(RenderProcessHost* host, BadMessageReason reason) { |
| 47 | LogBadMessage(reason); |
rockot | f050c89 | 2016-10-05 01:28:28 | [diff] [blame] | 48 | host->ShutdownForBadMessage( |
| 49 | RenderProcessHost::CrashReportMode::GENERATE_CRASH_DUMP); |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 50 | } |
| 51 | |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 52 | void ReceivedBadMessage(int render_process_id, BadMessageReason reason) { |
rockot | d296679 | 2017-01-13 01:36:47 | [diff] [blame] | 53 | // We generate a crash dump here since generating one after posting to the UI |
| 54 | // thread is less useful. |
| 55 | LogBadMessage(reason); |
rockot | f050c89 | 2016-10-05 01:28:28 | [diff] [blame] | 56 | base::debug::DumpWithoutCrashing(); |
| 57 | |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 58 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 59 | base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, |
| 60 | base::BindOnce(&ReceivedBadMessageOnUIThread, |
| 61 | render_process_id, reason)); |
darin | fa18d50 | 2016-08-12 19:49:27 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | ReceivedBadMessageOnUIThread(render_process_id, reason); |
| 65 | } |
| 66 | |
nick | 1679482 | 2015-06-02 23:23:31 | [diff] [blame] | 67 | void ReceivedBadMessage(BrowserMessageFilter* filter, BadMessageReason reason) { |
| 68 | LogBadMessage(reason); |
| 69 | filter->ShutdownForBadMessage(); |
| 70 | } |
| 71 | |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 72 | base::debug::CrashKeyString* GetMojoErrorCrashKey() { |
| 73 | static auto* crash_key = base::debug::AllocateCrashKeyString( |
| 74 | "mojo-message-error", base::debug::CrashKeySize::Size256); |
| 75 | return crash_key; |
| 76 | } |
| 77 | |
Robert Sesek | 1e07e37 | 2017-12-09 01:34:42 | [diff] [blame] | 78 | base::debug::CrashKeyString* GetKilledProcessOriginLockKey() { |
| 79 | static auto* crash_key = base::debug::AllocateCrashKeyString( |
| 80 | "killed_process_origin_lock", base::debug::CrashKeySize::Size64); |
| 81 | return crash_key; |
| 82 | } |
| 83 | |
| 84 | base::debug::CrashKeyString* GetRequestedSiteURLKey() { |
| 85 | static auto* crash_key = base::debug::AllocateCrashKeyString( |
| 86 | "requested_site_url", base::debug::CrashKeySize::Size64); |
| 87 | return crash_key; |
| 88 | } |
| 89 | |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 90 | } // namespace bad_message |
| 91 | } // namespace content |