• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bufferutils_fuzzer.h"
17 
18 #include <securec.h>
19 #include <vector>
20 
21 #include "surface_buffer.h"
22 #include "surface_buffer_impl.h"
23 #include "buffer_utils.h"
24 #include <message_parcel.h>
25 
26 namespace OHOS {
27     namespace {
28         const uint8_t* g_data = nullptr;
29         size_t g_size = 0;
30         size_t g_pos;
31     }
32 
33     /*
34     * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
35     * tips: only support basic type
36     */
37     template<class T>
GetData()38     T GetData()
39     {
40         T object {};
41         size_t objectSize = sizeof(object);
42         if (g_data == nullptr || objectSize > g_size - g_pos) {
43             return object;
44         }
45         errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
46         if (ret != EOK) {
47             return {};
48         }
49         g_pos += objectSize;
50         return object;
51     }
52 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)53     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
54     {
55         if (data == nullptr) {
56             return false;
57         }
58 
59         // initialize
60         g_data = data;
61         g_size = size;
62         g_pos = 0;
63 
64         // get data
65         int fd = GetData<int>();
66         BufferRequestConfig rqConfig = GetData<BufferRequestConfig>();
67         BufferFlushConfig flConfig = GetData<BufferFlushConfig>();
68         uint32_t seqNum = GetData<uint32_t>();
69         uint32_t sequence = GetData<uint32_t>();
70         BufferVerifyAllocInfo vaInfo = GetData<BufferVerifyAllocInfo>();
71         GraphicHDRMetaData metaData = GetData<GraphicHDRMetaData>();
72         uint8_t metaData2 = GetData<uint8_t>();
73         uint32_t reserveInts = GetData<uint32_t>() % 0x100000; // no more than 0x100000
74 
75         // test
76         MessageParcel parcel;
77         sptr<SurfaceBuffer> buffer = new SurfaceBufferImpl(seqNum);
78         WriteFileDescriptor(parcel, fd);
79         ReadFileDescriptor(parcel, fd);
80         WriteRequestConfig(parcel, rqConfig);
81         ReadRequestConfig(parcel, rqConfig);
82         WriteFlushConfig(parcel, flConfig);
83         ReadFlushConfig(parcel, flConfig);
84         WriteSurfaceBufferImpl(parcel, sequence, buffer);
85         ReadSurfaceBufferImpl(parcel, sequence, buffer);
86         std::vector<BufferVerifyAllocInfo> infos = {vaInfo};
87         WriteVerifyAllocInfo(parcel, infos);
88         ReadVerifyAllocInfo(parcel, infos);
89         std::vector<GraphicHDRMetaData> metaDatas = {metaData};
90         WriteHDRMetaData(parcel, metaDatas);
91         ReadHDRMetaData(parcel, metaDatas);
92         std::vector<uint8_t> metaDatas2 = {metaData2};
93         WriteHDRMetaDataSet(parcel, metaDatas2);
94         ReadHDRMetaDataSet(parcel, metaDatas2);
95 
96         GraphicExtDataHandle *handle = AllocExtDataHandle(reserveInts);
97         WriteExtDataHandle(parcel, handle);
98         sptr<SurfaceTunnelHandle> tunnelHandle = nullptr;
99         ReadExtDataHandle(parcel, tunnelHandle);
100         sptr<SurfaceTunnelHandle> tunnelHandle2 = new SurfaceTunnelHandle();
101         tunnelHandle2->SetHandle(handle);
102         tunnelHandle2->Different(tunnelHandle);
103         FreeExtDataHandle(handle);
104 
105         return true;
106     }
107 } // namespace OHOS
108 
109 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
111 {
112     /* Run your code on data */
113     OHOS::DoSomethingInterestingWithMyAPI(data, size);
114     return 0;
115 }
116 
117