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: ScopedCodeCacheWrite(const JitMemoryRegion & region)38 explicit ScopedCodeCacheWrite(const JitMemoryRegion& region) 39 : ScopedTrace("ScopedCodeCacheWrite"), 40 region_(region) { 41 ScopedTrace trace("mprotect all"); 42 const MemMap* const updatable_pages = region.GetUpdatableCodeMapping(); 43 if (updatable_pages != nullptr) { 44 int prot = region.HasDualCodeMapping() ? kProtRW : kProtRWX; 45 CheckedCall(mprotect, "Cache +W", updatable_pages->Begin(), updatable_pages->Size(), prot); 46 } 47 } 48 ~ScopedCodeCacheWrite()49 ~ScopedCodeCacheWrite() { 50 ScopedTrace trace("mprotect code"); 51 const MemMap* const updatable_pages = region_.GetUpdatableCodeMapping(); 52 if (updatable_pages != nullptr) { 53 int prot = region_.HasDualCodeMapping() ? kProtR : kProtRX; 54 CheckedCall(mprotect, "Cache -W", updatable_pages->Begin(), updatable_pages->Size(), prot); 55 } 56 } 57 58 private: 59 const JitMemoryRegion& region_; 60 61 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite); 62 }; 63 64 } // namespace jit 65 } // namespace art 66 67 #endif // ART_RUNTIME_JIT_JIT_SCOPED_CODE_CACHE_WRITE_H_ 68