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 "hitracedump_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <iostream>
22 #include <unistd.h>
23 #include <vector>
24 #include <functional>
25
26 #include "hitrace_dump.h"
27 #include "hitrace_fuzztest_common.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace Hitrace {
32 namespace {
33 constexpr int MAX_DURATION = 30;
34 constexpr int MAX_FILE_SIZE = 1024 * 1024;
35 }
HitraceDumpCmdModeTest(const uint8_t * data,size_t size)36 void HitraceDumpCmdModeTest(const uint8_t* data, size_t size)
37 {
38 uint64_t traceTags = 0;
39 if (size < sizeof(traceTags)) {
40 return;
41 }
42 StreamToValueInfo(data, traceTags);
43 std::string hitraceTags = " sched freq idle disk";
44 GenerateTagStr(traceTags, hitraceTags);
45 std::cout << "trace mode : " << GetTraceMode() << std::endl;
46 (void)OpenTrace(hitraceTags);
47 std::cout << "trace mode : " << GetTraceMode() << std::endl;
48 (void)RecordTraceOn();
49 sleep(1);
50 (void)RecordTraceOff();
51 (void)CloseTrace();
52 std::cout << "trace mode : " << GetTraceMode() << std::endl;
53 }
54
HitraceDumpCacheTest(const uint8_t * data,size_t size)55 void HitraceDumpCacheTest(const uint8_t* data, size_t size)
56 {
57 uint64_t traceTags = 0;
58 if (size < sizeof(traceTags)) {
59 return;
60 }
61 StreamToValueInfo(data, traceTags);
62 std::vector<std::string> hitraceTags = { "sched", "freq", "idle", "disk" };
63 GenerateTagVec(traceTags, hitraceTags);
64 std::cout << "trace mode : " << GetTraceMode() << std::endl;
65 (void)OpenTrace(hitraceTags);
66 std::cout << "trace mode : " << GetTraceMode() << std::endl;
67 sleep(1);
68 (void)CacheTraceOn();
69 std::cout << "trace mode : " << GetTraceMode() << std::endl;
70 sleep(1);
71 (void)DumpTrace();
72 std::cout << "trace mode : " << GetTraceMode() << std::endl;
73 sleep(1);
74 (void)CacheTraceOff();
75 std::cout << "trace mode : " << GetTraceMode() << std::endl;
76 sleep(1);
77 (void)DumpTrace();
78 std::cout << "trace mode : " << GetTraceMode() << std::endl;
79 (void)CloseTrace();
80 std::cout << "trace mode : " << GetTraceMode() << std::endl;
81 }
82
HitraceDumpServiceModeTest(const uint8_t * data,size_t size)83 void HitraceDumpServiceModeTest(const uint8_t* data, size_t size)
84 {
85 int duration = 0;
86 uint64_t happenTime = 0;
87 uint64_t traceTags = 0;
88 if (size < sizeof(traceTags) + sizeof(duration) + sizeof(happenTime)) {
89 return;
90 }
91 StreamToValueInfo(data, duration);
92 StreamToValueInfo(data, happenTime);
93 StreamToValueInfo(data, traceTags);
94 duration = duration > 30 ? 30 : duration; // 30 : max duration
95 std::vector<std::string> hitraceTags = { "sched", "freq", "idle", "disk" };
96 GenerateTagVec(traceTags, hitraceTags);
97 std::cout << "trace mode : " << GetTraceMode() << std::endl;
98 (void)OpenTrace(hitraceTags);
99 std::cout << "trace mode : " << GetTraceMode() << std::endl;
100 sleep(1);
101 (void)DumpTrace();
102 sleep(1);
103 (void)DumpTrace(duration, happenTime);
104 (void)CloseTrace();
105 std::cout << "trace mode : " << GetTraceMode() << std::endl;
106 }
107
HitraceDumpTest(const uint8_t * data,size_t size)108 void HitraceDumpTest(const uint8_t* data, size_t size)
109 {
110 bool enable = 0;
111 if (size < sizeof(enable)) {
112 return;
113 }
114 StreamToValueInfo(data, enable);
115 SetTraceStatus(enable);
116 }
117
HitraceDumpAsyncTest(const uint8_t * data,size_t size)118 void HitraceDumpAsyncTest(const uint8_t* data, size_t size)
119 {
120 int maxDuration = 0;
121 uint64_t utTraceEndTime = 0;
122 int64_t fileSizeLimit = 0;
123 if (size < sizeof(maxDuration) + sizeof(utTraceEndTime) + sizeof(fileSizeLimit)) {
124 return;
125 }
126 StreamToValueInfo(data, maxDuration);
127 StreamToValueInfo(data, utTraceEndTime);
128 StreamToValueInfo(data, fileSizeLimit);
129
130 maxDuration = maxDuration > MAX_DURATION ? MAX_DURATION : maxDuration;
131 fileSizeLimit = fileSizeLimit > MAX_FILE_SIZE ? MAX_FILE_SIZE : fileSizeLimit;
132
133 std::vector<std::string> hitraceTags = { "sched", "freq", "idle", "disk" };
134 std::cout << "trace mode : " << GetTraceMode() << std::endl;
135 (void)OpenTrace(hitraceTags);
136 std::cout << "trace mode : " << GetTraceMode() << std::endl;
137
138 // 定义异步回调函数
139 std::function<void(TraceRetInfo)> asyncCallback = [](TraceRetInfo traceInfo) {
140 std::cout << "Async dump completed with error code: " << static_cast<int>(traceInfo.errorCode) << std::endl;
141 for (const auto& file : traceInfo.outputFiles) {
142 std::cout << "Output file: " << file << std::endl;
143 }
144 };
145
146 (void)DumpTraceAsync(maxDuration, utTraceEndTime, fileSizeLimit, asyncCallback);
147 sleep(1);
148 (void)CloseTrace();
149 std::cout << "trace mode : " << GetTraceMode() << std::endl;
150 }
151 } // namespace Hitrace
152 } // namespace HiviewDFX
153 } // namespace OHOS
154
155 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)156 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
157 {
158 if (data == nullptr || size == 0) {
159 return 0;
160 }
161
162 /* Run your code on data */
163 OHOS::HiviewDFX::Hitrace::HitraceDumpCmdModeTest(data, size);
164 OHOS::HiviewDFX::Hitrace::HitraceDumpCacheTest(data, size);
165 OHOS::HiviewDFX::Hitrace::HitraceDumpServiceModeTest(data, size);
166 OHOS::HiviewDFX::Hitrace::HitraceDumpTest(data, size);
167 OHOS::HiviewDFX::Hitrace::HitraceDumpAsyncTest(data, size);
168 return 0;
169 }
170