OLD | NEW |
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 <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | |
11 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
12 #include "base/file_path.h" | 11 #include "base/file_path.h" |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/path_service.h" | 14 #include "base/path_service.h" |
16 #include "base/process_util.h" | 15 #include "base/process_util.h" |
17 #include "base/test/multiprocess_test.h" | 16 #include "base/test/multiprocess_test.h" |
18 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
20 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
21 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "testing/multiprocess_func_list.h" | 22 #include "testing/multiprocess_func_list.h" |
24 | 23 |
25 #if defined(OS_LINUX) | 24 #if defined(OS_LINUX) |
| 25 #include <errno.h> |
26 #include <malloc.h> | 26 #include <malloc.h> |
27 #include <glib.h> | 27 #include <glib.h> |
28 #include <sched.h> | 28 #include <sched.h> |
29 #endif | 29 #endif |
30 #if defined(OS_POSIX) | 30 #if defined(OS_POSIX) |
31 #include <errno.h> | |
32 #include <dlfcn.h> | 31 #include <dlfcn.h> |
33 #include <fcntl.h> | 32 #include <fcntl.h> |
34 #include <signal.h> | 33 #include <signal.h> |
35 #include <sys/resource.h> | 34 #include <sys/resource.h> |
36 #include <sys/socket.h> | 35 #include <sys/socket.h> |
37 #include <sys/wait.h> | 36 #include <sys/wait.h> |
38 #endif | 37 #endif |
39 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
40 #include <windows.h> | 39 #include <windows.h> |
41 #endif | 40 #endif |
42 #if defined(OS_MACOSX) | 41 #if defined(OS_MACOSX) |
43 #include <mach/vm_param.h> | |
44 #include <malloc/malloc.h> | 42 #include <malloc/malloc.h> |
45 #include "base/process_util_unittest_mac.h" | 43 #include "base/process_util_unittest_mac.h" |
46 #endif | 44 #endif |
47 | 45 |
48 namespace { | 46 namespace { |
49 | 47 |
50 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
51 const wchar_t kProcessName[] = L"base_unittests.exe"; | 49 const wchar_t kProcessName[] = L"base_unittests.exe"; |
52 #else | 50 #else |
53 const wchar_t kProcessName[] = L"base_unittests"; | 51 const wchar_t kProcessName[] = L"base_unittests"; |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); | 442 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); |
445 base::LaunchOptions options; | 443 base::LaunchOptions options; |
446 options.as_user = token; | 444 options.as_user = token; |
447 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); | 445 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); |
448 } | 446 } |
449 | 447 |
450 #endif // defined(OS_WIN) | 448 #endif // defined(OS_WIN) |
451 | 449 |
452 #if defined(OS_MACOSX) | 450 #if defined(OS_MACOSX) |
453 | 451 |
454 // For the following Mac tests: | 452 TEST_F(ProcessUtilTest, MacTerminateOnHeapCorruption) { |
455 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | 453 // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
456 // test suite setup and does not need to be done again, else mach_override | 454 // test suite setup and does not need to be done again, else mach_override |
457 // will fail. | 455 // will fail. |
458 | 456 |
459 TEST_F(ProcessUtilTest, MacMallocFailureDoesNotTerminate) { | |
460 // Test that ENOMEM doesn't crash. The number of bytes is one less than | |
461 // MALLOC_ABSOLUTE_MAX_SIZE, above which the system early-returns NULL and | |
462 // does not call through malloc_error_break(). See the comment at | |
463 // EnableTerminationOnOutOfMemory() for more information. | |
464 void* buf = malloc(std::numeric_limits<size_t>::max() - (2 * PAGE_SIZE) - 1); | |
465 | |
466 // The optimizer can be too efficient in Release mode, so alias the value. | |
467 base::debug::Alias(buf); | |
468 | |
469 EXPECT_FALSE(buf); | |
470 EXPECT_EQ(ENOMEM, errno); | |
471 } | |
472 | |
473 TEST_F(ProcessUtilTest, MacTerminateOnHeapCorruption) { | |
474 // Assert that freeing an unallocated pointer will crash the process. | |
475 char buf[3]; | 457 char buf[3]; |
476 #ifndef ADDRESS_SANITIZER | 458 #ifndef ADDRESS_SANITIZER |
477 ASSERT_DEATH(free(buf), "being freed.*" | 459 ASSERT_DEATH(free(buf), "being freed.*" |
478 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*" | 460 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*" |
479 "Terminating process due to a potential for future heap corruption"); | 461 "Terminating process due to a potential for future heap corruption"); |
480 #else | 462 #else |
481 // AddressSanitizer replaces malloc() and prints a different error message on | 463 // AddressSanitizer replaces malloc() and prints a different error message on |
482 // heap corruption. | 464 // heap corruption. |
483 ASSERT_DEATH(free(buf), "attempting free on address which " | 465 ASSERT_DEATH(free(buf), "attempting free on address which " |
484 "was not malloc\\(\\)-ed"); | 466 "was not malloc\\(\\)-ed"); |
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1175 SetUpInDeathAssert(); | 1157 SetUpInDeathAssert(); |
1176 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | 1158 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} |
1177 }, ""); | 1159 }, ""); |
1178 } | 1160 } |
1179 | 1161 |
1180 #endif // !ARCH_CPU_64_BITS | 1162 #endif // !ARCH_CPU_64_BITS |
1181 #endif // OS_MACOSX | 1163 #endif // OS_MACOSX |
1182 | 1164 |
1183 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | 1165 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
1184 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | 1166 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
OLD | NEW |