/src/binutils-gdb/binutils/fuzz_ranlib_simulation.c
Line | Count | Source (jump to first uncovered line) |
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 | | * The goal of this fuzzer is to simulate the logic for ranlib. We implement the |
15 | | * fuzzer without calling ar.c itself in order to avoid bfd_fatal calls. |
16 | | */ |
17 | | #include "sysdep.h" |
18 | | #include "bfd.h" |
19 | | #include "libiberty.h" |
20 | | #include "getopt.h" |
21 | | #include "aout/ar.h" |
22 | | #include "bucomm.h" |
23 | | #include "arsup.h" |
24 | | #include "filenames.h" |
25 | | #include "binemul.h" |
26 | | #include "plugin-api.h" |
27 | | #include "plugin.h" |
28 | | #include "ansidecl.h" |
29 | | |
30 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
31 | | int |
32 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
33 | 24.4k | { |
34 | 24.4k | char filename[256]; |
35 | 24.4k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
36 | 24.4k | FILE *fp = fopen(filename, "wb"); |
37 | 24.4k | if (!fp) { |
38 | 0 | return 0; |
39 | 0 | } |
40 | 24.4k | fwrite(data, size, 1, fp); |
41 | 24.4k | fclose(fp); |
42 | | |
43 | 24.4k | int f; |
44 | 24.4k | bfd *arch; |
45 | | |
46 | 24.4k | f = open (filename, O_RDWR | O_BINARY, 0); |
47 | 24.4k | if (f < 0) { |
48 | 0 | return 0; |
49 | 0 | } |
50 | | |
51 | 24.4k | arch = bfd_fdopenr (filename, (const char *) NULL, f); |
52 | 24.4k | if (arch == NULL) { |
53 | 0 | close(f); |
54 | 0 | return 0; |
55 | 0 | } |
56 | 24.4k | if (! bfd_check_format (arch, bfd_archive)) { |
57 | 1.96k | bfd_close(arch); |
58 | 1.96k | return 0; |
59 | 1.96k | } |
60 | | |
61 | 22.4k | if (! bfd_has_map (arch)) { |
62 | 1.91k | bfd_close(arch); |
63 | 1.91k | return 0; |
64 | 1.91k | } |
65 | | |
66 | 20.5k | bfd_is_thin_archive (arch); |
67 | | |
68 | 20.5k | bfd_update_armap_timestamp(arch); |
69 | 20.5k | bfd_close(arch); |
70 | | |
71 | 20.5k | unlink(filename); |
72 | 20.5k | return 0; |
73 | 22.4k | } |