• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "abilityautostartupserviceb_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "ability_auto_startup_service.h"
24 #undef protected
25 #undef private
26 
27 #include "ability_record.h"
28 #include "auto_startup_callback_proxy.h"
29 
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 using namespace OHOS::AbilityRuntime;
33 
34 namespace OHOS {
35 namespace {
36 constexpr int INPUT_ZERO = 0;
37 constexpr int INPUT_ONE = 1;
38 constexpr int INPUT_THREE = 3;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr uint8_t ENABLE = 2;
41 constexpr size_t OFFSET_ZERO = 24;
42 constexpr size_t OFFSET_ONE = 16;
43 constexpr size_t OFFSET_TWO = 8;
44 }
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 }
52 
GetFuzzAbilityToken()53 sptr<Token> GetFuzzAbilityToken()
54 {
55     sptr<Token> token = nullptr;
56     AbilityRequest abilityRequest;
57     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
58     abilityRequest.abilityInfo.name = "MainAbility";
59     abilityRequest.abilityInfo.type = AbilityType::DATA;
60     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
61     if (abilityRecord) {
62         token = abilityRecord->GetToken();
63     }
64     return token;
65 }
66 
AbilityStartupServiceFuzztest1(bool boolParam,std::string & stringParam,int32_t int32Param)67 void AbilityStartupServiceFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param)
68 {
69     std::shared_ptr<AbilityAutoStartupService> service = std::make_shared<AbilityAutoStartupService>();
70     service->SetDeathRecipient(nullptr, nullptr);
71     sptr<Token> token1 = GetFuzzAbilityToken();
72     service->SetDeathRecipient(token1, nullptr); // add deathrecipient
73     sptr<IRemoteObject::DeathRecipient> client =
74         new (std::nothrow) AbilityAutoStartupService::ClientDeathRecipient(std::weak_ptr(service));
75     service->SetDeathRecipient(token1, client); // add deathrecipient
76     service->SetDeathRecipient(token1, client); // duplicate add deathrecipient
77     sptr<Token> token2 = GetFuzzAbilityToken();
78     service->RegisterAutoStartupSystemCallback(token1);
79     service->CleanResource(nullptr); // branch nullptr token
80     service->CleanResource(token1); // branch clean exists token
81     service->CleanResource(token2);  // branch clean not exists token
82 
83     client->OnRemoteDied(nullptr); // branch
84     client->OnRemoteDied(token1); // branch
85 
86     service->GetSelfApplicationBundleName();
87     service->CheckSelfApplication(stringParam);
88     AppExecFwk::BundleInfo bundleInfo;
89     service->GetBundleInfo(stringParam, bundleInfo, int32Param, int32Param, int32Param); // branch
90     AutoStartupInfo info;
91     service->GetAbilityData(info, boolParam, stringParam, stringParam, int32Param); // branch
92     AppExecFwk::AbilityInfo abilityInfo;
93     service->GetAbilityTypeName(abilityInfo); // branch
94     abilityInfo.type == AppExecFwk::AbilityType::PAGE;
95     service->GetAbilityTypeName(abilityInfo); // branch
96     AppExecFwk::ExtensionAbilityInfo extensionInfo;
97     service->GetExtensionTypeName(extensionInfo);
98     extensionInfo.type == AppExecFwk::ExtensionAbilityType::SERVICE;
99     service->GetExtensionTypeName(extensionInfo);
100     service->GetBundleMgrClient();
101     service->CheckPermissionForSystem();
102     service->CheckPermissionForSelf(stringParam);
103     service->GetAbilityInfo(info, stringParam, stringParam, int32Param);
104     service->SetApplicationAutoStartupByEDM(info, boolParam);
105     service->CancelApplicationAutoStartupByEDM(info, boolParam);
106     service->InnerApplicationAutoStartupByEDM(info, boolParam, boolParam);
107     service->CheckPermissionForEDM();
108 }
109 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)110 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
111 {
112     bool boolParam = *data % ENABLE;
113     std::string stringParam(data, size);
114     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
115     AbilityStartupServiceFuzztest1(boolParam, stringParam, int32Param);
116     return true;
117 }
118 }
119 
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
122 {
123     /* Run your code on data */
124     if (data == nullptr) {
125         return 0;
126     }
127 
128     /* Validate the length of size */
129     if (size < OHOS::U32_AT_SIZE) {
130         return 0;
131     }
132 
133     char* ch = static_cast<char*>(malloc(size + 1));
134     if (ch == nullptr) {
135         std::cout << "malloc failed." << std::endl;
136         return 0;
137     }
138 
139     (void)memset_s(ch, size + 1, 0x00, size + 1);
140     if (memcpy_s(ch, size, data, size) != EOK) {
141         std::cout << "copy failed." << std::endl;
142         free(ch);
143         ch = nullptr;
144         return 0;
145     }
146 
147     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
148     free(ch);
149     ch = nullptr;
150     return 0;
151 }
152 
153