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
parcel_for_marshall(JNIEnv * env,jlong nativePtr)561 static Parcel* parcel_for_marshall(JNIEnv* env, jlong nativePtr) {
562 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
563 if (parcel == NULL) {
564 return NULL;
565 }
566
567 if (parcel->isForRpc()) {
568 jniThrowException(env, "java/lang/RuntimeException", "Tried to marshall an RPC Parcel.");
569 return NULL;
570 }
571
572 if (parcel->objectsCount())
573 {
574 jniThrowException(env, "java/lang/RuntimeException",
575 "Tried to marshall a Parcel that contains objects (binders or FDs).");
576 return NULL;
577 }
578
579 return parcel;
580 }
581
android_os_Parcel_marshall(JNIEnv * env,jclass clazz,jlong nativePtr)582 static jbyteArray android_os_Parcel_marshall(JNIEnv* env, jclass clazz, jlong nativePtr)
583 {
584 Parcel* parcel = parcel_for_marshall(env, nativePtr);
585 if (parcel == NULL) {
586 return NULL;
587 }
588
589 jbyteArray ret = env->NewByteArray(parcel->dataSize());
590
591 if (ret != NULL)
592 {
593 jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
594 if (array != NULL)
595 {
596 memcpy(array, parcel->data(), parcel->dataSize());
597 env->ReleasePrimitiveArrayCritical(ret, array, 0);
598 }
599 }
600
601 return ret;
602 }
603
ensure_capacity(JNIEnv * env,Parcel * parcel,jint remaining)604 static long ensure_capacity(JNIEnv* env, Parcel* parcel, jint remaining) {
605 long dataSize = parcel->dataSize();
606 if (remaining < dataSize) {
607 jnihelp::ThrowException(env, "java/nio/BufferOverflowException", "()V");
608 return -1;
609 }
610 return dataSize;
611 }
612
android_os_Parcel_marshall_array(JNIEnv * env,jclass clazz,jlong nativePtr,jbyteArray data,jint offset,jint remaining)613 static int android_os_Parcel_marshall_array(JNIEnv* env, jclass clazz, jlong nativePtr,
614 jbyteArray data, jint offset, jint remaining)
615 {
616 Parcel* parcel = parcel_for_marshall(env, nativePtr);
617 if (parcel == NULL) {
618 return 0;
619 }
620
621 long data_size = ensure_capacity(env, parcel, remaining);
622 if (data_size < 0) {
623 return 0;
624 }
625
626 jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(data, 0);
627 if (array != NULL)
628 {
629 memcpy(array + offset, parcel->data(), data_size);
630 env->ReleasePrimitiveArrayCritical(data, array, 0);
631 }
632 return data_size;
633 }
634
android_os_Parcel_marshall_buffer(JNIEnv * env,jclass clazz,jlong nativePtr,jobject javaBuffer,jint offset,jint remaining)635 static int android_os_Parcel_marshall_buffer(JNIEnv* env, jclass clazz, jlong nativePtr,
636 jobject javaBuffer, jint offset, jint remaining) {
637 Parcel* parcel = parcel_for_marshall(env, nativePtr);
638 if (parcel == NULL) {
639 return 0;
640 }
641
642 long data_size = ensure_capacity(env, parcel, remaining);
643 if (data_size < 0) {
644 return 0;
645 }
646
647 jbyte* buffer = (jbyte*)env->GetDirectBufferAddress(javaBuffer);
648 if (buffer != NULL) {
649 memcpy(buffer + offset, parcel->data(), data_size);
650 }
651 return data_size;
652 }
653
android_os_Parcel_unmarshall(JNIEnv * env,jclass clazz,jlong nativePtr,jbyteArray data,jint offset,jint length)654 static void android_os_Parcel_unmarshall(JNIEnv* env, jclass clazz, jlong nativePtr,
655 jbyteArray data, jint offset, jint length)
656 {
657 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
658 if (parcel == NULL || length < 0) {
659 return;
660 }
661
662 jbyte* array = (jbyte*)env->GetPrimitiveArrayCritical(data, 0);
663 if (array)
664 {
665 parcel->setDataSize(length);
666 parcel->setDataPosition(0);
667
668 void* raw = parcel->writeInplace(length);
669 memcpy(raw, (array + offset), length);
670
671 env->ReleasePrimitiveArrayCritical(data, array, 0);
672 }
673 }
674
android_os_Parcel_unmarshall_buffer(JNIEnv * env,jclass clazz,jlong nativePtr,jobject javaBuffer,jint offset,jint length)675 static void android_os_Parcel_unmarshall_buffer(JNIEnv* env, jclass clazz, jlong nativePtr,
676 jobject javaBuffer, jint offset, jint length)
677 {
678 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
679 if (parcel == NULL || length < 0) {
680 return;
681 }
682
683 jbyte* buffer = (jbyte*)env->GetDirectBufferAddress(javaBuffer);
684 if (buffer)
685 {
686 parcel->setDataSize(length);
687 parcel->setDataPosition(0);
688
689 void* raw = parcel->writeInplace(length);
690 memcpy(raw, (buffer + offset), length);
691 }
692 }
693
android_os_Parcel_compareData(JNIEnv * env,jclass clazz,jlong thisNativePtr,jlong otherNativePtr)694 static jint android_os_Parcel_compareData(JNIEnv* env, jclass clazz, jlong thisNativePtr,
695 jlong otherNativePtr)
696 {
697 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
698 LOG_ALWAYS_FATAL_IF(thisParcel == nullptr, "Should not be null");
699
700 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
701 LOG_ALWAYS_FATAL_IF(otherParcel == nullptr, "Should not be null");
702
703 return thisParcel->compareData(*otherParcel);
704 }
705
android_os_Parcel_compareDataInRange(JNIEnv * env,jclass clazz,jlong thisNativePtr,jint thisOffset,jlong otherNativePtr,jint otherOffset,jint length)706 static jboolean android_os_Parcel_compareDataInRange(JNIEnv* env, jclass clazz, jlong thisNativePtr,
707 jint thisOffset, jlong otherNativePtr,
708 jint otherOffset, jint length) {
709 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
710 LOG_ALWAYS_FATAL_IF(thisParcel == nullptr, "Should not be null");
711
712 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
713 LOG_ALWAYS_FATAL_IF(otherParcel == nullptr, "Should not be null");
714
715 int result;
716 status_t err =
717 thisParcel->compareDataInRange(thisOffset, *otherParcel, otherOffset, length, &result);
718 if (err != NO_ERROR) {
719 signalExceptionForError(env, clazz, err);
720 return JNI_FALSE;
721 }
722 return (result == 0) ? JNI_TRUE : JNI_FALSE;
723 }
724
android_os_Parcel_appendFrom(JNIEnv * env,jclass clazz,jlong thisNativePtr,jlong otherNativePtr,jint offset,jint length)725 static void android_os_Parcel_appendFrom(JNIEnv* env, jclass clazz, jlong thisNativePtr,
726 jlong otherNativePtr, jint offset, jint length)
727 {
728 Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
729 if (thisParcel == NULL) {
730 return;
731 }
732 Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
733 if (otherParcel == NULL) {
734 return;
735 }
736
737 status_t err = thisParcel->appendFrom(otherParcel, offset, length);
738 if (err != NO_ERROR) {
739 signalExceptionForError(env, clazz, err);
740 }
741 return;
742 }
743
android_os_Parcel_hasBinders(JNIEnv * env,jclass clazz,jlong nativePtr)744 static jboolean android_os_Parcel_hasBinders(JNIEnv* env, jclass clazz, jlong nativePtr) {
745 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
746 if (parcel != NULL) {
747 bool result;
748 status_t err = parcel->hasBinders(&result);
749 if (err != NO_ERROR) {
750 signalExceptionForError(env, clazz, err);
751 return JNI_FALSE;
752 }
753 return result ? JNI_TRUE : JNI_FALSE;
754 }
755 return JNI_FALSE;
756 }
757
android_os_Parcel_hasBindersInRange(JNIEnv * env,jclass clazz,jlong nativePtr,jint offset,jint length)758 static jboolean android_os_Parcel_hasBindersInRange(JNIEnv* env, jclass clazz, jlong nativePtr,
759 jint offset, jint length) {
760 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
761 if (parcel != NULL) {
762 bool result;
763 status_t err = parcel->hasBindersInRange(offset, length, &result);
764 if (err != NO_ERROR) {
765 signalExceptionForError(env, clazz, err);
766 return JNI_FALSE;
767 }
768 return result ? JNI_TRUE : JNI_FALSE;
769 }
770 return JNI_FALSE;
771 }
772
android_os_Parcel_hasFileDescriptors(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)773 static jboolean android_os_Parcel_hasFileDescriptors(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
774 {
775 jboolean ret = JNI_FALSE;
776 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
777 if (parcel != NULL) {
778 if (parcel->hasFileDescriptors()) {
779 ret = JNI_TRUE;
780 }
781 }
782 return ret;
783 }
784
android_os_Parcel_hasFileDescriptorsInRange(JNIEnv * env,jclass clazz,jlong nativePtr,jint offset,jint length)785 static jboolean android_os_Parcel_hasFileDescriptorsInRange(JNIEnv* env, jclass clazz,
786 jlong nativePtr, jint offset,
787 jint length) {
788 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
789 if (parcel != NULL) {
790 bool result;
791 status_t err = parcel->hasFileDescriptorsInRange(offset, length, &result);
792 if (err != NO_ERROR) {
793 signalExceptionForError(env, clazz, err);
794 return JNI_FALSE;
795 }
796 return result ? JNI_TRUE : JNI_FALSE;
797 }
798 return JNI_FALSE;
799 }
800
801 // String tries to allocate itself on the stack, within a known size, but will
802 // make a heap allocation if not.
803 template <jsize StackReserve>
804 class StackString {
805 public:
StackString(JNIEnv * env,jstring str)806 StackString(JNIEnv* env, jstring str) : mEnv(env), mJStr(str) {
807 LOG_ALWAYS_FATAL_IF(str == nullptr);
808 mSize = env->GetStringLength(str);
809 if (mSize > StackReserve) {
810 mStr = new jchar[mSize];
811 } else {
812 mStr = &mBuffer[0];
813 }
814 mEnv->GetStringRegion(str, 0, mSize, mStr);
815 }
~StackString()816 ~StackString() {
817 if (mStr != &mBuffer[0]) {
818 delete[] mStr;
819 }
820 }
str()821 const jchar* str() { return mStr; }
size()822 jsize size() { return mSize; }
823
824 private:
825 JNIEnv* mEnv;
826 jstring mJStr;
827
828 jchar mBuffer[StackReserve];
829 // pointer to &mBuffer[0] if string fits in mBuffer, otherwise owned
830 jchar* mStr;
831 jsize mSize;
832 };
833
834 // This size is chosen to be longer than most interface descriptors.
835 // Ones longer than this will be allocated on the heap.
836 typedef StackString<64> InterfaceDescriptorString;
837
android_os_Parcel_writeInterfaceToken(JNIEnv * env,jclass clazz,jlong nativePtr,jstring name)838 static void android_os_Parcel_writeInterfaceToken(JNIEnv* env, jclass clazz, jlong nativePtr,
839 jstring name)
840 {
841 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
842 if (parcel != nullptr) {
843 InterfaceDescriptorString descriptor(env, name);
844 parcel->writeInterfaceToken(reinterpret_cast<const char16_t*>(descriptor.str()),
845 descriptor.size());
846 }
847 }
848
android_os_Parcel_enforceInterface(JNIEnv * env,jclass clazz,jlong nativePtr,jstring name)849 static void android_os_Parcel_enforceInterface(JNIEnv* env, jclass clazz, jlong nativePtr, jstring name)
850 {
851 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
852 if (parcel != nullptr) {
853 InterfaceDescriptorString descriptor(env, name);
854 IPCThreadState* threadState = IPCThreadState::self();
855 const int32_t oldPolicy = threadState->getStrictModePolicy();
856 const bool isValid =
857 parcel->enforceInterface(reinterpret_cast<const char16_t*>(descriptor.str()),
858 descriptor.size(), threadState);
859 if (isValid) {
860 const int32_t newPolicy = threadState->getStrictModePolicy();
861 if (oldPolicy != newPolicy) {
862 // Need to keep the Java-level thread-local strict
863 // mode policy in sync for the libcore
864 // enforcements, which involves an upcall back
865 // into Java. (We can't modify the
866 // Parcel.enforceInterface signature, as it's
867 // pseudo-public, and used via AIDL
868 // auto-generation...)
869 set_dalvik_blockguard_policy(env, newPolicy);
870 }
871 return; // everything was correct -> return silently
872 }
873 }
874
875 // all error conditions wind up here
876 jniThrowException(env, "java/lang/SecurityException",
877 "Binder invocation to an incorrect interface");
878 }
879
android_os_Parcel_getGlobalAllocSize(JNIEnv * env,jclass clazz)880 static jlong android_os_Parcel_getGlobalAllocSize(JNIEnv* env, jclass clazz)
881 {
882 return Parcel::getGlobalAllocSize();
883 }
884
android_os_Parcel_getGlobalAllocCount(JNIEnv * env,jclass clazz)885 static jlong android_os_Parcel_getGlobalAllocCount(JNIEnv* env, jclass clazz)
886 {
887 return Parcel::getGlobalAllocCount();
888 }
889
android_os_Parcel_getOpenAshmemSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)890 static jlong android_os_Parcel_getOpenAshmemSize(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
891 {
892 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
893 if (parcel != NULL) {
894 return parcel->getOpenAshmemSize();
895 }
896 return 0;
897 }
898
android_os_Parcel_readCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)899 static jint android_os_Parcel_readCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr)
900 {
901 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
902 if (parcel != NULL) {
903 return parcel->readCallingWorkSourceUid();
904 }
905 return IPCThreadState::kUnsetWorkSource;
906 }
907
android_os_Parcel_replaceCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA jlong nativePtr,jint uid)908 static jboolean android_os_Parcel_replaceCallingWorkSourceUid(CRITICAL_JNI_PARAMS_COMMA
909 jlong nativePtr, jint uid)
910 {
911 Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);
912 if (parcel != NULL) {
913 return parcel->replaceCallingWorkSourceUid(uid);
914 }
915 return false;
916 }
917
918 // ----------------------------------------------------------------------------
919 // clang-format off
920 static const JNINativeMethod gParcelMethods[] = {
921 // @CriticalNative
nativeMarkSensitive(J)922 {"nativeMarkSensitive", "(J)V", (void*)android_os_Parcel_markSensitive},
923 // @FastNative
nativeMarkForBinder(JLandroid/os/IBinder;)924 {"nativeMarkForBinder", "(JLandroid/os/IBinder;)V", (void*)android_os_Parcel_markForBinder},
925 // @CriticalNative
nativeIsForRpc(J)926 {"nativeIsForRpc", "(J)Z", (void*)android_os_Parcel_isForRpc},
927 // @CriticalNative
nativeDataSize(J)928 {"nativeDataSize", "(J)I", (void*)android_os_Parcel_dataSize},
929 // @CriticalNative
nativeDataAvail(J)930 {"nativeDataAvail", "(J)I", (void*)android_os_Parcel_dataAvail},
931 // @CriticalNative
nativeDataPosition(J)932 {"nativeDataPosition", "(J)I", (void*)android_os_Parcel_dataPosition},
933 // @CriticalNative
nativeDataCapacity(J)934 {"nativeDataCapacity", "(J)I", (void*)android_os_Parcel_dataCapacity},
935 // @FastNative
nativeSetDataSize(JI)936 {"nativeSetDataSize", "(JI)V", (void*)android_os_Parcel_setDataSize},
937 // @CriticalNative
nativeSetDataPosition(JI)938 {"nativeSetDataPosition", "(JI)V", (void*)android_os_Parcel_setDataPosition},
939 // @FastNative
nativeSetDataCapacity(JI)940 {"nativeSetDataCapacity", "(JI)V", (void*)android_os_Parcel_setDataCapacity},
941
942 // @CriticalNative
nativePushAllowFds(JZ)943 {"nativePushAllowFds", "(JZ)Z", (void*)android_os_Parcel_pushAllowFds},
944 // @CriticalNative
nativeRestoreAllowFds(JZ)945 {"nativeRestoreAllowFds", "(JZ)V", (void*)android_os_Parcel_restoreAllowFds},
946
nativeWriteByteArray(J[BII)947 {"nativeWriteByteArray", "(J[BII)V", (void*)android_os_Parcel_writeByteArray},
nativeWriteBlob(J[BII)948 {"nativeWriteBlob", "(J[BII)V", (void*)android_os_Parcel_writeBlob},
949 // @CriticalNative
nativeWriteInt(JI)950 {"nativeWriteInt", "(JI)I", (void*)android_os_Parcel_writeInt},
951 // @CriticalNative
nativeWriteLong(JJ)952 {"nativeWriteLong", "(JJ)I", (void*)android_os_Parcel_writeLong},
953 // @CriticalNative
nativeWriteFloat(JF)954 {"nativeWriteFloat", "(JF)I", (void*)android_os_Parcel_writeFloat},
955 // @CriticalNative
nativeWriteDouble(JD)956 {"nativeWriteDouble", "(JD)I", (void*)android_os_Parcel_writeDouble},
nativeSignalExceptionForError(I)957 {"nativeSignalExceptionForError", "(I)V", (void*)android_os_Parcel_nativeSignalExceptionForError},
958 // @FastNative
nativeWriteString8(JLjava/lang/String;)959 {"nativeWriteString8", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeString8},
960 // @FastNative
nativeWriteString16(JLjava/lang/String;)961 {"nativeWriteString16", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeString16},
962 // @FastNative
nativeWriteStrongBinder(JLandroid/os/IBinder;)963 {"nativeWriteStrongBinder", "(JLandroid/os/IBinder;)V", (void*)android_os_Parcel_writeStrongBinder},
964 // @FastNative
nativeWriteFileDescriptor(JLjava/io/FileDescriptor;)965 {"nativeWriteFileDescriptor", "(JLjava/io/FileDescriptor;)V", (void*)android_os_Parcel_writeFileDescriptor},
966
nativeCreateByteArray(J)967 {"nativeCreateByteArray", "(J)[B", (void*)android_os_Parcel_createByteArray},
nativeReadByteArray(J[BI)968 {"nativeReadByteArray", "(J[BI)Z", (void*)android_os_Parcel_readByteArray},
nativeReadBlob(J)969 {"nativeReadBlob", "(J)[B", (void*)android_os_Parcel_readBlob},
970 // @CriticalNative
nativeReadInt(J)971 {"nativeReadInt", "(J)I", (void*)android_os_Parcel_readInt},
972 // @CriticalNative
nativeReadLong(J)973 {"nativeReadLong", "(J)J", (void*)android_os_Parcel_readLong},
974 // @CriticalNative
nativeReadFloat(J)975 {"nativeReadFloat", "(J)F", (void*)android_os_Parcel_readFloat},
976 // @CriticalNative
nativeReadDouble(J)977 {"nativeReadDouble", "(J)D", (void*)android_os_Parcel_readDouble},
978 // @FastNative
nativeReadString8(J)979 {"nativeReadString8", "(J)Ljava/lang/String;", (void*)android_os_Parcel_readString8},
980 // @FastNative
nativeReadString16(J)981 {"nativeReadString16", "(J)Ljava/lang/String;", (void*)android_os_Parcel_readString16},
982 // @FastNative
nativeReadStrongBinder(J)983 {"nativeReadStrongBinder", "(J)Landroid/os/IBinder;", (void*)android_os_Parcel_readStrongBinder},
984 // @FastNative
nativeReadFileDescriptor(J)985 {"nativeReadFileDescriptor", "(J)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor},
986
nativeCreate()987 {"nativeCreate", "()J", (void*)android_os_Parcel_create},
nativeFreeBuffer(J)988 {"nativeFreeBuffer", "(J)V", (void*)android_os_Parcel_freeBuffer},
nativeDestroy(J)989 {"nativeDestroy", "(J)V", (void*)android_os_Parcel_destroy},
990
nativeMarshall(J)991 {"nativeMarshall", "(J)[B", (void*)android_os_Parcel_marshall},
nativeMarshallArray(J[BII)992 {"nativeMarshallArray", "(J[BII)I", (void*)android_os_Parcel_marshall_array},
nativeMarshallBuffer(JLjava/nio/ByteBuffer;II)993 {"nativeMarshallBuffer", "(JLjava/nio/ByteBuffer;II)I", (void*)android_os_Parcel_marshall_buffer},
nativeUnmarshall(J[BII)994 {"nativeUnmarshall", "(J[BII)V", (void*)android_os_Parcel_unmarshall},
nativeUnmarshallBuffer(JLjava/nio/ByteBuffer;II)995 {"nativeUnmarshallBuffer", "(JLjava/nio/ByteBuffer;II)V", (void*)android_os_Parcel_unmarshall_buffer},
nativeCompareData(JJ)996 {"nativeCompareData", "(JJ)I", (void*)android_os_Parcel_compareData},
nativeCompareDataInRange(JIJII)997 {"nativeCompareDataInRange", "(JIJII)Z", (void*)android_os_Parcel_compareDataInRange},
nativeAppendFrom(JJII)998 {"nativeAppendFrom", "(JJII)V", (void*)android_os_Parcel_appendFrom},
999 // @CriticalNative
nativeHasFileDescriptors(J)1000 {"nativeHasFileDescriptors", "(J)Z", (void*)android_os_Parcel_hasFileDescriptors},
nativeHasFileDescriptorsInRange(JII)1001 {"nativeHasFileDescriptorsInRange", "(JII)Z", (void*)android_os_Parcel_hasFileDescriptorsInRange},
1002
nativeHasBinders(J)1003 {"nativeHasBinders", "(J)Z", (void*)android_os_Parcel_hasBinders},
nativeHasBindersInRange(JII)1004 {"nativeHasBindersInRange", "(JII)Z", (void*)android_os_Parcel_hasBindersInRange},
nativeWriteInterfaceToken(JLjava/lang/String;)1005 {"nativeWriteInterfaceToken", "(JLjava/lang/String;)V", (void*)android_os_Parcel_writeInterfaceToken},
nativeEnforceInterface(JLjava/lang/String;)1006 {"nativeEnforceInterface", "(JLjava/lang/String;)V", (void*)android_os_Parcel_enforceInterface},
1007
getGlobalAllocSize()1008 {"getGlobalAllocSize", "()J", (void*)android_os_Parcel_getGlobalAllocSize},
getGlobalAllocCount()1009 {"getGlobalAllocCount", "()J", (void*)android_os_Parcel_getGlobalAllocCount},
1010
1011 // @CriticalNative
nativeGetOpenAshmemSize(J)1012 {"nativeGetOpenAshmemSize", "(J)J", (void*)android_os_Parcel_getOpenAshmemSize},
1013
1014 // @CriticalNative
nativeReadCallingWorkSourceUid(J)1015 {"nativeReadCallingWorkSourceUid", "(J)I", (void*)android_os_Parcel_readCallingWorkSourceUid},
1016 // @CriticalNative
nativeReplaceCallingWorkSourceUid(JI)1017 {"nativeReplaceCallingWorkSourceUid", "(JI)Z", (void*)android_os_Parcel_replaceCallingWorkSourceUid},
1018 };
1019 // clang-format on
1020
1021 const char* const kParcelPathName = "android/os/Parcel";
1022
register_android_os_Parcel(JNIEnv * env)1023 int register_android_os_Parcel(JNIEnv* env)
1024 {
1025 jclass clazz = FindClassOrDie(env, kParcelPathName);
1026
1027 gParcelOffsets.clazz = MakeGlobalRefOrDie(env, clazz);
1028 gParcelOffsets.mNativePtr = GetFieldIDOrDie(env, clazz, "mNativePtr", "J");
1029 gParcelOffsets.obtain = GetStaticMethodIDOrDie(env, clazz, "obtain", "()Landroid/os/Parcel;");
1030 gParcelOffsets.recycle = GetMethodIDOrDie(env, clazz, "recycle", "()V");
1031
1032 return RegisterMethodsOrDie(env, kParcelPathName, gParcelMethods, NELEM(gParcelMethods));
1033 }
1034
1035 };
1036