| 1 |
// Copyright 2018 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 *fuzzer_get_tmpfile(const uint8_t *data, size_t size) |
| 30 |
{ |
| 31 |
char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); |
| 32 |
if (!filename_buffer) { |
| 33 |
perror("Failed to allocate file name buffer."); |
| 34 |
abort(); |
| 35 |
} |
| 36 |
const int file_descriptor = mkstemp(filename_buffer); |
| 37 |
if (file_descriptor < 0) { |
| 38 |
perror("Failed to make temporary file."); |
| 39 |
abort(); |
| 40 |
} |
| 41 |
FILE *file = fdopen(file_descriptor, "wb"); |
| 42 |
if (!file) { |
| 43 |
perror("Failed to open file descriptor."); |
| 44 |
close(file_descriptor); |
| 45 |
abort(); |
| 46 |
} |
| 47 |
const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); |
| 48 |
if (bytes_written < size) { |
| 49 |
close(file_descriptor); |
| 50 |
fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); |
| 51 |
abort(); |
| 52 |
} |
| 53 |
fclose(file); |
| 54 |
return filename_buffer; |
| 55 |
} |
| 56 |
|
| 57 |
static void fuzzer_release_tmpfile(char *filename) |
| 58 |
{ |
| 59 |
if (unlink(filename) != 0) { |
| 60 |
perror("WARNING: Failed to delete temporary file."); |
| 61 |
} |
| 62 |
free(filename); |
| 63 |
} |
| 64 |
|
| 65 |
// C++ RAII object for creating temporary files. |
| 66 |
|
| 67 |
#ifdef __cplusplus |
| 68 |
class FuzzerTemporaryFile |
| 69 |
{ |
| 70 |
public: |
| 71 |
FuzzerTemporaryFile(const uint8_t *data, size_t size) : filename_(fuzzer_get_tmpfile(data, size)) { } |
| 72 |
|
| 73 |
~FuzzerTemporaryFile() { fuzzer_release_tmpfile(filename_); } |
| 74 |
|
| 75 |
const char *filename() const { return filename_; } |
| 76 |
|
| 77 |
private: |
| 78 |
char *filename_; |
| 79 |
}; |
| 80 |
#endif |
| 81 |
|
| 82 |
#endif // FUZZER_TEMP_FILE_H_ |