Add lock to protect UnwantedInteractionBlocker
The call to 'dump' may come from any thread, and therefore could cause a
crash. Add a lock to protect this input stage.
To run the test:
adb shell -t "/data/nativetest64/inputflinger_tests/inputflinger_tests --gtest_filter='*Dump*' --gtest_repeat=100000 --gtest_break_on_failure"
Before this patch, the test failed after ~5K - ~13K iterations (took
10-20 seconds to crash).
Bug: 232645962
Test: m inputflinger_tests && adb sync data && <run the test>
Change-Id: I2a199690450bc5bb4a8576aa59075e99d37a531b
(cherry picked from commit 9f330c542b48dc6edba9aeaff3b3f4bf305713f3)
diff --git a/services/inputflinger/InputClassifier.cpp b/services/inputflinger/InputClassifier.cpp
index 3ea0986..8ce2f35 100644
--- a/services/inputflinger/InputClassifier.cpp
+++ b/services/inputflinger/InputClassifier.cpp
@@ -367,7 +367,7 @@
// --- InputClassifier ---
-InputClassifier::InputClassifier(InputListenerInterface& listener) : mListener(listener) {}
+InputClassifier::InputClassifier(InputListenerInterface& listener) : mQueuedListener(listener) {}
void InputClassifier::onBinderDied(void* cookie) {
InputClassifier* classifier = static_cast<InputClassifier*>(cookie);
@@ -417,55 +417,67 @@
void InputClassifier::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
// pass through
- mListener.notifyConfigurationChanged(args);
+ mQueuedListener.notifyConfigurationChanged(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifyKey(const NotifyKeyArgs* args) {
// pass through
- mListener.notifyKey(args);
+ mQueuedListener.notifyKey(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifyMotion(const NotifyMotionArgs* args) {
- std::scoped_lock lock(mLock);
- // MotionClassifier is only used for touch events, for now
- const bool sendToMotionClassifier = mMotionClassifier && isTouchEvent(*args);
- if (!sendToMotionClassifier) {
- mListener.notifyMotion(args);
- return;
- }
-
- NotifyMotionArgs newArgs(*args);
- newArgs.classification = mMotionClassifier->classify(newArgs);
- mListener.notifyMotion(&newArgs);
+ { // acquire lock
+ std::scoped_lock lock(mLock);
+ // MotionClassifier is only used for touch events, for now
+ const bool sendToMotionClassifier = mMotionClassifier && isTouchEvent(*args);
+ if (!sendToMotionClassifier) {
+ mQueuedListener.notifyMotion(args);
+ } else {
+ NotifyMotionArgs newArgs(*args);
+ newArgs.classification = mMotionClassifier->classify(newArgs);
+ mQueuedListener.notifyMotion(&newArgs);
+ }
+ } // release lock
+ mQueuedListener.flush();
}
void InputClassifier::notifySensor(const NotifySensorArgs* args) {
// pass through
- mListener.notifySensor(args);
+ mQueuedListener.notifySensor(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifyVibratorState(const NotifyVibratorStateArgs* args) {
// pass through
- mListener.notifyVibratorState(args);
+ mQueuedListener.notifyVibratorState(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifySwitch(const NotifySwitchArgs* args) {
// pass through
- mListener.notifySwitch(args);
+ mQueuedListener.notifySwitch(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
- std::scoped_lock lock(mLock);
- if (mMotionClassifier) {
- mMotionClassifier->reset(*args);
- }
+ { // acquire lock
+ std::scoped_lock lock(mLock);
+ if (mMotionClassifier) {
+ mMotionClassifier->reset(*args);
+ }
+ } // release lock
+
// continue to next stage
- mListener.notifyDeviceReset(args);
+ mQueuedListener.notifyDeviceReset(args);
+ mQueuedListener.flush();
}
void InputClassifier::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
// pass through
- mListener.notifyPointerCaptureChanged(args);
+ mQueuedListener.notifyPointerCaptureChanged(args);
+ mQueuedListener.flush();
}
void InputClassifier::setMotionClassifierLocked(
@@ -490,6 +502,10 @@
dump += "\n";
}
+void InputClassifier::monitor() {
+ std::scoped_lock lock(mLock);
+}
+
InputClassifier::~InputClassifier() {
}