• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundleactiveonremoterequest_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "app_mgr_interface.h"
20 
21 #define private public
22 #include "system_ability_definition.h"
23 #include "iservice_registry.h"
24 #include "bundle_active_stub.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28     constexpr int32_t MIN_LEN = 4;
29     constexpr int32_t MAX_CODE_TEST = 15; // current max code is 9
30     static bool isInited = false;
31     std::mutex mutexLock;
32     sptr<IRemoteObject> remoteObject;
33 
DoInit()34     bool DoInit()
35     {
36         std::lock_guard<std::mutex> lock(mutexLock);
37         if (remoteObject != nullptr) {
38             return true;
39         }
40         sptr<ISystemAbilityManager> SystemAbilityManager =
41             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42         if (SystemAbilityManager == nullptr) {
43             return false;
44         }
45         remoteObject = SystemAbilityManager->GetSystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID);
46         if (remoteObject == nullptr) {
47             return false;
48         }
49         return true;
50     }
51 
OnRemoteRequest(uint32_t code,MessageParcel & data)52     int32_t OnRemoteRequest(uint32_t code, MessageParcel& data)
53     {
54         MessageParcel reply;
55         MessageOption option;
56         int32_t ret = remoteObject->SendRequest(code, data, reply, option);
57         return ret;
58     }
59 
IpcBundleActiveStubFuzzTest(const uint8_t * data,size_t size)60     void IpcBundleActiveStubFuzzTest(const uint8_t* data, size_t size)
61     {
62         if (size <= MIN_LEN) {
63             return;
64         }
65 
66         MessageParcel dataMessageParcel;
67         if (!dataMessageParcel.WriteInterfaceToken(BundleActiveStub::GetDescriptor())) {
68             return;
69         }
70 
71         uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
72         if (code > MAX_CODE_TEST) {
73             return;
74         }
75 
76         size -= sizeof(uint32_t);
77 
78         dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size);
79         dataMessageParcel.RewindRead(0);
80 
81         if (!isInited) {
82             isInited = DoInit();
83         }
84         if (isInited) {
85             OnRemoteRequest(code, dataMessageParcel);
86         }
87     }
88 } // namespace DeviceUsageStats
89 } // namespace OHOS
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     OHOS::DeviceUsageStats::IpcBundleActiveStubFuzzTest(data, size);
96     return 0;
97 }
98 
99