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"
DoSomething2(const char * data,size_t size)24 void DoSomething2(const char* data, size_t size){};
25 using Want = OHOS::AAFwk::Want;
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
28 constexpr size_t FOO_MAX_LEN = 1024;
29 constexpr size_t U32_AT_SIZE = 4;
30 constexpr size_t MESSAGE_SIZE = 4;
31 constexpr size_t DCAMERA_SHIFT_24 = 24;
32 constexpr size_t DCAMERA_SHIFT_16 = 16;
33 constexpr size_t DCAMERA_SHIFT_8 = 8;
34
35
GetU32Data(const char * ptr)36 uint32_t GetU32Data(const char* ptr)
37 {
38 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
39 }
40
DoSomethingInterestingWithMyAPI(const char * data,size_t size)41 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
42 {
43 BundleConnectAbilityMgr bundleConnectAbilityMgr;
44 Want want;
45 int32_t userId = reinterpret_cast<uintptr_t>(data);
46 int32_t flags = reinterpret_cast<uintptr_t>(data);
47 AbilityInfo abilityInfo;
48 sptr<IRemoteObject> callBack = nullptr;
49 bundleConnectAbilityMgr.QueryAbilityInfo(want, flags, userId, abilityInfo, callBack);
50 bundleConnectAbilityMgr.SilentInstall(want, userId, callBack);
51 bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
52 bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
53 bundleConnectAbilityMgr.UpgradeAtomicService(want, userId);
54 std::string installResult(data, size);
55 bundleConnectAbilityMgr.OnServiceCenterCall(installResult);
56 bundleConnectAbilityMgr.OnDelayedHeartbeat(installResult);
57 bundleConnectAbilityMgr.DeathRecipientSendCallback();
58 sptr<IRemoteObject> callerToken = nullptr;
59 bundleConnectAbilityMgr.ConnectAbility(want, callerToken);
60 bundleConnectAbilityMgr.ProcessPreload(want);
61 bundleConnectAbilityMgr.DisconnectAbility();
62 int32_t code = reinterpret_cast<uintptr_t>(data);
63 MessageParcel datas;
64 datas.WriteBuffer(data, size);
65 datas.RewindRead(0);
66 MessageParcel reply;
67 bundleConnectAbilityMgr.SendRequest(code, datas, reply);
68 TargetAbilityInfo targetAbilityInfo1;
69 FreeInstallParams freeInstallParams;
70 bundleConnectAbilityMgr.SilentInstall(targetAbilityInfo1, want, freeInstallParams, userId);
71 bundleConnectAbilityMgr.UpgradeCheck(targetAbilityInfo1, want, freeInstallParams, userId);
72 int32_t callingUid = reinterpret_cast<uintptr_t>(data);
73 std::vector<std::string> bundleNames = { std::string(data, size) };
74 std::vector<std::string> callingAppIds = { std::string(data, size) };
75 bundleConnectAbilityMgr.GetCallingInfo(userId, callingUid, bundleNames, callingAppIds);
76 InnerBundleInfo innerBundleInfo;
77 sptr<TargetAbilityInfo> targetAbilityInfo = nullptr;
78 bundleConnectAbilityMgr.GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
79 bundleConnectAbilityMgr.CheckIsModuleNeedUpdate(innerBundleInfo, want, userId, callBack);
80 int32_t resultCode = reinterpret_cast<uintptr_t>(data);
81 std::string transactId(data, size);
82 bundleConnectAbilityMgr.SendCallBack(resultCode, want, userId, transactId);
83 freeInstallParams.callback = nullptr;
84 bundleConnectAbilityMgr.SendCallBack(transactId, freeInstallParams);
85 bundleConnectAbilityMgr.SendRequestToServiceCenter(flags, targetAbilityInfo1, want,
86 userId, freeInstallParams);
87 DoSomething2(data, size);
88 return true;
89 }
DoSomething2(const char * data,size_t size)90 void DoSomething2(const char* data, size_t size)
91 {
92 BundleConnectAbilityMgr bundleConnectAbilityMgr;
93 OHOS::sptr<OHOS::IRemoteObject> callBack = nullptr;
94 Want want;
95 int32_t userId = reinterpret_cast<uintptr_t>(data);
96 int32_t flags = reinterpret_cast<uintptr_t>(data);
97 TargetAbilityInfo targetAbilityInfo;
98 FreeInstallParams freeInstallParams;
99 freeInstallParams.callback = nullptr;
100 bundleConnectAbilityMgr.SendRequest(flags, targetAbilityInfo, want, userId,
101 freeInstallParams);
102 std::string transactId(data, size);
103 bundleConnectAbilityMgr.GetAbilityManagerServiceCallBack(transactId);
104 bundleConnectAbilityMgr.OutTimeMonitor(transactId);
105 bundleConnectAbilityMgr.GetTransactId();
106 int32_t resultCode = reinterpret_cast<uintptr_t>(data);
107 bundleConnectAbilityMgr.CallAbilityManager(resultCode, want, userId, callBack);
108 AbilityInfo abilityInfo;
109 InnerBundleInfo innerBundleInfo;
110 bundleConnectAbilityMgr.IsObtainAbilityInfo(want, flags, userId, abilityInfo,
111 callBack, innerBundleInfo);
112 bundleConnectAbilityMgr.GetAbilityMgrProxy();
113 std::mutex mutex_;
114 std::unique_lock<std::mutex> lock(mutex_);
115 bundleConnectAbilityMgr.WaitFromConnecting(lock);
116 bundleConnectAbilityMgr.WaitFromConnected(lock);
117 bundleConnectAbilityMgr.DisconnectDelay();
118 bundleConnectAbilityMgr.PreloadRequest(flags, targetAbilityInfo);
119 bundleConnectAbilityMgr.ProcessPreloadCheck(targetAbilityInfo);
120 ErmsCallerInfo callerInfo;
121 bundleConnectAbilityMgr.GetEcologicalCallerInfo(want, callerInfo, userId);
122 bundleConnectAbilityMgr.GetPreloadFlag();
123 std::string moduleName(data, size);
124 std::string bundleName(data, size);
125 OHOS::sptr<TargetAbilityInfo> targetAbilityInfo1 = nullptr;
126 bundleConnectAbilityMgr.GetPreloadList(bundleName, moduleName, userId, targetAbilityInfo1);
127 bundleConnectAbilityMgr.LoadDownloadService();
128 BmsExperienceRule rule;
129 bundleConnectAbilityMgr.CheckEcologicalRule(want, callerInfo, rule);
130 bundleConnectAbilityMgr.CheckIsOnDemandLoad(targetAbilityInfo);
131 bundleConnectAbilityMgr.GetModuleName(innerBundleInfo, want, moduleName);
132 bundleConnectAbilityMgr.CheckIsModuleNeedUpdateWrap(innerBundleInfo, want, userId, callBack);
133 bundleConnectAbilityMgr.CheckSubPackageName(innerBundleInfo, want);
134 }
135 }
136
137 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)138 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
139 {
140 /* Run your code on data */
141 if (data == nullptr) {
142 return 0;
143 }
144
145 if (size < OHOS::U32_AT_SIZE) {
146 return 0;
147 }
148
149 /* Validate the length of size */
150 if (size > OHOS::FOO_MAX_LEN) {
151 return 0;
152 }
153
154 char* ch = static_cast<char*>(malloc(size + 1));
155 if (ch == nullptr) {
156 return 0;
157 }
158
159 (void)memset_s(ch, size + 1, 0x00, size + 1);
160 if (memcpy_s(ch, size, data, size) != EOK) {
161 free(ch);
162 ch = nullptr;
163 return 0;
164 }
165 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
166 free(ch);
167 ch = nullptr;
168 return 0;
169 }