1 /*
2  * Copyright (c) 2022-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 "socperf_fuzzer.h"
17 
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "i_socperf_service.h"
22 #include "socperf_fuzz_mock.h"
23 #include "socperf_log.h"
24 
25 namespace OHOS {
26 namespace SOCPERF {
27     constexpr int32_t MIN_LEN = 4;
28     std::mutex mutexLock;
29     sptr<IRemoteObject> remoteObj = nullptr;
30 
DoInit()31     bool DoInit()
32     {
33         std::lock_guard<std::mutex> lock(mutexLock);
34         if (remoteObj) {
35             return true;
36         }
37         auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38         if (!samgr) {
39             return false;
40         }
41         remoteObj = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID);
42         if (!remoteObj) {
43             return false;
44         }
45         return true;
46     }
47 
onRemoteRequest(uint32_t code, MessageParcel& data)48     int32_t onRemoteRequest(uint32_t code, MessageParcel& data)
49     {
50         if (!DoInit()) {
51             return -1;
52         }
53         MessageParcel reply;
54         MessageOption option;
55         return remoteObj->SendRequest(code, data, reply, option);
56     }
57 
DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)58     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
59     {
60         if (size <= MIN_LEN) {
61             return false;
62         }
63 
64         MessageParcel dataMessageParcel;
65         if (!dataMessageParcel.WriteInterfaceToken(IRemoteStub<ISocPerfService>::GetDescriptor())) {
66             return false;
67         }
68 
69         uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
70         size -= sizeof(uint32_t);
71 
72         dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size);
73         dataMessageParcel.RewindRead(0);
74 
75         onRemoteRequest(code, dataMessageParcel);
76         return true;
77     }
78 } // namespace SOCPERF
79 } // namespace OHOS
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::SOCPERF::MockProcess();
86     OHOS::SOCPERF::DoSomethingInterestingWithMyAPI(data, size);
87     return 0;
88 }
89 
90