1 /*
2  * Copyright (c) 2024 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 <cstddef>
17 #include <cstdint>
18 #include <parcel.h>
19 #include <securec.h>
20 
21 #include <iremote_stub.h>
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "marshalling_helper.h"
25 #include "screen_session_manager_client_interface.h"
26 #include "screen_session_manager_client_stub.h"
27 #include "screensessionmanagerclientstub_fuzzer.h"
28 #include "screen_session_manager_client.h"
29 
30 using namespace OHOS::Rosen;
31 
32 namespace OHOS {
33 namespace {
34 constexpr size_t DATA_MIN_SIZE = 2;
35 }
36 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)37 size_t GetObject(T &object, const uint8_t *data, size_t size)
38 {
39     size_t objectSize = sizeof(object);
40     if (objectSize > size) {
41         return 0;
42     }
43     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
44 }
ScreenSessionMgrClientStubTest1(MessageParcel & parcel,MessageParcel & reply,MessageOption & option)45 void ScreenSessionMgrClientStubTest1(MessageParcel &parcel, MessageParcel &reply, MessageOption &option)
46 {
47     parcel.RewindRead(0);
48     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
49         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
50         ScreenSessionManagerClientMessage::TRANS_ID_ON_SCREEN_CONNECTION_CHANGED), parcel, reply, option);
51     parcel.RewindRead(0);
52     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
53         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
54         ScreenSessionManagerClientMessage::TRANS_ID_ON_PROPERTY_CHANGED), parcel, reply, option);
55     parcel.RewindRead(0);
56     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
57         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
58         ScreenSessionManagerClientMessage::TRANS_ID_ON_POWER_STATUS_CHANGED), parcel, reply, option);
59     parcel.RewindRead(0);
60     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
61         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
62         ScreenSessionManagerClientMessage::TRANS_ID_ON_SENSOR_ROTATION_CHANGED), parcel, reply, option);
63     parcel.RewindRead(0);
64     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
65         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
66         ScreenSessionManagerClientMessage::TRANS_ID_ON_SCREEN_ORIENTATION_CHANGED), parcel, reply, option);
67     parcel.RewindRead(0);
68     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
69         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
70         ScreenSessionManagerClientMessage::TRANS_ID_ON_SCREEN_ROTATION_LOCKED_CHANGED), parcel, reply, option);
71     parcel.RewindRead(0);
72     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
73         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
74         ScreenSessionManagerClientMessage::TRANS_ID_ON_DISPLAY_STATE_CHANGED), parcel, reply, option);
75     return;
76 }
ScreenSessionMgrClientStubTest2(MessageParcel & parcel,MessageParcel & reply,MessageOption & option)77 void ScreenSessionMgrClientStubTest2(MessageParcel &parcel, MessageParcel &reply, MessageOption &option)
78 {
79     parcel.RewindRead(0);
80     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
81         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
82         ScreenSessionManagerClientMessage::TRANS_ID_ON_SCREEN_SHOT), parcel, reply, option);
83     parcel.RewindRead(0);
84     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
85         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
86         ScreenSessionManagerClientMessage::TRANS_ID_ON_IMMERSIVE_STATE_CHANGED), parcel, reply, option);
87     parcel.RewindRead(0);
88     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
89         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
90         ScreenSessionManagerClientMessage::TRANS_ID_SET_DISPLAY_NODE_SCREEN_ID), parcel, reply, option);
91     parcel.RewindRead(0);
92     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
93         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
94         ScreenSessionManagerClientMessage::TRANS_ID_GET_SURFACENODEID_FROM_MISSIONID), parcel, reply, option);
95     parcel.RewindRead(0);
96     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
97         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
98         ScreenSessionManagerClientMessage::TRANS_ID_SET_FOLD_DISPLAY_MODE), parcel, reply, option);
99     parcel.RewindRead(0);
100     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
101         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
102         ScreenSessionManagerClientMessage::TRANS_ID_ON_SWITCH_USER_CMD), parcel, reply, option);
103     parcel.RewindRead(0);
104     ScreenSessionManagerClient::GetInstance().OnRemoteRequest(
105         static_cast<uint32_t>(Rosen::IScreenSessionManagerClient::
106         ScreenSessionManagerClientMessage::TRANS_ID_SET_VIRTUAL_PIXEL_RATIO_SYSTEM), parcel, reply, option);
107     return;
108 }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)109 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
110 {
111     if (data == nullptr || size < DATA_MIN_SIZE) {
112         return false;
113     }
114 
115     MessageParcel parcel;
116     MessageParcel reply;
117     MessageOption option;
118 
119     parcel.WriteInterfaceToken(ScreenSessionManagerClientStub::GetDescriptor());
120     parcel.WriteBuffer(data, size);
121 
122     ScreenSessionMgrClientStubTest1(parcel, reply, option);
123     ScreenSessionMgrClientStubTest2(parcel, reply, option);
124     return true;
125 }
126 } // namespace.OHOS
127 
128 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)129 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
130 {
131     /* Run your code on data */
132     OHOS::DoSomethingInterestingWithMyAPI(data, size);
133     return 0;
134 }