1 /*
2 * Copyright (c) 2022 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 "windowpair_fuzzer.h"
17
18 #include <ability_manager_client.h>
19 #include "display_manager.h"
20 #include "common_event_manager.h"
21 #include "window_manager_hilog.h"
22 #include "window_helper.h"
23 #include "surface_draw.h"
24 #include "window_pair.h"
25
26 using namespace OHOS::Rosen;
27
28 namespace OHOS {
29 namespace {
30 constexpr size_t DATA_MIN_SIZE = 2;
31 constexpr size_t LEN = 10;
32 }
33
34 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)35 size_t GetObject(T &object, const uint8_t *data, size_t size)
36 {
37 size_t objectSize = sizeof(object);
38 if (objectSize > size) {
39 return 0;
40 }
41 return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
42 }
43
InitWindowNode1(sptr<OHOS::Rosen::WindowNode> & windowNode,const uint8_t * data,size_t size)44 size_t InitWindowNode1(sptr<OHOS::Rosen::WindowNode> &windowNode, const uint8_t *data, size_t size)
45 {
46 size_t startPos = 0;
47 sptr<OHOS::Rosen::WindowProperty> property = new WindowProperty();
48 Parcel parcel;
49 if (parcel.WriteBuffer(data + startPos, size - startPos)) {
50 property = OHOS::Rosen::WindowProperty::Unmarshalling(parcel);
51 }
52 windowNode = new OHOS::Rosen::WindowNode(property);
53 if (windowNode == nullptr) {
54 return startPos;
55 }
56 return startPos;
57 }
58
DoSomethingInterestingWithMyAPI2(const uint8_t * data,size_t size)59 bool DoSomethingInterestingWithMyAPI2(const uint8_t* data, size_t size)
60 {
61 if (data == nullptr || size < DATA_MIN_SIZE) {
62 return false;
63 }
64 size_t startPos = 0;
65 DisplayId displayId = OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplayId();
66 sptr<OHOS::Rosen::WindowPair> windowPair = new OHOS::Rosen::WindowPair(displayId);
67 sptr<OHOS::Rosen::WindowNode> windowNode;
68 startPos += InitWindowNode1(windowNode, data + startPos, size -startPos);
69 std::vector<int32_t> vec;
70 int32_t num;
71 for(uint32_t i = 0; i < LEN; ++i) {
72 startPos += GetObject(num, data + startPos, size - startPos);
73 vec.emplace_back(num);
74 }
75 windowPair->IsDockSliceInExitSplitModeArea(vec);
76 windowPair->ExitSplitMode();
77 windowPair->Clear();
78 windowPair->IsSplitRelated(windowNode);
79 windowPair->GetOrderedPair(windowNode);
80 windowPair->GetPairedWindows();
81 windowPair->UpdateIfSplitRelated(windowNode);
82 windowPair->UpdateWindowPairStatus();
83 windowPair->SwitchPosition();
84 windowPair->HandlePairedNodesChange();
85 windowPair->UpdateWindowPairStatus();
86 windowPair->Insert(windowNode);
87 windowPair->DumpPairInfo();
88 windowPair->HandleRemoveWindow(windowNode);
89 OHOS::Rosen::Rect rect;
90 GetObject(rect, data + startPos, size - startPos);
91 windowPair->RotateDividerWindow(rect);
92 windowPair->SetDividerRect(rect);
93 windowPair->ClearPairSnapshot();
94 return true;
95 }
96
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)97 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
98 {
99 if (data == nullptr || size < DATA_MIN_SIZE) {
100 return false;
101 }
102 OHOS::Rosen::DisplayId displayId = OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplayId();
103 sptr<OHOS::Rosen::WindowPair> windowPair = new OHOS::Rosen::WindowPair(displayId);
104 size_t startPos = 0;
105 OHOS::Rosen::SplitEventMsgType msg;
106 startPos += GetObject<OHOS::Rosen::SplitEventMsgType>(msg, data + startPos, size - startPos);
107 int32_t missionId;
108 startPos += GetObject<int32_t>(missionId, data + startPos, size - startPos);
109 sptr<WindowNode> windowNode;
110 startPos += InitWindowNode1(windowNode, data + startPos, size -startPos);
111 bool isDestroy;
112 startPos += GetObject<bool>(isDestroy, data + startPos, size -startPos);
113 windowPair->NotifyCreateOrDestroyDivider(windowNode, isDestroy);
114 windowPair->Find(windowNode);
115 windowPair->IsPaired();
116 float ratio;
117 GetObject<float>(ratio, data + startPos, size - startPos);
118 windowPair->SetSplitRatio(ratio);
119 windowPair->GetSplitRatio();
120 windowPair->GetPairStatus();
121 windowPair->GetDividerWindow();
122 windowPair->IsForbidDockSliceMove();
123 return true;
124 }
125 } // namespace OHOS
126
127 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)128 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
129 {
130 /* Run your code on data */
131 OHOS::DoSomethingInterestingWithMyAPI(data, size);
132 OHOS::DoSomethingInterestingWithMyAPI2(data, size);
133 return 0;
134 }
135
136