bad_message.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "extensions/browser/bad_message.h"
  5. #include "base/debug/crash_logging.h"
  6. #include "base/logging.h"
  7. #include "base/metrics/histogram_functions.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "content/public/browser/browser_message_filter.h"
  10. #include "content/public/browser/render_process_host.h"
  11. namespace extensions {
  12. namespace bad_message {
  13. namespace {
  14. void LogBadMessage(BadMessageReason reason) {
  15. LOG(ERROR) << "Terminating extension renderer for bad IPC message, reason "
  16. << reason;
  17. base::UmaHistogramSparse("Stability.BadMessageTerminated.Extensions", reason);
  18. }
  19. base::debug::CrashKeyString* GetBadMessageCrashKey() {
  20. static auto* crash_key = base::debug::AllocateCrashKeyString(
  21. "extension_bad_message_reason", base::debug::CrashKeySize::Size64);
  22. return crash_key;
  23. }
  24. } // namespace
  25. void ReceivedBadMessage(content::RenderProcessHost* host,
  26. BadMessageReason reason) {
  27. base::debug::ScopedCrashKeyString crash_key(GetBadMessageCrashKey(),
  28. base::NumberToString(reason));
  29. LogBadMessage(reason);
  30. host->ShutdownForBadMessage(
  31. content::RenderProcessHost::CrashReportMode::GENERATE_CRASH_DUMP);
  32. }
  33. void ReceivedBadMessage(int render_process_id, BadMessageReason reason) {
  34. content::RenderProcessHost* rph =
  35. content::RenderProcessHost::FromID(render_process_id);
  36. // The render process was already terminated.
  37. if (!rph)
  38. return;
  39. ReceivedBadMessage(rph, reason);
  40. }
  41. void ReceivedBadMessage(content::BrowserMessageFilter* filter,
  42. BadMessageReason reason) {
  43. base::debug::ScopedCrashKeyString crash_key(GetBadMessageCrashKey(),
  44. base::NumberToString(reason));
  45. LogBadMessage(reason);
  46. filter->ShutdownForBadMessage();
  47. }
  48. } // namespace bad_message
  49. } // namespace extensions