• 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 "mergeapfiles_fuzzer.h"
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
19 
20 using namespace panda;
21 using PGOProfilerManager = panda::ecmascript::pgo::PGOProfilerManager;
22 using ApGenMode = panda::ecmascript::pgo::ApGenMode;
23 
24 namespace OHOS {
25 constexpr size_t MIN_FUZZ_INPUT = 8;
26 
MergeApFilesFuzzTest(const uint8_t * data,size_t size)27 bool MergeApFilesFuzzTest(const uint8_t *data, size_t size)
28 {
29     if (size < MIN_FUZZ_INPUT) {
30         return false;
31     }
32 
33     FuzzedDataProvider provider(data, size);
34 
35     std::string inFiles = "/tmp/fuzz_test_loadfull.ap";
36 
37     // write file
38     std::ofstream outFile(inFiles, std::ios::binary);
39     if (outFile.is_open()) {
40         outFile.write(reinterpret_cast<const char *>(data), size);
41         outFile.close();
42     } else {
43         return false;
44     }
45 
46     std::string outPath = "/tmp/fuzz_test" + std::to_string(provider.ConsumeIntegral<uint32_t>());
47 
48     // generat random hotnessThreshold and ApGenMode
49     uint32_t hotnessThreshold = provider.ConsumeIntegral<uint32_t>();
50     ApGenMode mode = static_cast<ApGenMode>(provider.ConsumeIntegralInRange<int>(0, 1));
51 
52     bool result = PGOProfilerManager::GetInstance()->MergeApFiles(inFiles, outPath, hotnessThreshold, mode);
53 
54     std::remove(outPath.c_str());
55 
56     return result;
57 }
58 } // namespace OHOS
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62 {
63     OHOS::MergeApFilesFuzzTest(data, size);
64     return 0;
65 }