aboutsummaryrefslogtreecommitdiff
path: root/tests/fuzzer/fuzzer_temp_file.h
blob: 5d12bbea6253591c18b8c405a3bd6cacede7ef20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright 2020 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LIBGAV1_TESTS_FUZZER_FUZZER_TEMP_FILE_H_
#define LIBGAV1_TESTS_FUZZER_FUZZER_TEMP_FILE_H_

// Adapter utility from fuzzer input to a temporary file, for fuzzing APIs that
// require a file instead of an input buffer.

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// Pure-C interface for creating and cleaning up temporary files.

static char* fuzzer_get_tmpfile_with_suffix(const uint8_t* data, size_t size,
                                            const char* suffix) {
  if (suffix == NULL) {  // NOLINT (this could be a C compilation unit)
    suffix = "";
  }
  const size_t suffix_len = strlen(suffix);
  if (suffix_len > INT_MAX) {  // mkstemps takes int for suffixlen param
    perror("Suffix too long");
    abort();
  }

#ifdef __ANDROID__
  const char* leading_temp_path =
      "/data/local/tmp/generate_temporary_file.XXXXXX";
#else
  const char* leading_temp_path = "/tmp/generate_temporary_file.XXXXXX";
#endif
  const size_t buffer_sz = strlen(leading_temp_path) + suffix_len + 1;
  char* filename_buffer =
      (char*)malloc(buffer_sz);  // NOLINT (this could be a C compilation unit)
  if (!filename_buffer) {
    perror("Failed to allocate file name buffer.");
    abort();
  }

  if (snprintf(filename_buffer, buffer_sz, "%s%s", leading_temp_path, suffix) >=
      buffer_sz) {
    perror("File name buffer too short.");
    abort();
  }

  const int file_descriptor = mkstemps(filename_buffer, suffix_len);
  if (file_descriptor < 0) {
    perror("Failed to make temporary file.");
    abort();
  }
  FILE* file = fdopen(file_descriptor, "wb");
  if (!file) {
    perror("Failed to open file descriptor.");
    close(file_descriptor);
    abort();
  }
  const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file);
  if (bytes_written < size) {
    close(file_descriptor);
    fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)",
            bytes_written, size);
    abort();
  }
  fclose(file);
  return filename_buffer;
}

static char* fuzzer_get_tmpfile(
    const uint8_t* data,
    size_t size) {  // NOLINT (people include this .inc file directly)
  return fuzzer_get_tmpfile_with_suffix(data, size, NULL);  // NOLINT
}

static void fuzzer_release_tmpfile(char* filename) {
  if (unlink(filename) != 0) {
    perror("WARNING: Failed to delete temporary file.");
  }
  free(filename);
}

// C++ RAII object for creating temporary files.

#ifdef __cplusplus
class FuzzerTemporaryFile {
 public:
  FuzzerTemporaryFile(const uint8_t* data, size_t size)
      : original_filename_(fuzzer_get_tmpfile(data, size)) {
    filename_ = strdup(original_filename_);
    if (!filename_) {
      perror("Failed to allocate file name copy.");
      abort();
    }
  }

  FuzzerTemporaryFile(const uint8_t* data, size_t size, const char* suffix)
      : original_filename_(fuzzer_get_tmpfile_with_suffix(data, size, suffix)) {
    filename_ = strdup(original_filename_);
    if (!filename_) {
      perror("Failed to allocate file name copy.");
      abort();
    }
  }

  ~FuzzerTemporaryFile() {
    free(filename_);
    fuzzer_release_tmpfile(original_filename_);
  }

  FuzzerTemporaryFile(const FuzzerTemporaryFile& other) = delete;
  FuzzerTemporaryFile operator=(const FuzzerTemporaryFile& other) = delete;

  FuzzerTemporaryFile(const FuzzerTemporaryFile&& other) = delete;
  FuzzerTemporaryFile operator=(const FuzzerTemporaryFile&& other) = delete;

  const char* filename() const { return filename_; }

  // Returns a mutable pointer to the file name. Should be used sparingly, only
  // in case the fuzzed API demands it or when making a mutable copy is
  // inconvenient (e.g., in auto-generated code).
  char* mutable_filename() const { return filename_; }

 private:
  char* original_filename_;

  // A mutable copy of the original filename, returned by the accessor. This
  // guarantees that the original filename can always be used to release the
  // temporary path.
  char* filename_;
};
#endif  // __cplusplus
#endif  // LIBGAV1_TESTS_FUZZER_FUZZER_TEMP_FILE_H_