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 <cstring>
17 #include <securec.h>
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #define private public
21 #include "aafwk_app_mgr_client_adapter_impl.h"
22 #include "aafwk_browser_client_adapter_impl.h"
23 #include "aafwk_browser_host_impl.h"
24 #include "aafwk_render_scheduler_impl.h"
25 #undef private
26
27 #include "aafwk_browser_client_adapter.h"
28 #include "aafwk_render_scheduler_host_adapter.h"
29 #include "app_mgr_client.h"
30 #include "ibrowser.h"
31 #include "iconsumer_surface.h"
32 #include "iremote_proxy.h"
33 #include "irender_scheduler.h"
34 #include "ohos_adapter_helper.h"
35
36 using namespace OHOS::NWeb;
37
38 namespace OHOS {
39 constexpr int MAX_SET_NUMBER = 1000;
40
41 class MockBrowserClient : public BrowserClient {
42 explicit MockBrowserClient(const sptr<IRemoteObject>& impl);
43
44 sptr<IRemoteObject> QueryRenderSurface(int32_t surface_id);
45
46 void ReportThread(int32_t status, int32_t process_id, int32_t thread_id, int32_t role);
47
48 void PassSurface(sptr<Surface> surface, int64_t surface_id);
49
50 void DestroyRenderSurface(int32_t surface_id);
51 };
52
QueryRenderSurface(int32_t surface_id)53 sptr<IRemoteObject> MockBrowserClient::QueryRenderSurface(int32_t surface_id)
54 {
55 (void)surface_id;
56 return nullptr;
57 }
58
ReportThread(int32_t status,int32_t process_id,int32_t thread_id,int32_t role)59 void MockBrowserClient::ReportThread(int32_t status, int32_t process_id, int32_t thread_id, int32_t role)
60 {
61 (void)status;
62 (void)process_id;
63 (void)thread_id;
64 (void)role;
65 }
66
PassSurface(sptr<Surface> surface,int64_t surface_id)67 void MockBrowserClient::PassSurface(sptr<Surface> surface, int64_t surface_id)
68 {
69 (void)surface;
70 (void)surface_id;
71 }
72
DestroyRenderSurface(int32_t surface_id)73 void MockBrowserClient::DestroyRenderSurface(int32_t surface_id)
74 {
75 (void)surface_id;
76 }
77
AafwkBrowserClientAdapterFuzzTest(const uint8_t * data,size_t size)78 bool AafwkBrowserClientAdapterFuzzTest(const uint8_t* data, size_t size)
79 {
80 sptr<IRemoteObject> impl;
81 auto client = new BrowserClient(impl);
82 int32_t surface_id = 0;
83 client->QueryRenderSurface(surface_id);
84 int32_t status = 0;
85 int32_t process_id = 0;
86 int32_t thread_id = 0;
87 FuzzedDataProvider dataProvider(data, size);
88 int32_t role = dataProvider.ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
89
90 sptr<Surface> surface;
91 client->ReportThread(status, process_id, thread_id, role);
92 client->PassSurface(surface, surface_id);
93 client->DestroyRenderSurface(surface_id);
94
95 std::shared_ptr<AafwkBrowserClientAdapterImpl> clientAdapter = std::make_shared<AafwkBrowserClientAdapterImpl>();
96 clientAdapter->QueryRenderSurface(surface_id);
97 ResSchedStatusAdapter newstatus = ResSchedStatusAdapter::THREAD_CREATED;
98 ResSchedRoleAdapter newrole = ResSchedRoleAdapter::USER_INTERACT;
99 clientAdapter->ReportThread(newstatus, process_id, thread_id, newrole);
100 clientAdapter->PassSurface(surface_id);
101 clientAdapter->DestroyRenderSurface(surface_id);
102 return true;
103 }
104 } // namespace OHOS
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::AafwkBrowserClientAdapterFuzzTest(data, size);
111 return 0;
112 }
113