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