xref: /aosp_15_r20/system/unwinding/libunwindstack/tests/fuzz/UnwinderComponentCreator.h (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <elf.h>
20 #include <sys/mman.h>
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <fuzzer/FuzzedDataProvider.h>
27 #include <unwindstack/DexFiles.h>
28 #include <unwindstack/Maps.h>
29 #include <unwindstack/Regs.h>
30 #include <unwindstack/RegsArm.h>
31 #include <unwindstack/RegsArm64.h>
32 #include <unwindstack/RegsRiscv64.h>
33 #include <unwindstack/RegsX86.h>
34 #include <unwindstack/RegsX86_64.h>
35 
36 #include "../ElfFake.h"
37 #include "utils/MemoryFake.h"
38 
39 #include "fuzzer/FuzzedDataProvider.h"
40 
41 using unwindstack::ArchEnum;
42 using unwindstack::DexFiles;
43 using unwindstack::Elf;
44 using unwindstack::ElfFake;
45 using unwindstack::ElfInterfaceFake;
46 using unwindstack::FunctionData;
47 using unwindstack::Maps;
48 using unwindstack::Memory;
49 using unwindstack::MemoryFake;
50 using unwindstack::Regs;
51 using unwindstack::StepData;
52 
53 static constexpr uint8_t kArchCount = 5;
54 
55 static constexpr uint8_t kMaxSoNameLen = 150;
56 
57 static constexpr uint8_t kMaxFuncNameLen = 50;
58 static constexpr uint8_t kMaxFuncCount = 100;
59 
60 static constexpr uint8_t kMaxJitElfFiles = 20;
61 static constexpr uint8_t kJitElfPadding = 32;
62 
63 static constexpr uint8_t kMaxStepCount = 100;
64 static constexpr uint8_t kMaxMapEntryCount = 50;
65 static constexpr uint8_t kMaxBuildIdLen = 100;
66 static constexpr uint8_t kMaxMapInfoNameLen = 150;
67 
68 std::unique_ptr<unwindstack::Regs> GetRegisters(unwindstack::ArchEnum arch);
69 std::unique_ptr<unwindstack::Maps> GetMaps(FuzzedDataProvider* data_provider);
70 std::vector<std::string> GetStringList(FuzzedDataProvider* data_provider, uint max_str_len,
71                                        uint max_strings);
72 unwindstack::ArchEnum GetArch(FuzzedDataProvider* data_provider);
73 
74 void AddMapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const char* name,
75                 Elf* elf = nullptr);
76 void PutElfFilesInMemory(MemoryFake* memory, FuzzedDataProvider* data_provider);
77 
78 std::unique_ptr<unwindstack::DexFiles> GetDexFiles(FuzzedDataProvider* data_provider,
79                                                    std::shared_ptr<unwindstack::Memory> memory,
80                                                    uint max_libraries, uint max_library_length,
81                                                    unwindstack::ArchEnum arch);
82