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

Side by Side Diff: base/security_unittest.cc

Issue 774683003: Remove tcmalloc when not being used. Restore shim on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix nits. add new OOM death test. Created 5 years, 11 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
OLDNEW
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>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <limits> 13 #include <limits>
14 14
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 #if defined(OS_POSIX) 21 #if defined(OS_POSIX)
22 #include <sys/mman.h> 22 #include <sys/mman.h>
23 #include <unistd.h> 23 #include <unistd.h>
24 #endif 24 #endif
25 25
26 #if defined(OS_WIN)
27 #include <new.h>
28 #endif
29
26 using std::nothrow; 30 using std::nothrow;
27 using std::numeric_limits; 31 using std::numeric_limits;
28 32
29 namespace { 33 namespace {
30 34
35 #if defined(OS_WIN)
36 int OnNoMemory(size_t) {
37 _exit(1);
38 }
39
40 void exhaustMemory() {
scottmg 2015/01/12 16:51:01 nit; exhaustMemory -> ExhaustMemory
Will Harris 2015/01/12 19:03:44 Done.
41 for (;;) {
42 // This is a permitted size but exhausts memory pretty quickly.
43 void* buf = malloc(0x7FFFE000);
44 if (!buf)
45 break;
46 }
47 }
48 #endif
49
31 // This function acts as a compiler optimization barrier. We use it to 50 // This function acts as a compiler optimization barrier. We use it to
32 // prevent the compiler from making an expression a compile-time constant. 51 // prevent the compiler from making an expression a compile-time constant.
33 // We also use it so that the compiler doesn't discard certain return values 52 // We also use it so that the compiler doesn't discard certain return values
34 // as something we don't need (see the comment with calloc below). 53 // as something we don't need (see the comment with calloc below).
35 template <typename Type> 54 template <typename Type>
36 Type HideValueFromCompiler(volatile Type value) { 55 Type HideValueFromCompiler(volatile Type value) {
37 #if defined(__GNUC__) 56 #if defined(__GNUC__)
38 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier 57 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
39 // more robust than merely using "volatile". 58 // more robust than merely using "volatile".
40 __asm__ volatile ("" : "+r" (value)); 59 __asm__ volatile ("" : "+r" (value));
41 #endif // __GNUC__ 60 #endif // __GNUC__
42 return value; 61 return value;
43 } 62 }
44 63
64 // Tcmalloc and Windows allocator shim support setting malloc limits.
45 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc") 65 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
46 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator 66 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator
47 // - IOS does not use tcmalloc 67 // - IOS does not use tcmalloc
48 // - OS_MACOSX does not use tcmalloc 68 // - OS_MACOSX does not use tcmalloc
49 #if !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER) && \ 69 // - Windows allocator shim defines ALLOCATOR_SHIM
50 !defined(OS_IOS) && !defined(OS_MACOSX) && !defined(SYZYASAN) 70 #if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \
51 #define TCMALLOC_TEST(function) function 71 !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \
72 !defined(SYZYASAN)
73 #define MALLOC_OVERFLOW_TEST(function) function
52 #else 74 #else
53 #define TCMALLOC_TEST(function) DISABLED_##function 75 #define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
54 #endif 76 #endif
55 77
56 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to 78 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to
57 // C++11. 79 // C++11.
58 const size_t kTooBigAllocSize = INT_MAX; 80 const size_t kTooBigAllocSize = INT_MAX;
59 81
60 // Detect runtime TCMalloc bypasses. 82 // Detect runtime TCMalloc bypasses.
61 bool IsTcMallocBypassed() { 83 bool IsTcMallocBypassed() {
62 #if defined(OS_LINUX) 84 #if defined(OS_LINUX)
63 // This should detect a TCMalloc bypass from Valgrind. 85 // This should detect a TCMalloc bypass from Valgrind.
64 char* g_slice = getenv("G_SLICE"); 86 char* g_slice = getenv("G_SLICE");
65 if (g_slice && !strcmp(g_slice, "always-malloc")) 87 if (g_slice && !strcmp(g_slice, "always-malloc"))
66 return true; 88 return true;
67 #elif defined(OS_WIN)
68 // This should detect a TCMalloc bypass from setting
69 // the CHROME_ALLOCATOR environment variable.
70 char* allocator = getenv("CHROME_ALLOCATOR");
71 if (allocator && strcmp(allocator, "tcmalloc"))
72 return true;
73 #endif 89 #endif
74 return false; 90 return false;
75 } 91 }
76 92
77 bool CallocDiesOnOOM() { 93 bool CallocDiesOnOOM() {
78 // The sanitizers' calloc dies on OOM instead of returning NULL. 94 // The sanitizers' calloc dies on OOM instead of returning NULL.
79 // The wrapper function in base/process_util_linux.cc that is used when we 95 // The wrapper function in base/process_util_linux.cc that is used when we
80 // compile without TCMalloc will just die on OOM instead of returning NULL. 96 // compile without TCMalloc will just die on OOM instead of returning NULL.
81 #if defined(ADDRESS_SANITIZER) || \ 97 #if defined(ADDRESS_SANITIZER) || \
82 defined(MEMORY_SANITIZER) || \ 98 defined(MEMORY_SANITIZER) || \
83 defined(THREAD_SANITIZER) || \ 99 defined(THREAD_SANITIZER) || \
84 (defined(OS_LINUX) && defined(NO_TCMALLOC)) 100 (defined(OS_LINUX) && defined(NO_TCMALLOC))
85 return true; 101 return true;
86 #else 102 #else
87 return false; 103 return false;
88 #endif 104 #endif
89 } 105 }
90 106
91 // Fake test that allow to know the state of TCMalloc by looking at bots. 107 // Fake test that allow to know the state of TCMalloc by looking at bots.
92 TEST(SecurityTest, TCMALLOC_TEST(IsTCMallocDynamicallyBypassed)) { 108 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(IsTCMallocDynamicallyBypassed)) {
93 printf("Malloc is dynamically bypassed: %s\n", 109 printf("Malloc is dynamically bypassed: %s\n",
94 IsTcMallocBypassed() ? "yes." : "no."); 110 IsTcMallocBypassed() ? "yes." : "no.");
95 } 111 }
96 112
97 // The MemoryAllocationRestrictions* tests test that we can not allocate a 113 // The MemoryAllocationRestrictions* tests test that we can not allocate a
98 // memory range that cannot be indexed via an int. This is used to mitigate 114 // memory range that cannot be indexed via an int. This is used to mitigate
99 // vulnerabilities in libraries that use int instead of size_t. See 115 // vulnerabilities in libraries that use int instead of size_t. See
100 // crbug.com/169327. 116 // crbug.com/169327.
101 117
102 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsMalloc)) { 118 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsMalloc)) {
103 if (!IsTcMallocBypassed()) { 119 if (!IsTcMallocBypassed()) {
104 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( 120 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
105 HideValueFromCompiler(malloc(kTooBigAllocSize)))); 121 HideValueFromCompiler(malloc(kTooBigAllocSize))));
106 ASSERT_TRUE(!ptr); 122 ASSERT_TRUE(!ptr);
107 } 123 }
108 } 124 }
109 125
110 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsCalloc)) { 126 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN)
127 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationMallocDeathTest)) {
128 if (!IsTcMallocBypassed()) {
scottmg 2015/01/12 16:51:01 Remove the IsTcMallocBypassed's for OS_WIN? (and b
Will Harris 2015/01/12 19:03:44 Done.
129 _set_new_handler(&OnNoMemory);
130 _set_new_mode(1);
131 {
132 scoped_ptr<char, base::FreeDeleter> ptr;
133 EXPECT_DEATH(ptr.reset(static_cast<char*>(
134 HideValueFromCompiler(malloc(kTooBigAllocSize)))),
scottmg 2015/01/12 16:51:01 Maybe a similar test that uses realloc instead of
Will Harris 2015/01/12 19:03:44 Done.
135 "");
136 ASSERT_TRUE(!ptr);
137 }
138 _set_new_handler(NULL);
139 _set_new_mode(0);
140 }
141 }
142
143 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationExhaustDeathTest)) {
144 if (!IsTcMallocBypassed()) {
145 _set_new_handler(&OnNoMemory);
146 _set_new_mode(1);
147 {
148 ASSERT_DEATH(exhaustMemory(), "");
149 }
150 _set_new_handler(NULL);
151 _set_new_mode(0);
152 }
153 }
154
155 #endif
156
157 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsCalloc)) {
111 if (!IsTcMallocBypassed()) { 158 if (!IsTcMallocBypassed()) {
112 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( 159 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
113 HideValueFromCompiler(calloc(kTooBigAllocSize, 1)))); 160 HideValueFromCompiler(calloc(kTooBigAllocSize, 1))));
114 ASSERT_TRUE(!ptr); 161 ASSERT_TRUE(!ptr);
115 } 162 }
116 } 163 }
117 164
118 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsRealloc)) { 165 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsRealloc)) {
119 if (!IsTcMallocBypassed()) { 166 if (!IsTcMallocBypassed()) {
120 char* orig_ptr = static_cast<char*>(malloc(1)); 167 char* orig_ptr = static_cast<char*>(malloc(1));
121 ASSERT_TRUE(orig_ptr); 168 ASSERT_TRUE(orig_ptr);
122 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( 169 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
123 HideValueFromCompiler(realloc(orig_ptr, kTooBigAllocSize)))); 170 HideValueFromCompiler(realloc(orig_ptr, kTooBigAllocSize))));
124 ASSERT_TRUE(!ptr); 171 ASSERT_TRUE(!ptr);
125 // If realloc() did not succeed, we need to free orig_ptr. 172 // If realloc() did not succeed, we need to free orig_ptr.
126 free(orig_ptr); 173 free(orig_ptr);
127 } 174 }
128 } 175 }
129 176
130 typedef struct { 177 typedef struct {
131 char large_array[kTooBigAllocSize]; 178 char large_array[kTooBigAllocSize];
132 } VeryLargeStruct; 179 } VeryLargeStruct;
133 180
134 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNew)) { 181 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNew)) {
135 if (!IsTcMallocBypassed()) { 182 if (!IsTcMallocBypassed()) {
136 scoped_ptr<VeryLargeStruct> ptr( 183 scoped_ptr<VeryLargeStruct> ptr(
137 HideValueFromCompiler(new (nothrow) VeryLargeStruct)); 184 HideValueFromCompiler(new (nothrow) VeryLargeStruct));
138 ASSERT_TRUE(!ptr); 185 ASSERT_TRUE(!ptr);
139 } 186 }
140 } 187 }
141 188
142 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNewArray)) { 189 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN)
190 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationNewDeathTest)) {
191 if (!IsTcMallocBypassed()) {
192 _set_new_handler(&OnNoMemory);
193 {
194 scoped_ptr<VeryLargeStruct> ptr;
195 EXPECT_DEATH(
196 ptr.reset(HideValueFromCompiler(new (nothrow) VeryLargeStruct)), "");
197 ASSERT_TRUE(!ptr);
198 }
199 _set_new_handler(NULL);
200 }
201 }
202 #endif
203
204 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNewArray)) {
143 if (!IsTcMallocBypassed()) { 205 if (!IsTcMallocBypassed()) {
144 scoped_ptr<char[]> ptr( 206 scoped_ptr<char[]> ptr(
145 HideValueFromCompiler(new (nothrow) char[kTooBigAllocSize])); 207 HideValueFromCompiler(new (nothrow) char[kTooBigAllocSize]));
146 ASSERT_TRUE(!ptr); 208 ASSERT_TRUE(!ptr);
147 } 209 }
148 } 210 }
149 211
150 // The tests bellow check for overflows in new[] and calloc(). 212 // The tests bellow check for overflows in new[] and calloc().
151 213
152 // There are platforms where these tests are known to fail. We would like to 214 // There are platforms where these tests are known to fail. We would like to
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 297
236 #if defined(OS_LINUX) && defined(__x86_64__) 298 #if defined(OS_LINUX) && defined(__x86_64__)
237 // Check if ptr1 and ptr2 are separated by less than size chars. 299 // Check if ptr1 and ptr2 are separated by less than size chars.
238 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { 300 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) {
239 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - 301 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) -
240 reinterpret_cast<char*>(std::min(ptr1, ptr2)); 302 reinterpret_cast<char*>(std::min(ptr1, ptr2));
241 return static_cast<size_t>(ptr_diff) <= size; 303 return static_cast<size_t>(ptr_diff) <= size;
242 } 304 }
243 305
244 // Check if TCMalloc uses an underlying random memory allocator. 306 // Check if TCMalloc uses an underlying random memory allocator.
245 TEST(SecurityTest, TCMALLOC_TEST(RandomMemoryAllocations)) { 307 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) {
246 if (IsTcMallocBypassed()) 308 if (IsTcMallocBypassed())
247 return; 309 return;
248 size_t kPageSize = 4096; // We support x86_64 only. 310 size_t kPageSize = 4096; // We support x86_64 only.
249 // Check that malloc() returns an address that is neither the kernel's 311 // Check that malloc() returns an address that is neither the kernel's
250 // un-hinted mmap area, nor the current brk() area. The first malloc() may 312 // un-hinted mmap area, nor the current brk() area. The first malloc() may
251 // not be at a random address because TCMalloc will first exhaust any memory 313 // not be at a random address because TCMalloc will first exhaust any memory
252 // that it has allocated early on, before starting the sophisticated 314 // that it has allocated early on, before starting the sophisticated
253 // allocators. 315 // allocators.
254 void* default_mmap_heap_address = 316 void* default_mmap_heap_address =
255 mmap(0, kPageSize, PROT_READ|PROT_WRITE, 317 mmap(0, kPageSize, PROT_READ|PROT_WRITE,
(...skipping 29 matching lines...) Expand all
285 // kRandomMask, so we use it as an additional detection mechanism. 347 // kRandomMask, so we use it as an additional detection mechanism.
286 const uintptr_t kRandomMask = 0x3fffffffffffULL; 348 const uintptr_t kRandomMask = 0x3fffffffffffULL;
287 bool impossible_random_address = 349 bool impossible_random_address =
288 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; 350 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask;
289 EXPECT_FALSE(impossible_random_address); 351 EXPECT_FALSE(impossible_random_address);
290 } 352 }
291 353
292 #endif // defined(OS_LINUX) && defined(__x86_64__) 354 #endif // defined(OS_LINUX) && defined(__x86_64__)
293 355
294 } // namespace 356 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698