1 /*
2  * Copyright (c) 2024 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 "timerproxy_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21 
22 #include "time_service_fuzz_utils.h"
23 #include "time_service_ipc_interface_code.h"
24 #include "timer_proxy.h"
25 #include <fuzzer/FuzzedDataProvider.h>
26 
27 using namespace OHOS::MiscServices;
28 
29 namespace OHOS {
30 constexpr size_t THRESHOLD = 4;
31 
FuzzTimerProxyTimer(const uint8_t * data,size_t size)32 bool FuzzTimerProxyTimer(const uint8_t *data, size_t size)
33 {
34     auto now = std::chrono::steady_clock::now();
35     auto callback = [] (std::shared_ptr<TimerInfo> &alarm) {};
36     int32_t uid = static_cast<int32_t>(*data);
37 
38     TimerProxy::GetInstance().ProxyTimer(uid, true, true, now, callback);
39     TimerProxy::GetInstance().ProxyTimer(uid, true, false, now, callback);
40     TimerProxy::GetInstance().ProxyTimer(uid, false, true, now, callback);
41     TimerProxy::GetInstance().ProxyTimer(uid, false, false, now, callback);
42 
43     TimerProxy::GetInstance().PidProxyTimer(uid, uid, true, true, now, callback);
44     TimerProxy::GetInstance().PidProxyTimer(uid, uid, true, false, now, callback);
45     TimerProxy::GetInstance().PidProxyTimer(uid, uid, false, true, now, callback);
46     TimerProxy::GetInstance().PidProxyTimer(uid, uid, false, false, now, callback);
47     return true;
48 }
49 
FuzzTimerAdjustTimer(const uint8_t * data,size_t size)50 bool FuzzTimerAdjustTimer(const uint8_t *data, size_t size)
51 {
52     auto interval = static_cast<uint32_t>(*data);
53     auto now = std::chrono::steady_clock::now();
54     auto callback = [] (AdjustTimerCallback adjustTimer) {};
55     TimerProxy::GetInstance().AdjustTimer(true, interval, now, callback);
56     TimerProxy::GetInstance().AdjustTimer(false, interval, now, callback);
57     return true;
58 }
59 
FuzzTimerSetTimerExemption(const uint8_t * data,size_t size)60 bool FuzzTimerSetTimerExemption(const uint8_t *data, size_t size)
61 {
62     std::string name(reinterpret_cast<const char *>(data), size);
63     std::unordered_set<std::string> nameArr{name};
64     TimerProxy::GetInstance().SetTimerExemption(nameArr, false);
65     TimerProxy::GetInstance().SetTimerExemption(nameArr, true);
66     return true;
67 }
68 
FuzzTimerResetProxy(const uint8_t * data,size_t size)69 bool FuzzTimerResetProxy(const uint8_t *data, size_t size)
70 {
71     auto now = std::chrono::steady_clock::now();
72     auto callback = [] (std::shared_ptr<TimerInfo> &alarm) {};
73     TimerProxy::GetInstance().ResetAllProxy(now, callback);
74     return true;
75 }
76 
FuzzTimerEraseTimer(const uint8_t * data,size_t size)77 bool FuzzTimerEraseTimer(const uint8_t *data, size_t size)
78 {
79     auto id = static_cast<uint64_t>(*data);
80     auto uid = static_cast<uint32_t>(*data);
81     auto pid = static_cast<int>(*data);
82     TimerProxy::GetInstance().EraseTimerFromProxyUidMap(id, uid);
83     TimerProxy::GetInstance().EraseTimerFromProxyPidMap(id, uid, pid);
84     return true;
85 }
86 
FuzzTimerRemoveTimerMap(const uint8_t * data,size_t size)87 bool FuzzTimerRemoveTimerMap(const uint8_t *data, size_t size)
88 {
89     auto id = static_cast<int64_t>(*data);
90     TimerProxy::GetInstance().RemoveUidTimerMap(id);
91     TimerProxy::GetInstance().RemovePidTimerMap(id);
92     return true;
93 }
94 
FuzzTimerIsProxy(const uint8_t * data,size_t size)95 bool FuzzTimerIsProxy(const uint8_t *data, size_t size)
96 {
97     FuzzedDataProvider fdp(data, size);
98     int uid = fdp.ConsumeIntegral<int>();
99     int pid = fdp.ConsumeIntegral<int>();
100     TimerProxy::GetInstance().IsUidProxy(uid);
101     TimerProxy::GetInstance().IsPidProxy(uid, pid);
102     return true;
103 }
104 
105 } // namespace OHOS
106 
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
109 {
110     if (size < OHOS::THRESHOLD) {
111         return 0;
112     }
113 
114     /* Run your code on data */
115     OHOS::FuzzTimerProxyTimer(data, size);
116     OHOS::FuzzTimerAdjustTimer(data, size);
117     OHOS::FuzzTimerSetTimerExemption(data, size);
118     OHOS::FuzzTimerResetProxy(data, size);
119     OHOS::FuzzTimerEraseTimer(data, size);
120     OHOS::FuzzTimerRemoveTimerMap(data, size);
121     OHOS::FuzzTimerIsProxy(data, size);
122     return 0;
123 }