OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Platform-specific code for Win32. | 5 // Platform-specific code for Win32. |
6 | 6 |
7 // Secure API functions are not available using MinGW with msvcrt.dll | 7 // Secure API functions are not available using MinGW with msvcrt.dll |
8 // on Windows XP. Make sure MINGW_HAS_SECURE_API is not defined to | 8 // on Windows XP. Make sure MINGW_HAS_SECURE_API is not defined to |
9 // disable definition of secure API functions in standard headers that | 9 // disable definition of secure API functions in standard headers that |
10 // would conflict with our own implementation. | 10 // would conflict with our own implementation. |
11 #ifdef __MINGW32__ | 11 #ifdef __MINGW32__ |
12 #include <_mingw.h> | 12 #include <_mingw.h> |
13 #ifdef MINGW_HAS_SECURE_API | 13 #ifdef MINGW_HAS_SECURE_API |
14 #undef MINGW_HAS_SECURE_API | 14 #undef MINGW_HAS_SECURE_API |
15 #endif // MINGW_HAS_SECURE_API | 15 #endif // MINGW_HAS_SECURE_API |
16 #endif // __MINGW32__ | 16 #endif // __MINGW32__ |
17 | 17 |
18 #include "src/base/win32-headers.h" | 18 #include "src/base/win32-headers.h" |
19 | 19 |
20 #include "src/v8.h" | 20 #include "src/v8.h" |
21 | 21 |
22 #include "src/isolate-inl.h" | 22 #include "src/base/lazy-instance.h" |
23 #include "src/platform.h" | 23 #include "src/platform.h" |
| 24 #include "src/utils/random-number-generator.h" |
24 | 25 |
25 #ifdef _MSC_VER | 26 #ifdef _MSC_VER |
26 | 27 |
27 // Case-insensitive bounded string comparisons. Use stricmp() on Win32. Usually | 28 // Case-insensitive bounded string comparisons. Use stricmp() on Win32. Usually |
28 // defined in strings.h. | 29 // defined in strings.h. |
29 int strncasecmp(const char* s1, const char* s2, int n) { | 30 int strncasecmp(const char* s1, const char* s2, int n) { |
30 return _strnicmp(s1, s2, n); | 31 return _strnicmp(s1, s2, n); |
31 } | 32 } |
32 | 33 |
33 #endif // _MSC_VER | 34 #endif // _MSC_VER |
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 static size_t allocate_alignment = 0; | 702 static size_t allocate_alignment = 0; |
702 if (allocate_alignment == 0) { | 703 if (allocate_alignment == 0) { |
703 SYSTEM_INFO info; | 704 SYSTEM_INFO info; |
704 GetSystemInfo(&info); | 705 GetSystemInfo(&info); |
705 allocate_alignment = info.dwAllocationGranularity; | 706 allocate_alignment = info.dwAllocationGranularity; |
706 } | 707 } |
707 return allocate_alignment; | 708 return allocate_alignment; |
708 } | 709 } |
709 | 710 |
710 | 711 |
| 712 static base::LazyInstance<RandomNumberGenerator>::type |
| 713 platform_random_number_generator = LAZY_INSTANCE_INITIALIZER; |
| 714 |
| 715 |
| 716 void OS::SetRandomSeed(int64_t seed) { |
| 717 platform_random_number_generator.Pointer()->SetSeed(seed); |
| 718 } |
| 719 |
| 720 |
711 void* OS::GetRandomMmapAddr() { | 721 void* OS::GetRandomMmapAddr() { |
712 Isolate* isolate = Isolate::UncheckedCurrent(); | 722 // The address range used to randomize RWX allocations in OS::Allocate |
713 // Note that the current isolate isn't set up in a call path via | 723 // Try not to map pages into the default range that windows loads DLLs |
714 // CpuFeatures::Probe. We don't care about randomization in this case because | 724 // Use a multiple of 64k to prevent committing unused memory. |
715 // the code page is immediately freed. | 725 // Note: This does not guarantee RWX regions will be within the |
716 if (isolate != NULL) { | 726 // range kAllocationRandomAddressMin to kAllocationRandomAddressMax |
717 // The address range used to randomize RWX allocations in OS::Allocate | |
718 // Try not to map pages into the default range that windows loads DLLs | |
719 // Use a multiple of 64k to prevent committing unused memory. | |
720 // Note: This does not guarantee RWX regions will be within the | |
721 // range kAllocationRandomAddressMin to kAllocationRandomAddressMax | |
722 #ifdef V8_HOST_ARCH_64_BIT | 727 #ifdef V8_HOST_ARCH_64_BIT |
723 static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000; | 728 static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000; |
724 static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000; | 729 static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000; |
725 #else | 730 #else |
726 static const intptr_t kAllocationRandomAddressMin = 0x04000000; | 731 static const intptr_t kAllocationRandomAddressMin = 0x04000000; |
727 static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000; | 732 static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000; |
728 #endif | 733 #endif |
729 uintptr_t address = | 734 uintptr_t address = |
730 (isolate->random_number_generator()->NextInt() << kPageSizeBits) | | 735 (platform_random_number_generator.Pointer()->NextInt() << kPageSizeBits) | |
731 kAllocationRandomAddressMin; | 736 kAllocationRandomAddressMin; |
732 address &= kAllocationRandomAddressMax; | 737 address &= kAllocationRandomAddressMax; |
733 return reinterpret_cast<void *>(address); | 738 return reinterpret_cast<void *>(address); |
734 } | |
735 return NULL; | |
736 } | 739 } |
737 | 740 |
738 | 741 |
739 static void* RandomizedVirtualAlloc(size_t size, int action, int protection) { | 742 static void* RandomizedVirtualAlloc(size_t size, int action, int protection) { |
740 LPVOID base = NULL; | 743 LPVOID base = NULL; |
741 | 744 |
742 if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) { | 745 if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) { |
743 // For exectutable pages try and randomize the allocation address | 746 // For exectutable pages try and randomize the allocation address |
744 for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) { | 747 for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) { |
745 base = VirtualAlloc(OS::GetRandomMmapAddr(), size, action, protection); | 748 base = VirtualAlloc(OS::GetRandomMmapAddr(), size, action, protection); |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 ASSERT(result); | 1404 ASSERT(result); |
1402 } | 1405 } |
1403 | 1406 |
1404 | 1407 |
1405 | 1408 |
1406 void Thread::YieldCPU() { | 1409 void Thread::YieldCPU() { |
1407 Sleep(0); | 1410 Sleep(0); |
1408 } | 1411 } |
1409 | 1412 |
1410 } } // namespace v8::internal | 1413 } } // namespace v8::internal |
OLD | NEW |