OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
7 | 7 |
8 #include "vm/virtual_memory.h" | 8 #include "vm/virtual_memory.h" |
9 | 9 |
10 #include <sys/mman.h> // NOLINT | 10 #include <sys/mman.h> // NOLINT |
11 #include <sys/unistd.h> // NOLINT | 11 #include <sys/unistd.h> // NOLINT |
12 #include <unistd.h> // NOLINT | 12 #include <unistd.h> // NOLINT |
13 | 13 |
14 #include "platform/assert.h" | 14 #include "platform/assert.h" |
15 #include "platform/utils.h" | 15 #include "platform/utils.h" |
| 16 // #include "vm/os.h" |
16 | 17 |
17 namespace dart { | 18 namespace dart { |
18 | 19 |
19 // standard MAP_FAILED causes "error: use of old-style cast" as it | 20 // standard MAP_FAILED causes "error: use of old-style cast" as it |
20 // defines MAP_FAILED as ((void *) -1) | 21 // defines MAP_FAILED as ((void *) -1) |
21 #undef MAP_FAILED | 22 #undef MAP_FAILED |
22 #define MAP_FAILED reinterpret_cast<void*>(-1) | 23 #define MAP_FAILED reinterpret_cast<void*>(-1) |
23 | 24 |
24 uword VirtualMemory::page_size_ = 0; | 25 uword VirtualMemory::page_size_ = 0; |
25 | 26 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, | 70 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, |
70 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | 71 MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
71 -1, 0); | 72 -1, 0); |
72 if (address == MAP_FAILED) { | 73 if (address == MAP_FAILED) { |
73 return false; | 74 return false; |
74 } | 75 } |
75 return true; | 76 return true; |
76 } | 77 } |
77 | 78 |
78 | 79 |
79 bool VirtualMemory::Protect(Protection mode) { | 80 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
| 81 uword start_address = reinterpret_cast<uword>(address); |
| 82 uword end_address = start_address + size; |
| 83 uword page_address = start_address & ~(PageSize() - 1); |
80 int prot = 0; | 84 int prot = 0; |
81 switch (mode) { | 85 switch (mode) { |
82 case kNoAccess: | 86 case kNoAccess: |
83 prot = PROT_NONE; | 87 prot = PROT_NONE; |
84 break; | 88 break; |
85 case kReadOnly: | 89 case kReadOnly: |
86 prot = PROT_READ; | 90 prot = PROT_READ; |
87 break; | 91 break; |
88 case kReadWrite: | 92 case kReadWrite: |
89 prot = PROT_READ | PROT_WRITE; | 93 prot = PROT_READ | PROT_WRITE; |
90 break; | 94 break; |
91 case kReadExecute: | 95 case kReadExecute: |
92 prot = PROT_READ | PROT_EXEC; | 96 prot = PROT_READ | PROT_EXEC; |
93 break; | 97 break; |
94 case kReadWriteExecute: | 98 case kReadWriteExecute: |
95 prot = PROT_READ | PROT_WRITE | PROT_EXEC; | 99 prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
96 break; | 100 break; |
97 } | 101 } |
98 return (mprotect(address(), size(), prot) == 0); | 102 return (mprotect(reinterpret_cast<void*>(page_address), |
| 103 end_address - page_address, |
| 104 prot) == 0); |
99 } | 105 } |
100 | 106 |
| 107 |
101 } // namespace dart | 108 } // namespace dart |
102 | 109 |
103 #endif // defined(TARGET_OS_LINUX) | 110 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |