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 #include "native_buffer_inner.h"
25
26 using namespace g_fuzzCommon;
27
28 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
30 {
31 if (data == nullptr) {
32 return false;
33 }
34
35 // initialize
36 g_data = data;
37 g_size = size;
38 g_pos = 0;
39
40 // get data
41 OH_NativeBuffer_Config config;
42 config.width = 100; // 100 pixels
43 config.height = 100; // 100 pixels
44 config.format = GRAPHIC_PIXEL_FMT_RGBA_8888;
45 config.usage = BUFFER_USAGE_CPU_READ;
46 OH_NativeBuffer_Config checkConfig = GetData<OH_NativeBuffer_Config>();
47 void *virAddr = static_cast<void*>(GetStringFromData(STR_LEN).data());
48 OH_NativeBuffer_ColorSpace colorSpace = GetData<OH_NativeBuffer_ColorSpace>();
49
50 // test
51 OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
52 OH_NativeBuffer_GetSeqNum(buffer);
53 OH_NativeBuffer_GetBufferHandle(buffer);
54 OH_NativeBuffer_GetNativeBufferConfig(buffer, &checkConfig);
55 OHNativeWindowBuffer *nativeWindowBuffer = CreateNativeWindowBufferFromNativeBuffer(buffer);
56 OH_NativeBufferFromNativeWindowBuffer(nativeWindowBuffer);
57 OH_NativeBuffer_SetColorSpace(buffer, colorSpace);
58 void *getVirAddr;
59 OH_NativeBuffer_Planes outPlanes;
60 OH_NativeBuffer_MapPlanes(buffer, &getVirAddr, &outPlanes);
61 OH_NativeBuffer *nativeBuffer;
62 OH_NativeBuffer_FromNativeWindowBuffer(nativeWindowBuffer, &nativeBuffer);
63 OH_NativeBuffer_ColorSpace getColorSpace;
64 OH_NativeBuffer_GetColorSpace(buffer, &getColorSpace);
65 OH_NativeBuffer_MetadataKey metadataKey = GetData<OH_NativeBuffer_MetadataKey>();
66 uint32_t setSize = GetData<uint32_t>() % 1000;
67 uint8_t *metadata = (uint8_t *)malloc(setSize * sizeof(uint8_t));
68 OH_NativeBuffer_SetMetadataValue(buffer, metadataKey, static_cast<int32_t>(setSize), metadata);
69 free(metadata);
70 int32_t getSize;
71 uint8_t *getMetadata;
72 OH_NativeBuffer_GetMetadataValue(buffer, metadataKey, &getSize, &getMetadata);
73 OH_NativeBuffer_GetConfig(buffer, &checkConfig);
74 OH_NativeBuffer_Reference(buffer);
75 OH_NativeBuffer_Unreference(buffer);
76 OH_NativeBuffer_Map(buffer, &virAddr);
77 OH_NativeBuffer_Unmap(buffer);
78 DestroyNativeWindowBuffer(nativeWindowBuffer);
79 OH_NativeBuffer_Unreference(buffer);
80
81 return true;
82 }
83 } // namespace OHOS
84
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88 /* Run your code on data */
89 OHOS::DoSomethingInterestingWithMyAPI(data, size);
90 return 0;
91 }
92
93