• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "abilitykeepaliveservice_fuzzer.h"
16 
17 #define private public
18 #include "ability_keep_alive_service.h"
19 #include "ability_fuzz_util.h"
20 #undef private
21 
22 #include "keep_alive_info.h"
23 
24 #include <fuzzer/FuzzedDataProvider.h>
25 #include <iostream>
26 #include "securec.h"
27 
28 namespace OHOS {
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AbilityRuntime;
31 namespace {
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr size_t STRING_MAX_LENGTH = 128;
34 }
35 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)36 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
37 {
38     auto &abilityKeepAliveService = AbilityKeepAliveService::GetInstance();
39     std::vector<KeepAliveInfo> infoList;
40     FuzzedDataProvider fdp(data, size);
41 
42     KeepAliveInfo info;
43     info.userId = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
44     info.setterId = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
45     info.appType = static_cast<AAFwk::KeepAliveAppType>(fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE));
46     info.setter = static_cast<AAFwk::KeepAliveSetter>(fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE));
47     info.policy = static_cast<AAFwk::KeepAlivePolicy>(fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE));
48     info.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
49 
50     size_t arraySize = fdp.ConsumeIntegralInRange<size_t>(0, STRING_MAX_LENGTH);
51     for (size_t i = 0; i < arraySize; ++i) {
52         AppExecFwk::AbilityFuzzUtil::GetRandomKeepAliveInfo(fdp, info);
53         infoList.emplace_back(info);
54     }
55 
56     int32_t int32Param = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
57     bool boolParam = fdp.ConsumeBool();
58     std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
59     int32_t in32appType = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
60 
61     abilityKeepAliveService.QueryKeepAliveApplications(int32Param, in32appType, infoList);
62     abilityKeepAliveService.CancelKeepAlive(info);
63     abilityKeepAliveService.GetValidUserId(int32Param);
64     abilityKeepAliveService.IsKeepAliveApp(bundleName, int32Param);
65     abilityKeepAliveService.GetKeepAliveApplications(int32Param, infoList);
66     abilityKeepAliveService.QueryKeepAliveAppServiceExtensions(infoList);
67     abilityKeepAliveService.ClearKeepAliveAppServiceExtension(info);
68     return true;
69 }
70 }
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     // Run your code on data.
76     OHOS::DoSomethingInterestingWithMyAPI(data, size);
77     return 0;
78 }