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