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

Commit 2fedccd8 authored by Jing Ji's avatar Jing Ji
Browse files

Revert "Split attachApplication to more accurately identify app startup"

This reverts commit 331ef23f.

Reason for revert: b/261638650

Bug: 261638650
Test: Treehugger
Change-Id: I0890f8f25d5f522dbab9a9af0554e8565ae39379
parent 2800296d
Loading
Loading
Loading
Loading
+0 −9
Original line number Diff line number Diff line
@@ -502,7 +502,6 @@ public final class ActivityThread extends ClientTransactionHandler

    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    static volatile Handler sMainThreadHandler;  // set once in main()
    private long mStartSeq; // Only accesssed from the main thread

    Bundle mCoreSettings = null;

@@ -6739,12 +6738,6 @@ public final class ActivityThread extends ClientTransactionHandler
        }

        final IActivityManager mgr = ActivityManager.getService();
        try {
            mgr.finishAttachApplication(mStartSeq);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }

        final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
        mConfigurationController.updateLocaleListFromAppContext(appContext);

@@ -7675,8 +7668,6 @@ public final class ActivityThread extends ClientTransactionHandler
        sCurrentActivityThread = this;
        mConfigurationController = new ConfigurationController(this);
        mSystemThread = system;
        mStartSeq = startSeq;

        if (!system) {
            android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
                                                    UserHandle.myUserId());
+2 −3
Original line number Diff line number Diff line
@@ -147,7 +147,6 @@ interface IActivityManager {
    oneway void finishReceiver(in IBinder who, int resultCode, in String resultData, in Bundle map,
            boolean abortBroadcast, int flags);
    void attachApplication(in IApplicationThread app, long startSeq);
    void finishAttachApplication(long startSeq);
    List<ActivityManager.RunningTaskInfo> getTasks(int maxNum);
    @UnsupportedAppUsage
    void moveTaskToFront(in IApplicationThread caller, in String callingPackage, int task,
+115 −156
Original line number Diff line number Diff line
@@ -87,7 +87,6 @@ import static android.os.Process.getTotalMemory;
import static android.os.Process.isSdkSandboxUid;
import static android.os.Process.isThreadInProcess;
import static android.os.Process.killProcess;
import static android.os.Process.killProcessGroup;
import static android.os.Process.killProcessQuiet;
import static android.os.Process.myPid;
import static android.os.Process.myUid;
@@ -961,6 +960,13 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            return false;
        }
        boolean doRemoveIfNoThreadInternal(int pid, ProcessRecord app) {
            if (app == null || app.getThread() != null) {
                return false;
            }
            return doRemoveInternal(pid, app);
        }
    }
    private final PendingStartActivityUids mPendingStartActivityUids;
@@ -992,7 +998,7 @@ public class ActivityManagerService extends IActivityManager.Stub
     * method.
     */
    @GuardedBy("this")
    boolean removePidLocked(int pid, ProcessRecord app) {
    void removePidLocked(int pid, ProcessRecord app) {
        final boolean removed;
        synchronized (mPidsSelfLocked) {
            removed = mPidsSelfLocked.doRemoveInternal(pid, app);
@@ -1003,6 +1009,26 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            mAtmInternal.onProcessUnMapped(pid);
        }
    }
    /**
     * Removes the process record from the map if it doesn't have a thread.
     * <p>NOTE: Callers should avoid acquiring the mPidsSelfLocked lock before calling this
     * method.
     */
    @GuardedBy("this")
    private boolean removePidIfNoThreadLocked(ProcessRecord app) {
        final boolean removed;
        final int pid = app.getPid();
        synchronized (mPidsSelfLocked) {
            removed = mPidsSelfLocked.doRemoveIfNoThreadInternal(pid, app);
        }
        if (removed) {
            synchronized (sActiveProcessInfoSelfLocked) {
                sActiveProcessInfoSelfLocked.remove(pid);
            }
            mAtmInternal.onProcessUnMapped(pid);
        }
        return removed;
    }
@@ -2350,7 +2376,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        mAppErrors = null;
        mPackageWatchdog = null;
        mAppOpsService = mInjector.getAppOpsService(null /* file */, null /* handler */);
        mBatteryStatsService = mInjector.getBatteryStatsService();
        mBatteryStatsService = null;
        mHandler = new MainHandler(handlerThread.getLooper());
        mHandlerThread = handlerThread;
        mConstants = new ActivityManagerConstants(mContext, this, mHandler);
@@ -2365,7 +2391,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        mIntentFirewall = null;
        mProcessStats = new ProcessStatsService(this, mContext.getCacheDir());
        mCpHelper = new ContentProviderHelper(this, false);
        mServices = mInjector.getActiveServices(this);
        mServices = null;
        mSystemThread = null;
        mUiHandler = injector.getUiHandler(null /* service */);
        mUidObserverController = new UidObserverController(mUiHandler);
@@ -4757,7 +4783,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    @GuardedBy("this")
    void handleProcessStartOrKillTimeoutLocked(ProcessRecord app, boolean isKillTimeout) {
        final int pid = app.getPid();
        boolean gone = isKillTimeout || removePidLocked(pid, app);
        boolean gone = isKillTimeout || removePidIfNoThreadLocked(app);
        if (gone) {
            if (isKillTimeout) {
@@ -4838,7 +4864,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    }
    @GuardedBy("this")
    private void attachApplicationLocked(@NonNull IApplicationThread thread,
    private boolean attachApplicationLocked(@NonNull IApplicationThread thread,
            int pid, int callingUid, long startSeq) {
        // Find the application record that is being attached...  either via
@@ -4903,7 +4929,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    // Ignore exceptions.
                }
            }
            return;
            return false;
        }
        // If this application record is still attached to a previous
@@ -4928,7 +4954,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            mProcessList.startProcessLocked(app,
                    new HostingRecord(HostingRecord.HOSTING_TYPE_LINK_FAIL, processName),
                    ZYGOTE_POLICY_FLAG_EMPTY);
            return;
            return false;
        }
        EventLogTags.writeAmProcBound(app.userId, pid, app.processName);
@@ -4951,6 +4977,8 @@ public class ActivityManagerService extends IActivityManager.Stub
            app.setUnlocked(StorageManager.isUserKeyUnlocked(app.userId));
        }
        mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
        boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
        List<ProviderInfo> providers = normalMode
                                            ? mCpHelper.generateApplicationProvidersLocked(app)
@@ -5116,7 +5144,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            app.killLocked("error during bind", ApplicationExitInfo.REASON_INITIALIZATION_FAILURE,
                    true);
            handleAppDiedLocked(app, pid, false, true, false /* fromBinderDied */);
            return;
            return false;
        }
        // Remove this record from the list of starting applications.
@@ -5124,53 +5152,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG_PROCESSES,
                "Attach application locked removing on hold: " + app);
        mProcessesOnHold.remove(app);
    }
    @Override
    public final void attachApplication(IApplicationThread thread, long startSeq) {
        if (thread == null) {
            throw new SecurityException("Invalid application interface");
        }
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final int callingUid = Binder.getCallingUid();
            final long origId = Binder.clearCallingIdentity();
            attachApplicationLocked(thread, callingPid, callingUid, startSeq);
            Binder.restoreCallingIdentity(origId);
        }
    }
    private void finishAttachApplicationInner(long startSeq, int uid, int pid) {
        final long startTime = SystemClock.uptimeMillis();
        // Find the application record that is being attached...  either via
        // the pid if we are running in multiple processes, or just pull the
        // next app record if we are emulating process with anonymous threads.
        final ProcessRecord app;
        synchronized (mPidsSelfLocked) {
            app = mPidsSelfLocked.get(pid);
        }
        if (app != null && app.getStartUid() == uid && app.getStartSeq() == startSeq) {
            mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
        } else {
            Slog.wtf(TAG, "Mismatched or missing ProcessRecord: " + app + ". Pid: " + pid
                    + ". Uid: " + uid);
            killProcess(pid);
            killProcessGroup(uid, pid);
            mProcessList.noteAppKill(pid, uid,
                    ApplicationExitInfo.REASON_INITIALIZATION_FAILURE,
                    ApplicationExitInfo.SUBREASON_UNKNOWN,
                    "wrong startSeq");
            synchronized (this) {
                app.killLocked("unexpected process record",
                        ApplicationExitInfo.REASON_OTHER, true);
            }
            return;
        }
        synchronized (this) {
            final boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
            final String processName = app.processName;
        boolean badApp = false;
        boolean didSomething = false;
@@ -5188,8 +5170,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (!badApp) {
            try {
                didSomething |= mServices.attachApplicationLocked(app, processName);
                    checkTime(startTime, "finishAttachApplicationInner: "
                            + "after mServices.attachApplicationLocked");
                checkTime(startTime, "attachApplicationLocked: after mServices.attachApplicationLocked");
            } catch (Exception e) {
                Slog.wtf(TAG, "Exception thrown starting services in " + app, e);
                badApp = true;
@@ -5202,8 +5183,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                for (BroadcastQueue queue : mBroadcastQueues) {
                    didSomething |= queue.onApplicationAttachedLocked(app);
                }
                    checkTime(startTime, "finishAttachApplicationInner: "
                            + "after dispatching broadcasts");
                checkTime(startTime, "attachApplicationLocked: after dispatching broadcasts");
            } catch (Exception e) {
                // If the app died trying to launch the receiver we declare it 'bad'
                Slog.wtf(TAG, "Exception thrown dispatching broadcasts in " + app, e);
@@ -5212,17 +5192,13 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
        // Check whether the next backup agent is in this process...
            final BackupRecord backupTarget = mBackupTargets.get(app.userId);
        if (!badApp && backupTarget != null && backupTarget.app == app) {
                if (DEBUG_BACKUP) {
                    Slog.v(TAG_BACKUP,
            if (DEBUG_BACKUP) Slog.v(TAG_BACKUP,
                    "New app is backup target, launching agent for " + app);
                }
            notifyPackageUse(backupTarget.appInfo.packageName,
                             PackageManager.NOTIFY_PACKAGE_USE_BACKUP);
            try {
                    app.getThread().scheduleCreateBackupAgent(backupTarget.appInfo,
                thread.scheduleCreateBackupAgent(backupTarget.appInfo,
                        backupTarget.backupMode, backupTarget.userId,
                        backupTarget.backupDestination);
            } catch (Exception e) {
@@ -5232,19 +5208,20 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
        if (badApp) {
                app.killLocked("error during init",
                        ApplicationExitInfo.REASON_INITIALIZATION_FAILURE, true);
            app.killLocked("error during init", ApplicationExitInfo.REASON_INITIALIZATION_FAILURE,
                    true);
            handleAppDiedLocked(app, pid, false, true, false /* fromBinderDied */);
                return;
            return false;
        }
        if (!didSomething) {
            updateOomAdjLocked(app, OomAdjuster.OOM_ADJ_REASON_PROCESS_BEGIN);
                checkTime(startTime, "finishAttachApplicationInner: after updateOomAdjLocked");
            checkTime(startTime, "attachApplicationLocked: after updateOomAdjLocked");
        }
        final HostingRecord hostingRecord = app.getHostingRecord();
            final String shortAction = getShortAction(hostingRecord.getAction());
        String shortAction = getShortAction(hostingRecord.getAction());
        FrameworkStatsLog.write(
                FrameworkStatsLog.PROCESS_START_TIME,
                app.info.uid,
@@ -5252,29 +5229,26 @@ public class ActivityManagerService extends IActivityManager.Stub
                app.info.packageName,
                FrameworkStatsLog.PROCESS_START_TIME__TYPE__COLD,
                app.getStartElapsedTime(),
                    (int) (app.getBindApplicationTime() - app.getStartUptime()),
                (int) (bindApplicationTimeMillis - app.getStartUptime()),
                (int) (SystemClock.uptimeMillis() - app.getStartUptime()),
                hostingRecord.getType(),
                hostingRecord.getName(),
                shortAction,
                HostingRecord.getHostingTypeIdStatsd(hostingRecord.getType()),
                HostingRecord.getTriggerTypeForStatsd(hostingRecord.getTriggerType()));
        }
        return true;
    }
    @Override
    public final void finishAttachApplication(long startSeq) {
        final int pid = Binder.getCallingPid();
        final int uid = Binder.getCallingUid();
        if (pid == MY_PID && uid == SYSTEM_UID) {
            return;
    public final void attachApplication(IApplicationThread thread, long startSeq) {
        if (thread == null) {
            throw new SecurityException("Invalid application interface");
        }
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final int callingUid = Binder.getCallingUid();
            final long origId = Binder.clearCallingIdentity();
        try {
            finishAttachApplicationInner(startSeq, uid, pid);
        } finally {
            attachApplicationLocked(thread, callingPid, callingUid, startSeq);
            Binder.restoreCallingIdentity(origId);
        }
    }
@@ -18919,21 +18893,6 @@ public class ActivityManagerService extends IActivityManager.Stub
            return new ProcessList();
        }
        /**
         * Returns the {@link BatteryStatsService} instance
         */
        public BatteryStatsService getBatteryStatsService() {
            return new BatteryStatsService(mContext, SystemServiceManager.ensureSystemDir(),
                BackgroundThread.get().getHandler());
        }
        /**
         * Returns the {@link ActiveServices} instance
         */
        public ActiveServices getActiveServices(ActivityManagerService service) {
            return new ActiveServices(service);
        }
        private boolean ensureHasNetworkManagementInternal() {
            if (mNmi == null) {
                mNmi = LocalServices.getService(NetworkManagementInternal.class);
+1 −1
Original line number Diff line number Diff line
@@ -2508,7 +2508,7 @@ public final class ProcessList {
    }

    @GuardedBy("mService")
    String isProcStartValidLocked(ProcessRecord app, long expectedStartSeq) {
    private String isProcStartValidLocked(ProcessRecord app, long expectedStartSeq) {
        StringBuilder sb = null;
        if (app.isKilledByAm()) {
            if (sb == null) sb = new StringBuilder();
+0 −9
Original line number Diff line number Diff line
@@ -199,11 +199,6 @@ class ProcessRecord implements WindowProcessListener {
     */
    private volatile long mStartElapsedTime;

    /**
     * When the process was sent the bindApplication request
     */
    private volatile long mBindApplicationTime;

    /**
     * This will be same as {@link #uid} usually except for some apps used during factory testing.
     */
@@ -744,10 +739,6 @@ class ProcessRecord implements WindowProcessListener {
        return mStartElapsedTime;
    }

    long getBindApplicationTime() {
        return mBindApplicationTime;
    }

    int getStartUid() {
        return mStartUid;
    }
Loading