• 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 "cacheprocessmanagera_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "cache_process_manager.h"
24 #include "ability_record.h"
25 #undef protected
26 #undef private
27 
28 #include "app_mgr_service_inner.h"
29 
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 using namespace OHOS::AbilityRuntime;
33 
34 namespace OHOS {
35 namespace {
36 constexpr int INPUT_ZERO = 0;
37 constexpr int INPUT_ONE = 1;
38 constexpr int INPUT_THREE = 3;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr uint8_t ENABLE = 2;
41 constexpr size_t OFFSET_ZERO = 24;
42 constexpr size_t OFFSET_ONE = 16;
43 constexpr size_t OFFSET_TWO = 8;
44 }
45 
GetU32Data(const char * ptr)46 uint32_t GetU32Data(const char* ptr)
47 {
48     // convert fuzz input data to an integer
49     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
50         ptr[INPUT_THREE];
51 }
52 
GetFuzzAbilityToken()53 sptr<Token> GetFuzzAbilityToken()
54 {
55     sptr<Token> token = nullptr;
56     AbilityRequest abilityRequest;
57     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
58     abilityRequest.abilityInfo.name = "MainAbility";
59     abilityRequest.abilityInfo.type = AbilityType::DATA;
60     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
61     if (abilityRecord) {
62         token = abilityRecord->GetToken();
63     }
64     return token;
65 }
66 
CacheProcessManagerFuzztestFunc1(bool boolParam,std::string & stringParam,int32_t int32Param)67 void CacheProcessManagerFuzztestFunc1(bool boolParam, std::string &stringParam, int32_t int32Param)
68 {
69     std::shared_ptr<CacheProcessManager> mgr = std::make_shared<CacheProcessManager>();
70     std::shared_ptr<AppMgrServiceInner> serviceInner1;
71     mgr->SetAppMgr(serviceInner1); // null mgr
72     mgr->RefreshCacheNum(); // called.
73     mgr->QueryEnableProcessCache(); // called.
74     mgr->maxProcCacheNum_ = 0;
75     mgr->PenddingCacheProcess(nullptr); // called.
76     mgr->maxProcCacheNum_ = int32Param;
77 
78     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
79     std::shared_ptr<AppRunningRecord> appRecord1 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
80     mgr->PenddingCacheProcess(nullptr); // nullptr
81     appRecord1->isKeepAliveRdb_ = true;
82     appRecord1->isKeepAliveBundle_ = true;
83     appRecord1->isSingleton_ = true;
84     appRecord1->isMainProcess_ = true;
85     mgr->PenddingCacheProcess(appRecord1); // keepalive
86     std::shared_ptr<AppRunningRecord> appRecord2 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
87     mgr->PenddingCacheProcess(appRecord2); // not alive
88 
89     mgr->maxProcCacheNum_ = 0;
90     mgr->CheckAndCacheProcess(nullptr); // nullptr
91     mgr->maxProcCacheNum_ = int32Param;
92     mgr->CheckAndCacheProcess(appRecord2); // not cached
93     mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
94     mgr->CheckAndCacheProcess(appRecord2); // cached
95 
96     mgr->CheckAndNotifyCachedState(nullptr);
97     std::shared_ptr<AppMgrServiceInner> serviceInner = std::make_shared<AppMgrServiceInner>();
98     mgr->SetAppMgr(serviceInner);
99     mgr->CheckAndNotifyCachedState(appRecord2); // appMgr not null
100 }
101 
CacheProcessManagerFuzztestFunc2(bool boolParam,std::string & stringParam,int32_t int32Param)102 void CacheProcessManagerFuzztestFunc2(bool boolParam, std::string &stringParam, int32_t int32Param)
103 {
104     std::shared_ptr<CacheProcessManager> mgr = std::make_shared<CacheProcessManager>();
105     std::shared_ptr<AppMgrServiceInner> serviceInner1;
106     mgr->SetAppMgr(serviceInner1);
107     mgr->IsCachedProcess(nullptr); // called.
108     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
109     std::shared_ptr<AppRunningRecord> appRecord2 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
110     mgr->IsCachedProcess(appRecord2); // not cached called.
111     mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
112     mgr->IsCachedProcess(appRecord2); // cached called.
113     mgr->cachedAppRecordQueue_.clear(); // clear
114 
115     mgr->maxProcCacheNum_ = int32Param;
116     mgr->OnProcessKilled(nullptr); // nullptr called.
117     mgr->OnProcessKilled(appRecord2); // not cached called.
118     mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
119     mgr->OnProcessKilled(appRecord2); // cached called.
120     mgr->cachedAppRecordQueue_.clear(); // clear
121 
122     mgr->maxProcCacheNum_ = int32Param;
123     mgr->ReuseCachedProcess(nullptr);
124     mgr->ReuseCachedProcess(appRecord2); // not cached
125     mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
126     mgr->ReuseCachedProcess(appRecord2); // cached
127     mgr->cachedAppRecordQueue_.clear(); // clear
128 
129     std::shared_ptr<AppMgrServiceInner> serviceInner = std::make_shared<AppMgrServiceInner>();
130     mgr->SetAppMgr(serviceInner); // appInner not null
131     mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
132     mgr->ReuseCachedProcess(appRecord2); // cached
133 
134     mgr->IsAppSupportProcessCache(nullptr); // null ptr check
135     std::shared_ptr<AppRunningRecord> appRecord3 = std::make_shared<AppRunningRecord>(nullptr, int32Param, stringParam);
136     mgr->IsAppSupportProcessCache(appRecord3); // null appInfo
137     mgr->srvExtRecords.emplace(appRecord2);
138     mgr->IsAppSupportProcessCache(appRecord2); // appInfo not null
139     mgr->srvExtRecords.clear();
140 
141     appRecord2->SetAttachedToStatusBar(true);
142     mgr->IsAppSupportProcessCache(appRecord2); // appInfo not null &attached true
143     appRecord2->SetAttachedToStatusBar(false);
144     mgr->IsAppSupportProcessCache(appRecord2); // appInfo not null &attached false
145     appRecord2->isKeepAliveRdb_ = true;
146     appRecord2->isKeepAliveBundle_ = true;
147     appRecord2->isSingleton_ = true;
148     appRecord2->isMainProcess_ = true;
149     mgr->IsAppSupportProcessCache(appRecord2); // appInfo not null &attached false & keepalive called
150 }
151 
CacheProcessManagerFuzztestFunc3(bool boolParam,std::string & stringParam,int32_t int32Param)152 void CacheProcessManagerFuzztestFunc3(bool boolParam, std::string &stringParam, int32_t int32Param)
153 {
154     std::shared_ptr<CacheProcessManager> mgr = std::make_shared<CacheProcessManager>();
155     std::shared_ptr<AppMgrServiceInner> serviceInner1;
156     mgr->SetAppMgr(serviceInner1);
157     mgr->IsAppSupportProcessCacheInnerFirst(nullptr); // nullptr
158     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
159     std::shared_ptr<AppRunningRecord> appRecord1 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
160     mgr->shouldCheckSupport = true;
161     appRecord1->procCacheSupportState_ = SupportProcessCacheState::UNSPECIFIED;
162     mgr->IsAppSupportProcessCacheInnerFirst(appRecord1); // case CacheState UNSPECIFIED
163     appRecord1->procCacheSupportState_ = SupportProcessCacheState::SUPPORT;
164     mgr->IsAppSupportProcessCacheInnerFirst(appRecord1); // case CacheState SUPPORT
165     appRecord1->procCacheSupportState_ = SupportProcessCacheState::NOT_SUPPORT;
166     mgr->IsAppSupportProcessCacheInnerFirst(appRecord1); // case CacheState SUPPORT
167     int32_t num = static_cast<int32_t>(SupportProcessCacheState::NOT_SUPPORT) + 1;
168     appRecord1->procCacheSupportState_ = static_cast<SupportProcessCacheState>(num);
169     mgr->IsAppSupportProcessCacheInnerFirst(appRecord1); // case CacheState default branch
170 
171     mgr->shouldCheckSupport = false;
172     appRecord1->procCacheSupportState_ = SupportProcessCacheState::UNSPECIFIED;
173     mgr->IsAppSupportProcessCacheInnerFirst(appRecord1); // case CacheState UNSPECIFIED
174 
175     mgr->IsAppShouldCache(nullptr); // called.
176     mgr->maxProcCacheNum_ = int32Param;
177     mgr->IsAppShouldCache(appRecord1); // not ccached called.
178     mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
179     mgr->IsAppShouldCache(appRecord1); //ccached called.
180 
181     mgr->IsAppAbilitiesEmpty(nullptr); // called
182     mgr->IsAppAbilitiesEmpty(appRecord1); // called
183 }
184 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)185 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
186 {
187     bool boolParam = *data % ENABLE;
188     std::string stringParam(data, size);
189     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
190     CacheProcessManagerFuzztestFunc1(boolParam, stringParam, int32Param);
191     CacheProcessManagerFuzztestFunc2(boolParam, stringParam, int32Param);
192     CacheProcessManagerFuzztestFunc3(boolParam, stringParam, int32Param);
193     return true;
194 }
195 }
196 
197 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)198 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
199 {
200     /* Run your code on data */
201     if (data == nullptr) {
202         return 0;
203     }
204 
205     /* Validate the length of size */
206     if (size < OHOS::U32_AT_SIZE) {
207         return 0;
208     }
209 
210     char* ch = static_cast<char*>(malloc(size + 1));
211     if (ch == nullptr) {
212         std::cout << "malloc failed." << std::endl;
213         return 0;
214     }
215 
216     (void)memset_s(ch, size + 1, 0x00, size + 1);
217     if (memcpy_s(ch, size, data, size) != EOK) {
218         std::cout << "copy failed." << std::endl;
219         free(ch);
220         ch = nullptr;
221         return 0;
222     }
223 
224     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
225     free(ch);
226     ch = nullptr;
227     return 0;
228 }
229 
230