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 "missionlistmanagerthird_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(
41 const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode) override
42 {}
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)43 void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override
44 {}
AsObject()45 sptr<IRemoteObject> AsObject() override
46 {
47 return {};
48 }
49 };
50 }
51
GetU32Data(const char * ptr)52 uint32_t GetU32Data(const char* ptr)
53 {
54 // convert fuzz input data to an integer
55 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
56 }
57
GetFuzzAbilityRecord()58 std::shared_ptr<AbilityRecord> GetFuzzAbilityRecord()
59 {
60 sptr<Token> token = nullptr;
61 AbilityRequest abilityRequest;
62 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
63 abilityRequest.abilityInfo.name = "MainAbility";
64 abilityRequest.abilityInfo.type = AbilityType::DATA;
65 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
66 if (!abilityRecord) {
67 return nullptr;
68 }
69 return abilityRecord;
70 }
71
GetFuzzAbilityToken()72 sptr<Token> GetFuzzAbilityToken()
73 {
74 sptr<Token> token = nullptr;
75 std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
76 if (abilityRecord) {
77 token = abilityRecord->GetToken();
78 }
79 return token;
80 }
81
DoSomethingInterestingWithMyAPI(const char * data,size_t size)82 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
83 {
84 bool boolParam = *data % ENABLE;
85 int intParam = static_cast<int>(GetU32Data(data));
86 Parcel wantParcel;
87 Want* want = nullptr;
88 if (wantParcel.WriteBuffer(data, size)) {
89 want = Want::Unmarshalling(wantParcel);
90 if (!want) {
91 return false;
92 }
93 }
94 std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
95 sptr<IRemoteObject> token = GetFuzzAbilityToken();
96 auto missionListManager = std::make_shared<MissionListManager>(intParam);
97 auto launcherList = std::make_shared<MissionList>(MissionListType::LAUNCHER);
98 missionListManager->launcherList_ = launcherList;
99 missionListManager->defaultStandardList_ = std::make_shared<MissionList>(MissionListType::DEFAULT_STANDARD);
100 missionListManager->defaultSingleList_ = std::make_shared<MissionList>(MissionListType::DEFAULT_SINGLE);
101 missionListManager->currentMissionLists_.push_front(launcherList);
102 if (!missionListManager->listenerController_) {
103 missionListManager->listenerController_ = std::make_shared<MissionListenerController>();
104 }
105 DelayedSingleton<MissionInfoMgr>::GetInstance()->taskDataPersistenceMgr_ =
106 DelayedSingleton<TaskDataPersistenceMgr>::GetInstance();
107 std::shared_ptr<MissionList> missionList = std::make_shared<MissionList>();
108 PacMap pacMap;
109 missionListManager->AbilityTransactionDone(token, intParam, pacMap);
110 missionListManager->DispatchState(abilityRecord, intParam);
111 missionListManager->DispatchForeground(abilityRecord, boolParam);
112 missionListManager->CompleteForegroundSuccess(abilityRecord);
113 missionListManager->TerminatePreviousAbility(abilityRecord);
114 missionListManager->DispatchBackground(abilityRecord);
115 missionListManager->CompleteBackground(abilityRecord);
116 missionListManager->TerminateAbility(abilityRecord, intParam, want, boolParam);
117 if (want) {
118 delete want;
119 want = nullptr;
120 }
121
122 return true;
123 }
124 }
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
134 /* Validate the length of size */
135 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
136 return 0;
137 }
138
139 char* ch = static_cast<char*>(malloc(size + 1));
140 if (ch == nullptr) {
141 std::cout << "malloc failed." << std::endl;
142 return 0;
143 }
144
145 (void)memset_s(ch, size + 1, 0x00, size + 1);
146 if (memcpy_s(ch, size, data, size) != EOK) {
147 std::cout << "copy failed." << std::endl;
148 free(ch);
149 ch = nullptr;
150 return 0;
151 }
152
153 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
154 free(ch);
155 ch = nullptr;
156 return 0;
157 }