Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 98f5bde4 authored by yutingfang's avatar yutingfang
Browse files

Add a rate limiter in AppOpsService to prevent excessive loggings

The atom AppOpNoteOpOrCheckOpBinderApiCalled can log excessively on some devices. Add a rate limiter to put the atom logging frequency within the boundry of once per 10 miliseconds.

Flag: EXEMPT log only update
Bug: 365584286
Test: presubmit
Change-Id: Ifd4a5efbb66926c3e88c5fc68a469f75673d1c72
parent e1b76619
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -182,6 +182,7 @@ import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.pkg.AndroidPackage;
import com.android.server.pm.pkg.PackageState;
import com.android.server.policy.AppOpsPolicy;
import com.android.server.selinux.RateLimiter;

import dalvik.annotation.optimization.NeverCompile;

@@ -201,6 +202,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
@@ -351,6 +353,10 @@ public class AppOpsService extends IAppOpsService.Stub {
    @GuardedBy("this")
    private boolean mUidStatesInitialized;

    // A rate limiter to prevent excessive Atom pushing. Used by noteOperation.
    private static final Duration RATE_LIMITER_WINDOW = Duration.ofMillis(10);
    private final RateLimiter mRateLimiter = new RateLimiter(RATE_LIMITER_WINDOW);

    volatile @NonNull HistoricalRegistry mHistoricalRegistry = new HistoricalRegistry(this);

    /*
@@ -3135,11 +3141,13 @@ public class AppOpsService extends IAppOpsService.Stub {
            boolean shouldCollectMessage) {
        if (Binder.getCallingPid() != Process.myPid()
                && Flags.appopAccessTrackingLoggingEnabled()) {
            if (mRateLimiter.tryAcquire()) {
                FrameworkStatsLog.write(
                        APP_OP_NOTE_OP_OR_CHECK_OP_BINDER_API_CALLED, uid, code,
                        APP_OP_NOTE_OP_OR_CHECK_OP_BINDER_API_CALLED__BINDER_API__NOTE_OPERATION,
                        attributionTag != null);
            }
        }
        return mCheckOpsDelegateDispatcher.noteOperation(code, uid, packageName,
                attributionTag, Context.DEVICE_ID_DEFAULT, shouldCollectAsyncNotedOp, message,
                shouldCollectMessage);