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

Commit 8ecf83f3 authored by Hui Yu's avatar Hui Yu
Browse files

Log a WTF message if sendBroadcast() or bindService() is called from a

cached state.

On droidfood devices, the WTF logs are sent to server side. We can run
Plx query to generate reports on these logs. This will help to develop
background restriciton.

These WTF logs will be removed in final release.

Bug: 199554878
Test: build and run, adb logcat | grep "from cached state"
Change-Id: Ic09b5dc3fb9d975c989220778134bea7409fbc31
parent c813fed6
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import static android.Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND;
import static android.Manifest.permission.REQUEST_COMPANION_START_FOREGROUND_SERVICES_FROM_BACKGROUND;
import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
import static android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND;
import static android.app.ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
import static android.app.ActivityManager.PROCESS_STATE_HEAVY_WEIGHT;
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static android.app.ActivityManager.PROCESS_STATE_PERSISTENT_UI;
import static android.app.ActivityManager.PROCESS_STATE_RECEIVER;
import static android.app.ActivityManager.PROCESS_STATE_TOP;
@@ -2896,6 +2898,7 @@ public final class ActiveServices {
            }
            setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, s, userId,
                    false);
            logBindServiceFromCachedState(callingPackage, callingUid, service);

            if (s.app != null) {
                ProcessServiceRecord servicePsr = s.app.mServices;
@@ -2960,6 +2963,30 @@ public final class ActiveServices {
        return 1;
    }

    /**
     * Log a WTF message if the bindService is called by a process from a cached proc state.
     * This WTF log is to debug background restriction, it will be removed in before final release.
     * @param callerPackage the caller's package name.
     * @param callingUid the caller's UID.
     * @param intent the service's intent.
     */
    private void logBindServiceFromCachedState(String callerPackage, int callingUid,
            Intent intent) {
        final int callerUidState = mAm.getUidStateLocked(callingUid);
        if (callerUidState == PROCESS_STATE_NONEXISTENT
                || callerUidState < PROCESS_STATE_CACHED_ACTIVITY) {
            return;
        }
        final String msg = "bindService from cached state "
                + "[callerPackage:" + callerPackage
                + "; callingUid:" + callingUid
                + "; uidState:" + ProcessList.makeProcStateString(callerUidState)
                + "; intent:" + intent
                + ";]";
        Slog.wtfQuiet(TAG, msg);
        Slog.i(TAG, msg);
    }

    private void maybeLogBindCrossProfileService(
            int userId, String callingPackage, int callingUid) {
        if (UserHandle.isCore(callingUid)) {
+7 −3
Original line number Diff line number Diff line
@@ -12670,7 +12670,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                    Intent intent = allSticky.get(i);
                    BroadcastQueue queue = broadcastQueueForIntent(intent);
                    BroadcastRecord r = new BroadcastRecord(queue, intent, null,
                            null, null, -1, -1, false, null, null, null, OP_NONE, null, receivers,
                            null, null, -1, -1, PROCESS_STATE_NONEXISTENT, false, null, null, null,
                            OP_NONE, null, receivers,
                            null, 0, null, null, false, true, true, -1, false, null,
                            false /* only PRE_BOOT_COMPLETED should be exempt, no stickies */);
                    queue.enqueueParallelBroadcastLocked(r);
@@ -12931,6 +12932,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            @Nullable int[] broadcastAllowList) {
        intent = new Intent(intent);
        final int callerUidState = getUidStateLocked(realCallingUid);
        final boolean callerInstantApp = isInstantApp(callerApp, callerPackage, callingUid);
        // Instant Apps cannot use FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS
        if (callerInstantApp) {
@@ -13498,7 +13500,8 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            final BroadcastQueue queue = broadcastQueueForIntent(intent);
            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, callerPackage,
                    callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType,
                    callerFeatureId, callingPid, callingUid, callerUidState, callerInstantApp,
                    resolvedType,
                    requiredPermissions, excludedPermissions, appOp, brOptions, registeredReceivers,
                    resultTo, resultCode, resultData, resultExtras, ordered, sticky, false, userId,
                    allowBackgroundActivityStarts, backgroundActivityStartsToken,
@@ -13596,7 +13599,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                || resultTo != null) {
            BroadcastQueue queue = broadcastQueueForIntent(intent);
            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, callerPackage,
                    callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType,
                    callerFeatureId, callingPid, callingUid, callerUidState, callerInstantApp,
                    resolvedType,
                    requiredPermissions, excludedPermissions, appOp, brOptions,
                    receivers, resultTo, resultCode, resultData, resultExtras,
                    ordered, sticky, false, userId, allowBackgroundActivityStarts,
+28 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.am;

import static android.app.ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static android.os.Process.ZYGOTE_POLICY_FLAG_EMPTY;
import static android.os.Process.ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE;
import static android.text.TextUtils.formatSimple;
@@ -1656,6 +1658,9 @@ public final class BroadcastQueue {
            scheduleBroadcastsLocked();
            return;
        }

        logSendBroadcastFromCachedState(r, component);

        r.manifestCount++;

        r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED;
@@ -1750,6 +1755,29 @@ public final class BroadcastQueue {
        mPendingBroadcastRecvIndex = recIdx;
    }

    /**
     * Log a WTF message if the broadcast is sent by a process from a cached proc state.
     * This WTF log is to debug background restriction, it will be removed in before final release.
     * @param r the BroadcastRecord.
     * @param component the broadcast's resolved ComponentName.
     */
    private void logSendBroadcastFromCachedState(BroadcastRecord r, ComponentName component) {
        if (r.callerUidState == PROCESS_STATE_NONEXISTENT
                || r.callerUidState < PROCESS_STATE_CACHED_ACTIVITY) {
            return;
        }
        final String msg = "sendBroadcast from cached state"
                + "[callerPackage:" + r.callerPackage
                + "; callingUid:" + r.callingUid
                + "; realCallingUid:" + r.callingUid
                + "; uidState:" + ProcessList.makeProcStateString(r.callerUidState)
                + "; intent:" + r.intent
                + "; component:" + component.flattenToShortString()
                + ";]";
        Slog.wtfQuiet(TAG, msg);
        Slog.i(TAG, msg);
    }

    private boolean noteOpForManifestReceiver(int appOp, BroadcastRecord r, ResolveInfo info,
            ComponentName component) {
        if (info.activityInfo.attributionTags == null) {
+6 −2
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ final class BroadcastRecord extends Binder {
    final @Nullable String callerFeatureId; // which feature in the package sent this
    final int callingPid;   // the pid of who sent this
    final int callingUid;   // the uid of who sent this
    final int callerUidState; // the sender's UID state when sent this.
    final boolean callerInstantApp; // caller is an Instant App?
    final boolean ordered;  // serialize the send to receivers?
    final boolean sticky;   // originated from existing sticky data?
@@ -244,7 +245,7 @@ final class BroadcastRecord extends Binder {
    BroadcastRecord(BroadcastQueue _queue,
            Intent _intent, ProcessRecord _callerApp, String _callerPackage,
            @Nullable String _callerFeatureId, int _callingPid, int _callingUid,
            boolean _callerInstantApp, String _resolvedType,
            int _callerUidState, boolean _callerInstantApp, String _resolvedType,
            String[] _requiredPermissions, String[] _excludedPermissions, int _appOp,
            BroadcastOptions _options, List _receivers, IIntentReceiver _resultTo, int _resultCode,
            String _resultData, Bundle _resultExtras, boolean _serialized, boolean _sticky,
@@ -261,6 +262,7 @@ final class BroadcastRecord extends Binder {
        callerFeatureId = _callerFeatureId;
        callingPid = _callingPid;
        callingUid = _callingUid;
        this.callerUidState = _callerUidState;
        callerInstantApp = _callerInstantApp;
        resolvedType = _resolvedType;
        requiredPermissions = _requiredPermissions;
@@ -298,6 +300,7 @@ final class BroadcastRecord extends Binder {
        callerFeatureId = from.callerFeatureId;
        callingPid = from.callingPid;
        callingUid = from.callingUid;
        callerUidState = from.callerUidState;
        callerInstantApp = from.callerInstantApp;
        ordered = from.ordered;
        sticky = from.sticky;
@@ -362,7 +365,8 @@ final class BroadcastRecord extends Binder {

        // build a new BroadcastRecord around that single-target list
        BroadcastRecord split = new BroadcastRecord(queue, intent, callerApp, callerPackage,
                callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType,
                callerFeatureId, callingPid, callingUid, callerUidState, callerInstantApp,
                resolvedType,
                requiredPermissions, excludedPermissions, appOp, options, splitReceivers, resultTo,
                resultCode, resultData, resultExtras, ordered, sticky, initialSticky, userId,
                allowBackgroundActivityStarts, mBackgroundActivityStartsToken, timeoutExempt);
+4 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.am;

import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;

import static org.junit.Assert.assertNull;

import android.content.Intent;
@@ -181,6 +183,7 @@ public class BroadcastRecordTest {
                null /* callerFeatureId */,
                0 /* callingPid */,
                0 /* callingUid */,
                PROCESS_STATE_NONEXISTENT,
                false /* callerInstantApp */,
                null /* resolvedType */,
                null /* requiredPermissions */,