1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.os;
18 
19 import android.annotation.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Binder;
22 import android.os.Handler;
23 import android.os.IBinder;
24 import android.os.SystemClock;
25 import android.util.EventLog;
26 import android.util.SparseIntArray;
27 
28 import com.android.internal.util.Preconditions;
29 
30 import dalvik.system.VMRuntime;
31 
32 import java.lang.ref.WeakReference;
33 import java.util.ArrayList;
34 
35 /**
36  * Private and debugging Binder APIs.
37  *
38  * @see IBinder
39  */
40 public class BinderInternal {
41     private static final String TAG = "BinderInternal";
42     static WeakReference<GcWatcher> sGcWatcher
43             = new WeakReference<GcWatcher>(new GcWatcher());
44     static ArrayList<Runnable> sGcWatchers = new ArrayList<>();
45     static Runnable[] sTmpWatchers = new Runnable[1];
46     static long sLastGcTime;
47     static final BinderProxyLimitListenerDelegate sBinderProxyLimitListenerDelegate =
48             new BinderProxyLimitListenerDelegate();
49 
50     static final class GcWatcher {
51         @Override
finalize()52         protected void finalize() throws Throwable {
53             handleGc();
54             sLastGcTime = SystemClock.uptimeMillis();
55             synchronized (sGcWatchers) {
56                 sTmpWatchers = sGcWatchers.toArray(sTmpWatchers);
57             }
58             for (int i=0; i<sTmpWatchers.length; i++) {
59                 if (sTmpWatchers[i] != null) {
60                     sTmpWatchers[i].run();
61                 }
62             }
63             sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher());
64         }
65     }
66 
addGcWatcher(Runnable watcher)67     public static void addGcWatcher(Runnable watcher) {
68         synchronized (sGcWatchers) {
69             sGcWatchers.add(watcher);
70         }
71     }
72 
73     /**
74      * A session used by {@link Observer} in order to keep track of some data.
75      */
76     public static class CallSession {
77         // Binder interface descriptor.
78         public Class<? extends Binder> binderClass;
79         // Binder transaction code.
80         public int transactionCode;
81         // CPU time at the beginning of the call.
82         long cpuTimeStarted;
83         // System time at the beginning of the call.
84         long timeStarted;
85         // Should be set to one when an exception is thrown.
86         boolean exceptionThrown;
87     }
88 
89 
90     /**
91      * Responsible for resolving a work source.
92      */
93     @FunctionalInterface
94     public interface WorkSourceProvider {
95         /**
96          * <p>This method is called in a critical path of the binder transaction.
97          * <p>The implementation should never execute a binder call since it is called during a
98          * binder transaction.
99          *
100          * @param untrustedWorkSourceUid The work source set by the caller.
101          * @return the uid of the process to attribute the binder transaction to.
102          */
resolveWorkSourceUid(int untrustedWorkSourceUid)103         int resolveWorkSourceUid(int untrustedWorkSourceUid);
104     }
105 
106     /**
107      * Allows to track various steps of an API call.
108      */
109     public interface Observer {
110         /**
111          * Called when a binder call starts.
112          *
113          * @return a CallSession to pass to the callEnded method.
114          */
callStarted(Binder binder, int code, int workSourceUid)115         CallSession callStarted(Binder binder, int code, int workSourceUid);
116 
117         /**
118          * Called when a binder call stops.
119          *
120          * <li>This method will be called even when an exception is thrown by the binder stub
121          * implementation.
122          */
callEnded(CallSession s, int parcelRequestSize, int parcelReplySize, int workSourceUid)123         void callEnded(CallSession s, int parcelRequestSize, int parcelReplySize,
124                 int workSourceUid);
125 
126         /**
127          * Called if an exception is thrown while executing the binder transaction.
128          *
129          * <li>BinderCallsStats#callEnded will be called afterwards.
130          * <li>Do not throw an exception in this method, it will swallow the original exception
131          * thrown by the binder transaction.
132          */
callThrewException(CallSession s, Exception exception)133         public void callThrewException(CallSession s, Exception exception);
134     }
135 
136     /**
137      * Add the calling thread to the IPC thread pool.  This function does
138      * not return until the current process is exiting.
139      */
joinThreadPool()140     public static final native void joinThreadPool();
141 
142     /**
143      * Return the system time (as reported by {@link SystemClock#uptimeMillis
144      * SystemClock.uptimeMillis()}) that the last garbage collection occurred
145      * in this process.  This is not for general application use, and the
146      * meaning of "when a garbage collection occurred" will change as the
147      * garbage collector evolves.
148      *
149      * @return Returns the time as per {@link SystemClock#uptimeMillis
150      * SystemClock.uptimeMillis()} of the last garbage collection.
151      */
getLastGcTime()152     public static long getLastGcTime() {
153         return sLastGcTime;
154     }
155 
156     /**
157      * Return the global "context object" of the system.  This is usually
158      * an implementation of IServiceManager, which you can use to find
159      * other services.
160      */
161     @UnsupportedAppUsage
getContextObject()162     public static final native IBinder getContextObject();
163 
164     /**
165      * Special for system process to not allow incoming calls to run at
166      * background scheduling priority.
167      * @hide
168      */
disableBackgroundScheduling(boolean disable)169     public static final native void disableBackgroundScheduling(boolean disable);
170 
setMaxThreads(int numThreads)171     public static final native void setMaxThreads(int numThreads);
172 
173     @UnsupportedAppUsage
handleGc()174     static native final void handleGc();
175 
forceGc(String reason)176     public static void forceGc(String reason) {
177         EventLog.writeEvent(2741, reason);
178         VMRuntime.getRuntime().requestConcurrentGC();
179     }
180 
forceBinderGc()181     static void forceBinderGc() {
182         forceGc("Binder");
183     }
184 
185     /**
186      * Enable/disable Binder Proxy Instance Counting by Uid. While enabled, the set callback will
187      * be called if this process holds too many Binder Proxies on behalf of a Uid.
188      * @param enabled true to enable counting, false to disable
189      */
nSetBinderProxyCountEnabled(boolean enabled)190     public static final native void nSetBinderProxyCountEnabled(boolean enabled);
191 
192     /**
193      * Get the current number of Binder Proxies held for each uid.
194      * @return SparseIntArray mapping uids to the number of Binder Proxies currently held
195      */
nGetBinderProxyPerUidCounts()196     public static final native SparseIntArray nGetBinderProxyPerUidCounts();
197 
198     /**
199      * Get the current number of Binder Proxies held for an individual uid.
200      * @param uid Requested uid for Binder Proxy count
201      * @return int with the number of Binder proxies held for a uid
202      */
nGetBinderProxyCount(int uid)203     public static final native int nGetBinderProxyCount(int uid);
204 
205     /**
206      * Set the Binder Proxy watermarks. Default high watermark = 2500. Default low watermark = 2000
207      * @param high  The limit at which the BinderProxyListener callback will be called.
208      * @param low   The threshold a binder count must drop below before the callback
209      *              can be called again. (This is to avoid many repeated calls to the
210      *              callback in a brief period of time)
211      */
nSetBinderProxyCountWatermarks(int high, int low)212     public static final native void nSetBinderProxyCountWatermarks(int high, int low);
213 
214     /**
215      * Interface for callback invocation when the Binder Proxy limit is reached. onLimitReached will
216      * be called with the uid of the app causing too many Binder Proxies
217      */
218     public interface BinderProxyLimitListener {
onLimitReached(int uid)219         public void onLimitReached(int uid);
220     }
221 
222     /**
223      * Callback used by native code to trigger a callback in java code. The callback will be
224      * triggered when too many binder proxies from a uid hits the allowed limit.
225      * @param uid The uid of the bad behaving app sending too many binders
226      */
binderProxyLimitCallbackFromNative(int uid)227     public static void binderProxyLimitCallbackFromNative(int uid) {
228        sBinderProxyLimitListenerDelegate.notifyClient(uid);
229     }
230 
231     /**
232      * Set a callback to be triggered when a uid's Binder Proxy limit is reached for this process.
233      * @param listener OnLimitReached of listener will be called in the thread provided by handler
234      * @param handler must not be null, callback will be posted through the handler;
235      *
236      */
setBinderProxyCountCallback(BinderProxyLimitListener listener, @NonNull Handler handler)237     public static void setBinderProxyCountCallback(BinderProxyLimitListener listener,
238             @NonNull Handler handler) {
239         Preconditions.checkNotNull(handler,
240                 "Must provide NonNull Handler to setBinderProxyCountCallback when setting "
241                         + "BinderProxyLimitListener");
242         sBinderProxyLimitListenerDelegate.setListener(listener, handler);
243     }
244 
245     /**
246      * Clear the Binder Proxy callback
247      */
clearBinderProxyCountCallback()248     public static void clearBinderProxyCountCallback() {
249         sBinderProxyLimitListenerDelegate.setListener(null, null);
250     }
251 
252     static private class BinderProxyLimitListenerDelegate {
253         private BinderProxyLimitListener mBinderProxyLimitListener;
254         private Handler mHandler;
255 
setListener(BinderProxyLimitListener listener, Handler handler)256         void setListener(BinderProxyLimitListener listener, Handler handler) {
257             synchronized (this) {
258                 mBinderProxyLimitListener = listener;
259                 mHandler = handler;
260             }
261         }
262 
notifyClient(final int uid)263         void notifyClient(final int uid) {
264             synchronized (this) {
265                 if (mBinderProxyLimitListener != null) {
266                     mHandler.post(new Runnable() {
267                         @Override
268                         public void run() {
269                             mBinderProxyLimitListener.onLimitReached(uid);
270                         }
271                     });
272                 }
273             }
274         }
275     }
276 }
277