1 /*
2 * Copyright (c) 2025 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 "nativefence_fuzzer.h"
17
18 #include <fcntl.h>
19 #include <securec.h>
20 #include <string>
21 #include <unistd.h>
22
23 #include "data_generate.h"
24 #include "native_fence.h"
25
26 using namespace g_fuzzCommon;
27
28 namespace OHOS {
29 constexpr uint32_t TIMEOUT_MS = 1000;
DoSomethingInterestingWithMyAPI001(const uint8_t * data,size_t size)30 bool DoSomethingInterestingWithMyAPI001(const uint8_t* data, size_t size)
31 {
32 if (data == nullptr) {
33 return false;
34 }
35
36 // initialize
37 g_data = data;
38 g_size = size;
39 g_pos = 0;
40
41 int32_t fenceFd = open("/dev/GPIO_TEST", O_RDONLY);
42 if (fenceFd < 0) {
43 return false;
44 }
45 OH_NativeFence_IsValid(fenceFd);
46 OH_NativeFence_Wait(fenceFd, TIMEOUT_MS);
47 OH_NativeFence_Close(fenceFd);
48
49 return true;
50 }
51
DoSomethingInterestingWithMyAPI002(const uint8_t * data,size_t size)52 bool DoSomethingInterestingWithMyAPI002(const uint8_t* data, size_t size)
53 {
54 if (data == nullptr) {
55 return false;
56 }
57
58 // initialize
59 g_data = data;
60 g_size = size;
61
62 int32_t fenceFd = GetData<int32_t>();
63 OH_NativeFence_IsValid(fenceFd);
64 OH_NativeFence_WaitForever(-1);
65
66 return true;
67 }
68 } // namespace OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::DoSomethingInterestingWithMyAPI001(data, size);
75 OHOS::DoSomethingInterestingWithMyAPI002(data, size);
76 return 0;
77 }
78
79