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 FOO_MAX_LEN = 1024;
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 }
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 }
53
GetFuzzAbilityToken()54 sptr<Token> GetFuzzAbilityToken()
55 {
56 sptr<Token> token = nullptr;
57 AbilityRequest abilityRequest;
58 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
59 abilityRequest.abilityInfo.name = "MainAbility";
60 abilityRequest.abilityInfo.type = AbilityType::DATA;
61 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
62 if (abilityRecord) {
63 token = abilityRecord->GetToken();
64 }
65 return token;
66 }
67
AbilityStartupServiceFuzztest1(bool boolParam,std::string & stringParam,int32_t int32Param)68 void AbilityStartupServiceFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param)
69 {
70 std::shared_ptr<AbilityAutoStartupService> service = std::make_shared<AbilityAutoStartupService>();
71 service->SetDeathRecipient(nullptr, nullptr);
72 sptr<Token> token1 = GetFuzzAbilityToken();
73 service->SetDeathRecipient(token1, nullptr); // add deathrecipient
74 sptr<IRemoteObject::DeathRecipient> client =
75 new (std::nothrow) AbilityAutoStartupService::ClientDeathRecipient(std::weak_ptr(service));
76 service->SetDeathRecipient(token1, client); // add deathrecipient
77 service->SetDeathRecipient(token1, client); // duplicate add deathrecipient
78 sptr<Token> token2 = GetFuzzAbilityToken();
79 service->RegisterAutoStartupSystemCallback(token1);
80 service->CleanResource(nullptr); // branch nullptr token
81 service->CleanResource(token1); // branch clean exists token
82 service->CleanResource(token2); // branch clean not exists token
83
84 client->OnRemoteDied(nullptr); // branch
85 client->OnRemoteDied(token1); // branch
86
87 service->GetSelfApplicationBundleName();
88 service->CheckSelfApplication(stringParam);
89 AppExecFwk::BundleInfo bundleInfo;
90 service->GetBundleInfo(stringParam, bundleInfo, int32Param, int32Param, int32Param); // branch
91 AutoStartupInfo info;
92 service->GetAbilityData(info, boolParam, stringParam, stringParam, int32Param); // branch
93 AppExecFwk::AbilityInfo abilityInfo;
94 service->GetAbilityTypeName(abilityInfo); // branch
95 abilityInfo.type == AppExecFwk::AbilityType::PAGE;
96 service->GetAbilityTypeName(abilityInfo); // branch
97 AppExecFwk::ExtensionAbilityInfo extensionInfo;
98 service->GetExtensionTypeName(extensionInfo);
99 extensionInfo.type == AppExecFwk::ExtensionAbilityType::SERVICE;
100 service->GetExtensionTypeName(extensionInfo);
101 service->GetBundleMgrClient();
102 service->CheckPermissionForSystem();
103 service->CheckPermissionForSelf(stringParam);
104 service->GetAbilityInfo(info, stringParam, stringParam, int32Param);
105 service->SetApplicationAutoStartupByEDM(info, boolParam);
106 service->CancelApplicationAutoStartupByEDM(info, boolParam);
107 service->InnerApplicationAutoStartupByEDM(info, boolParam, boolParam);
108 service->CheckPermissionForEDM();
109 }
110
DoSomethingInterestingWithMyAPI(const char * data,size_t size)111 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
112 {
113 bool boolParam = *data % ENABLE;
114 std::string stringParam(data, size);
115 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
116 AbilityStartupServiceFuzztest1(boolParam, stringParam, int32Param);
117 return true;
118 }
119 }
120
121 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)122 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
123 {
124 /* Run your code on data */
125 if (data == nullptr) {
126 return 0;
127 }
128
129 /* Validate the length of size */
130 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
131 return 0;
132 }
133
134 char* ch = static_cast<char*>(malloc(size + 1));
135 if (ch == nullptr) {
136 std::cout << "malloc failed." << std::endl;
137 return 0;
138 }
139
140 (void)memset_s(ch, size + 1, 0x00, size + 1);
141 if (memcpy_s(ch, size, data, size) != EOK) {
142 std::cout << "copy failed." << std::endl;
143 free(ch);
144 ch = nullptr;
145 return 0;
146 }
147
148 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
149 free(ch);
150 ch = nullptr;
151 return 0;
152 }
153
154