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 "copyframe_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 
30 namespace OHOS {
31 
CopyFrameFuzzTest(const uint8_t * data,size_t size)32 bool CopyFrameFuzzTest(const uint8_t* data, size_t size)
33 {
34     constexpr int BITS_PER_PIXEL = 4;
35     if ((data == nullptr) || (size < sizeof(uint32_t))) {
36         return false;
37     }
38     uint32_t width;
39     uint32_t height;
40     if (memcpy_s(&width, sizeof(uint32_t), data, sizeof(uint32_t)) != 0) {
41         return false;
42     }
43     if (memcpy_s(&height, sizeof(uint32_t), data, sizeof(uint32_t)) != 0) {
44         return false;
45     }
46     sptr<SurfaceBuffer> surfaceBuffer = nullptr;
47     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
48     if (width == 0 || height == 0) {
49         return false;
50     }
51     char* src = new char[BITS_PER_PIXEL] { 0 };
52     if (src == nullptr) {
53         return false;
54     }
55     surfaceAdapter.CopyFrame(surfaceBuffer, src, width, height);
56     delete[] src;
57     return true;
58 }
59 } // namespace OHOS
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     OHOS::CopyFrameFuzzTest(data, size);
66     return 0;
67 }
68