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 "eglwrapperdisplay_fuzzer.h"
17
18 #include <securec.h>
19
20 #include <EGL/egl.h>
21 #include "egl_wrapper_display.h"
22 #include "egl_wrapper_object.h"
23
24 namespace OHOS {
25 namespace {
26 const uint8_t* data_ = nullptr;
27 size_t size_ = 0;
28 size_t pos;
29 }
30
31 /*
32 * describe: get data from outside untrusted data(data_) which size is according to sizeof(T)
33 * tips: only support basic type
34 */
35 template<class T>
GetData()36 T GetData()
37 {
38 T object {};
39 size_t objectSize = sizeof(object);
40 if (data_ == nullptr || objectSize > size_ - pos) {
41 return object;
42 }
43 errno_t ret = memcpy_s(&object, objectSize, data_ + pos, objectSize);
44 if (ret != EOK) {
45 return {};
46 }
47 pos += objectSize;
48 return object;
49 }
50
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
52 {
53 if (data == nullptr || size < 0) {
54 return false;
55 }
56
57 // initialize
58 data_ = data;
59 size_ = size;
60 pos = 0;
61
62 auto eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
63 EGLDisplay display1 = GetData<EGLDisplay>();
64 EGLDisplay display2 = GetData<EGLDisplay>();
65 EGLint major1 = GetData<EGLint>();
66 EGLint minor1 = GetData<EGLint>();
67 EGLSurface draw1 = GetData<EGLSurface>();
68 EGLSurface read1 = GetData<EGLSurface>();
69 EGLContext ctx1 = GetData<EGLContext>();
70 EGLenum platform1 = GetData<EGLenum>();
71 EGLNativeDisplayType disp1 = GetData<EGLNativeDisplayType>();
72 EGLAttrib attribList1 = GetData<EGLAttrib>();
73 EGLenum platform2 = GetData<EGLenum>();
74 void* disp2 = GetData<void*>();
75 EGLint attribList2 = GetData<EGLint>();
76 EGLContext ctx2 = GetData<EGLContext>();
77 EGLSurface surf1 = GetData<EGLSurface>();
78 EGLConfig config1 = GetData<EGLConfig>();
79 EGLContext shareList1 = GetData<EGLContext>();
80 EGLint attribList3 = GetData<EGLint>();
81 EGLConfig config2 = GetData<EGLConfig>();
82 NativeWindowType window1 = GetData<NativeWindowType>();
83 EGLint attribList4 = GetData<EGLint>();
84 EGLContext context1 = GetData<EGLContext>();
85 EGLSurface surf2 = GetData<EGLSurface>();
86 EglWrapperDisplay* disp3 = EglWrapperDisplay::GetWrapperDisplay(display1);
87 EglWrapperObject *obj1 = new EglWrapperObject(disp3);
88 EglWrapperDisplay* disp4 = EglWrapperDisplay::GetWrapperDisplay(display2);
89 EglWrapperObject *obj2 = new EglWrapperObject(disp4);
90 EGLSurface surf3 = GetData<EGLSurface>();
91 NativePixmapType target1 = GetData<NativePixmapType>();
92 EGLConfig config3 = GetData<EGLConfig>();
93 EGLint attribList5 = GetData<EGLint>();
94 EGLConfig config4 = GetData<EGLConfig>();
95 EGLNativePixmapType pixmap1 = GetData<EGLNativePixmapType>();
96 EGLint attribList6 = GetData<EGLint>();
97 EGLContext ctx3 = GetData<EGLContext>();
98 EGLint attribList7 = GetData<EGLint>();
99 EGLint value1 = GetData<EGLint>();
100 EGLSurface surf4 = GetData<EGLSurface>();
101 EGLint attribList8 = GetData<EGLint>();
102 EGLint value2 = GetData<EGLint>();
103 EGLSurface surf5 = GetData<EGLSurface>();
104 EGLSurface surf6 = GetData<EGLSurface>();
105 EGLint buffer1 = GetData<EGLint>();
106 EGLSurface surf7 = GetData<EGLSurface>();
107 EGLint buffer2 = GetData<EGLint>();
108 EGLSurface surf8 = GetData<EGLSurface>();
109 EGLint attribList9 = GetData<EGLint>();
110 EGLint value3 = GetData<EGLint>();
111 EGLenum buftype1 = GetData<EGLenum>();
112 EGLClientBuffer buffer3 = GetData<EGLClientBuffer>();
113 EGLConfig config5 = GetData<EGLConfig>();
114 EGLint attribList10 = GetData<EGLint>();
115 EGLContext ctx4 = GetData<EGLContext>();
116 EGLenum target2 = GetData<EGLenum>();
117 EGLClientBuffer buffer4 = GetData<EGLClientBuffer>();
118 EGLAttrib attribList11 = GetData<EGLAttrib>();
119 EGLImage img1 = GetData<EGLImage>();
120 EGLConfig config6 = GetData<EGLConfig>();
121 void *nativeWindow = GetData<void*>();
122 EGLAttrib attribList12 = GetData<EGLAttrib>();
123 EGLConfig config7 = GetData<EGLConfig>();
124 void *nativePixmap = GetData<void*>();
125 EGLAttrib attribList13 = GetData<EGLAttrib>();
126 EGLSurface surf9 = GetData<EGLSurface>();
127 EGLint attribList14 = GetData<EGLint>();
128 EGLSurface surf10 = GetData<EGLSurface>();
129 EGLContext ctx5 = GetData<EGLContext>();
130 EGLenum target3 = GetData<EGLenum>();
131 EGLClientBuffer buffer5 = GetData<EGLClientBuffer>();
132 EGLint attribList15 = GetData<EGLint>();
133 EGLImageKHR img2 = GetData<EGLImageKHR>();
134 EGLConfig config8 = GetData<EGLConfig>();
135 EGLStreamKHR stream = GetData<EGLStreamKHR>();
136 EGLint attribList16 = GetData<EGLint>();
137 EGLSurface draw2 = GetData<EGLSurface>();
138 EGLint rects1 = GetData<EGLint>();
139 EGLint nRects1 = GetData<EGLint>();
140 EGLSurface surf11 = GetData<EGLSurface>();
141 EGLint rects2 = GetData<EGLint>();
142 EGLint nRects2 = GetData<EGLint>();
143
144 // test
145 EglWrapperDisplay* eglWrapperDisplay = EglWrapperDisplay::GetWrapperDisplay(eglDisplay);
146 eglWrapperDisplay->Init(&major1, &minor1);
147 eglWrapperDisplay->MakeCurrent(draw1, read1, ctx1);
148 EglWrapperDisplay::GetEglDisplay(platform1, disp1, &attribList1);
149 EglWrapperDisplay::GetEglDisplayExt(platform2, disp2, &attribList2);
150 eglWrapperDisplay->ValidateEglContext(ctx2);
151 eglWrapperDisplay->ValidateEglSurface(surf1);
152 eglWrapperDisplay->CreateEglContext(config1, shareList1, &attribList3);
153 eglWrapperDisplay->CreateEglSurface(config2, window1, &attribList4);
154 eglWrapperDisplay->DestroyEglContext(context1);
155 eglWrapperDisplay->DestroyEglSurface(surf2);
156 eglWrapperDisplay->AddObject(obj1);
157 eglWrapperDisplay->RemoveObject(obj2);
158 eglWrapperDisplay->CopyBuffers(surf3, target1);
159 eglWrapperDisplay->CreatePbufferSurface(config3, &attribList5);
160 eglWrapperDisplay->CreatePixmapSurface(config4, pixmap1, &attribList6);
161 eglWrapperDisplay->QueryContext(ctx3, attribList7, &value1);
162 eglWrapperDisplay->QuerySurface(surf4, attribList8, &value2);
163 eglWrapperDisplay->SwapBuffers(surf5);
164 eglWrapperDisplay->BindTexImage(surf6, buffer1);
165 eglWrapperDisplay->ReleaseTexImage(surf7, buffer2);
166 eglWrapperDisplay->SurfaceAttrib(surf8, attribList9, value3);
167 eglWrapperDisplay->CreatePbufferFromClientBuffer(buftype1, buffer3, config5, &attribList10);
168 eglWrapperDisplay->CreateImage(ctx4, target2, buffer4, &attribList11);
169 eglWrapperDisplay->DestroyImage(img1);
170 eglWrapperDisplay->CreatePlatformWindowSurface(config6, nativeWindow, &attribList12);
171 eglWrapperDisplay->CreatePlatformPixmapSurface(config7, nativePixmap, &attribList13);
172 eglWrapperDisplay->LockSurfaceKHR(surf9, &attribList14);
173 eglWrapperDisplay->UnlockSurfaceKHR(surf10);
174 eglWrapperDisplay->CreateImageKHR(ctx5, target3, buffer5, &attribList15);
175 eglWrapperDisplay->DestroyImageKHR(img2);
176 eglWrapperDisplay->CreateStreamProducerSurfaceKHR(config8, stream, &attribList16);
177 eglWrapperDisplay->SwapBuffersWithDamageKHR(draw2, &rects1, nRects1);
178 eglWrapperDisplay->SetDamageRegionKHR(surf11, &rects2, nRects2);
179
180 return true;
181 }
182 }
183
184 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)185 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
186 {
187 /* Run your code on data */
188 OHOS::DoSomethingInterestingWithMyAPI(data, size);
189 return 0;
190 }