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 <nativehelper/JNIPlatformHelp.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 <binder/IInterface.h>
32 #include <binder/IPCThreadState.h>
33 #include <cutils/atomic.h>
34 #include <utils/Log.h>
35 #include <utils/SystemClock.h>
36 #include <utils/List.h>
37 #include <utils/KeyedVector.h>
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 <nativehelper/ScopedUtfChars.h>
45 #include <nativehelper/ScopedLocalRef.h>
46
47 #include <android_runtime/AndroidRuntime.h>
48
49 #include "core_jni_helpers.h"
50
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 {
65 jclass clazz;
66 jfieldID mNativePtr;
67 jmethodID obtain;
68 jmethodID recycle;
69 } gParcelOffsets;
70
parcelForJavaObject(JNIEnv * env,jobject obj)71 Parcel* parcelForJavaObject(JNIEnv* env, jobject obj)
72 {
73 if (obj) {
74 Parcel* p = (Parcel*)env->GetLongField(obj, gParcelOffsets.mNativePtr);
75 if (p != NULL) {
76 return p;
77 }
78 jniThrowException(env, "java/lang/IllegalStateException", "Parcel has been finalized!");
79 }
80 return NULL;
81 }
82
createJavaParcelObject(JNIEnv * env)83 jobject createJavaParcelObject(JNIEnv* env)
84 {
85 return env->CallStaticObjectMethod(gParcelOffsets.clazz, gParcelOffsets.obtain);
86 }
87
recycleJavaParcelObject(JNIEnv * env,jobject parcelObj)88 void recycleJavaParcelObject(JNIEnv* env, jobject parcelObj)
89 {
90 env->CallVoidMethod(parcelObj, gParcelOffsets.recycle);
91 }
92
android_os_Parcel_markSensitive(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)93 static void android_os_Parcel_markSensitive(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
94 {
95 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
96 if (parcel) {
97 parcel->markSensitive();
98 }
99 }
100
android_os_Parcel_markForBinder(JNIEnv * env,jclass clazz,jlong nativePtr,jobject binder)101 static void android_os_Parcel_markForBinder(JNIEnv* env, jclass clazz, jlong nativePtr,
102 jobject binder)
103 {
104 LOG_ALWAYS_FATAL_IF(binder == nullptr, "Null binder specified for markForBinder");
105
106 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
107 if (parcel) {
108 sp<IBinder> nBinder = ibinderForJavaObject(env, binder);
109
110 if (nBinder == nullptr) {
111 ALOGE("Native binder in markForBinder is null for non-null jobject");
112 return;
113 }
114
115 parcel->markForBinder(nBinder);
116 }
117 }
118
android_os_Parcel_isForRpc(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)119 static jboolean android_os_Parcel_isForRpc(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr) {
120 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
121 return parcel ? parcel->isForRpc() : false;
122 }
123
android_os_Parcel_dataSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)124 static jint android_os_Parcel_dataSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
125 {
126 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
127 return parcel ? parcel->dataSize() : 0;
128 }
129
android_os_Parcel_dataAvail(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)130 static jint android_os_Parcel_dataAvail(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
131 {
132 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
133 return parcel ? parcel->dataAvail() : 0;
134 }
135
android_os_Parcel_dataPosition(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)136 static jint android_os_Parcel_dataPosition(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
137 {
138 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
139 return parcel ? parcel->dataPosition() : 0;
140 }
141
android_os_Parcel_dataCapacity(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)142 static jint android_os_Parcel_dataCapacity(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
143 {
144 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
145 return parcel ? parcel->dataCapacity() : 0;
146 }
147
android_os_Parcel_setDataSize(JNIEnv * env,jclass clazz,jlong nativePtr,jint size)148 static void android_os_Parcel_setDataSize(JNIEnv* env, jclass clazz, jlong nativePtr, jint size)
149 {
150 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
151 if (parcel != NULL) {
152 const status_t err = parcel->setDataSize(size);
153 if (err != NO_ERROR) {
154 signalExceptionForError(env, clazz, err);
155 }
156 }
157 }
158
android_os_Parcel_setDataPosition(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jint pos)159 static void android_os_Parcel_setDataPosition(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr, jint pos)
160 {
161 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
162 if (parcel != NULL) {
163 parcel->setDataPosition(pos);
164 }
165 }
166
android_os_Parcel_setDataCapacity(JNIEnv * env,jclass clazz,jlong nativePtr,jint size)167 static void android_os_Parcel_setDataCapacity(JNIEnv* env, jclass clazz, jlong nativePtr, jint size)
168 {
169 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
170 if (parcel != NULL) {
171 const status_t err = parcel->setDataCapacity(size);
172 if (err != NO_ERROR) {
173 signalExceptionForError(env, clazz, err);
174 }
175 }
176 }
177
android_os_Parcel_pushAllowFds(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jboolean allowFds)178 static jboolean android_os_Parcel_pushAllowFds(CRITICAL_JNI_PARAMS_COMMA
179 jlong nativePtr, jboolean allowFds)
180 {
181 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
182 jboolean ret = JNI_TRUE;
183 if (parcel != NULL) {
184 ret = (jboolean)parcel->pushAllowFds(allowFds);
185 }
186 return ret;
187 }
188
android_os_Parcel_restoreAllowFds(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jboolean lastValue)189 static void android_os_Parcel_restoreAllowFds(CRITICAL_JNI_PARAMS_COMMA
190 jlong nativePtr, jboolean lastValue)
191 {
192 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
193 if (parcel != NULL) {
194 parcel->restoreAllowFds((bool)lastValue);
195 }
196 }
197
android_os_Parcel_writeByteArray(JNIEnv * env,jclass clazz,jlong nativePtr,jobject data,jint offset,jint length)198 static void android_os_Parcel_writeByteArray(JNIEnv* env, jclass clazz, jlong nativePtr,
199 jobject data, jint offset, jint length)
200 {
201 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
202 if (parcel == NULL) {
203 return;
204 }
205
206 const status_t err = parcel->writeInt32(length);
207 if (err != NO_ERROR) {
208 signalExceptionForError(env, clazz, err);
209 return;
210 }
211
212 void* dest = parcel->writeInplace(length);
213 if (dest == NULL) {
214 signalExceptionForError(env, clazz, NO_MEMORY);
215 return;
216 }
217
218 jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)data, 0);
219 if (ar) {
220 memcpy(dest, ar + offset, length);
221 env->ReleasePrimitiveArrayCritical((jarray)data, ar, 0);
222 }
223 }
224
android_os_Parcel_writeBlob(JNIEnv * env,jclass clazz,jlong nativePtr,jobject data,jint offset,jint length)225 static void android_os_Parcel_writeBlob(JNIEnv* env, jclass clazz, jlong nativePtr, jobject data,
226 jint offset, jint length) {
227 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
228 if (parcel == NULL) {
229 return;
230 }
231
232 if (data == NULL) {
233 const status_t err = parcel->writeInt32(-1);
234 if (err != NO_ERROR) {
235 signalExceptionForError(env, clazz, err);
236 }
237 return;
238 }
239
240 const status_t err = parcel->writeInt32(length);
241 if (err != NO_ERROR) {
242 signalExceptionForError(env, clazz, err);
243 return;
244 }
245
246 android::Parcel::WritableBlob blob;
247 android::status_t err2 = parcel->writeBlob(length, false, &blob);
248 if (err2 != NO_ERROR) {
249 signalExceptionForError(env, clazz, err2);
250 return;
251 }
252
253 jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)data, 0);
254 if (ar == NULL) {
255 memset(blob.data(), 0, length);
256 } else {
257 memcpy(blob.data(), ar + offset, length);
258 env->ReleasePrimitiveArrayCritical((jarray)data, ar, 0);
259 }
260
261 blob.release();
262 }
263
android_os_Parcel_writeInt(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jint val)264 static int android_os_Parcel_writeInt(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr, jint val) {
265 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
266 return (parcel != NULL) ? parcel->writeInt32(val) : OK;
267 }
268
android_os_Parcel_writeLong(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jlong val)269 static int android_os_Parcel_writeLong(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr, jlong val) {
270 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
271 return (parcel != NULL) ? parcel->writeInt64(val) : OK;
272 }
273
android_os_Parcel_writeFloat(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jfloat val)274 static int android_os_Parcel_writeFloat(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr, jfloat val) {
275 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
276 return (parcel != NULL) ? parcel->writeFloat(val) : OK;
277 }
278
android_os_Parcel_writeDouble(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jdouble val)279 static int android_os_Parcel_writeDouble(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr, jdouble val) {
280 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
281 return (parcel != NULL) ? parcel->writeDouble(val) : OK;
282 }
283
android_os_Parcel_nativeSignalExceptionForError(JNIEnv * env,jclass clazz,jint err)284 static void android_os_Parcel_nativeSignalExceptionForError(JNIEnv* env, jclass clazz, jint err) {
285 signalExceptionForError(env, clazz, err);
286 }
287
android_os_Parcel_writeString8(JNIEnv * env,jclass clazz,jlong nativePtr,jstring val)288 static void android_os_Parcel_writeString8(JNIEnv *env, jclass clazz, jlong nativePtr,
289 jstring val) {
290 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
291 if (parcel != nullptr) {
292 status_t err = NO_ERROR;
293 if (val) {
294 // NOTE: Keep this logic in sync with Parcel.cpp
295 const size_t len = env->GetStringLength(val);
296 const size_t allocLen = env->GetStringUTFLength(val);
297 err = parcel->writeInt32(allocLen);
298 char *data = reinterpret_cast<char*>(parcel->writeInplace(allocLen + sizeof(char)));
299 if (data != nullptr) {
300 env->GetStringUTFRegion(val, 0, len, data);
301 *(data + allocLen) = 0;
302 } else {
303 err = NO_MEMORY;
304 }
305 } else {
306 err = parcel->writeString8(nullptr, 0);
307 }
308 if (err != NO_ERROR) {
309 signalExceptionForError(env, clazz, err);
310 }
311 }
312 }
313
android_os_Parcel_writeString16(JNIEnv * env,jclass clazz,jlong nativePtr,jstring val)314 static void android_os_Parcel_writeString16(JNIEnv *env, jclass clazz, jlong nativePtr,
315 jstring val) {
316 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
317 if (parcel != nullptr) {
318 status_t err = NO_ERROR;
319 if (val) {
320 // NOTE: Keep this logic in sync with Parcel.cpp
321 const size_t len = env->GetStringLength(val);
322 const size_t allocLen = len * sizeof(char16_t);
323 err = parcel->writeInt32(len);
324 char *data = reinterpret_cast<char*>(parcel->writeInplace(allocLen + sizeof(char16_t)));
325 if (data != nullptr) {
326 env->GetStringRegion(val, 0, len, reinterpret_cast<jchar*>(data));
327 *reinterpret_cast<char16_t*>(data + allocLen) = 0;
328 } else {
329 err = NO_MEMORY;
330 }
331 } else {
332 err = parcel->writeString16(nullptr, 0);
333 }
334 if (err != NO_ERROR) {
335 signalExceptionForError(env, clazz, err);
336 }
337 }
338 }
339
android_os_Parcel_writeStrongBinder(JNIEnv * env,jclass clazz,jlong nativePtr,jobject object)340 static void android_os_Parcel_writeStrongBinder(JNIEnv* env, jclass clazz, jlong nativePtr, jobject object)
341 {
342 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
343 if (parcel != NULL) {
344 const status_t err = parcel->writeStrongBinder(ibinderForJavaObject(env, object));
345 if (err != NO_ERROR) {
346 signalExceptionForError(env, clazz, err);
347 }
348 }
349 }
350
android_os_Parcel_writeFileDescriptor(JNIEnv * env,jclass clazz,jlong nativePtr,jobject object)351 static void android_os_Parcel_writeFileDescriptor(JNIEnv* env, jclass clazz, jlong nativePtr, jobject object)
352 {
353 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
354 if (parcel != NULL) {
355 const status_t err =
356 parcel->writeDupFileDescriptor(jniGetFDFromFileDescriptor(env, object));
357 if (err != NO_ERROR) {
358 signalExceptionForError(env, clazz, err);
359 }
360 }
361 }
362
android_os_Parcel_createByteArray(JNIEnv * env,jclass clazz,jlong nativePtr)363 static jbyteArray android_os_Parcel_createByteArray(JNIEnv* env, jclass clazz, jlong nativePtr)
364 {
365 jbyteArray ret = NULL;
366
367 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
368 if (parcel != NULL) {
369 int32_t len = parcel->readInt32();
370
371 // Validate the stored length against the true data size
372 if (len >= 0 && len <= (int32_t)parcel->dataAvail()) {
373 ret = env->NewByteArray(len);
374
375 if (ret != NULL) {
376 jbyte* a2 = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
377 if (a2) {
378 const void* data = parcel->readInplace(len);
379 if (data) {
380 memcpy(a2, data, len);
381 }
382 env->ReleasePrimitiveArrayCritical(ret, a2, 0);
383 if (!data) {
384 ret = NULL;
385 }
386 }
387 }
388 }
389 }
390
391 return ret;
392 }
393
android_os_Parcel_readByteArray(JNIEnv * env,jclass clazz,jlong nativePtr,jobject dest,jint destLen)394 static jboolean android_os_Parcel_readByteArray(JNIEnv* env, jclass clazz, jlong nativePtr,
395 jobject dest, jint destLen)
396 {
397 jboolean ret = JNI_FALSE;
398 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
399 if (parcel == NULL) {
400 return ret;
401 }
402
403 int32_t len = parcel->readInt32();
404 if (len >= 0 && len <= (int32_t)parcel->dataAvail() && len == destLen) {
405 jbyte* ar = (jbyte*)env->GetPrimitiveArrayCritical((jarray)dest, 0);
406 if (ar) {
407 const void* data = parcel->readInplace(len);
408 if (data) {
409 memcpy(ar, data, len);
410 ret = JNI_TRUE;
411 } else {
412 ret = JNI_FALSE;
413 }
414
415 env->ReleasePrimitiveArrayCritical((jarray)dest, ar, 0);
416 }
417 }
418 return ret;
419 }
420
android_os_Parcel_readBlob(JNIEnv * env,jclass clazz,jlong nativePtr)421 static jbyteArray android_os_Parcel_readBlob(JNIEnv* env, jclass clazz, jlong nativePtr)
422 {
423 jbyteArray ret = NULL;
424
425 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
426 if (parcel != NULL) {
427 int32_t len = parcel->readInt32();
428 if (len >= 0) {
429 android::Parcel::ReadableBlob blob;
430 android::status_t err = parcel->readBlob(len, &blob);
431 if (err != NO_ERROR) {
432 signalExceptionForError(env, clazz, err);
433 return NULL;
434 }
435
436 ret = env->NewByteArray(len);
437 if (ret != NULL) {
438 jbyte* a2 = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
439 if (a2) {
440 memcpy(a2, blob.data(), len);
441 env->ReleasePrimitiveArrayCritical(ret, a2, 0);
442 }
443 }
444 blob.release();
445 }
446 }
447
448 return ret;
449 }
450
android_os_Parcel_readInt(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)451 static jint android_os_Parcel_readInt(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
452 {
453 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
454 if (parcel != NULL) {
455 return parcel->readInt32();
456 }
457 return 0;
458 }
459
android_os_Parcel_readLong(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)460 static jlong android_os_Parcel_readLong(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
461 {
462 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
463 if (parcel != NULL) {
464 return parcel->readInt64();
465 }
466 return 0;
467 }
468
android_os_Parcel_readFloat(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)469 static jfloat android_os_Parcel_readFloat(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
470 {
471 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
472 if (parcel != NULL) {
473 return parcel->readFloat();
474 }
475 return 0;
476 }
477
android_os_Parcel_readDouble(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)478 static jdouble android_os_Parcel_readDouble(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
479 {
480 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
481 if (parcel != NULL) {
482 return parcel->readDouble();
483 }
484 return 0;
485 }
486
android_os_Parcel_readString8(JNIEnv * env,jclass clazz,jlong nativePtr)487 static jstring android_os_Parcel_readString8(JNIEnv* env, jclass clazz, jlong nativePtr)
488 {
489 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
490 if (parcel != NULL) {
491 size_t len;
492 const char* str = parcel->readString8Inplace(&len);
493 if (str) {
494 return env->NewStringUTF(str);
495 }
496 return NULL;
497 }
498 return NULL;
499 }
500
android_os_Parcel_readString16(JNIEnv * env,jclass clazz,jlong nativePtr)501 static jstring android_os_Parcel_readString16(JNIEnv* env, jclass clazz, jlong nativePtr)
502 {
503 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
504 if (parcel != NULL) {
505 size_t len;
506 const char16_t* str = parcel->readString16Inplace(&len);
507 if (str) {
508 return env->NewString(reinterpret_cast<const jchar*>(str), len);
509 }
510 return NULL;
511 }
512 return NULL;
513 }
514
android_os_Parcel_readStrongBinder(JNIEnv * env,jclass clazz,jlong nativePtr)515 static jobject android_os_Parcel_readStrongBinder(JNIEnv* env, jclass clazz, jlong nativePtr)
516 {
517 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
518 if (parcel != NULL) {
519 return javaObjectForIBinder(env, parcel->readStrongBinder());
520 }
521 return NULL;
522 }
523
android_os_Parcel_readFileDescriptor(JNIEnv * env,jclass clazz,jlong nativePtr)524 static jobject android_os_Parcel_readFileDescriptor(JNIEnv* env, jclass clazz, jlong nativePtr)
525 {
526 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
527 if (parcel != NULL) {
528 int fd = parcel->readFileDescriptor();
529 if (fd < 0) return NULL;
530 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
531 if (fd < 0) return NULL;
532 jobject jifd = jniCreateFileDescriptor(env, fd);
533 if (jifd == NULL) {
534 close(fd);
535 }
536 return jifd;
537 }
538 return NULL;
539 }
540
android_os_Parcel_create(JNIEnv * env,jclass clazz)541 static jlong android_os_Parcel_create(JNIEnv* env, jclass clazz)
542 {
543 Parcel* parcel = new Parcel();
544 return reinterpret_cast<jlong>(parcel);
545 }
546
android_os_Parcel_freeBuffer(JNIEnv * env,jclass clazz,jlong nativePtr)547 static void android_os_Parcel_freeBuffer(JNIEnv* env, jclass clazz, jlong nativePtr)
548 {
549 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
550 if (parcel != NULL) {
551 parcel->freeData();
552 }
553 }
554
android_os_Parcel_destroy(JNIEnv * env,jclass clazz,jlong nativePtr)555 static void android_os_Parcel_destroy(JNIEnv* env, jclass clazz, jlong nativePtr)
556 {
557 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
558 delete parcel;
559 }
560
android_os_Parcel_marshall(JNIEnv * env,jclass clazz,jlong nativePtr)561 static jbyteArray android_os_Parcel_marshall(JNIEnv* env, jclass clazz, jlong nativePtr)
562 {
563 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
564 if (parcel == NULL) {
565 return NULL;
566 }
567
568 if (parcel->isForRpc()) {
569 jniThrowException(env, "java/lang/RuntimeException", "Tried to marshall an RPC Parcel.");
570 return NULL;
571 }
572
573 if (parcel->objectsCount())
574 {
575 jniThrowException(env, "java/lang/RuntimeException",
576 "Tried to marshall a Parcel that contains objects (binders or FDs).");
577 return NULL;
578 }
579
580 jbyteArray ret = env->NewByteArray(parcel->dataSize());
581
582 if (ret != NULL)
583 {
584 jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
585 if (array != NULL)
586 {
587 memcpy(array, parcel->data(), parcel->dataSize());
588 env->ReleasePrimitiveArrayCritical(ret, array, 0);
589 }
590 }
591
592 return ret;
593 }
594
android_os_Parcel_unmarshall(JNIEnv * env,jclass clazz,jlong nativePtr,jbyteArray data,jint offset,jint length)595 static void android_os_Parcel_unmarshall(JNIEnv* env, jclass clazz, jlong nativePtr,
596 jbyteArray data, jint offset, jint length)
597 {
598 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
599 if (parcel == NULL || length < 0) {
600 return;
601 }
602
603 jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(data, 0);
604 if (array)
605 {
606 parcel->setDataSize(length);
607 parcel->setDataPosition(0);
608
609 void* raw = parcel->writeInplace(length);
610 memcpy(raw, (array + offset), length);
611
612 env->ReleasePrimitiveArrayCritical(data, array, 0);
613 }
614 }
615
android_os_Parcel_compareData(JNIEnv * env,jclass clazz,jlong thisNativePtr,jlong otherNativePtr)616 static jint android_os_Parcel_compareData(JNIEnv* env, jclass clazz, jlong thisNativePtr,
617 jlong otherNativePtr)
618 {
619 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
620 LOG_ALWAYS_FATAL_IF(thisParcel == nullptr, "Should not be null");
621
622 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
623 LOG_ALWAYS_FATAL_IF(otherParcel == nullptr, "Should not be null");
624
625 return thisParcel->compareData(*otherParcel);
626 }
627
android_os_Parcel_compareDataInRange(JNIEnv * env,jclass clazz,jlong thisNativePtr,jint thisOffset,jlong otherNativePtr,jint otherOffset,jint length)628 static jboolean android_os_Parcel_compareDataInRange(JNIEnv* env, jclass clazz, jlong thisNativePtr,
629 jint thisOffset, jlong otherNativePtr,
630 jint otherOffset, jint length) {
631 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
632 LOG_ALWAYS_FATAL_IF(thisParcel == nullptr, "Should not be null");
633
634 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
635 LOG_ALWAYS_FATAL_IF(otherParcel == nullptr, "Should not be null");
636
637 int result;
638 status_t err =
639 thisParcel->compareDataInRange(thisOffset, *otherParcel, otherOffset, length, &result);
640 if (err != NO_ERROR) {
641 signalExceptionForError(env, clazz, err);
642 return JNI_FALSE;
643 }
644 return (result == 0) ? JNI_TRUE : JNI_FALSE;
645 }
646
android_os_Parcel_appendFrom(JNIEnv * env,jclass clazz,jlong thisNativePtr,jlong otherNativePtr,jint offset,jint length)647 static void android_os_Parcel_appendFrom(JNIEnv* env, jclass clazz, jlong thisNativePtr,
648 jlong otherNativePtr, jint offset, jint length)
649 {
650 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
651 if (thisParcel == NULL) {
652 return;
653 }
654 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
655 if (otherParcel == NULL) {
656 return;
657 }
658
659 status_t err = thisParcel->appendFrom(otherParcel, offset, length);
660 if (err != NO_ERROR) {
661 signalExceptionForError(env, clazz, err);
662 }
663 return;
664 }
665
android_os_Parcel_hasBinders(JNIEnv * env,jclass clazz,jlong nativePtr)666 static jboolean android_os_Parcel_hasBinders(JNIEnv* env, jclass clazz, jlong nativePtr) {
667 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
668 if (parcel != NULL) {
669 bool result;
670 status_t err = parcel->hasBinders(&result);
671 if (err != NO_ERROR) {
672 signalExceptionForError(env, clazz, err);
673 return JNI_FALSE;
674 }
675 return result ? JNI_TRUE : JNI_FALSE;
676 }
677 return JNI_FALSE;
678 }
679
android_os_Parcel_hasBindersInRange(JNIEnv * env,jclass clazz,jlong nativePtr,jint offset,jint length)680 static jboolean android_os_Parcel_hasBindersInRange(JNIEnv* env, jclass clazz, jlong nativePtr,
681 jint offset, jint length) {
682 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
683 if (parcel != NULL) {
684 bool result;
685 status_t err = parcel->hasBindersInRange(offset, length, &result);
686 if (err != NO_ERROR) {
687 signalExceptionForError(env, clazz, err);
688 return JNI_FALSE;
689 }
690 return result ? JNI_TRUE : JNI_FALSE;
691 }
692 return JNI_FALSE;
693 }
694
android_os_Parcel_hasFileDescriptors(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)695 static jboolean android_os_Parcel_hasFileDescriptors(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
696 {
697 jboolean ret = JNI_FALSE;
698 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
699 if (parcel != NULL) {
700 if (parcel->hasFileDescriptors()) {
701 ret = JNI_TRUE;
702 }
703 }
704 return ret;
705 }
706
android_os_Parcel_hasFileDescriptorsInRange(JNIEnv * env,jclass clazz,jlong nativePtr,jint offset,jint length)707 static jboolean android_os_Parcel_hasFileDescriptorsInRange(JNIEnv* env, jclass clazz,
708 jlong nativePtr, jint offset,
709 jint length) {
710 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
711 if (parcel != NULL) {
712 bool result;
713 status_t err = parcel->hasFileDescriptorsInRange(offset, length, &result);
714 if (err != NO_ERROR) {
715 signalExceptionForError(env, clazz, err);
716 return JNI_FALSE;
717 }
718 return result ? JNI_TRUE : JNI_FALSE;
719 }
720 return JNI_FALSE;
721 }
722
723 // String tries to allocate itself on the stack, within a known size, but will
724 // make a heap allocation if not.
725 template <jsize StackReserve>
726 class StackString {
727 public:
StackString(JNIEnv * env,jstring str)728 StackString(JNIEnv* env, jstring str) : mEnv(env), mJStr(str) {
729 LOG_ALWAYS_FATAL_IF(str == nullptr);
730 mSize = env->GetStringLength(str);
731 if (mSize > StackReserve) {
732 mStr = new jchar[mSize];
733 } else {
734 mStr = &mBuffer[0];
735 }
736 mEnv->GetStringRegion(str, 0, mSize, mStr);
737 }
~StackString()738 ~StackString() {
739 if (mStr != &mBuffer[0]) {
740 delete[] mStr;
741 }
742 }
str()743 const jchar* str() { return mStr; }
size()744 jsize size() { return mSize; }
745
746 private:
747 JNIEnv* mEnv;
748 jstring mJStr;
749
750 jchar mBuffer[StackReserve];
751 // pointer to &mBuffer[0] if string fits in mBuffer, otherwise owned
752 jchar* mStr;
753 jsize mSize;
754 };
755
756 // This size is chosen to be longer than most interface descriptors.
757 // Ones longer than this will be allocated on the heap.
758 typedef StackString<64> InterfaceDescriptorString;
759
android_os_Parcel_writeInterfaceToken(JNIEnv * env,jclass clazz,jlong nativePtr,jstring name)760 static void android_os_Parcel_writeInterfaceToken(JNIEnv* env, jclass clazz, jlong nativePtr,
761 jstring name)
762 {
763 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
764 if (parcel != nullptr) {
765 InterfaceDescriptorString descriptor(env, name);
766 parcel->writeInterfaceToken(reinterpret_cast<const char16_t*>(descriptor.str()),
767 descriptor.size());
768 }
769 }
770
android_os_Parcel_enforceInterface(JNIEnv * env,jclass clazz,jlong nativePtr,jstring name)771 static void android_os_Parcel_enforceInterface(JNIEnv* env, jclass clazz, jlong nativePtr, jstring name)
772 {
773 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
774 if (parcel != nullptr) {
775 InterfaceDescriptorString descriptor(env, name);
776 IPCThreadState* threadState = IPCThreadState::self();
777 const int32_t oldPolicy = threadState->getStrictModePolicy();
778 const bool isValid =
779 parcel->enforceInterface(reinterpret_cast<const char16_t*>(descriptor.str()),
780 descriptor.size(), threadState);
781 if (isValid) {
782 const int32_t newPolicy = threadState->getStrictModePolicy();
783 if (oldPolicy != newPolicy) {
784 // Need to keep the Java-level thread-local strict
785 // mode policy in sync for the libcore
786 // enforcements, which involves an upcall back
787 // into Java. (We can't modify the
788 // Parcel.enforceInterface signature, as it's
789 // pseudo-public, and used via AIDL
790 // auto-generation...)
791 set_dalvik_blockguard_policy(env, newPolicy);
792 }
793 return; // everything was correct -> return silently
794 }
795 }
796
797 // all error conditions wind up here
798 jniThrowException(env, "java/lang/SecurityException",
799 "Binder invocation to an incorrect interface");
800 }
801
android_os_Parcel_getGlobalAllocSize(JNIEnv * env,jclass clazz)802 static jlong android_os_Parcel_getGlobalAllocSize(JNIEnv* env, jclass clazz)
803 {
804 return Parcel::getGlobalAllocSize();
805 }
806
android_os_Parcel_getGlobalAllocCount(JNIEnv * env,jclass clazz)807 static jlong android_os_Parcel_getGlobalAllocCount(JNIEnv* env, jclass clazz)
808 {
809 return Parcel::getGlobalAllocCount();
810 }
811
android_os_Parcel_getOpenAshmemSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)812 static jlong android_os_Parcel_getOpenAshmemSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
813 {
814 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
815 if (parcel != NULL) {
816 return parcel->getOpenAshmemSize();
817 }
818 return 0;
819 }
820
android_os_Parcel_readCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)821 static jint android_os_Parcel_readCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
822 {
823 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
824 if (parcel != NULL) {
825 return parcel->readCallingWorkSourceUid();
826 }
827 return IPCThreadState::kUnsetWorkSource;
828 }
829
android_os_Parcel_replaceCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jint uid)830 static jboolean android_os_Parcel_replaceCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA
831 jlong nativePtr, jint uid)
832 {
833 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
834 if (parcel != NULL) {
835 return parcel->replaceCallingWorkSourceUid(uid);
836 }
837 return false;
838 }
839
840 // ----------------------------------------------------------------------------
841 // clang-format off
842 static const JNINativeMethod gParcelMethods[] = {
843 // @CriticalNative
844 {"nativeMarkSensitive", "(J)V", (void*)android_os_Parcel_markSensitive},
845 // @FastNative
846 {"nativeMarkForBinder", "(JLandroid/os/IBinder;)V", (void*)android_os_Parcel_markForBinder},
847 // @CriticalNative
848 {"nativeIsForRpc", "(J)Z", (void*)android_os_Parcel_isForRpc},
849 // @CriticalNative
850 {"nativeDataSize", "(J)I", (void*)android_os_Parcel_dataSize},
851 // @CriticalNative
852 {"nativeDataAvail", "(J)I", (void*)android_os_Parcel_dataAvail},
853 // @CriticalNative
854 {"nativeDataPosition", "(J)I", (void*)android_os_Parcel_dataPosition},
855 // @CriticalNative
856 {"nativeDataCapacity", "(J)I", (void*)android_os_Parcel_dataCapacity},
857 // @FastNative
858 {"nativeSetDataSize", "(JI)V", (void*)android_os_Parcel_setDataSize},
859 // @CriticalNative
860 {"nativeSetDataPosition", "(JI)V", (void*)android_os_Parcel_setDataPosition},
861 // @FastNative
862 {"nativeSetDataCapacity", "(JI)V", (void*)android_os_Parcel_setDataCapacity},
863
864 // @CriticalNative
865 {"nativePushAllowFds", "(JZ)Z", (void*)android_os_Parcel_pushAllowFds},
866 // @CriticalNative
867 {"nativeRestoreAllowFds", "(JZ)V", (void*)android_os_Parcel_restoreAllowFds},
868
869 {"nativeWriteByteArray", "(J[BII)V", (void*)android_os_Parcel_writeByteArray},
870 {"nativeWriteBlob", "(J[BII)V", (void*)android_os_Parcel_writeBlob},
871 // @CriticalNative
872 {"nativeWriteInt", "(JI)I", (void*)android_os_Parcel_writeInt},
873 // @CriticalNative
874 {"nativeWriteLong", "(JJ)I", (void*)android_os_Parcel_writeLong},
875 // @CriticalNative
876 {"nativeWriteFloat", "(JF)I", (void*)android_os_Parcel_writeFloat},
877 // @CriticalNative
878 {"nativeWriteDouble", "(JD)I", (void*)android_os_Parcel_writeDouble},
879 {"nativeSignalExceptionForError", "(I)V", (void*)android_os_Parcel_nativeSignalExceptionForError},
880 // @FastNative
881 {"nativeWriteString8", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeString8},
882 // @FastNative
883 {"nativeWriteString16", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeString16},
884 // @FastNative
885 {"nativeWriteStrongBinder", "(JLandroid/os/IBinder;)V", (void*)android_os_Parcel_writeStrongBinder},
886 // @FastNative
887 {"nativeWriteFileDescriptor", "(JLjava/io/FileDescriptor;)V", (void*)android_os_Parcel_writeFileDescriptor},
888
889 {"nativeCreateByteArray", "(J)[B", (void*)android_os_Parcel_createByteArray},
890 {"nativeReadByteArray", "(J[BI)Z", (void*)android_os_Parcel_readByteArray},
891 {"nativeReadBlob", "(J)[B", (void*)android_os_Parcel_readBlob},
892 // @CriticalNative
893 {"nativeReadInt", "(J)I", (void*)android_os_Parcel_readInt},
894 // @CriticalNative
895 {"nativeReadLong", "(J)J", (void*)android_os_Parcel_readLong},
896 // @CriticalNative
897 {"nativeReadFloat", "(J)F", (void*)android_os_Parcel_readFloat},
898 // @CriticalNative
899 {"nativeReadDouble", "(J)D", (void*)android_os_Parcel_readDouble},
900 // @FastNative
901 {"nativeReadString8", "(J)Ljava/lang/String;", (void*)android_os_Parcel_readString8},
902 // @FastNative
903 {"nativeReadString16", "(J)Ljava/lang/String;", (void*)android_os_Parcel_readString16},
904 // @FastNative
905 {"nativeReadStrongBinder", "(J)Landroid/os/IBinder;", (void*)android_os_Parcel_readStrongBinder},
906 // @FastNative
907 {"nativeReadFileDescriptor", "(J)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor},
908
909 {"nativeCreate", "()J", (void*)android_os_Parcel_create},
910 {"nativeFreeBuffer", "(J)V", (void*)android_os_Parcel_freeBuffer},
911 {"nativeDestroy", "(J)V", (void*)android_os_Parcel_destroy},
912
913 {"nativeMarshall", "(J)[B", (void*)android_os_Parcel_marshall},
914 {"nativeUnmarshall", "(J[BII)V", (void*)android_os_Parcel_unmarshall},
915 {"nativeCompareData", "(JJ)I", (void*)android_os_Parcel_compareData},
916 {"nativeCompareDataInRange", "(JIJII)Z", (void*)android_os_Parcel_compareDataInRange},
917 {"nativeAppendFrom", "(JJII)V", (void*)android_os_Parcel_appendFrom},
918 // @CriticalNative
919 {"nativeHasFileDescriptors", "(J)Z", (void*)android_os_Parcel_hasFileDescriptors},
920 {"nativeHasFileDescriptorsInRange", "(JII)Z", (void*)android_os_Parcel_hasFileDescriptorsInRange},
921
922 {"nativeHasBinders", "(J)Z", (void*)android_os_Parcel_hasBinders},
923 {"nativeHasBindersInRange", "(JII)Z", (void*)android_os_Parcel_hasBindersInRange},
924 {"nativeWriteInterfaceToken", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeInterfaceToken},
925 {"nativeEnforceInterface", "(JLjava/lang/String;)V", (void*)android_os_Parcel_enforceInterface},
926
927 {"getGlobalAllocSize", "()J", (void*)android_os_Parcel_getGlobalAllocSize},
928 {"getGlobalAllocCount", "()J", (void*)android_os_Parcel_getGlobalAllocCount},
929
930 // @CriticalNative
931 {"nativeGetOpenAshmemSize", "(J)J", (void*)android_os_Parcel_getOpenAshmemSize},
932
933 // @CriticalNative
934 {"nativeReadCallingWorkSourceUid", "(J)I", (void*)android_os_Parcel_readCallingWorkSourceUid},
935 // @CriticalNative
936 {"nativeReplaceCallingWorkSourceUid", "(JI)Z", (void*)android_os_Parcel_replaceCallingWorkSourceUid},
937 };
938 // clang-format on
939
940 const char* const kParcelPathName = "android/os/Parcel";
941
register_android_os_Parcel(JNIEnv * env)942 int register_android_os_Parcel(JNIEnv* env)
943 {
944 jclass clazz = FindClassOrDie(env, kParcelPathName);
945
946 gParcelOffsets.clazz = MakeGlobalRefOrDie(env, clazz);
947 gParcelOffsets.mNativePtr = GetFieldIDOrDie(env, clazz, "mNativePtr", "J");
948 gParcelOffsets.obtain = GetStaticMethodIDOrDie(env, clazz, "obtain", "()Landroid/os/Parcel;");
949 gParcelOffsets.recycle = GetMethodIDOrDie(env, clazz, "recycle", "()V");
950
951 return RegisterMethodsOrDie(env, kParcelPathName, gParcelMethods, NELEM(gParcelMethods));
952 }
953
954 };
955