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 <fuzzer/FuzzedDataProvider.h>
21 #include <ui/rs_surface_node.h>
22
23 #include "nweb.h"
24 #include "nweb_adapter_helper.h"
25
26 #define private public
27 #include "nweb_surface_adapter.h"
28
29 using namespace OHOS::NWeb;
30
31 namespace OHOS {
32 const uint32_t DEFAULT_WIDTH = 2560;
33 const uint32_t DEFAULT_HEIGHT = 1396;
34 constexpr int MAX_SET_NUMBER = 1000;
35 constexpr int BITS_PER_PIXEL = 4;
36
CopyFrameFuzzTest(const uint8_t * data,size_t size)37 bool CopyFrameFuzzTest(const uint8_t* data, size_t size)
38 {
39 if ((data == nullptr) || (size == 0)) {
40 return false;
41 }
42 FuzzedDataProvider dataProvider(data, size);
43 uint32_t width = dataProvider.ConsumeIntegralInRange<uint32_t>(1, MAX_SET_NUMBER);
44 uint32_t height = dataProvider.ConsumeIntegralInRange<uint32_t>(1, MAX_SET_NUMBER);
45 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
46 auto surfaceAdapter = NWebSurfaceAdapter::Instance();
47 char* src = new char[DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL] { 0 };
48 if (src == nullptr) {
49 return false;
50 }
51 surfaceAdapter.CopyFrame(surfaceBuffer, src, width, height);
52 int32_t releaseFence = -1;
53 BufferRequestConfig requestConfig = {
54 .width = DEFAULT_WIDTH,
55 .height = DEFAULT_HEIGHT,
56 .strideAlignment = sizeof(void *),
57 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
58 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
59 .timeout = 0,
60 };
61 sptr<Surface> fuzzSurface = nullptr;
62 Rosen::RSSurfaceNodeConfig config;
63 config.SurfaceNodeName = "webTestSurfaceName";
64 auto surfaceNode = Rosen::RSSurfaceNode::Create(config, false);
65 if (surfaceNode == nullptr) {
66 return false;
67 }
68 fuzzSurface = surfaceNode->GetSurface();
69 if (fuzzSurface == nullptr) {
70 return false;
71 }
72 fuzzSurface->RequestBuffer(surfaceBuffer, releaseFence, requestConfig);
73 if (surfaceBuffer == nullptr) {
74 return false;
75 }
76 surfaceAdapter.CopyFrame(surfaceBuffer, src, width, height);
77 delete[] src;
78 return true;
79 }
80 } // namespace OHOS
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 OHOS::CopyFrameFuzzTest(data, size);
87 return 0;
88 }
89