The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 20 | #include "android_os_Parcel.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "android_util_Binder.h" |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include "JNIHelp.h" |
| 24 | |
| 25 | #include <fcntl.h> |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 26 | #include <inttypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
Brad Fitzpatrick | 8f26b32 | 2010-03-25 00:25:37 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | #include <utils/Atomic.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/IInterface.h> |
| 34 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <utils/Log.h> |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 36 | #include <utils/SystemClock.h> |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 37 | #include <utils/List.h> |
| 38 | #include <utils/KeyedVector.h> |
Colin Cross | 44bc186 | 2013-07-23 18:17:08 -0700 | [diff] [blame] | 39 | #include <log/logger.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 40 | #include <binder/Parcel.h> |
| 41 | #include <binder/ProcessState.h> |
| 42 | #include <binder/IServiceManager.h> |
Brad Fitzpatrick | 5348c01 | 2010-03-25 12:43:56 -0700 | [diff] [blame] | 43 | #include <utils/threads.h> |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 44 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 46 | #include <ScopedUtfChars.h> |
| 47 | #include <ScopedLocalRef.h> |
| 48 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 49 | #include "core_jni_helpers.h" |
| 50 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 51 | //#undef ALOGV |
| 52 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 54 | #define DEBUG_DEATH 0 |
| 55 | #if DEBUG_DEATH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 56 | #define LOGDEATH ALOGD |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 57 | #else |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 58 | #define LOGDEATH ALOGV |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | using namespace android; |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | static 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 | |
| 78 | static struct binderinternal_offsets_t |
| 79 | { |
| 80 | // Class state. |
| 81 | jclass mClass; |
| 82 | jmethodID mForceGc; |
| 83 | |
| 84 | } gBinderInternalOffsets; |
| 85 | |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | static struct error_offsets_t |
| 89 | { |
| 90 | jclass mClass; |
| 91 | } gErrorOffsets; |
| 92 | |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | |
| 95 | static 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 105 | jfieldID mOrgue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
| 107 | } gBinderProxyOffsets; |
| 108 | |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 109 | static struct class_offsets_t |
| 110 | { |
| 111 | jmethodID mGetName; |
| 112 | } gClassOffsets; |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | // ---------------------------------------------------------------------------- |
| 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | static struct log_offsets_t |
| 117 | { |
| 118 | // Class state. |
| 119 | jclass mClass; |
| 120 | jmethodID mLogE; |
| 121 | } gLogOffsets; |
| 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | static struct parcel_file_descriptor_offsets_t |
| 124 | { |
| 125 | jclass mClass; |
| 126 | jmethodID mConstructor; |
| 127 | } gParcelFileDescriptorOffsets; |
| 128 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 129 | static struct strict_mode_callback_offsets_t |
| 130 | { |
| 131 | jclass mClass; |
| 132 | jmethodID mCallback; |
| 133 | } gStrictModeCallbackOffsets; |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | // **************************************************************************** |
| 136 | // **************************************************************************** |
| 137 | // **************************************************************************** |
| 138 | |
| 139 | static volatile int32_t gNumRefsCreated = 0; |
| 140 | static volatile int32_t gNumProxyRefs = 0; |
| 141 | static volatile int32_t gNumLocalRefs = 0; |
| 142 | static volatile int32_t gNumDeathRefs = 0; |
| 143 | |
| 144 | static 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 152 | ALOGV("Now have %d binder ops", old); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | static JavaVM* jnienv_to_javavm(JNIEnv* env) |
| 157 | { |
| 158 | JavaVM* vm; |
| 159 | return env->GetJavaVM(&vm) >= 0 ? vm : NULL; |
| 160 | } |
| 161 | |
| 162 | static 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 | |
| 168 | static void report_exception(JNIEnv* env, jthrowable excep, const char* msg) |
| 169 | { |
| 170 | env->ExceptionClear(); |
| 171 | |
| 172 | jstring tagstr = env->NewStringUTF(LOG_TAG); |
Mathieu Chartier | cf6775e | 2014-08-06 13:39:17 -0700 | [diff] [blame] | 173 | jstring msgstr = NULL; |
| 174 | if (tagstr != NULL) { |
| 175 | msgstr = env->NewStringUTF(msg); |
| 176 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | if ((tagstr == NULL) || (msgstr == NULL)) { |
| 179 | env->ExceptionClear(); /* assume exception (OOM?) was thrown */ |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 180 | ALOGE("Unable to call Log.e()\n"); |
| 181 | ALOGE("%s", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 189 | ALOGW("Failed trying to log exception, msg='%s'\n", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | env->Throw(excep); |
Mathieu Chartier | 876b7d8f | 2015-04-28 16:30:30 -0700 | [diff] [blame] | 204 | env->ExceptionDescribe(); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 205 | ALOGE("Forcefully exiting"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | exit(1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bail: |
| 210 | /* discard local refs created for us by VM */ |
| 211 | env->DeleteLocalRef(tagstr); |
| 212 | env->DeleteLocalRef(msgstr); |
| 213 | } |
| 214 | |
| 215 | class JavaBBinderHolder; |
| 216 | |
| 217 | class JavaBBinder : public BBinder |
| 218 | { |
| 219 | public: |
| 220 | JavaBBinder(JNIEnv* env, jobject object) |
| 221 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)) |
| 222 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 223 | ALOGV("Creating JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | 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 | |
| 238 | protected: |
| 239 | virtual ~JavaBBinder() |
| 240 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 241 | ALOGV("Destroying JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 252 | ALOGV("onTransact() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 254 | IPCThreadState* thread_state = IPCThreadState::self(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 255 | const int32_t strict_policy_before = thread_state->getStrictModePolicy(); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | //printf("Transact from %p to Java code sending: ", this); |
| 258 | //data.print(); |
| 259 | //printf("\n"); |
| 260 | jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 261 | code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 262 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 263 | if (env->ExceptionCheck()) { |
| 264 | jthrowable excep = env->ExceptionOccurred(); |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 265 | 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 Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 274 | // 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 Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 279 | set_dalvik_blockguard_policy(env, strict_policy_before); |
| 280 | } |
| 281 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 282 | if (env->ExceptionCheck()) { |
| 283 | jthrowable excep = env->ExceptionOccurred(); |
| 284 | report_exception(env, excep, |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 285 | "*** Uncaught exception in onBinderStrictModePolicyChange"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | /* clean up JNI local ref -- we don't return to Java code */ |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 287 | env->DeleteLocalRef(excep); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 290 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | //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 | |
| 307 | private: |
| 308 | JavaVM* const mVM; |
| 309 | jobject const mObject; |
| 310 | }; |
| 311 | |
| 312 | // ---------------------------------------------------------------------------- |
| 313 | |
| 314 | class JavaBBinderHolder : public RefBase |
| 315 | { |
| 316 | public: |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 317 | sp<JavaBBinder> get(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | { |
| 319 | AutoMutex _l(mLock); |
| 320 | sp<JavaBBinder> b = mBinder.promote(); |
| 321 | if (b == NULL) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 322 | b = new JavaBBinder(env, obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | mBinder = b; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 324 | ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n", |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 325 | b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | return b; |
| 329 | } |
| 330 | |
| 331 | sp<JavaBBinder> getExisting() |
| 332 | { |
| 333 | AutoMutex _l(mLock); |
| 334 | return mBinder.promote(); |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | Mutex mLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | wp<JavaBBinder> mBinder; |
| 340 | }; |
| 341 | |
| 342 | // ---------------------------------------------------------------------------- |
| 343 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 344 | // 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 | |
| 348 | class JavaDeathRecipient; |
| 349 | |
| 350 | class DeathRecipientList : public RefBase { |
| 351 | List< sp<JavaDeathRecipient> > mList; |
| 352 | Mutex mLock; |
| 353 | |
| 354 | public: |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 355 | DeathRecipientList(); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 356 | ~DeathRecipientList(); |
| 357 | |
| 358 | void add(const sp<JavaDeathRecipient>& recipient); |
| 359 | void remove(const sp<JavaDeathRecipient>& recipient); |
| 360 | sp<JavaDeathRecipient> find(jobject recipient); |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 361 | |
| 362 | Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | // ---------------------------------------------------------------------------- |
| 366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | class JavaDeathRecipient : public IBinder::DeathRecipient |
| 368 | { |
| 369 | public: |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 370 | JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list) |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 371 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)), |
| 372 | mObjectWeak(NULL), mList(list) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 374 | // These objects manage their own lifetimes so are responsible for final bookkeeping. |
| 375 | // The list holds a strong reference to this object. |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 376 | LOGDEATH("Adding JDR %p to DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 377 | list->add(this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | android_atomic_inc(&gNumDeathRefs); |
| 380 | incRefsCreated(env); |
| 381 | } |
| 382 | |
| 383 | void binderDied(const wp<IBinder>& who) |
| 384 | { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 385 | LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 386 | if (mObject != NULL) { |
| 387 | JNIEnv* env = javavm_to_jnienv(mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 389 | env->CallStaticVoidMethod(gBinderProxyOffsets.mClass, |
| 390 | gBinderProxyOffsets.mSendDeathNotice, mObject); |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 391 | if (env->ExceptionCheck()) { |
| 392 | jthrowable excep = env->ExceptionOccurred(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 393 | report_exception(env, excep, |
| 394 | "*** Uncaught exception returned from death notification!"); |
| 395 | } |
| 396 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 397 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void clearReference() |
| 413 | { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 414 | sp<DeathRecipientList> list = mList.promote(); |
| 415 | if (list != NULL) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 416 | LOGDEATH("Removing JDR %p from DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 417 | list->remove(this); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 418 | } else { |
| 419 | LOGDEATH("clearReference() on JDR %p but DRL wp purged", this); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 420 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | bool matches(jobject obj) { |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 424 | bool result; |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 425 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 426 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 437 | 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 Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 442 | 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 447 | ALOGW("BinderProxy is being destroyed but the application did not call " |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 448 | "unlinkToDeath to unlink all of its death recipients beforehand. " |
| 449 | "Releasing leaked death recipient: %s", nameUtf.c_str()); |
| 450 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 451 | ALOGW("BinderProxy being destroyed; unable to get DR object name"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 452 | env->ExceptionClear(); |
| 453 | } |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | protected: |
| 458 | virtual ~JavaDeathRecipient() |
| 459 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 460 | //ALOGI("Removing death ref: recipient=%p\n", mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | android_atomic_dec(&gNumDeathRefs); |
| 462 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 463 | if (mObject != NULL) { |
| 464 | env->DeleteGlobalRef(mObject); |
| 465 | } else { |
| 466 | env->DeleteWeakGlobalRef(mObjectWeak); |
| 467 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | private: |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 471 | JavaVM* const mVM; |
| 472 | jobject mObject; |
| 473 | jweak mObjectWeak; // will be a weak ref to the same VM-side DeathRecipient after binderDied() |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 474 | wp<DeathRecipientList> mList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | // ---------------------------------------------------------------------------- |
| 478 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 479 | DeathRecipientList::DeathRecipientList() { |
| 480 | LOGDEATH("New DRL @ %p", this); |
| 481 | } |
| 482 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 483 | DeathRecipientList::~DeathRecipientList() { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 484 | LOGDEATH("Destroy DRL @ %p", this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 485 | 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 Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 491 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 492 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 493 | (*iter)->warnIfStillLive(); |
| 494 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) { |
| 499 | AutoMutex _l(mLock); |
| 500 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 501 | LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 502 | mList.push_back(recipient); |
| 503 | } |
| 504 | |
| 505 | void 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 Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 511 | LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 512 | mList.erase(iter); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | sp<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 Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 530 | Mutex& DeathRecipientList::lock() { |
| 531 | return mLock; |
| 532 | } |
| 533 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 534 | // ---------------------------------------------------------------------------- |
| 535 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | namespace android { |
| 537 | |
| 538 | static 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 | |
| 545 | static Mutex mProxyLock; |
| 546 | |
| 547 | jobject 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 Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 554 | LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | return object; |
| 556 | } |
| 557 | |
| 558 | // For the rest of the function we will hold this lock, to serialize |
| 559 | // looking/creation of Java proxies for native Binder proxies. |
| 560 | 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 Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 565 | jobject res = jniGetReferent(env, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | if (res != NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 567 | ALOGV("objectForBinder %p: found existing %p!\n", val.get(), res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | return res; |
| 569 | } |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 570 | LOGDEATH("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | 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 Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 578 | LOGDEATH("objectForBinder %p: created new proxy %p !\n", val.get(), object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | // The proxy holds a reference to the native object. |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 580 | env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get()); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 581 | val->incStrong((void*)javaObjectForIBinder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | |
| 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 590 | // Also remember the death recipients registered on this proxy |
| 591 | sp<DeathRecipientList> drl = new DeathRecipientList; |
| 592 | drl->incStrong((void*)javaObjectForIBinder); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 593 | env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get())); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | // Note that a new object reference has been created. |
| 596 | android_atomic_inc(&gNumProxyRefs); |
| 597 | incRefsCreated(env); |
| 598 | } |
| 599 | |
| 600 | return object; |
| 601 | } |
| 602 | |
| 603 | sp<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 Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 609 | env->GetLongField(obj, gBinderOffsets.mObject); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 610 | return jbh != NULL ? jbh->get(env, obj) : NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) { |
| 614 | return (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 615 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 618 | ALOGW("ibinderForJavaObject: %p is not a Binder object", obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc) |
| 623 | { |
| 624 | return env->NewObject( |
| 625 | gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc); |
| 626 | } |
| 627 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 628 | void 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 | |
| 637 | void signalExceptionForError(JNIEnv* env, jobject obj, status_t err, |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 638 | bool canThrowRemoteException, int parcelSize) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | { |
| 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 Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 675 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | break; |
| 680 | case UNKNOWN_TRANSACTION: |
| 681 | jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code"); |
| 682 | break; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 683 | case FAILED_TRANSACTION: { |
| 684 | ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize); |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 685 | const char* exceptionToThrow; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 686 | char msg[128]; |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 687 | // 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 Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 693 | 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 Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 700 | // it seems to always mean that the remote process died while the binder |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 701 | // transaction was already in flight. |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 702 | exceptionToThrow = (canThrowRemoteException) |
| 703 | ? "android/os/DeadObjectException" |
| 704 | : "java/lang/RuntimeException"; |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 705 | snprintf(msg, sizeof(msg)-1, |
| 706 | "Transaction failed on small parcel; remote process probably died"); |
| 707 | } |
| 708 | jniThrowException(env, exceptionToThrow, msg); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 709 | } break; |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 710 | case FDS_NOT_ALLOWED: |
| 711 | jniThrowException(env, "java/lang/RuntimeException", |
| 712 | "Not allowed to write file descriptors here"); |
| 713 | break; |
Christopher Wiley | a94fc52 | 2015-11-22 14:21:25 -0800 | [diff] [blame] | 714 | case UNEXPECTED_NULL: |
| 715 | jniThrowNullPointerException(env, NULL); |
| 716 | break; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 717 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | default: |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 750 | ALOGE("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 751 | String8 msg; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 752 | msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 753 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | } |
| 761 | |
| 762 | // ---------------------------------------------------------------------------- |
| 763 | |
| 764 | static jint android_os_Binder_getCallingPid(JNIEnv* env, jobject clazz) |
| 765 | { |
| 766 | return IPCThreadState::self()->getCallingPid(); |
| 767 | } |
| 768 | |
| 769 | static jint android_os_Binder_getCallingUid(JNIEnv* env, jobject clazz) |
| 770 | { |
| 771 | return IPCThreadState::self()->getCallingUid(); |
| 772 | } |
| 773 | |
| 774 | static jlong android_os_Binder_clearCallingIdentity(JNIEnv* env, jobject clazz) |
| 775 | { |
| 776 | return IPCThreadState::self()->clearCallingIdentity(); |
| 777 | } |
| 778 | |
| 779 | static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token) |
| 780 | { |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 781 | // 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 Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 786 | sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token); |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 787 | jniThrowException(env, "java/lang/IllegalStateException", buf); |
| 788 | return; |
| 789 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 791 | } |
| 792 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 793 | static void android_os_Binder_setThreadStrictModePolicy(JNIEnv* env, jobject clazz, jint policyMask) |
| 794 | { |
| 795 | IPCThreadState::self()->setStrictModePolicy(policyMask); |
| 796 | } |
| 797 | |
| 798 | static jint android_os_Binder_getThreadStrictModePolicy(JNIEnv* env, jobject clazz) |
| 799 | { |
| 800 | return IPCThreadState::self()->getStrictModePolicy(); |
| 801 | } |
| 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz) |
| 804 | { |
| 805 | IPCThreadState::self()->flushCommands(); |
| 806 | } |
| 807 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 808 | static void android_os_Binder_init(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 810 | JavaBBinderHolder* jbh = new JavaBBinderHolder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | if (jbh == NULL) { |
| 812 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 813 | return; |
| 814 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 815 | ALOGV("Java Binder %p: acquiring first ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 816 | jbh->incStrong((void*)android_os_Binder_init); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 817 | env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 820 | static void android_os_Binder_destroy(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | { |
| 822 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 823 | env->GetLongField(obj, gBinderOffsets.mObject); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 824 | if (jbh != NULL) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 825 | env->SetLongField(obj, gBinderOffsets.mObject, 0); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 826 | ALOGV("Java Binder %p: removing ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 827 | jbh->decStrong((void*)android_os_Binder_init); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 828 | } 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 835 | ALOGV("Java Binder %p: ignoring uninitialized binder", obj); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 836 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 839 | static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz) |
| 840 | { |
| 841 | return IPCThreadState::self()->blockUntilThreadAvailable(); |
| 842 | } |
| 843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | // ---------------------------------------------------------------------------- |
| 845 | |
| 846 | static 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 Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 852 | { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy }, |
| 853 | { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands }, |
| 855 | { "init", "()V", (void*)android_os_Binder_init }, |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 856 | { "destroy", "()V", (void*)android_os_Binder_destroy }, |
| 857 | { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | }; |
| 859 | |
| 860 | const char* const kBinderPathName = "android/os/Binder"; |
| 861 | |
| 862 | static int int_register_android_os_Binder(JNIEnv* env) |
| 863 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 864 | jclass clazz = FindClassOrDie(env, kBinderPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 866 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 870 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | env, kBinderPathName, |
| 872 | gBinderMethods, NELEM(gBinderMethods)); |
| 873 | } |
| 874 | |
| 875 | // **************************************************************************** |
| 876 | // **************************************************************************** |
| 877 | // **************************************************************************** |
| 878 | |
| 879 | namespace android { |
| 880 | |
| 881 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz) |
| 882 | { |
| 883 | return gNumLocalRefs; |
| 884 | } |
| 885 | |
| 886 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz) |
| 887 | { |
| 888 | return gNumProxyRefs; |
| 889 | } |
| 890 | |
| 891 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz) |
| 892 | { |
| 893 | return gNumDeathRefs; |
| 894 | } |
| 895 | |
| 896 | } |
| 897 | |
| 898 | // **************************************************************************** |
| 899 | // **************************************************************************** |
| 900 | // **************************************************************************** |
| 901 | |
| 902 | static 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 | |
| 908 | static 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 Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 914 | static 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz) |
| 921 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 922 | ALOGV("Gc has executed, clearing binder ops"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | android_atomic_and(0, &gNumRefsCreated); |
| 924 | } |
| 925 | |
| 926 | // ---------------------------------------------------------------------------- |
| 927 | |
| 928 | static 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 Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 932 | { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc } |
| 934 | }; |
| 935 | |
| 936 | const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal"; |
| 937 | |
| 938 | static int int_register_android_os_BinderInternal(JNIEnv* env) |
| 939 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 940 | jclass clazz = FindClassOrDie(env, kBinderInternalPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 942 | gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 943 | gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 945 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | env, kBinderInternalPathName, |
| 947 | gBinderInternalMethods, NELEM(gBinderInternalMethods)); |
| 948 | } |
| 949 | |
| 950 | // **************************************************************************** |
| 951 | // **************************************************************************** |
| 952 | // **************************************************************************** |
| 953 | |
| 954 | static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj) |
| 955 | { |
| 956 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 957 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | 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 | |
| 965 | static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj) |
| 966 | { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 967 | IBinder* target = (IBinder*) env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | if (target != NULL) { |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 969 | const String16& desc = target->getInterfaceDescriptor(); |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 970 | return env->NewString(reinterpret_cast<const jchar*>(desc.string()), |
| 971 | desc.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | jniThrowException(env, "java/lang/RuntimeException", |
| 974 | "No binder found for object"); |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj) |
| 979 | { |
| 980 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 981 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | if (target == NULL) { |
| 983 | return JNI_FALSE; |
| 984 | } |
| 985 | bool alive = target->isBinderAlive(); |
| 986 | return alive ? JNI_TRUE : JNI_FALSE; |
| 987 | } |
| 988 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 989 | static int getprocname(pid_t pid, char *buf, size_t len) { |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 990 | char filename[32]; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 991 | FILE *f; |
| 992 | |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 993 | snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 994 | f = fopen(filename, "r"); |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 995 | 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 Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1004 | fclose(f); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | static 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 Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1012 | ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1013 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1014 | 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 | |
| 1025 | static 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 Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1028 | ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1029 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1030 | 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 Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1040 | |
| 1041 | static const bool kEnableBinderSample = false; |
| 1042 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1043 | #define LOGTAG_BINDER_OPERATION 52004 |
| 1044 | |
| 1045 | static 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 Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1080 | // We only measure binder call durations to potentially log them if |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1081 | // we're on the main thread. |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1082 | static bool should_time_binder_calls() { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1083 | return (getpid() == gettid()); |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1087 | jint code, jobject dataObj, jobject replyObj, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | { |
| 1089 | if (dataObj == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1090 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | 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 Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1104 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | if (target == NULL) { |
| 1106 | jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!"); |
| 1107 | return JNI_FALSE; |
| 1108 | } |
| 1109 | |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1110 | ALOGV("Java code calling transact on %p in Java object %p with code %" PRId32 "\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | target, obj, code); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1112 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1113 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1114 | bool time_binder_calls; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1115 | int64_t start_millis; |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1116 | 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 Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1124 | } |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | //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 Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1129 | |
| 1130 | if (kEnableBinderSample) { |
| 1131 | if (time_binder_calls) { |
| 1132 | conditionally_log_binder_call(start_millis, target, code); |
| 1133 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | if (err == NO_ERROR) { |
| 1137 | return JNI_TRUE; |
| 1138 | } else if (err == UNKNOWN_TRANSACTION) { |
| 1139 | return JNI_FALSE; |
| 1140 | } |
| 1141 | |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1142 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | return JNI_FALSE; |
| 1144 | } |
| 1145 | |
| 1146 | static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1147 | jobject recipient, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | { |
| 1149 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1150 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1155 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1157 | ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | assert(false); |
| 1159 | } |
| 1160 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1161 | LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | |
| 1163 | if (!target->localBinder()) { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1164 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1165 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1166 | sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1167 | status_t err = target->linkToDeath(jdr, NULL, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | if (err != NO_ERROR) { |
| 1169 | // Failure adding the death recipient, so clear its reference |
| 1170 | // now. |
| 1171 | jdr->clearReference(); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1172 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | static 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1182 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | return res; |
| 1184 | } |
| 1185 | |
| 1186 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1187 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1189 | ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | return JNI_FALSE; |
| 1191 | } |
| 1192 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1193 | LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | |
| 1195 | if (!target->localBinder()) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1196 | status_t err = NAME_NOT_FOUND; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1197 | |
| 1198 | // If we find the matching recipient, proceed to unlink using that |
| 1199 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1200 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1201 | sp<JavaDeathRecipient> origJDR = list->find(recipient); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1202 | LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1203 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | 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 | |
| 1226 | static void android_os_BinderProxy_destroy(JNIEnv* env, jobject obj) |
| 1227 | { |
| 1228 | IBinder* b = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1229 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1230 | DeathRecipientList* drl = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1231 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1232 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1233 | LOGDEATH("Destroying BinderProxy %p: binder=%p drl=%p\n", obj, b, drl); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1234 | env->SetLongField(obj, gBinderProxyOffsets.mObject, 0); |
| 1235 | env->SetLongField(obj, gBinderProxyOffsets.mOrgue, 0); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1236 | drl->decStrong((void*)javaObjectForIBinder); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 1237 | b->decStrong((void*)javaObjectForIBinder); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1238 | |
| 1239 | IPCThreadState::self()->flushCommands(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | // ---------------------------------------------------------------------------- |
| 1243 | |
| 1244 | static const JNINativeMethod gBinderProxyMethods[] = { |
| 1245 | /* name, signature, funcPtr */ |
| 1246 | {"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder}, |
| 1247 | {"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive}, |
| 1248 | {"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor}, |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 1249 | {"transactNative", "(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void*)android_os_BinderProxy_transact}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | {"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath}, |
| 1251 | {"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath}, |
| 1252 | {"destroy", "()V", (void*)android_os_BinderProxy_destroy}, |
| 1253 | }; |
| 1254 | |
| 1255 | const char* const kBinderProxyPathName = "android/os/BinderProxy"; |
| 1256 | |
| 1257 | static int int_register_android_os_BinderProxy(JNIEnv* env) |
| 1258 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1259 | jclass clazz = FindClassOrDie(env, "java/lang/Error"); |
| 1260 | gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1262 | clazz = FindClassOrDie(env, kBinderProxyPathName); |
| 1263 | gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1264 | gBinderProxyOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", "()V"); |
| 1265 | gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice", |
| 1266 | "(Landroid/os/IBinder$DeathRecipient;)V"); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1267 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1268 | gBinderProxyOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J"); |
| 1269 | gBinderProxyOffsets.mSelf = GetFieldIDOrDie(env, clazz, "mSelf", |
| 1270 | "Ljava/lang/ref/WeakReference;"); |
| 1271 | gBinderProxyOffsets.mOrgue = GetFieldIDOrDie(env, clazz, "mOrgue", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1273 | clazz = FindClassOrDie(env, "java/lang/Class"); |
| 1274 | gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 1275 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1276 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | env, kBinderProxyPathName, |
| 1278 | gBinderProxyMethods, NELEM(gBinderProxyMethods)); |
| 1279 | } |
| 1280 | |
| 1281 | // **************************************************************************** |
| 1282 | // **************************************************************************** |
| 1283 | // **************************************************************************** |
| 1284 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1285 | int register_android_os_Binder(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | { |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1287 | if (int_register_android_os_Binder(env) < 0) |
| 1288 | return -1; |
| 1289 | if (int_register_android_os_BinderInternal(env) < 0) |
| 1290 | return -1; |
| 1291 | if (int_register_android_os_BinderProxy(env) < 0) |
| 1292 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1294 | jclass clazz = FindClassOrDie(env, "android/util/Log"); |
| 1295 | gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1296 | gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e", |
| 1297 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1299 | clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor"); |
| 1300 | gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1301 | gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", |
| 1302 | "(Ljava/io/FileDescriptor;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1304 | clazz = FindClassOrDie(env, "android/os/StrictMode"); |
| 1305 | gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1306 | gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz, |
| 1307 | "onBinderStrictModePolicyChange", "(I)V"); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |