• 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 "applifecycledeal_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "app_lifecycle_deal.h"
22 #include "ability_record.h"
23 #include "message_parcel.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28 
29 namespace OHOS {
30 namespace {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr size_t OFFSET_ZERO = 24;
34 constexpr size_t OFFSET_ONE = 16;
35 constexpr size_t OFFSET_TWO = 8;
36 }
GetU32Data(const char * ptr)37 uint32_t GetU32Data(const char* ptr)
38 {
39     // convert fuzz input data to an integer
40     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
41 }
GetFuzzAbilityToken()42 sptr<Token> GetFuzzAbilityToken()
43 {
44     sptr<Token> token = nullptr;
45 
46     AbilityRequest abilityRequest;
47     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
48     abilityRequest.abilityInfo.name = "MainAbility";
49     abilityRequest.abilityInfo.type = AbilityType::PAGE;
50     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
51     if (abilityRecord) {
52         token = abilityRecord->GetToken();
53     }
54 
55     return token;
56 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)57 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
58 {
59     AppLifeCycleDeal appLifeCycleDeal;
60     sptr<IAppScheduler> thread = nullptr;
61     appLifeCycleDeal.SetApplicationClient(thread);
62     std::shared_ptr<AbilityRunningRecord> ability = nullptr;
63     appLifeCycleDeal.LaunchAbility(ability);
64     AppLaunchData launchData;
65     Configuration config;
66     appLifeCycleDeal.LaunchApplication(launchData, config);
67     HapModuleInfo abilityStage;
68     appLifeCycleDeal.AddAbilityStage(abilityStage);
69     int32_t timeLevel = static_cast<int32_t>(GetU32Data(data));
70     appLifeCycleDeal.ScheduleTrimMemory(timeLevel);
71     int32_t level = static_cast<int32_t>(GetU32Data(data));
72     appLifeCycleDeal.ScheduleMemoryLevel(level);
73     sptr<IRemoteObject> token = GetFuzzAbilityToken();
74     appLifeCycleDeal.ScheduleCleanAbility(token);
75     Want want;
76     std::string bundleName(data, size);
77     appLifeCycleDeal.ScheduleAcceptWant(want, bundleName);
78     sptr<IQuickFixCallback> callback = nullptr;
79     int32_t recordId = 0;
80     appLifeCycleDeal.NotifyLoadRepairPatch(bundleName, callback, recordId);
81     appLifeCycleDeal.NotifyHotReloadPage(callback, recordId);
82     appLifeCycleDeal.NotifyUnLoadRepairPatch(bundleName, callback, recordId);
83     appLifeCycleDeal.GetApplicationClient();
84     appLifeCycleDeal.LowMemoryWarning();
85     appLifeCycleDeal.ScheduleForegroundRunning();
86     appLifeCycleDeal.ScheduleBackgroundRunning();
87     appLifeCycleDeal.ScheduleProcessSecurityExit();
88     appLifeCycleDeal.ScheduleTerminate();
89     return (appLifeCycleDeal.UpdateConfiguration(config) == 0);
90 }
91 }
92 
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96     /* Run your code on data */
97     if (data == nullptr) {
98         std::cout << "invalid data" << std::endl;
99         return 0;
100     }
101 
102     /* Validate the length of size */
103     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
104         return 0;
105     }
106 
107     char* ch = (char *)malloc(size + 1);
108     if (ch == nullptr) {
109         std::cout << "malloc failed." << std::endl;
110         return 0;
111     }
112 
113     (void)memset_s(ch, size + 1, 0x00, size + 1);
114     if (memcpy_s(ch, size, data, size) != EOK) {
115         std::cout << "copy failed." << std::endl;
116         free(ch);
117         ch = nullptr;
118         return 0;
119     }
120 
121     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
122     free(ch);
123     ch = nullptr;
124     return 0;
125 }
126