Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "Parcel" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "android_os_Parcel.h" |
| 21 | #include "android_util_Binder.h" |
| 22 | |
| 23 | #include "JNIHelp.h" |
| 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <stdio.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <utils/Atomic.h> |
| 32 | #include <binder/IInterface.h> |
| 33 | #include <binder/IPCThreadState.h> |
| 34 | #include <utils/Log.h> |
| 35 | #include <utils/SystemClock.h> |
| 36 | #include <utils/List.h> |
| 37 | #include <utils/KeyedVector.h> |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 38 | #include <binder/Parcel.h> |
| 39 | #include <binder/ProcessState.h> |
| 40 | #include <binder/IServiceManager.h> |
| 41 | #include <utils/threads.h> |
| 42 | #include <utils/String8.h> |
| 43 | |
| 44 | #include <ScopedUtfChars.h> |
| 45 | #include <ScopedLocalRef.h> |
| 46 | |
| 47 | #include <android_runtime/AndroidRuntime.h> |
| 48 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 49 | #include "core_jni_helpers.h" |
| 50 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 51 | //#undef ALOGV |
| 52 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
| 53 | |
| 54 | #define DEBUG_DEATH 0 |
| 55 | #if DEBUG_DEATH |
| 56 | #define LOGDEATH ALOGD |
| 57 | #else |
| 58 | #define LOGDEATH ALOGV |
| 59 | #endif |
| 60 | |
| 61 | namespace android { |
| 62 | |
| 63 | static struct parcel_offsets_t |
| 64 | { |
Insun Kang | 8902097 | 2012-05-01 14:13:19 +0900 | [diff] [blame] | 65 | jclass clazz; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 66 | jfieldID mNativePtr; |
Insun Kang | 8902097 | 2012-05-01 14:13:19 +0900 | [diff] [blame] | 67 | jmethodID obtain; |
| 68 | jmethodID recycle; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 69 | } gParcelOffsets; |
| 70 | |
| 71 | Parcel* parcelForJavaObject(JNIEnv* env, jobject obj) |
| 72 | { |
| 73 | if (obj) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 74 | Parcel* p = (Parcel*)env->GetLongField(obj, gParcelOffsets.mNativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 75 | if (p != NULL) { |
| 76 | return p; |
| 77 | } |
| 78 | jniThrowException(env, "java/lang/IllegalStateException", "Parcel has been finalized!"); |
| 79 | } |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Insun Kang | 8902097 | 2012-05-01 14:13:19 +0900 | [diff] [blame] | 83 | jobject createJavaParcelObject(JNIEnv* env) |
| 84 | { |
| 85 | return env->CallStaticObjectMethod(gParcelOffsets.clazz, gParcelOffsets.obtain); |
| 86 | } |
| 87 | |
| 88 | void recycleJavaParcelObject(JNIEnv* env, jobject parcelObj) |
| 89 | { |
| 90 | env->CallVoidMethod(parcelObj, gParcelOffsets.recycle); |
| 91 | } |
| 92 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 93 | static jint android_os_Parcel_dataSize(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 94 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 95 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 96 | return parcel ? parcel->dataSize() : 0; |
| 97 | } |
| 98 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 99 | static jint android_os_Parcel_dataAvail(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 100 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 101 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 102 | return parcel ? parcel->dataAvail() : 0; |
| 103 | } |
| 104 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 105 | static jint android_os_Parcel_dataPosition(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 106 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 107 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 108 | return parcel ? parcel->dataPosition() : 0; |
| 109 | } |
| 110 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 111 | static jint android_os_Parcel_dataCapacity(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 112 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 113 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 114 | return parcel ? parcel->dataCapacity() : 0; |
| 115 | } |
| 116 | |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 117 | static jlong android_os_Parcel_setDataSize(JNIEnv* env, jclass clazz, jlong nativePtr, jint size) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 118 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 119 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 120 | if (parcel != NULL) { |
| 121 | const status_t err = parcel->setDataSize(size); |
| 122 | if (err != NO_ERROR) { |
| 123 | signalExceptionForError(env, clazz, err); |
| 124 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 125 | return parcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 126 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 127 | return 0; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 130 | static void android_os_Parcel_setDataPosition(JNIEnv* env, jclass clazz, jlong nativePtr, jint pos) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 131 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 132 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 133 | if (parcel != NULL) { |
| 134 | parcel->setDataPosition(pos); |
| 135 | } |
| 136 | } |
| 137 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 138 | static void android_os_Parcel_setDataCapacity(JNIEnv* env, jclass clazz, jlong nativePtr, jint size) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 139 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 140 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 141 | if (parcel != NULL) { |
| 142 | const status_t err = parcel->setDataCapacity(size); |
| 143 | if (err != NO_ERROR) { |
| 144 | signalExceptionForError(env, clazz, err); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 149 | static jboolean android_os_Parcel_pushAllowFds(JNIEnv* env, jclass clazz, jlong nativePtr, jboolean allowFds) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 150 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 151 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 152 | jboolean ret = JNI_TRUE; |
| 153 | if (parcel != NULL) { |
| 154 | ret = (jboolean)parcel->pushAllowFds(allowFds); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 159 | static void android_os_Parcel_restoreAllowFds(JNIEnv* env, jclass clazz, jlong nativePtr, jboolean lastValue) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 160 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 161 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 162 | if (parcel != NULL) { |
| 163 | parcel->restoreAllowFds((bool)lastValue); |
| 164 | } |
| 165 | } |
| 166 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 167 | static void android_os_Parcel_writeNative(JNIEnv* env, jclass clazz, jlong nativePtr, jobject data, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 168 | jint offset, jint length) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 169 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 170 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 171 | if (parcel == NULL) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | const status_t err = parcel->writeInt32(length); |
| 176 | if (err != NO_ERROR) { |
| 177 | signalExceptionForError(env, clazz, err); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | void* dest = parcel->writeInplace(length); |
| 182 | if (dest == NULL) { |
| 183 | signalExceptionForError(env, clazz, NO_MEMORY); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)data, 0); |
| 188 | if (ar) { |
| 189 | memcpy(dest, ar + offset, length); |
| 190 | env->ReleasePrimitiveArrayCritical((jarray)data, ar, 0); |
| 191 | } |
| 192 | } |
| 193 | |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 194 | static void android_os_Parcel_writeBlob(JNIEnv* env, jclass clazz, jlong nativePtr, jobject data, |
| 195 | jint offset, jint length) { |
| 196 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
| 197 | if (parcel == NULL) { |
| 198 | return; |
| 199 | } |
| 200 | |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame] | 201 | if (data == NULL) { |
| 202 | const status_t err = parcel->writeInt32(-1); |
| 203 | if (err != NO_ERROR) { |
| 204 | signalExceptionForError(env, clazz, err); |
| 205 | } |
| 206 | return; |
| 207 | } |
| 208 | |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 209 | const status_t err = parcel->writeInt32(length); |
| 210 | if (err != NO_ERROR) { |
| 211 | signalExceptionForError(env, clazz, err); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | android::Parcel::WritableBlob blob; |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 216 | android::status_t err2 = parcel->writeBlob(length, false, &blob); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 217 | if (err2 != NO_ERROR) { |
| 218 | signalExceptionForError(env, clazz, err2); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)data, 0); |
| 223 | if (ar == NULL) { |
| 224 | memset(blob.data(), 0, length); |
| 225 | } else { |
| 226 | memcpy(blob.data(), ar + offset, length); |
| 227 | env->ReleasePrimitiveArrayCritical((jarray)data, ar, 0); |
| 228 | } |
| 229 | |
| 230 | blob.release(); |
| 231 | } |
| 232 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 233 | static void android_os_Parcel_writeInt(JNIEnv* env, jclass clazz, jlong nativePtr, jint val) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 234 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Brian Carlstrom | 849ea02 | 2015-06-23 18:02:12 -0700 | [diff] [blame] | 235 | if (parcel != NULL) { |
| 236 | const status_t err = parcel->writeInt32(val); |
| 237 | if (err != NO_ERROR) { |
| 238 | signalExceptionForError(env, clazz, err); |
| 239 | } |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 243 | static void android_os_Parcel_writeLong(JNIEnv* env, jclass clazz, jlong nativePtr, jlong val) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 244 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 245 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 246 | if (parcel != NULL) { |
| 247 | const status_t err = parcel->writeInt64(val); |
| 248 | if (err != NO_ERROR) { |
| 249 | signalExceptionForError(env, clazz, err); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 254 | static void android_os_Parcel_writeFloat(JNIEnv* env, jclass clazz, jlong nativePtr, jfloat val) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 255 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 256 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 257 | if (parcel != NULL) { |
| 258 | const status_t err = parcel->writeFloat(val); |
| 259 | if (err != NO_ERROR) { |
| 260 | signalExceptionForError(env, clazz, err); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 265 | static void android_os_Parcel_writeDouble(JNIEnv* env, jclass clazz, jlong nativePtr, jdouble val) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 266 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 267 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 268 | if (parcel != NULL) { |
| 269 | const status_t err = parcel->writeDouble(val); |
| 270 | if (err != NO_ERROR) { |
| 271 | signalExceptionForError(env, clazz, err); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 276 | static void android_os_Parcel_writeString(JNIEnv* env, jclass clazz, jlong nativePtr, jstring val) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 277 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 278 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 279 | if (parcel != NULL) { |
| 280 | status_t err = NO_MEMORY; |
| 281 | if (val) { |
| 282 | const jchar* str = env->GetStringCritical(val, 0); |
| 283 | if (str) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 284 | err = parcel->writeString16( |
| 285 | reinterpret_cast<const char16_t*>(str), |
| 286 | env->GetStringLength(val)); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 287 | env->ReleaseStringCritical(val, str); |
| 288 | } |
| 289 | } else { |
| 290 | err = parcel->writeString16(NULL, 0); |
| 291 | } |
| 292 | if (err != NO_ERROR) { |
| 293 | signalExceptionForError(env, clazz, err); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 298 | static void android_os_Parcel_writeStrongBinder(JNIEnv* env, jclass clazz, jlong nativePtr, jobject object) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 299 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 300 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 301 | if (parcel != NULL) { |
| 302 | const status_t err = parcel->writeStrongBinder(ibinderForJavaObject(env, object)); |
| 303 | if (err != NO_ERROR) { |
| 304 | signalExceptionForError(env, clazz, err); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 309 | static jlong android_os_Parcel_writeFileDescriptor(JNIEnv* env, jclass clazz, jlong nativePtr, jobject object) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 310 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 311 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 312 | if (parcel != NULL) { |
| 313 | const status_t err = |
| 314 | parcel->writeDupFileDescriptor(jniGetFDFromFileDescriptor(env, object)); |
| 315 | if (err != NO_ERROR) { |
| 316 | signalExceptionForError(env, clazz, err); |
| 317 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 318 | return parcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 319 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 320 | return 0; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 323 | static jbyteArray android_os_Parcel_createByteArray(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 324 | { |
| 325 | jbyteArray ret = NULL; |
| 326 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 327 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 328 | if (parcel != NULL) { |
| 329 | int32_t len = parcel->readInt32(); |
| 330 | |
| 331 | // sanity check the stored length against the true data size |
| 332 | if (len >= 0 && len <= (int32_t)parcel->dataAvail()) { |
| 333 | ret = env->NewByteArray(len); |
| 334 | |
| 335 | if (ret != NULL) { |
| 336 | jbyte* a2 = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0); |
| 337 | if (a2) { |
| 338 | const void* data = parcel->readInplace(len); |
| 339 | memcpy(a2, data, len); |
| 340 | env->ReleasePrimitiveArrayCritical(ret, a2, 0); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 349 | static jbyteArray android_os_Parcel_readBlob(JNIEnv* env, jclass clazz, jlong nativePtr) |
| 350 | { |
| 351 | jbyteArray ret = NULL; |
| 352 | |
| 353 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
| 354 | if (parcel != NULL) { |
| 355 | int32_t len = parcel->readInt32(); |
| 356 | if (len >= 0) { |
| 357 | android::Parcel::ReadableBlob blob; |
| 358 | android::status_t err = parcel->readBlob(len, &blob); |
| 359 | if (err != NO_ERROR) { |
| 360 | signalExceptionForError(env, clazz, err); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | ret = env->NewByteArray(len); |
| 365 | if (ret != NULL) { |
| 366 | jbyte* a2 = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0); |
| 367 | if (a2) { |
| 368 | memcpy(a2, blob.data(), len); |
| 369 | env->ReleasePrimitiveArrayCritical(ret, a2, 0); |
| 370 | } |
| 371 | } |
| 372 | blob.release(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 379 | static jint android_os_Parcel_readInt(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 380 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 381 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 382 | if (parcel != NULL) { |
| 383 | return parcel->readInt32(); |
| 384 | } |
| 385 | return 0; |
| 386 | } |
| 387 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 388 | static jlong android_os_Parcel_readLong(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 389 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 390 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 391 | if (parcel != NULL) { |
| 392 | return parcel->readInt64(); |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 397 | static jfloat android_os_Parcel_readFloat(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 398 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 399 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 400 | if (parcel != NULL) { |
| 401 | return parcel->readFloat(); |
| 402 | } |
| 403 | return 0; |
| 404 | } |
| 405 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 406 | static jdouble android_os_Parcel_readDouble(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 407 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 408 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 409 | if (parcel != NULL) { |
| 410 | return parcel->readDouble(); |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 415 | static jstring android_os_Parcel_readString(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 416 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 417 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 418 | if (parcel != NULL) { |
| 419 | size_t len; |
| 420 | const char16_t* str = parcel->readString16Inplace(&len); |
| 421 | if (str) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 422 | return env->NewString(reinterpret_cast<const jchar*>(str), len); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 423 | } |
| 424 | return NULL; |
| 425 | } |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 429 | static jobject android_os_Parcel_readStrongBinder(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 430 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 431 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 432 | if (parcel != NULL) { |
| 433 | return javaObjectForIBinder(env, parcel->readStrongBinder()); |
| 434 | } |
| 435 | return NULL; |
| 436 | } |
| 437 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 438 | static jobject android_os_Parcel_readFileDescriptor(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 439 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 440 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 441 | if (parcel != NULL) { |
| 442 | int fd = parcel->readFileDescriptor(); |
| 443 | if (fd < 0) return NULL; |
| 444 | fd = dup(fd); |
| 445 | if (fd < 0) return NULL; |
| 446 | return jniCreateFileDescriptor(env, fd); |
| 447 | } |
| 448 | return NULL; |
| 449 | } |
| 450 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 451 | static jobject android_os_Parcel_openFileDescriptor(JNIEnv* env, jclass clazz, |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 452 | jstring name, jint mode) |
| 453 | { |
| 454 | if (name == NULL) { |
| 455 | jniThrowNullPointerException(env, NULL); |
| 456 | return NULL; |
| 457 | } |
tao.pei | d415303 | 2015-06-02 10:06:20 +0800 | [diff] [blame] | 458 | ScopedUtfChars name8(env, name); |
| 459 | if (name8.c_str() == NULL) { |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
tao.pei | d415303 | 2015-06-02 10:06:20 +0800 | [diff] [blame] | 462 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 463 | int flags=0; |
| 464 | switch (mode&0x30000000) { |
| 465 | case 0: |
| 466 | case 0x10000000: |
| 467 | flags = O_RDONLY; |
| 468 | break; |
| 469 | case 0x20000000: |
| 470 | flags = O_WRONLY; |
| 471 | break; |
| 472 | case 0x30000000: |
| 473 | flags = O_RDWR; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (mode&0x08000000) flags |= O_CREAT; |
| 478 | if (mode&0x04000000) flags |= O_TRUNC; |
| 479 | if (mode&0x02000000) flags |= O_APPEND; |
| 480 | |
| 481 | int realMode = S_IRWXU|S_IRWXG; |
| 482 | if (mode&0x00000001) realMode |= S_IROTH; |
| 483 | if (mode&0x00000002) realMode |= S_IWOTH; |
| 484 | |
tao.pei | d415303 | 2015-06-02 10:06:20 +0800 | [diff] [blame] | 485 | int fd = open(name8.c_str(), flags, realMode); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 486 | if (fd < 0) { |
| 487 | jniThrowException(env, "java/io/FileNotFoundException", strerror(errno)); |
| 488 | return NULL; |
| 489 | } |
| 490 | jobject object = jniCreateFileDescriptor(env, fd); |
| 491 | if (object == NULL) { |
| 492 | close(fd); |
| 493 | } |
| 494 | return object; |
| 495 | } |
| 496 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 497 | static jobject android_os_Parcel_dupFileDescriptor(JNIEnv* env, jclass clazz, jobject orig) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 498 | { |
| 499 | if (orig == NULL) { |
| 500 | jniThrowNullPointerException(env, NULL); |
| 501 | return NULL; |
| 502 | } |
| 503 | int origfd = jniGetFDFromFileDescriptor(env, orig); |
| 504 | if (origfd < 0) { |
| 505 | jniThrowException(env, "java/lang/IllegalArgumentException", "bad FileDescriptor"); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | int fd = dup(origfd); |
| 510 | if (fd < 0) { |
| 511 | jniThrowIOException(env, errno); |
| 512 | return NULL; |
| 513 | } |
| 514 | jobject object = jniCreateFileDescriptor(env, fd); |
| 515 | if (object == NULL) { |
| 516 | close(fd); |
| 517 | } |
| 518 | return object; |
| 519 | } |
| 520 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 521 | static void android_os_Parcel_closeFileDescriptor(JNIEnv* env, jclass clazz, jobject object) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 522 | { |
| 523 | if (object == NULL) { |
| 524 | jniThrowNullPointerException(env, NULL); |
| 525 | return; |
| 526 | } |
| 527 | int fd = jniGetFDFromFileDescriptor(env, object); |
| 528 | if (fd >= 0) { |
| 529 | jniSetFileDescriptorOfFD(env, object, -1); |
| 530 | //ALOGI("Closing ParcelFileDescriptor %d\n", fd); |
| 531 | close(fd); |
| 532 | } |
| 533 | } |
| 534 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 535 | static void android_os_Parcel_clearFileDescriptor(JNIEnv* env, jclass clazz, jobject object) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 536 | { |
| 537 | if (object == NULL) { |
| 538 | jniThrowNullPointerException(env, NULL); |
| 539 | return; |
| 540 | } |
| 541 | int fd = jniGetFDFromFileDescriptor(env, object); |
| 542 | if (fd >= 0) { |
| 543 | jniSetFileDescriptorOfFD(env, object, -1); |
| 544 | } |
| 545 | } |
| 546 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 547 | static jlong android_os_Parcel_create(JNIEnv* env, jclass clazz) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 548 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 549 | Parcel* parcel = new Parcel(); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 550 | return reinterpret_cast<jlong>(parcel); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 553 | static jlong android_os_Parcel_freeBuffer(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 554 | { |
| 555 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
| 556 | if (parcel != NULL) { |
| 557 | parcel->freeData(); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 558 | return parcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 559 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 560 | return 0; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 563 | static void android_os_Parcel_destroy(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 564 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 565 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
| 566 | delete parcel; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 569 | static jbyteArray android_os_Parcel_marshall(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 570 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 571 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 572 | if (parcel == NULL) { |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | // do not marshall if there are binder objects in the parcel |
| 577 | if (parcel->objectsCount()) |
| 578 | { |
| 579 | jniThrowException(env, "java/lang/RuntimeException", "Tried to marshall a Parcel that contained Binder objects."); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | jbyteArray ret = env->NewByteArray(parcel->dataSize()); |
| 584 | |
| 585 | if (ret != NULL) |
| 586 | { |
| 587 | jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0); |
| 588 | if (array != NULL) |
| 589 | { |
| 590 | memcpy(array, parcel->data(), parcel->dataSize()); |
| 591 | env->ReleasePrimitiveArrayCritical(ret, array, 0); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return ret; |
| 596 | } |
| 597 | |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 598 | static jlong android_os_Parcel_unmarshall(JNIEnv* env, jclass clazz, jlong nativePtr, |
| 599 | jbyteArray data, jint offset, jint length) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 600 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 601 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 602 | if (parcel == NULL || length < 0) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 603 | return 0; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(data, 0); |
| 607 | if (array) |
| 608 | { |
| 609 | parcel->setDataSize(length); |
| 610 | parcel->setDataPosition(0); |
| 611 | |
| 612 | void* raw = parcel->writeInplace(length); |
| 613 | memcpy(raw, (array + offset), length); |
| 614 | |
| 615 | env->ReleasePrimitiveArrayCritical(data, array, 0); |
| 616 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 617 | return parcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 620 | static jlong android_os_Parcel_appendFrom(JNIEnv* env, jclass clazz, jlong thisNativePtr, |
| 621 | jlong otherNativePtr, jint offset, jint length) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 622 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 623 | Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 624 | if (thisParcel == NULL) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 625 | return 0; |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 626 | } |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 627 | Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 628 | if (otherParcel == NULL) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 629 | return thisParcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | status_t err = thisParcel->appendFrom(otherParcel, offset, length); |
| 633 | if (err != NO_ERROR) { |
| 634 | signalExceptionForError(env, clazz, err); |
| 635 | } |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 636 | return thisParcel->getOpenAshmemSize(); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 639 | static jboolean android_os_Parcel_hasFileDescriptors(JNIEnv* env, jclass clazz, jlong nativePtr) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 640 | { |
| 641 | jboolean ret = JNI_FALSE; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 642 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 643 | if (parcel != NULL) { |
| 644 | if (parcel->hasFileDescriptors()) { |
| 645 | ret = JNI_TRUE; |
| 646 | } |
| 647 | } |
| 648 | return ret; |
| 649 | } |
| 650 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 651 | static void android_os_Parcel_writeInterfaceToken(JNIEnv* env, jclass clazz, jlong nativePtr, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 652 | jstring name) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 653 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 654 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 655 | if (parcel != NULL) { |
| 656 | // In the current implementation, the token is just the serialized interface name that |
| 657 | // the caller expects to be invoking |
| 658 | const jchar* str = env->GetStringCritical(name, 0); |
| 659 | if (str != NULL) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 660 | parcel->writeInterfaceToken(String16( |
| 661 | reinterpret_cast<const char16_t*>(str), |
| 662 | env->GetStringLength(name))); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 663 | env->ReleaseStringCritical(name, str); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 668 | static void android_os_Parcel_enforceInterface(JNIEnv* env, jclass clazz, jlong nativePtr, jstring name) |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 669 | { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 670 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 671 | if (parcel != NULL) { |
| 672 | const jchar* str = env->GetStringCritical(name, 0); |
| 673 | if (str) { |
| 674 | IPCThreadState* threadState = IPCThreadState::self(); |
| 675 | const int32_t oldPolicy = threadState->getStrictModePolicy(); |
| 676 | const bool isValid = parcel->enforceInterface( |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 677 | String16(reinterpret_cast<const char16_t*>(str), |
| 678 | env->GetStringLength(name)), |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 679 | threadState); |
| 680 | env->ReleaseStringCritical(name, str); |
| 681 | if (isValid) { |
| 682 | const int32_t newPolicy = threadState->getStrictModePolicy(); |
| 683 | if (oldPolicy != newPolicy) { |
| 684 | // Need to keep the Java-level thread-local strict |
| 685 | // mode policy in sync for the libcore |
| 686 | // enforcements, which involves an upcall back |
| 687 | // into Java. (We can't modify the |
| 688 | // Parcel.enforceInterface signature, as it's |
| 689 | // pseudo-public, and used via AIDL |
| 690 | // auto-generation...) |
| 691 | set_dalvik_blockguard_policy(env, newPolicy); |
| 692 | } |
| 693 | return; // everything was correct -> return silently |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // all error conditions wind up here |
| 699 | jniThrowException(env, "java/lang/SecurityException", |
| 700 | "Binder invocation to an incorrect interface"); |
| 701 | } |
| 702 | |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 703 | static jlong android_os_Parcel_getGlobalAllocSize(JNIEnv* env, jclass clazz) |
| 704 | { |
| 705 | return Parcel::getGlobalAllocSize(); |
| 706 | } |
| 707 | |
| 708 | static jlong android_os_Parcel_getGlobalAllocCount(JNIEnv* env, jclass clazz) |
| 709 | { |
| 710 | return Parcel::getGlobalAllocCount(); |
| 711 | } |
| 712 | |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 713 | static jlong android_os_Parcel_getBlobAshmemSize(JNIEnv* env, jclass clazz, jlong nativePtr) |
| 714 | { |
| 715 | Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); |
| 716 | if (parcel != NULL) { |
| 717 | return parcel->getBlobAshmemSize(); |
| 718 | } |
| 719 | return 0; |
| 720 | } |
| 721 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 722 | // ---------------------------------------------------------------------------- |
| 723 | |
| 724 | static const JNINativeMethod gParcelMethods[] = { |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 725 | // @FastNative |
| 726 | {"nativeDataSize", "(J)I", (void*)android_os_Parcel_dataSize}, |
| 727 | // @FastNative |
| 728 | {"nativeDataAvail", "(J)I", (void*)android_os_Parcel_dataAvail}, |
| 729 | // @FastNative |
| 730 | {"nativeDataPosition", "(J)I", (void*)android_os_Parcel_dataPosition}, |
| 731 | // @FastNative |
| 732 | {"nativeDataCapacity", "(J)I", (void*)android_os_Parcel_dataCapacity}, |
| 733 | // @FastNative |
| 734 | {"nativeSetDataSize", "(JI)J", (void*)android_os_Parcel_setDataSize}, |
| 735 | // @FastNative |
| 736 | {"nativeSetDataPosition", "(JI)V", (void*)android_os_Parcel_setDataPosition}, |
| 737 | // @FastNative |
| 738 | {"nativeSetDataCapacity", "(JI)V", (void*)android_os_Parcel_setDataCapacity}, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 739 | |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 740 | // @FastNative |
| 741 | {"nativePushAllowFds", "(JZ)Z", (void*)android_os_Parcel_pushAllowFds}, |
| 742 | // @FastNative |
| 743 | {"nativeRestoreAllowFds", "(JZ)V", (void*)android_os_Parcel_restoreAllowFds}, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 744 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 745 | {"nativeWriteByteArray", "(J[BII)V", (void*)android_os_Parcel_writeNative}, |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 746 | {"nativeWriteBlob", "(J[BII)V", (void*)android_os_Parcel_writeBlob}, |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 747 | // @FastNative |
| 748 | {"nativeWriteInt", "(JI)V", (void*)android_os_Parcel_writeInt}, |
| 749 | // @FastNative |
| 750 | {"nativeWriteLong", "(JJ)V", (void*)android_os_Parcel_writeLong}, |
| 751 | // @FastNative |
| 752 | {"nativeWriteFloat", "(JF)V", (void*)android_os_Parcel_writeFloat}, |
| 753 | // @FastNative |
| 754 | {"nativeWriteDouble", "(JD)V", (void*)android_os_Parcel_writeDouble}, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 755 | {"nativeWriteString", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeString}, |
| 756 | {"nativeWriteStrongBinder", "(JLandroid/os/IBinder;)V", (void*)android_os_Parcel_writeStrongBinder}, |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 757 | {"nativeWriteFileDescriptor", "(JLjava/io/FileDescriptor;)J", (void*)android_os_Parcel_writeFileDescriptor}, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 758 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 759 | {"nativeCreateByteArray", "(J)[B", (void*)android_os_Parcel_createByteArray}, |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 760 | {"nativeReadBlob", "(J)[B", (void*)android_os_Parcel_readBlob}, |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 761 | // @FastNative |
| 762 | {"nativeReadInt", "(J)I", (void*)android_os_Parcel_readInt}, |
| 763 | // @FastNative |
| 764 | {"nativeReadLong", "(J)J", (void*)android_os_Parcel_readLong}, |
| 765 | // @FastNative |
| 766 | {"nativeReadFloat", "(J)F", (void*)android_os_Parcel_readFloat}, |
| 767 | // @FastNative |
| 768 | {"nativeReadDouble", "(J)D", (void*)android_os_Parcel_readDouble}, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 769 | {"nativeReadString", "(J)Ljava/lang/String;", (void*)android_os_Parcel_readString}, |
| 770 | {"nativeReadStrongBinder", "(J)Landroid/os/IBinder;", (void*)android_os_Parcel_readStrongBinder}, |
| 771 | {"nativeReadFileDescriptor", "(J)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor}, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 772 | |
| 773 | {"openFileDescriptor", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_openFileDescriptor}, |
| 774 | {"dupFileDescriptor", "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_dupFileDescriptor}, |
| 775 | {"closeFileDescriptor", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Parcel_closeFileDescriptor}, |
| 776 | {"clearFileDescriptor", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Parcel_clearFileDescriptor}, |
| 777 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 778 | {"nativeCreate", "()J", (void*)android_os_Parcel_create}, |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 779 | {"nativeFreeBuffer", "(J)J", (void*)android_os_Parcel_freeBuffer}, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 780 | {"nativeDestroy", "(J)V", (void*)android_os_Parcel_destroy}, |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 781 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 782 | {"nativeMarshall", "(J)[B", (void*)android_os_Parcel_marshall}, |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 783 | {"nativeUnmarshall", "(J[BII)J", (void*)android_os_Parcel_unmarshall}, |
| 784 | {"nativeAppendFrom", "(JJII)J", (void*)android_os_Parcel_appendFrom}, |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 785 | // @FastNative |
| 786 | {"nativeHasFileDescriptors", "(J)Z", (void*)android_os_Parcel_hasFileDescriptors}, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 787 | {"nativeWriteInterfaceToken", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeInterfaceToken}, |
| 788 | {"nativeEnforceInterface", "(JLjava/lang/String;)V", (void*)android_os_Parcel_enforceInterface}, |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 789 | |
| 790 | {"getGlobalAllocSize", "()J", (void*)android_os_Parcel_getGlobalAllocSize}, |
| 791 | {"getGlobalAllocCount", "()J", (void*)android_os_Parcel_getGlobalAllocCount}, |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 792 | |
Dan Sandler | aa86166 | 2015-04-21 10:24:32 -0400 | [diff] [blame] | 793 | {"nativeGetBlobAshmemSize", "(J)J", (void*)android_os_Parcel_getBlobAshmemSize}, |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | const char* const kParcelPathName = "android/os/Parcel"; |
| 797 | |
| 798 | int register_android_os_Parcel(JNIEnv* env) |
| 799 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 800 | jclass clazz = FindClassOrDie(env, kParcelPathName); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 801 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 802 | gParcelOffsets.clazz = MakeGlobalRefOrDie(env, clazz); |
| 803 | gParcelOffsets.mNativePtr = GetFieldIDOrDie(env, clazz, "mNativePtr", "J"); |
| 804 | gParcelOffsets.obtain = GetStaticMethodIDOrDie(env, clazz, "obtain", "()Landroid/os/Parcel;"); |
| 805 | gParcelOffsets.recycle = GetMethodIDOrDie(env, clazz, "recycle", "()V"); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 806 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 807 | return RegisterMethodsOrDie(env, kParcelPathName, gParcelMethods, NELEM(gParcelMethods)); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | }; |