1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ohos_rpc_message_parcel.h"
17 #include <ashmem.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "ipc_debug.h"
21 #include "ipc_file_descriptor.h"
22 #include "jkit_utils.h"
23 #include "jni_helper.h"
24 #include "log_tags.h"
25 #include "ohos_rpc_remote_object.h"
26 #include "ohos_utils_parcel.h"
27
28 using namespace OHOS;
29 using namespace OHOS::HiviewDFX;
30
31 namespace OHOS {
32 struct JMessageParcel {
33 jclass klazz;
34 jfieldID nativeObject;
35 jfieldID nativeObjectOwner;
36 } g_jMessageParcel;
37
38 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCJni" };
39
40 class AshmemSmartPointWrapper {
41 public:
AshmemSmartPointWrapper(const sptr<Ashmem> & ashmem)42 explicit AshmemSmartPointWrapper(const sptr<Ashmem> &ashmem) : ashmem_(ashmem)
43 {
44 if (ashmem == nullptr) {
45 ZLOGE(LABEL, "%s: ashmem is null", __func__);
46 }
47 }
48 ~AshmemSmartPointWrapper() = default;
49
GetAshmem() const50 const sptr<Ashmem> &GetAshmem() const
51 {
52 return ashmem_;
53 }
54
55 private:
56 // make sure this is immutable
57 sptr<Ashmem> const ashmem_;
58 };
59
60 /*
61 * Get Native Message Parcel instance of zindaneos/rpc/MessageParcel
62 */
JavaOhosRpcMessageParcelGetNative(JNIEnv * env,jobject object)63 MessageParcel *JavaOhosRpcMessageParcelGetNative(JNIEnv *env, jobject object)
64 {
65 ZLOGD(LABEL, "%s", __func__);
66 jlong nativeObject = env->GetLongField(object, g_jMessageParcel.nativeObject);
67 return reinterpret_cast<MessageParcel *>(nativeObject);
68 }
69
Java_ohos_rpc_Ashmem_getSptrAshmem(JNIEnv * env,jobject object,jlong id)70 sptr<Ashmem> Java_ohos_rpc_Ashmem_getSptrAshmem(JNIEnv *env, jobject object, jlong id)
71 {
72 if (id == 0) {
73 return nullptr;
74 }
75 AshmemSmartPointWrapper *wrapper = reinterpret_cast<AshmemSmartPointWrapper *>(id);
76 return wrapper->GetAshmem();
77 }
78
79 static const JNINativeMethod sMethods[] = {
80 /* Name, Signature, FunctionPointer */
81 { "nativeNewObject", "(J)J", (void *)Java_ohos_rpc_MessageParcel_nativeNewObject },
82 { "nativeFreeObject", "(J)V", (void *)Java_ohos_rpc_MessageParcel_nativeFreeObject },
83 { "nativeWriteRemoteObject", "(Lohos/rpc/IRemoteObject;)Z",
84 (void *)Java_ohos_rpc_MessageParcel_nativeWriteRemoteObject },
85 { "nativeReadRemoteObject", "()Lohos/rpc/IRemoteObject;",
86 (void *)Java_ohos_rpc_MessageParcel_nativeReadRemoteObject },
87 { "nativeWriteFileDescriptor", "(Ljava/io/FileDescriptor;)Z",
88 (void *)Java_ohos_rpc_MessageParcel_nativeWriteFileDescriptor },
89 { "nativeReadFileDescriptor", "()Ljava/io/FileDescriptor;",
90 (void *)Java_ohos_rpc_MessageParcel_nativeReadFileDescriptor },
91 { "nativeWriteInterfaceToken", "(Ljava/lang/String;I)Z",
92 (void *)Java_ohos_rpc_MessageParcel_nativeWriteInterfaceToken },
93 { "nativeReadInterfaceToken", "()Ljava/lang/String;",
94 (void *)Java_ohos_rpc_MessageParcel_nativeReadInterfaceToken },
95 { "nativeWriteRawData", "([BI)Z", (void *)Java_ohos_rpc_MessageParcel_nativeWriteRawData },
96 { "nativeReadRawData", "(I)[B", (void *)Java_ohos_rpc_MessageParcel_nativeReadRawData },
97 { "nativeGetRawDataCapacity", "()I", (void *)Java_ohos_rpc_MessageParcel_nativeGetRawDataCapacity },
98 { "nativeCloseFileDescriptor", "(Ljava/io/FileDescriptor;)V",
99 (void *)Java_ohos_rpc_MessageParcel_nativeCloseFileDescriptor },
100 { "nativeDupFileDescriptor", "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;",
101 (void *)Java_ohos_rpc_MessageParcel_nativeDupFileDescriptor },
102 { "nativeContainFileDescriptors", "()Z", (void *)Java_ohos_rpc_MessageParcel_nativeContainFileDescriptors },
103 { "nativeWriteAshmem", "(J)Z", (void *)Java_ohos_rpc_MessageParcel_nativeWriteAshmem },
104 { "nativeReadAshmem", "()J", (void *)Java_ohos_rpc_MessageParcel_nativeReadAshmem },
105 { "nativeReleaseAshmem", "(J)V", (void *)Java_ohos_rpc_MessageParcel_nativeReleaseAshmem },
106 };
107
108 /*
109 * register native methods fopr ohos.rpc.MessageParcel.
110 */
JavaOhosRpcMessageParcelRegisterNativeMethods(JNIEnv * env)111 int JavaOhosRpcMessageParcelRegisterNativeMethods(JNIEnv *env)
112 {
113 ZLOGD(LABEL, "%s", __func__);
114 jclass klazz = (jclass)env->NewGlobalRef(env->FindClass("ohos/rpc/MessageParcel"));
115 if (klazz == nullptr) {
116 ZLOGE(LABEL, "could not find class for MessageParcel");
117 return -1;
118 }
119 g_jMessageParcel.klazz = (jclass)env->NewGlobalRef(klazz);
120 g_jMessageParcel.nativeObject = env->GetFieldID(g_jMessageParcel.klazz, "mNativeObject", "J");
121 if (g_jMessageParcel.nativeObject == nullptr) {
122 ZLOGE(LABEL, "could not Get mNativeObject field for MessageParcel");
123 if (g_jMessageParcel.klazz != nullptr) {
124 env->DeleteGlobalRef(g_jMessageParcel.klazz);
125 }
126 env->DeleteGlobalRef(klazz);
127 return -1;
128 }
129 g_jMessageParcel.nativeObjectOwner = env->GetFieldID(g_jMessageParcel.klazz, "mOwnsNativeObject", "Z");
130 if (g_jMessageParcel.nativeObjectOwner == nullptr) {
131 ZLOGE(LABEL, "could not Get mOwnsNativeObject field for MessageParcel");
132 if (g_jMessageParcel.klazz != nullptr) {
133 env->DeleteGlobalRef(g_jMessageParcel.klazz);
134 }
135 env->DeleteGlobalRef(klazz);
136 return -1;
137 }
138 return JkitRegisterNativeMethods(env, "ohos/rpc/MessageParcel", sMethods, NUM_METHODS(sMethods));
139 }
140 } // namespace OHOS
141
142 /*
143 * Class: ohos.rpc.MessageParcel
144 * Method: nativeWriteRemoteObject
145 * Signature: (Lohos/rpc/IRemoteObject;)Z
146 */
Java_ohos_rpc_MessageParcel_nativeWriteRemoteObject(JNIEnv * env,jobject parcel,jobject object)147 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeWriteRemoteObject(JNIEnv *env, jobject parcel, jobject object)
148 {
149 ZLOGD(LABEL, "%s", __func__);
150 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, parcel);
151 if (nativeParcel == nullptr) {
152 ZLOGE(LABEL, "could not get native parcel for marshalling");
153 return JNI_FALSE;
154 }
155
156 sptr<IRemoteObject> target = Java_ohos_rpc_getNativeRemoteObject(env, object);
157 if (target != nullptr) {
158 if (nativeParcel->WriteRemoteObject(target)) {
159 return JNI_TRUE;
160 }
161 }
162 return JNI_FALSE;
163 }
164
165 /*
166 * Class: ohos.rpc.MessageParcel
167 * Method: nativeReadRemoteObject
168 * Signature: ()Lohos/rpc/IRemoteObject;
169 */
Java_ohos_rpc_MessageParcel_nativeReadRemoteObject(JNIEnv * env,jobject object)170 jobject JNICALL Java_ohos_rpc_MessageParcel_nativeReadRemoteObject(JNIEnv *env, jobject object)
171 {
172 ZLOGD(LABEL, "%s", __func__);
173 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
174 if (nativeParcel == nullptr) {
175 ZLOGE(LABEL, "could not get native parcel for unmarshalling");
176 return nullptr;
177 }
178
179 return Java_ohos_rpc_getJavaRemoteObject(env, nativeParcel->ReadRemoteObject());
180 }
181
182 /*
183 * Class: ohos.rpc.MessageParcel
184 * Method: nativeWriteFileDescriptor
185 * Signature: (Ljava/io/FileDescriptor;)Z
186 */
Java_ohos_rpc_MessageParcel_nativeWriteFileDescriptor(JNIEnv * env,jobject object,jobject descriptor)187 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeWriteFileDescriptor(JNIEnv *env, jobject object, jobject descriptor)
188 {
189 ZLOGD(LABEL, "%s", __func__);
190 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
191 if (nativeParcel == nullptr) {
192 ZLOGE(LABEL, "could not get native parcel for marshalling");
193 return JNI_FALSE;
194 }
195
196 int fd = JniHelperJavaIoGetFdFromFileDescriptor(env, descriptor);
197 return fd > 0 ? nativeParcel->WriteFileDescriptor(fd) : JNI_FALSE;
198 }
199
200 /*
201 * Class: ohos.rpc.MessageParcel
202 * Method: nativeReadFileDescriptor
203 * Signature: ()Ljava/io/FileDescriptor;
204 */
Java_ohos_rpc_MessageParcel_nativeReadFileDescriptor(JNIEnv * env,jobject object)205 jobject JNICALL Java_ohos_rpc_MessageParcel_nativeReadFileDescriptor(JNIEnv *env, jobject object)
206 {
207 ZLOGD(LABEL, "%s", __func__);
208 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
209 if (nativeParcel == nullptr) {
210 ZLOGE(LABEL, "unable to get native parcel");
211 return nullptr;
212 }
213
214 int fd = nativeParcel->ReadFileDescriptor();
215 if (fd != INVALID_FD) {
216 return JniHelperJavaIoCreateFileDescriptor(env, fd);
217 }
218
219 ZLOGE(LABEL, "Got invalid fd from parcel");
220 return nullptr;
221 }
222
223 /*
224 * Class: ohos.rpc.MessageParcel
225 * Method: nativeContainFileDescriptors
226 * Signature: ()Z;
227 */
Java_ohos_rpc_MessageParcel_nativeContainFileDescriptors(JNIEnv * env,jobject object)228 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeContainFileDescriptors(JNIEnv *env, jobject object)
229 {
230 ZLOGD(LABEL, "%s", __func__);
231 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
232 if (nativeParcel == nullptr) {
233 ZLOGE(LABEL, "unable to get native parcel");
234 return JNI_FALSE;
235 }
236
237 bool result = nativeParcel->ContainFileDescriptors();
238 return result ? JNI_TRUE : JNI_FALSE;
239 }
240
241 /*
242 * Class: ohos.rpc.MessageParcel
243 * Method: nativeWriteInterfaceToken
244 * Signature: (Ljava/lang/String;I)Z
245 */
Java_ohos_rpc_MessageParcel_nativeWriteInterfaceToken(JNIEnv * env,jobject object,jstring name,jint len)246 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeWriteInterfaceToken(JNIEnv *env, jobject object, jstring name,
247 jint len)
248 {
249 ZLOGD(LABEL, "%s", __func__);
250 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
251 if (nativeParcel == nullptr) {
252 ZLOGE(LABEL, "could not get native parcel for marshalling");
253 return JNI_FALSE;
254 }
255
256 bool result = false;
257 const jchar *u16chars = env->GetStringCritical(name, 0);
258 if (u16chars != nullptr) {
259 const auto *u16Str = reinterpret_cast<const char16_t *>(u16chars);
260 result = nativeParcel->WriteInterfaceToken(std::u16string(u16Str, len));
261 env->ReleaseStringCritical(name, u16chars);
262 }
263
264 return result ? JNI_TRUE : JNI_FALSE;
265 }
266
267 /*
268 * Class: ohos.rpc.MessageParcel
269 * Method: nativeReadInterfaceToken
270 * Signature: ()Ljava/lang/String;
271 */
Java_ohos_rpc_MessageParcel_nativeReadInterfaceToken(JNIEnv * env,jobject object)272 jobject JNICALL Java_ohos_rpc_MessageParcel_nativeReadInterfaceToken(JNIEnv *env, jobject object)
273 {
274 ZLOGD(LABEL, "%s", __func__);
275 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
276 if (nativeParcel == nullptr) {
277 ZLOGE(LABEL, "could not get native parcel for marshalling");
278 return JNI_FALSE;
279 }
280
281 std::u16string name = nativeParcel->ReadInterfaceToken();
282 return env->NewString(reinterpret_cast<const jchar *>(name.data()), name.size());
283 }
284
285 /*
286 * Class: ohos.rpc.MessageParcel
287 * Method: nativeWriteRawData
288 * Signature: ([BI)Z
289 */
Java_ohos_rpc_MessageParcel_nativeWriteRawData(JNIEnv * env,jobject object,jobject rawData,jint size)290 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeWriteRawData(JNIEnv *env, jobject object, jobject rawData, jint size)
291 {
292 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
293 if (nativeParcel == nullptr) {
294 ZLOGE(LABEL, "could not get native parcel for raw data");
295 return JNI_FALSE;
296 }
297
298 jbyte *ptr = static_cast<jbyte *>(env->GetPrimitiveArrayCritical(static_cast<jarray>(rawData), 0));
299 if (ptr == nullptr) {
300 return JNI_FALSE;
301 }
302 bool result = nativeParcel->WriteRawData(ptr, size);
303 env->ReleasePrimitiveArrayCritical(static_cast<jarray>(rawData), ptr, 0);
304 return result ? JNI_TRUE : JNI_FALSE;
305 }
306
307 /*
308 * Class: ohos.rpc.MessageParcel
309 * Method: nativeReadRawData
310 * Signature: (I)[B
311 */
Java_ohos_rpc_MessageParcel_nativeReadRawData(JNIEnv * env,jobject object,jint size)312 jbyteArray JNICALL Java_ohos_rpc_MessageParcel_nativeReadRawData(JNIEnv *env, jobject object, jint size)
313 {
314 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
315 if (nativeParcel == nullptr) {
316 ZLOGE(LABEL, "could not get native parcel for rawData");
317 return nullptr;
318 }
319
320 const void *rawData = nativeParcel->ReadRawData(size);
321 if (rawData == nullptr) {
322 ZLOGE(LABEL, "read raw data failed");
323 return nullptr;
324 }
325 jbyteArray bytes = env->NewByteArray(size);
326 if (bytes == nullptr) {
327 ZLOGE(LABEL, "NewByteArray failed");
328 return nullptr;
329 }
330 jbyte *ptr = static_cast<jbyte *>(env->GetPrimitiveArrayCritical(bytes, 0));
331 if (ptr != nullptr) {
332 int result = memcpy_s(ptr, size, rawData, size);
333 env->ReleasePrimitiveArrayCritical(bytes, ptr, 0);
334 if (result != 0) {
335 ZLOGE(LABEL, "copy raw data failed");
336 env->DeleteLocalRef(bytes);
337 return nullptr;
338 }
339 }
340 return bytes;
341 }
342
343
344 /*
345 * Class: ohos.rpc.MessageParcel
346 * Method: nativeGetRawDataCapacity
347 * Signature: ()I;
348 */
Java_ohos_rpc_MessageParcel_nativeGetRawDataCapacity(JNIEnv * env,jobject object)349 jint JNICALL Java_ohos_rpc_MessageParcel_nativeGetRawDataCapacity(JNIEnv *env, jobject object)
350 {
351 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
352 if (nativeParcel == nullptr) {
353 ZLOGE(LABEL, "could not get native parcel for rawData");
354 return 0;
355 }
356
357 return static_cast<jint>(nativeParcel->GetRawDataCapacity());
358 }
359
360 /*
361 * Set mOwnsNativeObject filed to ohos.rpc.MessageParcel
362 */
JavaOhosRpcMessageOptionSetNativeObjectOwner(JNIEnv * env,jobject object,jboolean value)363 void JavaOhosRpcMessageOptionSetNativeObjectOwner(JNIEnv *env, jobject object, jboolean value)
364 {
365 ZLOGD(LABEL, "%s", __func__);
366 env->SetBooleanField(object, g_jMessageParcel.nativeObjectOwner, value);
367 }
368
369 /*
370 * Class: ohos.rpc.MessageParcel
371 * Method: nativeNewObject
372 * Signature: (J)J;
373 */
Java_ohos_rpc_MessageParcel_nativeNewObject(JNIEnv * env,jobject object,jlong nativeObject)374 jlong JNICALL Java_ohos_rpc_MessageParcel_nativeNewObject(JNIEnv *env, jobject object, jlong nativeObject)
375 {
376 ZLOGD(LABEL, "%s", __func__);
377 MessageParcel *nativeMessageParcel = nullptr;
378 if (nativeObject != 0) {
379 nativeMessageParcel = reinterpret_cast<MessageParcel *>(nativeObject);
380 JavaOhosRpcMessageOptionSetNativeObjectOwner(env, object, JNI_FALSE);
381 } else {
382 JavaOhosRpcMessageOptionSetNativeObjectOwner(env, object, JNI_TRUE);
383 nativeMessageParcel = new MessageParcel();
384 }
385
386 if (nativeMessageParcel == nullptr) {
387 return 0L;
388 }
389 jclass superClass = env->GetSuperclass(g_jMessageParcel.klazz);
390 if (superClass == nullptr) {
391 ZLOGE(LABEL, "get supper class for MessageParcel failed");
392 delete nativeMessageParcel;
393 return 0L;
394 }
395
396 jmethodID superInit = env->GetMethodID(superClass, "<init>", "(J)V");
397 if (superInit == nullptr) {
398 ZLOGE(LABEL, "get supper method for MessageParcel failed");
399 delete nativeMessageParcel;
400 return 0L;
401 }
402 Parcel *nativeParcel = static_cast<Parcel *>(nativeMessageParcel);
403 ZLOGD(LABEL, "intSuperClass's native holder:%s", __func__);
404 env->CallNonvirtualVoidMethod(object, superClass, superInit, reinterpret_cast<jlong>(nativeParcel));
405
406 return reinterpret_cast<jlong>(nativeMessageParcel);
407 }
408
409 /*
410 * Class: ohos.rpc.MessageParcel
411 * Method: nativeFreeObject
412 * Signature: (J)V;
413 */
Java_ohos_rpc_MessageParcel_nativeFreeObject(JNIEnv * env,jobject object,jlong nativeObject)414 void JNICALL Java_ohos_rpc_MessageParcel_nativeFreeObject(JNIEnv *env, jobject object, jlong nativeObject)
415 {
416 ZLOGD(LABEL, "%s", __func__);
417 std::unique_ptr<MessageParcel> nativeParcel(reinterpret_cast<MessageParcel *>(nativeObject));
418 }
419
420 /*
421 * Class: ohos.rpc.MessageParcel
422 * Method: nativeCloseFileDescriptor
423 * Signature: (Ljava/io/FileDescriptor;)V
424 */
Java_ohos_rpc_MessageParcel_nativeCloseFileDescriptor(JNIEnv * env,jobject object,jobject descriptor)425 void JNICALL Java_ohos_rpc_MessageParcel_nativeCloseFileDescriptor(JNIEnv *env, jobject object, jobject descriptor)
426 {
427 ZLOGD(LABEL, "%s", __func__);
428 if (descriptor != nullptr) {
429 int fd = JniHelperJavaIoGetFdFromFileDescriptor(env, descriptor);
430 if (fd != INVALID_FD) {
431 close(fd);
432 JniHelperJavaIoSetFdToFileDescriptor(env, descriptor, INVALID_FD);
433 }
434 }
435 }
436
437 /*
438 * Class: ohos.rpc.MessageParcel
439 * Method: nativeDupFileDescriptor
440 * Signature: (Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;
441 */
Java_ohos_rpc_MessageParcel_nativeDupFileDescriptor(JNIEnv * env,jobject object,jobject descriptor)442 jobject JNICALL Java_ohos_rpc_MessageParcel_nativeDupFileDescriptor(JNIEnv *env, jobject object, jobject descriptor)
443 {
444 ZLOGD(LABEL, "%s", __func__);
445 if (descriptor != nullptr) {
446 int fd = JniHelperJavaIoGetFdFromFileDescriptor(env, descriptor);
447 int dupFd = INVALID_FD;
448 if (fd != INVALID_FD) {
449 dupFd = dup(fd);
450 }
451 if (dupFd != INVALID_FD) {
452 return JniHelperJavaIoCreateFileDescriptor(env, dupFd);
453 }
454 }
455 return nullptr;
456 }
457
458 /*
459 * Class: ohos.rpc.MessageParcel
460 * Method: nativeWriteAshmem
461 * Signature: (J)Z
462 */
Java_ohos_rpc_MessageParcel_nativeWriteAshmem(JNIEnv * env,jobject object,jlong id)463 jboolean JNICALL Java_ohos_rpc_MessageParcel_nativeWriteAshmem(JNIEnv *env, jobject object, jlong id)
464 {
465 ZLOGD(LABEL, "%s", __func__);
466 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
467 if (nativeParcel == nullptr) {
468 ZLOGE(LABEL, "could not get native parcel for raw data");
469 return JNI_FALSE;
470 }
471
472 sptr<Ashmem> ashmem = Java_ohos_rpc_Ashmem_getSptrAshmem(env, object, id);
473 if (ashmem == nullptr) {
474 ZLOGE(LABEL, "%s: ashmem=null", __func__);
475 return JNI_FALSE;
476 }
477
478 bool result = nativeParcel->WriteAshmem(ashmem);
479 return result ? JNI_TRUE : JNI_FALSE;
480 }
481
482 /*
483 * Class: ohos.rpc.MessageParcel
484 * Method: nativeReadAshmem
485 * Signature: (V)J
486 */
Java_ohos_rpc_MessageParcel_nativeReadAshmem(JNIEnv * env,jobject object)487 jlong JNICALL Java_ohos_rpc_MessageParcel_nativeReadAshmem(JNIEnv *env, jobject object)
488 {
489 ZLOGD(LABEL, "%s", __func__);
490 MessageParcel *nativeParcel = JavaOhosRpcMessageParcelGetNative(env, object);
491 if (nativeParcel == nullptr) {
492 ZLOGE(LABEL, "could not get native parcel for rawData");
493 return 0;
494 }
495
496 sptr<Ashmem> nativeAshmem = nativeParcel->ReadAshmem();
497 if (nativeAshmem == nullptr) {
498 ZLOGE(LABEL, "read raw data failed");
499 return 0;
500 }
501
502 // memory is released in Java_ohos_rpc_MessageParcel_nativeReleaseAshmem
503 AshmemSmartPointWrapper *wrapper = new AshmemSmartPointWrapper(nativeAshmem);
504 return reinterpret_cast<jlong>(wrapper);
505 }
506
507 /*
508 * Class: ohos.rpc.MessageParcel
509 * Method: nativeReleaseAshmem
510 * Signature: (J)V
511 */
Java_ohos_rpc_MessageParcel_nativeReleaseAshmem(JNIEnv * env,jobject object,jlong id)512 void JNICALL Java_ohos_rpc_MessageParcel_nativeReleaseAshmem(JNIEnv *env, jobject object, jlong id)
513 {
514 ZLOGD(LABEL, "%s", __func__);
515 if (id == 0) {
516 return;
517 }
518 std::unique_ptr<AshmemSmartPointWrapper> nativeParcel(reinterpret_cast<AshmemSmartPointWrapper *>(id));
519 }
520