• 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 "mergeapfilesformerger_fuzzer.h"
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
19 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h"
20 #include "ecmascript/mem/c_string.h"
21 
22 #include <unordered_map>
23 #include <string>
24 
25 using namespace panda;
26 using namespace panda::ecmascript::pgo;
27 using panda::ecmascript::CString;
28 
29 namespace OHOS {
30 constexpr size_t MIN_FUZZ_INPUT = 8;
31 constexpr uint32_t FAKE_ABC_CHECKSUM_FOR_FUZZ = 123456;
32 
MergeApFilesForMergerFuzzTest(const uint8_t * data,size_t size)33 void MergeApFilesForMergerFuzzTest(const uint8_t *data, size_t size)
34 {
35     if (size < MIN_FUZZ_INPUT) {
36         return;
37     }
38     // generate random file path
39     std::string randomFilePath = "/tmp/fuzz_test_loadfull.profiler";
40 
41     // write file
42     std::ofstream outFile(randomFilePath, std::ios::binary);
43     if (outFile.is_open()) {
44         outFile.write(reinterpret_cast<const char *>(data), size);
45         outFile.close();
46     } else {
47         return;
48     }
49 
50     uint32_t hotnessThreshold = 0;
51 
52     PGOProfilerDecoder decoder(randomFilePath, hotnessThreshold);
53 
54     std::unordered_map<CString, uint32_t> fileNameToChecksumMap;
55     fileNameToChecksumMap["dummy.abc"] = FAKE_ABC_CHECKSUM_FOR_FUZZ;
56 
57     PGOProfilerManager::GetInstance()->MergeApFiles(fileNameToChecksumMap, decoder);
58 
59     std::remove(randomFilePath.c_str());
60 
61     return;
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     OHOS::MergeApFilesForMergerFuzzTest(data, size);
69     return 0;
70 }
71