1 /*
2 * Copyright (c) 2023 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 "nativeImageadapter_fuzzer.h"
17
18 #include <cstdint>
19 #include <cstring>
20 #include <memory>
21 #include <securec.h>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #include "native_image_adapter_impl.h"
25
26 namespace OHOS {
27
28 using namespace OHOS::NWeb;
29 constexpr int MAX_SET_NUMBER = 1000;
30
31 class DummyFrameAvailableListener : public FrameAvailableListener {
32 public:
33 void (*OnFrameAvailableCallback)() = nullptr;
34
SetOnFrameAvailableCallback(void (* callback)())35 void SetOnFrameAvailableCallback(void (*callback)())
36 {
37 OnFrameAvailableCallback = callback;
38 }
39
OnFrameAvailable()40 void OnFrameAvailable()
41 {
42 if (OnFrameAvailableCallback) {
43 OnFrameAvailableCallback();
44 }
45 }
46
GetContext()47 void* GetContext() override
48 {
49 return nullptr;
50 }
51
GetOnFrameAvailableCb()52 OnFrameAvailableCb GetOnFrameAvailableCb() override
53 {
54 return nullptr;
55 }
56 };
57
NativeImageFuzzTest(const uint8_t * data,size_t size)58 bool NativeImageFuzzTest(const uint8_t* data, size_t size)
59 {
60 if (data == nullptr || size == 0) {
61 return false;
62 }
63 NativeImageAdapterImpl adapter;
64 FuzzedDataProvider dataProvider(data, size);
65
66 uint32_t textureId = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
67 uint32_t textureTarget = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
68 adapter.CreateNativeImage(textureId, textureTarget);
69
70 NWebNativeWindow window = adapter.AquireNativeWindowFromNativeImage();
71 if (window == nullptr) {
72 return false;
73 }
74
75 adapter.AttachContext(textureId);
76
77 adapter.GetTimestamp();
78
79 adapter.UpdateSurfaceImage();
80
81 float matrix[16] = { 0 };
82 int32_t result = adapter.GetTransformMatrix(matrix);
83 if (result != 0) {
84 return false;
85 }
86
87 uint64_t surfaceId;
88 result = adapter.GetSurfaceId(&surfaceId);
89 if (result != 0) {
90 return false;
91 }
92
93 auto listener = std::make_shared<DummyFrameAvailableListener>();
94 listener->SetOnFrameAvailableCallback([]() {});
95
96 adapter.SetOnFrameAvailableListener(listener);
97 adapter.UnsetOnFrameAvailableListener();
98 adapter.NewNativeImage();
99 adapter.DestroyNativeImage();
100 adapter.DetachContext();
101 void* buffer = nullptr;
102 int fd = dataProvider.ConsumeIntegralInRange<int>(0, MAX_SET_NUMBER);
103 adapter.AcquireNativeWindowBuffer(&buffer, &fd);
104 void* nativeBuffer = nullptr;
105 adapter.GetNativeBuffer(buffer, &nativeBuffer);
106 uint32_t width = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
107 uint32_t height = dataProvider.ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
108 adapter.GetNativeWindowBufferSize(nativeBuffer, &width, &height);
109 adapter.ReleaseNativeWindowBuffer(nativeBuffer, fd);
110
111 return true;
112 }
113
114 } // namespace OHOS
115
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117 {
118 return OHOS::NativeImageFuzzTest(data, size);
119 }