1 /*
2 * Copyright (c) 2025 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 "bmscheckissystemappbyuid_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 #include <fuzzer/FuzzedDataProvider.h>
22
23 #include "bundle_mgr_interface.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 #include "bms_fuzztest_util.h"
28
29 using namespace OHOS::AppExecFwk;
30 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
31
32 namespace OHOS {
GetBundleMgr()33 sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr()
34 {
35 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36 if (systemAbilityManager == nullptr) {
37 std::cout << "GetBundleMgr GetSystemAbilityManager is null";
38 return nullptr;
39 }
40
41 auto bundleMgrSa = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
42 if (bundleMgrSa == nullptr) {
43 std::cout << "[fuzz] GetBundleMgr GetSystemAbility is null";
44 return nullptr;
45 }
46
47 return iface_cast<IBundleMgr>(bundleMgrSa);
48 }
49
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)50 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
51 {
52 auto bundleMgr = GetBundleMgr();
53 if (bundleMgr == nullptr) {
54 return false;
55 }
56 FuzzedDataProvider fdp(data, size);
57 int uid = fdp.ConsumeIntegral<int>();
58
59 return bundleMgr->CheckIsSystemAppByUid(uid);
60 }
61 }
62
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66 /* Run your code on data */
67 OHOS::DoSomethingInterestingWithMyAPI(data, size);
68 return 0;
69 }
70