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 #define private public
24 #include "app_spawn_client.h"
25 #undef private
26 #include "remote_client_manager.h"
27 #include "window_focus_changed_listener.h"
28 #include "ability_record.h"
29 #include "parcel.h"
30 #include "securec.h"
31 
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34 
35 namespace OHOS {
36 namespace {
37 constexpr int INPUT_ZERO = 0;
38 constexpr int INPUT_ONE = 1;
39 constexpr int INPUT_THREE = 3;
40 constexpr size_t FOO_MAX_LEN = 1024;
41 constexpr size_t U32_AT_SIZE = 4;
42 constexpr uint8_t ENABLE = 2;
43 constexpr size_t OFFSET_ZERO = 24;
44 constexpr size_t OFFSET_ONE = 16;
45 constexpr size_t OFFSET_TWO = 8;
46 }
GetU32Data(const char * ptr)47 uint32_t GetU32Data(const char* ptr)
48 {
49     // convert fuzz input data to an integer
50     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
51         ptr[INPUT_THREE];
52 }
GetFuzzAbilityToken()53 sptr<Token> GetFuzzAbilityToken()
54 {
55     sptr<Token> token = nullptr;
56 
57     AbilityRequest abilityRequest;
58     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
59     abilityRequest.abilityInfo.name = "MainAbility";
60     abilityRequest.abilityInfo.type = AbilityType::PAGE;
61     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
62     if (abilityRecord) {
63         token = abilityRecord->GetToken();
64     }
65 
66     return token;
67 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)68 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
69 {
70     sptr<AppDeathRecipient> appDeathRecipient = new AppDeathRecipient();
71     wptr<IRemoteObject> remote;
72     appDeathRecipient->OnRemoteDied(remote);
73     std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
74     appDeathRecipient->SetTaskHandler(handler);
75     std::shared_ptr<AppMgrServiceInner> serviceInner;
76     appDeathRecipient->SetAppMgrServiceInner(serviceInner);
77     bool isRenderProcess = *data % ENABLE;
78     appDeathRecipient->SetIsRenderProcess(isRenderProcess);
79     AppSpawnClient appSpawnClient;
80     appSpawnClient.OpenConnection();
81     appSpawnClient.PreStartNWebSpawnProcess();
82     AppSpawnStartMsg startMsg;
83     pid_t pid = static_cast<pid_t>(GetU32Data(data));
84     appSpawnClient.StartProcess(startMsg, pid);
85     int status = static_cast<int>(GetU32Data(data));
86     appSpawnClient.GetRenderProcessTerminationStatus(startMsg, status);
87     appSpawnClient.QueryConnectionState();
88     RemoteClientManager remoteClientManager;
89     std::shared_ptr<BundleMgrHelper> bundleManagerHelper = nullptr;
90     remoteClientManager.SetBundleManagerHelper(bundleManagerHelper);
91     std::shared_ptr<AppSpawnClient> appSpawnClientptr;
92     remoteClientManager.SetSpawnClient(appSpawnClientptr);
93     remoteClientManager.GetSpawnClient();
94     remoteClientManager.GetBundleManagerHelper();
95     remoteClientManager.GetNWebSpawnClient();
96     std::shared_ptr<AppMgrServiceInner> owner;
97     WindowFocusChangedListener windowFocusChangedListener(owner, handler);
98     sptr<Rosen::FocusChangeInfo> focusChangeInfo = nullptr;
99     windowFocusChangedListener.OnFocused(focusChangeInfo);
100     windowFocusChangedListener.OnUnfocused(focusChangeInfo);
101     return (appSpawnClient.StartProcess(startMsg, pid) != 0);
102 }
103 }
104 
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107 {
108     /* Run your code on data */
109     if (data == nullptr) {
110         std::cout << "invalid data" << std::endl;
111         return 0;
112     }
113 
114     /* Validate the length of size */
115     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
116         return 0;
117     }
118 
119     char* ch = static_cast<char*>(malloc(size + 1));
120     if (ch == nullptr) {
121         std::cout << "malloc failed." << std::endl;
122         return 0;
123     }
124 
125     (void)memset_s(ch, size + 1, 0x00, size + 1);
126     if (memcpy_s(ch, size, data, size) != EOK) {
127         std::cout << "copy failed." << std::endl;
128         free(ch);
129         ch = nullptr;
130         return 0;
131     }
132 
133     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
134     free(ch);
135     ch = nullptr;
136     return 0;
137 }
138 
139