/src/upx/fuzzers/test_packed_file_fuzzer.cpp
Line | Count | Source |
1 | | /* Copyright 2023 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 | | #include <stddef.h> |
14 | | #include <stdint.h> |
15 | | #include <stdio.h> |
16 | | |
17 | | #include "../src/conf.h" |
18 | | #include "../src/file.h" |
19 | | #include "../src/packmast.h" |
20 | | |
21 | | enum OpenMode { RO_MUST_EXIST, WO_MUST_EXIST_TRUNCATE, WO_MUST_CREATE, WO_CREATE_OR_TRUNCATE }; |
22 | | |
23 | 42.6k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
24 | 42.6k | char infilename[256]; |
25 | 42.6k | char outfilename[256]; |
26 | 42.6k | snprintf(infilename, 256, "/tmp/libfuzzer.%d", getpid()); |
27 | 42.6k | snprintf(outfilename, 256, "/tmp/libfuzzer.%d.decompressed", getpid()); |
28 | | |
29 | 42.6k | FILE *fp = fopen(infilename, "wb"); |
30 | 42.6k | if (!fp) { |
31 | 0 | return 0; |
32 | 0 | } |
33 | 42.6k | fwrite(data, size, 1, fp); |
34 | 42.6k | fclose(fp); |
35 | | |
36 | 42.6k | char argv_progname[4] = "upx"; |
37 | 42.6k | char argv_test_file[3] = "-t"; |
38 | | |
39 | 42.6k | char* argv_data[] = {argv_progname, argv_test_file, infilename}; |
40 | | |
41 | 42.6k | try { |
42 | 42.6k | upx_main(3, argv_data); |
43 | 42.6k | } catch(...) { |
44 | 0 | } |
45 | | |
46 | 42.6k | unlink(infilename); |
47 | 42.6k | unlink(outfilename); |
48 | 42.6k | return 0; |
49 | 42.6k | } |