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

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

Issue 9372083: Randomize allocation addresses on windows. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 years, 10 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
« no previous file with comments | « src/platform.h ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 811 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 // VirtualAlloc'ed blocks of memory. 822 // VirtualAlloc'ed blocks of memory.
823 size_t OS::AllocateAlignment() { 823 size_t OS::AllocateAlignment() {
824 static size_t allocate_alignment = 0; 824 static size_t allocate_alignment = 0;
825 if (allocate_alignment == 0) { 825 if (allocate_alignment == 0) {
826 SYSTEM_INFO info; 826 SYSTEM_INFO info;
827 GetSystemInfo(&info); 827 GetSystemInfo(&info);
828 allocate_alignment = info.dwAllocationGranularity; 828 allocate_alignment = info.dwAllocationGranularity;
829 } 829 }
830 return allocate_alignment; 830 return allocate_alignment;
831 } 831 }
832 832
Vyacheslav Egorov (Chromium) 2012/02/22 12:22:36 one more new line
833 833 void* OS::GetRandomAddr() {
834 void* OS::Allocate(const size_t requested,
835 size_t* allocated,
836 bool is_executable) {
837 // The address range used to randomize RWX allocations in OS::Allocate 834 // The address range used to randomize RWX allocations in OS::Allocate
838 // Try not to map pages into the default range that windows loads DLLs 835 // Try not to map pages into the default range that windows loads DLLs
839 // Use a multiple of 64k to prevent committing unused memory. 836 // Use a multiple of 64k to prevent committing unused memory.
840 // Note: This does not guarantee RWX regions will be within the 837 // Note: This does not guarantee RWX regions will be within the
841 // range kAllocationRandomAddressMin to kAllocationRandomAddressMax 838 // range kAllocationRandomAddressMin to kAllocationRandomAddressMax
842 #ifdef V8_HOST_ARCH_64_BIT 839 #ifdef V8_HOST_ARCH_64_BIT
843 static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000; 840 static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
844 static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000; 841 static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
845 #else 842 #else
846 static const intptr_t kAllocationRandomAddressMin = 0x04000000; 843 static const intptr_t kAllocationRandomAddressMin = 0x04000000;
847 static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000; 844 static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
848 #endif 845 #endif
846 uintptr_t address = (V8::RandomPrivate(Isolate::Current()) << kPageSizeBits)
847 | kAllocationRandomAddressMin;
848 address &= kAllocationRandomAddressMax;
849 return reinterpret_cast<void *>(address);
850 }
849 851
Vyacheslav Egorov (Chromium) 2012/02/22 12:22:36 one more new line
852 void* OS::Allocate(const size_t requested,
853 size_t* allocated,
854 bool is_executable) {
850 // VirtualAlloc rounds allocated size to page size automatically. 855 // VirtualAlloc rounds allocated size to page size automatically.
851 size_t msize = RoundUp(requested, static_cast<int>(GetPageSize())); 856 size_t msize = RoundUp(requested, static_cast<int>(GetPageSize()));
852 intptr_t address = 0; 857 void* address = 0;
853 858
854 // Windows XP SP2 allows Data Excution Prevention (DEP). 859 // Windows XP SP2 allows Data Excution Prevention (DEP).
855 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; 860 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
856 861
857 // For exectutable pages try and randomize the allocation address 862 // For exectutable pages try and randomize the allocation address
858 if (prot == PAGE_EXECUTE_READWRITE && 863 if (prot == PAGE_EXECUTE_READWRITE &&
859 msize >= static_cast<size_t>(Page::kPageSize)) { 864 msize >= static_cast<size_t>(Page::kPageSize)) {
860 address = (V8::RandomPrivate(Isolate::Current()) << kPageSizeBits) 865 address = OS::GetRandomAddr();
Vyacheslav Egorov (Chromium) 2012/02/22 12:22:36 I don't think GetRandomAddr has to be member of OS
861 | kAllocationRandomAddressMin;
862 address &= kAllocationRandomAddressMax;
863 } 866 }
864 867
865 LPVOID mbase = VirtualAlloc(reinterpret_cast<void *>(address), 868 LPVOID mbase = VirtualAlloc(address,
866 msize, 869 msize,
867 MEM_COMMIT | MEM_RESERVE, 870 MEM_COMMIT | MEM_RESERVE,
868 prot); 871 prot);
869 if (mbase == NULL && address != 0) 872 if (mbase == NULL && address != 0)
870 mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot); 873 mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot);
871 874
872 if (mbase == NULL) { 875 if (mbase == NULL) {
873 LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed")); 876 LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed"));
874 return NULL; 877 return NULL;
875 } 878 }
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
1464 } 1467 }
1465 1468
1466 1469
1467 bool VirtualMemory::Uncommit(void* address, size_t size) { 1470 bool VirtualMemory::Uncommit(void* address, size_t size) {
1468 ASSERT(IsReserved()); 1471 ASSERT(IsReserved());
1469 return UncommitRegion(address, size); 1472 return UncommitRegion(address, size);
1470 } 1473 }
1471 1474
1472 1475
1473 void* VirtualMemory::ReserveRegion(size_t size) { 1476 void* VirtualMemory::ReserveRegion(size_t size) {
1474 return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); 1477 void* address = 0;
1478 LPVOID mbase = NULL;
1479
1480 for (size_t attempts = 0; mbase == NULL && attempts < 3; ++attempts) {
1481 address = OS::GetRandomAddr();
1482 mbase = VirtualAlloc(address, size, MEM_RESERVE, PAGE_NOACCESS);
1483 }
1484
1485 // After three attempts give up and let the OS find an address to use.
1486 if (mbase == NULL)
1487 mbase = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
Vyacheslav Egorov (Chromium) 2012/02/22 12:22:36 we do not omit {} for if-s that span several lines
1488 return mbase;
Vyacheslav Egorov (Chromium) 2012/02/22 12:22:36 I would abstract loop and last attempt into a func
1475 } 1489 }
1476 1490
1477 1491
1478 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { 1492 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
1479 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; 1493 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
1480 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) { 1494 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) {
1481 return false; 1495 return false;
1482 } 1496 }
1483 1497
1484 UpdateAllocatedSpaceLimits(base, static_cast<int>(size)); 1498 UpdateAllocatedSpaceLimits(base, static_cast<int>(size));
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 2048
2035 2049
2036 void Sampler::Stop() { 2050 void Sampler::Stop() {
2037 ASSERT(IsActive()); 2051 ASSERT(IsActive());
2038 SamplerThread::RemoveActiveSampler(this); 2052 SamplerThread::RemoveActiveSampler(this);
2039 SetActive(false); 2053 SetActive(false);
2040 } 2054 }
2041 2055
2042 2056
2043 } } // namespace v8::internal 2057 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698