Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: src/platform-macos.cc

Issue 8115014: Add hinting to improve ASLR for macos (all allocations) and linux (newly added allocation types). (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/platform-linux.cc ('K') | « src/platform-linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 if (-1.0 < x && x < 0.0) { 85 if (-1.0 < x && x < 0.0) {
86 return -0.0; 86 return -0.0;
87 } else { 87 } else {
88 return ceil(x); 88 return ceil(x);
89 } 89 }
90 } 90 }
91 91
92 92
93 static Mutex* limit_mutex = NULL; 93 static Mutex* limit_mutex = NULL;
94 94
95 static void* GetRandomMmapAddr() {
Vyacheslav Egorov (Chromium) 2011/10/04 09:18:33 consider moving somewhere (e.g. platfrom-posix.cc)
Cris Neckar 2011/10/04 19:20:47 Done.
96 Isolate* isolate = Isolate::UncheckedCurrent();
97 // Note that the current isolate isn't set up in a call path via
98 // CpuFeatures::Probe. We don't care about randomization in this case because
99 // the code page is immediately freed.
100 if (isolate != NULL) {
101 #ifdef V8_TARGET_ARCH_X64
102 uint64_t rnd1 = V8::RandomPrivate(isolate);
103 uint64_t rnd2 = V8::RandomPrivate(isolate);
104 uint64_t rnd2 = V8::RandomPrivate(isolate);
105 raw_addr &= V8_UINT64_C(0x3ffffffff000);
106 #else
107 uint32_t raw_addr = V8::RandomPrivate(isolate);
108 // The range 0x20000000 - 0x60000000 is relatively unpopulated on macos
109 // 10.6 and 10.7.
110 raw_addr &= 0x3ffff000;
111 raw_addr += 0x20000000;
112 #endif
113 return reinterpret_cast<void*>(raw_addr);
114 }
115 return NULL;
116 }
95 117
96 void OS::Setup() { 118 void OS::Setup() {
97 // Seed the random number generator. 119 // Seed the random number generator. We preserve microsecond resolution.
98 // Convert the current time to a 64-bit integer first, before converting it 120 uint64_t seed = Ticks() ^ (getpid() << 16);
99 // to an unsigned. Going directly will cause an overflow and the seed to be
100 // set to all ones. The seed will be identical for different instances that
101 // call this setup code within the same millisecond.
102 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
103 srandom(static_cast<unsigned int>(seed)); 121 srandom(static_cast<unsigned int>(seed));
104 limit_mutex = CreateMutex(); 122 limit_mutex = CreateMutex();
105 } 123 }
106 124
107 125
108 // We keep the lowest and highest addresses mapped as a quick way of 126 // We keep the lowest and highest addresses mapped as a quick way of
109 // determining that pointers are outside the heap (used mostly in assertions 127 // determining that pointers are outside the heap (used mostly in assertions
110 // and verification). The estimate is conservative, ie, not all addresses in 128 // and verification). The estimate is conservative, ie, not all addresses in
111 // 'allocated' space are actually allocated to our heap. The range is 129 // 'allocated' space are actually allocated to our heap. The range is
112 // [lowest, highest), inclusive on the low and and exclusive on the high end. 130 // [lowest, highest), inclusive on the low and and exclusive on the high end.
(...skipping 28 matching lines...) Expand all
141 // tools like vmmap(1). 159 // tools like vmmap(1).
142 static const int kMmapFd = VM_MAKE_TAG(255); 160 static const int kMmapFd = VM_MAKE_TAG(255);
143 static const off_t kMmapFdOffset = 0; 161 static const off_t kMmapFdOffset = 0;
144 162
145 163
146 void* OS::Allocate(const size_t requested, 164 void* OS::Allocate(const size_t requested,
147 size_t* allocated, 165 size_t* allocated,
148 bool is_executable) { 166 bool is_executable) {
149 const size_t msize = RoundUp(requested, getpagesize()); 167 const size_t msize = RoundUp(requested, getpagesize());
150 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); 168 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
151 void* mbase = mmap(NULL, msize, prot, 169 void* mbase = mmap(GetRandomMmapAddr(), msize, prot,
Vyacheslav Egorov (Chromium) 2011/10/04 09:18:33 formatting: one argument per line
Cris Neckar 2011/10/04 19:20:47 Done.
152 MAP_PRIVATE | MAP_ANON, 170 MAP_PRIVATE | MAP_ANON,
153 kMmapFd, kMmapFdOffset); 171 kMmapFd, kMmapFdOffset);
154 if (mbase == MAP_FAILED) { 172 if (mbase == MAP_FAILED) {
155 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed")); 173 LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
156 return NULL; 174 return NULL;
157 } 175 }
158 *allocated = msize; 176 *allocated = msize;
159 UpdateAllocatedSpaceLimits(mbase, msize); 177 UpdateAllocatedSpaceLimits(mbase, msize);
160 return mbase; 178 return mbase;
161 } 179 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 218
201 219
202 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { 220 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
203 FILE* file = fopen(name, "r+"); 221 FILE* file = fopen(name, "r+");
204 if (file == NULL) return NULL; 222 if (file == NULL) return NULL;
205 223
206 fseek(file, 0, SEEK_END); 224 fseek(file, 0, SEEK_END);
207 int size = ftell(file); 225 int size = ftell(file);
208 226
209 void* memory = 227 void* memory =
210 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); 228 mmap(GetRandomMmapAddr(), size, PROT_READ | PROT_WRITE,
Vyacheslav Egorov (Chromium) 2011/10/04 09:18:33 formatting: one argument per line
Cris Neckar 2011/10/04 19:20:47 Done.
229 MAP_SHARED, fileno(file), 0);
211 return new PosixMemoryMappedFile(file, memory, size); 230 return new PosixMemoryMappedFile(file, memory, size);
212 } 231 }
213 232
214 233
215 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, 234 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
216 void* initial) { 235 void* initial) {
217 FILE* file = fopen(name, "w+"); 236 FILE* file = fopen(name, "w+");
218 if (file == NULL) return NULL; 237 if (file == NULL) return NULL;
219 int result = fwrite(initial, size, 1, file); 238 int result = fwrite(initial, size, 1, file);
220 if (result < 1) { 239 if (result < 1) {
221 fclose(file); 240 fclose(file);
222 return NULL; 241 return NULL;
223 } 242 }
224 void* memory = 243 void* memory =
225 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); 244 mmap(GetRandomMmapAddr(), size, PROT_READ | PROT_WRITE,
Vyacheslav Egorov (Chromium) 2011/10/04 09:18:33 formatting: one argument per line
Cris Neckar 2011/10/04 19:20:47 Done.
245 MAP_SHARED, fileno(file), 0);
226 return new PosixMemoryMappedFile(file, memory, size); 246 return new PosixMemoryMappedFile(file, memory, size);
227 } 247 }
228 248
229 249
230 PosixMemoryMappedFile::~PosixMemoryMappedFile() { 250 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
231 if (memory_) OS::Free(memory_, size_); 251 if (memory_) OS::Free(memory_, size_);
232 fclose(file_); 252 fclose(file_);
233 } 253 }
234 254
235 255
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 359
340 VirtualMemory::VirtualMemory(size_t size) 360 VirtualMemory::VirtualMemory(size_t size)
341 : address_(ReserveRegion(size)), size_(size) { } 361 : address_(ReserveRegion(size)), size_(size) { }
342 362
343 363
344 VirtualMemory::VirtualMemory(size_t size, size_t alignment) 364 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
345 : address_(NULL), size_(0) { 365 : address_(NULL), size_(0) {
346 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); 366 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
347 size_t request_size = RoundUp(size + alignment, 367 size_t request_size = RoundUp(size + alignment,
348 static_cast<intptr_t>(OS::AllocateAlignment())); 368 static_cast<intptr_t>(OS::AllocateAlignment()));
349 void* reservation = mmap(NULL, 369 void* reservation = mmap(GetRandomMmapAddr(),
350 request_size, 370 request_size,
351 PROT_NONE, 371 PROT_NONE,
352 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, 372 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
353 kMmapFd, 373 kMmapFd,
354 kMmapFdOffset); 374 kMmapFdOffset);
355 if (reservation == MAP_FAILED) return; 375 if (reservation == MAP_FAILED) return;
356 376
357 Address base = static_cast<Address>(reservation); 377 Address base = static_cast<Address>(reservation);
358 Address aligned_base = RoundUp(base, alignment); 378 Address aligned_base = RoundUp(base, alignment);
359 ASSERT_LE(base, aligned_base); 379 ASSERT_LE(base, aligned_base);
(...skipping 30 matching lines...) Expand all
390 } 410 }
391 411
392 412
393 void VirtualMemory::Reset() { 413 void VirtualMemory::Reset() {
394 address_ = NULL; 414 address_ = NULL;
395 size_ = 0; 415 size_ = 0;
396 } 416 }
397 417
398 418
399 void* VirtualMemory::ReserveRegion(size_t size) { 419 void* VirtualMemory::ReserveRegion(size_t size) {
400 void* result = mmap(NULL, 420 void* result = mmap(GetRandomMmapAddr(),
401 size, 421 size,
402 PROT_NONE, 422 PROT_NONE,
403 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, 423 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
404 kMmapFd, 424 kMmapFd,
405 kMmapFdOffset); 425 kMmapFdOffset);
406 426
407 if (result == MAP_FAILED) return NULL; 427 if (result == MAP_FAILED) return NULL;
408 428
409 return result; 429 return result;
410 } 430 }
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 899
880 900
881 void Sampler::Stop() { 901 void Sampler::Stop() {
882 ASSERT(IsActive()); 902 ASSERT(IsActive());
883 SamplerThread::RemoveActiveSampler(this); 903 SamplerThread::RemoveActiveSampler(this);
884 SetActive(false); 904 SetActive(false);
885 } 905 }
886 906
887 907
888 } } // namespace v8::internal 908 } } // namespace v8::internal
OLDNEW
« src/platform-linux.cc ('K') | « src/platform-linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698