1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include "ipc_issues.h"
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <memory>
19800b99b8Sopenharmony_ci
20800b99b8Sopenharmony_ci#include "message_parcel.h"
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ci#include "parcel_sample.h"
23800b99b8Sopenharmony_ci#include "remote_object_interface.h"
24800b99b8Sopenharmony_ci#include "remote_object_interface_stub.h"
25800b99b8Sopenharmony_ci#include "remote_object_interface_proxy.h"
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_cinamespace OHOS {
28800b99b8Sopenharmony_cinamespace HiviewDFX {
29800b99b8Sopenharmony_ciint IPCIssues::SptrMismatch()
30800b99b8Sopenharmony_ci{
31800b99b8Sopenharmony_ci    printf("IPCIssues::SptrMismatch begin\n");
32800b99b8Sopenharmony_ci    {
33800b99b8Sopenharmony_ci        MessageParcel data;
34800b99b8Sopenharmony_ci        auto stub = new RemoteObjectInterfaceStub();
35800b99b8Sopenharmony_ci        sptr<IRemoteObject> sendObj = stub->AsObject();
36800b99b8Sopenharmony_ci        printf("sendObj:%p count:%d.\n", sendObj.GetRefPtr(), sendObj->GetSptrRefCount());
37800b99b8Sopenharmony_ci
38800b99b8Sopenharmony_ci        sptr<IRemoteObject> sendObj2 = stub;
39800b99b8Sopenharmony_ci        printf("sendObj2:%p count:%d.\n", sendObj2.GetRefPtr(), sendObj2->GetSptrRefCount());
40800b99b8Sopenharmony_ci        delete stub;
41800b99b8Sopenharmony_ci        sendObj = nullptr;
42800b99b8Sopenharmony_ci        sendObj2 = nullptr;
43800b99b8Sopenharmony_ci    }
44800b99b8Sopenharmony_ci    printf("IPCIssues::SptrMismatch end\n");
45800b99b8Sopenharmony_ci    return 0;
46800b99b8Sopenharmony_ci}
47800b99b8Sopenharmony_ci
48800b99b8Sopenharmony_ciint IPCIssues::SptrAndSharedPtrMixUsage()
49800b99b8Sopenharmony_ci{
50800b99b8Sopenharmony_ci    printf("IPCIssues::SptrAndSharedPtrMixUsage begin.\n");
51800b99b8Sopenharmony_ci    {
52800b99b8Sopenharmony_ci        auto sendObj = std::make_shared<RemoteObjectInterfaceStub>();
53800b99b8Sopenharmony_ci        printf("sendObj:%p.\n", sendObj.get());
54800b99b8Sopenharmony_ci        delete sendObj.get();
55800b99b8Sopenharmony_ci        sendObj = nullptr;
56800b99b8Sopenharmony_ci    }
57800b99b8Sopenharmony_ci    printf("IPCIssues::SptrAndSharedPtrMixUsage end.\n");
58800b99b8Sopenharmony_ci    return 0;
59800b99b8Sopenharmony_ci}
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ciint IPCIssues::ParcelReadWriteMismatch()
62800b99b8Sopenharmony_ci{
63800b99b8Sopenharmony_ci    MessageParcel data;
64800b99b8Sopenharmony_ci    if (!data.WriteInterfaceToken(RemoteObjectInterfaceProxy::GetDescriptor())) {
65800b99b8Sopenharmony_ci        printf("Failed to write ipc interface.\n");
66800b99b8Sopenharmony_ci        return 0;
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ci    ParcelSample sample;
70800b99b8Sopenharmony_ci    if (!sample.Marshalling(data)) {
71800b99b8Sopenharmony_ci        printf("Failed to write sample content.\n");
72800b99b8Sopenharmony_ci        return 0;
73800b99b8Sopenharmony_ci    }
74800b99b8Sopenharmony_ci
75800b99b8Sopenharmony_ci    auto stub = new RemoteObjectInterfaceStub();
76800b99b8Sopenharmony_ci    data.WriteRemoteObject(stub->AsObject());
77800b99b8Sopenharmony_ci    data.WriteInt32(1);
78800b99b8Sopenharmony_ci    sptr<IRemoteObject> recvObj = data.ReadRemoteObject();
79800b99b8Sopenharmony_ci    if (recvObj == nullptr) {
80800b99b8Sopenharmony_ci        printf("Failed to read remote object.\n");
81800b99b8Sopenharmony_ci    }
82800b99b8Sopenharmony_ci
83800b99b8Sopenharmony_ci    sptr<ParcelSample> samplePtr = ParcelSample::Unmarshalling(data);
84800b99b8Sopenharmony_ci    delete samplePtr.GetRefPtr();
85800b99b8Sopenharmony_ci    samplePtr = nullptr;
86800b99b8Sopenharmony_ci    return 0;
87800b99b8Sopenharmony_ci}
88800b99b8Sopenharmony_ci} // namespace HiviewDFX
89800b99b8Sopenharmony_ci} // namespace OHOS
90