• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "appmgrrest_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "app_death_recipient.h"
22 #include "app_mgr_service_event_handler.h"
23 #include "app_process_manager.h"
24 #define private public
25 #include "app_spawn_client.h"
26 #include "app_spawn_msg_wrapper.h"
27 #undef private
28 #include "app_spawn_socket.h"
29 #include "remote_client_manager.h"
30 #include "window_focus_changed_listener.h"
31 #include "ability_record.h"
32 #include "parcel.h"
33 #include "securec.h"
34 
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37 
38 namespace OHOS {
39 namespace {
40 constexpr int INPUT_ZERO = 0;
41 constexpr int INPUT_ONE = 1;
42 constexpr int INPUT_THREE = 3;
43 constexpr size_t FOO_MAX_LEN = 1024;
44 constexpr size_t U32_AT_SIZE = 4;
45 constexpr uint8_t ENABLE = 2;
46 constexpr size_t OFFSET_ZERO = 24;
47 constexpr size_t OFFSET_ONE = 16;
48 constexpr size_t OFFSET_TWO = 8;
49 }
GetU32Data(const char * ptr)50 uint32_t GetU32Data(const char* ptr)
51 {
52     // convert fuzz input data to an integer
53     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
54         ptr[INPUT_THREE];
55 }
GetFuzzAbilityToken()56 sptr<Token> GetFuzzAbilityToken()
57 {
58     sptr<Token> token = nullptr;
59 
60     AbilityRequest abilityRequest;
61     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
62     abilityRequest.abilityInfo.name = "MainAbility";
63     abilityRequest.abilityInfo.type = AbilityType::PAGE;
64     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
65     if (abilityRecord) {
66         token = abilityRecord->GetToken();
67     }
68 
69     return token;
70 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)71 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
72 {
73     sptr<AppDeathRecipient> appDeathRecipient = new AppDeathRecipient();
74     wptr<IRemoteObject> remote;
75     appDeathRecipient->OnRemoteDied(remote);
76     std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
77     appDeathRecipient->SetTaskHandler(handler);
78     std::shared_ptr<AppMgrServiceInner> serviceInner;
79     appDeathRecipient->SetAppMgrServiceInner(serviceInner);
80     bool isRenderProcess = *data % ENABLE;
81     appDeathRecipient->SetIsRenderProcess(isRenderProcess);
82     AppProcessManager appProcessManager;
83     std::shared_ptr<AppTaskInfo> appTaskInfo;
84     appProcessManager.RemoveAppFromRecentList(appTaskInfo);
85     std::string appName(data, size);
86     std::string processName(data, size);
87     pid_t pid = static_cast<pid_t>(GetU32Data(data));
88     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
89     appProcessManager.AddAppToRecentList(appName, processName, pid, recordId);
90     appProcessManager.PushAppFront(recordId);
91     appProcessManager.GetAppTaskInfoById(recordId);
92     std::string bundleName(data, size);
93     appProcessManager.GetAppTaskInfoByProcessName(appName, bundleName);
94     appProcessManager.GetRecentAppList();
95     appProcessManager.RemoveAppFromRecentListById(recordId);
96     appProcessManager.ClearRecentAppList();
97     AppSpawnClient appSpawnClient;
98     appSpawnClient.OpenConnection();
99     std::shared_ptr<AppSpawnSocket> socket;
100     appSpawnClient.SetSocket(socket);
101     appSpawnClient.PreStartNWebSpawnProcess();
102     appSpawnClient.PreStartNWebSpawnProcessImpl();
103     AppSpawnStartMsg startMsg;
104     appSpawnClient.StartProcess(startMsg, pid);
105     int status = static_cast<int>(GetU32Data(data));
106     appSpawnClient.GetRenderProcessTerminationStatus(startMsg, status);
107     appSpawnClient.QueryConnectionState();
108     appSpawnClient.CloseConnection();
109     AppSpawnMsgWrapper appSpawnMsgWrapper;
110     appSpawnMsgWrapper.AssembleMsg(startMsg);
111     appSpawnMsgWrapper.VerifyMsg(startMsg);
112     appSpawnMsgWrapper.DumpMsg();
113     appSpawnMsgWrapper.FreeMsg();
114     AppSpawnSocket appSpawnSocket;
115     std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket;
116     appSpawnSocket.SetClientSocket(clientSocket);
117     appSpawnSocket.OpenAppSpawnConnection();
118     int32_t len = appSpawnMsgWrapper.GetMsgLength();
119     appSpawnSocket.WriteMessage(appSpawnMsgWrapper.GetMsgBuf(), len);
120     appSpawnSocket.ReadMessage(reinterpret_cast<void*>(*data), len);
121     appSpawnSocket.CloseAppSpawnConnection();
122     RemoteClientManager remoteClientManager;
123     std::shared_ptr<BundleMgrHelper> bundleManagerHelper = nullptr;
124     remoteClientManager.SetBundleManagerHelper(bundleManagerHelper);
125     std::shared_ptr<AppSpawnClient> appSpawnClientptr;
126     remoteClientManager.SetSpawnClient(appSpawnClientptr);
127     remoteClientManager.GetSpawnClient();
128     remoteClientManager.GetBundleManagerHelper();
129     remoteClientManager.GetNWebSpawnClient();
130     std::shared_ptr<AppMgrServiceInner> owner;
131     WindowFocusChangedListener windowFocusChangedListener(owner, handler);
132     sptr<Rosen::FocusChangeInfo> focusChangeInfo = nullptr;
133     windowFocusChangedListener.OnFocused(focusChangeInfo);
134     windowFocusChangedListener.OnUnfocused(focusChangeInfo);
135     return (appSpawnClient.StartProcessImpl(startMsg, pid) != 0);
136 }
137 }
138 
139 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)140 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
141 {
142     /* Run your code on data */
143     if (data == nullptr) {
144         std::cout << "invalid data" << std::endl;
145         return 0;
146     }
147 
148     /* Validate the length of size */
149     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
150         return 0;
151     }
152 
153     char* ch = (char*)malloc(size + 1);
154     if (ch == nullptr) {
155         std::cout << "malloc failed." << std::endl;
156         return 0;
157     }
158 
159     (void)memset_s(ch, size + 1, 0x00, size + 1);
160     if (memcpy_s(ch, size, data, size) != EOK) {
161         std::cout << "copy failed." << std::endl;
162         free(ch);
163         ch = nullptr;
164         return 0;
165     }
166 
167     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
168     free(ch);
169     ch = nullptr;
170     return 0;
171 }
172 
173