Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jln@chromium.org | 1b556f8 | 2013-01-31 02:23:43 | [diff] [blame] | 5 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 6 | #include <stddef.h> |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
jln@chromium.org | 1b556f8 | 2013-01-31 02:23:43 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 15 | #include <memory> |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 16 | |
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 17 | #include "base/allocator/buildflags.h" |
brettw@chromium.org | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 18 | #include "base/files/file_util.h" |
dcheng | db5935f | 2016-03-26 00:16:27 | [diff] [blame] | 19 | #include "base/memory/free_deleter.h" |
Peter Collingbourne | 5a35305d | 2019-02-06 02:51:43 | [diff] [blame] | 20 | #include "base/sanitizer_buildflags.h" |
jln@chromium.org | 547683f | 2013-02-04 23:39:48 | [diff] [blame] | 21 | #include "build/build_config.h" |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 24 | #if BUILDFLAG(IS_POSIX) |
jln@chromium.org | 547683f | 2013-02-04 23:39:48 | [diff] [blame] | 25 | #include <sys/mman.h> |
| 26 | #include <unistd.h> |
| 27 | #endif |
| 28 | |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 29 | using std::nothrow; |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 30 | using std::numeric_limits; |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
jln@chromium.org | fe394f3 | 2013-02-06 03:23:49 | [diff] [blame] | 34 | // This function acts as a compiler optimization barrier. We use it to |
| 35 | // prevent the compiler from making an expression a compile-time constant. |
| 36 | // We also use it so that the compiler doesn't discard certain return values |
| 37 | // as something we don't need (see the comment with calloc below). |
| 38 | template <typename Type> |
Peter Kasting | 0acb7f5ef | 2022-05-05 23:57:27 | [diff] [blame] | 39 | NOINLINE Type HideValueFromCompiler(Type value) { |
jln@chromium.org | 1cdfdb7 | 2013-04-04 12:02:35 | [diff] [blame] | 40 | #if defined(__GNUC__) |
| 41 | // In a GCC compatible compiler (GCC or Clang), make this compiler barrier |
Peter Kasting | 0acb7f5ef | 2022-05-05 23:57:27 | [diff] [blame] | 42 | // more robust. |
jln@chromium.org | 1cdfdb7 | 2013-04-04 12:02:35 | [diff] [blame] | 43 | __asm__ volatile ("" : "+r" (value)); |
| 44 | #endif // __GNUC__ |
jln@chromium.org | fe394f3 | 2013-02-06 03:23:49 | [diff] [blame] | 45 | return value; |
| 46 | } |
| 47 | |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 48 | // There are platforms where these tests are known to fail. We would like to |
| 49 | // be able to easily check the status on the bots, but marking tests as |
| 50 | // FAILS_ is too clunky. |
| 51 | void OverflowTestsSoftExpectTrue(bool overflow_detected) { |
| 52 | if (!overflow_detected) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 53 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ |
| 54 | BUILDFLAG(IS_APPLE) |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 55 | // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't |
| 56 | // fail the test, but report. |
| 57 | printf("Platform has overflow: %s\n", |
| 58 | !overflow_detected ? "yes." : "no."); |
| 59 | #else |
| 60 | // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT |
| 61 | // aren't). |
| 62 | EXPECT_TRUE(overflow_detected); |
| 63 | #endif |
| 64 | } |
| 65 | } |
| 66 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 67 | #if BUILDFLAG(IS_APPLE) || defined(ADDRESS_SANITIZER) || \ |
Sergey Ulanov | ef5b763 | 2021-09-30 14:59:08 | [diff] [blame] | 68 | defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
Yuki Shiino | cdbedb5 | 2020-08-25 09:23:03 | [diff] [blame] | 69 | BUILDFLAG(IS_HWASAN) || BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
John Abd-El-Malek | 17727ff | 2014-10-02 22:55:15 | [diff] [blame] | 70 | #define MAYBE_NewOverflow DISABLED_NewOverflow |
| 71 | #else |
| 72 | #define MAYBE_NewOverflow NewOverflow |
| 73 | #endif |
Yuki Shiino | cdbedb5 | 2020-08-25 09:23:03 | [diff] [blame] | 74 | // Test that array[TooBig][X] and array[X][TooBig] allocations fail and not |
| 75 | // succeed with the wrong size allocation in case of size_t overflow. This |
| 76 | // test is disabled on environments that operator new (nothrow) crashes in |
| 77 | // case of size_t overflow. |
| 78 | // |
| 79 | // - iOS doesn't honor nothrow. |
| 80 | // - XSan aborts when operator new returns nullptr. |
| 81 | // - PartitionAlloc crashes by design when size_t overflows. |
| 82 | // |
Sergey Ulanov | ef5b763 | 2021-09-30 14:59:08 | [diff] [blame] | 83 | // TODO(https://crbug.com/927179): Fix the test on Mac. |
John Abd-El-Malek | 17727ff | 2014-10-02 22:55:15 | [diff] [blame] | 84 | TEST(SecurityTest, MAYBE_NewOverflow) { |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 85 | const size_t kArraySize = 4096; |
| 86 | // We want something "dynamic" here, so that the compiler doesn't |
| 87 | // immediately reject crazy arrays. |
Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 88 | [[maybe_unused]] const size_t kDynamicArraySize = |
| 89 | HideValueFromCompiler(kArraySize); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 90 | const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 91 | const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 92 | const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); |
| 93 | { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 94 | std::unique_ptr<char[][kArraySize]> array_pointer( |
| 95 | new (nothrow) char[kDynamicArraySize2][kArraySize]); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 96 | // Prevent clang from optimizing away the whole test. |
| 97 | char* volatile p = reinterpret_cast<char*>(array_pointer.get()); |
| 98 | OverflowTestsSoftExpectTrue(!p); |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 99 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 100 | #if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS) |
Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 101 | // On Windows, the compiler prevents static array sizes of more than |
| 102 | // 0x7fffffff (error C2148). |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 103 | #else |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 104 | { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 105 | std::unique_ptr<char[][kArraySize2]> array_pointer( |
| 106 | new (nothrow) char[kDynamicArraySize][kArraySize2]); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 107 | // Prevent clang from optimizing away the whole test. |
| 108 | char* volatile p = reinterpret_cast<char*>(array_pointer.get()); |
| 109 | OverflowTestsSoftExpectTrue(!p); |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 110 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 111 | #endif // BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS) |
jln@chromium.org | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 112 | } |
| 113 | |
jln@chromium.org | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 114 | } // namespace |