| Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 ART_RUNTIME_JIT_JIT_SCOPED_CODE_CACHE_WRITE_H_ |
| 18 | #define ART_RUNTIME_JIT_JIT_SCOPED_CODE_CACHE_WRITE_H_ |
| 19 | |
| 20 | #include <sys/mman.h> |
| 21 | |
| 22 | #include "base/systrace.h" |
| 23 | #include "base/utils.h" // For CheckedCall |
| 24 | |
| 25 | namespace art { |
| 26 | namespace jit { |
| 27 | |
| 28 | class JitMemoryRegion; |
| 29 | |
| 30 | static constexpr int kProtR = PROT_READ; |
| 31 | static constexpr int kProtRW = PROT_READ | PROT_WRITE; |
| 32 | static constexpr int kProtRWX = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 33 | static constexpr int kProtRX = PROT_READ | PROT_EXEC; |
| 34 | |
| 35 | // Helper for toggling JIT memory R <-> RW. |
| 36 | class ScopedCodeCacheWrite : ScopedTrace { |
| 37 | public: |
| 38 | explicit ScopedCodeCacheWrite(const JitMemoryRegion& region) |
| 39 | : ScopedTrace("ScopedCodeCacheWrite"), |
| 40 | region_(region) { |
| Nicolas Geoffray | 670ff88 | 2020-11-06 18:00:39 +0000 | [diff] [blame] | 41 | if (kIsDebugBuild || !region.HasDualCodeMapping()) { |
| 42 | ScopedTrace trace("mprotect all"); |
| 43 | const MemMap* const updatable_pages = region.GetUpdatableCodeMapping(); |
| 44 | if (updatable_pages != nullptr) { |
| 45 | int prot = region.HasDualCodeMapping() ? kProtRW : kProtRWX; |
| 46 | CheckedCall(mprotect, "Cache +W", updatable_pages->Begin(), updatable_pages->Size(), prot); |
| 47 | } |
| Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | ~ScopedCodeCacheWrite() { |
| Nicolas Geoffray | 670ff88 | 2020-11-06 18:00:39 +0000 | [diff] [blame] | 52 | if (kIsDebugBuild || !region_.HasDualCodeMapping()) { |
| 53 | ScopedTrace trace("mprotect code"); |
| 54 | const MemMap* const updatable_pages = region_.GetUpdatableCodeMapping(); |
| 55 | if (updatable_pages != nullptr) { |
| 56 | int prot = region_.HasDualCodeMapping() ? kProtR : kProtRX; |
| 57 | CheckedCall(mprotect, "Cache -W", updatable_pages->Begin(), updatable_pages->Size(), prot); |
| 58 | } |
| Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | const JitMemoryRegion& region_; |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite); |
| 66 | }; |
| 67 | |
| 68 | } // namespace jit |
| 69 | } // namespace art |
| 70 | |
| 71 | #endif // ART_RUNTIME_JIT_JIT_SCOPED_CODE_CACHE_WRITE_H_ |