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