• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "resschedadapter_fuzz.h"
17 
18 #include <cstdint>
19 #include <cstring>
20 #include <mutex>
21 #include <securec.h>
22 #include <unordered_map>
23 #include <vector>
24 #include <fuzzer/FuzzedDataProvider.h>
25 
26 #include "res_sched_client_adapter.h"
27 
28 using namespace OHOS::NWeb;
29 
30 namespace OHOS {
31 namespace NWeb {
32 constexpr int MAX_SET_NUMBER = 1000;
33 
ResSchedAdapterFuzzTest(const uint8_t * data,size_t size)34 bool ResSchedAdapterFuzzTest(const uint8_t* data, size_t size)
35 {
36     if ((data == nullptr) || (size == 0)) {
37         return false;
38     }
39 
40     FuzzedDataProvider dataProvider(data, size);
41 
42     pid_t pid = 1;
43     pid_t tid = 1;
44     int32_t nwebId = dataProvider.ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
45     uint32_t windowId = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
46 
47     std::vector<ResSchedStatusAdapter> statuses = { ResSchedStatusAdapter::WEB_ACTIVE,
48         ResSchedStatusAdapter::WEB_INACTIVE, ResSchedStatusAdapter::THREAD_CREATED,
49         ResSchedStatusAdapter::THREAD_DESTROYED, ResSchedStatusAdapter::VIDEO_PLAYING_START,
50         ResSchedStatusAdapter::VIDEO_PLAYING_STOP, ResSchedStatusAdapter::SCREEN_CAPTURE_START,
51         ResSchedStatusAdapter::SCREEN_CAPTURE_STOP, ResSchedStatusAdapter::AUDIO_STATUS_START,
52         ResSchedStatusAdapter::AUDIO_STATUS_STOP, ResSchedStatusAdapter::WEB_SCENE_ENTER,
53         ResSchedStatusAdapter::WEB_SCENE_EXIT };
54 
55     std::vector<ResSchedRoleAdapter> roles = { ResSchedRoleAdapter::USER_INTERACT, ResSchedRoleAdapter::NORMAL_DISPLAY,
56         ResSchedRoleAdapter::IMPORTANT_DISPLAY, ResSchedRoleAdapter::NORMAL_AUDIO, ResSchedRoleAdapter::IMPORTANT_AUDIO,
57         ResSchedRoleAdapter::IMAGE_DECODE };
58 
59     ResSchedClientAdapter adapter;
60 
61     for (auto status : statuses) {
62         adapter.ReportVideoPlaying(status, pid);
63         adapter.ReportScreenCapture(status, pid);
64         adapter.ReportRenderProcessStatus(status, pid);
65         adapter.ReportNWebInit(status, nwebId);
66         adapter.ReportWindowId(windowId, nwebId);
67 
68         for (auto role : roles) {
69             adapter.ReportKeyThread(status, pid, tid, role);
70         }
71 
72         adapter.ReportAudioData(status, pid, tid);
73         adapter.ReportProcessInUse(pid);
74         adapter.ReportSiteIsolationMode(true);
75         adapter.ReportWindowStatus(status, pid, windowId, nwebId);
76         adapter.ReportScene(status, ResSchedSceneAdapter::CLICK, nwebId);
77         adapter.ReportScene(status, ResSchedSceneAdapter::LOAD_URL, nwebId);
78     }
79 
80     adapter.ReportProcessInUse(0);
81     adapter.ReportWindowId(0, nwebId);
82     adapter.ReportKeyThread(statuses[0], pid, tid, static_cast<ResSchedRoleAdapter>(-1));
83     adapter.ReportAudioData(statuses[0], pid, 0);
84     adapter.ReportSiteIsolationMode(true);
85 
86     adapter.ReportWindowId(0, nwebId);
87     adapter.ReportKeyThread(statuses[0], -1, tid, ResSchedRoleAdapter::USER_INTERACT);
88     adapter.ReportAudioData(statuses[0], pid, -1);
89     adapter.ReportWindowStatus(statuses[0], pid, windowId, -1);
90     adapter.ReportNWebInit(ResSchedStatusAdapter::WEB_SCENE_EXIT, -1);
91 
92     return true;
93 }
94 } // namespace NWeb
95 } // namespace OHOS
96 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99     OHOS::NWeb::ResSchedAdapterFuzzTest(data, size);
100     return 0;
101 }
102