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