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