• 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 "apprunningrecordfirst_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "app_running_record.h"
23 #undef private
24 
25 #include "ability_record.h"
26 #include "app_running_manager.h"
27 #include "parcel.h"
28 #include "securec.h"
29 #include "want.h"
30 
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33 
34 namespace OHOS {
35 namespace {
36 constexpr size_t FOO_MAX_LEN = 1024;
37 constexpr size_t U32_AT_SIZE = 4;
38 constexpr uint8_t ENABLE = 2;
39 }
GetU32Data(const char * ptr)40 uint32_t GetU32Data(const char* ptr)
41 {
42     // convert fuzz input data to an integer
43     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
44 }
GetFuzzAbilityToken()45 sptr<Token> GetFuzzAbilityToken()
46 {
47     sptr<Token> token = nullptr;
48     AbilityRequest abilityRequest;
49     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
50     abilityRequest.abilityInfo.name = "MainAbility";
51     abilityRequest.abilityInfo.type = AbilityType::PAGE;
52     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
53     if (abilityRecord) {
54         token = abilityRecord->GetToken();
55     }
56     return token;
57 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)58 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
59 {
60     pid_t hostPid = static_cast<pid_t>(GetU32Data(data));
61     std::string renderParam(data, size);
62     int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
63     int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
64     int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
65     std::shared_ptr<AppRunningRecord> host;
66     RenderRecord *renderRecord =
67         new RenderRecord(hostPid, renderParam, ipcFd, sharedFd, crashFd, host);
68     renderRecord->RegisterDeathRecipient();
69     renderRecord->CreateRenderRecord(hostPid, renderParam, ipcFd, sharedFd,
70                                      crashFd, host);
71     pid_t pid = static_cast<pid_t>(GetU32Data(data));
72     renderRecord->SetPid(pid);
73     sptr<IRenderScheduler> scheduler;
74     renderRecord->SetScheduler(scheduler);
75     sptr<AppDeathRecipient> recipient;
76     renderRecord->SetDeathRecipient(recipient);
77     renderRecord->SetHostUid(hostPid);
78     std::string hostBundleName(data, size);
79     renderRecord->SetHostBundleName(hostBundleName);
80     renderRecord->GetPid();
81     renderRecord->GetHostPid();
82     renderRecord->GetHostUid();
83     renderRecord->GetHostBundleName();
84     renderRecord->GetRenderParam();
85     renderRecord->GetIpcFd();
86     renderRecord->GetSharedFd();
87     renderRecord->GetCrashFd();
88     renderRecord->GetHostRecord();
89     renderRecord->GetScheduler();
90     std::shared_ptr<ApplicationInfo> appInfo;
91     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
92     std::string processName(data, size);
93     AppRunningRecord* appRecord = new AppRunningRecord(appInfo, recordId, processName);
94     appRecord->appLifeCycleDeal_ = std::make_shared<AppLifeCycleDeal>();
95     sptr<IAppScheduler> thread;
96     appRecord->SetApplicationClient(thread);
97     std::string signCode(data, size);
98     appRecord->SetSignCode(signCode);
99     std::string jointUserId(data, size);
100     appRecord->SetJointUserId(jointUserId);
101     int32_t uid = static_cast<int32_t>(GetU32Data(data));
102     appRecord->SetUid(uid);
103     ApplicationState state = ApplicationState::APP_STATE_CREATE;
104     appRecord->SetState(state);
105     std::weak_ptr<AppMgrServiceInner> inner;
106     appRecord->SetAppMgrServiceInner(inner);
107     std::shared_ptr<AMSEventHandler> handler;
108     appRecord->SetEventHandler(handler);
109     bool isKeepAlive = *data % ENABLE;
110     bool isEmptyKeepAliveApp = *data % ENABLE;
111     appRecord->SetKeepAliveAppState(isKeepAlive, isEmptyKeepAliveApp);
112     bool isStageBasedModel = *data % ENABLE;
113     appRecord->SetStageModelState(isStageBasedModel);
114     int count = static_cast<int>(GetU32Data(data));
115     appRecord->SetRestartResidentProcCount(count);
116     std::shared_ptr<UserTestRecord> testRecord;
117     appRecord->SetUserTestInfo(testRecord);
118     std::shared_ptr<RenderRecord> record;
119     appRecord->AddRenderRecord(record);
120     AppSpawnStartMsg appMsg;
121     appRecord->SetStartMsg(appMsg);
122     bool isDebugApp = *data % ENABLE;
123     appRecord->SetDebugApp(isDebugApp);
124     int32_t appIndex = static_cast<int32_t>(GetU32Data(data));
125     appRecord->SetAppIndex(appIndex);
126     bool securityFlag = *data % ENABLE;
127     appRecord->SetSecurityFlag(securityFlag);
128     appRecord->SetTerminating();
129     appRecord->SetKilling();
130     appRecord->GetSignCode();
131     appRecord->GetJointUserId();
132     appRecord->GetUid();
133     appRecord->GetState();
134     appRecord->GetApplicationClient();
135     appRecord->IsTerminating();
136     appRecord->IsKeepAliveApp();
137     appRecord->IsEmptyKeepAliveApp();
138     appRecord->GetRestartResidentProcCount();
139     appRecord->GetUserTestInfo();
140     appRecord->GetRenderRecordMap();
141     appRecord->GetStartMsg();
142     appRecord->GetAppIndex();
143     appRecord->GetSecurityFlag();
144     return appRecord->IsKilling();
145 }
146 }
147 
148 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)149 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
150 {
151     /* Run your code on data */
152     if (data == nullptr) {
153         std::cout << "invalid data" << std::endl;
154         return 0;
155     }
156 
157     /* Validate the length of size */
158     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
159         return 0;
160     }
161 
162     char* ch = (char*)malloc(size + 1);
163     if (ch == nullptr) {
164         std::cout << "malloc failed." << std::endl;
165         return 0;
166     }
167 
168     (void)memset_s(ch, size + 1, 0x00, size + 1);
169     if (memcpy_s(ch, size, data, size) != EOK) {
170         std::cout << "copy failed." << std::endl;
171         free(ch);
172         ch = nullptr;
173         return 0;
174     }
175 
176     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
177     free(ch);
178     ch = nullptr;
179     return 0;
180 }
181