1 /*
2 * Copyright (c) 2021 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <iostream>
20 #include "dump_usage.h"
21
22 using namespace std;
23 using OHOS::HiviewDFX::DumpUsage;
24
25 namespace OHOS {
26 constexpr int OFFSET = 8;
GetPidFromData(const uint8_t * data,size_t size)27 int GetPidFromData(const uint8_t* data, size_t size)
28 {
29 int pid = 0;
30 if (size == 1) {
31 pid = static_cast<int>(data[0]);
32 } else {
33 pid = static_cast<int>(data[0]) | (static_cast<int>(data[1]) << OFFSET);
34 }
35 return pid;
36 }
37
GetCpuUsageFuzzTest(const uint8_t * data,size_t size)38 bool GetCpuUsageFuzzTest(const uint8_t* data, size_t size)
39 {
40 int pid = GetPidFromData(data, size);
41 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
42 auto result = dumpUsage->GetCpuUsage(pid);
43 std::cout << "pid:" << pid << " cpuusage:" << result << std::endl;
44 return true;
45 }
46
GetPssFuzzTest(const uint8_t * data,size_t size)47 bool GetPssFuzzTest(const uint8_t* data, size_t size)
48 {
49 int pid = GetPidFromData(data, size);
50 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
51 auto result = dumpUsage->GetPss(pid);
52 std::cout << "pid:" << pid << " pss:" << result << std::endl;
53 return true;
54 }
55
GetPrivateDirtyFuzzTest(const uint8_t * data,size_t size)56 bool GetPrivateDirtyFuzzTest(const uint8_t* data, size_t size)
57 {
58 int pid = GetPidFromData(data, size);
59 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
60 auto result = dumpUsage->GetPrivateDirty(pid);
61 std::cout << "pid:" << pid << " private dirty:" << result << std::endl;
62 return true;
63 }
64
GetSharedDirtyFuzzTest(const uint8_t * data,size_t size)65 bool GetSharedDirtyFuzzTest(const uint8_t* data, size_t size)
66 {
67 int pid = GetPidFromData(data, size);
68 unique_ptr<DumpUsage> dumpUsage = make_unique<DumpUsage>();
69 auto result = dumpUsage->GetSharedDirty(pid);
70 std::cout << "pid:" << pid << " shared dirty:" << result << std::endl;
71 return true;
72 }
73
GetMemInfoFuzzTest(const uint8_t * data,size_t size)74 bool GetMemInfoFuzzTest(const uint8_t* data, size_t size)
75 {
76 int pid = GetPidFromData(data, size);
77 unique_ptr<HiviewDFX::DumpUsage> dumpUsage = make_unique<HiviewDFX::DumpUsage>();
78 HiviewDFX::MemInfoData::MemInfo memInfo;
79 auto ret = dumpUsage->GetMemInfo(pid, memInfo);
80 std::cout << "pid:" << pid << " ret:" << ret << std::endl;
81 return true;
82 }
83 }
84
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88 if (data == nullptr || size == 0) {
89 return 0;
90 }
91 /* Run your code on data */
92 OHOS::GetCpuUsageFuzzTest(data, size);
93 OHOS::GetPssFuzzTest(data, size);
94 OHOS::GetPrivateDirtyFuzzTest(data, size);
95 OHOS::GetSharedDirtyFuzzTest(data, size);
96 OHOS::GetMemInfoFuzzTest(data, size);
97 return 0;
98 }