Prevents system busy or restarted due to rapid activity launch loop

Tracking rapid activity starts. Move the task to back and
kill the app if the rapid launch count exceeds maximum.

Bug: 145096943
Test: verified using a bad app
Change-Id: I164596fb21df55e9daf6d28fb3664e7300eafd7e
This commit is contained in:
Louis Chang
2023-04-20 03:29:35 +00:00
parent 6218576ef9
commit 5693405bac

View File

@@ -107,6 +107,12 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
private static final int MAX_RAPID_ACTIVITY_LAUNCH_COUNT = 500;
private static final long RAPID_ACTIVITY_LAUNCH_MS = 300;
private static final long RESET_RAPID_ACTIVITY_LAUNCH_MS = 5 * RAPID_ACTIVITY_LAUNCH_MS;
private int mRapidActivityLaunchCount;
// all about the first app in the process
final ApplicationInfo mInfo;
final String mName;
@@ -538,7 +544,8 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
return mLastActivityLaunchTime > 0;
}
void setLastActivityLaunchTime(long launchTime) {
void setLastActivityLaunchTime(ActivityRecord r) {
long launchTime = r.lastLaunchTime;
if (launchTime <= mLastActivityLaunchTime) {
if (launchTime < mLastActivityLaunchTime) {
Slog.w(TAG,
@@ -547,9 +554,29 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
}
return;
}
updateRapidActivityLaunch(r, launchTime, mLastActivityLaunchTime);
mLastActivityLaunchTime = launchTime;
}
void updateRapidActivityLaunch(ActivityRecord r, long launchTime, long lastLaunchTime) {
if (mInstrumenting || mDebugging || lastLaunchTime <= 0) {
return;
}
final long diff = lastLaunchTime - launchTime;
if (diff < RAPID_ACTIVITY_LAUNCH_MS) {
mRapidActivityLaunchCount++;
} else if (diff >= RESET_RAPID_ACTIVITY_LAUNCH_MS) {
mRapidActivityLaunchCount = 0;
}
if (mRapidActivityLaunchCount > MAX_RAPID_ACTIVITY_LAUNCH_COUNT) {
Slog.w(TAG, "Killing " + mPid + " because of rapid activity launch");
r.getRootTask().moveTaskToBack(r.getTask());
mAtm.mH.post(() -> mAtm.mAmInternal.killProcess(mName, mUid, "rapidActivityLaunch"));
}
}
void setLastActivityFinishTimeIfNeeded(long finishTime) {
if (finishTime <= mLastActivityFinishTime || !hasActivityInVisibleTask()) {
return;
@@ -696,7 +723,7 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
void addActivityIfNeeded(ActivityRecord r) {
// even if we already track this activity, note down that it has been launched
setLastActivityLaunchTime(r.lastLaunchTime);
setLastActivityLaunchTime(r);
if (mActivities.contains(r)) {
return;
}