Skip to content

Commit dc65953

Browse files
Ensure objects remain valid after calling policy am: 7d7ac48
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/31313194 Change-Id: Iae3bd884a452ba8d1c5d79dc33591e06d0759688 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 parents f7f924b + 7d7ac48 commit dc65953

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5730,9 +5730,7 @@ void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
57305730

57315731
bool restartEvent;
57325732
if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) {
5733-
KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry));
5734-
restartEvent =
5735-
afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled);
5733+
restartEvent = afterKeyEventLockedInterruptable(connection, dispatchEntry, handled);
57365734
} else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) {
57375735
MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry));
57385736
restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry,
@@ -5960,7 +5958,17 @@ void InputDispatcher::processConnectionResponsiveLocked(const Connection& connec
59605958

59615959
bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection,
59625960
DispatchEntry* dispatchEntry,
5963-
KeyEntry& keyEntry, bool handled) {
5961+
bool handled) {
5962+
// The dispatchEntry is currently valid, but it might point to a deleted object after we release
5963+
// the lock. For simplicity, make copies of the data of interest here and assume that
5964+
// 'dispatchEntry' is not valid after this section.
5965+
// Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
5966+
// function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
5967+
std::shared_ptr<EventEntry> eventEntry = dispatchEntry->eventEntry;
5968+
const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget();
5969+
KeyEntry& keyEntry = static_cast<KeyEntry&>(*(eventEntry));
5970+
// To prevent misuse, ensure dispatchEntry is no longer valid.
5971+
dispatchEntry = nullptr;
59645972
if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
59655973
if (!handled) {
59665974
// Report the key as unhandled, since the fallback was not handled.
@@ -5977,7 +5985,7 @@ bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& con
59775985
connection->inputState.removeFallbackKey(originalKeyCode);
59785986
}
59795987

5980-
if (handled || !dispatchEntry->hasForegroundTarget()) {
5988+
if (handled || !hasForegroundTarget) {
59815989
// If the application handles the original key for which we previously
59825990
// generated a fallback or if the window is not a foreground window,
59835991
// then cancel the associated fallback key, if any.

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ class InputDispatcher : public android::InputDispatcherInterface {
662662
void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason)
663663
REQUIRES(mLock);
664664
bool afterKeyEventLockedInterruptable(const sp<Connection>& connection,
665-
DispatchEntry* dispatchEntry, KeyEntry& keyEntry,
665+
DispatchEntry* dispatchEntry,
666666
bool handled) REQUIRES(mLock);
667667
bool afterMotionEventLockedInterruptable(const sp<Connection>& connection,
668668
DispatchEntry* dispatchEntry, MotionEntry& motionEntry,

0 commit comments

Comments
 (0)