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 std::shared_ptr<AppRunningRecord> host;
65 RenderRecord* renderRecord = new RenderRecord(hostPid, renderParam, ipcFd, sharedFd, host);
66 renderRecord->RegisterDeathRecipient();
67 renderRecord->CreateRenderRecord(hostPid, renderParam, ipcFd, sharedFd, host);
68 pid_t pid = static_cast<pid_t>(GetU32Data(data));
69 renderRecord->SetPid(pid);
70 sptr<IRenderScheduler> scheduler;
71 renderRecord->SetScheduler(scheduler);
72 sptr<AppDeathRecipient> recipient;
73 renderRecord->SetDeathRecipient(recipient);
74 renderRecord->SetHostUid(hostPid);
75 std::string hostBundleName(data, size);
76 renderRecord->SetHostBundleName(hostBundleName);
77 renderRecord->GetPid();
78 renderRecord->GetHostPid();
79 renderRecord->GetHostUid();
80 renderRecord->GetHostBundleName();
81 renderRecord->GetRenderParam();
82 renderRecord->GetIpcFd();
83 renderRecord->GetSharedFd();
84 renderRecord->GetHostRecord();
85 renderRecord->GetScheduler();
86 std::shared_ptr<ApplicationInfo> appInfo;
87 int32_t recordId = static_cast<int32_t>(GetU32Data(data));
88 std::string processName(data, size);
89 AppRunningRecord* appRecord = new AppRunningRecord(appInfo, recordId, processName);
90 appRecord->appLifeCycleDeal_ = std::make_shared<AppLifeCycleDeal>();
91 sptr<IAppScheduler> thread;
92 appRecord->SetApplicationClient(thread);
93 std::string signCode(data, size);
94 appRecord->SetSignCode(signCode);
95 std::string jointUserId(data, size);
96 appRecord->SetJointUserId(jointUserId);
97 int32_t uid = static_cast<int32_t>(GetU32Data(data));
98 appRecord->SetUid(uid);
99 ApplicationState state = ApplicationState::APP_STATE_BEGIN;
100 appRecord->SetState(state);
101 std::weak_ptr<AppMgrServiceInner> inner;
102 appRecord->SetAppMgrServiceInner(inner);
103 std::shared_ptr<AMSEventHandler> handler;
104 appRecord->SetEventHandler(handler);
105 bool isKeepAlive = *data % ENABLE;
106 bool isEmptyKeepAliveApp = *data % ENABLE;
107 appRecord->SetKeepAliveAppState(isKeepAlive, isEmptyKeepAliveApp);
108 bool isStageBasedModel = *data % ENABLE;
109 appRecord->SetStageModelState(isStageBasedModel);
110 int count = static_cast<int>(GetU32Data(data));
111 appRecord->SetRestartResidentProcCount(count);
112 std::shared_ptr<UserTestRecord> testRecord;
113 appRecord->SetUserTestInfo(testRecord);
114 std::shared_ptr<RenderRecord> record;
115 appRecord->SetRenderRecord(record);
116 AppSpawnStartMsg appMsg;
117 appRecord->SetStartMsg(appMsg);
118 bool isDebugApp = *data % ENABLE;
119 appRecord->SetDebugApp(isDebugApp);
120 int32_t appIndex = static_cast<int32_t>(GetU32Data(data));
121 appRecord->SetAppIndex(appIndex);
122 bool securityFlag = *data % ENABLE;
123 appRecord->SetSecurityFlag(securityFlag);
124 appRecord->SetTerminating();
125 appRecord->SetKilling();
126 appRecord->GetSignCode();
127 appRecord->GetJointUserId();
128 appRecord->GetUid();
129 appRecord->GetState();
130 appRecord->GetApplicationClient();
131 appRecord->IsTerminating();
132 appRecord->IsKeepAliveApp();
133 appRecord->IsEmptyKeepAliveApp();
134 appRecord->GetRestartResidentProcCount();
135 appRecord->GetUserTestInfo();
136 appRecord->GetRenderRecord();
137 appRecord->GetStartMsg();
138 appRecord->GetAppIndex();
139 appRecord->GetSecurityFlag();
140 return appRecord->IsKilling();
141 }
142 }
143
144 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)145 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
146 {
147 /* Run your code on data */
148 if (data == nullptr) {
149 std::cout << "invalid data" << std::endl;
150 return 0;
151 }
152
153 /* Validate the length of size */
154 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
155 return 0;
156 }
157
158 char* ch = (char *)malloc(size + 1);
159 if (ch == nullptr) {
160 std::cout << "malloc failed." << std::endl;
161 return 0;
162 }
163
164 (void)memset_s(ch, size + 1, 0x00, size + 1);
165 if (memcpy_s(ch, size, data, size) != EOK) {
166 std::cout << "copy failed." << std::endl;
167 free(ch);
168 ch = nullptr;
169 return 0;
170 }
171
172 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
173 free(ch);
174 ch = nullptr;
175 return 0;
176 }
177