blob: e0a02d1808526ab1ca6eaff34f2b695e243fbb55 [file] [log] [blame]
jamescookda2505812015-03-20 18:01:181// 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
darinfa18d502016-08-12 19:49:277#include "base/bind.h"
rockotf050c892016-10-05 01:28:288#include "base/debug/dump_without_crashing.h"
jamescookda2505812015-03-20 18:01:189#include "base/logging.h"
Ilya Sherman1c811db2017-12-14 10:36:1810#include "base/metrics/histogram_functions.h"
creis37a5d6ee2016-06-21 17:33:5211#include "base/strings/string_number_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2812#include "base/task/post_task.h"
nick16794822015-06-02 23:23:3113#include "content/public/browser/browser_message_filter.h"
Eric Seckler8652dcd52018-09-20 10:42:2814#include "content/public/browser/browser_task_traits.h"
darinfa18d502016-08-12 19:49:2715#include "content/public/browser/browser_thread.h"
jamescookda2505812015-03-20 18:01:1816#include "content/public/browser/render_process_host.h"
17
18namespace content {
19namespace bad_message {
20
nick16794822015-06-02 23:23:3121namespace {
22
23void LogBadMessage(BadMessageReason reason) {
Robert Sesek1419427e2017-12-07 15:01:3224 static auto* bad_message_reason = base::debug::AllocateCrashKeyString(
25 "bad_message_reason", base::debug::CrashKeySize::Size32);
26
jamescookda2505812015-03-20 18:01:1827 LOG(ERROR) << "Terminating renderer for bad IPC message, reason " << reason;
Ilya Sherman1c811db2017-12-14 10:36:1828 base::UmaHistogramSparse("Stability.BadMessageTerminated.Content", reason);
Robert Sesek1419427e2017-12-07 15:01:3229 base::debug::SetCrashKeyString(bad_message_reason, base::IntToString(reason));
nick16794822015-06-02 23:23:3130}
31
darinfa18d502016-08-12 19:49:2732void ReceivedBadMessageOnUIThread(int render_process_id,
33 BadMessageReason reason) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
rockotf050c892016-10-05 01:28:2836 if (!host)
37 return;
38
rockotf050c892016-10-05 01:28:2839 // A dump has already been generated by the caller. Don't generate another.
40 host->ShutdownForBadMessage(
41 RenderProcessHost::CrashReportMode::NO_CRASH_DUMP);
darinfa18d502016-08-12 19:49:2742}
43
nick16794822015-06-02 23:23:3144} // namespace
45
46void ReceivedBadMessage(RenderProcessHost* host, BadMessageReason reason) {
47 LogBadMessage(reason);
rockotf050c892016-10-05 01:28:2848 host->ShutdownForBadMessage(
49 RenderProcessHost::CrashReportMode::GENERATE_CRASH_DUMP);
jamescookda2505812015-03-20 18:01:1850}
51
darinfa18d502016-08-12 19:49:2752void ReceivedBadMessage(int render_process_id, BadMessageReason reason) {
rockotd2966792017-01-13 01:36:4753 // We generate a crash dump here since generating one after posting to the UI
54 // thread is less useful.
55 LogBadMessage(reason);
rockotf050c892016-10-05 01:28:2856 base::debug::DumpWithoutCrashing();
57
darinfa18d502016-08-12 19:49:2758 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
Eric Seckler8652dcd52018-09-20 10:42:2859 base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
60 base::BindOnce(&ReceivedBadMessageOnUIThread,
61 render_process_id, reason));
darinfa18d502016-08-12 19:49:2762 return;
63 }
64 ReceivedBadMessageOnUIThread(render_process_id, reason);
65}
66
nick16794822015-06-02 23:23:3167void ReceivedBadMessage(BrowserMessageFilter* filter, BadMessageReason reason) {
68 LogBadMessage(reason);
69 filter->ShutdownForBadMessage();
70}
71
Robert Sesek1419427e2017-12-07 15:01:3272base::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 Sesek1e07e372017-12-09 01:34:4278base::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
84base::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
jamescookda2505812015-03-20 18:01:1890} // namespace bad_message
91} // namespace content