Skip to content

Commit 8cd4e1f

Browse files
Jimmy Shiulifehackerhansol
authored andcommitted
power-libperfmgr: ADPF: fix use-after-free crash
The main problem is the timer thread could be woken after the session was destroyed. We did have a closed flag which was set in destructor and the flag would be checked before handleMessage accessing the session instance. To fix the problem, the operations of flag checking and session instance accessing should be guarded by the lock. Bug: 236674672 Test: manual test Change-Id: I49a18efbc135b1bc070b101038a8a0bcc6e19fec (cherry picked from commit 5c75978f530b27bd976d8695ed79acd336c24776) Merged-In: I49a18efbc135b1bc070b101038a8a0bcc6e19fec
1 parent d859b1d commit 8cd4e1f

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

power-libperfmgr/aidl/PowerHintSession.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,10 @@ ndk::ScopedAStatus PowerHintSession::close() {
263263
}
264264
// Remove the session from PowerSessionManager first to avoid racing.
265265
PowerSessionManager::getInstance()->removePowerSession(this);
266-
setSessionUclampMin(0);
267-
{
268-
std::lock_guard<std::mutex> guard(mSessionLock);
269-
mSessionClosed.store(true);
270-
}
271-
mDescriptor->is_active.store(false);
272266
mEarlyBoostHandler->setSessionDead();
273267
mStaleTimerHandler->setSessionDead();
268+
setSessionUclampMin(0);
269+
mDescriptor->is_active.store(false);
274270
updateUniveralBoostMode();
275271
return ndk::ScopedAStatus::ok();
276272
}
@@ -501,6 +497,7 @@ void PowerHintSession::StaleTimerHandler::updateTimer(time_point<steady_clock> s
501497
}
502498

503499
void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) {
500+
std::lock_guard<std::mutex> guard(mClosedLock);
504501
if (mIsSessionDead) {
505502
return;
506503
}
@@ -530,7 +527,7 @@ void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) {
530527
}
531528

532529
void PowerHintSession::StaleTimerHandler::setSessionDead() {
533-
std::lock_guard<std::mutex> guard(mStaleLock);
530+
std::lock_guard<std::mutex> guard(mClosedLock);
534531
mIsSessionDead = true;
535532
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
536533
}

power-libperfmgr/aidl/PowerHintSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class PowerHintSession : public BnPowerHintSession {
103103

104104
private:
105105
PowerHintSession *mSession;
106-
std::mutex mStaleLock;
106+
std::mutex mClosedLock;
107107
std::mutex mMessageLock;
108108
std::atomic<time_point<steady_clock>> mStaleTime;
109109
std::atomic<bool> mIsMonitoring;

0 commit comments

Comments
 (0)