Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2022 Google LLC. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <libgen.h> |
16 | | #include <stddef.h> |
17 | | #include <stdint.h> |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <string> |
21 | | #include <fcntl.h> |
22 | | |
23 | | #include "fuzzer_temp_file.h" |
24 | | |
25 | | #include <magic.h> |
26 | | |
27 | | struct Environment { |
28 | 2 | Environment(std::string data_dir) { |
29 | 2 | magic = magic_open(MAGIC_COMPRESS|MAGIC_CONTINUE|MAGIC_NO_COMPRESS_FORK); |
30 | 2 | std::string magic_path = data_dir + "/magic"; |
31 | 2 | if (magic_load(magic, magic_path.c_str())) { |
32 | 0 | fprintf(stderr, "error loading magic file: %s\n", magic_error(magic)); |
33 | 0 | exit(1); |
34 | 0 | } |
35 | 2 | } |
36 | | |
37 | | magic_t magic; |
38 | | }; |
39 | | |
40 | | static Environment* env; |
41 | | |
42 | 2 | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { |
43 | 2 | char* exe_path = (*argv)[0]; |
44 | | // dirname() can modify its argument. |
45 | 2 | char* exe_path_copy = strdup(exe_path); |
46 | 2 | char* dir = dirname(exe_path_copy); |
47 | 2 | env = new Environment(dir); |
48 | 2 | free(exe_path_copy); |
49 | 2 | return 0; |
50 | 2 | } |
51 | | |
52 | 8.80k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
53 | 8.80k | FuzzerTemporaryFile ftf (data, size); |
54 | 8.80k | int fd = open(ftf.filename(), O_RDONLY); |
55 | 8.80k | magic_descriptor(env->magic, fd); |
56 | 8.80k | close(fd); |
57 | 8.80k | return 0; |
58 | 8.80k | } |