Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/FileOutputBuffer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Utility for creating a in-memory buffer that will be written to a file.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/FileOutputBuffer.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/Support/Errc.h"
17
#include "llvm/Support/Memory.h"
18
#include "llvm/Support/Path.h"
19
#include <system_error>
20
21
#if !defined(_MSC_VER) && !defined(__MINGW32__)
22
#include <unistd.h>
23
#else
24
#include <io.h>
25
#endif
26
27
using namespace llvm;
28
using namespace llvm::sys;
29
30
namespace {
31
// A FileOutputBuffer which creates a temporary file in the same directory
32
// as the final output file. The final output file is atomically replaced
33
// with the temporary file on commit().
34
class OnDiskBuffer : public FileOutputBuffer {
35
public:
36
  OnDiskBuffer(StringRef Path, fs::TempFile Temp,
37
               std::unique_ptr<fs::mapped_file_region> Buf)
38
7.23k
      : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
39
40
534k
  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
41
42
247
  uint8_t *getBufferEnd() const override {
43
247
    return (uint8_t *)Buffer->data() + Buffer->size();
44
247
  }
45
46
576
  size_t getBufferSize() const override { return Buffer->size(); }
47
48
3.98k
  Error commit() override {
49
3.98k
    // Unmap buffer, letting OS flush dirty pages to file on disk.
50
3.98k
    Buffer.reset();
51
3.98k
52
3.98k
    // Atomically replace the existing file with the new one.
53
3.98k
    return Temp.keep(FinalPath);
54
3.98k
  }
55
56
7.23k
  ~OnDiskBuffer() override {
57
7.23k
    // Close the mapping before deleting the temp file, so that the removal
58
7.23k
    // succeeds.
59
7.23k
    Buffer.reset();
60
7.23k
    consumeError(Temp.discard());
61
7.23k
  }
62
63
0
  void discard() override {
64
0
    // Delete the temp file if it still was open, but keeping the mapping
65
0
    // active.
66
0
    consumeError(Temp.discard());
67
0
  }
68
69
private:
70
  std::unique_ptr<fs::mapped_file_region> Buffer;
71
  fs::TempFile Temp;
72
};
73
74
// A FileOutputBuffer which keeps data in memory and writes to the final
75
// output file on commit(). This is used only when we cannot use OnDiskBuffer.
76
class InMemoryBuffer : public FileOutputBuffer {
77
public:
78
  InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
79
                 unsigned Mode)
80
      : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
81
450
        Mode(Mode) {}
82
83
364
  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
84
85
0
  uint8_t *getBufferEnd() const override {
86
0
    return (uint8_t *)Buffer.base() + BufferSize;
87
0
  }
88
89
0
  size_t getBufferSize() const override { return BufferSize; }
90
91
160
  Error commit() override {
92
160
    if (FinalPath == "-") {
93
39
      llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
94
39
      llvm::outs().flush();
95
39
      return Error::success();
96
39
    }
97
121
98
121
    using namespace sys::fs;
99
121
    int FD;
100
121
    std::error_code EC;
101
121
    if (auto EC =
102
0
            openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
103
0
      return errorCodeToError(EC);
104
121
    raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
105
121
    OS << StringRef((const char *)Buffer.base(), BufferSize);
106
121
    return Error::success();
107
121
  }
108
109
private:
110
  // Buffer may actually contain a larger memory block than BufferSize
111
  OwningMemoryBlock Buffer;
112
  size_t BufferSize;
113
  unsigned Mode;
114
};
115
} // namespace
116
117
static Expected<std::unique_ptr<InMemoryBuffer>>
118
450
createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
119
450
  std::error_code EC;
120
450
  MemoryBlock MB = Memory::allocateMappedMemory(
121
450
      Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
122
450
  if (EC)
123
0
    return errorCodeToError(EC);
124
450
  return llvm::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
125
450
}
126
127
static Expected<std::unique_ptr<FileOutputBuffer>>
128
7.24k
createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
129
7.24k
  Expected<fs::TempFile> FileOrErr =
130
7.24k
      fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
131
7.24k
  if (!FileOrErr)
132
10
    return FileOrErr.takeError();
133
7.23k
  fs::TempFile File = std::move(*FileOrErr);
134
7.23k
135
7.23k
#ifndef _WIN32
136
7.23k
  // On Windows, CreateFileMapping (the mmap function on Windows)
137
7.23k
  // automatically extends the underlying file. We don't need to
138
7.23k
  // extend the file beforehand. _chsize (ftruncate on Windows) is
139
7.23k
  // pretty slow just like it writes specified amount of bytes,
140
7.23k
  // so we should avoid calling that function.
141
7.23k
  if (auto EC = fs::resize_file(File.FD, Size)) {
142
0
    consumeError(File.discard());
143
0
    return errorCodeToError(EC);
144
0
  }
145
7.23k
#endif
146
7.23k
147
7.23k
  // Mmap it.
148
7.23k
  std::error_code EC;
149
7.23k
  auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
150
7.23k
      fs::convertFDToNativeFile(File.FD), fs::mapped_file_region::readwrite,
151
7.23k
      Size, 0, EC);
152
7.23k
153
7.23k
  // mmap(2) can fail if the underlying filesystem does not support it.
154
7.23k
  // If that happens, we fall back to in-memory buffer as the last resort.
155
7.23k
  if (EC) {
156
0
    consumeError(File.discard());
157
0
    return createInMemoryBuffer(Path, Size, Mode);
158
0
  }
159
7.23k
160
7.23k
  return llvm::make_unique<OnDiskBuffer>(Path, std::move(File),
161
7.23k
                                         std::move(MappedFile));
162
7.23k
}
163
164
// Create an instance of FileOutputBuffer.
165
Expected<std::unique_ptr<FileOutputBuffer>>
166
7.70k
FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
167
7.70k
  // Handle "-" as stdout just like llvm::raw_ostream does.
168
7.70k
  if (Path == "-")
169
39
    return createInMemoryBuffer("-", Size, /*Mode=*/0);
170
7.66k
171
7.66k
  unsigned Mode = fs::all_read | fs::all_write;
172
7.66k
  if (Flags & F_executable)
173
3.85k
    Mode |= fs::all_exe;
174
7.66k
175
7.66k
  fs::file_status Stat;
176
7.66k
  fs::status(Path, Stat);
177
7.66k
178
7.66k
  // Usually, we want to create OnDiskBuffer to create a temporary file in
179
7.66k
  // the same directory as the destination file and atomically replaces it
180
7.66k
  // by rename(2).
181
7.66k
  //
182
7.66k
  // However, if the destination file is a special file, we don't want to
183
7.66k
  // use rename (e.g. we don't want to replace /dev/null with a regular
184
7.66k
  // file.) If that's the case, we create an in-memory buffer, open the
185
7.66k
  // destination file and write to it on commit().
186
7.66k
  switch (Stat.type()) {
187
7.66k
  case fs::file_type::directory_file:
188
3
    return errorCodeToError(errc::is_a_directory);
189
7.66k
  case fs::file_type::regular_file:
190
7.24k
  case fs::file_type::file_not_found:
191
7.24k
  case fs::file_type::status_error:
192
7.24k
    return createOnDiskBuffer(Path, Size, Mode);
193
7.24k
  default:
194
411
    return createInMemoryBuffer(Path, Size, Mode);
195
7.66k
  }
196
7.66k
}