| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium 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 #include <fcntl.h> | 5 #include <fcntl.h> | 
| 6 #include <stdio.h> | 6 #include <stdio.h> | 
| 7 #include <stdlib.h> | 7 #include <stdlib.h> | 
| 8 #include <string.h> | 8 #include <string.h> | 
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> | 
| 10 #include <sys/types.h> | 10 #include <sys/types.h> | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 52 #else | 52 #else | 
| 53   #define TCMALLOC_TEST(function) DISABLED_##function | 53   #define TCMALLOC_TEST(function) DISABLED_##function | 
| 54 #endif | 54 #endif | 
| 55 | 55 | 
| 56 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to | 56 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to | 
| 57 // C++11. | 57 // C++11. | 
| 58 const size_t kTooBigAllocSize = INT_MAX; | 58 const size_t kTooBigAllocSize = INT_MAX; | 
| 59 | 59 | 
| 60 // Detect runtime TCMalloc bypasses. | 60 // Detect runtime TCMalloc bypasses. | 
| 61 bool IsTcMallocBypassed() { | 61 bool IsTcMallocBypassed() { | 
| 62 #if defined(OS_LINUX) || defined(OS_CHROMEOS) | 62 #if defined(OS_LINUX) | 
| 63   // This should detect a TCMalloc bypass from Valgrind. | 63   // This should detect a TCMalloc bypass from Valgrind. | 
| 64   char* g_slice = getenv("G_SLICE"); | 64   char* g_slice = getenv("G_SLICE"); | 
| 65   if (g_slice && !strcmp(g_slice, "always-malloc")) | 65   if (g_slice && !strcmp(g_slice, "always-malloc")) | 
| 66     return true; | 66     return true; | 
| 67 #elif defined(OS_WIN) | 67 #elif defined(OS_WIN) | 
| 68   // This should detect a TCMalloc bypass from setting | 68   // This should detect a TCMalloc bypass from setting | 
| 69   // the CHROME_ALLOCATOR environment variable. | 69   // the CHROME_ALLOCATOR environment variable. | 
| 70   char* allocator = getenv("CHROME_ALLOCATOR"); | 70   char* allocator = getenv("CHROME_ALLOCATOR"); | 
| 71   if (allocator && strcmp(allocator, "tcmalloc")) | 71   if (allocator && strcmp(allocator, "tcmalloc")) | 
| 72     return true; | 72     return true; | 
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 223     EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize)); | 223     EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize)); | 
| 224   } else { | 224   } else { | 
| 225     // It's also ok for calloc to just terminate the process. | 225     // It's also ok for calloc to just terminate the process. | 
| 226 #if defined(GTEST_HAS_DEATH_TEST) | 226 #if defined(GTEST_HAS_DEATH_TEST) | 
| 227     EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), ""); | 227     EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), ""); | 
| 228     EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), ""); | 228     EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), ""); | 
| 229 #endif  // GTEST_HAS_DEATH_TEST | 229 #endif  // GTEST_HAS_DEATH_TEST | 
| 230   } | 230   } | 
| 231 } | 231 } | 
| 232 | 232 | 
| 233 #if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | 233 #if defined(OS_LINUX) && defined(__x86_64__) | 
| 234 // Check if ptr1 and ptr2 are separated by less than size chars. | 234 // Check if ptr1 and ptr2 are separated by less than size chars. | 
| 235 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { | 235 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { | 
| 236   ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - | 236   ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - | 
| 237                        reinterpret_cast<char*>(std::min(ptr1, ptr2)); | 237                        reinterpret_cast<char*>(std::min(ptr1, ptr2)); | 
| 238   return static_cast<size_t>(ptr_diff) <= size; | 238   return static_cast<size_t>(ptr_diff) <= size; | 
| 239 } | 239 } | 
| 240 | 240 | 
| 241 // Check if TCMalloc uses an underlying random memory allocator. | 241 // Check if TCMalloc uses an underlying random memory allocator. | 
| 242 TEST(SecurityTest, TCMALLOC_TEST(RandomMemoryAllocations)) { | 242 TEST(SecurityTest, TCMALLOC_TEST(RandomMemoryAllocations)) { | 
| 243   if (IsTcMallocBypassed()) | 243   if (IsTcMallocBypassed()) | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 279   EXPECT_FALSE(in_default_brk_heap); | 279   EXPECT_FALSE(in_default_brk_heap); | 
| 280 | 280 | 
| 281   // In the implementation, we always mask our random addresses with | 281   // In the implementation, we always mask our random addresses with | 
| 282   // kRandomMask, so we use it as an additional detection mechanism. | 282   // kRandomMask, so we use it as an additional detection mechanism. | 
| 283   const uintptr_t kRandomMask = 0x3fffffffffffULL; | 283   const uintptr_t kRandomMask = 0x3fffffffffffULL; | 
| 284   bool impossible_random_address = | 284   bool impossible_random_address = | 
| 285       reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; | 285       reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; | 
| 286   EXPECT_FALSE(impossible_random_address); | 286   EXPECT_FALSE(impossible_random_address); | 
| 287 } | 287 } | 
| 288 | 288 | 
| 289 #endif  // (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | 289 #endif  // defined(OS_LINUX) && defined(__x86_64__) | 
| 290 | 290 | 
| 291 }  // namespace | 291 }  // namespace | 
| OLD | NEW | 
|---|