• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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(
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     missionListManager->TerminateAbilityLocked(abilityRecord, boolParam);
109     missionListManager->RemoveTerminatingAbility(abilityRecord, boolParam);
110     missionListManager->RemoveMissionList(missionList);
111     missionListManager->DispatchTerminate(abilityRecord);
112     missionListManager->DelayCompleteTerminate(abilityRecord);
113     missionListManager->CompleteTerminate(abilityRecord);
114     missionListManager->CompleteTerminateAndUpdateMission(abilityRecord);
115     missionListManager->GetAbilityFromTerminateList(token);
116     missionListManager->SetMissionLockedState(intParam, boolParam);
117     return true;
118 }
119 }
120 
121 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)122 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
123 {
124     /* Run your code on data */
125     if (data == nullptr) {
126         return 0;
127     }
128 
129     /* Validate the length of size */
130     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
131         return 0;
132     }
133 
134     char* ch = (char *)malloc(size + 1);
135     if (ch == nullptr) {
136         std::cout << "malloc failed." << std::endl;
137         return 0;
138     }
139 
140     (void)memset_s(ch, size + 1, 0x00, size + 1);
141     if (memcpy_s(ch, size, data, size) != EOK) {
142         std::cout << "copy failed." << std::endl;
143         free(ch);
144         ch = nullptr;
145         return 0;
146     }
147 
148     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
149     free(ch);
150     ch = nullptr;
151     return 0;
152 }
153 
154