• 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 "abilityappfreezemanagertenth_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #include "appfreeze_manager.h"
24 #undef private
25 
26 #include "securec.h"
27 #include "ability_record.h"
28 
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 namespace {
34 constexpr size_t U32_AT_SIZE = 4;
GetRandomTerminalBinder(FuzzedDataProvider & fdp,AppfreezeManager::TerminalBinder & terminalBinder)35 void GetRandomTerminalBinder(FuzzedDataProvider& fdp, AppfreezeManager::TerminalBinder& terminalBinder)
36 {
37     terminalBinder.pid = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
38     terminalBinder.tid = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
39 }
40 }
41 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)42 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
43 {
44     auto freeze = AppfreezeManager::GetInstance();
45     if (!freeze) {
46         return false;
47     }
48     std::string stack;
49     int32_t pid;
50     std::set<int> asyncPids;
51     AppfreezeManager::TerminalBinder terminalBinder;
52     FuzzedDataProvider fdp(data, size);
53     size_t arraySize = fdp.ConsumeIntegralInRange<size_t>(0, U32_AT_SIZE);
54     for (size_t i = 0; i < arraySize; ++i) {
55         stack = fdp.ConsumeRandomLengthString();
56         pid = fdp.ConsumeIntegralInRange<int32_t>(0, U32_AT_SIZE);
57         GetRandomTerminalBinder(fdp, terminalBinder);
58     }
59     AppfreezeManager::ParseBinderParam params = {0, 0, pid, 0};
60     freeze->GetBinderPeerPids(stack, params, asyncPids, terminalBinder);
61     return true;
62 }
63 }
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
67 {
68     // Run your code on data.
69     OHOS::DoSomethingInterestingWithMyAPI(data, size);
70     return 0;
71 }