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 "flushbuffer_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 using namespace OHOS::Rosen;
31
32 namespace OHOS {
33 namespace {
34 sptr<Surface> g_surface = nullptr;
35 }
36 constexpr int MAX_SET_NUMBER = 1000;
37 constexpr int BITS_PER_PIXEL = 4;
38
FlushBufferFuzzTest(const uint8_t * data,size_t size)39 bool FlushBufferFuzzTest(const uint8_t* data, size_t size)
40 {
41 if ((data == nullptr) || (size == 0)) {
42 return false;
43 }
44 FuzzedDataProvider dataProvider(data, size);
45 uint32_t width = dataProvider.ConsumeIntegralInRange<uint32_t>(1, MAX_SET_NUMBER);
46 uint32_t height = dataProvider.ConsumeIntegralInRange<uint32_t>(1, MAX_SET_NUMBER);
47 char* buffer = new char[width * height * BITS_PER_PIXEL] { 0 };
48 if (buffer == nullptr) {
49 return false;
50 }
51 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
52 auto surfaceAdapter = NWebSurfaceAdapter::Instance();
53 surfaceAdapter.FlushBuffer(g_surface, surfaceBuffer, width, height);
54 wptr<Surface> surfaceWeakPtr(g_surface);
55 surfaceAdapter.OutputFrameCallback(buffer, width, height, surfaceWeakPtr);
56 surfaceAdapter.RequestBuffer(g_surface, width, height);
57 if (!g_surface) {
58 RSSurfaceNodeConfig config;
59 config.SurfaceNodeName = "webTestSurfaceName";
60 auto surfaceNode = RSSurfaceNode::Create(config, false);
61 if (surfaceNode == nullptr) {
62 delete[] buffer;
63 return false;
64 }
65 g_surface = surfaceNode->GetSurface();
66 if (g_surface == nullptr) {
67 delete[] buffer;
68 return false;
69 }
70 }
71 surfaceAdapter.FlushBuffer(g_surface, surfaceBuffer, width, height);
72 wptr<Surface> consumerSurfaceWeakPtr(Surface::CreateSurfaceAsConsumer());
73 surfaceAdapter.OutputFrameCallback(buffer, width, height, consumerSurfaceWeakPtr);
74 surfaceAdapter.RequestBuffer(g_surface, width, height);
75 delete[] buffer;
76 return true;
77 }
78 } // namespace OHOS
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::FlushBufferFuzzTest(data, size);
85 return 0;
86 }
87