xref: /aosp_15_r20/external/stg/fuzz/elf_reader_fuzzer.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*-
3*9e3b08aeSAndroid Build Coastguard Worker //
4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2021-2022 Google LLC
5*9e3b08aeSAndroid Build Coastguard Worker //
6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the
8*9e3b08aeSAndroid Build Coastguard Worker // License.  You may obtain a copy of the License at
9*9e3b08aeSAndroid Build Coastguard Worker //
10*9e3b08aeSAndroid Build Coastguard Worker //     https://llvm.org/LICENSE.txt
11*9e3b08aeSAndroid Build Coastguard Worker //
12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License.
17*9e3b08aeSAndroid Build Coastguard Worker //
18*9e3b08aeSAndroid Build Coastguard Worker // Author: Matthias Maennich
19*9e3b08aeSAndroid Build Coastguard Worker // Author: Aleksei Vetrov
20*9e3b08aeSAndroid Build Coastguard Worker 
21*9e3b08aeSAndroid Build Coastguard Worker #include <cstddef>
22*9e3b08aeSAndroid Build Coastguard Worker #include <sstream>
23*9e3b08aeSAndroid Build Coastguard Worker #include <vector>
24*9e3b08aeSAndroid Build Coastguard Worker 
25*9e3b08aeSAndroid Build Coastguard Worker #include "elf_dwarf_handle.h"
26*9e3b08aeSAndroid Build Coastguard Worker #include "elf_reader.h"
27*9e3b08aeSAndroid Build Coastguard Worker #include "error.h"
28*9e3b08aeSAndroid Build Coastguard Worker #include "graph.h"
29*9e3b08aeSAndroid Build Coastguard Worker #include "reader_options.h"
30*9e3b08aeSAndroid Build Coastguard Worker #include "runtime.h"
31*9e3b08aeSAndroid Build Coastguard Worker 
LLVMFuzzerTestOneInput(const char * data,size_t size)32*9e3b08aeSAndroid Build Coastguard Worker extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
33*9e3b08aeSAndroid Build Coastguard Worker   try {
34*9e3b08aeSAndroid Build Coastguard Worker     // Fuzzer forbids changing "data", but libdwfl, used in elf::Read, requires
35*9e3b08aeSAndroid Build Coastguard Worker     // read and write access to memory.
36*9e3b08aeSAndroid Build Coastguard Worker     // Luckily, such trivial copy can be easily tracked by fuzzer.
37*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream os;
38*9e3b08aeSAndroid Build Coastguard Worker     stg::Runtime runtime(os, false);
39*9e3b08aeSAndroid Build Coastguard Worker     stg::Graph graph;
40*9e3b08aeSAndroid Build Coastguard Worker     std::vector<char> data_copy(data, data + size);
41*9e3b08aeSAndroid Build Coastguard Worker     stg::ElfDwarfHandle elf_dwarf_handle(data_copy.data(), size);
42*9e3b08aeSAndroid Build Coastguard Worker     stg::elf::Read(runtime, graph, elf_dwarf_handle, stg::ReadOptions(),
43*9e3b08aeSAndroid Build Coastguard Worker                    nullptr);
44*9e3b08aeSAndroid Build Coastguard Worker   } catch (const stg::Exception&) {
45*9e3b08aeSAndroid Build Coastguard Worker     // Pass as this is us catching invalid ELF properly.
46*9e3b08aeSAndroid Build Coastguard Worker   }
47*9e3b08aeSAndroid Build Coastguard Worker   return 0;
48*9e3b08aeSAndroid Build Coastguard Worker }
49