• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "ElfParser_fuzzer.h"
17 
18 namespace OHOS {
19 using namespace OHOS::Developtools::HiPerf;
20 using namespace OHOS::Developtools::HiPerf::ELF;
21 class ElfFileFuzzer : public ElfFile {
22 public:
ElfFileFuzzer(const std::string & filename)23     explicit ElfFileFuzzer(const std::string &filename) : ElfFile(filename) {}
24 
MakeUnique(const std::string & filename,const uint8_t * data,size_t size)25     static std::unique_ptr<ElfFileFuzzer> MakeUnique(const std::string &filename,
26                                                      const uint8_t *data, size_t size)
27     {
28         std::unique_ptr<ElfFileFuzzer> file = std::make_unique<ElfFileFuzzer>(filename);
29         if (file == nullptr) {
30             HLOGE("Error in ElfFile::MakeUnique(): ElfFile::ElfFile() failed");
31             return nullptr;
32         }
33         if (!file->IsOpened()) {
34             HLOGE("Error in ElfFile::MakeUnique(): elf file not opened");
35             return nullptr;
36         }
37         if (!file->ParseFile()) {
38             HLOGE("parse elf file failed");
39             return nullptr;
40         }
41         return file;
42     }
43 };
44 
FuzzElfFile(const uint8_t * data,size_t size)45 bool FuzzElfFile(const uint8_t *data, size_t size)
46 {
47     const std::string testData = "/data/test/resource/testdata/elf_test";
48     HLOGV("test data size %zu\n", size);
49     if (size == 0) {
50         return true;
51     }
52     FILE *fp = fopen(testData.c_str(), "ab");
53     if (fp == nullptr) {
54         printf("fail to append file %s\n", testData.c_str());
55         return false;
56     } else {
57         (void)fwrite(data, sizeof(uint8_t), size, fp);
58         (void)fclose(fp);
59     }
60     ElfFileFuzzer::MakeUnique(testData, data, size);
61     return true;
62 }
63 } // namespace OHOS
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68 #ifdef DEBUG_HIPERF_FUZZ
69     OHOS::ScopeDebugLevel mix(OHOS::LEVEL_DEBUG, true);
70     OHOS::DebugLogger::GetInstance()->Disable(false);
71 #else
72     OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
73 #endif
74     /* Run your code on data */
75     OHOS::FuzzElfFile(data, size);
76     return 0;
77 }
78