OLD | NEW |
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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 | 72 |
73 double ceiling(double x) { | 73 double ceiling(double x) { |
74 return ceil(x); | 74 return ceil(x); |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 static Mutex* limit_mutex = NULL; | 78 static Mutex* limit_mutex = NULL; |
79 | 79 |
80 | 80 |
| 81 static void* GetRandomMmapAddr() { |
| 82 Isolate* isolate = Isolate::UncheckedCurrent(); |
| 83 // Note that the current isolate isn't set up in a call path via |
| 84 // CpuFeatures::Probe. We don't care about randomization in this case because |
| 85 // the code page is immediately freed. |
| 86 if (isolate != NULL) { |
| 87 #ifdef V8_TARGET_ARCH_X64 |
| 88 uint64_t rnd1 = V8::RandomPrivate(isolate); |
| 89 uint64_t rnd2 = V8::RandomPrivate(isolate); |
| 90 uint64_t raw_addr = (rnd1 << 32) ^ rnd2; |
| 91 raw_addr &= V8_UINT64_C(0x3ffffffff000); |
| 92 #else |
| 93 uint32_t raw_addr = V8::RandomPrivate(isolate); |
| 94 // The range 0x20000000 - 0x60000000 is relatively unpopulated across a |
| 95 // variety of ASLR modes (PAE kernel, NX compat mode, etc). |
| 96 raw_addr &= 0x3ffff000; |
| 97 raw_addr += 0x20000000; |
| 98 #endif |
| 99 return reinterpret_cast<void*>(raw_addr); |
| 100 } |
| 101 return NULL; |
| 102 } |
| 103 |
| 104 |
81 void OS::Setup() { | 105 void OS::Setup() { |
82 // Seed the random number generator. | 106 // Seed the random number generator. We preserve microsecond resolution. |
83 // Convert the current time to a 64-bit integer first, before converting it | 107 uint64_t seed = Ticks() ^ (getpid() << 16); |
84 // to an unsigned. Going directly can cause an overflow and the seed to be | |
85 // set to all ones. The seed will be identical for different instances that | |
86 // call this setup code within the same millisecond. | |
87 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); | |
88 srandom(static_cast<unsigned int>(seed)); | 108 srandom(static_cast<unsigned int>(seed)); |
89 limit_mutex = CreateMutex(); | 109 limit_mutex = CreateMutex(); |
90 | 110 |
91 #ifdef __arm__ | 111 #ifdef __arm__ |
92 // When running on ARM hardware check that the EABI used by V8 and | 112 // When running on ARM hardware check that the EABI used by V8 and |
93 // by the C code is the same. | 113 // by the C code is the same. |
94 bool hard_float = OS::ArmUsingHardFloat(); | 114 bool hard_float = OS::ArmUsingHardFloat(); |
95 if (hard_float) { | 115 if (hard_float) { |
96 #if !USE_EABI_HARDFLOAT | 116 #if !USE_EABI_HARDFLOAT |
97 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " | 117 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 380 |
361 | 381 |
362 size_t OS::AllocateAlignment() { | 382 size_t OS::AllocateAlignment() { |
363 return sysconf(_SC_PAGESIZE); | 383 return sysconf(_SC_PAGESIZE); |
364 } | 384 } |
365 | 385 |
366 | 386 |
367 void* OS::Allocate(const size_t requested, | 387 void* OS::Allocate(const size_t requested, |
368 size_t* allocated, | 388 size_t* allocated, |
369 bool is_executable) { | 389 bool is_executable) { |
370 // TODO(805): Port randomization of allocated executable memory to Linux. | |
371 const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); | 390 const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE)); |
372 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 391 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
373 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 392 void* addr = GetRandomMmapAddr(); |
| 393 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
374 if (mbase == MAP_FAILED) { | 394 if (mbase == MAP_FAILED) { |
375 LOG(i::Isolate::Current(), | 395 LOG(i::Isolate::Current(), |
376 StringEvent("OS::Allocate", "mmap failed")); | 396 StringEvent("OS::Allocate", "mmap failed")); |
377 return NULL; | 397 return NULL; |
378 } | 398 } |
379 *allocated = msize; | 399 *allocated = msize; |
380 UpdateAllocatedSpaceLimits(mbase, msize); | 400 UpdateAllocatedSpaceLimits(mbase, msize); |
381 return mbase; | 401 return mbase; |
382 } | 402 } |
383 | 403 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 #endif // ndef __GLIBC__ | 599 #endif // ndef __GLIBC__ |
580 } | 600 } |
581 | 601 |
582 | 602 |
583 // Constants used for mmap. | 603 // Constants used for mmap. |
584 static const int kMmapFd = -1; | 604 static const int kMmapFd = -1; |
585 static const int kMmapFdOffset = 0; | 605 static const int kMmapFdOffset = 0; |
586 | 606 |
587 | 607 |
588 VirtualMemory::VirtualMemory(size_t size) { | 608 VirtualMemory::VirtualMemory(size_t size) { |
589 address_ = mmap(NULL, size, PROT_NONE, | 609 address_ = mmap(GetRandomMmapAddr(), size, PROT_NONE, |
590 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, | 610 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
591 kMmapFd, kMmapFdOffset); | 611 kMmapFd, kMmapFdOffset); |
592 size_ = size; | 612 size_ = size; |
593 } | 613 } |
594 | 614 |
595 | 615 |
596 VirtualMemory::~VirtualMemory() { | 616 VirtualMemory::~VirtualMemory() { |
597 if (IsReserved()) { | 617 if (IsReserved()) { |
598 if (0 == munmap(address(), size())) address_ = MAP_FAILED; | 618 if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
599 } | 619 } |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 | 1141 |
1122 | 1142 |
1123 void Sampler::Stop() { | 1143 void Sampler::Stop() { |
1124 ASSERT(IsActive()); | 1144 ASSERT(IsActive()); |
1125 SignalSender::RemoveActiveSampler(this); | 1145 SignalSender::RemoveActiveSampler(this); |
1126 SetActive(false); | 1146 SetActive(false); |
1127 } | 1147 } |
1128 | 1148 |
1129 | 1149 |
1130 } } // namespace v8::internal | 1150 } } // namespace v8::internal |
OLD | NEW |