blob: abc6c4b5e7cc0829d2ed82a065e0218ec7d1f82d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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#define LOG_TAG "JavaBinder"
18//#define LOG_NDEBUG 0
19
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080020#include "android_os_Parcel.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "android_util_Binder.h"
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include "JNIHelp.h"
24
25#include <fcntl.h>
Mark Salyzyncfd91e72014-04-17 15:40:01 -070026#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <stdio.h>
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -070028#include <sys/stat.h>
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -070029#include <sys/types.h>
Brad Fitzpatrick8f26b322010-03-25 00:25:37 -070030#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32#include <utils/Atomic.h>
Mathias Agopian07952722009-05-19 19:08:10 -070033#include <binder/IInterface.h>
34#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/Log.h>
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -070036#include <utils/SystemClock.h>
Christopher Tate0b414482011-02-17 13:00:38 -080037#include <utils/List.h>
38#include <utils/KeyedVector.h>
Colin Cross44bc1862013-07-23 18:17:08 -070039#include <log/logger.h>
Mathias Agopian07952722009-05-19 19:08:10 -070040#include <binder/Parcel.h>
41#include <binder/ProcessState.h>
42#include <binder/IServiceManager.h>
Brad Fitzpatrick5348c012010-03-25 12:43:56 -070043#include <utils/threads.h>
Jeff Brown0bde66a2011-11-07 12:50:08 -080044#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Christopher Tateac5e3502011-08-25 15:48:09 -070046#include <ScopedUtfChars.h>
47#include <ScopedLocalRef.h>
48
Andreas Gampe987f79f2014-11-18 17:29:46 -080049#include "core_jni_helpers.h"
50
Steve Block71f2cf12011-10-20 11:56:00 +010051//#undef ALOGV
52//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Christopher Tate79dd31f2011-03-04 17:45:00 -080054#define DEBUG_DEATH 0
55#if DEBUG_DEATH
Steve Block5baa3a62011-12-20 16:23:08 +000056#define LOGDEATH ALOGD
Christopher Tate79dd31f2011-03-04 17:45:00 -080057#else
Steve Block71f2cf12011-10-20 11:56:00 +010058#define LOGDEATH ALOGV
Christopher Tate79dd31f2011-03-04 17:45:00 -080059#endif
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061using namespace android;
62
63// ----------------------------------------------------------------------------
64
65static struct bindernative_offsets_t
66{
67 // Class state.
68 jclass mClass;
69 jmethodID mExecTransact;
70
71 // Object state.
72 jfieldID mObject;
73
74} gBinderOffsets;
75
76// ----------------------------------------------------------------------------
77
78static struct binderinternal_offsets_t
79{
80 // Class state.
81 jclass mClass;
82 jmethodID mForceGc;
83
84} gBinderInternalOffsets;
85
86// ----------------------------------------------------------------------------
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088static struct error_offsets_t
89{
90 jclass mClass;
91} gErrorOffsets;
92
93// ----------------------------------------------------------------------------
94
95static struct binderproxy_offsets_t
96{
97 // Class state.
98 jclass mClass;
99 jmethodID mConstructor;
100 jmethodID mSendDeathNotice;
101
102 // Object state.
103 jfieldID mObject;
104 jfieldID mSelf;
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800105 jfieldID mOrgue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107} gBinderProxyOffsets;
108
Christopher Tate0d4a7922011-08-30 12:09:43 -0700109static struct class_offsets_t
110{
111 jmethodID mGetName;
112} gClassOffsets;
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114// ----------------------------------------------------------------------------
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116static struct log_offsets_t
117{
118 // Class state.
119 jclass mClass;
120 jmethodID mLogE;
121} gLogOffsets;
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123static struct parcel_file_descriptor_offsets_t
124{
125 jclass mClass;
126 jmethodID mConstructor;
127} gParcelFileDescriptorOffsets;
128
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700129static struct strict_mode_callback_offsets_t
130{
131 jclass mClass;
132 jmethodID mCallback;
133} gStrictModeCallbackOffsets;
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135// ****************************************************************************
136// ****************************************************************************
137// ****************************************************************************
138
139static volatile int32_t gNumRefsCreated = 0;
140static volatile int32_t gNumProxyRefs = 0;
141static volatile int32_t gNumLocalRefs = 0;
142static volatile int32_t gNumDeathRefs = 0;
143
144static void incRefsCreated(JNIEnv* env)
145{
146 int old = android_atomic_inc(&gNumRefsCreated);
147 if (old == 200) {
148 android_atomic_and(0, &gNumRefsCreated);
149 env->CallStaticVoidMethod(gBinderInternalOffsets.mClass,
150 gBinderInternalOffsets.mForceGc);
151 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100152 ALOGV("Now have %d binder ops", old);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
154}
155
156static JavaVM* jnienv_to_javavm(JNIEnv* env)
157{
158 JavaVM* vm;
159 return env->GetJavaVM(&vm) >= 0 ? vm : NULL;
160}
161
162static JNIEnv* javavm_to_jnienv(JavaVM* vm)
163{
164 JNIEnv* env;
165 return vm->GetEnv((void **)&env, JNI_VERSION_1_4) >= 0 ? env : NULL;
166}
167
168static void report_exception(JNIEnv* env, jthrowable excep, const char* msg)
169{
170 env->ExceptionClear();
171
172 jstring tagstr = env->NewStringUTF(LOG_TAG);
Mathieu Chartiercf6775e2014-08-06 13:39:17 -0700173 jstring msgstr = NULL;
174 if (tagstr != NULL) {
175 msgstr = env->NewStringUTF(msg);
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 if ((tagstr == NULL) || (msgstr == NULL)) {
179 env->ExceptionClear(); /* assume exception (OOM?) was thrown */
Steve Block3762c312012-01-06 19:20:56 +0000180 ALOGE("Unable to call Log.e()\n");
181 ALOGE("%s", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 goto bail;
183 }
184
185 env->CallStaticIntMethod(
186 gLogOffsets.mClass, gLogOffsets.mLogE, tagstr, msgstr, excep);
187 if (env->ExceptionCheck()) {
188 /* attempting to log the failure has failed */
Steve Block8564c8d2012-01-05 23:22:43 +0000189 ALOGW("Failed trying to log exception, msg='%s'\n", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 env->ExceptionClear();
191 }
192
193 if (env->IsInstanceOf(excep, gErrorOffsets.mClass)) {
194 /*
195 * It's an Error: Reraise the exception, detach this thread, and
196 * wait for the fireworks. Die even more blatantly after a minute
197 * if the gentler attempt doesn't do the trick.
198 *
199 * The GetJavaVM function isn't on the "approved" list of JNI calls
200 * that can be made while an exception is pending, so we want to
201 * get the VM ptr, throw the exception, and then detach the thread.
202 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 env->Throw(excep);
Mathieu Chartier876b7d8f2015-04-28 16:30:30 -0700204 env->ExceptionDescribe();
Steve Block3762c312012-01-06 19:20:56 +0000205 ALOGE("Forcefully exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 exit(1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208
209bail:
210 /* discard local refs created for us by VM */
211 env->DeleteLocalRef(tagstr);
212 env->DeleteLocalRef(msgstr);
213}
214
215class JavaBBinderHolder;
216
217class JavaBBinder : public BBinder
218{
219public:
220 JavaBBinder(JNIEnv* env, jobject object)
221 : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))
222 {
Steve Block71f2cf12011-10-20 11:56:00 +0100223 ALOGV("Creating JavaBBinder %p\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 android_atomic_inc(&gNumLocalRefs);
225 incRefsCreated(env);
226 }
227
228 bool checkSubclass(const void* subclassID) const
229 {
230 return subclassID == &gBinderOffsets;
231 }
232
233 jobject object() const
234 {
235 return mObject;
236 }
237
238protected:
239 virtual ~JavaBBinder()
240 {
Steve Block71f2cf12011-10-20 11:56:00 +0100241 ALOGV("Destroying JavaBBinder %p\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 android_atomic_dec(&gNumLocalRefs);
243 JNIEnv* env = javavm_to_jnienv(mVM);
244 env->DeleteGlobalRef(mObject);
245 }
246
247 virtual status_t onTransact(
248 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0)
249 {
250 JNIEnv* env = javavm_to_jnienv(mVM);
251
Steve Block71f2cf12011-10-20 11:56:00 +0100252 ALOGV("onTransact() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700254 IPCThreadState* thread_state = IPCThreadState::self();
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700255 const int32_t strict_policy_before = thread_state->getStrictModePolicy();
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 //printf("Transact from %p to Java code sending: ", this);
258 //data.print();
259 //printf("\n");
260 jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact,
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000261 code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags);
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700262
Mathieu Chartier98671c32014-08-20 10:04:08 -0700263 if (env->ExceptionCheck()) {
264 jthrowable excep = env->ExceptionOccurred();
Bjorn Bringert9013ccd2011-04-26 19:10:58 +0100265 report_exception(env, excep,
266 "*** Uncaught remote exception! "
267 "(Exceptions are not yet supported across processes.)");
268 res = JNI_FALSE;
269
270 /* clean up JNI local ref -- we don't return to Java code */
271 env->DeleteLocalRef(excep);
272 }
273
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700274 // Check if the strict mode state changed while processing the
275 // call. The Binder state will be restored by the underlying
276 // Binder system in IPCThreadState, however we need to take care
277 // of the parallel Java state as well.
278 if (thread_state->getStrictModePolicy() != strict_policy_before) {
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700279 set_dalvik_blockguard_policy(env, strict_policy_before);
280 }
281
Mathieu Chartier98671c32014-08-20 10:04:08 -0700282 if (env->ExceptionCheck()) {
283 jthrowable excep = env->ExceptionOccurred();
284 report_exception(env, excep,
Bjorn Bringert9013ccd2011-04-26 19:10:58 +0100285 "*** Uncaught exception in onBinderStrictModePolicyChange");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /* clean up JNI local ref -- we don't return to Java code */
Mathieu Chartier98671c32014-08-20 10:04:08 -0700287 env->DeleteLocalRef(excep);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
Dianne Hackborna53de062012-05-08 18:53:51 -0700290 // Need to always call through the native implementation of
291 // SYSPROPS_TRANSACTION.
292 if (code == SYSPROPS_TRANSACTION) {
293 BBinder::onTransact(code, data, reply, flags);
294 }
295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 //aout << "onTransact to Java code; result=" << res << endl
297 // << "Transact from " << this << " to Java code returning "
298 // << reply << ": " << *reply << endl;
299 return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION;
300 }
301
302 virtual status_t dump(int fd, const Vector<String16>& args)
303 {
304 return 0;
305 }
306
307private:
308 JavaVM* const mVM;
309 jobject const mObject;
310};
311
312// ----------------------------------------------------------------------------
313
314class JavaBBinderHolder : public RefBase
315{
316public:
Christopher Tate0b414482011-02-17 13:00:38 -0800317 sp<JavaBBinder> get(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 {
319 AutoMutex _l(mLock);
320 sp<JavaBBinder> b = mBinder.promote();
321 if (b == NULL) {
Christopher Tate0b414482011-02-17 13:00:38 -0800322 b = new JavaBBinder(env, obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mBinder = b;
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700324 ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
Christopher Tate0b414482011-02-17 13:00:38 -0800325 b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327
328 return b;
329 }
330
331 sp<JavaBBinder> getExisting()
332 {
333 AutoMutex _l(mLock);
334 return mBinder.promote();
335 }
336
337private:
338 Mutex mLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 wp<JavaBBinder> mBinder;
340};
341
342// ----------------------------------------------------------------------------
343
Christopher Tate0b414482011-02-17 13:00:38 -0800344// Per-IBinder death recipient bookkeeping. This is how we reconcile local jobject
345// death recipient references passed in through JNI with the permanent corresponding
346// JavaDeathRecipient objects.
347
348class JavaDeathRecipient;
349
350class DeathRecipientList : public RefBase {
351 List< sp<JavaDeathRecipient> > mList;
352 Mutex mLock;
353
354public:
Christopher Tate79dd31f2011-03-04 17:45:00 -0800355 DeathRecipientList();
Christopher Tate0b414482011-02-17 13:00:38 -0800356 ~DeathRecipientList();
357
358 void add(const sp<JavaDeathRecipient>& recipient);
359 void remove(const sp<JavaDeathRecipient>& recipient);
360 sp<JavaDeathRecipient> find(jobject recipient);
Christopher Tate090c08f2015-05-19 18:16:58 -0700361
362 Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death
Christopher Tate0b414482011-02-17 13:00:38 -0800363};
364
365// ----------------------------------------------------------------------------
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367class JavaDeathRecipient : public IBinder::DeathRecipient
368{
369public:
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800370 JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list)
Christopher Tate86284c62011-08-17 15:19:29 -0700371 : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)),
372 mObjectWeak(NULL), mList(list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 {
Christopher Tate0b414482011-02-17 13:00:38 -0800374 // These objects manage their own lifetimes so are responsible for final bookkeeping.
375 // The list holds a strong reference to this object.
Christopher Tate79dd31f2011-03-04 17:45:00 -0800376 LOGDEATH("Adding JDR %p to DRL %p", this, list.get());
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800377 list->add(this);
Christopher Tate0b414482011-02-17 13:00:38 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 android_atomic_inc(&gNumDeathRefs);
380 incRefsCreated(env);
381 }
382
383 void binderDied(const wp<IBinder>& who)
384 {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800385 LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this);
Christopher Tate86284c62011-08-17 15:19:29 -0700386 if (mObject != NULL) {
387 JNIEnv* env = javavm_to_jnienv(mVM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
Christopher Tate86284c62011-08-17 15:19:29 -0700389 env->CallStaticVoidMethod(gBinderProxyOffsets.mClass,
390 gBinderProxyOffsets.mSendDeathNotice, mObject);
Mathieu Chartier98671c32014-08-20 10:04:08 -0700391 if (env->ExceptionCheck()) {
392 jthrowable excep = env->ExceptionOccurred();
Christopher Tate86284c62011-08-17 15:19:29 -0700393 report_exception(env, excep,
394 "*** Uncaught exception returned from death notification!");
395 }
396
Christopher Tate090c08f2015-05-19 18:16:58 -0700397 // Serialize with our containing DeathRecipientList so that we can't
398 // delete the global ref on mObject while the list is being iterated.
399 sp<DeathRecipientList> list = mList.promote();
400 if (list != NULL) {
401 AutoMutex _l(list->lock());
402
403 // Demote from strong ref to weak after binderDied() has been delivered,
404 // to allow the DeathRecipient and BinderProxy to be GC'd if no longer needed.
405 mObjectWeak = env->NewWeakGlobalRef(mObject);
406 env->DeleteGlobalRef(mObject);
407 mObject = NULL;
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
412 void clearReference()
413 {
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800414 sp<DeathRecipientList> list = mList.promote();
415 if (list != NULL) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800416 LOGDEATH("Removing JDR %p from DRL %p", this, list.get());
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800417 list->remove(this);
Christopher Tate79dd31f2011-03-04 17:45:00 -0800418 } else {
419 LOGDEATH("clearReference() on JDR %p but DRL wp purged", this);
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800420 }
Christopher Tate0b414482011-02-17 13:00:38 -0800421 }
422
423 bool matches(jobject obj) {
Christopher Tate86284c62011-08-17 15:19:29 -0700424 bool result;
Christopher Tate0b414482011-02-17 13:00:38 -0800425 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate86284c62011-08-17 15:19:29 -0700426
427 if (mObject != NULL) {
428 result = env->IsSameObject(obj, mObject);
429 } else {
430 jobject me = env->NewLocalRef(mObjectWeak);
431 result = env->IsSameObject(obj, me);
432 env->DeleteLocalRef(me);
433 }
434 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
Christopher Tateac5e3502011-08-25 15:48:09 -0700437 void warnIfStillLive() {
438 if (mObject != NULL) {
439 // Okay, something is wrong -- we have a hard reference to a live death
440 // recipient on the VM side, but the list is being torn down.
441 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate0d4a7922011-08-30 12:09:43 -0700442 ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject));
443 ScopedLocalRef<jstring> nameRef(env,
444 (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName));
445 ScopedUtfChars nameUtf(env, nameRef.get());
446 if (nameUtf.c_str() != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000447 ALOGW("BinderProxy is being destroyed but the application did not call "
Christopher Tate0d4a7922011-08-30 12:09:43 -0700448 "unlinkToDeath to unlink all of its death recipients beforehand. "
449 "Releasing leaked death recipient: %s", nameUtf.c_str());
450 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000451 ALOGW("BinderProxy being destroyed; unable to get DR object name");
Christopher Tate0d4a7922011-08-30 12:09:43 -0700452 env->ExceptionClear();
453 }
Christopher Tateac5e3502011-08-25 15:48:09 -0700454 }
455 }
456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457protected:
458 virtual ~JavaDeathRecipient()
459 {
Steve Block6215d3f2012-01-04 20:05:49 +0000460 //ALOGI("Removing death ref: recipient=%p\n", mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 android_atomic_dec(&gNumDeathRefs);
462 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate86284c62011-08-17 15:19:29 -0700463 if (mObject != NULL) {
464 env->DeleteGlobalRef(mObject);
465 } else {
466 env->DeleteWeakGlobalRef(mObjectWeak);
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
470private:
Christopher Tate86284c62011-08-17 15:19:29 -0700471 JavaVM* const mVM;
472 jobject mObject;
473 jweak mObjectWeak; // will be a weak ref to the same VM-side DeathRecipient after binderDied()
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800474 wp<DeathRecipientList> mList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475};
476
477// ----------------------------------------------------------------------------
478
Christopher Tate79dd31f2011-03-04 17:45:00 -0800479DeathRecipientList::DeathRecipientList() {
480 LOGDEATH("New DRL @ %p", this);
481}
482
Christopher Tate0b414482011-02-17 13:00:38 -0800483DeathRecipientList::~DeathRecipientList() {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800484 LOGDEATH("Destroy DRL @ %p", this);
Christopher Tate0b414482011-02-17 13:00:38 -0800485 AutoMutex _l(mLock);
486
487 // Should never happen -- the JavaDeathRecipient objects that have added themselves
488 // to the list are holding references on the list object. Only when they are torn
489 // down can the list header be destroyed.
490 if (mList.size() > 0) {
Christopher Tateac5e3502011-08-25 15:48:09 -0700491 List< sp<JavaDeathRecipient> >::iterator iter;
492 for (iter = mList.begin(); iter != mList.end(); iter++) {
493 (*iter)->warnIfStillLive();
494 }
Christopher Tate0b414482011-02-17 13:00:38 -0800495 }
496}
497
498void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) {
499 AutoMutex _l(mLock);
500
Christopher Tate79dd31f2011-03-04 17:45:00 -0800501 LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get());
Christopher Tate0b414482011-02-17 13:00:38 -0800502 mList.push_back(recipient);
503}
504
505void DeathRecipientList::remove(const sp<JavaDeathRecipient>& recipient) {
506 AutoMutex _l(mLock);
507
508 List< sp<JavaDeathRecipient> >::iterator iter;
509 for (iter = mList.begin(); iter != mList.end(); iter++) {
510 if (*iter == recipient) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800511 LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get());
Christopher Tate0b414482011-02-17 13:00:38 -0800512 mList.erase(iter);
513 return;
514 }
515 }
516}
517
518sp<JavaDeathRecipient> DeathRecipientList::find(jobject recipient) {
519 AutoMutex _l(mLock);
520
521 List< sp<JavaDeathRecipient> >::iterator iter;
522 for (iter = mList.begin(); iter != mList.end(); iter++) {
523 if ((*iter)->matches(recipient)) {
524 return *iter;
525 }
526 }
527 return NULL;
528}
529
Christopher Tate090c08f2015-05-19 18:16:58 -0700530Mutex& DeathRecipientList::lock() {
531 return mLock;
532}
533
Christopher Tate0b414482011-02-17 13:00:38 -0800534// ----------------------------------------------------------------------------
535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536namespace android {
537
538static void proxy_cleanup(const void* id, void* obj, void* cleanupCookie)
539{
540 android_atomic_dec(&gNumProxyRefs);
541 JNIEnv* env = javavm_to_jnienv((JavaVM*)cleanupCookie);
542 env->DeleteGlobalRef((jobject)obj);
543}
544
545static Mutex mProxyLock;
546
547jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val)
548{
549 if (val == NULL) return NULL;
550
551 if (val->checkSubclass(&gBinderOffsets)) {
552 // One of our own!
553 jobject object = static_cast<JavaBBinder*>(val.get())->object();
Christopher Tate86284c62011-08-17 15:19:29 -0700554 LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 return object;
556 }
557
558 // For the rest of the function we will hold this lock, to serialize
Christopher Tate10c3a282016-02-26 17:48:08 -0800559 // looking/creation/destruction of Java proxies for native Binder proxies.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 AutoMutex _l(mProxyLock);
561
562 // Someone else's... do we know about it?
563 jobject object = (jobject)val->findObject(&gBinderProxyOffsets);
564 if (object != NULL) {
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700565 jobject res = jniGetReferent(env, object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 if (res != NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +0100567 ALOGV("objectForBinder %p: found existing %p!\n", val.get(), res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 return res;
569 }
Christopher Tate86284c62011-08-17 15:19:29 -0700570 LOGDEATH("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 android_atomic_dec(&gNumProxyRefs);
572 val->detachObject(&gBinderProxyOffsets);
573 env->DeleteGlobalRef(object);
574 }
575
576 object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor);
577 if (object != NULL) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800578 LOGDEATH("objectForBinder %p: created new proxy %p !\n", val.get(), object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 // The proxy holds a reference to the native object.
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000580 env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get());
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800581 val->incStrong((void*)javaObjectForIBinder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
583 // The native object needs to hold a weak reference back to the
584 // proxy, so we can retrieve the same proxy if it is still active.
585 jobject refObject = env->NewGlobalRef(
586 env->GetObjectField(object, gBinderProxyOffsets.mSelf));
587 val->attachObject(&gBinderProxyOffsets, refObject,
588 jnienv_to_javavm(env), proxy_cleanup);
589
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800590 // Also remember the death recipients registered on this proxy
591 sp<DeathRecipientList> drl = new DeathRecipientList;
592 drl->incStrong((void*)javaObjectForIBinder);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000593 env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get()));
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 // Note that a new object reference has been created.
596 android_atomic_inc(&gNumProxyRefs);
597 incRefsCreated(env);
598 }
599
600 return object;
601}
602
603sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj)
604{
605 if (obj == NULL) return NULL;
606
607 if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) {
608 JavaBBinderHolder* jbh = (JavaBBinderHolder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000609 env->GetLongField(obj, gBinderOffsets.mObject);
Christopher Tate0b414482011-02-17 13:00:38 -0800610 return jbh != NULL ? jbh->get(env, obj) : NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612
613 if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) {
614 return (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000615 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617
Steve Block8564c8d2012-01-05 23:22:43 +0000618 ALOGW("ibinderForJavaObject: %p is not a Binder object", obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 return NULL;
620}
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc)
623{
624 return env->NewObject(
625 gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc);
626}
627
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800628void set_dalvik_blockguard_policy(JNIEnv* env, jint strict_policy)
629{
630 // Call back into android.os.StrictMode#onBinderStrictModePolicyChange
631 // to sync our state back to it. See the comments in StrictMode.java.
632 env->CallStaticVoidMethod(gStrictModeCallbackOffsets.mClass,
633 gStrictModeCallbackOffsets.mCallback,
634 strict_policy);
635}
636
637void signalExceptionForError(JNIEnv* env, jobject obj, status_t err,
Dianne Hackborne5c42622015-05-19 16:04:04 -0700638 bool canThrowRemoteException, int parcelSize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639{
640 switch (err) {
641 case UNKNOWN_ERROR:
642 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
643 break;
644 case NO_MEMORY:
645 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
646 break;
647 case INVALID_OPERATION:
648 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
649 break;
650 case BAD_VALUE:
651 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
652 break;
653 case BAD_INDEX:
654 jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL);
655 break;
656 case BAD_TYPE:
657 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
658 break;
659 case NAME_NOT_FOUND:
660 jniThrowException(env, "java/util/NoSuchElementException", NULL);
661 break;
662 case PERMISSION_DENIED:
663 jniThrowException(env, "java/lang/SecurityException", NULL);
664 break;
665 case NOT_ENOUGH_DATA:
666 jniThrowException(env, "android/os/ParcelFormatException", "Not enough data");
667 break;
668 case NO_INIT:
669 jniThrowException(env, "java/lang/RuntimeException", "Not initialized");
670 break;
671 case ALREADY_EXISTS:
672 jniThrowException(env, "java/lang/RuntimeException", "Item already exists");
673 break;
674 case DEAD_OBJECT:
Jeff Brown0bde66a2011-11-07 12:50:08 -0800675 // DeadObjectException is a checked exception, only throw from certain methods.
676 jniThrowException(env, canThrowRemoteException
677 ? "android/os/DeadObjectException"
678 : "java/lang/RuntimeException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 break;
680 case UNKNOWN_TRANSACTION:
681 jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code");
682 break;
Dianne Hackborne5c42622015-05-19 16:04:04 -0700683 case FAILED_TRANSACTION: {
684 ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize);
Christopher Tate02ca7a72015-06-24 18:16:42 -0700685 const char* exceptionToThrow;
Dianne Hackborne5c42622015-05-19 16:04:04 -0700686 char msg[128];
Jeff Brown0bde66a2011-11-07 12:50:08 -0800687 // TransactionTooLargeException is a checked exception, only throw from certain methods.
688 // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION
689 // but it is not the only one. The Binder driver can return BR_FAILED_REPLY
690 // for other reasons also, such as if the transaction is malformed or
691 // refers to an FD that has been closed. We should change the driver
692 // to enable us to distinguish these cases in the future.
Christopher Tate02ca7a72015-06-24 18:16:42 -0700693 if (canThrowRemoteException && parcelSize > 200*1024) {
694 // bona fide large payload
695 exceptionToThrow = "android/os/TransactionTooLargeException";
696 snprintf(msg, sizeof(msg)-1, "data parcel size %d bytes", parcelSize);
697 } else {
698 // Heuristic: a payload smaller than this threshold "shouldn't" be too
699 // big, so it's probably some other, more subtle problem. In practice
Christopher Tateffd58642015-06-29 11:00:15 -0700700 // it seems to always mean that the remote process died while the binder
Christopher Tate02ca7a72015-06-24 18:16:42 -0700701 // transaction was already in flight.
Christopher Tateffd58642015-06-29 11:00:15 -0700702 exceptionToThrow = (canThrowRemoteException)
703 ? "android/os/DeadObjectException"
704 : "java/lang/RuntimeException";
Christopher Tate02ca7a72015-06-24 18:16:42 -0700705 snprintf(msg, sizeof(msg)-1,
706 "Transaction failed on small parcel; remote process probably died");
707 }
708 jniThrowException(env, exceptionToThrow, msg);
Dianne Hackborne5c42622015-05-19 16:04:04 -0700709 } break;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400710 case FDS_NOT_ALLOWED:
711 jniThrowException(env, "java/lang/RuntimeException",
712 "Not allowed to write file descriptors here");
713 break;
Christopher Wileya94fc522015-11-22 14:21:25 -0800714 case UNEXPECTED_NULL:
715 jniThrowNullPointerException(env, NULL);
716 break;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700717 case -EBADF:
718 jniThrowException(env, "java/lang/RuntimeException",
719 "Bad file descriptor");
720 break;
721 case -ENFILE:
722 jniThrowException(env, "java/lang/RuntimeException",
723 "File table overflow");
724 break;
725 case -EMFILE:
726 jniThrowException(env, "java/lang/RuntimeException",
727 "Too many open files");
728 break;
729 case -EFBIG:
730 jniThrowException(env, "java/lang/RuntimeException",
731 "File too large");
732 break;
733 case -ENOSPC:
734 jniThrowException(env, "java/lang/RuntimeException",
735 "No space left on device");
736 break;
737 case -ESPIPE:
738 jniThrowException(env, "java/lang/RuntimeException",
739 "Illegal seek");
740 break;
741 case -EROFS:
742 jniThrowException(env, "java/lang/RuntimeException",
743 "Read-only file system");
744 break;
745 case -EMLINK:
746 jniThrowException(env, "java/lang/RuntimeException",
747 "Too many links");
748 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 default:
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700750 ALOGE("Unknown binder error code. 0x%" PRIx32, err);
Jeff Brown0bde66a2011-11-07 12:50:08 -0800751 String8 msg;
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700752 msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err);
Jeff Brown0bde66a2011-11-07 12:50:08 -0800753 // RemoteException is a checked exception, only throw from certain methods.
754 jniThrowException(env, canThrowRemoteException
755 ? "android/os/RemoteException" : "java/lang/RuntimeException", msg.string());
756 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758}
759
760}
761
762// ----------------------------------------------------------------------------
763
764static jint android_os_Binder_getCallingPid(JNIEnv* env, jobject clazz)
765{
766 return IPCThreadState::self()->getCallingPid();
767}
768
769static jint android_os_Binder_getCallingUid(JNIEnv* env, jobject clazz)
770{
771 return IPCThreadState::self()->getCallingUid();
772}
773
774static jlong android_os_Binder_clearCallingIdentity(JNIEnv* env, jobject clazz)
775{
776 return IPCThreadState::self()->clearCallingIdentity();
777}
778
779static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token)
780{
Dianne Hackborncf3004a2011-03-14 14:24:04 -0700781 // XXX temporary sanity check to debug crashes.
782 int uid = (int)(token>>32);
783 if (uid > 0 && uid < 999) {
784 // In Android currently there are no uids in this range.
785 char buf[128];
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700786 sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token);
Dianne Hackborncf3004a2011-03-14 14:24:04 -0700787 jniThrowException(env, "java/lang/IllegalStateException", buf);
788 return;
789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 IPCThreadState::self()->restoreCallingIdentity(token);
791}
792
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700793static void android_os_Binder_setThreadStrictModePolicy(JNIEnv* env, jobject clazz, jint policyMask)
794{
795 IPCThreadState::self()->setStrictModePolicy(policyMask);
796}
797
798static jint android_os_Binder_getThreadStrictModePolicy(JNIEnv* env, jobject clazz)
799{
800 return IPCThreadState::self()->getStrictModePolicy();
801}
802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz)
804{
805 IPCThreadState::self()->flushCommands();
806}
807
Christopher Tate0b414482011-02-17 13:00:38 -0800808static void android_os_Binder_init(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809{
Christopher Tate0b414482011-02-17 13:00:38 -0800810 JavaBBinderHolder* jbh = new JavaBBinderHolder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 if (jbh == NULL) {
812 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
813 return;
814 }
Steve Block71f2cf12011-10-20 11:56:00 +0100815 ALOGV("Java Binder %p: acquiring first ref on holder %p", obj, jbh);
Christopher Tate0b414482011-02-17 13:00:38 -0800816 jbh->incStrong((void*)android_os_Binder_init);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000817 env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818}
819
Christopher Tate0b414482011-02-17 13:00:38 -0800820static void android_os_Binder_destroy(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821{
822 JavaBBinderHolder* jbh = (JavaBBinderHolder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000823 env->GetLongField(obj, gBinderOffsets.mObject);
Jeff Brown582763a2010-03-24 18:56:57 -0700824 if (jbh != NULL) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000825 env->SetLongField(obj, gBinderOffsets.mObject, 0);
Steve Block71f2cf12011-10-20 11:56:00 +0100826 ALOGV("Java Binder %p: removing ref on holder %p", obj, jbh);
Christopher Tate0b414482011-02-17 13:00:38 -0800827 jbh->decStrong((void*)android_os_Binder_init);
Jeff Brown582763a2010-03-24 18:56:57 -0700828 } else {
829 // Encountering an uninitialized binder is harmless. All it means is that
830 // the Binder was only partially initialized when its finalizer ran and called
831 // destroy(). The Binder could be partially initialized for several reasons.
832 // For example, a Binder subclass constructor might have thrown an exception before
833 // it could delegate to its superclass's constructor. Consequently init() would
834 // not have been called and the holder pointer would remain NULL.
Steve Block71f2cf12011-10-20 11:56:00 +0100835 ALOGV("Java Binder %p: ignoring uninitialized binder", obj);
Jeff Brown582763a2010-03-24 18:56:57 -0700836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837}
838
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700839static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz)
840{
841 return IPCThreadState::self()->blockUntilThreadAvailable();
842}
843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844// ----------------------------------------------------------------------------
845
846static const JNINativeMethod gBinderMethods[] = {
847 /* name, signature, funcPtr */
848 { "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid },
849 { "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid },
850 { "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity },
851 { "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity },
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700852 { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy },
853 { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands },
855 { "init", "()V", (void*)android_os_Binder_init },
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700856 { "destroy", "()V", (void*)android_os_Binder_destroy },
857 { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858};
859
860const char* const kBinderPathName = "android/os/Binder";
861
862static int int_register_android_os_Binder(JNIEnv* env)
863{
Andreas Gampe987f79f2014-11-18 17:29:46 -0800864 jclass clazz = FindClassOrDie(env, kBinderPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
Andreas Gampe987f79f2014-11-18 17:29:46 -0800866 gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
867 gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z");
868 gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800870 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 env, kBinderPathName,
872 gBinderMethods, NELEM(gBinderMethods));
873}
874
875// ****************************************************************************
876// ****************************************************************************
877// ****************************************************************************
878
879namespace android {
880
881jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz)
882{
883 return gNumLocalRefs;
884}
885
886jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz)
887{
888 return gNumProxyRefs;
889}
890
891jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz)
892{
893 return gNumDeathRefs;
894}
895
896}
897
898// ****************************************************************************
899// ****************************************************************************
900// ****************************************************************************
901
902static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
903{
904 sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
905 return javaObjectForIBinder(env, b);
906}
907
908static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz)
909{
910 sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
911 android::IPCThreadState::self()->joinThreadPool();
912}
913
Dianne Hackborn887f3552009-12-07 17:59:37 -0800914static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env,
915 jobject clazz, jboolean disable)
916{
917 IPCThreadState::disableBackgroundScheduling(disable ? true : false);
918}
919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz)
921{
Steve Block71f2cf12011-10-20 11:56:00 +0100922 ALOGV("Gc has executed, clearing binder ops");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 android_atomic_and(0, &gNumRefsCreated);
924}
925
926// ----------------------------------------------------------------------------
927
928static const JNINativeMethod gBinderInternalMethods[] = {
929 /* name, signature, funcPtr */
930 { "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject },
931 { "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool },
Dianne Hackborn887f3552009-12-07 17:59:37 -0800932 { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }
934};
935
936const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal";
937
938static int int_register_android_os_BinderInternal(JNIEnv* env)
939{
Andreas Gampe987f79f2014-11-18 17:29:46 -0800940 jclass clazz = FindClassOrDie(env, kBinderInternalPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941
Andreas Gampe987f79f2014-11-18 17:29:46 -0800942 gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
943 gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800945 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 env, kBinderInternalPathName,
947 gBinderInternalMethods, NELEM(gBinderInternalMethods));
948}
949
950// ****************************************************************************
951// ****************************************************************************
952// ****************************************************************************
953
954static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj)
955{
956 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000957 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 if (target == NULL) {
959 return JNI_FALSE;
960 }
961 status_t err = target->pingBinder();
962 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
963}
964
965static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj)
966{
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000967 IBinder* target = (IBinder*) env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (target != NULL) {
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -0700969 const String16& desc = target->getInterfaceDescriptor();
Dan Albert66987492014-11-20 11:41:21 -0800970 return env->NewString(reinterpret_cast<const jchar*>(desc.string()),
971 desc.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
973 jniThrowException(env, "java/lang/RuntimeException",
974 "No binder found for object");
975 return NULL;
976}
977
978static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj)
979{
980 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000981 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (target == NULL) {
983 return JNI_FALSE;
984 }
985 bool alive = target->isBinderAlive();
986 return alive ? JNI_TRUE : JNI_FALSE;
987}
988
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -0700989static int getprocname(pid_t pid, char *buf, size_t len) {
Sungmin Choiec3d44c2012-12-21 14:24:33 +0900990 char filename[32];
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -0700991 FILE *f;
992
Sungmin Choiec3d44c2012-12-21 14:24:33 +0900993 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -0700994 f = fopen(filename, "r");
Sungmin Choiec3d44c2012-12-21 14:24:33 +0900995 if (!f) {
996 *buf = '\0';
997 return 1;
998 }
999 if (!fgets(buf, len, f)) {
1000 *buf = '\0';
1001 fclose(f);
1002 return 2;
1003 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001004 fclose(f);
1005 return 0;
1006}
1007
1008static bool push_eventlog_string(char** pos, const char* end, const char* str) {
1009 jint len = strlen(str);
1010 int space_needed = 1 + sizeof(len) + len;
1011 if (end - *pos < space_needed) {
Mark Salyzyn5b6da1a2014-04-17 17:25:36 -07001012 ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d",
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001013 end - *pos, space_needed);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001014 return false;
1015 }
1016 **pos = EVENT_TYPE_STRING;
1017 (*pos)++;
1018 memcpy(*pos, &len, sizeof(len));
1019 *pos += sizeof(len);
1020 memcpy(*pos, str, len);
1021 *pos += len;
1022 return true;
1023}
1024
1025static bool push_eventlog_int(char** pos, const char* end, jint val) {
1026 int space_needed = 1 + sizeof(val);
1027 if (end - *pos < space_needed) {
Mark Salyzyn5b6da1a2014-04-17 17:25:36 -07001028 ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d",
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001029 end - *pos, space_needed);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001030 return false;
1031 }
1032 **pos = EVENT_TYPE_INT;
1033 (*pos)++;
1034 memcpy(*pos, &val, sizeof(val));
1035 *pos += sizeof(val);
1036 return true;
1037}
1038
1039// From frameworks/base/core/java/android/content/EventLogTags.logtags:
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001040
1041static const bool kEnableBinderSample = false;
1042
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001043#define LOGTAG_BINDER_OPERATION 52004
1044
1045static void conditionally_log_binder_call(int64_t start_millis,
1046 IBinder* target, jint code) {
1047 int duration_ms = static_cast<int>(uptimeMillis() - start_millis);
1048
1049 int sample_percent;
1050 if (duration_ms >= 500) {
1051 sample_percent = 100;
1052 } else {
1053 sample_percent = 100 * duration_ms / 500;
1054 if (sample_percent == 0) {
1055 return;
1056 }
1057 if (sample_percent < (random() % 100 + 1)) {
1058 return;
1059 }
1060 }
1061
1062 char process_name[40];
1063 getprocname(getpid(), process_name, sizeof(process_name));
1064 String8 desc(target->getInterfaceDescriptor());
1065
1066 char buf[LOGGER_ENTRY_MAX_PAYLOAD];
1067 buf[0] = EVENT_TYPE_LIST;
1068 buf[1] = 5;
1069 char* pos = &buf[2];
1070 char* end = &buf[LOGGER_ENTRY_MAX_PAYLOAD - 1]; // leave room for final \n
1071 if (!push_eventlog_string(&pos, end, desc.string())) return;
1072 if (!push_eventlog_int(&pos, end, code)) return;
1073 if (!push_eventlog_int(&pos, end, duration_ms)) return;
1074 if (!push_eventlog_string(&pos, end, process_name)) return;
1075 if (!push_eventlog_int(&pos, end, sample_percent)) return;
1076 *(pos++) = '\n'; // conventional with EVENT_TYPE_LIST apparently.
1077 android_bWriteLog(LOGTAG_BINDER_OPERATION, buf, pos - buf);
1078}
1079
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001080// We only measure binder call durations to potentially log them if
Elliott Hughes06451fe2014-08-18 10:26:52 -07001081// we're on the main thread.
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001082static bool should_time_binder_calls() {
Elliott Hughes06451fe2014-08-18 10:26:52 -07001083 return (getpid() == gettid());
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001084}
1085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj,
Jeff Brown0bde66a2011-11-07 12:50:08 -08001087 jint code, jobject dataObj, jobject replyObj, jint flags) // throws RemoteException
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088{
1089 if (dataObj == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001090 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 return JNI_FALSE;
1092 }
1093
1094 Parcel* data = parcelForJavaObject(env, dataObj);
1095 if (data == NULL) {
1096 return JNI_FALSE;
1097 }
1098 Parcel* reply = parcelForJavaObject(env, replyObj);
1099 if (reply == NULL && replyObj != NULL) {
1100 return JNI_FALSE;
1101 }
1102
1103 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001104 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (target == NULL) {
1106 jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!");
1107 return JNI_FALSE;
1108 }
1109
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001110 ALOGV("Java code calling transact on %p in Java object %p with code %" PRId32 "\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 target, obj, code);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001112
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001113
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001114 bool time_binder_calls;
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001115 int64_t start_millis;
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001116 if (kEnableBinderSample) {
1117 // Only log the binder call duration for things on the Java-level main thread.
1118 // But if we don't
1119 time_binder_calls = should_time_binder_calls();
1120
1121 if (time_binder_calls) {
1122 start_millis = uptimeMillis();
1123 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001124 }
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 //printf("Transact from Java code to %p sending: ", target); data->print();
1127 status_t err = target->transact(code, *data, reply, flags);
1128 //if (reply) printf("Transact from Java code to %p received: ", target); reply->print();
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001129
1130 if (kEnableBinderSample) {
1131 if (time_binder_calls) {
1132 conditionally_log_binder_call(start_millis, target, code);
1133 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001134 }
1135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 if (err == NO_ERROR) {
1137 return JNI_TRUE;
1138 } else if (err == UNKNOWN_TRANSACTION) {
1139 return JNI_FALSE;
1140 }
1141
Dianne Hackborne5c42622015-05-19 16:04:04 -07001142 signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 return JNI_FALSE;
1144}
1145
1146static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj,
Jeff Brown0bde66a2011-11-07 12:50:08 -08001147 jobject recipient, jint flags) // throws RemoteException
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148{
1149 if (recipient == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001150 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 return;
1152 }
1153
1154 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001155 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 if (target == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001157 ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 assert(false);
1159 }
1160
Christopher Tate79dd31f2011-03-04 17:45:00 -08001161 LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162
1163 if (!target->localBinder()) {
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001164 DeathRecipientList* list = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001165 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001166 sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list);
Christopher Tate0b414482011-02-17 13:00:38 -08001167 status_t err = target->linkToDeath(jdr, NULL, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 if (err != NO_ERROR) {
1169 // Failure adding the death recipient, so clear its reference
1170 // now.
1171 jdr->clearReference();
Jeff Brown0bde66a2011-11-07 12:50:08 -08001172 signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
1175}
1176
1177static jboolean android_os_BinderProxy_unlinkToDeath(JNIEnv* env, jobject obj,
1178 jobject recipient, jint flags)
1179{
1180 jboolean res = JNI_FALSE;
1181 if (recipient == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001182 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 return res;
1184 }
1185
1186 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001187 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 if (target == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001189 ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 return JNI_FALSE;
1191 }
1192
Christopher Tate79dd31f2011-03-04 17:45:00 -08001193 LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194
1195 if (!target->localBinder()) {
Christopher Tate0b414482011-02-17 13:00:38 -08001196 status_t err = NAME_NOT_FOUND;
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001197
1198 // If we find the matching recipient, proceed to unlink using that
1199 DeathRecipientList* list = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001200 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001201 sp<JavaDeathRecipient> origJDR = list->find(recipient);
Christopher Tate79dd31f2011-03-04 17:45:00 -08001202 LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get());
Christopher Tate0b414482011-02-17 13:00:38 -08001203 if (origJDR != NULL) {
1204 wp<IBinder::DeathRecipient> dr;
1205 err = target->unlinkToDeath(origJDR, NULL, flags, &dr);
1206 if (err == NO_ERROR && dr != NULL) {
1207 sp<IBinder::DeathRecipient> sdr = dr.promote();
1208 JavaDeathRecipient* jdr = static_cast<JavaDeathRecipient*>(sdr.get());
1209 if (jdr != NULL) {
1210 jdr->clearReference();
1211 }
1212 }
1213 }
1214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 if (err == NO_ERROR || err == DEAD_OBJECT) {
1216 res = JNI_TRUE;
1217 } else {
1218 jniThrowException(env, "java/util/NoSuchElementException",
1219 "Death link does not exist");
1220 }
1221 }
1222
1223 return res;
1224}
1225
1226static void android_os_BinderProxy_destroy(JNIEnv* env, jobject obj)
1227{
Christopher Tate10c3a282016-02-26 17:48:08 -08001228 // Don't race with construction/initialization
1229 AutoMutex _l(mProxyLock);
1230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 IBinder* b = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001232 env->GetLongField(obj, gBinderProxyOffsets.mObject);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001233 DeathRecipientList* drl = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001234 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tate0b414482011-02-17 13:00:38 -08001235
Christopher Tate79dd31f2011-03-04 17:45:00 -08001236 LOGDEATH("Destroying BinderProxy %p: binder=%p drl=%p\n", obj, b, drl);
Christopher Tate10c3a282016-02-26 17:48:08 -08001237 if (b != nullptr) {
1238 env->SetLongField(obj, gBinderProxyOffsets.mObject, 0);
1239 env->SetLongField(obj, gBinderProxyOffsets.mOrgue, 0);
1240 drl->decStrong((void*)javaObjectForIBinder);
1241 b->decStrong((void*)javaObjectForIBinder);
1242 }
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001243
1244 IPCThreadState::self()->flushCommands();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245}
1246
1247// ----------------------------------------------------------------------------
1248
1249static const JNINativeMethod gBinderProxyMethods[] = {
1250 /* name, signature, funcPtr */
1251 {"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder},
1252 {"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive},
1253 {"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor},
Dianne Hackborn017c6a22014-09-25 17:41:34 -07001254 {"transactNative", "(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void*)android_os_BinderProxy_transact},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 {"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath},
1256 {"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath},
1257 {"destroy", "()V", (void*)android_os_BinderProxy_destroy},
1258};
1259
1260const char* const kBinderProxyPathName = "android/os/BinderProxy";
1261
1262static int int_register_android_os_BinderProxy(JNIEnv* env)
1263{
Andreas Gampe987f79f2014-11-18 17:29:46 -08001264 jclass clazz = FindClassOrDie(env, "java/lang/Error");
1265 gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266
Andreas Gampe987f79f2014-11-18 17:29:46 -08001267 clazz = FindClassOrDie(env, kBinderProxyPathName);
1268 gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1269 gBinderProxyOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
1270 gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice",
1271 "(Landroid/os/IBinder$DeathRecipient;)V");
Elliott Hughes69a017b2011-04-08 14:10:28 -07001272
Andreas Gampe987f79f2014-11-18 17:29:46 -08001273 gBinderProxyOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
1274 gBinderProxyOffsets.mSelf = GetFieldIDOrDie(env, clazz, "mSelf",
1275 "Ljava/lang/ref/WeakReference;");
1276 gBinderProxyOffsets.mOrgue = GetFieldIDOrDie(env, clazz, "mOrgue", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
Andreas Gampe987f79f2014-11-18 17:29:46 -08001278 clazz = FindClassOrDie(env, "java/lang/Class");
1279 gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;");
Christopher Tate0d4a7922011-08-30 12:09:43 -07001280
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001281 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 env, kBinderProxyPathName,
1283 gBinderProxyMethods, NELEM(gBinderProxyMethods));
1284}
1285
1286// ****************************************************************************
1287// ****************************************************************************
1288// ****************************************************************************
1289
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001290int register_android_os_Binder(JNIEnv* env)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291{
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001292 if (int_register_android_os_Binder(env) < 0)
1293 return -1;
1294 if (int_register_android_os_BinderInternal(env) < 0)
1295 return -1;
1296 if (int_register_android_os_BinderProxy(env) < 0)
1297 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298
Andreas Gampe987f79f2014-11-18 17:29:46 -08001299 jclass clazz = FindClassOrDie(env, "android/util/Log");
1300 gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1301 gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e",
1302 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303
Andreas Gampe987f79f2014-11-18 17:29:46 -08001304 clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor");
1305 gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1306 gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>",
1307 "(Ljava/io/FileDescriptor;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
Andreas Gampe987f79f2014-11-18 17:29:46 -08001309 clazz = FindClassOrDie(env, "android/os/StrictMode");
1310 gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1311 gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz,
1312 "onBinderStrictModePolicyChange", "(I)V");
Brad Fitzpatrick727de402010-07-07 16:06:39 -07001313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 return 0;
1315}