1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "partition_alloc/address_space_randomization.h" 6 7 #include "build/build_config.h" 8 #include "partition_alloc/partition_alloc_buildflags.h" 9 #include "partition_alloc/partition_alloc_check.h" 10 #include "partition_alloc/random.h" 11 12 #if BUILDFLAG(IS_WIN) 13 #include <windows.h> 14 #endif 15 16 namespace partition_alloc { 17 GetRandomPageBase()18uintptr_t GetRandomPageBase() { 19 uintptr_t random = static_cast<uintptr_t>(internal::RandomValue()); 20 21 #if BUILDFLAG(HAS_64_BIT_POINTERS) 22 random <<= 32ULL; 23 random |= static_cast<uintptr_t>(internal::RandomValue()); 24 25 // The ASLRMask() and ASLROffset() constants will be suitable for the 26 // OS and build configuration. 27 random &= internal::ASLRMask(); 28 random += internal::ASLROffset(); 29 #else // BUILDFLAG(HAS_64_BIT_POINTERS) 30 #if BUILDFLAG(IS_WIN) 31 // On win32 host systems the randomization plus huge alignment causes 32 // excessive fragmentation. Plus most of these systems lack ASLR, so the 33 // randomization isn't buying anything. In that case we just skip it. 34 // TODO(palmer): Just dump the randomization when HE-ASLR is present. 35 static BOOL is_wow64 = -1; 36 if (is_wow64 == -1 && !IsWow64Process(GetCurrentProcess(), &is_wow64)) { 37 is_wow64 = FALSE; 38 } 39 if (!is_wow64) { 40 return 0; 41 } 42 #endif // BUILDFLAG(IS_WIN) 43 random &= internal::ASLRMask(); 44 random += internal::ASLROffset(); 45 #endif // BUILDFLAG(HAS_64_BIT_POINTERS) 46 47 PA_DCHECK(!(random & internal::PageAllocationGranularityOffsetMask())); 48 return random; 49 } 50 51 } // namespace partition_alloc 52