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
25 #include "hitrace_dump.h"
26 #include "hitrace_fuzztest_common.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace Hitrace {
HitraceDumpCmdModeTest(const uint8_t * data,size_t size)31 void HitraceDumpCmdModeTest(const uint8_t* data, size_t size)
32 {
33 uint64_t traceTags = 0;
34 if (size < sizeof(traceTags)) {
35 return;
36 }
37 StreamToValueInfo(data, traceTags);
38 std::string hitraceTags = " sched freq idle disk";
39 GenerateTagStr(traceTags, hitraceTags);
40 std::cout << "trace mode : " << GetTraceMode() << std::endl;
41 (void)OpenTrace(hitraceTags);
42 std::cout << "trace mode : " << GetTraceMode() << std::endl;
43 (void)RecordTraceOn();
44 sleep(1);
45 (void)RecordTraceOff();
46 (void)CloseTrace();
47 std::cout << "trace mode : " << GetTraceMode() << std::endl;
48 }
49
HitraceDumpCacheTest(const uint8_t * data,size_t size)50 void HitraceDumpCacheTest(const uint8_t* data, size_t size)
51 {
52 uint64_t traceTags = 0;
53 if (size < sizeof(traceTags)) {
54 return;
55 }
56 StreamToValueInfo(data, traceTags);
57 std::vector<std::string> hitraceTags = { "sched", "freq", "idle", "disk" };
58 GenerateTagVec(traceTags, hitraceTags);
59 std::cout << "trace mode : " << GetTraceMode() << std::endl;
60 (void)OpenTrace(hitraceTags);
61 std::cout << "trace mode : " << GetTraceMode() << std::endl;
62 sleep(1);
63 (void)CacheTraceOn();
64 std::cout << "trace mode : " << GetTraceMode() << std::endl;
65 sleep(1);
66 (void)DumpTrace();
67 std::cout << "trace mode : " << GetTraceMode() << std::endl;
68 sleep(1);
69 (void)CacheTraceOff();
70 std::cout << "trace mode : " << GetTraceMode() << std::endl;
71 sleep(1);
72 (void)DumpTrace();
73 std::cout << "trace mode : " << GetTraceMode() << std::endl;
74 (void)CloseTrace();
75 std::cout << "trace mode : " << GetTraceMode() << std::endl;
76 }
77
HitraceDumpServiceModeTest(const uint8_t * data,size_t size)78 void HitraceDumpServiceModeTest(const uint8_t* data, size_t size)
79 {
80 int duration = 0;
81 uint64_t happenTime = 0;
82 uint64_t traceTags = 0;
83 if (size < sizeof(traceTags) + sizeof(duration) + sizeof(happenTime)) {
84 return;
85 }
86 StreamToValueInfo(data, duration);
87 StreamToValueInfo(data, happenTime);
88 StreamToValueInfo(data, traceTags);
89 duration = duration > 30 ? 30 : duration; // 30 : max duration
90 std::vector<std::string> hitraceTags = { "sched", "freq", "idle", "disk" };
91 GenerateTagVec(traceTags, hitraceTags);
92 std::cout << "trace mode : " << GetTraceMode() << std::endl;
93 (void)OpenTrace(hitraceTags);
94 std::cout << "trace mode : " << GetTraceMode() << std::endl;
95 sleep(1);
96 (void)DumpTrace();
97 sleep(1);
98 (void)DumpTrace(duration, happenTime);
99 (void)CloseTrace();
100 std::cout << "trace mode : " << GetTraceMode() << std::endl;
101 }
102 } // namespace Hitrace
103 } // namespace HiviewDFX
104 } // namespace OHOS
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109 if (data == nullptr || size == 0) {
110 return 0;
111 }
112
113 /* Run your code on data */
114 OHOS::HiviewDFX::Hitrace::HitraceDumpCmdModeTest(data, size);
115 OHOS::HiviewDFX::Hitrace::HitraceDumpCacheTest(data, size);
116 OHOS::HiviewDFX::Hitrace::HitraceDumpServiceModeTest(data, size);
117 return 0;
118 }
119