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

Side by Side Diff: base/process/memory_unittest.cc

Issue 55333002: Make possible to check memory allocations inside chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improve testing code Created 6 years, 9 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
« no previous file with comments | « base/process/memory_mac.mm ('k') | base/process/memory_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #define _CRT_SECURE_NO_WARNINGS 5 #define _CRT_SECURE_NO_WARNINGS
6 6
7 #include "base/process/memory.h" 7 #include "base/process/memory.h"
8 8
9 #include <limits> 9 #include <limits>
10 10
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 "was not malloc\\(\\)-ed"); 142 "was not malloc\\(\\)-ed");
143 #else 143 #else
144 ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*" 144 ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*"
145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" 145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*"
146 "Terminating process due to a potential for future heap corruption"); 146 "Terminating process due to a potential for future heap corruption");
147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) 147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER)
148 } 148 }
149 149
150 #endif // defined(OS_MACOSX) 150 #endif // defined(OS_MACOSX)
151 151
152 // Android doesn't implement set_new_handler, so we can't use the
153 // OutOfMemoryTest cases.
154 // OpenBSD does not support these tests either.
155 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc.
156 // functions so that they don't crash if the program is out of memory, so the
157 // OOM tests aren't supposed to work.
158 // TODO(vandebo) make this work on Windows too.
159 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
160 !defined(OS_WIN) && \
161 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER)
162
163 #if defined(USE_TCMALLOC) 152 #if defined(USE_TCMALLOC)
164 extern "C" { 153 extern "C" {
165 int tc_set_new_mode(int mode); 154 int tc_set_new_mode(int mode);
166 } 155 }
167 #endif // defined(USE_TCMALLOC) 156 #endif // defined(USE_TCMALLOC)
168 157
169 class OutOfMemoryDeathTest : public testing::Test { 158 class MemoryTest : public testing::Test {
170 public: 159 public:
171 OutOfMemoryDeathTest() 160 MemoryTest()
172 : value_(NULL), 161 : value_(NULL),
173 // Make test size as large as possible minus a few pages so 162 // Make test size as large as possible minus a few pages so
174 // that alignment or other rounding doesn't make it wrap. 163 // that alignment or other rounding doesn't make it wrap.
175 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), 164 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024),
176 signed_test_size_(std::numeric_limits<ssize_t>::max()) { 165 signed_test_size_(std::numeric_limits<ssize_t>::max()) {
177 } 166 }
178 167
179 #if defined(USE_TCMALLOC) 168 #if defined(USE_TCMALLOC)
Scott Hess - ex-Googler 2014/03/18 21:10:38 Indentation.
180 virtual void SetUp() OVERRIDE { 169 virtual void SetUp() OVERRIDE {
181 tc_set_new_mode(1); 170 tc_set_new_mode(1);
182 } 171 }
183 172
184 virtual void TearDown() OVERRIDE { 173 virtual void TearDown() OVERRIDE {
185 tc_set_new_mode(0); 174 tc_set_new_mode(0);
186 } 175 }
187 #endif // defined(USE_TCMALLOC) 176 #endif // defined(USE_TCMALLOC)
188 177
189 void SetUpInDeathAssert() { 178 protected:
190 // Must call EnableTerminationOnOutOfMemory() because that is called from
191 // chrome's main function and therefore hasn't been called yet.
192 // Since this call may result in another thread being created and death
193 // tests shouldn't be started in a multithread environment, this call
194 // should be done inside of the ASSERT_DEATH.
195 base::EnableTerminationOnOutOfMemory();
196 }
197
198 void* value_; 179 void* value_;
199 size_t test_size_; 180 size_t test_size_;
200 ssize_t signed_test_size_; 181 ssize_t signed_test_size_;
201 }; 182 };
202 183
184 class OutOfMemoryDeathTest : public MemoryTest {
185 public:
186 void SetUpInDeathAssert() {
Scott Hess - ex-Googler 2014/03/18 21:10:38 I think the function is indented 2 spaces too much
187 // Must call EnableTerminationOnOutOfMemory() because that is called from
188 // chrome's main function and therefore hasn't been called yet.
189 // Since this call may result in another thread being created and death
190 // tests shouldn't be started in a multithread environment, this call
191 // should be done inside of the ASSERT_DEATH.
192 base::EnableTerminationOnOutOfMemory();
193 }
194 };
195
196 // Android doesn't implement set_new_handler, so we can't use the
197 // OutOfMemoryTest cases.
198 // OpenBSD does not support these tests either.
199 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc.
200 // functions so that they don't crash if the program is out of memory, so the
201 // OOM tests aren't supposed to work.
202 // TODO(vandebo) make this work on Windows too.
203 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
204 !defined(OS_WIN) && \
205 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER)
206
203 TEST_F(OutOfMemoryDeathTest, New) { 207 TEST_F(OutOfMemoryDeathTest, New) {
204 ASSERT_DEATH({ 208 ASSERT_DEATH({
205 SetUpInDeathAssert(); 209 SetUpInDeathAssert();
206 value_ = operator new(test_size_); 210 value_ = operator new(test_size_);
207 }, ""); 211 }, "");
208 } 212 }
209 213
210 TEST_F(OutOfMemoryDeathTest, NewArray) { 214 TEST_F(OutOfMemoryDeathTest, NewArray) {
211 ASSERT_DEATH({ 215 ASSERT_DEATH({
212 SetUpInDeathAssert(); 216 SetUpInDeathAssert();
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 SetUpInDeathAssert(); 374 SetUpInDeathAssert();
371 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} 375 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {}
372 }, ""); 376 }, "");
373 } 377 }
374 378
375 #endif // !ARCH_CPU_64_BITS 379 #endif // !ARCH_CPU_64_BITS
376 #endif // OS_MACOSX 380 #endif // OS_MACOSX
377 381
378 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && 382 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) &&
379 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) 383 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER)
384
385 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work
386 // well on Windows.
387 #if defined(USE_TCMALLOC) || defined(LIBC_GLIBC)
388
389 class OutOfMemoryHandledTest : public MemoryTest {
390 public:
391 static const size_t kSafeMallocSize = 512;
392 static const size_t kSafeCallocSize = 128;
393 static const size_t kSafeCallocItems = 4;
394
395 virtual void SetUp() {
396 MemoryTest::SetUp();
397 base::EnableTerminationOnOutOfMemory();
Scott Hess - ex-Googler 2014/03/18 21:10:38 Suggest a comment on this line about being importa
398 }
399 };
400
401 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) {
402 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_));
403 EXPECT_TRUE(value_ != NULL);
404 free(value_);
405
406 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_));
407 EXPECT_TRUE(value_ == NULL);
408 }
409
410 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) {
411 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_));
412 EXPECT_TRUE(value_ != NULL);
413 const char* bytes = static_cast<const char*>(value_);
414 for (size_t i = 0; i < kSafeMallocSize; ++i)
415 EXPECT_EQ(bytes[i], 0);
Scott Hess - ex-Googler 2014/03/18 21:10:38 EXPECT_EQ(expected, actual), so 0 on the left, byt
416 free(value_);
417
418 EXPECT_TRUE(
419 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_));
420 EXPECT_TRUE(value_ != NULL);
421 bytes = static_cast<const char*>(value_);
422 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i)
423 EXPECT_EQ(bytes[i], 0);
Scott Hess - ex-Googler 2014/03/18 21:10:38 Also here.
424 free(value_);
425
426 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_));
427 EXPECT_TRUE(value_ == NULL);
428 }
429
430 #endif
OLDNEW
« no previous file with comments | « base/process/memory_mac.mm ('k') | base/process/memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698