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 <fcntl.h> |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
Brad Fitzpatrick | 8f26b32 | 2010-03-25 00:25:37 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Andreas Gampe | 58383ba | 2017-06-12 10:43:05 -0700 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 31 | #include <binder/IInterface.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 34 | #include <binder/Parcel.h> |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 35 | #include <binder/BpBinder.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 36 | #include <binder/ProcessState.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 37 | #include <log/log.h> |
| 38 | #include <utils/Atomic.h> |
| 39 | #include <utils/KeyedVector.h> |
| 40 | #include <utils/List.h> |
| 41 | #include <utils/Log.h> |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 42 | #include <utils/String8.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 43 | #include <utils/SystemClock.h> |
| 44 | #include <utils/threads.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 46 | #include <nativehelper/JNIHelp.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 47 | #include <nativehelper/ScopedLocalRef.h> |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 48 | #include <nativehelper/ScopedUtfChars.h> |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 49 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 50 | #include "core_jni_helpers.h" |
| 51 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 52 | //#undef ALOGV |
| 53 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 55 | #define DEBUG_DEATH 0 |
| 56 | #if DEBUG_DEATH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 57 | #define LOGDEATH ALOGD |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 58 | #else |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 59 | #define LOGDEATH ALOGV |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 60 | #endif |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | using namespace android; |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | static struct bindernative_offsets_t |
| 67 | { |
| 68 | // Class state. |
| 69 | jclass mClass; |
| 70 | jmethodID mExecTransact; |
| 71 | |
| 72 | // Object state. |
| 73 | jfieldID mObject; |
| 74 | |
| 75 | } gBinderOffsets; |
| 76 | |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | |
| 79 | static struct binderinternal_offsets_t |
| 80 | { |
| 81 | // Class state. |
| 82 | jclass mClass; |
| 83 | jmethodID mForceGc; |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 84 | jmethodID mProxyLimitCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| 86 | } gBinderInternalOffsets; |
| 87 | |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 88 | static struct sparseintarray_offsets_t |
| 89 | { |
| 90 | jclass classObject; |
| 91 | jmethodID constructor; |
| 92 | jmethodID put; |
| 93 | } gSparseIntArrayOffsets; |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | // ---------------------------------------------------------------------------- |
| 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | static struct error_offsets_t |
| 98 | { |
| 99 | jclass mClass; |
| 100 | } gErrorOffsets; |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | |
| 104 | static struct binderproxy_offsets_t |
| 105 | { |
| 106 | // Class state. |
| 107 | jclass mClass; |
| 108 | jmethodID mConstructor; |
| 109 | jmethodID mSendDeathNotice; |
| 110 | |
| 111 | // Object state. |
| 112 | jfieldID mObject; |
| 113 | jfieldID mSelf; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 114 | jfieldID mOrgue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | } gBinderProxyOffsets; |
| 117 | |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 118 | static struct class_offsets_t |
| 119 | { |
| 120 | jmethodID mGetName; |
| 121 | } gClassOffsets; |
| 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | // ---------------------------------------------------------------------------- |
| 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | static struct log_offsets_t |
| 126 | { |
| 127 | // Class state. |
| 128 | jclass mClass; |
| 129 | jmethodID mLogE; |
| 130 | } gLogOffsets; |
| 131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | static struct parcel_file_descriptor_offsets_t |
| 133 | { |
| 134 | jclass mClass; |
| 135 | jmethodID mConstructor; |
| 136 | } gParcelFileDescriptorOffsets; |
| 137 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 138 | static struct strict_mode_callback_offsets_t |
| 139 | { |
| 140 | jclass mClass; |
| 141 | jmethodID mCallback; |
| 142 | } gStrictModeCallbackOffsets; |
| 143 | |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 144 | static struct thread_dispatch_offsets_t |
| 145 | { |
| 146 | // Class state. |
| 147 | jclass mClass; |
| 148 | jmethodID mDispatchUncaughtException; |
| 149 | jmethodID mCurrentThread; |
| 150 | } gThreadDispatchOffsets; |
| 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | // **************************************************************************** |
| 153 | // **************************************************************************** |
| 154 | // **************************************************************************** |
| 155 | |
| 156 | static volatile int32_t gNumRefsCreated = 0; |
| 157 | static volatile int32_t gNumProxyRefs = 0; |
| 158 | static volatile int32_t gNumLocalRefs = 0; |
| 159 | static volatile int32_t gNumDeathRefs = 0; |
| 160 | |
| 161 | static void incRefsCreated(JNIEnv* env) |
| 162 | { |
| 163 | int old = android_atomic_inc(&gNumRefsCreated); |
| 164 | if (old == 200) { |
| 165 | android_atomic_and(0, &gNumRefsCreated); |
| 166 | env->CallStaticVoidMethod(gBinderInternalOffsets.mClass, |
| 167 | gBinderInternalOffsets.mForceGc); |
| 168 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 169 | ALOGV("Now have %d binder ops", old); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | static JavaVM* jnienv_to_javavm(JNIEnv* env) |
| 174 | { |
| 175 | JavaVM* vm; |
| 176 | return env->GetJavaVM(&vm) >= 0 ? vm : NULL; |
| 177 | } |
| 178 | |
| 179 | static JNIEnv* javavm_to_jnienv(JavaVM* vm) |
| 180 | { |
| 181 | JNIEnv* env; |
| 182 | return vm->GetEnv((void **)&env, JNI_VERSION_1_4) >= 0 ? env : NULL; |
| 183 | } |
| 184 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 185 | // Report a java.lang.Error (or subclass). This will terminate the runtime by |
| 186 | // calling FatalError with a message derived from the given error. |
| 187 | static void report_java_lang_error_fatal_error(JNIEnv* env, jthrowable error, |
| 188 | const char* msg) |
| 189 | { |
| 190 | // Report an error: reraise the exception and ask the runtime to abort. |
| 191 | |
| 192 | // Try to get the exception string. Sometimes logcat isn't available, |
| 193 | // so try to add it to the abort message. |
| 194 | std::string exc_msg = "(Unknown exception message)"; |
| 195 | { |
| 196 | ScopedLocalRef<jclass> exc_class(env, env->GetObjectClass(error)); |
| 197 | jmethodID method_id = env->GetMethodID(exc_class.get(), "toString", |
| 198 | "()Ljava/lang/String;"); |
| 199 | ScopedLocalRef<jstring> jstr( |
| 200 | env, |
| 201 | reinterpret_cast<jstring>( |
| 202 | env->CallObjectMethod(error, method_id))); |
| 203 | env->ExceptionClear(); // Just for good measure. |
| 204 | if (jstr.get() != nullptr) { |
| 205 | ScopedUtfChars jstr_utf(env, jstr.get()); |
| 206 | if (jstr_utf.c_str() != nullptr) { |
| 207 | exc_msg = jstr_utf.c_str(); |
| 208 | } else { |
| 209 | env->ExceptionClear(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | env->Throw(error); |
| 215 | ALOGE("java.lang.Error thrown during binder transaction (stack trace follows) : "); |
| 216 | env->ExceptionDescribe(); |
| 217 | |
| 218 | std::string error_msg = base::StringPrintf( |
| 219 | "java.lang.Error thrown during binder transaction: %s", |
| 220 | exc_msg.c_str()); |
| 221 | env->FatalError(error_msg.c_str()); |
| 222 | } |
| 223 | |
| 224 | // Report a java.lang.Error (or subclass). This will terminate the runtime, either by |
| 225 | // the uncaught exception handler, or explicitly by calling |
| 226 | // report_java_lang_error_fatal_error. |
| 227 | static void report_java_lang_error(JNIEnv* env, jthrowable error, const char* msg) |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 228 | { |
| 229 | // Try to run the uncaught exception machinery. |
| 230 | jobject thread = env->CallStaticObjectMethod(gThreadDispatchOffsets.mClass, |
| 231 | gThreadDispatchOffsets.mCurrentThread); |
| 232 | if (thread != nullptr) { |
| 233 | env->CallVoidMethod(thread, gThreadDispatchOffsets.mDispatchUncaughtException, |
| 234 | error); |
| 235 | // Should not return here, unless more errors occured. |
| 236 | } |
| 237 | // Some error occurred that meant that either dispatchUncaughtException could not be |
| 238 | // called or that it had an error itself (as this should be unreachable under normal |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 239 | // conditions). As the binder code cannot handle Errors, attempt to log the error and |
| 240 | // abort. |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 241 | env->ExceptionClear(); |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 242 | report_java_lang_error_fatal_error(env, error, msg); |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 243 | } |
| 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | static void report_exception(JNIEnv* env, jthrowable excep, const char* msg) |
| 246 | { |
| 247 | env->ExceptionClear(); |
| 248 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 249 | ScopedLocalRef<jstring> tagstr(env, env->NewStringUTF(LOG_TAG)); |
Andreas Gampe | 8571ec3 | 2017-09-21 10:55:59 -0700 | [diff] [blame] | 250 | ScopedLocalRef<jstring> msgstr(env); |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 251 | if (tagstr != nullptr) { |
| 252 | msgstr.reset(env->NewStringUTF(msg)); |
Mathieu Chartier | cf6775e | 2014-08-06 13:39:17 -0700 | [diff] [blame] | 253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 255 | if ((tagstr != nullptr) && (msgstr != nullptr)) { |
| 256 | env->CallStaticIntMethod(gLogOffsets.mClass, gLogOffsets.mLogE, |
| 257 | tagstr.get(), msgstr.get(), excep); |
| 258 | if (env->ExceptionCheck()) { |
| 259 | // Attempting to log the failure has failed. |
| 260 | ALOGW("Failed trying to log exception, msg='%s'\n", msg); |
| 261 | env->ExceptionClear(); |
| 262 | } |
| 263 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | env->ExceptionClear(); /* assume exception (OOM?) was thrown */ |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 265 | ALOGE("Unable to call Log.e()\n"); |
| 266 | ALOGE("%s", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (env->IsInstanceOf(excep, gErrorOffsets.mClass)) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 270 | report_java_lang_error(env, excep, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | class JavaBBinderHolder; |
| 275 | |
| 276 | class JavaBBinder : public BBinder |
| 277 | { |
| 278 | public: |
| 279 | JavaBBinder(JNIEnv* env, jobject object) |
| 280 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)) |
| 281 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 282 | ALOGV("Creating JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | android_atomic_inc(&gNumLocalRefs); |
| 284 | incRefsCreated(env); |
| 285 | } |
| 286 | |
| 287 | bool checkSubclass(const void* subclassID) const |
| 288 | { |
| 289 | return subclassID == &gBinderOffsets; |
| 290 | } |
| 291 | |
| 292 | jobject object() const |
| 293 | { |
| 294 | return mObject; |
| 295 | } |
| 296 | |
| 297 | protected: |
| 298 | virtual ~JavaBBinder() |
| 299 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 300 | ALOGV("Destroying JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | android_atomic_dec(&gNumLocalRefs); |
| 302 | JNIEnv* env = javavm_to_jnienv(mVM); |
| 303 | env->DeleteGlobalRef(mObject); |
| 304 | } |
| 305 | |
| 306 | virtual status_t onTransact( |
| 307 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0) |
| 308 | { |
| 309 | JNIEnv* env = javavm_to_jnienv(mVM); |
| 310 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 311 | 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] | 312 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 313 | IPCThreadState* thread_state = IPCThreadState::self(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 314 | const int32_t strict_policy_before = thread_state->getStrictModePolicy(); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 315 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | //printf("Transact from %p to Java code sending: ", this); |
| 317 | //data.print(); |
| 318 | //printf("\n"); |
| 319 | jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 320 | code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 321 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 322 | if (env->ExceptionCheck()) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 323 | ScopedLocalRef<jthrowable> excep(env, env->ExceptionOccurred()); |
| 324 | report_exception(env, excep.get(), |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 325 | "*** Uncaught remote exception! " |
| 326 | "(Exceptions are not yet supported across processes.)"); |
| 327 | res = JNI_FALSE; |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 330 | // Check if the strict mode state changed while processing the |
| 331 | // call. The Binder state will be restored by the underlying |
| 332 | // Binder system in IPCThreadState, however we need to take care |
| 333 | // of the parallel Java state as well. |
| 334 | if (thread_state->getStrictModePolicy() != strict_policy_before) { |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 335 | set_dalvik_blockguard_policy(env, strict_policy_before); |
| 336 | } |
| 337 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 338 | if (env->ExceptionCheck()) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 339 | ScopedLocalRef<jthrowable> excep(env, env->ExceptionOccurred()); |
| 340 | report_exception(env, excep.get(), |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 341 | "*** Uncaught exception in onBinderStrictModePolicyChange"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 344 | // Need to always call through the native implementation of |
| 345 | // SYSPROPS_TRANSACTION. |
| 346 | if (code == SYSPROPS_TRANSACTION) { |
| 347 | BBinder::onTransact(code, data, reply, flags); |
| 348 | } |
| 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | //aout << "onTransact to Java code; result=" << res << endl |
| 351 | // << "Transact from " << this << " to Java code returning " |
| 352 | // << reply << ": " << *reply << endl; |
| 353 | return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION; |
| 354 | } |
| 355 | |
| 356 | virtual status_t dump(int fd, const Vector<String16>& args) |
| 357 | { |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | JavaVM* const mVM; |
| 363 | jobject const mObject; |
| 364 | }; |
| 365 | |
| 366 | // ---------------------------------------------------------------------------- |
| 367 | |
| 368 | class JavaBBinderHolder : public RefBase |
| 369 | { |
| 370 | public: |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 371 | sp<JavaBBinder> get(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | { |
| 373 | AutoMutex _l(mLock); |
| 374 | sp<JavaBBinder> b = mBinder.promote(); |
| 375 | if (b == NULL) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 376 | b = new JavaBBinder(env, obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | mBinder = b; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 378 | ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n", |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 379 | b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return b; |
| 383 | } |
| 384 | |
| 385 | sp<JavaBBinder> getExisting() |
| 386 | { |
| 387 | AutoMutex _l(mLock); |
| 388 | return mBinder.promote(); |
| 389 | } |
| 390 | |
| 391 | private: |
| 392 | Mutex mLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | wp<JavaBBinder> mBinder; |
| 394 | }; |
| 395 | |
| 396 | // ---------------------------------------------------------------------------- |
| 397 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 398 | // Per-IBinder death recipient bookkeeping. This is how we reconcile local jobject |
| 399 | // death recipient references passed in through JNI with the permanent corresponding |
| 400 | // JavaDeathRecipient objects. |
| 401 | |
| 402 | class JavaDeathRecipient; |
| 403 | |
| 404 | class DeathRecipientList : public RefBase { |
| 405 | List< sp<JavaDeathRecipient> > mList; |
| 406 | Mutex mLock; |
| 407 | |
| 408 | public: |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 409 | DeathRecipientList(); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 410 | ~DeathRecipientList(); |
| 411 | |
| 412 | void add(const sp<JavaDeathRecipient>& recipient); |
| 413 | void remove(const sp<JavaDeathRecipient>& recipient); |
| 414 | sp<JavaDeathRecipient> find(jobject recipient); |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 415 | |
| 416 | Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | // ---------------------------------------------------------------------------- |
| 420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | class JavaDeathRecipient : public IBinder::DeathRecipient |
| 422 | { |
| 423 | public: |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 424 | JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list) |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 425 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)), |
| 426 | mObjectWeak(NULL), mList(list) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 428 | // These objects manage their own lifetimes so are responsible for final bookkeeping. |
| 429 | // The list holds a strong reference to this object. |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 430 | LOGDEATH("Adding JDR %p to DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 431 | list->add(this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 432 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | android_atomic_inc(&gNumDeathRefs); |
| 434 | incRefsCreated(env); |
| 435 | } |
| 436 | |
| 437 | void binderDied(const wp<IBinder>& who) |
| 438 | { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 439 | LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 440 | if (mObject != NULL) { |
| 441 | JNIEnv* env = javavm_to_jnienv(mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 443 | env->CallStaticVoidMethod(gBinderProxyOffsets.mClass, |
| 444 | gBinderProxyOffsets.mSendDeathNotice, mObject); |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 445 | if (env->ExceptionCheck()) { |
| 446 | jthrowable excep = env->ExceptionOccurred(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 447 | report_exception(env, excep, |
| 448 | "*** Uncaught exception returned from death notification!"); |
| 449 | } |
| 450 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 451 | // Serialize with our containing DeathRecipientList so that we can't |
| 452 | // delete the global ref on mObject while the list is being iterated. |
| 453 | sp<DeathRecipientList> list = mList.promote(); |
| 454 | if (list != NULL) { |
| 455 | AutoMutex _l(list->lock()); |
| 456 | |
| 457 | // Demote from strong ref to weak after binderDied() has been delivered, |
| 458 | // to allow the DeathRecipient and BinderProxy to be GC'd if no longer needed. |
| 459 | mObjectWeak = env->NewWeakGlobalRef(mObject); |
| 460 | env->DeleteGlobalRef(mObject); |
| 461 | mObject = NULL; |
| 462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void clearReference() |
| 467 | { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 468 | sp<DeathRecipientList> list = mList.promote(); |
| 469 | if (list != NULL) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 470 | LOGDEATH("Removing JDR %p from DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 471 | list->remove(this); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 472 | } else { |
| 473 | LOGDEATH("clearReference() on JDR %p but DRL wp purged", this); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 474 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | bool matches(jobject obj) { |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 478 | bool result; |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 479 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 480 | |
| 481 | if (mObject != NULL) { |
| 482 | result = env->IsSameObject(obj, mObject); |
| 483 | } else { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 484 | ScopedLocalRef<jobject> me(env, env->NewLocalRef(mObjectWeak)); |
| 485 | result = env->IsSameObject(obj, me.get()); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 486 | } |
| 487 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 490 | void warnIfStillLive() { |
| 491 | if (mObject != NULL) { |
| 492 | // Okay, something is wrong -- we have a hard reference to a live death |
| 493 | // recipient on the VM side, but the list is being torn down. |
| 494 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 495 | ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject)); |
| 496 | ScopedLocalRef<jstring> nameRef(env, |
| 497 | (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName)); |
| 498 | ScopedUtfChars nameUtf(env, nameRef.get()); |
| 499 | if (nameUtf.c_str() != NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 500 | ALOGW("BinderProxy is being destroyed but the application did not call " |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 501 | "unlinkToDeath to unlink all of its death recipients beforehand. " |
| 502 | "Releasing leaked death recipient: %s", nameUtf.c_str()); |
| 503 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 504 | ALOGW("BinderProxy being destroyed; unable to get DR object name"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 505 | env->ExceptionClear(); |
| 506 | } |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | protected: |
| 511 | virtual ~JavaDeathRecipient() |
| 512 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 513 | //ALOGI("Removing death ref: recipient=%p\n", mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | android_atomic_dec(&gNumDeathRefs); |
| 515 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 516 | if (mObject != NULL) { |
| 517 | env->DeleteGlobalRef(mObject); |
| 518 | } else { |
| 519 | env->DeleteWeakGlobalRef(mObjectWeak); |
| 520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | private: |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 524 | JavaVM* const mVM; |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 525 | jobject mObject; // Initial strong ref to Java-side DeathRecipient. Cleared on binderDied(). |
| 526 | jweak mObjectWeak; // weak ref to the same Java-side DeathRecipient after binderDied(). |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 527 | wp<DeathRecipientList> mList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | }; |
| 529 | |
| 530 | // ---------------------------------------------------------------------------- |
| 531 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 532 | DeathRecipientList::DeathRecipientList() { |
| 533 | LOGDEATH("New DRL @ %p", this); |
| 534 | } |
| 535 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 536 | DeathRecipientList::~DeathRecipientList() { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 537 | LOGDEATH("Destroy DRL @ %p", this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 538 | AutoMutex _l(mLock); |
| 539 | |
| 540 | // Should never happen -- the JavaDeathRecipient objects that have added themselves |
| 541 | // to the list are holding references on the list object. Only when they are torn |
| 542 | // down can the list header be destroyed. |
| 543 | if (mList.size() > 0) { |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 544 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 545 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 546 | (*iter)->warnIfStillLive(); |
| 547 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) { |
| 552 | AutoMutex _l(mLock); |
| 553 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 554 | LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 555 | mList.push_back(recipient); |
| 556 | } |
| 557 | |
| 558 | void DeathRecipientList::remove(const sp<JavaDeathRecipient>& recipient) { |
| 559 | AutoMutex _l(mLock); |
| 560 | |
| 561 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 562 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 563 | if (*iter == recipient) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 564 | LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 565 | mList.erase(iter); |
| 566 | return; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | sp<JavaDeathRecipient> DeathRecipientList::find(jobject recipient) { |
| 572 | AutoMutex _l(mLock); |
| 573 | |
| 574 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 575 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 576 | if ((*iter)->matches(recipient)) { |
| 577 | return *iter; |
| 578 | } |
| 579 | } |
| 580 | return NULL; |
| 581 | } |
| 582 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 583 | Mutex& DeathRecipientList::lock() { |
| 584 | return mLock; |
| 585 | } |
| 586 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 587 | // ---------------------------------------------------------------------------- |
| 588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | namespace android { |
| 590 | |
| 591 | static void proxy_cleanup(const void* id, void* obj, void* cleanupCookie) |
| 592 | { |
| 593 | android_atomic_dec(&gNumProxyRefs); |
| 594 | JNIEnv* env = javavm_to_jnienv((JavaVM*)cleanupCookie); |
| 595 | env->DeleteGlobalRef((jobject)obj); |
| 596 | } |
| 597 | |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 598 | static Mutex gProxyLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | |
| 600 | jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val) |
| 601 | { |
| 602 | if (val == NULL) return NULL; |
| 603 | |
| 604 | if (val->checkSubclass(&gBinderOffsets)) { |
| 605 | // One of our own! |
| 606 | jobject object = static_cast<JavaBBinder*>(val.get())->object(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 607 | 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] | 608 | return object; |
| 609 | } |
| 610 | |
| 611 | // For the rest of the function we will hold this lock, to serialize |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 612 | // looking/creation/destruction of Java proxies for native Binder proxies. |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 613 | AutoMutex _l(gProxyLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | // Someone else's... do we know about it? |
| 616 | jobject object = (jobject)val->findObject(&gBinderProxyOffsets); |
| 617 | if (object != NULL) { |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 618 | jobject res = jniGetReferent(env, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | if (res != NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 620 | 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] | 621 | return res; |
| 622 | } |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 623 | 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] | 624 | android_atomic_dec(&gNumProxyRefs); |
| 625 | val->detachObject(&gBinderProxyOffsets); |
| 626 | env->DeleteGlobalRef(object); |
| 627 | } |
| 628 | |
| 629 | object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor); |
| 630 | if (object != NULL) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 631 | 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] | 632 | // The proxy holds a reference to the native object. |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 633 | env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get()); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 634 | val->incStrong((void*)javaObjectForIBinder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | |
| 636 | // The native object needs to hold a weak reference back to the |
| 637 | // proxy, so we can retrieve the same proxy if it is still active. |
| 638 | jobject refObject = env->NewGlobalRef( |
| 639 | env->GetObjectField(object, gBinderProxyOffsets.mSelf)); |
| 640 | val->attachObject(&gBinderProxyOffsets, refObject, |
| 641 | jnienv_to_javavm(env), proxy_cleanup); |
| 642 | |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 643 | // Also remember the death recipients registered on this proxy |
| 644 | sp<DeathRecipientList> drl = new DeathRecipientList; |
| 645 | drl->incStrong((void*)javaObjectForIBinder); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 646 | env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get())); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | // Note that a new object reference has been created. |
| 649 | android_atomic_inc(&gNumProxyRefs); |
| 650 | incRefsCreated(env); |
| 651 | } |
| 652 | |
| 653 | return object; |
| 654 | } |
| 655 | |
| 656 | sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj) |
| 657 | { |
| 658 | if (obj == NULL) return NULL; |
| 659 | |
| 660 | if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) { |
| 661 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 662 | env->GetLongField(obj, gBinderOffsets.mObject); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 663 | return jbh != NULL ? jbh->get(env, obj) : NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) { |
| 667 | return (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 668 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 671 | ALOGW("ibinderForJavaObject: %p is not a Binder object", obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc) |
| 676 | { |
| 677 | return env->NewObject( |
| 678 | gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc); |
| 679 | } |
| 680 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 681 | void set_dalvik_blockguard_policy(JNIEnv* env, jint strict_policy) |
| 682 | { |
| 683 | // Call back into android.os.StrictMode#onBinderStrictModePolicyChange |
| 684 | // to sync our state back to it. See the comments in StrictMode.java. |
| 685 | env->CallStaticVoidMethod(gStrictModeCallbackOffsets.mClass, |
| 686 | gStrictModeCallbackOffsets.mCallback, |
| 687 | strict_policy); |
| 688 | } |
| 689 | |
| 690 | void signalExceptionForError(JNIEnv* env, jobject obj, status_t err, |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 691 | bool canThrowRemoteException, int parcelSize) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | { |
| 693 | switch (err) { |
| 694 | case UNKNOWN_ERROR: |
| 695 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 696 | break; |
| 697 | case NO_MEMORY: |
| 698 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 699 | break; |
| 700 | case INVALID_OPERATION: |
| 701 | jniThrowException(env, "java/lang/UnsupportedOperationException", NULL); |
| 702 | break; |
| 703 | case BAD_VALUE: |
| 704 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 705 | break; |
| 706 | case BAD_INDEX: |
| 707 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL); |
| 708 | break; |
| 709 | case BAD_TYPE: |
| 710 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 711 | break; |
| 712 | case NAME_NOT_FOUND: |
| 713 | jniThrowException(env, "java/util/NoSuchElementException", NULL); |
| 714 | break; |
| 715 | case PERMISSION_DENIED: |
| 716 | jniThrowException(env, "java/lang/SecurityException", NULL); |
| 717 | break; |
| 718 | case NOT_ENOUGH_DATA: |
| 719 | jniThrowException(env, "android/os/ParcelFormatException", "Not enough data"); |
| 720 | break; |
| 721 | case NO_INIT: |
| 722 | jniThrowException(env, "java/lang/RuntimeException", "Not initialized"); |
| 723 | break; |
| 724 | case ALREADY_EXISTS: |
| 725 | jniThrowException(env, "java/lang/RuntimeException", "Item already exists"); |
| 726 | break; |
| 727 | case DEAD_OBJECT: |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 728 | // DeadObjectException is a checked exception, only throw from certain methods. |
| 729 | jniThrowException(env, canThrowRemoteException |
| 730 | ? "android/os/DeadObjectException" |
| 731 | : "java/lang/RuntimeException", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | break; |
| 733 | case UNKNOWN_TRANSACTION: |
| 734 | jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code"); |
| 735 | break; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 736 | case FAILED_TRANSACTION: { |
| 737 | ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize); |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 738 | const char* exceptionToThrow; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 739 | char msg[128]; |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 740 | // TransactionTooLargeException is a checked exception, only throw from certain methods. |
| 741 | // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION |
| 742 | // but it is not the only one. The Binder driver can return BR_FAILED_REPLY |
| 743 | // for other reasons also, such as if the transaction is malformed or |
| 744 | // refers to an FD that has been closed. We should change the driver |
| 745 | // to enable us to distinguish these cases in the future. |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 746 | if (canThrowRemoteException && parcelSize > 200*1024) { |
| 747 | // bona fide large payload |
| 748 | exceptionToThrow = "android/os/TransactionTooLargeException"; |
| 749 | snprintf(msg, sizeof(msg)-1, "data parcel size %d bytes", parcelSize); |
| 750 | } else { |
| 751 | // Heuristic: a payload smaller than this threshold "shouldn't" be too |
| 752 | // big, so it's probably some other, more subtle problem. In practice |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 753 | // 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] | 754 | // transaction was already in flight. |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 755 | exceptionToThrow = (canThrowRemoteException) |
| 756 | ? "android/os/DeadObjectException" |
| 757 | : "java/lang/RuntimeException"; |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 758 | snprintf(msg, sizeof(msg)-1, |
| 759 | "Transaction failed on small parcel; remote process probably died"); |
| 760 | } |
| 761 | jniThrowException(env, exceptionToThrow, msg); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 762 | } break; |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 763 | case FDS_NOT_ALLOWED: |
| 764 | jniThrowException(env, "java/lang/RuntimeException", |
| 765 | "Not allowed to write file descriptors here"); |
| 766 | break; |
Christopher Wiley | a94fc52 | 2015-11-22 14:21:25 -0800 | [diff] [blame] | 767 | case UNEXPECTED_NULL: |
| 768 | jniThrowNullPointerException(env, NULL); |
| 769 | break; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 770 | case -EBADF: |
| 771 | jniThrowException(env, "java/lang/RuntimeException", |
| 772 | "Bad file descriptor"); |
| 773 | break; |
| 774 | case -ENFILE: |
| 775 | jniThrowException(env, "java/lang/RuntimeException", |
| 776 | "File table overflow"); |
| 777 | break; |
| 778 | case -EMFILE: |
| 779 | jniThrowException(env, "java/lang/RuntimeException", |
| 780 | "Too many open files"); |
| 781 | break; |
| 782 | case -EFBIG: |
| 783 | jniThrowException(env, "java/lang/RuntimeException", |
| 784 | "File too large"); |
| 785 | break; |
| 786 | case -ENOSPC: |
| 787 | jniThrowException(env, "java/lang/RuntimeException", |
| 788 | "No space left on device"); |
| 789 | break; |
| 790 | case -ESPIPE: |
| 791 | jniThrowException(env, "java/lang/RuntimeException", |
| 792 | "Illegal seek"); |
| 793 | break; |
| 794 | case -EROFS: |
| 795 | jniThrowException(env, "java/lang/RuntimeException", |
| 796 | "Read-only file system"); |
| 797 | break; |
| 798 | case -EMLINK: |
| 799 | jniThrowException(env, "java/lang/RuntimeException", |
| 800 | "Too many links"); |
| 801 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | default: |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 803 | ALOGE("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 804 | String8 msg; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 805 | msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 806 | // RemoteException is a checked exception, only throw from certain methods. |
| 807 | jniThrowException(env, canThrowRemoteException |
| 808 | ? "android/os/RemoteException" : "java/lang/RuntimeException", msg.string()); |
| 809 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | } |
| 814 | |
| 815 | // ---------------------------------------------------------------------------- |
| 816 | |
| 817 | static jint android_os_Binder_getCallingPid(JNIEnv* env, jobject clazz) |
| 818 | { |
| 819 | return IPCThreadState::self()->getCallingPid(); |
| 820 | } |
| 821 | |
| 822 | static jint android_os_Binder_getCallingUid(JNIEnv* env, jobject clazz) |
| 823 | { |
| 824 | return IPCThreadState::self()->getCallingUid(); |
| 825 | } |
| 826 | |
| 827 | static jlong android_os_Binder_clearCallingIdentity(JNIEnv* env, jobject clazz) |
| 828 | { |
| 829 | return IPCThreadState::self()->clearCallingIdentity(); |
| 830 | } |
| 831 | |
| 832 | static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token) |
| 833 | { |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 834 | // XXX temporary sanity check to debug crashes. |
| 835 | int uid = (int)(token>>32); |
| 836 | if (uid > 0 && uid < 999) { |
| 837 | // In Android currently there are no uids in this range. |
| 838 | char buf[128]; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 839 | sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token); |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 840 | jniThrowException(env, "java/lang/IllegalStateException", buf); |
| 841 | return; |
| 842 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 844 | } |
| 845 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 846 | static void android_os_Binder_setThreadStrictModePolicy(JNIEnv* env, jobject clazz, jint policyMask) |
| 847 | { |
| 848 | IPCThreadState::self()->setStrictModePolicy(policyMask); |
| 849 | } |
| 850 | |
| 851 | static jint android_os_Binder_getThreadStrictModePolicy(JNIEnv* env, jobject clazz) |
| 852 | { |
| 853 | return IPCThreadState::self()->getStrictModePolicy(); |
| 854 | } |
| 855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz) |
| 857 | { |
| 858 | IPCThreadState::self()->flushCommands(); |
| 859 | } |
| 860 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 861 | 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] | 862 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 863 | JavaBBinderHolder* jbh = new JavaBBinderHolder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | if (jbh == NULL) { |
| 865 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 866 | return; |
| 867 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 868 | ALOGV("Java Binder %p: acquiring first ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 869 | jbh->incStrong((void*)android_os_Binder_init); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 870 | env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Vladimir Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 873 | static void android_os_Binder_destroyBinder(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | { |
| 875 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 876 | env->GetLongField(obj, gBinderOffsets.mObject); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 877 | if (jbh != NULL) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 878 | env->SetLongField(obj, gBinderOffsets.mObject, 0); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 879 | ALOGV("Java Binder %p: removing ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 880 | jbh->decStrong((void*)android_os_Binder_init); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 881 | } else { |
| 882 | // Encountering an uninitialized binder is harmless. All it means is that |
| 883 | // the Binder was only partially initialized when its finalizer ran and called |
Vladimir Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 884 | // destroyBinder(). The Binder could be partially initialized for several reasons. |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 885 | // For example, a Binder subclass constructor might have thrown an exception before |
| 886 | // it could delegate to its superclass's constructor. Consequently init() would |
| 887 | // not have been called and the holder pointer would remain NULL. |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 888 | ALOGV("Java Binder %p: ignoring uninitialized binder", obj); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 892 | static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz) |
| 893 | { |
| 894 | return IPCThreadState::self()->blockUntilThreadAvailable(); |
| 895 | } |
| 896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | // ---------------------------------------------------------------------------- |
| 898 | |
| 899 | static const JNINativeMethod gBinderMethods[] = { |
| 900 | /* name, signature, funcPtr */ |
| 901 | { "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid }, |
| 902 | { "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid }, |
| 903 | { "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity }, |
| 904 | { "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity }, |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 905 | { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy }, |
| 906 | { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands }, |
| 908 | { "init", "()V", (void*)android_os_Binder_init }, |
Vladimir Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 909 | { "destroyBinder", "()V", (void*)android_os_Binder_destroyBinder }, |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 910 | { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | const char* const kBinderPathName = "android/os/Binder"; |
| 914 | |
| 915 | static int int_register_android_os_Binder(JNIEnv* env) |
| 916 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 917 | jclass clazz = FindClassOrDie(env, kBinderPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 919 | gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 920 | gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z"); |
| 921 | gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 923 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | env, kBinderPathName, |
| 925 | gBinderMethods, NELEM(gBinderMethods)); |
| 926 | } |
| 927 | |
| 928 | // **************************************************************************** |
| 929 | // **************************************************************************** |
| 930 | // **************************************************************************** |
| 931 | |
| 932 | namespace android { |
| 933 | |
| 934 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz) |
| 935 | { |
| 936 | return gNumLocalRefs; |
| 937 | } |
| 938 | |
| 939 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz) |
| 940 | { |
| 941 | return gNumProxyRefs; |
| 942 | } |
| 943 | |
| 944 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz) |
| 945 | { |
| 946 | return gNumDeathRefs; |
| 947 | } |
| 948 | |
| 949 | } |
| 950 | |
| 951 | // **************************************************************************** |
| 952 | // **************************************************************************** |
| 953 | // **************************************************************************** |
| 954 | |
| 955 | static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz) |
| 956 | { |
| 957 | sp<IBinder> b = ProcessState::self()->getContextObject(NULL); |
| 958 | return javaObjectForIBinder(env, b); |
| 959 | } |
| 960 | |
| 961 | static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz) |
| 962 | { |
| 963 | sp<IBinder> b = ProcessState::self()->getContextObject(NULL); |
| 964 | android::IPCThreadState::self()->joinThreadPool(); |
| 965 | } |
| 966 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 967 | static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env, |
| 968 | jobject clazz, jboolean disable) |
| 969 | { |
| 970 | IPCThreadState::disableBackgroundScheduling(disable ? true : false); |
| 971 | } |
| 972 | |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 973 | static void android_os_BinderInternal_setMaxThreads(JNIEnv* env, |
| 974 | jobject clazz, jint maxThreads) |
| 975 | { |
| 976 | ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads); |
| 977 | } |
| 978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz) |
| 980 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 981 | ALOGV("Gc has executed, clearing binder ops"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | android_atomic_and(0, &gNumRefsCreated); |
| 983 | } |
| 984 | |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 985 | static void android_os_BinderInternal_proxyLimitcallback(int uid) |
| 986 | { |
| 987 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 988 | env->CallStaticVoidMethod(gBinderInternalOffsets.mClass, |
| 989 | gBinderInternalOffsets.mProxyLimitCallback, |
| 990 | uid); |
| 991 | } |
| 992 | |
| 993 | static void android_os_BinderInternal_setBinderProxyCountEnabled(JNIEnv* env, jobject clazz, |
| 994 | jboolean enable) |
| 995 | { |
| 996 | BpBinder::setCountByUidEnabled((bool) enable); |
| 997 | } |
| 998 | |
| 999 | static jobject android_os_BinderInternal_getBinderProxyPerUidCounts(JNIEnv* env, jclass clazz) |
| 1000 | { |
| 1001 | Vector<uint32_t> uids, counts; |
| 1002 | BpBinder::getCountByUid(uids, counts); |
| 1003 | jobject sparseIntArray = env->NewObject(gSparseIntArrayOffsets.classObject, |
| 1004 | gSparseIntArrayOffsets.constructor); |
| 1005 | for (size_t i = 0; i < uids.size(); i++) { |
| 1006 | env->CallVoidMethod(sparseIntArray, gSparseIntArrayOffsets.put, |
| 1007 | static_cast<jint>(uids[i]), static_cast<jint>(counts[i])); |
| 1008 | } |
| 1009 | return sparseIntArray; |
| 1010 | } |
| 1011 | |
| 1012 | static jint android_os_BinderInternal_getBinderProxyCount(JNIEnv* env, jobject clazz, jint uid) { |
| 1013 | return static_cast<jint>(BpBinder::getBinderProxyCount(static_cast<uint32_t>(uid))); |
| 1014 | } |
| 1015 | |
| 1016 | static void android_os_BinderInternal_setBinderProxyCountWatermarks(JNIEnv* env, jobject clazz, |
| 1017 | jint high, jint low) |
| 1018 | { |
| 1019 | BpBinder::setBinderProxyCountWatermarks(high, low); |
| 1020 | } |
| 1021 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | // ---------------------------------------------------------------------------- |
| 1023 | |
| 1024 | static const JNINativeMethod gBinderInternalMethods[] = { |
| 1025 | /* name, signature, funcPtr */ |
| 1026 | { "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject }, |
| 1027 | { "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool }, |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 1028 | { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling }, |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 1029 | { "setMaxThreads", "(I)V", (void*)android_os_BinderInternal_setMaxThreads }, |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 1030 | { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }, |
| 1031 | { "nSetBinderProxyCountEnabled", "(Z)V", (void*)android_os_BinderInternal_setBinderProxyCountEnabled }, |
| 1032 | { "nGetBinderProxyPerUidCounts", "()Landroid/util/SparseIntArray;", (void*)android_os_BinderInternal_getBinderProxyPerUidCounts }, |
| 1033 | { "nGetBinderProxyCount", "(I)I", (void*)android_os_BinderInternal_getBinderProxyCount }, |
| 1034 | { "nSetBinderProxyCountWatermarks", "(II)V", (void*)android_os_BinderInternal_setBinderProxyCountWatermarks} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | }; |
| 1036 | |
| 1037 | const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal"; |
| 1038 | |
| 1039 | static int int_register_android_os_BinderInternal(JNIEnv* env) |
| 1040 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1041 | jclass clazz = FindClassOrDie(env, kBinderInternalPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1043 | gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1044 | gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V"); |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 1045 | gBinderInternalOffsets.mProxyLimitCallback = GetStaticMethodIDOrDie(env, clazz, "binderProxyLimitCallbackFromNative", "(I)V"); |
| 1046 | |
| 1047 | jclass SparseIntArrayClass = FindClassOrDie(env, "android/util/SparseIntArray"); |
| 1048 | gSparseIntArrayOffsets.classObject = MakeGlobalRefOrDie(env, SparseIntArrayClass); |
| 1049 | gSparseIntArrayOffsets.constructor = GetMethodIDOrDie(env, gSparseIntArrayOffsets.classObject, |
| 1050 | "<init>", "()V"); |
| 1051 | gSparseIntArrayOffsets.put = GetMethodIDOrDie(env, gSparseIntArrayOffsets.classObject, "put", |
| 1052 | "(II)V"); |
| 1053 | |
| 1054 | BpBinder::setLimitCallback(android_os_BinderInternal_proxyLimitcallback); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1056 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | env, kBinderInternalPathName, |
| 1058 | gBinderInternalMethods, NELEM(gBinderInternalMethods)); |
| 1059 | } |
| 1060 | |
| 1061 | // **************************************************************************** |
| 1062 | // **************************************************************************** |
| 1063 | // **************************************************************************** |
| 1064 | |
| 1065 | static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj) |
| 1066 | { |
| 1067 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1068 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | if (target == NULL) { |
| 1070 | return JNI_FALSE; |
| 1071 | } |
| 1072 | status_t err = target->pingBinder(); |
| 1073 | return err == NO_ERROR ? JNI_TRUE : JNI_FALSE; |
| 1074 | } |
| 1075 | |
| 1076 | static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj) |
| 1077 | { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1078 | IBinder* target = (IBinder*) env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | if (target != NULL) { |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1080 | const String16& desc = target->getInterfaceDescriptor(); |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 1081 | return env->NewString(reinterpret_cast<const jchar*>(desc.string()), |
| 1082 | desc.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | } |
| 1084 | jniThrowException(env, "java/lang/RuntimeException", |
| 1085 | "No binder found for object"); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj) |
| 1090 | { |
| 1091 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1092 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | if (target == NULL) { |
| 1094 | return JNI_FALSE; |
| 1095 | } |
| 1096 | bool alive = target->isBinderAlive(); |
| 1097 | return alive ? JNI_TRUE : JNI_FALSE; |
| 1098 | } |
| 1099 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1100 | static int getprocname(pid_t pid, char *buf, size_t len) { |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1101 | char filename[32]; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1102 | FILE *f; |
| 1103 | |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1104 | snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1105 | f = fopen(filename, "r"); |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1106 | if (!f) { |
| 1107 | *buf = '\0'; |
| 1108 | return 1; |
| 1109 | } |
| 1110 | if (!fgets(buf, len, f)) { |
| 1111 | *buf = '\0'; |
| 1112 | fclose(f); |
| 1113 | return 2; |
| 1114 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1115 | fclose(f); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static bool push_eventlog_string(char** pos, const char* end, const char* str) { |
| 1120 | jint len = strlen(str); |
| 1121 | int space_needed = 1 + sizeof(len) + len; |
| 1122 | if (end - *pos < space_needed) { |
Mark Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1123 | ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1124 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
| 1127 | **pos = EVENT_TYPE_STRING; |
| 1128 | (*pos)++; |
| 1129 | memcpy(*pos, &len, sizeof(len)); |
| 1130 | *pos += sizeof(len); |
| 1131 | memcpy(*pos, str, len); |
| 1132 | *pos += len; |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | static bool push_eventlog_int(char** pos, const char* end, jint val) { |
| 1137 | int space_needed = 1 + sizeof(val); |
| 1138 | if (end - *pos < space_needed) { |
Mark Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1139 | ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1140 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
| 1143 | **pos = EVENT_TYPE_INT; |
| 1144 | (*pos)++; |
| 1145 | memcpy(*pos, &val, sizeof(val)); |
| 1146 | *pos += sizeof(val); |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
| 1150 | // From frameworks/base/core/java/android/content/EventLogTags.logtags: |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1151 | |
| 1152 | static const bool kEnableBinderSample = false; |
| 1153 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1154 | #define LOGTAG_BINDER_OPERATION 52004 |
| 1155 | |
| 1156 | static void conditionally_log_binder_call(int64_t start_millis, |
| 1157 | IBinder* target, jint code) { |
| 1158 | int duration_ms = static_cast<int>(uptimeMillis() - start_millis); |
| 1159 | |
| 1160 | int sample_percent; |
| 1161 | if (duration_ms >= 500) { |
| 1162 | sample_percent = 100; |
| 1163 | } else { |
| 1164 | sample_percent = 100 * duration_ms / 500; |
| 1165 | if (sample_percent == 0) { |
| 1166 | return; |
| 1167 | } |
| 1168 | if (sample_percent < (random() % 100 + 1)) { |
| 1169 | return; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | char process_name[40]; |
| 1174 | getprocname(getpid(), process_name, sizeof(process_name)); |
| 1175 | String8 desc(target->getInterfaceDescriptor()); |
| 1176 | |
| 1177 | char buf[LOGGER_ENTRY_MAX_PAYLOAD]; |
| 1178 | buf[0] = EVENT_TYPE_LIST; |
| 1179 | buf[1] = 5; |
| 1180 | char* pos = &buf[2]; |
| 1181 | char* end = &buf[LOGGER_ENTRY_MAX_PAYLOAD - 1]; // leave room for final \n |
| 1182 | if (!push_eventlog_string(&pos, end, desc.string())) return; |
| 1183 | if (!push_eventlog_int(&pos, end, code)) return; |
| 1184 | if (!push_eventlog_int(&pos, end, duration_ms)) return; |
| 1185 | if (!push_eventlog_string(&pos, end, process_name)) return; |
| 1186 | if (!push_eventlog_int(&pos, end, sample_percent)) return; |
| 1187 | *(pos++) = '\n'; // conventional with EVENT_TYPE_LIST apparently. |
| 1188 | android_bWriteLog(LOGTAG_BINDER_OPERATION, buf, pos - buf); |
| 1189 | } |
| 1190 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1191 | // We only measure binder call durations to potentially log them if |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1192 | // we're on the main thread. |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1193 | static bool should_time_binder_calls() { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1194 | return (getpid() == gettid()); |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1198 | 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] | 1199 | { |
| 1200 | if (dataObj == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1201 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | return JNI_FALSE; |
| 1203 | } |
| 1204 | |
| 1205 | Parcel* data = parcelForJavaObject(env, dataObj); |
| 1206 | if (data == NULL) { |
| 1207 | return JNI_FALSE; |
| 1208 | } |
| 1209 | Parcel* reply = parcelForJavaObject(env, replyObj); |
| 1210 | if (reply == NULL && replyObj != NULL) { |
| 1211 | return JNI_FALSE; |
| 1212 | } |
| 1213 | |
| 1214 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1215 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | if (target == NULL) { |
| 1217 | jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!"); |
| 1218 | return JNI_FALSE; |
| 1219 | } |
| 1220 | |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1221 | 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] | 1222 | target, obj, code); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1223 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1224 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1225 | bool time_binder_calls; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1226 | int64_t start_millis; |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1227 | if (kEnableBinderSample) { |
| 1228 | // Only log the binder call duration for things on the Java-level main thread. |
| 1229 | // But if we don't |
| 1230 | time_binder_calls = should_time_binder_calls(); |
| 1231 | |
| 1232 | if (time_binder_calls) { |
| 1233 | start_millis = uptimeMillis(); |
| 1234 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1235 | } |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | //printf("Transact from Java code to %p sending: ", target); data->print(); |
| 1238 | status_t err = target->transact(code, *data, reply, flags); |
| 1239 | //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] | 1240 | |
| 1241 | if (kEnableBinderSample) { |
| 1242 | if (time_binder_calls) { |
| 1243 | conditionally_log_binder_call(start_millis, target, code); |
| 1244 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | if (err == NO_ERROR) { |
| 1248 | return JNI_TRUE; |
| 1249 | } else if (err == UNKNOWN_TRANSACTION) { |
| 1250 | return JNI_FALSE; |
| 1251 | } |
| 1252 | |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1253 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | return JNI_FALSE; |
| 1255 | } |
| 1256 | |
| 1257 | static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1258 | jobject recipient, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | { |
| 1260 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1261 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1266 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1268 | 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] | 1269 | assert(false); |
| 1270 | } |
| 1271 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1272 | LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | |
| 1274 | if (!target->localBinder()) { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1275 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1276 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1277 | sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1278 | status_t err = target->linkToDeath(jdr, NULL, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | if (err != NO_ERROR) { |
| 1280 | // Failure adding the death recipient, so clear its reference |
| 1281 | // now. |
| 1282 | jdr->clearReference(); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1283 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | static jboolean android_os_BinderProxy_unlinkToDeath(JNIEnv* env, jobject obj, |
| 1289 | jobject recipient, jint flags) |
| 1290 | { |
| 1291 | jboolean res = JNI_FALSE; |
| 1292 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1293 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | return res; |
| 1295 | } |
| 1296 | |
| 1297 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1298 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1300 | 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] | 1301 | return JNI_FALSE; |
| 1302 | } |
| 1303 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1304 | LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | |
| 1306 | if (!target->localBinder()) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1307 | status_t err = NAME_NOT_FOUND; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1308 | |
| 1309 | // If we find the matching recipient, proceed to unlink using that |
| 1310 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1311 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1312 | sp<JavaDeathRecipient> origJDR = list->find(recipient); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1313 | LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1314 | if (origJDR != NULL) { |
| 1315 | wp<IBinder::DeathRecipient> dr; |
| 1316 | err = target->unlinkToDeath(origJDR, NULL, flags, &dr); |
| 1317 | if (err == NO_ERROR && dr != NULL) { |
| 1318 | sp<IBinder::DeathRecipient> sdr = dr.promote(); |
| 1319 | JavaDeathRecipient* jdr = static_cast<JavaDeathRecipient*>(sdr.get()); |
| 1320 | if (jdr != NULL) { |
| 1321 | jdr->clearReference(); |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | if (err == NO_ERROR || err == DEAD_OBJECT) { |
| 1327 | res = JNI_TRUE; |
| 1328 | } else { |
| 1329 | jniThrowException(env, "java/util/NoSuchElementException", |
| 1330 | "Death link does not exist"); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | return res; |
| 1335 | } |
| 1336 | |
| 1337 | static void android_os_BinderProxy_destroy(JNIEnv* env, jobject obj) |
| 1338 | { |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1339 | // Don't race with construction/initialization |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 1340 | AutoMutex _l(gProxyLock); |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | IBinder* b = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1343 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1344 | DeathRecipientList* drl = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1345 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1346 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1347 | LOGDEATH("Destroying BinderProxy %p: binder=%p drl=%p\n", obj, b, drl); |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1348 | if (b != nullptr) { |
| 1349 | env->SetLongField(obj, gBinderProxyOffsets.mObject, 0); |
| 1350 | env->SetLongField(obj, gBinderProxyOffsets.mOrgue, 0); |
| 1351 | drl->decStrong((void*)javaObjectForIBinder); |
| 1352 | b->decStrong((void*)javaObjectForIBinder); |
| 1353 | } |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1354 | |
| 1355 | IPCThreadState::self()->flushCommands(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | // ---------------------------------------------------------------------------- |
| 1359 | |
| 1360 | static const JNINativeMethod gBinderProxyMethods[] = { |
| 1361 | /* name, signature, funcPtr */ |
| 1362 | {"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder}, |
| 1363 | {"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive}, |
| 1364 | {"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor}, |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 1365 | {"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] | 1366 | {"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath}, |
| 1367 | {"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath}, |
| 1368 | {"destroy", "()V", (void*)android_os_BinderProxy_destroy}, |
| 1369 | }; |
| 1370 | |
| 1371 | const char* const kBinderProxyPathName = "android/os/BinderProxy"; |
| 1372 | |
| 1373 | static int int_register_android_os_BinderProxy(JNIEnv* env) |
| 1374 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1375 | jclass clazz = FindClassOrDie(env, "java/lang/Error"); |
| 1376 | gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1378 | clazz = FindClassOrDie(env, kBinderProxyPathName); |
| 1379 | gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1380 | gBinderProxyOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", "()V"); |
| 1381 | gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice", |
| 1382 | "(Landroid/os/IBinder$DeathRecipient;)V"); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1383 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1384 | gBinderProxyOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J"); |
| 1385 | gBinderProxyOffsets.mSelf = GetFieldIDOrDie(env, clazz, "mSelf", |
| 1386 | "Ljava/lang/ref/WeakReference;"); |
| 1387 | gBinderProxyOffsets.mOrgue = GetFieldIDOrDie(env, clazz, "mOrgue", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1388 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1389 | clazz = FindClassOrDie(env, "java/lang/Class"); |
| 1390 | gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 1391 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1392 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | env, kBinderProxyPathName, |
| 1394 | gBinderProxyMethods, NELEM(gBinderProxyMethods)); |
| 1395 | } |
| 1396 | |
| 1397 | // **************************************************************************** |
| 1398 | // **************************************************************************** |
| 1399 | // **************************************************************************** |
| 1400 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1401 | int register_android_os_Binder(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | { |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1403 | if (int_register_android_os_Binder(env) < 0) |
| 1404 | return -1; |
| 1405 | if (int_register_android_os_BinderInternal(env) < 0) |
| 1406 | return -1; |
| 1407 | if (int_register_android_os_BinderProxy(env) < 0) |
| 1408 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1410 | jclass clazz = FindClassOrDie(env, "android/util/Log"); |
| 1411 | gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1412 | gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e", |
| 1413 | "(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] | 1414 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1415 | clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor"); |
| 1416 | gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1417 | gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", |
| 1418 | "(Ljava/io/FileDescriptor;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1420 | clazz = FindClassOrDie(env, "android/os/StrictMode"); |
| 1421 | gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1422 | gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz, |
| 1423 | "onBinderStrictModePolicyChange", "(I)V"); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1424 | |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 1425 | clazz = FindClassOrDie(env, "java/lang/Thread"); |
| 1426 | gThreadDispatchOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1427 | gThreadDispatchOffsets.mDispatchUncaughtException = GetMethodIDOrDie(env, clazz, |
| 1428 | "dispatchUncaughtException", "(Ljava/lang/Throwable;)V"); |
| 1429 | gThreadDispatchOffsets.mCurrentThread = GetStaticMethodIDOrDie(env, clazz, "currentThread", |
| 1430 | "()Ljava/lang/Thread;"); |
| 1431 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | return 0; |
| 1433 | } |