• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_EXEC_REGION_H_
18 #define BERBERIS_BASE_EXEC_REGION_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 namespace berberis {
24 
25 // ExecRegion manages a range of writable executable memory.
26 // Move-only!
27 class ExecRegion {
28  public:
29   ExecRegion() = default;
ExecRegion(uint8_t * exec,size_t size)30   explicit ExecRegion(uint8_t* exec, size_t size) : exec_{exec}, size_{size} {}
31 
32   ExecRegion(const ExecRegion& other) = delete;
33   ExecRegion& operator=(const ExecRegion& other) = delete;
34 
ExecRegion(ExecRegion && other)35   ExecRegion(ExecRegion&& other) noexcept {
36     exec_ = other.exec_;
37     size_ = other.size_;
38     other.exec_ = nullptr;
39     other.size_ = 0;
40   }
41 
42   ExecRegion& operator=(ExecRegion&& other) noexcept {
43     if (this == &other) {
44       return *this;
45     }
46     exec_ = other.exec_;
47     size_ = other.size_;
48     other.exec_ = nullptr;
49     other.size_ = 0;
50     return *this;
51   }
52 
begin()53   [[nodiscard]] const uint8_t* begin() const { return exec_; }
end()54   [[nodiscard]] const uint8_t* end() const { return exec_ + size_; }
55 
56   void Write(const uint8_t* dst, const void* src, size_t size);
57 
58   void Detach();
59   void Free();
60 
61  private:
62   uint8_t* exec_ = nullptr;
63   size_t size_ = 0;
64 };
65 
66 }  // namespace berberis
67 
68 #endif  // BERBERIS_BASE_EXEC_REGION_H_
69