| 1 | /* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stddef.h> |
| 26 | #include <algorithm> |
| 27 | #include <deque> |
| 28 | #include <list> |
| 29 | #include <memory> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "my_inttypes.h" |
| 33 | #include "sql/malloc_allocator.h" |
| 34 | #include "sql/mem_root_allocator.h" |
| 35 | #include "sql/psi_memory_key.h" |
| 36 | #include "sql/stateless_allocator.h" |
| 37 | #include "sql/thr_malloc.h" |
| 38 | |
| 39 | using std::deque; |
| 40 | using std::list; |
| 41 | using std::vector; |
| 42 | |
| 43 | /* |
| 44 | Tests of custom STL memory allocators. |
| 45 | */ |
| 46 | |
| 47 | namespace stlalloc_unittest { |
| 48 | |
| 49 | /* |
| 50 | Wrappers to overcome the issue that we need allocators with |
| 51 | default constructors for TYPED_TEST_SUITE, which neither |
| 52 | Malloc_allocator nor Mem_root_allocator have. |
| 53 | |
| 54 | These wrappers need to inherit so that they are allocators themselves. |
| 55 | Otherwise TypeParam in the tests below will be wrong. |
| 56 | */ |
| 57 | template <typename T, class A = Malloc_allocator<T>> |
| 58 | class Malloc_allocator_wrapper : public A { |
| 59 | using a_t = std::allocator_traits<A>; |
| 60 | |
| 61 | public: |
| 62 | template <class U> |
| 63 | struct rebind { |
| 64 | using other = |
| 65 | Malloc_allocator_wrapper<U, typename a_t::template rebind_alloc<U>>; |
| 66 | }; |
| 67 | |
| 68 | // Inherit CTORs from base class. |
| 69 | using A::A; |
| 70 | |
| 71 | Malloc_allocator_wrapper() : Malloc_allocator<T>(PSI_NOT_INSTRUMENTED) {} |
| 72 | }; |
| 73 | |
| 74 | // Use a static mem-root for everything, so that list::splice will work. |
| 75 | static MEM_ROOT static_mem_root{PSI_NOT_INSTRUMENTED, 1024}; |
| 76 | |
| 77 | template <typename T, class A = Mem_root_allocator<T>> |
| 78 | class Mem_root_allocator_wrapper : public A { |
| 79 | using a_t = std::allocator_traits<A>; |
| 80 | |
| 81 | public: |
| 82 | template <class U> |
| 83 | struct rebind { |
| 84 | using other = |
| 85 | Mem_root_allocator_wrapper<U, typename a_t::template rebind_alloc<U>>; |
| 86 | }; |
| 87 | |
| 88 | // Inherit CTORs from base class. |
| 89 | using A::A; |
| 90 | |
| 91 | Mem_root_allocator_wrapper() : Mem_root_allocator<T>(&static_mem_root) { |
| 92 | // memory allocation error is expected, don't abort unit test. |
| 93 | static_mem_root.set_error_handler(nullptr); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | Allocators before C++17 need to be copy-constructible, and libc++ enforces |
| 98 | this (libstdc++ is fine with them being move-only). As a hack, we implement |
| 99 | copying the MEM_ROOT here; this type is never used outside of unit tests. |
| 100 | |
| 101 | Without this copy CTOR, clang on macOS will complain about |
| 102 | "call to implicitly-deleted copy constructor of ..." |
| 103 | even with -std=c++17 |
| 104 | */ |
| 105 | Mem_root_allocator_wrapper(const Mem_root_allocator_wrapper &other) |
| 106 | : Mem_root_allocator<T>(other.m_mem_root_ptr) {} |
| 107 | |
| 108 | MEM_ROOT *m_mem_root_ptr = &static_mem_root; |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | Flag for turning on allocation failure simulation. |
| 113 | */ |
| 114 | static bool simulate_failed_allocation = false; |
| 115 | |
| 116 | /* |
| 117 | Custom std::unique_ptr "Deleter" to reset flag. Allows unique_ptr to be used |
| 118 | as scope guard which ensures that the flag is reset. |
| 119 | */ |
| 120 | static auto reset_sfa = [](bool *sfap) { *sfap = false; }; |
| 121 | |
| 122 | /* |
| 123 | Local utility function which calls my_malloc with the standard MYF flags. |
| 124 | */ |
| 125 | |
| 126 | static void *my_malloc_(PSI_memory_key k, size_t s) { |
| 127 | return simulate_failed_allocation |
| 128 | ? nullptr |
| 129 | : my_malloc(k, s, MYF(MY_WME | ME_FATALERROR)); |
| 130 | } |
| 131 | |
| 132 | /* Functor for un-instrumented my_malloc */ |
| 133 | struct Not_instr_alloc { |
| 134 | void *operator()(size_t s) const { |
| 135 | return my_malloc_(PSI_NOT_INSTRUMENTED, s); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | Template alias for a Stateless_allocator using un-instrumented |
| 141 | my_malloc allocation. Deallocation functor is the default template |
| 142 | argument which is functor invoking my_free. |
| 143 | */ |
| 144 | template <class T> |
| 145 | using Not_instr_allocator = Stateless_allocator<T, Not_instr_alloc>; |
| 146 | |
| 147 | /* |
| 148 | Templated functor for my_malloc allocation using a PSI_KEY specified |
| 149 | a compile time. This is generally not that useful outside unit |
| 150 | testing as the PSI_KEY value is not normally known at compile time. |
| 151 | */ |
| 152 | template <int PSI_KEY> |
| 153 | struct PSI_key_alloc { |
| 154 | void *operator()(size_t s) const { return my_malloc_(PSI_KEY, s); } |
| 155 | }; |
| 156 | |
| 157 | /* |
| 158 | Template alias for a Stateless allocator which allocates with |
| 159 | my_malloc and PSI key 42. |
| 160 | */ |
| 161 | template <class T> |
| 162 | using PSI_42_allocator = Stateless_allocator<T, PSI_key_alloc<42>>; |
| 163 | |
| 164 | /* |
| 165 | Functor which allocates using the global operator new and |
| 166 | initializes the allocated memory with the value provided in the |
| 167 | template argument. |
| 168 | */ |
| 169 | template <unsigned char INIT> |
| 170 | struct Init_alloc { |
| 171 | void *operator()(size_t s) const { |
| 172 | if (simulate_failed_allocation || |
| 173 | DBUG_EVALUATE_IF("simulate_out_of_memory", true, false)) { |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | char *buf = static_cast<char *>(operator new(s)); |
| 178 | memset(buf, INIT, s); |
| 179 | return buf; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | /* |
| 184 | Functor which deallocates using the global operator delete and and |
| 185 | writes the value provided in the template argument into the memory |
| 186 | being released. |
| 187 | */ |
| 188 | template <unsigned char TRASH> |
| 189 | struct Trash_dealloc { |
| 190 | void operator()(void *p, size_t s) const { |
| 191 | memset(p, TRASH, s); |
| 192 | operator delete(p); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | /* |
| 197 | Template alias for a Stateless_allocator using initialized |
| 198 | allocation with new and trash-filled deallocation with delete |
| 199 | */ |
| 200 | template <class T> |
| 201 | using Init_aa_allocator = |
| 202 | Stateless_allocator<T, Init_alloc<0xaa>, Trash_dealloc<0xbb>>; |
| 203 | |
| 204 | // |
| 205 | // Test of container with simple objects |
| 206 | // |
| 207 | |
| 208 | template <typename T> |
| 209 | class STLAllocTestInt : public ::testing::Test { |
| 210 | protected: |
| 211 | T allocator; |
| 212 | }; |
| 213 | |
| 214 | typedef ::testing::Types< |
| 215 | Malloc_allocator_wrapper<int>, Mem_root_allocator_wrapper<int>, |
| 216 | Not_instr_allocator<int>, PSI_42_allocator<int>, Init_aa_allocator<int>> |
| 217 | AllocatorTypesInt; |
| 218 | |
| 219 | TYPED_TEST_SUITE(STLAllocTestInt, AllocatorTypesInt); |
| 220 | |
| 221 | TYPED_TEST(STLAllocTestInt, SimpleVector) { |
| 222 | vector<int, TypeParam> v1(this->allocator); |
| 223 | vector<int, TypeParam> v2(this->allocator); |
| 224 | for (int i = 0; i < 100; i++) { |
| 225 | v1.push_back(i); |
| 226 | v2.push_back(100 - i); |
| 227 | } |
| 228 | |
| 229 | EXPECT_EQ(100U, v1.size()); |
| 230 | EXPECT_EQ(100U, v2.size()); |
| 231 | |
| 232 | v1.swap(v2); |
| 233 | |
| 234 | EXPECT_EQ(100U, v1.size()); |
| 235 | EXPECT_EQ(100U, v2.size()); |
| 236 | |
| 237 | for (int i = 0; i < 100; i++) { |
| 238 | EXPECT_EQ(i, v2[i]); |
| 239 | EXPECT_EQ((100 - i), v1[i]); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | TYPED_TEST(STLAllocTestInt, SimpleList) { |
| 244 | list<int, TypeParam> l1(this->allocator); |
| 245 | list<int, TypeParam> l2(this->allocator); |
| 246 | |
| 247 | for (int i = 0; i < 100; i++) l1.push_back(i); |
| 248 | |
| 249 | EXPECT_EQ(100U, l1.size()); |
| 250 | EXPECT_EQ(0U, l2.size()); |
| 251 | |
| 252 | l2.splice(l2.begin(), l1); |
| 253 | |
| 254 | EXPECT_EQ(0U, l1.size()); |
| 255 | EXPECT_EQ(100U, l2.size()); |
| 256 | |
| 257 | std::reverse(l2.begin(), l2.end()); |
| 258 | |
| 259 | for (int i = 0; i < 100; i++) { |
| 260 | EXPECT_EQ((99 - i), l2.front()); |
| 261 | l2.pop_front(); |
| 262 | } |
| 263 | |
| 264 | EXPECT_EQ(0U, l2.size()); |
| 265 | } |
| 266 | |
| 267 | #ifndef NDEBUG |
| 268 | TYPED_TEST(STLAllocTestInt, OutOfMemory) { |
| 269 | vector<int, TypeParam> v1(this->allocator); |
| 270 | v1.reserve(10); |
| 271 | EXPECT_EQ(10U, v1.capacity()); |
| 272 | |
| 273 | DBUG_SET("+d,simulate_out_of_memory"); |
| 274 | ASSERT_THROW(v1.reserve(1000), std::bad_alloc); |
| 275 | DBUG_SET("-d,simulate_out_of_memory"); |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | // |
| 280 | // Test of container with non-trival objects |
| 281 | // |
| 282 | |
| 283 | class Container_object; |
| 284 | |
| 285 | template <typename T> |
| 286 | class STLAllocTestObject : public STLAllocTestInt<T> {}; |
| 287 | |
| 288 | typedef ::testing::Types<Malloc_allocator_wrapper<Container_object>, |
| 289 | Mem_root_allocator_wrapper<Container_object>, |
| 290 | Not_instr_allocator<Container_object>, |
| 291 | PSI_42_allocator<Container_object>, |
| 292 | Init_aa_allocator<Container_object>> |
| 293 | AllocatorTypesObject; |
| 294 | |
| 295 | TYPED_TEST_SUITE(STLAllocTestObject, AllocatorTypesObject); |
| 296 | |
| 297 | class Container_object { |
| 298 | char *buffer; |
| 299 | |
| 300 | public: |
| 301 | Container_object() { buffer = new char[20]; } |
| 302 | |
| 303 | Container_object(const Container_object &) { |
| 304 | buffer = new char[20]; // Don't care about contents |
| 305 | } |
| 306 | |
| 307 | ~Container_object() { delete[] buffer; } |
| 308 | }; |
| 309 | |
| 310 | TYPED_TEST(STLAllocTestObject, ContainerObject) { |
| 311 | vector<Container_object, TypeParam> v1(this->allocator); |
| 312 | v1.push_back(Container_object()); |
| 313 | v1.push_back(Container_object()); |
| 314 | v1.push_back(Container_object()); |
| 315 | } |
| 316 | |
| 317 | // |
| 318 | // Test of container with containers |
| 319 | // |
| 320 | |
| 321 | class Container_container; |
| 322 | |
| 323 | template <typename T> |
| 324 | class STLAllocTestNested : public STLAllocTestInt<T> {}; |
| 325 | |
| 326 | typedef ::testing::Types<Malloc_allocator_wrapper<Container_container>, |
| 327 | Mem_root_allocator_wrapper<Container_container>, |
| 328 | Not_instr_allocator<Container_container>, |
| 329 | PSI_42_allocator<Container_container>, |
| 330 | Init_aa_allocator<Container_container>> |
| 331 | AllocatorTypesNested; |
| 332 | |
| 333 | TYPED_TEST_SUITE(STLAllocTestNested, AllocatorTypesNested); |
| 334 | |
| 335 | class Container_container { |
| 336 | deque<Container_object> d; |
| 337 | |
| 338 | public: |
| 339 | Container_container() { |
| 340 | d.push_back(Container_object()); |
| 341 | d.push_back(Container_object()); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | TYPED_TEST(STLAllocTestNested, NestedContainers) { |
| 346 | Container_container cc1; |
| 347 | Container_container cc2; |
| 348 | list<Container_container, TypeParam> l1(this->allocator); |
| 349 | l1.push_back(cc1); |
| 350 | l1.push_back(cc2); |
| 351 | } |
| 352 | |
| 353 | // |
| 354 | // Test that it is possible to instantiate the std::basic_string |
| 355 | // template with various Stateless_allocator instances. |
| 356 | // |
| 357 | |
| 358 | /* |
| 359 | Template alias for basic_string with char. |
| 360 | */ |
| 361 | template <class A> |
| 362 | using default_string = std::basic_string<char, std::char_traits<char>, A>; |
| 363 | |
| 364 | template <class A> |
| 365 | class STLAllocTestBasicStringTemplate : public ::testing::Test {}; |
| 366 | |
| 367 | /* |
| 368 | Cannot use the stateful allocators with basic_string. The following |
| 369 | will not compile, due to |
| 370 | https://bugzilla.redhat.com/show_bug.cgi?id=1546704 |
| 371 | "Define _GLIBCXX_USE_CXX11_ABI gets ignored by gcc in devtoolset-7" |
| 372 | |
| 373 | typedef std::basic_string<char, std::char_traits<char>, |
| 374 | Malloc_allocator<char> > MA_string_type; |
| 375 | MA_string_type y("bar", Malloc_allocator<char>(42)); |
| 376 | */ |
| 377 | typedef ::testing::Types<Not_instr_allocator<char>, PSI_42_allocator<char>, |
| 378 | Init_aa_allocator<char>> |
| 379 | AllocatorTypesBasicStringTemplate; |
| 380 | |
| 381 | TYPED_TEST_SUITE(STLAllocTestBasicStringTemplate, |
| 382 | AllocatorTypesBasicStringTemplate); |
| 383 | |
| 384 | // |
| 385 | // Verify that a default_string can be created and extended with the |
| 386 | // Stateless_allocator instantiations. |
| 387 | // |
| 388 | TYPED_TEST(STLAllocTestBasicStringTemplate, BasicTest) { |
| 389 | typedef default_string<TypeParam> String_type; |
| 390 | |
| 391 | String_type x("foobar"); |
| 392 | x += "_tag"; |
| 393 | EXPECT_EQ(10U, x.size()); |
| 394 | } |
| 395 | |
| 396 | // |
| 397 | // Verify that std::bad_alloc is thrown in out-of-memory conditions |
| 398 | // |
| 399 | TYPED_TEST(STLAllocTestBasicStringTemplate, OutOfMemTest) { |
| 400 | // Scope guard which ensures flag is reset |
| 401 | std::unique_ptr<bool, decltype(reset_sfa)> sg(&simulate_failed_allocation, |
| 402 | reset_sfa); |
| 403 | |
| 404 | typedef default_string<TypeParam> String_type; |
| 405 | |
| 406 | String_type x("foobar"); |
| 407 | |
| 408 | // Set flag to force allocation failure |
| 409 | simulate_failed_allocation = true; |
| 410 | ASSERT_THROW(x.reserve(1000), std::bad_alloc); |
| 411 | } |
| 412 | |
| 413 | // |
| 414 | // Test of container of objects that cannot be copied. |
| 415 | // |
| 416 | |
| 417 | template <typename T> |
| 418 | class STLAllocTestMoveOnly : public STLAllocTestInt<T> {}; |
| 419 | |
| 420 | typedef ::testing::Types<Malloc_allocator_wrapper<std::unique_ptr<int>>, |
| 421 | Mem_root_allocator_wrapper<std::unique_ptr<int>>, |
| 422 | Not_instr_allocator<std::unique_ptr<int>>, |
| 423 | PSI_42_allocator<std::unique_ptr<int>>, |
| 424 | Init_aa_allocator<std::unique_ptr<int>>> |
| 425 | AllocatorTypesMoveOnly; |
| 426 | |
| 427 | TYPED_TEST_SUITE(STLAllocTestMoveOnly, AllocatorTypesMoveOnly); |
| 428 | |
| 429 | TYPED_TEST(STLAllocTestMoveOnly, MoveOnly) { |
| 430 | vector<std::unique_ptr<int>, TypeParam> v(this->allocator); |
| 431 | v.emplace_back(new int(1)); |
| 432 | v.emplace_back(new int(2)); |
| 433 | EXPECT_EQ(1, *v[0]); |
| 434 | EXPECT_EQ(2, *v[1]); |
| 435 | } |
| 436 | |
| 437 | } // namespace stlalloc_unittest |