File: extension_function_crash_keys.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,823 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/extension_function_crash_keys.h"

#include <array>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/no_destructor.h"
#include "base/time/time.h"
#include "components/crash/core/common/crash_key.h"
#include "extensions/common/extension_id.h"

namespace extensions::extension_function_crash_keys {
namespace {

struct CallInfo {
  int count = 0;              // Number of in-flight calls.
  base::TimeTicks timestamp;  // Time of the last call.
};

// Returns a map from an extension ID to information about in-flight calls to
// ExtensionFunction. Uses base::flat_map<> because the map is typically small
// (0 or 1 item) and the size is bounded by the number of installed extensions.
// NOTE: This approach isn't perfect. In particular, this call sequence ends up
// with slightly odd reporting:
// - API A start (1)
// - API B start
// - API A start (2)
// - API A end (2)
// This will report crash keys in the order (API A, API B) even though the most
// recent API A call has completed. This seemms OK because it's true that API A
// was the most recently called. It also avoids storing a stack of all in-flight
// API calls with per-call IDs to match them up. During startup when extensions
// are initializing there can be hundreds of in-flight calls.
base::flat_map<ExtensionId, CallInfo>& ExtensionIdToCallInfoMap() {
  static base::NoDestructor<base::flat_map<ExtensionId, CallInfo>> instance;
  return *instance;
}

// Updates the crash keys for extensions with in-flight ExtensionFunction calls.
void UpdateCrashKeys() {
  // Extract the call timestamps and extension IDs into a vector for sorting.
  // Use ExtensionId* to avoid copying the string IDs.
  const auto& map = ExtensionIdToCallInfoMap();
  std::vector<std::pair<base::TimeTicks, const ExtensionId*>> calls;
  calls.reserve(map.size());
  for (const auto& entry : map) {
    calls.emplace_back(entry.second.timestamp, &entry.first);
  }
  // Sort most recent calls to the front of the vector.
  std::sort(calls.begin(), calls.end(), std::greater<>());
  // Set up crash keys.
  using ArrayItemKey = crash_reporter::CrashKeyString<64>;
  static constexpr int kMaxCrashKeys = 3;
  static std::array<ArrayItemKey, kMaxCrashKeys> crash_keys = {
      ArrayItemKey{"extension-function-caller-1", ArrayItemKey::Tag::kArray},
      ArrayItemKey{"extension-function-caller-2", ArrayItemKey::Tag::kArray},
      ArrayItemKey{"extension-function-caller-3", ArrayItemKey::Tag::kArray},
  };
  // Store up to 3 crash keys with extension IDs.
  int index = 0;
  for (auto it = calls.begin(); it != calls.end() && index < kMaxCrashKeys;
       ++it, ++index) {
    const ExtensionId* extension_id = it->second;
    crash_keys[index].Set(*extension_id);
  }
  // Clear the remaining crash keys.
  for (; index < kMaxCrashKeys; ++index) {
    crash_keys[index].Clear();
  }
}

}  // namespace

void StartExtensionFunctionCall(const ExtensionId& extension_id) {
  base::TimeTicks now = base::TimeTicks::Now();
  auto& map = ExtensionIdToCallInfoMap();
  auto it = map.find(extension_id);
  if (it == map.end()) {
    map[extension_id] = {.count = 1, .timestamp = now};
  } else {
    it->second.count++;
    it->second.timestamp = now;
  }
  UpdateCrashKeys();
}

void EndExtensionFunctionCall(const ExtensionId& extension_id) {
  auto& map = ExtensionIdToCallInfoMap();
  auto it = map.find(extension_id);
  CHECK(it != map.end());
  int new_count = --it->second.count;
  CHECK_GE(new_count, 0);
  if (new_count == 0) {
    map.erase(it);
    UpdateCrashKeys();
  }
}

}  // namespace extensions::extension_function_crash_keys