• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "abilitymgrappexitreasonhelper_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #include "app_exit_reason_helper.h"
24 #include "ability_manager_service.h"
25 #undef private
26 
27 #include "ability_fuzz_util.h"
28 #include "ability_record.h"
29 #include "securec.h"
30 
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33 
34 namespace OHOS {
35 namespace {
36 constexpr size_t STRING_MAX_LENGTH = 128;
37 }
38 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
40 {
41     std::shared_ptr<SubManagersHelper> subManagersHelper;
42     std::shared_ptr<AppExitReasonHelper> infos = std::make_shared<AppExitReasonHelper>(subManagersHelper);
43     ExitReason exitReason;
44     RunningProcessInfo processInfo;
45     std::vector<std::string> abilities;
46     std::vector<std::string> abilityLists;
47     std::vector<std::string> abilityList;
48     int32_t pid;
49     std::string bundleName;
50     std::string abilityName;
51     int32_t uid;
52     int32_t appIndex;
53     uint32_t accessTokenId;
54     int32_t userId;
55     bool withKillMsg;
56     bool fromKillWithReason;
57     FuzzedDataProvider fdp(data, size);
58     pid = fdp.ConsumeIntegral<int32_t>();
59     fromKillWithReason = fdp.ConsumeBool();
60     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
61     abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
62     uid = fdp.ConsumeIntegral<int32_t>();
63     appIndex = fdp.ConsumeIntegral<int32_t>();
64     accessTokenId = fdp.ConsumeIntegral<int32_t>();
65     userId = fdp.ConsumeIntegral<int32_t>();
66     withKillMsg = fdp.ConsumeBool();
67     abilities = AbilityFuzzUtil::GenerateStringArray(fdp);
68     abilityLists = AbilityFuzzUtil::GenerateStringArray(fdp);
69     abilityList = AbilityFuzzUtil::GenerateStringArray(fdp);
70     infos->RecordAppExitReason(exitReason);
71     infos->RecordProcessExitReason(pid, exitReason, fromKillWithReason);
72     infos->RecordAppExitReason(bundleName, uid, appIndex, exitReason);
73     infos->RecordProcessExitReason(pid, uid, exitReason);
74     infos->RecordProcessExitReason(pid, bundleName, uid, accessTokenId,
75         exitReason, processInfo, fromKillWithReason);
76     infos->RecordProcessExtensionExitReason(pid, bundleName, exitReason, processInfo, withKillMsg);
77     infos->GetActiveAbilityList(uid, abilityLists, pid);
78     infos->GetActiveAbilityListFromUIAbilityManager(uid, abilityLists, pid);
79     infos->IsExitReasonValid(exitReason);
80     infos->GetActiveAbilityListWithPid(uid, abilityList, pid);
81     infos->RecordUIAbilityExitReason(pid, abilityName, exitReason);
82     infos->GetRunningProcessInfo(pid, userId, bundleName, processInfo);
83     return true;
84 }
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90     // Run your code on data.
91     OHOS::DoSomethingInterestingWithMyAPI(data, size);
92     return 0;
93 }
94