From c1638e07cadfc49f344fff5f8550ad956abb2345 Mon Sep 17 00:00:00 2001 From: redi Date: Thu, 15 Nov 2018 00:04:12 +0000 Subject: [PATCH] Fix test that does undefined shifts greater than width of size_t * testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Fix test for 32-bit targets. Test additional allocation sizes. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@266163 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 5 +++ .../unsynchronized_pool_resource/allocate.cc | 50 ++++++++++++---------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9b4bdc694d8..5262ef54782 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,8 @@ +2018-11-15 Jonathan Wakely + + * testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Fix + test for 32-bit targets. Test additional allocation sizes. + 2018-11-14 Jonathan Wakely * scripts/create_testsuite_files: Add special_functions to the list diff --git a/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc b/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc index 749655b63c7..0325a4358b6 100644 --- a/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc +++ b/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc @@ -170,7 +170,7 @@ test05() void test06() { - struct custom_mr : std::pmr::memory_resource + struct checking_mr : std::pmr::memory_resource { size_t expected_size = 0; size_t expected_alignment = 0; @@ -178,29 +178,30 @@ test06() struct bad_size { }; struct bad_alignment { }; - void* do_allocate(std::size_t b, std::size_t a) + void* do_allocate(std::size_t bytes, std::size_t align) { - if (expected_size != 0) - { - if (b < expected_size) - throw bad_size(); - else if (a != expected_alignment) - throw bad_alignment(); - // Else just throw, don't try to allocate: - throw std::bad_alloc(); - } - - return std::pmr::new_delete_resource()->allocate(b, a); + // Internal data structures in unsynchronized_pool_resource need to + // allocate memory, so handle those normally: + if (align <= alignof(std::max_align_t)) + return std::pmr::new_delete_resource()->allocate(bytes, align); + + // This is a large, unpooled allocation. Check the arguments: + if (bytes < expected_size) + throw bad_size(); + else if (align != expected_alignment) + throw bad_alignment(); + // Else just throw, don't really try to allocate: + throw std::bad_alloc(); } - void do_deallocate(void* p, std::size_t b, std::size_t a) - { std::pmr::new_delete_resource()->deallocate(p, b, a); } + void do_deallocate(void* p, std::size_t bytes, std::size_t align) + { std::pmr::new_delete_resource()->deallocate(p, bytes, align); } bool do_is_equal(const memory_resource& r) const noexcept { return false; } }; - custom_mr c; + checking_mr c; std::pmr::unsynchronized_pool_resource r({1, 1}, &c); std::pmr::pool_options opts = r.options(); const std::size_t largest_pool = opts.largest_required_pool_block; @@ -214,23 +215,26 @@ test06() // Try allocating various very large sizes and ensure the size requested // from the upstream allocator is at least as large as needed. - for (int i = 1; i < 64; ++i) + for (int i = 0; i < std::numeric_limits::digits; ++i) { - for (auto b : { -1, 0, 1, 3 }) + for (auto b : { -63, -5, -1, 0, 1, 3, std::numeric_limits::max() }) { std::size_t bytes = std::size_t(1) << i; - bytes += b; + bytes += b; // For negative b this can wrap to a large positive value. c.expected_size = bytes; c.expected_alignment = large_alignment; + bool caught_bad_alloc = false; try { (void) r.allocate(bytes, large_alignment); } catch (const std::bad_alloc&) { // expect to catch bad_alloc - } catch (custom_mr::bad_size) { - VERIFY(false); - } catch (custom_mr::bad_alignment) { - VERIFY(false); + caught_bad_alloc = true; + } catch (checking_mr::bad_size) { + VERIFY( ! "allocation from upstream resource had expected size" ); + } catch (checking_mr::bad_alignment) { + VERIFY( ! "allocation from upstream resource had expected alignment" ); } + VERIFY( caught_bad_alloc ); } } } -- 2.11.4.GIT