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 "missionlistmanagerfourth_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "mission_info_mgr.h"
23 #include "mission_list_manager.h"
24 #undef private
25
26 #include "ability_record.h"
27
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 namespace {
33 constexpr size_t FOO_MAX_LEN = 1024;
34 constexpr size_t U32_AT_SIZE = 4;
35 constexpr uint8_t ENABLE = 2;
36 class MyAbilityConnection : public IAbilityConnection {
37 public:
38 MyAbilityConnection() = default;
39 virtual ~MyAbilityConnection() = default;
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)40 void OnAbilityConnectDone(const AppExecFwk::ElementName &element,
41 const sptr<IRemoteObject> &remoteObject,
42 int resultCode) override {}
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)43 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element,
44 int resultCode) override {}
AsObject()45 sptr<IRemoteObject> AsObject() override { return {}; }
46 };
47 } // namespace
48
GetU32Data(const char * ptr)49 uint32_t GetU32Data(const char *ptr)
50 {
51 // convert fuzz input data to an integer
52 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
53 }
54
GetFuzzAbilityRecord()55 std::shared_ptr<AbilityRecord> GetFuzzAbilityRecord()
56 {
57 sptr<Token> token = nullptr;
58 AbilityRequest abilityRequest;
59 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
60 abilityRequest.abilityInfo.name = "MainAbility";
61 abilityRequest.abilityInfo.type = AbilityType::DATA;
62 std::shared_ptr<AbilityRecord> abilityRecord =
63 AbilityRecord::CreateAbilityRecord(abilityRequest);
64 if (!abilityRecord) {
65 return nullptr;
66 }
67 return abilityRecord;
68 }
69
GetFuzzAbilityToken()70 sptr<Token> GetFuzzAbilityToken()
71 {
72 sptr<Token> token = nullptr;
73 std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
74 if (abilityRecord) {
75 token = abilityRecord->GetToken();
76 }
77 return token;
78 }
79
DoSomethingInterestingWithMyAPI(const char * data,size_t size)80 bool DoSomethingInterestingWithMyAPI(const char *data, size_t size)
81 {
82 bool boolParam = *data % ENABLE;
83 int intParam = static_cast<int>(GetU32Data(data));
84 Parcel wantParcel;
85 Want *want = nullptr;
86 if (wantParcel.WriteBuffer(data, size)) {
87 want = Want::Unmarshalling(wantParcel);
88 if (!want) {
89 return false;
90 }
91 }
92 std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
93 sptr<IRemoteObject> token = GetFuzzAbilityToken();
94 auto missionListManager = std::make_shared<MissionListManager>(intParam);
95 auto launcherList = std::make_shared<MissionList>(MissionListType::LAUNCHER);
96 missionListManager->launcherList_ = launcherList;
97 missionListManager->defaultStandardList_ =
98 std::make_shared<MissionList>(MissionListType::DEFAULT_STANDARD);
99 missionListManager->defaultSingleList_ =
100 std::make_shared<MissionList>(MissionListType::DEFAULT_SINGLE);
101 missionListManager->currentMissionLists_.push_front(launcherList);
102 if (!missionListManager->listenerController_) {
103 missionListManager->listenerController_ =
104 std::make_shared<MissionListenerController>();
105 }
106 DelayedSingleton<MissionInfoMgr>::GetInstance()->taskDataPersistenceMgr_ =
107 DelayedSingleton<TaskDataPersistenceMgr>::GetInstance();
108 std::shared_ptr<MissionList> missionList = std::make_shared<MissionList>();
109 missionListManager->TerminateAbilityLocked(abilityRecord, boolParam);
110 missionListManager->RemoveTerminatingAbility(abilityRecord, boolParam);
111 missionListManager->RemoveMissionList(missionList);
112 missionListManager->DispatchTerminate(abilityRecord);
113 missionListManager->DelayCompleteTerminate(abilityRecord);
114 missionListManager->CompleteTerminate(abilityRecord);
115 missionListManager->CompleteTerminateAndUpdateMission(abilityRecord);
116 missionListManager->GetAbilityFromTerminateList(token);
117 missionListManager->SetMissionLockedState(intParam, boolParam);
118 if (want) {
119 delete want;
120 want = nullptr;
121 }
122 return true;
123 }
124 } // namespace OHOS
125
126 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)127 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
128 {
129 /* Run your code on data */
130 if (data == nullptr) {
131 return 0;
132 }
133 /* Validate the length of size */
134 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
135 return 0;
136 }
137 char *ch = static_cast<char*>(malloc(size + 1));
138 if (ch == nullptr) {
139 std::cout << "malloc failed." << std::endl;
140 return 0;
141 }
142 (void)memset_s(ch, size + 1, 0x00, size + 1);
143 if (memcpy_s(ch, size, data, size) != EOK) {
144 std::cout << "copy failed." << std::endl;
145 free(ch);
146 ch = nullptr;
147 return 0;
148 }
149 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
150 free(ch);
151 ch = nullptr;
152 return 0;
153 }