1/* 2 * Copyright (c) 2023 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 "ipc_issues.h" 17 18#include <memory> 19 20#include "message_parcel.h" 21 22#include "parcel_sample.h" 23#include "remote_object_interface.h" 24#include "remote_object_interface_stub.h" 25#include "remote_object_interface_proxy.h" 26 27namespace OHOS { 28namespace HiviewDFX { 29int IPCIssues::SptrMismatch() 30{ 31 printf("IPCIssues::SptrMismatch begin\n"); 32 { 33 MessageParcel data; 34 auto stub = new RemoteObjectInterfaceStub(); 35 sptr<IRemoteObject> sendObj = stub->AsObject(); 36 printf("sendObj:%p count:%d.\n", sendObj.GetRefPtr(), sendObj->GetSptrRefCount()); 37 38 sptr<IRemoteObject> sendObj2 = stub; 39 printf("sendObj2:%p count:%d.\n", sendObj2.GetRefPtr(), sendObj2->GetSptrRefCount()); 40 delete stub; 41 sendObj = nullptr; 42 sendObj2 = nullptr; 43 } 44 printf("IPCIssues::SptrMismatch end\n"); 45 return 0; 46} 47 48int IPCIssues::SptrAndSharedPtrMixUsage() 49{ 50 printf("IPCIssues::SptrAndSharedPtrMixUsage begin.\n"); 51 { 52 auto sendObj = std::make_shared<RemoteObjectInterfaceStub>(); 53 printf("sendObj:%p.\n", sendObj.get()); 54 delete sendObj.get(); 55 sendObj = nullptr; 56 } 57 printf("IPCIssues::SptrAndSharedPtrMixUsage end.\n"); 58 return 0; 59} 60 61int IPCIssues::ParcelReadWriteMismatch() 62{ 63 MessageParcel data; 64 if (!data.WriteInterfaceToken(RemoteObjectInterfaceProxy::GetDescriptor())) { 65 printf("Failed to write ipc interface.\n"); 66 return 0; 67 } 68 69 ParcelSample sample; 70 if (!sample.Marshalling(data)) { 71 printf("Failed to write sample content.\n"); 72 return 0; 73 } 74 75 auto stub = new RemoteObjectInterfaceStub(); 76 data.WriteRemoteObject(stub->AsObject()); 77 data.WriteInt32(1); 78 sptr<IRemoteObject> recvObj = data.ReadRemoteObject(); 79 if (recvObj == nullptr) { 80 printf("Failed to read remote object.\n"); 81 } 82 83 sptr<ParcelSample> samplePtr = ParcelSample::Unmarshalling(data); 84 delete samplePtr.GetRefPtr(); 85 samplePtr = nullptr; 86 return 0; 87} 88} // namespace HiviewDFX 89} // namespace OHOS 90