• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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     service->GetValidUserId(int32Param);
89     AppExecFwk::BundleInfo bundleInfo;
90     service->GetBundleInfo(stringParam, int32Param, int32Param, bundleInfo); // branch
91     AutoStartupInfo info;
92     AppExecFwk::AbilityInfo abilityInfo;
93     abilityInfo.bundleName = stringParam;
94     abilityInfo.name = stringParam;
95     abilityInfo.moduleName = stringParam;
96     service->GetAbilityTypeName(abilityInfo); // branch
97     abilityInfo.type == AppExecFwk::AbilityType::PAGE;
98     service->GetAbilityTypeName(abilityInfo); // branch
99     service->GetBundleMgrClient();
100     service->CheckPermissionForSystem();
101     service->CheckPermissionForSelf(stringParam);
102     service->SetApplicationAutoStartupByEDM(info, boolParam);
103     service->CancelApplicationAutoStartupByEDM(info, boolParam);
104     service->InnerApplicationAutoStartupByEDM(info, boolParam, boolParam);
105     service->CheckPermissionForEDM();
106 }
107 
AbilityStartupServiceFuzztest2(bool boolParam,std::string & stringParam,int32_t int32Param)108 void AbilityStartupServiceFuzztest2(bool boolParam, std::string &stringParam, int32_t int32Param)
109 {
110     std::shared_ptr<AbilityAutoStartupService> service = std::make_shared<AbilityAutoStartupService>();
111     AutoStartupAbilityData abilityData;
112     abilityData.isVisible = boolParam;
113     abilityData.abilityTypeName = stringParam;
114     abilityData.accessTokenId = stringParam;
115     abilityData.setterUserId = int32Param;
116     AutoStartupInfo info;
117     service->GetAbilityData(info, abilityData); // branch
118     AppExecFwk::AbilityInfo abilityInfo;
119     abilityInfo.bundleName = stringParam;
120     abilityInfo.name = stringParam;
121     abilityInfo.moduleName = stringParam;
122     service->IsTargetAbility(info, abilityInfo);
123     AppExecFwk::ExtensionAbilityInfo extensionInfo;
124     extensionInfo.bundleName = stringParam;
125     extensionInfo.name = stringParam;
126     extensionInfo.moduleName = stringParam;
127     service->IsTargetExtension(info, extensionInfo);
128     service->GetExtensionTypeName(extensionInfo);
129     extensionInfo.type == AppExecFwk::ExtensionAbilityType::SERVICE;
130     service->GetExtensionTypeName(extensionInfo);
131     extensionInfo.type == AppExecFwk::ExtensionAbilityType::APP_SERVICE;
132     service->GetExtensionTypeName(extensionInfo);
133     service->GetAbilityInfo(info, abilityData);
134 }
135 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)136 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
137 {
138     bool boolParam = *data % ENABLE;
139     std::string stringParam(data, size);
140     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
141     AbilityStartupServiceFuzztest1(boolParam, stringParam, int32Param);
142     AbilityStartupServiceFuzztest2(boolParam, stringParam, int32Param);
143     return true;
144 }
145 }
146 
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149 {
150     /* Run your code on data */
151     if (data == nullptr) {
152         return 0;
153     }
154 
155     /* Validate the length of size */
156     if (size < OHOS::U32_AT_SIZE) {
157         return 0;
158     }
159 
160     char* ch = static_cast<char*>(malloc(size + 1));
161     if (ch == nullptr) {
162         std::cout << "malloc failed." << std::endl;
163         return 0;
164     }
165 
166     (void)memset_s(ch, size + 1, 0x00, size + 1);
167     if (memcpy_s(ch, size, data, size) != EOK) {
168         std::cout << "copy failed." << std::endl;
169         free(ch);
170         ch = nullptr;
171         return 0;
172     }
173 
174     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
175     free(ch);
176     ch = nullptr;
177     return 0;
178 }
179 
180