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

Commit def1b90d authored by Olivier Gaillard's avatar Olivier Gaillard
Browse files

Use the BinderProxy#TransactListener to propagate the UID.

PropagateWorkSourceTransactListener intercepts outgoing calls and calls
Binde#setThreadWorkSource.

Also install the listener to system server to propagate the worksource
through binder calls.

Test: manual
Change-Id: I02e88c93eebdf200691dd72b79aa7648f4d85bcb
parent 510cdfc3
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -576,6 +576,39 @@ public class Binder implements IBinder {
        void onTransactEnded(@Nullable Object session);
    }

    /**
     * Propagates the work source to binder calls executed by the system server.
     *
     * <li>By default, this listener will propagate the worksource if the outgoing call happens on
     * the same thread as the incoming binder call.
     * <li>Custom attribution can be done by calling {@link ThreadLocalWorkSourceUid#set(int)}.
     * @hide
     */
    public static class PropagateWorkSourceTransactListener implements ProxyTransactListener {
        @Override
        public Object onTransactStarted(IBinder binder, int transactionCode) {
           // Note that {@link Binder#getCallingUid()} is already set to the UID of the current
           // process when this method is called.
           //
           // We use ThreadLocalWorkSourceUid instead. It also allows feature owners to set
           // {@link ThreadLocalWorkSourceUid#set(int) manually to attribute resources to a UID.
            int uid = ThreadLocalWorkSourceUid.get();
            if (uid >= 0) {
                int originalUid = Binder.setThreadWorkSource(uid);
                return Integer.valueOf(originalUid);
            }
            return null;
        }

        @Override
        public void onTransactEnded(Object session) {
            if (session != null) {
                int uid = (int) session;
                Binder.setThreadWorkSource(uid);
            }
        }
    }

    /**
     * Sets a listener for the transact method on the proxy-side.
     *
+9 −1
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ public class BinderCallsStatsService extends Binder {
    private static final String PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING
            = "persist.sys.binder_calls_detailed_tracking";


    /** Listens for flag changes. */
    private static class SettingsObserver extends ContentObserver {
        private static final String SETTINGS_ENABLED_KEY = "enabled";
@@ -101,7 +102,14 @@ public class BinderCallsStatsService extends Binder {
            final boolean enabled =
                    mParser.getBoolean(SETTINGS_ENABLED_KEY, BinderCallsStats.ENABLED_DEFAULT);
            if (mEnabled != enabled) {
                Binder.setObserver(enabled ? mBinderCallsStats : null);
                if (enabled) {
                    Binder.setObserver(mBinderCallsStats);
                    Binder.setProxyTransactListener(
                            new Binder.PropagateWorkSourceTransactListener());
                } else {
                    Binder.setObserver(null);
                    Binder.setProxyTransactListener(null);
                }
                mEnabled = enabled;
                mBinderCallsStats.reset();
            }