• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (!reader->ReadFileHeader()) {
48             printf("head read error\n");
49             (void)fclose(fp);
50             return nullptr;
51         }
52         if (!reader->ReadAttrSection()) {
53             printf("attr read error\n");
54             (void)fclose(fp);
55             return nullptr;
56         }
57         (void)fclose(fp);
58         return reader;
59     };
60 };
61 
FuzzPerfFileReader(const uint8_t * data,size_t size)62 bool FuzzPerfFileReader(const uint8_t *data, size_t size)
63 {
64     const std::string testData = "/data/test/resource/testdata/report_test.data";
65     HLOGV("test data size %zu\n", size);
66     if (size == 0) {
67         return false;
68     }
69     auto reader = PerfFileReaderFuzzer::Instance(testData, data, size);
70     if (reader == nullptr) {
71         printf("test open failed %s\n", testData.c_str());
72         return false;
73     }
74 
75     reader->ReadFeatureSection();
76     auto recordCallback = [](PerfEventRecord& record) {
77         // nothing to do
78         return true;
79     };
80     reader->ReadDataSection(recordCallback);
81     return false;
82 }
83 } // namespace OHOS
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
87 {
88 #ifdef DEBUG_HIPERF_FUZZ
89     OHOS::ScopeDebugLevel mix(OHOS::LEVEL_VERBOSE, true);
90     OHOS::DebugLogger::GetInstance()->Disable(false);
91 #else
92     OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
93 #endif
94 
95     /* Run your code on data */
96     OHOS::FuzzPerfFileReader(data, size);
97     return 0;
98 }
99