1b0e7dd80Sopenharmony_ci/*
2b0e7dd80Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b0e7dd80Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b0e7dd80Sopenharmony_ci * you may not use this file except in compliance with the License.
5b0e7dd80Sopenharmony_ci * You may obtain a copy of the License at
6b0e7dd80Sopenharmony_ci *
7b0e7dd80Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b0e7dd80Sopenharmony_ci *
9b0e7dd80Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b0e7dd80Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b0e7dd80Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b0e7dd80Sopenharmony_ci * See the License for the specific language governing permissions and
13b0e7dd80Sopenharmony_ci * limitations under the License.
14b0e7dd80Sopenharmony_ci */
15b0e7dd80Sopenharmony_ci
16b0e7dd80Sopenharmony_ci#include "hitrace_fuzzer.h"
17b0e7dd80Sopenharmony_ci
18b0e7dd80Sopenharmony_ci#include <cstddef>
19b0e7dd80Sopenharmony_ci#include <cstdint>
20b0e7dd80Sopenharmony_ci#include <string>
21b0e7dd80Sopenharmony_ci#include <iostream>
22b0e7dd80Sopenharmony_ci#include <vector>
23b0e7dd80Sopenharmony_ci
24b0e7dd80Sopenharmony_ci#include "hitrace_fuzztest_common.h"
25b0e7dd80Sopenharmony_ci
26b0e7dd80Sopenharmony_cinamespace OHOS {
27b0e7dd80Sopenharmony_cinamespace HiviewDFX {
28b0e7dd80Sopenharmony_cinamespace Hitrace {
29b0e7dd80Sopenharmony_civoid HitraceCommonTest(const uint8_t* data, size_t size)
30b0e7dd80Sopenharmony_ci{
31b0e7dd80Sopenharmony_ci    int bufferSz = 0;
32b0e7dd80Sopenharmony_ci    uint32_t duration = 0;
33b0e7dd80Sopenharmony_ci    uint64_t traceTags = 0;
34b0e7dd80Sopenharmony_ci    if (size < sizeof(bufferSz) + sizeof(duration) + sizeof(traceTags)) {
35b0e7dd80Sopenharmony_ci        return;
36b0e7dd80Sopenharmony_ci    }
37b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, bufferSz);
38b0e7dd80Sopenharmony_ci    bufferSz += 256; // 256 : min trace buffer size
39b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, duration);
40b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, traceTags);
41b0e7dd80Sopenharmony_ci    duration = duration > 10 ? 10 : duration; // 10 : max duration
42b0e7dd80Sopenharmony_ci    std::string hitraceCmd = "hitrace -b " + std::to_string(bufferSz) + " -t " + std::to_string(duration);
43b0e7dd80Sopenharmony_ci    std::string hitraceTags = " sched freq binder idle disk";
44b0e7dd80Sopenharmony_ci    GenerateTagStr(traceTags, hitraceTags);
45b0e7dd80Sopenharmony_ci    std::string outputCmd = " -o /data/local/tmp/HitraceCommonTest_fuzz.ftrace";
46b0e7dd80Sopenharmony_ci    std::cout << hitraceCmd << hitraceTags << outputCmd << std::endl;
47b0e7dd80Sopenharmony_ci    system((hitraceCmd + hitraceTags + outputCmd).c_str());
48b0e7dd80Sopenharmony_ci}
49b0e7dd80Sopenharmony_ci
50b0e7dd80Sopenharmony_civoid HitraceRecordTest(const uint8_t* data, size_t size)
51b0e7dd80Sopenharmony_ci{
52b0e7dd80Sopenharmony_ci    int bufferSz = 0;
53b0e7dd80Sopenharmony_ci    bool isRaw = false;
54b0e7dd80Sopenharmony_ci    int recordCmdRand = 0;
55b0e7dd80Sopenharmony_ci    uint64_t traceTags = 0;
56b0e7dd80Sopenharmony_ci    if (size < sizeof(bufferSz) + sizeof(isRaw) + sizeof(recordCmdRand) + sizeof(traceTags)) {
57b0e7dd80Sopenharmony_ci        return;
58b0e7dd80Sopenharmony_ci    }
59b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, bufferSz);
60b0e7dd80Sopenharmony_ci    bufferSz += 256; // 256 : min trace buffer size
61b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, isRaw);
62b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, recordCmdRand);
63b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, traceTags);
64b0e7dd80Sopenharmony_ci    const std::vector<std::string> recordCmdList = {
65b0e7dd80Sopenharmony_ci        "hitrace --trace_begin",
66b0e7dd80Sopenharmony_ci        "hitrace --trace_dump",
67b0e7dd80Sopenharmony_ci        "hitrace --trace_finish",
68b0e7dd80Sopenharmony_ci        "hitrace --trace_finish_nodump"
69b0e7dd80Sopenharmony_ci    };
70b0e7dd80Sopenharmony_ci    std::string hitraceBuffer = " -b " + std::to_string(bufferSz);
71b0e7dd80Sopenharmony_ci    std::string hitraceTags = " sched freq binder idle disk";
72b0e7dd80Sopenharmony_ci    GenerateTagStr(traceTags, hitraceTags);
73b0e7dd80Sopenharmony_ci    std::string outputCmd = " -o /data/local/tmp/HitraceCommonTest_fuzz.ftrace";
74b0e7dd80Sopenharmony_ci    std::string testCmd = recordCmdList[recordCmdRand % recordCmdList.size()] + hitraceBuffer + hitraceTags + outputCmd;
75b0e7dd80Sopenharmony_ci    std::cout << testCmd << std::endl;
76b0e7dd80Sopenharmony_ci    system(testCmd.c_str());
77b0e7dd80Sopenharmony_ci}
78b0e7dd80Sopenharmony_ci
79b0e7dd80Sopenharmony_civoid HitracesSnapshotTest(const uint8_t* data, size_t size)
80b0e7dd80Sopenharmony_ci{
81b0e7dd80Sopenharmony_ci    int snapshotCmdRand = 0;
82b0e7dd80Sopenharmony_ci    if (size < sizeof(snapshotCmdRand)) {
83b0e7dd80Sopenharmony_ci        return;
84b0e7dd80Sopenharmony_ci    }
85b0e7dd80Sopenharmony_ci    StreamToValueInfo(data, snapshotCmdRand);
86b0e7dd80Sopenharmony_ci    const std::vector<std::string> snapshotCmdList = {
87b0e7dd80Sopenharmony_ci        "hitrace --start_bgsrv",
88b0e7dd80Sopenharmony_ci        "hitrace --dump_bgsrv",
89b0e7dd80Sopenharmony_ci        "hitrace --stop_bgsrv"
90b0e7dd80Sopenharmony_ci    };
91b0e7dd80Sopenharmony_ci    system(snapshotCmdList[snapshotCmdRand % snapshotCmdList.size()].c_str());
92b0e7dd80Sopenharmony_ci}
93b0e7dd80Sopenharmony_ci} // namespace Hitrace
94b0e7dd80Sopenharmony_ci} // namespace HiviewDFX
95b0e7dd80Sopenharmony_ci} // namespace OHOS
96b0e7dd80Sopenharmony_ci
97b0e7dd80Sopenharmony_ci/* Fuzzer entry point */
98b0e7dd80Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99b0e7dd80Sopenharmony_ci{
100b0e7dd80Sopenharmony_ci    if (data == nullptr || size == 0) {
101b0e7dd80Sopenharmony_ci        return 0;
102b0e7dd80Sopenharmony_ci    }
103b0e7dd80Sopenharmony_ci
104b0e7dd80Sopenharmony_ci    /* Run your code on data */
105b0e7dd80Sopenharmony_ci    OHOS::HiviewDFX::Hitrace::HitraceCommonTest(data, size);
106b0e7dd80Sopenharmony_ci    OHOS::HiviewDFX::Hitrace::HitraceRecordTest(data, size);
107b0e7dd80Sopenharmony_ci    OHOS::HiviewDFX::Hitrace::HitracesSnapshotTest(data, size);
108b0e7dd80Sopenharmony_ci    return 0;
109b0e7dd80Sopenharmony_ci}
110