Chromium Code Reviews| Index: src/platform-linux.cc |
| =================================================================== |
| --- src/platform-linux.cc (revision 8657) |
| +++ src/platform-linux.cc (working copy) |
| @@ -78,13 +78,35 @@ |
| static Mutex* limit_mutex = NULL; |
| +static void* GetRandomMmapAddr() { |
| + Isolate* isolate; |
| + // Note that the current isolate isn't set up in a call path via |
| + // CpuFeatures::Probe. We don't care about randomization in this case because |
| + // the code page is immediately freed. |
| + if (sizeof(void*) == 8 && (isolate = Isolate::UncheckedCurrent())) { |
|
Vyacheslav Egorov (Chromium)
2011/07/15 11:36:23
I would prefer conditional compilation based on V8
|
| + uint32_t rnd1 = V8::RandomPrivate(isolate); |
| + uint32_t rnd2 = V8::RandomPrivate(isolate); |
| + uint64_t raw_addr = (static_cast<uint64_t>(rnd1) << 33) | |
|
Vyacheslav Egorov (Chromium)
2011/07/15 12:29:39
I think it should be 34 not 33.
William Hesse
2011/07/15 12:43:22
All of the masking can be replaced by
raw_addr =
|
| + (static_cast<uint64_t>(rnd2) << 2); |
| + // Mask off the lower 12 and upper 18 bits. (v8 compile options do not |
| + // permit the use of a 64-bit constant). |
|
William Hesse
2011/07/15 12:43:22
There are macros in globals.h to enter 64-bit cons
|
| + raw_addr <<= 18; |
|
Vyacheslav Egorov (Chromium)
2011/07/15 12:29:39
Look into globals.h. There are macroses that allow
|
| + raw_addr >>= 12 + 18; |
| + raw_addr <<= 12; |
| + return reinterpret_cast<void*>(raw_addr); |
| + } else { |
| + return NULL; |
| + } |
| +} |
| + |
| + |
| void OS::Setup() { |
| // Seed the random number generator. |
| // Convert the current time to a 64-bit integer first, before converting it |
| // to an unsigned. Going directly can cause an overflow and the seed to be |
| // set to all ones. The seed will be identical for different instances that |
| // call this setup code within the same millisecond. |
|
William Hesse
2011/07/15 12:43:22
Use XOR, not OR, to combine the overlapping seed s
|
| - uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| + uint64_t seed = Ticks() | (getpid() << 16); |
|
Vyacheslav Egorov (Chromium)
2011/07/15 11:36:23
I think the comment above needs updating.
|
| srandom(static_cast<unsigned int>(seed)); |
| limit_mutex = CreateMutex(); |
| @@ -367,10 +389,10 @@ |
| void* OS::Allocate(const size_t requested, |
| size_t* allocated, |
| bool is_executable) { |
| - // TODO(805): Port randomization of allocated executable memory to Linux. |
| const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); |
| int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| - void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| + void* addr = GetRandomMmapAddr(); |
| + void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| if (mbase == MAP_FAILED) { |
|
William Hesse
2011/07/15 12:43:22
I think you need to add the MAP_VARIABLE flag to t
|
| LOG(i::Isolate::Current(), |
| StringEvent("OS::Allocate", "mmap failed")); |
| @@ -586,7 +608,7 @@ |
| VirtualMemory::VirtualMemory(size_t size) { |
| - address_ = mmap(NULL, size, PROT_NONE, |
| + address_ = mmap(GetRandomMmapAddr(), size, PROT_NONE, |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
| kMmapFd, kMmapFdOffset); |
| size_ = size; |