Harden failure signal handler in the face of memory corruptions
[folly.git] / folly / MemoryMapping.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/MemoryMapping.h"
18 #include "folly/Format.h"
19
20 #include <fcntl.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <system_error>
24 #include <gflags/gflags.h>
25
26 DEFINE_int64(mlock_chunk_size, 1 << 20,  // 1MB
27              "Maximum bytes to mlock/munlock/munmap at once "
28              "(will be rounded up to PAGESIZE)");
29
30 namespace folly {
31
32 /* protected constructor */
33 MemoryMapping::MemoryMapping()
34   : mapStart_(nullptr)
35   , mapLength_(0)
36   , locked_(false) {
37 }
38
39 MemoryMapping::MemoryMapping(File file, off_t offset, off_t length)
40   : mapStart_(nullptr)
41   , mapLength_(0)
42   , locked_(false) {
43
44   init(std::move(file), offset, length, PROT_READ, false);
45 }
46
47 MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length)
48   : MemoryMapping(File(name), offset, length) { }
49
50 MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length)
51   : MemoryMapping(File(fd), offset, length) { }
52
53 void MemoryMapping::init(File file,
54                          off_t offset, off_t length,
55                          int prot,
56                          bool grow) {
57   off_t pageSize = sysconf(_SC_PAGESIZE);
58   CHECK_GE(offset, 0);
59
60   // Round down the start of the mapped region
61   size_t skipStart = offset % pageSize;
62   offset -= skipStart;
63
64   file_ = std::move(file);
65   mapLength_ = length;
66   if (mapLength_ != -1) {
67     mapLength_ += skipStart;
68
69     // Round up the end of the mapped region
70     mapLength_ = (mapLength_ + pageSize - 1) / pageSize * pageSize;
71   }
72
73   // stat the file
74   struct stat st;
75   CHECK_ERR(fstat(file_.fd(), &st));
76   off_t remaining = st.st_size - offset;
77   if (mapLength_ == -1) {
78     length = mapLength_ = remaining;
79   } else {
80     if (length > remaining) {
81       if (grow) {
82         PCHECK(0 == ftruncate(file_.fd(), offset + length))
83           << "ftructate() failed, couldn't grow file";
84         remaining = length;
85       } else {
86         length = remaining;
87       }
88     }
89     if (mapLength_ > remaining) mapLength_ = remaining;
90   }
91
92   if (length == 0) {
93     mapLength_ = 0;
94     mapStart_ = nullptr;
95   } else {
96     unsigned char* start = static_cast<unsigned char*>(
97       mmap(nullptr, mapLength_, prot, MAP_SHARED, file_.fd(), offset));
98     PCHECK(start != MAP_FAILED)
99       << " offset=" << offset
100       << " length=" << mapLength_;
101     mapStart_ = start;
102     data_.reset(start + skipStart, length);
103   }
104 }
105
106 namespace {
107
108 off_t memOpChunkSize(off_t length) {
109   off_t chunkSize = length;
110   if (FLAGS_mlock_chunk_size <= 0) {
111     return chunkSize;
112   }
113
114   chunkSize = FLAGS_mlock_chunk_size;
115   off_t pageSize = sysconf(_SC_PAGESIZE);
116   off_t r = chunkSize % pageSize;
117   if (r) {
118     chunkSize += (pageSize - r);
119   }
120   return chunkSize;
121 }
122
123 /**
124  * Run @op in chunks over the buffer @mem of @bufSize length.
125  *
126  * Return:
127  * - success: true + amountSucceeded == bufSize (op success on whole buffer)
128  * - failure: false + amountSucceeded == nr bytes on which op succeeded.
129  */
130 bool memOpInChunks(std::function<int(void*, size_t)> op,
131                    void* mem, size_t bufSize,
132                    size_t& amountSucceeded) {
133   // unmap/mlock/munlock take a kernel semaphore and block other threads from
134   // doing other memory operations. If the size of the buffer is big the
135   // semaphore can be down for seconds (for benchmarks see
136   // http://kostja-osipov.livejournal.com/42963.html).  Doing the operations in
137   // chunks breaks the locking into intervals and lets other threads do memory
138   // operations of their own.
139
140   size_t chunkSize = memOpChunkSize(bufSize);
141
142   char* addr = static_cast<char*>(mem);
143   amountSucceeded = 0;
144
145   while (amountSucceeded < bufSize) {
146     size_t size = std::min(chunkSize, bufSize - amountSucceeded);
147     if (op(addr + amountSucceeded, size) != 0) {
148       return false;
149     }
150     amountSucceeded += size;
151   }
152
153   return true;
154 }
155
156 }  // anonymous namespace
157
158 bool MemoryMapping::mlock(LockMode lock) {
159   size_t amountSucceeded = 0;
160   locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, amountSucceeded);
161   if (locked_) {
162     return true;
163   }
164
165   auto msg(folly::format(
166     "mlock({}) failed at {}",
167     mapLength_, amountSucceeded).str());
168
169   if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
170     PLOG(WARNING) << msg;
171   } else {
172     PLOG(FATAL) << msg;
173   }
174
175   // only part of the buffer was mlocked, unlock it back
176   if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, amountSucceeded)) {
177     PLOG(WARNING) << "munlock()";
178   }
179
180   return false;
181 }
182
183 void MemoryMapping::munlock(bool dontneed) {
184   if (!locked_) return;
185
186   size_t amountSucceeded = 0;
187   if (!memOpInChunks(::munlock, mapStart_, mapLength_, amountSucceeded)) {
188     PLOG(WARNING) << "munlock()";
189   }
190   if (mapLength_ && dontneed &&
191       ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
192     PLOG(WARNING) << "madvise()";
193   }
194   locked_ = false;
195 }
196
197 void MemoryMapping::hintLinearScan() {
198   advise(MADV_SEQUENTIAL);
199 }
200
201 MemoryMapping::~MemoryMapping() {
202   if (mapLength_) {
203     size_t amountSucceeded = 0;
204     if (!memOpInChunks(::munmap, mapStart_, mapLength_, amountSucceeded)) {
205       PLOG(FATAL) << folly::format(
206         "munmap({}) failed at {}",
207         mapLength_, amountSucceeded).str();
208     }
209   }
210 }
211
212 void MemoryMapping::advise(int advice) const {
213   if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
214     PLOG(WARNING) << "madvise()";
215   }
216 }
217
218 WritableMemoryMapping::WritableMemoryMapping(File file, off_t offset, off_t length) {
219   init(std::move(file), offset, length, PROT_READ | PROT_WRITE, true);
220 }
221
222 }  // namespace folly