OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * NaCl Service Runtime memory allocation code | 8 * NaCl Service Runtime memory allocation code |
9 */ | 9 */ |
10 #include "native_client/src/include/portability.h" | 10 #include "native_client/src/include/portability.h" |
11 #include "native_client/src/include/win/mman.h" | 11 #include "native_client/src/include/win/mman.h" |
12 | 12 |
13 #include <errno.h> | 13 #include <errno.h> |
14 #include <windows.h> | 14 #include <windows.h> |
15 #include <string.h> | 15 #include <string.h> |
16 | 16 |
17 #include "native_client/src/shared/platform/nacl_global_secure_random.h" | 17 #include "native_client/src/shared/platform/nacl_global_secure_random.h" |
18 #include "native_client/src/shared/platform/nacl_log.h" | 18 #include "native_client/src/shared/platform/nacl_log.h" |
19 #include "native_client/src/shared/platform/win/xlate_system_error.h" | 19 #include "native_client/src/shared/platform/win/xlate_system_error.h" |
20 | 20 |
21 #include "native_client/src/trusted/service_runtime/nacl_config.h" | 21 #include "native_client/src/trusted/service_runtime/nacl_config.h" |
22 #include "native_client/src/trusted/service_runtime/sel_memory.h" | 22 #include "native_client/src/trusted/service_runtime/sel_memory.h" |
23 #include "native_client/src/trusted/service_runtime/sel_util.h" | 23 #include "native_client/src/trusted/service_runtime/sel_util.h" |
24 | 24 |
25 #define MSGWIDTH "25" | 25 #define MSGWIDTH "25" |
26 | 26 |
27 #if NACL_ARCH_CPU_32_BITS | |
Mark Seaborn
2011/08/16 17:34:10
As before.
bbudge
2011/08/16 19:46:22
Done.
| |
28 | |
29 int NaCl_find_sandbox_memory(void **p, | |
Mark Seaborn
2011/08/16 17:34:10
Can you put a comment in to say that the purpose o
bbudge
2011/08/16 19:46:22
Done.
| |
30 size_t num_bytes) { | |
31 SYSTEM_INFO sys_info; | |
32 MEMORY_BASIC_INFORMATION mem; | |
33 char* start; | |
Mark Seaborn
2011/08/16 17:34:10
Nit: " *" rather than "* "
bbudge
2011/08/16 19:46:22
Done.
| |
34 | |
35 GetSystemInfo(&sys_info); | |
36 start = sys_info.lpMinimumApplicationAddress; | |
37 while (VirtualQuery((LPCVOID)start, &mem, sizeof(mem))) { | |
Mark Seaborn
2011/08/16 17:34:10
I'd suggest checking that the return value is eith
bbudge
2011/08/16 19:46:22
Done.
| |
38 if (mem.State == MEM_RESERVE && mem.RegionSize == num_bytes) { | |
39 VirtualFree(start, 0, MEM_RELEASE); | |
Mark Seaborn
2011/08/16 17:34:10
Please check return value. You can use LOG_FATAL
bbudge
2011/08/16 19:46:22
Done.
| |
40 *p = start; | |
41 return 0; | |
42 } | |
43 start += mem.RegionSize; | |
44 if ((LPVOID)start > sys_info.lpMaximumApplicationAddress) | |
Mark Seaborn
2011/08/16 17:34:10
Minor nit: maybe '>='?
bbudge
2011/08/16 19:46:22
Done.
| |
45 break; | |
46 } | |
47 return -ENOMEM; | |
48 } | |
49 | |
50 #endif /* NACL_ARCH_CPU_32_BITS */ | |
51 | |
27 /* | 52 /* |
28 * NaCl_page_free: free pages allocated with NaCl_page_alloc. | 53 * NaCl_page_free: free pages allocated with NaCl_page_alloc. |
29 * Must start at allocation granularity (NACL_MAP_PAGESIZE) and | 54 * Must start at allocation granularity (NACL_MAP_PAGESIZE) and |
30 * number of bytes must be a multiple of allocation granularity. | 55 * number of bytes must be a multiple of allocation granularity. |
31 */ | 56 */ |
32 void NaCl_page_free(void *p, | 57 void NaCl_page_free(void *p, |
33 size_t num_bytes) { | 58 size_t num_bytes) { |
34 void *end_addr; | 59 void *end_addr; |
35 | 60 |
36 end_addr = (void *) (((char *) p) + num_bytes); | 61 end_addr = (void *) (((char *) p) + num_bytes); |
37 while (p < end_addr) { | 62 while (p < end_addr) { |
38 if (!VirtualFree(p, 0, MEM_RELEASE)) { | 63 if (!VirtualFree(p, 0, MEM_RELEASE)) { |
39 DWORD err = GetLastError(); | 64 DWORD err = GetLastError(); |
40 NaClLog(LOG_FATAL, | 65 NaClLog(LOG_FATAL, |
41 "NaCl_page_free: VirtualFree(0x%016"NACL_PRIxPTR | 66 "NaCl_page_free: VirtualFree(0x%016"NACL_PRIxPTR |
42 ", 0, MEM_RELEASE) failed " | 67 ", 0, MEM_RELEASE) failed " |
43 "with error 0x%X\n", | 68 "with error 0x%X\n", |
44 (uintptr_t) p, err); | 69 (uintptr_t) p, err); |
45 } | 70 } |
46 p = (void *) (((char *) p) + NACL_MAP_PAGESIZE); | 71 p = (void *) (((char *) p) + NACL_MAP_PAGESIZE); |
47 } | 72 } |
48 } | 73 } |
49 | 74 |
50 | 75 |
51 static | 76 int NaCl_page_alloc_at_addr(void **p, |
52 int NaCl_page_alloc_hint(void **p, | |
53 size_t num_bytes) { | 77 size_t num_bytes) { |
54 SYSTEM_INFO sys_info; | 78 SYSTEM_INFO sys_info; |
55 | 79 |
56 int attempt_count; | 80 int attempt_count; |
57 | 81 |
58 void *hint = *p; | 82 void *hint = *p; |
59 void *addr; | 83 void *addr; |
60 void *end_addr; | 84 void *end_addr; |
61 void *chunk; | 85 void *chunk; |
62 void *unroll; | 86 void *unroll; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 /* double break */ | 168 /* double break */ |
145 } | 169 } |
146 } | 170 } |
147 NaClLog(2, | 171 NaClLog(2, |
148 ("NaCl_page_alloc: *p = 0x%016"NACL_PRIxPTR"," | 172 ("NaCl_page_alloc: *p = 0x%016"NACL_PRIxPTR"," |
149 " returning success.\n"), | 173 " returning success.\n"), |
150 (uintptr_t) addr); | 174 (uintptr_t) addr); |
151 *p = addr; | 175 *p = addr; |
152 return 0; | 176 return 0; |
153 retry: | 177 retry: |
154 NaClLog(2, "NaCl_page_alloc_hint: retrying w/o hint\n"); | 178 NaClLog(2, "NaCl_page_alloc_at_addr: retrying w/o hint\n"); |
155 hint = NULL; | 179 hint = NULL; |
156 } | 180 } |
157 | 181 |
158 return -ENOMEM; | 182 return -ENOMEM; |
159 } | 183 } |
160 | 184 |
161 int NaCl_page_alloc(void **p, | 185 int NaCl_page_alloc(void **p, |
162 size_t num_bytes) { | 186 size_t num_bytes) { |
163 *p = NULL; | 187 *p = NULL; |
164 return NaCl_page_alloc_hint(p, num_bytes); | 188 return NaCl_page_alloc_at_addr(p, num_bytes); |
165 } | 189 } |
166 | 190 |
167 /* | 191 /* |
168 * Pick a "hint" address that is random. | 192 * Pick a "hint" address that is random. |
169 */ | 193 */ |
170 int NaCl_page_alloc_randomized(void **p, | 194 int NaCl_page_alloc_randomized(void **p, |
171 size_t size) { | 195 size_t size) { |
172 uintptr_t addr; | 196 uintptr_t addr; |
173 int neg_errno = -ENOMEM; /* in case we change kNumTries to 0 */ | 197 int neg_errno = -ENOMEM; /* in case we change kNumTries to 0 */ |
174 int tries; | 198 int tries; |
(...skipping 21 matching lines...) Expand all Loading... | |
196 * bits of entropy. | 220 * bits of entropy. |
197 */ | 221 */ |
198 *p = (void *) ((addr << NACL_MAP_PAGESHIFT) /* bits [47:16] are random */ | 222 *p = (void *) ((addr << NACL_MAP_PAGESHIFT) /* bits [47:16] are random */ |
199 & ((((uintptr_t) 1) << 43) - 1)); /* now bits [42:16] */ | 223 & ((((uintptr_t) 1) << 43) - 1)); /* now bits [42:16] */ |
200 #else | 224 #else |
201 # error "where am i?" | 225 # error "where am i?" |
202 #endif | 226 #endif |
203 | 227 |
204 NaClLog(LOG_INFO, "NaCl_page_alloc_randomized: hint 0x%"NACL_PRIxPTR"\n", | 228 NaClLog(LOG_INFO, "NaCl_page_alloc_randomized: hint 0x%"NACL_PRIxPTR"\n", |
205 (uintptr_t) *p); | 229 (uintptr_t) *p); |
206 neg_errno = NaCl_page_alloc_hint(p, size); | 230 neg_errno = NaCl_page_alloc_at_addr(p, size); |
207 if (0 == neg_errno) { | 231 if (0 == neg_errno) { |
208 break; | 232 break; |
209 } | 233 } |
210 } | 234 } |
211 if (0 != neg_errno) { | 235 if (0 != neg_errno) { |
212 NaClLog(LOG_INFO, | 236 NaClLog(LOG_INFO, |
213 "NaCl_page_alloc_randomized: failed (%d), dropping hints\n", | 237 "NaCl_page_alloc_randomized: failed (%d), dropping hints\n", |
214 -neg_errno); | 238 -neg_errno); |
215 *p = 0; | 239 *p = 0; |
216 neg_errno = NaCl_page_alloc_hint(p, size); | 240 neg_errno = NaCl_page_alloc_at_addr(p, size); |
217 } | 241 } |
218 return neg_errno; | 242 return neg_errno; |
219 } | 243 } |
220 | 244 |
221 uintptr_t NaClProtectChunkSize(uintptr_t start, | 245 uintptr_t NaClProtectChunkSize(uintptr_t start, |
222 uintptr_t end) { | 246 uintptr_t end) { |
223 uintptr_t chunk_end; | 247 uintptr_t chunk_end; |
224 | 248 |
225 chunk_end = NaClRoundAllocPage(start + 1); | 249 chunk_end = NaClRoundAllocPage(start + 1); |
226 if (chunk_end > end) { | 250 if (chunk_end > end) { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 break; | 423 break; |
400 case MADV_NORMAL: | 424 case MADV_NORMAL: |
401 memset(start, 0, length); | 425 memset(start, 0, length); |
402 break; | 426 break; |
403 default: | 427 default: |
404 return -EINVAL; | 428 return -EINVAL; |
405 } | 429 } |
406 NaClLog(2, "NaCl_madvise: done\n"); | 430 NaClLog(2, "NaCl_madvise: done\n"); |
407 return 0; | 431 return 0; |
408 } | 432 } |
OLD | NEW |