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 "cacheprocessmanagerb_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->GetCurrentCachedProcNum(); // called.
72 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
73 std::shared_ptr<AppRunningRecord> appRecord1 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
74 std::shared_ptr<AppRunningRecord> appRecord2 = std::make_shared<AppRunningRecord>(nullptr, int32Param, stringParam);
75 mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
76 mgr->RemoveCacheRecord(appRecord1); // called branch cached.
77 mgr->RemoveCacheRecord(appRecord2); // called branch not cached.
78 mgr->cachedAppRecordQueue_.clear();
79
80 mgr->maxProcCacheNum_ = 0;
81 mgr->ShrinkAndKillCache(); // called branch
82 mgr->maxProcCacheNum_ = 1; // 1 means maxProcCacheNum
83 mgr->ShrinkAndKillCache(); // called branch current < maxProcCacheNum.
84 mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
85 mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
86 mgr->ShrinkAndKillCache(); // called branch current > maxProcCacheNum.
87 mgr->cachedAppRecordQueue_.clear();
88
89 mgr->SetAppMgr(serviceInner1);
90 mgr->KillProcessByRecord(nullptr); // called branch appMgr is nullptr.
91 mgr->KillProcessByRecord(appRecord2); // called branch appRecord not null.
92 std::shared_ptr<AppMgrServiceInner> serviceInner2 = std::make_shared<AppMgrServiceInner>();
93 mgr->SetAppMgr(serviceInner2);
94 mgr->KillProcessByRecord(appRecord2); // called branch appRecord not null.
95
96 mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
97 mgr->cachedAppRecordQueue_.emplace_back(appRecord2);
98 mgr->PrintCacheQueue(); // called branch apprecord exist,
99 mgr->cachedAppRecordQueue_.clear();
100 mgr->PrintCacheQueue(); // called branch no apprecord.
101
102 mgr->AddToApplicationSet(nullptr); // called nullptr.
103 mgr->AddToApplicationSet(appRecord2); // called.
104 mgr->RemoveFromApplicationSet(nullptr); // called nullptr.
105 mgr->RemoveFromApplicationSet(appRecord2); // called.
106 }
107
CacheProcessManagerFuzztestFunc2(bool boolParam,std::string & stringParam,int32_t int32Param)108 void CacheProcessManagerFuzztestFunc2(bool boolParam, std::string &stringParam, int32_t int32Param)
109 {
110 std::shared_ptr<CacheProcessManager> mgr = std::make_shared<CacheProcessManager>();
111 std::shared_ptr<AppMgrServiceInner> serviceInner1;
112 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
113 std::shared_ptr<AppRunningRecord> appRecord1 = std::make_shared<AppRunningRecord>(appInfo, int32Param, stringParam);
114 std::shared_ptr<AppRunningRecord> appRecord2 = std::make_shared<AppRunningRecord>(nullptr, int32Param, stringParam);
115
116 mgr->maxProcCacheNum_ = 0;
117 mgr->PrepareActivateCache(nullptr); // branch 0 maxProcCacheNum_
118 mgr->maxProcCacheNum_ = 1; // 1 means maxProcCacheNum.
119 mgr->PrepareActivateCache(nullptr); // branch 0 null apprecord
120 mgr->PrepareActivateCache(appRecord1);
121
122 mgr->SetAppMgr(serviceInner1);
123 mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
124 mgr->PrepareActivateCache(appRecord1); // // branch cached & appMgr null.
125 std::shared_ptr<AppMgrServiceInner> serviceInner2 = std::make_shared<AppMgrServiceInner>();
126 mgr->SetAppMgr(serviceInner2);
127 mgr->cachedAppRecordQueue_.emplace_back(appRecord1);
128 mgr->PrepareActivateCache(appRecord1); // branch cached & appMgr not null.
129 }
130
DoSomethingInterestingWithMyAPI(const char * data,size_t size)131 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
132 {
133 bool boolParam = *data % ENABLE;
134 std::string stringParam(data, size);
135 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
136 CacheProcessManagerFuzztestFunc1(boolParam, stringParam, int32Param);
137 CacheProcessManagerFuzztestFunc2(boolParam, stringParam, int32Param);
138 return true;
139 }
140 }
141
142 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)143 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
144 {
145 /* Run your code on data */
146 if (data == nullptr) {
147 return 0;
148 }
149
150 /* Validate the length of size */
151 if (size < OHOS::U32_AT_SIZE) {
152 return 0;
153 }
154
155 char* ch = static_cast<char*>(malloc(size + 1));
156 if (ch == nullptr) {
157 std::cout << "malloc failed." << std::endl;
158 return 0;
159 }
160
161 (void)memset_s(ch, size + 1, 0x00, size + 1);
162 if (memcpy_s(ch, size, data, size) != EOK) {
163 std::cout << "copy failed." << std::endl;
164 free(ch);
165 ch = nullptr;
166 return 0;
167 }
168
169 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
170 free(ch);
171 ch = nullptr;
172 return 0;
173 }
174
175