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 "requestbuffer_fuzzer.h"
17 
18 #include <cstring>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21 
22 #include "nweb.h"
23 #include "nweb_adapter_helper.h"
24 
25 #define private public
26 #include "nweb_surface_adapter.h"
27 
28 using namespace OHOS::NWeb;
29 using namespace OHOS::Rosen;
30 
31 namespace OHOS {
32 namespace {
33 sptr<Surface> g_surface = nullptr;
34 }
35 
RequestBufferFuzzTest(const uint8_t * data,size_t size)36 bool RequestBufferFuzzTest(const uint8_t* data, size_t size)
37 {
38     if ((data == nullptr) || (size < sizeof(uint32_t))) {
39         return false;
40     }
41 
42     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
43     uint32_t width;
44     uint32_t height;
45     if (memcpy_s(&width, sizeof(uint32_t), data, sizeof(uint32_t)) != 0) {
46         return false;
47     }
48     if (memcpy_s(&height, sizeof(uint32_t), data, sizeof(uint32_t)) != 0) {
49         return false;
50     }
51     if (!g_surface) {
52         RSSurfaceNodeConfig config;
53         config.SurfaceNodeName = "webTestSurfaceName";
54         auto surfaceNode = RSSurfaceNode::Create(config, false);
55         if (surfaceNode == nullptr) {
56             return false;
57         }
58         g_surface = surfaceNode->GetSurface();
59         if (g_surface == nullptr) {
60             return false;
61         }
62     }
63     surfaceAdapter.RequestBuffer(g_surface, width, height);
64     return true;
65 }
66 } // namespace OHOS
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71     /* Run your code on data */
72     OHOS::RequestBufferFuzzTest(data, size);
73     return 0;
74 }
75