/src/matio/ossfuzz/fuzzer_temp_file.h
Line | Count | Source |
1 | | // Copyright 2019 Google Inc. |
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 | | // Adapter utility from fuzzer input to a temporary file, for fuzzing APIs that |
16 | | // require a file instead of an input buffer. |
17 | | |
18 | | #ifndef FUZZER_TEMP_FILE_H_ |
19 | | #define FUZZER_TEMP_FILE_H_ |
20 | | |
21 | | #include <stdint.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | // Pure-C interface for creating and cleaning up temporary files. |
28 | | |
29 | | static char * |
30 | | fuzzer_get_tmpfile(const uint8_t *data, size_t size) |
31 | 8.98k | { |
32 | 8.98k | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); |
33 | 8.98k | if ( !filename_buffer ) { |
34 | 0 | perror("Failed to allocate file name buffer."); |
35 | 0 | abort(); |
36 | 0 | } |
37 | 8.98k | const int file_descriptor = mkstemp(filename_buffer); |
38 | 8.98k | if ( file_descriptor < 0 ) { |
39 | 0 | perror("Failed to make temporary file."); |
40 | 0 | abort(); |
41 | 0 | } |
42 | 8.98k | FILE *file = fdopen(file_descriptor, "wb"); |
43 | 8.98k | if ( !file ) { |
44 | 0 | perror("Failed to open file descriptor."); |
45 | 0 | close(file_descriptor); |
46 | 0 | abort(); |
47 | 0 | } |
48 | 8.98k | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); |
49 | 8.98k | if ( bytes_written < size ) { |
50 | 0 | close(file_descriptor); |
51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); |
52 | 0 | abort(); |
53 | 0 | } |
54 | 8.98k | fclose(file); |
55 | 8.98k | return filename_buffer; |
56 | 8.98k | } matio_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 771 | { | 32 | 771 | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 771 | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 771 | const int file_descriptor = mkstemp(filename_buffer); | 38 | 771 | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 771 | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 771 | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 771 | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 771 | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 771 | fclose(file); | 55 | 771 | return filename_buffer; | 56 | 771 | } |
matio_read_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 1.78k | { | 32 | 1.78k | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 1.78k | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 1.78k | const int file_descriptor = mkstemp(filename_buffer); | 38 | 1.78k | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 1.78k | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 1.78k | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 1.78k | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 1.78k | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 1.78k | fclose(file); | 55 | 1.78k | return filename_buffer; | 56 | 1.78k | } |
matio_readdataall_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 145 | { | 32 | 145 | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 145 | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 145 | const int file_descriptor = mkstemp(filename_buffer); | 38 | 145 | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 145 | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 145 | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 145 | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 145 | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 145 | fclose(file); | 55 | 145 | return filename_buffer; | 56 | 145 | } |
matio_hyperslab_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 5.43k | { | 32 | 5.43k | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 5.43k | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 5.43k | const int file_descriptor = mkstemp(filename_buffer); | 38 | 5.43k | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 5.43k | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 5.43k | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 5.43k | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 5.43k | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 5.43k | fclose(file); | 55 | 5.43k | return filename_buffer; | 56 | 5.43k | } |
matio_readnext_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 62 | { | 32 | 62 | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 62 | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 62 | const int file_descriptor = mkstemp(filename_buffer); | 38 | 62 | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 62 | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 62 | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 62 | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 62 | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 62 | fclose(file); | 55 | 62 | return filename_buffer; | 56 | 62 | } |
matio_struct_cell_fuzzer.cpp:fuzzer_get_tmpfile(unsigned char const*, unsigned long) Line | Count | Source | 31 | 789 | { | 32 | 789 | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); | 33 | 789 | if ( !filename_buffer ) { | 34 | 0 | perror("Failed to allocate file name buffer."); | 35 | 0 | abort(); | 36 | 0 | } | 37 | 789 | const int file_descriptor = mkstemp(filename_buffer); | 38 | 789 | if ( file_descriptor < 0 ) { | 39 | 0 | perror("Failed to make temporary file."); | 40 | 0 | abort(); | 41 | 0 | } | 42 | 789 | FILE *file = fdopen(file_descriptor, "wb"); | 43 | 789 | if ( !file ) { | 44 | 0 | perror("Failed to open file descriptor."); | 45 | 0 | close(file_descriptor); | 46 | 0 | abort(); | 47 | 0 | } | 48 | 789 | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); | 49 | 789 | if ( bytes_written < size ) { | 50 | 0 | close(file_descriptor); | 51 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); | 52 | 0 | abort(); | 53 | 0 | } | 54 | 789 | fclose(file); | 55 | 789 | return filename_buffer; | 56 | 789 | } |
|
57 | | |
58 | | static void |
59 | | fuzzer_release_tmpfile(char *filename) |
60 | 8.98k | { |
61 | 8.98k | if ( unlink(filename) != 0 ) { |
62 | 0 | perror("WARNING: Failed to delete temporary file."); |
63 | 0 | } |
64 | 8.98k | free(filename); |
65 | 8.98k | } matio_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 771 | { | 61 | 771 | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 771 | free(filename); | 65 | 771 | } |
matio_read_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 1.78k | { | 61 | 1.78k | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 1.78k | free(filename); | 65 | 1.78k | } |
matio_readdataall_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 145 | { | 61 | 145 | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 145 | free(filename); | 65 | 145 | } |
matio_hyperslab_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 5.43k | { | 61 | 5.43k | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 5.43k | free(filename); | 65 | 5.43k | } |
matio_readnext_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 62 | { | 61 | 62 | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 62 | free(filename); | 65 | 62 | } |
matio_struct_cell_fuzzer.cpp:fuzzer_release_tmpfile(char*) Line | Count | Source | 60 | 789 | { | 61 | 789 | if ( unlink(filename) != 0 ) { | 62 | 0 | perror("WARNING: Failed to delete temporary file."); | 63 | 0 | } | 64 | 789 | free(filename); | 65 | 789 | } |
|
66 | | |
67 | | // C++ RAII object for creating temporary files. |
68 | | |
69 | | #ifdef __cplusplus |
70 | | class FuzzerTemporaryFile |
71 | | { |
72 | | public: |
73 | | FuzzerTemporaryFile(const uint8_t *data, size_t size) |
74 | 8.98k | : filename_(fuzzer_get_tmpfile(data, size)) |
75 | 8.98k | { |
76 | 8.98k | } |
77 | | |
78 | | ~FuzzerTemporaryFile() |
79 | 8.98k | { |
80 | 8.98k | fuzzer_release_tmpfile(filename_); |
81 | 8.98k | } |
82 | | |
83 | | const char * |
84 | | filename() const |
85 | 8.98k | { |
86 | 8.98k | return filename_; |
87 | 8.98k | } |
88 | | |
89 | | private: |
90 | | char *filename_; |
91 | | }; |
92 | | #endif |
93 | | |
94 | | #endif // FUZZER_TEMP_FILE_H_ |