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 "nativebuffer_fuzzer.h"
17
18 #include <securec.h>
19 #include <string>
20
21 #include "data_generate.h"
22 #include "native_buffer.h"
23 #include "native_window.h"
24
25 using namespace g_fuzzCommon;
26
27 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
29 {
30 if (data == nullptr) {
31 return false;
32 }
33
34 // initialize
35 g_data = data;
36 g_size = size;
37 g_pos = 0;
38
39 // get data
40 OH_NativeBuffer_Config config = GetData<OH_NativeBuffer_Config>();
41 config.width = 1920; // 1920 pixels
42 config.height = 1080; // 1080 pixels
43 OH_NativeBuffer_Config checkConfig = GetData<OH_NativeBuffer_Config>();
44 void *virAddr = static_cast<void*>(GetStringFromData(STR_LEN).data());
45
46 // test
47 OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
48 CreateNativeWindowBufferFromNativeBuffer(buffer);
49 OH_NativeBuffer_GetSeqNum(buffer);
50 OH_NativeBuffer_GetConfig(buffer, &checkConfig);
51 OH_NativeBuffer_Reference(buffer);
52 OH_NativeBuffer_Unreference(buffer);
53 OH_NativeBuffer_Map(buffer, &virAddr);
54 OH_NativeBuffer_Unmap(buffer);
55 OH_NativeBuffer_Unreference(buffer);
56 OH_NativeBuffer_Unreference(buffer);
57
58 return true;
59 }
60 } // namespace OHOS
61
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65 /* Run your code on data */
66 OHOS::DoSomethingInterestingWithMyAPI(data, size);
67 return 0;
68 }
69
70