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 "data_generate.h"
22 #include "surface_buffer.h"
23 #include "surface_buffer_impl.h"
24 #include "buffer_utils.h"
25 #include "sandbox_utils.h"
26 #include <message_parcel.h>
27 
28 using namespace g_fuzzCommon;
29 
30 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32     {
33         if (data == nullptr) {
34             return false;
35         }
36         int fd = GetData<int>();
37         BufferRequestConfig rqConfig = GetData<BufferRequestConfig>();
38         BufferFlushConfigWithDamages flConfig = {
39             .damages =  { GetData<OHOS::Rect>() },
40             .timestamp = GetData<int64_t>(),
41         };
42         uint32_t seqNum = GetData<uint32_t>();
43         uint32_t sequence = GetData<uint32_t>();
44         BufferVerifyAllocInfo vaInfo = GetData<BufferVerifyAllocInfo>();
45         GraphicHDRMetaData metaData = GetData<GraphicHDRMetaData>();
46         uint8_t metaData2 = GetData<uint8_t>();
47         uint32_t reserveInts = GetData<uint32_t>() % 0x100000; // no more than 0x100000
48         MessageParcel parcel;
49         sptr<SurfaceBuffer> buffer = new SurfaceBufferImpl(seqNum);
50         WriteFileDescriptor(parcel, fd);
51         ReadFileDescriptor(parcel, fd);
52         WriteRequestConfig(parcel, rqConfig);
53         ReadRequestConfig(parcel, rqConfig);
54         WriteFlushConfig(parcel, flConfig);
55         ReadFlushConfig(parcel, flConfig);
56         WriteSurfaceBufferImpl(parcel, sequence, buffer);
57         ReadSurfaceBufferImpl(parcel, sequence, buffer);
58         std::vector<BufferVerifyAllocInfo> infos = {vaInfo};
59         WriteVerifyAllocInfo(parcel, infos);
60         ReadVerifyAllocInfo(parcel, infos);
61         std::vector<GraphicHDRMetaData> metaDatas = {metaData};
62         WriteHDRMetaData(parcel, metaDatas);
63         ReadHDRMetaData(parcel, metaDatas);
64         std::vector<uint8_t> metaDatas2 = {metaData2};
65         WriteHDRMetaDataSet(parcel, metaDatas2);
66         ReadHDRMetaDataSet(parcel, metaDatas2);
67         GraphicExtDataHandle *handle = AllocExtDataHandle(reserveInts);
68         WriteExtDataHandle(parcel, handle);
69         sptr<SurfaceTunnelHandle> tunnelHandle = nullptr;
70         ReadExtDataHandle(parcel, tunnelHandle);
71         sptr<SurfaceTunnelHandle> tunnelHandle2 = new SurfaceTunnelHandle();
72         ReadExtDataHandle(parcel, tunnelHandle);
73         tunnelHandle2->SetHandle(handle);
74         tunnelHandle2->GetHandle();
75         tunnelHandle2->Different(tunnelHandle);
76         FreeExtDataHandle(handle);
77         DumpToFileAsync(GetRealPid(), "test", buffer);
78         return true;
79     }
80 } // namespace OHOS
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     /* Run your code on data */
86     g_data = data;
87     g_size = size;
88     g_pos = 0;
89     OHOS::DoSomethingInterestingWithMyAPI(data, size);
90     return 0;
91 }
92 
93