blob: c879b5be6dfda589529936ea5678ca054b4860bb [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]b5bf9a132013-01-15 20:16:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tom Sepez8726d30e2025-01-29 02:11:085#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
7#pragma allow_unsafe_libc_calls
8#endif
9
[email protected]1b556f82013-01-31 02:23:4310#include <fcntl.h>
avi9b6f42932015-12-26 22:15:1411#include <stddef.h>
[email protected]b5bf9a132013-01-15 20:16:3312#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
[email protected]1b556f82013-01-31 02:23:4315#include <sys/stat.h>
16#include <sys/types.h>
[email protected]b5bf9a132013-01-15 20:16:3317
18#include <algorithm>
19#include <limits>
dcheng093de9b2016-04-04 21:25:5120#include <memory>
[email protected]b5bf9a132013-01-15 20:16:3321
[email protected]e3177dd52014-08-13 20:22:1422#include "base/files/file_util.h"
dchengdb5935f2016-03-26 00:16:2723#include "base/memory/free_deleter.h"
Peter Collingbourne5a35305d2019-02-06 02:51:4324#include "base/sanitizer_buildflags.h"
[email protected]547683f2013-02-04 23:39:4825#include "build/build_config.h"
Arthur Sonzognifd39d612024-06-26 08:16:2326#include "partition_alloc/buildflags.h"
[email protected]b5bf9a132013-01-15 20:16:3327#include "testing/gtest/include/gtest/gtest.h"
28
Xiaohan Wang38e4ebb2022-01-19 06:57:4329#if BUILDFLAG(IS_POSIX)
[email protected]547683f2013-02-04 23:39:4830#include <sys/mman.h>
31#include <unistd.h>
32#endif
33
[email protected]b5bf9a132013-01-15 20:16:3334using std::nothrow;
[email protected]9c4729b2013-01-26 04:41:1535using std::numeric_limits;
[email protected]b5bf9a132013-01-15 20:16:3336
37namespace {
38
[email protected]fe394f32013-02-06 03:23:4939// This function acts as a compiler optimization barrier. We use it to
40// prevent the compiler from making an expression a compile-time constant.
41// We also use it so that the compiler doesn't discard certain return values
42// as something we don't need (see the comment with calloc below).
43template <typename Type>
Peter Kasting0acb7f5ef2022-05-05 23:57:2744NOINLINE Type HideValueFromCompiler(Type value) {
[email protected]1cdfdb72013-04-04 12:02:3545#if defined(__GNUC__)
46 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
Peter Kasting0acb7f5ef2022-05-05 23:57:2747 // more robust.
Peter Kasting134ef9af2024-12-28 02:30:0948 __asm__ volatile("" : "+r"(value));
[email protected]1cdfdb72013-04-04 12:02:3549#endif // __GNUC__
[email protected]fe394f32013-02-06 03:23:4950 return value;
51}
52
[email protected]9c4729b2013-01-26 04:41:1553// There are platforms where these tests are known to fail. We would like to
54// be able to easily check the status on the bots, but marking tests as
55// FAILS_ is too clunky.
56void OverflowTestsSoftExpectTrue(bool overflow_detected) {
57 if (!overflow_detected) {
Xiaohan Wang38e4ebb2022-01-19 06:57:4358#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
59 BUILDFLAG(IS_APPLE)
[email protected]9c4729b2013-01-26 04:41:1560 // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't
61 // fail the test, but report.
Peter Kasting134ef9af2024-12-28 02:30:0962 printf("Platform has overflow: %s\n", !overflow_detected ? "yes." : "no.");
[email protected]9c4729b2013-01-26 04:41:1563#else
64 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT
65 // aren't).
66 EXPECT_TRUE(overflow_detected);
67#endif
68 }
69}
70
Xiaohan Wang38e4ebb2022-01-19 06:57:4371#if BUILDFLAG(IS_APPLE) || defined(ADDRESS_SANITIZER) || \
Sergey Ulanovef5b7632021-09-30 14:59:0872 defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
Arthur Sonzogni62e877a2024-04-30 16:09:4373 BUILDFLAG(IS_HWASAN) || PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
John Abd-El-Malek17727ff2014-10-02 22:55:1574#define MAYBE_NewOverflow DISABLED_NewOverflow
75#else
76#define MAYBE_NewOverflow NewOverflow
77#endif
Yuki Shiinocdbedb52020-08-25 09:23:0378// Test that array[TooBig][X] and array[X][TooBig] allocations fail and not
79// succeed with the wrong size allocation in case of size_t overflow. This
80// test is disabled on environments that operator new (nothrow) crashes in
81// case of size_t overflow.
82//
83// - iOS doesn't honor nothrow.
84// - XSan aborts when operator new returns nullptr.
85// - PartitionAlloc crashes by design when size_t overflows.
86//
Alison Gale81f4f2c72024-04-22 19:33:3187// TODO(crbug.com/40611888): Fix the test on Mac.
John Abd-El-Malek17727ff2014-10-02 22:55:1588TEST(SecurityTest, MAYBE_NewOverflow) {
[email protected]9c4729b2013-01-26 04:41:1589 const size_t kArraySize = 4096;
90 // We want something "dynamic" here, so that the compiler doesn't
91 // immediately reject crazy arrays.
Avi Drissmandea32052022-01-13 21:31:1892 [[maybe_unused]] const size_t kDynamicArraySize =
93 HideValueFromCompiler(kArraySize);
thakis4d7b56b2017-02-14 16:21:3594 const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
[email protected]9c4729b2013-01-26 04:41:1595 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
96 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
97 {
dcheng093de9b2016-04-04 21:25:5198 std::unique_ptr<char[][kArraySize]> array_pointer(
99 new (nothrow) char[kDynamicArraySize2][kArraySize]);
thakis4d7b56b2017-02-14 16:21:35100 // Prevent clang from optimizing away the whole test.
101 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
102 OverflowTestsSoftExpectTrue(!p);
[email protected]9c4729b2013-01-26 04:41:15103 }
Xiaohan Wang38e4ebb2022-01-19 06:57:43104#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS)
Avi Drissmandea32052022-01-13 21:31:18105 // On Windows, the compiler prevents static array sizes of more than
106 // 0x7fffffff (error C2148).
Peter Kastingbe940e92014-11-20 23:14:08107#else
[email protected]9c4729b2013-01-26 04:41:15108 {
dcheng093de9b2016-04-04 21:25:51109 std::unique_ptr<char[][kArraySize2]> array_pointer(
110 new (nothrow) char[kDynamicArraySize][kArraySize2]);
thakis4d7b56b2017-02-14 16:21:35111 // Prevent clang from optimizing away the whole test.
112 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
113 OverflowTestsSoftExpectTrue(!p);
[email protected]9c4729b2013-01-26 04:41:15114 }
Xiaohan Wang38e4ebb2022-01-19 06:57:43115#endif // BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS)
[email protected]9c4729b2013-01-26 04:41:15116}
117
[email protected]b5bf9a132013-01-15 20:16:33118} // namespace