• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "timetesttimer_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21 
22 #include "time_service_client.h"
23 #include "timer_info.h"
24 
25 using namespace OHOS::MiscServices;
26 
27 namespace OHOS {
28 constexpr size_t U64_AT_SIZE = 8;
29 
FuzzTimeCreateTimer(int64_t data,size_t size)30 bool FuzzTimeCreateTimer(int64_t data, size_t size)
31 {
32     auto timerInfo = std::make_shared<TimerInfo>();
33     timerInfo->SetType(static_cast<int>(data));
34     timerInfo->SetInterval(data);
35     timerInfo->SetRepeat(data);
36     uint64_t timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
37     TimeServiceClient::GetInstance()->DestroyTimer(timerId);
38     return true;
39 }
40 
FuzzTimeStartTimer(int64_t timerId,size_t size)41 bool FuzzTimeStartTimer(int64_t timerId, size_t size)
42 {
43     TimeServiceClient::GetInstance()->StartTimer(timerId, timerId);
44     return true;
45 }
46 
FuzzTimeStopTimer(int64_t timerId,size_t size)47 bool FuzzTimeStopTimer(int64_t timerId, size_t size)
48 {
49     TimeServiceClient::GetInstance()->StopTimer(timerId);
50     return true;
51 }
52 
FuzzTimeDestroyTimer(int64_t timerId,size_t size)53 bool FuzzTimeDestroyTimer(int64_t timerId, size_t size)
54 {
55     TimeServiceClient::GetInstance()->DestroyTimer(timerId);
56     return true;
57 }
58 
FuzzTimeCreateTimerV9(int64_t data,size_t size)59 bool FuzzTimeCreateTimerV9(int64_t data, size_t size)
60 {
61     auto timerInfo = std::make_shared<TimerInfo>();
62     timerInfo->SetType(static_cast<int>(data));
63     timerInfo->SetInterval(data);
64     timerInfo->SetRepeat(data);
65     timerInfo->SetAutoRestore(false);
66     uint64_t timerId = 0;
67     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
68     TimeServiceClient::GetInstance()->DestroyTimer(timerId);
69     return true;
70 }
71 
FuzzTimeStartTimerV9(int64_t timerId,size_t size)72 bool FuzzTimeStartTimerV9(int64_t timerId, size_t size)
73 {
74     TimeServiceClient::GetInstance()->StartTimerV9(timerId, timerId);
75     return true;
76 }
77 
FuzzTimeStopTimerV9(int64_t timerId,size_t size)78 bool FuzzTimeStopTimerV9(int64_t timerId, size_t size)
79 {
80     TimeServiceClient::GetInstance()->StopTimerV9(timerId);
81     return true;
82 }
83 
FuzzTimeDestroyTimerV9(int64_t timerId,size_t size)84 bool FuzzTimeDestroyTimerV9(int64_t timerId, size_t size)
85 {
86     TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
87     return true;
88 }
89 
90 }
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
93 {
94     /* Run your code on data */
95     if (data == nullptr) {
96         return 0;
97     }
98 
99     if (size < OHOS::U64_AT_SIZE) {
100         return 0;
101     }
102 
103     auto timerId = static_cast<int64_t>(*data);
104     OHOS::FuzzTimeCreateTimer(timerId, size);
105     OHOS::FuzzTimeStartTimer(timerId, size);
106     OHOS::FuzzTimeStopTimer(timerId, size);
107     OHOS::FuzzTimeDestroyTimer(timerId, size);
108     OHOS::FuzzTimeCreateTimerV9(timerId, size);
109     OHOS::FuzzTimeStartTimerV9(timerId, size);
110     OHOS::FuzzTimeStopTimerV9(timerId, size);
111     OHOS::FuzzTimeDestroyTimerV9(timerId, size);
112     return 0;
113 }