Enterprise policy: Ignore the deprecated ForceSafeSearch if ForceGoogleSafeSearch...
[chromium-blink-merge.git] / base / memory / discardable_shared_memory.cc
blob830d6b9d87fdde5438b2d1255c6dc07b62aabf93
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/discardable_shared_memory.h"
7 #if defined(OS_POSIX)
8 #include <unistd.h>
9 #endif
11 #include <algorithm>
13 #include "base/atomicops.h"
14 #include "base/logging.h"
15 #include "base/numerics/safe_math.h"
16 #include "base/process/process_metrics.h"
18 #if defined(OS_ANDROID)
19 #include "third_party/ashmem/ashmem.h"
20 #endif
22 namespace base {
23 namespace {
25 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or
26 // Atomic64 routines, depending on the architecture.
27 typedef intptr_t AtomicType;
28 typedef uintptr_t UAtomicType;
30 // Template specialization for timestamp serialization/deserialization. This
31 // is used to serialize timestamps using Unix time on systems where AtomicType
32 // does not have enough precision to contain a timestamp in the standard
33 // serialized format.
34 template <int>
35 Time TimeFromWireFormat(int64 value);
36 template <int>
37 int64 TimeToWireFormat(Time time);
39 // Serialize to Unix time when using 4-byte wire format.
40 // Note: 19 January 2038, this will cease to work.
41 template <>
42 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<4>(int64 value) {
43 return value ? Time::UnixEpoch() + TimeDelta::FromSeconds(value) : Time();
45 template <>
46 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<4>(Time time) {
47 return time > Time::UnixEpoch() ? (time - Time::UnixEpoch()).InSeconds() : 0;
50 // Standard serialization format when using 8-byte wire format.
51 template <>
52 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<8>(int64 value) {
53 return Time::FromInternalValue(value);
55 template <>
56 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<8>(Time time) {
57 return time.ToInternalValue();
60 struct SharedState {
61 enum LockState { UNLOCKED = 0, LOCKED = 1 };
63 explicit SharedState(AtomicType ivalue) { value.i = ivalue; }
64 SharedState(LockState lock_state, Time timestamp) {
65 int64 wire_timestamp = TimeToWireFormat<sizeof(AtomicType)>(timestamp);
66 DCHECK_GE(wire_timestamp, 0);
67 DCHECK_EQ(lock_state & ~1, 0);
68 value.u = (static_cast<UAtomicType>(wire_timestamp) << 1) | lock_state;
71 LockState GetLockState() const { return static_cast<LockState>(value.u & 1); }
73 Time GetTimestamp() const {
74 return TimeFromWireFormat<sizeof(AtomicType)>(value.u >> 1);
77 // Bit 1: Lock state. Bit is set when locked.
78 // Bit 2..sizeof(AtomicType)*8: Usage timestamp. NULL time when locked or
79 // purged.
80 union {
81 AtomicType i;
82 UAtomicType u;
83 } value;
86 // Shared state is stored at offset 0 in shared memory segments.
87 SharedState* SharedStateFromSharedMemory(const SharedMemory& shared_memory) {
88 DCHECK(shared_memory.memory());
89 return static_cast<SharedState*>(shared_memory.memory());
92 // Round up |size| to a multiple of alignment, which must be a power of two.
93 size_t Align(size_t alignment, size_t size) {
94 DCHECK_EQ(alignment & (alignment - 1), 0u);
95 return (size + alignment - 1) & ~(alignment - 1);
98 // Round up |size| to a multiple of page size.
99 size_t AlignToPageSize(size_t size) {
100 return Align(base::GetPageSize(), size);
103 } // namespace
105 DiscardableSharedMemory::DiscardableSharedMemory()
106 : mapped_size_(0), locked_page_count_(0) {
109 DiscardableSharedMemory::DiscardableSharedMemory(
110 SharedMemoryHandle shared_memory_handle)
111 : shared_memory_(shared_memory_handle, false),
112 mapped_size_(0),
113 locked_page_count_(0) {
116 DiscardableSharedMemory::~DiscardableSharedMemory() {
119 bool DiscardableSharedMemory::CreateAndMap(size_t size) {
120 CheckedNumeric<size_t> checked_size = size;
121 checked_size += AlignToPageSize(sizeof(SharedState));
122 if (!checked_size.IsValid())
123 return false;
125 if (!shared_memory_.CreateAndMapAnonymous(checked_size.ValueOrDie()))
126 return false;
128 mapped_size_ =
129 shared_memory_.mapped_size() - AlignToPageSize(sizeof(SharedState));
131 locked_page_count_ = AlignToPageSize(mapped_size_) / base::GetPageSize();
132 #if DCHECK_IS_ON()
133 for (size_t page = 0; page < locked_page_count_; ++page)
134 locked_pages_.insert(page);
135 #endif
137 DCHECK(last_known_usage_.is_null());
138 SharedState new_state(SharedState::LOCKED, Time());
139 subtle::Release_Store(&SharedStateFromSharedMemory(shared_memory_)->value.i,
140 new_state.value.i);
141 return true;
144 bool DiscardableSharedMemory::Map(size_t size) {
145 if (!shared_memory_.Map(AlignToPageSize(sizeof(SharedState)) + size))
146 return false;
148 mapped_size_ =
149 shared_memory_.mapped_size() - AlignToPageSize(sizeof(SharedState));
151 locked_page_count_ = AlignToPageSize(mapped_size_) / base::GetPageSize();
152 #if DCHECK_IS_ON()
153 for (size_t page = 0; page < locked_page_count_; ++page)
154 locked_pages_.insert(page);
155 #endif
157 return true;
160 DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock(
161 size_t offset, size_t length) {
162 DCHECK_EQ(AlignToPageSize(offset), offset);
163 DCHECK_EQ(AlignToPageSize(length), length);
165 // Calls to this function must be synchronized properly.
166 DFAKE_SCOPED_LOCK(thread_collision_warner_);
168 DCHECK(shared_memory_.memory());
170 // We need to successfully acquire the platform independent lock before
171 // individual pages can be locked.
172 if (!locked_page_count_) {
173 // Return false when instance has been purged or not initialized properly
174 // by checking if |last_known_usage_| is NULL.
175 if (last_known_usage_.is_null())
176 return FAILED;
178 SharedState old_state(SharedState::UNLOCKED, last_known_usage_);
179 SharedState new_state(SharedState::LOCKED, Time());
180 SharedState result(subtle::Acquire_CompareAndSwap(
181 &SharedStateFromSharedMemory(shared_memory_)->value.i,
182 old_state.value.i,
183 new_state.value.i));
184 if (result.value.u != old_state.value.u) {
185 // Update |last_known_usage_| in case the above CAS failed because of
186 // an incorrect timestamp.
187 last_known_usage_ = result.GetTimestamp();
188 return FAILED;
192 // Zero for length means "everything onward".
193 if (!length)
194 length = AlignToPageSize(mapped_size_) - offset;
196 size_t start = offset / base::GetPageSize();
197 size_t end = start + length / base::GetPageSize();
198 DCHECK_LT(start, end);
199 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize());
201 // Add pages to |locked_page_count_|.
202 // Note: Locking a page that is already locked is an error.
203 locked_page_count_ += end - start;
204 #if DCHECK_IS_ON()
205 // Detect incorrect usage by keeping track of exactly what pages are locked.
206 for (auto page = start; page < end; ++page) {
207 auto result = locked_pages_.insert(page);
208 DCHECK(result.second);
210 DCHECK_EQ(locked_pages_.size(), locked_page_count_);
211 #endif
213 #if defined(OS_ANDROID)
214 SharedMemoryHandle handle = shared_memory_.handle();
215 DCHECK(SharedMemory::IsHandleValid(handle));
216 if (ashmem_pin_region(
217 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
218 return PURGED;
220 #endif
222 return SUCCESS;
225 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) {
226 DCHECK_EQ(AlignToPageSize(offset), offset);
227 DCHECK_EQ(AlignToPageSize(length), length);
229 // Calls to this function must be synchronized properly.
230 DFAKE_SCOPED_LOCK(thread_collision_warner_);
232 // Zero for length means "everything onward".
233 if (!length)
234 length = AlignToPageSize(mapped_size_) - offset;
236 DCHECK(shared_memory_.memory());
238 #if defined(OS_ANDROID)
239 SharedMemoryHandle handle = shared_memory_.handle();
240 DCHECK(SharedMemory::IsHandleValid(handle));
241 if (ashmem_unpin_region(
242 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
243 DPLOG(ERROR) << "ashmem_unpin_region() failed";
245 #endif
247 size_t start = offset / base::GetPageSize();
248 size_t end = start + length / base::GetPageSize();
249 DCHECK_LT(start, end);
250 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize());
252 // Remove pages from |locked_page_count_|.
253 // Note: Unlocking a page that is not locked is an error.
254 DCHECK_GE(locked_page_count_, end - start);
255 locked_page_count_ -= end - start;
256 #if DCHECK_IS_ON()
257 // Detect incorrect usage by keeping track of exactly what pages are locked.
258 for (auto page = start; page < end; ++page) {
259 auto erased_count = locked_pages_.erase(page);
260 DCHECK_EQ(1u, erased_count);
262 DCHECK_EQ(locked_pages_.size(), locked_page_count_);
263 #endif
265 // Early out and avoid releasing the platform independent lock if some pages
266 // are still locked.
267 if (locked_page_count_)
268 return;
270 Time current_time = Now();
271 DCHECK(!current_time.is_null());
273 SharedState old_state(SharedState::LOCKED, Time());
274 SharedState new_state(SharedState::UNLOCKED, current_time);
275 // Note: timestamp cannot be NULL as that is a unique value used when
276 // locked or purged.
277 DCHECK(!new_state.GetTimestamp().is_null());
278 // Timestamp precision should at least be accurate to the second.
279 DCHECK_EQ((new_state.GetTimestamp() - Time::UnixEpoch()).InSeconds(),
280 (current_time - Time::UnixEpoch()).InSeconds());
281 SharedState result(subtle::Release_CompareAndSwap(
282 &SharedStateFromSharedMemory(shared_memory_)->value.i,
283 old_state.value.i,
284 new_state.value.i));
286 DCHECK_EQ(old_state.value.u, result.value.u);
288 last_known_usage_ = current_time;
291 void* DiscardableSharedMemory::memory() const {
292 return reinterpret_cast<uint8*>(shared_memory_.memory()) +
293 AlignToPageSize(sizeof(SharedState));
296 bool DiscardableSharedMemory::Purge(Time current_time) {
297 // Calls to this function must be synchronized properly.
298 DFAKE_SCOPED_LOCK(thread_collision_warner_);
300 // Early out if not mapped. This can happen if the segment was previously
301 // unmapped using a call to Close().
302 if (!shared_memory_.memory())
303 return true;
305 SharedState old_state(SharedState::UNLOCKED, last_known_usage_);
306 SharedState new_state(SharedState::UNLOCKED, Time());
307 SharedState result(subtle::Acquire_CompareAndSwap(
308 &SharedStateFromSharedMemory(shared_memory_)->value.i,
309 old_state.value.i,
310 new_state.value.i));
312 // Update |last_known_usage_| to |current_time| if the memory is locked. This
313 // allows the caller to determine if purging failed because last known usage
314 // was incorrect or memory was locked. In the second case, the caller should
315 // most likely wait for some amount of time before attempting to purge the
316 // the memory again.
317 if (result.value.u != old_state.value.u) {
318 last_known_usage_ = result.GetLockState() == SharedState::LOCKED
319 ? current_time
320 : result.GetTimestamp();
321 return false;
324 last_known_usage_ = Time();
325 return true;
328 bool DiscardableSharedMemory::IsMemoryResident() const {
329 DCHECK(shared_memory_.memory());
331 SharedState result(subtle::NoBarrier_Load(
332 &SharedStateFromSharedMemory(shared_memory_)->value.i));
334 return result.GetLockState() == SharedState::LOCKED ||
335 !result.GetTimestamp().is_null();
338 void DiscardableSharedMemory::Close() {
339 shared_memory_.Unmap();
340 shared_memory_.Close();
341 mapped_size_ = 0;
344 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
345 void DiscardableSharedMemory::Shrink() {
346 #if defined(OS_POSIX)
347 SharedMemoryHandle handle = shared_memory_.handle();
348 if (!SharedMemory::IsHandleValid(handle))
349 return;
351 // Truncate shared memory to size of SharedState.
352 if (HANDLE_EINTR(
353 ftruncate(handle.fd, AlignToPageSize(sizeof(SharedState)))) != 0) {
354 DPLOG(ERROR) << "ftruncate() failed";
355 return;
357 mapped_size_ = 0;
358 #else
359 NOTIMPLEMENTED();
360 #endif
362 #endif
364 Time DiscardableSharedMemory::Now() const {
365 return Time::Now();
368 } // namespace base