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 "hitrace_fuzzer.h"
17
18#include <cstddef>
19#include <cstdint>
20#include <string>
21#include <iostream>
22#include <vector>
23
24#include "hitrace_fuzztest_common.h"
25
26namespace OHOS {
27namespace HiviewDFX {
28namespace Hitrace {
29void HitraceCommonTest(const uint8_t* data, size_t size)
30{
31    int bufferSz = 0;
32    uint32_t duration = 0;
33    uint64_t traceTags = 0;
34    if (size < sizeof(bufferSz) + sizeof(duration) + sizeof(traceTags)) {
35        return;
36    }
37    StreamToValueInfo(data, bufferSz);
38    bufferSz += 256; // 256 : min trace buffer size
39    StreamToValueInfo(data, duration);
40    StreamToValueInfo(data, traceTags);
41    duration = duration > 10 ? 10 : duration; // 10 : max duration
42    std::string hitraceCmd = "hitrace -b " + std::to_string(bufferSz) + " -t " + std::to_string(duration);
43    std::string hitraceTags = " sched freq binder idle disk";
44    GenerateTagStr(traceTags, hitraceTags);
45    std::string outputCmd = " -o /data/local/tmp/HitraceCommonTest_fuzz.ftrace";
46    std::cout << hitraceCmd << hitraceTags << outputCmd << std::endl;
47    system((hitraceCmd + hitraceTags + outputCmd).c_str());
48}
49
50void HitraceRecordTest(const uint8_t* data, size_t size)
51{
52    int bufferSz = 0;
53    bool isRaw = false;
54    int recordCmdRand = 0;
55    uint64_t traceTags = 0;
56    if (size < sizeof(bufferSz) + sizeof(isRaw) + sizeof(recordCmdRand) + sizeof(traceTags)) {
57        return;
58    }
59    StreamToValueInfo(data, bufferSz);
60    bufferSz += 256; // 256 : min trace buffer size
61    StreamToValueInfo(data, isRaw);
62    StreamToValueInfo(data, recordCmdRand);
63    StreamToValueInfo(data, traceTags);
64    const std::vector<std::string> recordCmdList = {
65        "hitrace --trace_begin",
66        "hitrace --trace_dump",
67        "hitrace --trace_finish",
68        "hitrace --trace_finish_nodump"
69    };
70    std::string hitraceBuffer = " -b " + std::to_string(bufferSz);
71    std::string hitraceTags = " sched freq binder idle disk";
72    GenerateTagStr(traceTags, hitraceTags);
73    std::string outputCmd = " -o /data/local/tmp/HitraceCommonTest_fuzz.ftrace";
74    std::string testCmd = recordCmdList[recordCmdRand % recordCmdList.size()] + hitraceBuffer + hitraceTags + outputCmd;
75    std::cout << testCmd << std::endl;
76    system(testCmd.c_str());
77}
78
79void HitracesSnapshotTest(const uint8_t* data, size_t size)
80{
81    int snapshotCmdRand = 0;
82    if (size < sizeof(snapshotCmdRand)) {
83        return;
84    }
85    StreamToValueInfo(data, snapshotCmdRand);
86    const std::vector<std::string> snapshotCmdList = {
87        "hitrace --start_bgsrv",
88        "hitrace --dump_bgsrv",
89        "hitrace --stop_bgsrv"
90    };
91    system(snapshotCmdList[snapshotCmdRand % snapshotCmdList.size()].c_str());
92}
93} // namespace Hitrace
94} // namespace HiviewDFX
95} // namespace OHOS
96
97/* Fuzzer entry point */
98extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99{
100    if (data == nullptr || size == 0) {
101        return 0;
102    }
103
104    /* Run your code on data */
105    OHOS::HiviewDFX::Hitrace::HitraceCommonTest(data, size);
106    OHOS::HiviewDFX::Hitrace::HitraceRecordTest(data, size);
107    OHOS::HiviewDFX::Hitrace::HitracesSnapshotTest(data, size);
108    return 0;
109}
110