/src/binutils-gdb/binutils/fuzz_addr2line.c
Line | Count | Source |
1 | | /* Copyright 2021 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | /* |
14 | | * We convert addr2line.c into a header file to make convenient for fuzzing. |
15 | | * We do this for several of the binutils applications when creating |
16 | | * the binutils fuzzers. |
17 | | */ |
18 | | #include "fuzz_addr2line.h" |
19 | | |
20 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
21 | | int |
22 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
23 | 10.7k | { |
24 | 10.7k | if (size > 16384) |
25 | 9 | return 0; |
26 | 10.7k | char filename[256]; |
27 | 10.7k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
28 | 10.7k | FILE *fp = fopen(filename, "wb"); |
29 | 10.7k | if (!fp) { |
30 | 0 | return 0; |
31 | 0 | } |
32 | 10.7k | fwrite(data, size, 1, fp); |
33 | 10.7k | fclose(fp); |
34 | | |
35 | 10.7k | program_name = filename; |
36 | | |
37 | 10.7k | char **c2 = xmalloc(sizeof(char*)*6); |
38 | 10.7k | c2[0] = xstrdup("AAABC"); |
39 | 10.7k | c2[1] = xstrdup("BBC"); |
40 | 10.7k | c2[2] = xstrdup("0xbeefbeef"); |
41 | 10.7k | c2[3] = xstrdup("0xcafebabe"); |
42 | 10.7k | c2[4] = xstrdup("5123423"); |
43 | 10.7k | c2[5] = NULL; |
44 | | |
45 | 10.7k | naddr = 5; |
46 | 10.7k | addr = c2; |
47 | | |
48 | | // Main fuzz entrypoint in addr2line.c |
49 | 10.7k | process_file(filename, NULL, NULL); |
50 | | |
51 | 64.4k | for (int i = 5; --i >= 0; ) |
52 | 53.6k | free(c2[i]); |
53 | 10.7k | free(c2); |
54 | | |
55 | 10.7k | unlink(filename); |
56 | 10.7k | return 0; |
57 | 10.7k | } |