1 /*
2 * Copyright (c) 2021-2022 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 "PerfFile_fuzzer.h"
17 #include "utilities.h"
18
19 namespace OHOS {
20 using namespace OHOS::Developtools::HiPerf;
21 class PerfFileReaderFuzzer : public PerfFileReader {
22 public:
PerfFileReaderFuzzer(const std::string & fileName,FILE * fp)23 explicit PerfFileReaderFuzzer(const std::string &fileName, FILE *fp)
24 : PerfFileReader(fileName, fp) {}
25
Instance(const std::string & fileName,const uint8_t * data,size_t size)26 static std::unique_ptr<PerfFileReaderFuzzer> Instance(const std::string &fileName,
27 const uint8_t *data, size_t size)
28 {
29 std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str());
30 FILE *fpApp = fopen(resolvedPath.c_str(), "ab");
31 if (fpApp == nullptr) {
32 printf("fail to append file %s\n", fileName.c_str());
33 return nullptr;
34 } else {
35 (void)fwrite(data, sizeof(uint8_t), size, fpApp);
36 (void)fclose(fpApp);
37 fpApp = nullptr;
38 }
39 FILE *fp = fopen(resolvedPath.c_str(), "rb");
40 if (fp == nullptr) {
41 printf("fail to open file %s\n", fileName.c_str());
42 return nullptr;
43 }
44
45 std::unique_ptr<PerfFileReaderFuzzer> reader =
46 std::make_unique<PerfFileReaderFuzzer>(resolvedPath, fp);
47
48 if (!reader->ReadFileHeader()) {
49 printf("head read error\n");
50 return nullptr;
51 }
52 if (!reader->ReadAttrSection()) {
53 printf("attr read error\n");
54 return nullptr;
55 }
56 return reader;
57 };
58 };
59
FuzzPerfFileReader(const uint8_t * data,size_t size)60 bool FuzzPerfFileReader(const uint8_t *data, size_t size)
61 {
62 const std::string testData = "/data/test/resource/testdata/report_test.data";
63 HLOGV("test data size %zu\n", size);
64 if (size == 0) {
65 return false;
66 }
67 auto reader = PerfFileReaderFuzzer::Instance(testData, data, size);
68 if (reader == nullptr) {
69 printf("test open failed %s\n", testData.c_str());
70 return false;
71 }
72
73 reader->ReadFeatureSection();
74 auto recordCallback = [&](const std::unique_ptr<PerfEventRecord> &record) {
75 // nothing to do
76 return true;
77 };
78 reader->ReadDataSection(recordCallback);
79 return false;
80 }
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86 #ifdef DEBUG_HIPERF_FUZZ
87 OHOS::ScopeDebugLevel mix(OHOS::LEVEL_VERBOSE, true);
88 OHOS::DebugLogger::GetInstance()->Disable(false);
89 #else
90 OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
91 #endif
92
93 /* Run your code on data */
94 OHOS::FuzzPerfFileReader(data, size);
95 return 0;
96 }
97