blob: e2adebfb1b1060454f04f5b94c6651b768498602 [file] [log] [blame]
Nicolas Geoffray2a905b22019-06-06 09:04:07 +01001/*
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
25namespace art {
26namespace jit {
27
28class JitMemoryRegion;
29
30static constexpr int kProtR = PROT_READ;
31static constexpr int kProtRW = PROT_READ | PROT_WRITE;
32static constexpr int kProtRWX = PROT_READ | PROT_WRITE | PROT_EXEC;
33static constexpr int kProtRX = PROT_READ | PROT_EXEC;
34
35// Helper for toggling JIT memory R <-> RW.
36class ScopedCodeCacheWrite : ScopedTrace {
37 public:
38 explicit ScopedCodeCacheWrite(const JitMemoryRegion& region)
39 : ScopedTrace("ScopedCodeCacheWrite"),
40 region_(region) {
Nicolas Geoffray670ff882020-11-06 18:00:39 +000041 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 Geoffray2a905b22019-06-06 09:04:07 +010048 }
49 }
50
51 ~ScopedCodeCacheWrite() {
Nicolas Geoffray670ff882020-11-06 18:00:39 +000052 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 Geoffray2a905b22019-06-06 09:04:07 +010059 }
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_