1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #ifndef BERBERIS_BASE_MMAP_H_
18 #define BERBERIS_BASE_MMAP_H_
19 
20 #include <sys/mman.h>
21 
22 #include <cstddef>
23 
24 #include "berberis/base/bit_util.h"
25 #include "berberis/base/macros.h"
26 #include "berberis/base/page_size.h"
27 
28 namespace berberis {
29 
30 template <typename T>
AlignDownPageSize(T x)31 constexpr T AlignDownPageSize(T x) {
32   return AlignDown(x, kPageSize);
33 }
34 
35 template <typename T>
AlignUpPageSize(T x)36 constexpr T AlignUpPageSize(T x) {
37   static_assert(!std::is_signed_v<T>);
38   T result = AlignUp(x, kPageSize);
39   CHECK_GE(result, x);
40   return result;
41 }
42 
43 template <typename T>
AlignUpPageSizeOverflow(T x,T * result)44 constexpr bool AlignUpPageSizeOverflow(T x, T* result) {
45   static_assert(!std::is_signed_v<T>);
46   *result = AlignUp(x, kPageSize);
47   return *result < x;
48 }
49 
50 template <typename T>
IsAlignedPageSize(T x)51 constexpr bool IsAlignedPageSize(T x) {
52   return IsAligned(x, kPageSize);
53 }
54 
55 enum MmapBerberisFlags {
56   kMmapBerberis32Bit = 1,
57 };
58 
59 struct MmapImplArgs {
60   void* addr = nullptr;
61   size_t size = 0;
62   int prot = PROT_READ | PROT_WRITE;
63   int flags = MAP_PRIVATE | MAP_ANONYMOUS;
64   int fd = -1;
65   off_t offset = 0;
66   int berberis_flags = 0;
67 };
68 
69 void* MmapImpl(MmapImplArgs args);
70 void* MmapImplOrDie(MmapImplArgs args);
71 
Mmap(size_t size)72 inline void* Mmap(size_t size) {
73   return MmapImpl({.size = size});
74 }
75 
MmapOrDie(size_t size)76 inline void* MmapOrDie(size_t size) {
77   return MmapImplOrDie({.size = size});
78 }
79 
80 void MunmapOrDie(void* ptr, size_t size);
81 
82 void MprotectOrDie(void* ptr, size_t size, int prot);
83 
84 class ScopedMmap {
85  public:
ScopedMmap()86   ScopedMmap() : data_(nullptr), size_(0) {}
ScopedMmap(size_t size)87   explicit ScopedMmap(size_t size) { Init(size); }
88 
~ScopedMmap()89   ~ScopedMmap() {
90     if (size_) {
91       MunmapOrDie(data_, size_);
92     }
93   }
94 
Init(size_t size)95   void Init(size_t size) {
96     size_ = AlignUpPageSize(size);
97     data_ = MmapOrDie(size_);
98   }
99 
data()100   void* data() const { return data_; }
size()101   size_t size() const { return size_; }
102 
103  private:
104   void* data_;
105   size_t size_;
106 
107   DISALLOW_COPY_AND_ASSIGN(ScopedMmap);
108 };
109 
110 }  // namespace berberis
111 
112 #endif  // BERBERIS_BASE_MMAP_H_
113