• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundleconnectabilitymgr_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "bundle_connect_ability_mgr.h"
23 #include "securec.h"
24 using Want = OHOS::AAFwk::Want;
25 using namespace OHOS::AppExecFwk;
26 namespace OHOS {
27 constexpr size_t U32_AT_SIZE = 4;
28 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
30 {
31     BundleConnectAbilityMgr bundleConnectAbilityMgr;
32     Want want;
33     int32_t userId = reinterpret_cast<uintptr_t>(data);
34     int32_t flags = reinterpret_cast<uintptr_t>(data);
35     AbilityInfo abilityInfo;
36     sptr<IRemoteObject> callBack = nullptr;
37     bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
38     bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
39     bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
40     std::string installResult(data, size);
41     bundleConnectAbilityMgr.OnServiceCenterCall(installResult);
42     bundleConnectAbilityMgr.OnDelayedHeartbeat(installResult);
43     bundleConnectAbilityMgr.DeathRecipientSendCallback();
44     sptr<IRemoteObject> callerToken = nullptr;
45     bundleConnectAbilityMgr.ConnectAbility(want, callerToken);
46     bundleConnectAbilityMgr.ProcessPreload(want);
47     bundleConnectAbilityMgr.DisconnectAbility();
48     int32_t code = reinterpret_cast<uintptr_t>(data);
49     MessageParcel datas;
50     datas.WriteBuffer(data, size);
51     datas.RewindRead(0);
52     MessageParcel reply;
53     bundleConnectAbilityMgr.SendRequest(code, datas, reply);
54     TargetAbilityInfo targetAbilityInfo1;
55     FreeInstallParams freeInstallParams;
56     bundleConnectAbilityMgr.UpgradeCheck(targetAbilityInfo1, want, freeInstallParams, userId);
57     int32_t callingUid = reinterpret_cast<uintptr_t>(data);
58     std::vector<std::string> bundleNames = { std::string(data, size) };
59     std::vector<std::string> callingAppIds = { std::string(data, size) };
60     bundleConnectAbilityMgr.GetCallingInfo(userId, callingUid, bundleNames, callingAppIds);
61     InnerBundleInfo innerBundleInfo;
62     sptr<TargetAbilityInfo> targetAbilityInfo = nullptr;
63     bundleConnectAbilityMgr.GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
64     bundleConnectAbilityMgr.CheckIsModuleNeedUpdate(innerBundleInfo, want, userId, callBack);
65     int32_t resultCode = reinterpret_cast<uintptr_t>(data);
66     std::string transactId(data, size);
67     bundleConnectAbilityMgr.SendCallBack(resultCode, want, userId, transactId);
68     freeInstallParams.callback = nullptr;
69     bundleConnectAbilityMgr.SendCallBack(transactId, freeInstallParams);
70     bundleConnectAbilityMgr.SendRequestToServiceCenter(flags, targetAbilityInfo1, want,
71         userId, freeInstallParams);
72     return true;
73 }
74 }
75 
76 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     if (data == nullptr) {
81         return 0;
82     }
83 
84     if (size < OHOS::U32_AT_SIZE) {
85         return 0;
86     }
87 
88     char* ch = static_cast<char*>(malloc(size + 1));
89     if (ch == nullptr) {
90         return 0;
91     }
92 
93     (void)memset_s(ch, size + 1, 0x00, size + 1);
94     if (memcpy_s(ch, size, data, size) != EOK) {
95         free(ch);
96         ch = nullptr;
97         return 0;
98     }
99     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
100     free(ch);
101     ch = nullptr;
102     return 0;
103 }