summaryrefslogtreecommitdiff
path: root/libs/input/InputEventLabels.cpp
diff options
context:
space:
mode:
author Siarhei Vishniakou <svv@google.com> 2023-06-15 19:46:30 -0700
committer Siarhei Vishniakou <svv@google.com> 2023-06-16 10:13:12 -0700
commitffe62933bc0c0b2340b3d69dc202deaea2317ab0 (patch)
tree2bc347ae2e3209fca056a445fe8e9fb690bc89f0 /libs/input/InputEventLabels.cpp
parent02deea604821f351e2d9a21f2b2a7572df0dc76e (diff)
Initialize InputEventLookup in a static function
Currently, the InputEventLookup class uses static fields, which means that they are getting initialized early. This causes issues with address sanitizers. The sanitizers report a memory leak with the purely static approach. It's not clear whether this is an actual issue, or just a bug elsewhere in the system. To get around these problems, create a singleton instance of InputEventLookup. This singleton will be created upon the first access to any of its static functions. This helps resolves the sanitizer issues. Bug: 271455682 Test: FUZZER=inputflinger_input_dispatcher_fuzzer; m $FUZZER && out/host/linux-x86/fuzz/x86_64/$FUZZER/$FUZZER Note: test requires more CLs that are upcoming Change-Id: Id655554db9c706f5d45eaebbee55e12519dcfdc8
Diffstat (limited to 'libs/input/InputEventLabels.cpp')
-rw-r--r--libs/input/InputEventLabels.cpp37
1 files changed, 20 insertions, 17 deletions
diff --git a/libs/input/InputEventLabels.cpp b/libs/input/InputEventLabels.cpp
index 1c7cc129bd..bade68629d 100644
--- a/libs/input/InputEventLabels.cpp
+++ b/libs/input/InputEventLabels.cpp
@@ -434,17 +434,14 @@ namespace android {
// clang-format on
// --- InputEventLookup ---
-const std::unordered_map<std::string, int> InputEventLookup::KEYCODES = {KEYCODES_SEQUENCE};
-const std::vector<InputEventLabel> InputEventLookup::KEY_NAMES = {KEYCODES_SEQUENCE};
-
-const std::unordered_map<std::string, int> InputEventLookup::AXES = {AXES_SEQUENCE};
-
-const std::vector<InputEventLabel> InputEventLookup::AXES_NAMES = {AXES_SEQUENCE};
-
-const std::unordered_map<std::string, int> InputEventLookup::LEDS = {LEDS_SEQUENCE};
-
-const std::unordered_map<std::string, int> InputEventLookup::FLAGS = {FLAGS_SEQUENCE};
+InputEventLookup::InputEventLookup()
+ : KEYCODES({KEYCODES_SEQUENCE}),
+ KEY_NAMES({KEYCODES_SEQUENCE}),
+ AXES({AXES_SEQUENCE}),
+ AXES_NAMES({AXES_SEQUENCE}),
+ LEDS({LEDS_SEQUENCE}),
+ FLAGS({FLAGS_SEQUENCE}) {}
std::optional<int> InputEventLookup::lookupValueByLabel(
const std::unordered_map<std::string, int>& map, const char* literal) {
@@ -462,30 +459,36 @@ const char* InputEventLookup::lookupLabelByValue(const std::vector<InputEventLab
}
std::optional<int> InputEventLookup::getKeyCodeByLabel(const char* label) {
- return lookupValueByLabel(KEYCODES, label);
+ const auto& self = get();
+ return self.lookupValueByLabel(self.KEYCODES, label);
}
const char* InputEventLookup::getLabelByKeyCode(int32_t keyCode) {
- if (keyCode >= 0 && static_cast<size_t>(keyCode) < KEYCODES.size()) {
- return lookupLabelByValue(KEY_NAMES, keyCode);
+ const auto& self = get();
+ if (keyCode >= 0 && static_cast<size_t>(keyCode) < self.KEYCODES.size()) {
+ return get().lookupLabelByValue(self.KEY_NAMES, keyCode);
}
return nullptr;
}
std::optional<int> InputEventLookup::getKeyFlagByLabel(const char* label) {
- return lookupValueByLabel(FLAGS, label);
+ const auto& self = get();
+ return lookupValueByLabel(self.FLAGS, label);
}
std::optional<int> InputEventLookup::getAxisByLabel(const char* label) {
- return lookupValueByLabel(AXES, label);
+ const auto& self = get();
+ return lookupValueByLabel(self.AXES, label);
}
const char* InputEventLookup::getAxisLabel(int32_t axisId) {
- return lookupLabelByValue(AXES_NAMES, axisId);
+ const auto& self = get();
+ return lookupLabelByValue(self.AXES_NAMES, axisId);
}
std::optional<int> InputEventLookup::getLedByLabel(const char* label) {
- return lookupValueByLabel(LEDS, label);
+ const auto& self = get();
+ return lookupValueByLabel(self.LEDS, label);
}
namespace {