[go: nahoru, domu]

blob: bdb8180b1f0473088c7616f2b73ac43e3b47450d [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
jln@chromium.orgb5bf9a132013-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
jln@chromium.org1b556f82013-01-31 02:23:435#include <fcntl.h>
avi9b6f42932015-12-26 22:15:146#include <stddef.h>
jln@chromium.orgb5bf9a132013-01-15 20:16:337#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
jln@chromium.org1b556f82013-01-31 02:23:4310#include <sys/stat.h>
11#include <sys/types.h>
jln@chromium.orgb5bf9a132013-01-15 20:16:3312
13#include <algorithm>
14#include <limits>
dcheng093de9b2016-04-04 21:25:5115#include <memory>
jln@chromium.orgb5bf9a132013-01-15 20:16:3316
Arthur Sonzogni0bcc0232023-10-03 08:48:3217#include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h"
brettw@chromium.orge3177dd52014-08-13 20:22:1418#include "base/files/file_util.h"
dchengdb5935f2016-03-26 00:16:2719#include "base/memory/free_deleter.h"
Peter Collingbourne5a35305d2019-02-06 02:51:4320#include "base/sanitizer_buildflags.h"
jln@chromium.org547683f2013-02-04 23:39:4821#include "build/build_config.h"
jln@chromium.orgb5bf9a132013-01-15 20:16:3322#include "testing/gtest/include/gtest/gtest.h"
23
Xiaohan Wang38e4ebb2022-01-19 06:57:4324#if BUILDFLAG(IS_POSIX)
jln@chromium.org547683f2013-02-04 23:39:4825#include <sys/mman.h>
26#include <unistd.h>
27#endif
28
jln@chromium.orgb5bf9a132013-01-15 20:16:3329using std::nothrow;
jln@chromium.org9c4729b2013-01-26 04:41:1530using std::numeric_limits;
jln@chromium.orgb5bf9a132013-01-15 20:16:3331
32namespace {
33
jln@chromium.orgfe394f32013-02-06 03:23:4934// 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).
38template <typename Type>
Peter Kasting0acb7f5ef2022-05-05 23:57:2739NOINLINE Type HideValueFromCompiler(Type value) {
jln@chromium.org1cdfdb72013-04-04 12:02:3540#if defined(__GNUC__)
41 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
Peter Kasting0acb7f5ef2022-05-05 23:57:2742 // more robust.
jln@chromium.org1cdfdb72013-04-04 12:02:3543 __asm__ volatile ("" : "+r" (value));
44#endif // __GNUC__
jln@chromium.orgfe394f32013-02-06 03:23:4945 return value;
46}
47
jln@chromium.org9c4729b2013-01-26 04:41:1548// 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.
51void OverflowTestsSoftExpectTrue(bool overflow_detected) {
52 if (!overflow_detected) {
Xiaohan Wang38e4ebb2022-01-19 06:57:4353#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
54 BUILDFLAG(IS_APPLE)
jln@chromium.org9c4729b2013-01-26 04:41:1555 // 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 Wang38e4ebb2022-01-19 06:57:4367#if BUILDFLAG(IS_APPLE) || defined(ADDRESS_SANITIZER) || \
Sergey Ulanovef5b7632021-09-30 14:59:0868 defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
Yuki Shiinocdbedb52020-08-25 09:23:0369 BUILDFLAG(IS_HWASAN) || BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
John Abd-El-Malek17727ff2014-10-02 22:55:1570#define MAYBE_NewOverflow DISABLED_NewOverflow
71#else
72#define MAYBE_NewOverflow NewOverflow
73#endif
Yuki Shiinocdbedb52020-08-25 09:23:0374// 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 Ulanovef5b7632021-09-30 14:59:0883// TODO(https://crbug.com/927179): Fix the test on Mac.
John Abd-El-Malek17727ff2014-10-02 22:55:1584TEST(SecurityTest, MAYBE_NewOverflow) {
jln@chromium.org9c4729b2013-01-26 04:41:1585 const size_t kArraySize = 4096;
86 // We want something "dynamic" here, so that the compiler doesn't
87 // immediately reject crazy arrays.
Avi Drissmandea32052022-01-13 21:31:1888 [[maybe_unused]] const size_t kDynamicArraySize =
89 HideValueFromCompiler(kArraySize);
thakis4d7b56b2017-02-14 16:21:3590 const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
jln@chromium.org9c4729b2013-01-26 04:41:1591 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
92 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
93 {
dcheng093de9b2016-04-04 21:25:5194 std::unique_ptr<char[][kArraySize]> array_pointer(
95 new (nothrow) char[kDynamicArraySize2][kArraySize]);
thakis4d7b56b2017-02-14 16:21:3596 // Prevent clang from optimizing away the whole test.
97 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
98 OverflowTestsSoftExpectTrue(!p);
jln@chromium.org9c4729b2013-01-26 04:41:1599 }
Xiaohan Wang38e4ebb2022-01-19 06:57:43100#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS)
Avi Drissmandea32052022-01-13 21:31:18101 // On Windows, the compiler prevents static array sizes of more than
102 // 0x7fffffff (error C2148).
Peter Kastingbe940e92014-11-20 23:14:08103#else
jln@chromium.org9c4729b2013-01-26 04:41:15104 {
dcheng093de9b2016-04-04 21:25:51105 std::unique_ptr<char[][kArraySize2]> array_pointer(
106 new (nothrow) char[kDynamicArraySize][kArraySize2]);
thakis4d7b56b2017-02-14 16:21:35107 // Prevent clang from optimizing away the whole test.
108 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
109 OverflowTestsSoftExpectTrue(!p);
jln@chromium.org9c4729b2013-01-26 04:41:15110 }
Xiaohan Wang38e4ebb2022-01-19 06:57:43111#endif // BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS)
jln@chromium.org9c4729b2013-01-26 04:41:15112}
113
jln@chromium.orgb5bf9a132013-01-15 20:16:33114} // namespace