1 /*
2 * Copyright (c) 2024 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 "makereadexec_fuzzer.h"
17 #include "libpandabase/mem/base_mem_stats.h"
18 #include "libpandabase/mem/code_allocator.h"
19 #include "libpandabase/mem/pool_manager.h"
20
21 namespace OHOS {
MakeReadExecFuzzTest(const uint8_t * data,size_t size)22 void MakeReadExecFuzzTest(const uint8_t* data, size_t size)
23 {
24 {
25 // T* is nullptr
26 panda::os::mem::MapRange<uint8_t> tmp(nullptr, 0);
27 tmp.MakeReadExec();
28 }
29
30 {
31 // T* is not nullptr
32 constexpr size_t MB = 1024 * 1024;
33 panda::mem::MemConfig::Initialize(0, 32 * MB, 0, 32 * MB); // internal_size and code_size are set to 32MB
34 panda::PoolManager::Initialize();
35 {
36 panda::BaseMemStats stats;
37 panda::CodeAllocator ca(&stats);
38 uint8_t* buff = const_cast<uint8_t*>(data);
39 ca.AllocateCode(size, static_cast<void*>(buff));
40 panda::os::mem::MapRange<std::byte> map_range = ca.AllocateCodeUnprotected(8U);
41 ca.ProtectCode(map_range);
42 }
43 panda::PoolManager::Finalize();
44 panda::mem::MemConfig::Finalize();
45 }
46 }
47 }
48
49 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)50 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
51 {
52 /* Run your code on data */
53 OHOS::MakeReadExecFuzzTest(data, size);
54 return 0;
55 }