| 1 | /* |
| 2 | Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <stddef.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <memory> |
| 29 | #include <string> |
| 30 | |
| 31 | #include "my_alloc.h" |
| 32 | #include "my_inttypes.h" |
| 33 | #include "my_sys.h" |
| 34 | #include "mysys_err.h" |
| 35 | |
| 36 | extern "C" void mock_error_handler_hook(uint err, const char *str, myf MyFlags); |
| 37 | |
| 38 | /** |
| 39 | An alternative error_handler for non-server unit tests since it does |
| 40 | not rely on THD. It sets the global error handler function. |
| 41 | */ |
| 42 | class Mock_global_error_handler { |
| 43 | public: |
| 44 | Mock_global_error_handler(uint expected_error) |
| 45 | : m_expected_error(expected_error), m_handle_called(0) { |
| 46 | current = this; |
| 47 | m_old_error_handler_hook = error_handler_hook; |
| 48 | error_handler_hook = mock_error_handler_hook; |
| 49 | } |
| 50 | |
| 51 | virtual ~Mock_global_error_handler() { |
| 52 | if (m_expected_error == 0) { |
| 53 | EXPECT_EQ(0, m_handle_called); |
| 54 | } else { |
| 55 | EXPECT_GT(m_handle_called, 0); |
| 56 | } |
| 57 | error_handler_hook = m_old_error_handler_hook; |
| 58 | current = nullptr; |
| 59 | } |
| 60 | |
| 61 | void error_handler(uint err) { |
| 62 | EXPECT_EQ(m_expected_error, err); |
| 63 | ++m_handle_called; |
| 64 | } |
| 65 | |
| 66 | int handle_called() const { return m_handle_called; } |
| 67 | |
| 68 | static Mock_global_error_handler *current; |
| 69 | |
| 70 | private: |
| 71 | uint m_expected_error; |
| 72 | int m_handle_called; |
| 73 | |
| 74 | ErrorHandlerFunctionPointer m_old_error_handler_hook; |
| 75 | }; |
| 76 | |
| 77 | Mock_global_error_handler *Mock_global_error_handler::current = nullptr; |
| 78 | |
| 79 | /* |
| 80 | Error handler function. |
| 81 | */ |
| 82 | extern "C" void mock_error_handler_hook(uint err, const char *, myf) { |
| 83 | if (Mock_global_error_handler::current) |
| 84 | Mock_global_error_handler::current->error_handler(err); |
| 85 | } |
| 86 | |
| 87 | namespace my_alloc_unittest { |
| 88 | |
| 89 | const size_t num_iterations = 1ULL; |
| 90 | |
| 91 | class MyAllocTest : public ::testing::TestWithParam<size_t> { |
| 92 | protected: |
| 93 | void SetUp() override { |
| 94 | ::new ((void *)&m_root) MEM_ROOT(PSI_NOT_INSTRUMENTED, 1024); |
| 95 | } |
| 96 | void TearDown() override { m_root.Clear(); } |
| 97 | size_t m_num_objects; |
| 98 | MEM_ROOT m_root; |
| 99 | }; |
| 100 | |
| 101 | class MyPreAllocTest : public ::testing::Test { |
| 102 | protected: |
| 103 | void SetUp() override { |
| 104 | ::new ((void *)&m_prealloc_root) MEM_ROOT(PSI_NOT_INSTRUMENTED, 1024); |
| 105 | } |
| 106 | void TearDown() override { m_prealloc_root.Clear(); } |
| 107 | size_t m_num_objects; |
| 108 | MEM_ROOT m_prealloc_root; |
| 109 | }; |
| 110 | |
| 111 | size_t test_values[] = {100, 1000, 10000, 100000}; |
| 112 | |
| 113 | INSTANTIATE_TEST_SUITE_P(MyAlloc, MyAllocTest, |
| 114 | ::testing::ValuesIn(test_values)); |
| 115 | |
| 116 | TEST_P(MyAllocTest, NoMemoryLimit) { |
| 117 | m_num_objects = GetParam(); |
| 118 | for (size_t ix = 0; ix < num_iterations; ++ix) { |
| 119 | for (size_t objcount = 0; objcount < m_num_objects; ++objcount) |
| 120 | EXPECT_NE(nullptr, m_root.Alloc(8)); |
| 121 | } |
| 122 | // Normally larger, but with Valgrind/ASan, we'll get exact-sized blocks, |
| 123 | // so also allow equal. |
| 124 | EXPECT_GE(m_root.allocated_size(), num_iterations * m_num_objects * 8); |
| 125 | } |
| 126 | |
| 127 | TEST_P(MyAllocTest, WithMemoryLimit) { |
| 128 | m_num_objects = GetParam(); |
| 129 | m_root.set_max_capacity(num_iterations * m_num_objects * 8); |
| 130 | for (size_t ix = 0; ix < num_iterations; ++ix) { |
| 131 | for (size_t objcount = 0; objcount < m_num_objects; ++objcount) |
| 132 | EXPECT_NE(nullptr, m_root.Alloc(8)); |
| 133 | } |
| 134 | EXPECT_EQ(m_root.allocated_size(), num_iterations * m_num_objects * 8); |
| 135 | } |
| 136 | |
| 137 | TEST_F(MyAllocTest, CheckErrorReporting) { |
| 138 | const void *null_pointer = nullptr; |
| 139 | EXPECT_TRUE(m_root.Alloc(1000)); |
| 140 | m_root.set_max_capacity(100); |
| 141 | EXPECT_EQ(null_pointer, m_root.Alloc(1000)); |
| 142 | m_root.set_error_for_capacity_exceeded(true); |
| 143 | Mock_global_error_handler error_handler(EE_CAPACITY_EXCEEDED); |
| 144 | EXPECT_TRUE(m_root.Alloc(1000)); |
| 145 | EXPECT_EQ(1, error_handler.handle_called()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(MyAllocTest, MoveConstructorDoesNotLeak) { |
| 149 | MEM_ROOT alloc1(PSI_NOT_INSTRUMENTED, 512); |
| 150 | (void)alloc1.Alloc(10); |
| 151 | MEM_ROOT alloc2(PSI_NOT_INSTRUMENTED, 512); |
| 152 | (void)alloc2.Alloc(30); |
| 153 | alloc1 = std::move(alloc2); |
| 154 | } |
| 155 | |
| 156 | TEST_F(MyAllocTest, ExceptionalBlocksAreNotReusedForLargerAllocations) { |
| 157 | MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512); |
| 158 | void *ptr = alloc.Alloc(600); |
| 159 | alloc.ClearForReuse(); |
| 160 | |
| 161 | if (alloc.allocated_size() == 0) { |
| 162 | /* |
| 163 | The MEM_ROOT was all cleared out (probably because we're running under |
| 164 | Valgrind/ASAN), so we are obviously not doing any reuse. Moreover, |
| 165 | the test below is unsafe in this case, since the system malloc() could |
| 166 | reuse the block. |
| 167 | */ |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // The allocated block is too small to satisfy this new, larger allocation. |
| 172 | void *ptr2 = alloc.Alloc(605); |
| 173 | EXPECT_NE(ptr, ptr2); |
| 174 | } |
| 175 | |
| 176 | TEST_F(MyAllocTest, RawInterface) { |
| 177 | MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512); |
| 178 | |
| 179 | // Nothing allocated yet. |
| 180 | std::pair<char *, char *> block = alloc.Peek(); |
| 181 | EXPECT_EQ(0, block.second - block.first); |
| 182 | |
| 183 | // Create a block. |
| 184 | alloc.ForceNewBlock(16); |
| 185 | block = alloc.Peek(); |
| 186 | EXPECT_EQ(512, block.second - block.first); |
| 187 | |
| 188 | // Write and commit some memory. |
| 189 | char *store_ptr = reinterpret_cast<char *>(block.first); |
| 190 | strcpy(store_ptr, "12345"); |
| 191 | alloc.RawCommit(6); |
| 192 | block = alloc.Peek(); |
| 193 | EXPECT_EQ(506, block.second - block.first); |
| 194 | |
| 195 | // Get a new block. |
| 196 | alloc.ForceNewBlock(512); |
| 197 | block = alloc.Peek(); |
| 198 | #if defined(HAVE_VALGRIND) || defined(HAVE_ASAN) |
| 199 | EXPECT_EQ(512, block.second - block.first); |
| 200 | #else |
| 201 | EXPECT_EQ(768, block.second - block.first); |
| 202 | #endif |
| 203 | |
| 204 | // The value should still be there. |
| 205 | EXPECT_STREQ("12345", store_ptr); |
| 206 | } |
| 207 | |
| 208 | TEST_F(MyAllocTest, ArrayAllocInitialization) { |
| 209 | MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512); |
| 210 | |
| 211 | // No default value means each element is value-initialized. For int, it means |
| 212 | // they are set to 0. |
| 213 | int *int_array1 = alloc.ArrayAlloc<int>(100); |
| 214 | ASSERT_NE(nullptr, int_array1); |
| 215 | for (int i = 0; i < 100; ++i) { |
| 216 | EXPECT_EQ(0, int_array1[i]); |
| 217 | } |
| 218 | |
| 219 | // Initialize to explicit value. |
| 220 | int *int_array2 = alloc.ArrayAlloc<int>(100, 123); |
| 221 | ASSERT_NE(nullptr, int_array2); |
| 222 | for (int i = 0; i < 100; ++i) { |
| 223 | EXPECT_EQ(123, int_array2[i]); |
| 224 | } |
| 225 | |
| 226 | // Initialize from rvalue. (Verifies that a bug, which made it only initialize |
| 227 | // the first element correctly, is fixed.) |
| 228 | std::string *string_array1 = alloc.ArrayAlloc<std::string>( |
| 229 | 10, std::string("abcdefghijklmnopqrstuvwxyz")); |
| 230 | ASSERT_NE(nullptr, string_array1); |
| 231 | for (int i = 0; i < 10; ++i) { |
| 232 | EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", string_array1[i]); |
| 233 | } |
| 234 | destroy_array(string_array1, 10); |
| 235 | |
| 236 | // Should be allowed to create an array of a class which is not |
| 237 | // copy-constructible. |
| 238 | auto uptr_array1 = alloc.ArrayAlloc<std::unique_ptr<int>>(10); |
| 239 | ASSERT_NE(nullptr, uptr_array1); |
| 240 | auto uptr_array2 = alloc.ArrayAlloc<std::unique_ptr<int>>(10, nullptr); |
| 241 | ASSERT_NE(nullptr, uptr_array2); |
| 242 | for (int i = 0; i < 10; ++i) { |
| 243 | EXPECT_EQ(nullptr, uptr_array1[i]); |
| 244 | EXPECT_EQ(nullptr, uptr_array2[i]); |
| 245 | } |
| 246 | |
| 247 | // This should give a compiler error, because it attempts to copy a value of a |
| 248 | // non-copyable type: |
| 249 | // |
| 250 | // alloc.ArrayAlloc<std::unique_ptr<int>>(3, std::make_unique<int>(123)); |
| 251 | } |
| 252 | |
| 253 | } // namespace my_alloc_unittest |