|
16 | 16 | #include <cstdint> |
17 | 17 | #include <cstdlib> |
18 | 18 | #include <cstring> |
| 19 | +#include <limits> |
19 | 20 | #include <memory> |
20 | 21 | #include <new> // IWYU pragma: keep for operator new |
21 | 22 | #include <string> |
@@ -72,6 +73,7 @@ using proto2_unittest::TestRepeatedString; |
72 | 73 | using ::testing::AnyOf; |
73 | 74 | using ::testing::ElementsAre; |
74 | 75 | using ::testing::ElementsAreArray; |
| 76 | +using ::testing::Gt; |
75 | 77 | using ::testing::HasSubstr; |
76 | 78 | using ::testing::Optional; |
77 | 79 | using ::testing::Pointee; |
@@ -1786,6 +1788,34 @@ TEST(ArenaTest, StartingBlockSize) { |
1786 | 1788 | EXPECT_EQ(custom_arena.SpaceAllocated(), options.start_block_size); |
1787 | 1789 | } |
1788 | 1790 |
|
| 1791 | +TEST(ArenaTest, VeryLargeAllocIn32BitMode) { |
| 1792 | + if (sizeof(size_t) != 4) { |
| 1793 | + GTEST_SKIP() << "Only care about 32-bit mode."; |
| 1794 | + } |
| 1795 | + |
| 1796 | + static absl::optional<size_t> large_allocation_size; |
| 1797 | + |
| 1798 | + // Make sure we don't have bugs allocating buffers greater that ptrdiff_t max. |
| 1799 | + // It will likely fail, but it should fail in the allocator. |
| 1800 | + |
| 1801 | + ArenaOptions options; |
| 1802 | + options.block_alloc = [](size_t n) { |
| 1803 | + if (n > size_t{std::numeric_limits<ptrdiff_t>::max()}) { |
| 1804 | + large_allocation_size = n; |
| 1805 | + // Just fake it. Doesn't matter. |
| 1806 | + n = 1000000; |
| 1807 | + } |
| 1808 | + return ::operator new(n); |
| 1809 | + }; |
| 1810 | + options.block_dealloc = [](void* ptr, size_t) { ::operator delete(ptr); }; |
| 1811 | + |
| 1812 | + Arena arena(options); |
| 1813 | + void* ptr = arena.AllocateAligned(size_t{1} << 31); |
| 1814 | + EXPECT_NE(ptr, nullptr); |
| 1815 | + EXPECT_THAT(large_allocation_size, |
| 1816 | + Optional(Gt(size_t{std::numeric_limits<ptrdiff_t>::max()}))); |
| 1817 | +} |
| 1818 | + |
1789 | 1819 | TEST(ArenaTest, BlockSizeDoubling) { |
1790 | 1820 | Arena arena; |
1791 | 1821 | EXPECT_EQ(0, arena.SpaceUsed()); |
|
0 commit comments