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 OHOS::Rect rect = GetData<OHOS::Rect>();
68 int64_t timestamp = GetData<int64_t>();
69 BufferFlushConfigWithDamages flConfig = {
70 .damages = { rect },
71 .timestamp = timestamp,
72 };
73 uint32_t seqNum = GetData<uint32_t>();
74 uint32_t sequence = GetData<uint32_t>();
75 BufferVerifyAllocInfo vaInfo = GetData<BufferVerifyAllocInfo>();
76 GraphicHDRMetaData metaData = GetData<GraphicHDRMetaData>();
77 uint8_t metaData2 = GetData<uint8_t>();
78 uint32_t reserveInts = GetData<uint32_t>() % 0x100000; // no more than 0x100000
79
80 // test
81 MessageParcel parcel;
82 sptr<SurfaceBuffer> buffer = new SurfaceBufferImpl(seqNum);
83 WriteFileDescriptor(parcel, fd);
84 ReadFileDescriptor(parcel, fd);
85 WriteRequestConfig(parcel, rqConfig);
86 ReadRequestConfig(parcel, rqConfig);
87 WriteFlushConfig(parcel, flConfig);
88 ReadFlushConfig(parcel, flConfig);
89 WriteSurfaceBufferImpl(parcel, sequence, buffer);
90 ReadSurfaceBufferImpl(parcel, sequence, buffer);
91 std::vector<BufferVerifyAllocInfo> infos = {vaInfo};
92 WriteVerifyAllocInfo(parcel, infos);
93 ReadVerifyAllocInfo(parcel, infos);
94 std::vector<GraphicHDRMetaData> metaDatas = {metaData};
95 WriteHDRMetaData(parcel, metaDatas);
96 ReadHDRMetaData(parcel, metaDatas);
97 std::vector<uint8_t> metaDatas2 = {metaData2};
98 WriteHDRMetaDataSet(parcel, metaDatas2);
99 ReadHDRMetaDataSet(parcel, metaDatas2);
100
101 GraphicExtDataHandle *handle = AllocExtDataHandle(reserveInts);
102 WriteExtDataHandle(parcel, handle);
103 sptr<SurfaceTunnelHandle> tunnelHandle = nullptr;
104 ReadExtDataHandle(parcel, tunnelHandle);
105 sptr<SurfaceTunnelHandle> tunnelHandle2 = new SurfaceTunnelHandle();
106 tunnelHandle2->SetHandle(handle);
107 tunnelHandle2->Different(tunnelHandle);
108 FreeExtDataHandle(handle);
109
110 return true;
111 }
112 } // namespace OHOS
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
116 {
117 /* Run your code on data */
118 OHOS::DoSomethingInterestingWithMyAPI(data, size);
119 return 0;
120 }
121
122