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 <sstream>
22 #include <vector>
23
24 #include "elf_reader.h"
25 #include "error.h"
26 #include "graph.h"
27 #include "reader_options.h"
28 #include "runtime.h"
29
LLVMFuzzerTestOneInput(char * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(char* data, size_t size) {
31 try {
32 // Fuzzer forbids changing "data", but libdwfl, used in elf::Read, requires
33 // read and write access to memory.
34 // Luckily, such trivial copy can be easily tracked by fuzzer.
35 std::ostringstream os;
36 stg::Runtime runtime(os, false);
37 stg::Graph graph;
38 std::vector<char> data_copy(data, data + size);
39 stg::elf::Read(runtime, graph, data_copy.data(), size, stg::ReadOptions(),
40 nullptr);
41 } catch (const stg::Exception&) {
42 // Pass as this is us catching invalid ELF properly.
43 }
44 return 0;
45 }
46