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:
23 const char *dataPtr_ = nullptr;
24 size_t dataSize_ = 0;
25 size_t FuzzerTime_ = 0; // when we make a fuzzer read
26
ReadFile(void * buf,size_t len)27 ssize_t ReadFile(void *buf, size_t len) override
28 {
29 if (FuzzerTime_ != 0 or dataSize_ == 0) {
30 FuzzerTime_--;
31 return ElfFile::ReadFile(buf, len);
32 } else {
33 HLOGV("fuzz read %zu/%zu\n", dataSize_, len);
34 if (ElfFile::ReadFile(buf, len) != 0) {
35 std::copy(dataPtr_, dataPtr_ + std::min(len, dataSize_),
36 reinterpret_cast<char *>(buf));
37 }
38 return len;
39 }
40 }
41
ElfFileFuzzer(const std::string & filename)42 explicit ElfFileFuzzer(const std::string &filename) : ElfFile(filename) {}
43
MakeUnique(const std::string & filename,const uint8_t * data,size_t size)44 static std::unique_ptr<ElfFileFuzzer> MakeUnique(const std::string &filename,
45 const uint8_t *data, size_t size)
46 {
47 std::unique_ptr<ElfFileFuzzer> file = std::make_unique<ElfFileFuzzer>(filename);
48 if (file == nullptr) {
49 HLOGE("Error in ElfFile::MakeUnique(): ElfFile::ElfFile() failed");
50 return nullptr;
51 }
52 file->dataPtr_ = reinterpret_cast<const char *>(data);
53 file->dataSize_ = size;
54 file->FuzzerTime_ = size;
55 if (!file->IsOpened()) {
56 HLOGE("Error in ElfFile::MakeUnique(): elf file not opended");
57 return nullptr;
58 }
59 if (!file->ParseFile()) {
60 HLOGE("parse elf file failed");
61 return nullptr;
62 }
63 return file;
64 }
65 };
66
FuzzElfFile(const uint8_t * data,size_t size)67 bool FuzzElfFile(const uint8_t *data, size_t size)
68 {
69 const std::string testData = "/data/test/resource/testdata/elf_test";
70 HLOGV("test data size %zu\n", size);
71 if (size == 0) {
72 return 0;
73 }
74 ElfFileFuzzer::MakeUnique(testData, data, size);
75 return 0;
76 }
77 } // namespace OHOS
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
81 {
82 #ifdef DEBUG_HIPERF_FUZZ
83 ScopeDebugLevel mix(LEVEL_DEBUG, true);
84 DebugLogger::GetInstance()->Disable(false);
85 #else
86 OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
87 #endif
88 /* Run your code on data */
89 OHOS::FuzzElfFile(data, size);
90 return 0;
91 }
92