Skip to content

Commit 8ad049e

Browse files
committed
Revert "Utilize allocate_at_least (microsoft#3712)"
This reverts commit e37227e. This broke BitCoin in Microsoft's internal Real World Code (RWC) test suite. They publicy derive an allocator from `std::allocate`, implementing `allocate` and `deallocate` but not `allocate_at_least` (https://github.com/bitcoin/bitcoin/blob/f1b4975461364d5d40d2bfafc6b165dd5d7eec30/src/support/allocators/secure.h#L19-L56). When `vector` allocates memory with `std::allocator::allocate_at_least` and tries to free it with `secure_allocator::deallocate`, terrible things happen. We suspect this pattern is widespread, so we're reverting the change for now.
1 parent 15aea98 commit 8ad049e

File tree

9 files changed

+60
-211
lines changed

9 files changed

+60
-211
lines changed

stl/inc/deque

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,13 +1557,12 @@ private:
15571557

15581558
_Newsize *= 2;
15591559
}
1560+
_Count = _Newsize - _Mapsize();
15601561

15611562
size_type _Myboff = _Myoff() / _Block_size;
1562-
_Mapptr _Newmap = _Allocate_at_least_helper(_Almap, _Newsize);
1563+
_Mapptr _Newmap = _Almap.allocate(_Mapsize() + _Count);
15631564
_Mapptr _Myptr = _Newmap + _Myboff;
15641565

1565-
_Count = _Newsize - _Mapsize();
1566-
15671566
_Myptr = _STD uninitialized_copy(_Map() + _Myboff, _Map() + _Mapsize(), _Myptr); // copy initial to end
15681567
if (_Myboff <= _Count) { // increment greater than offset of initial block
15691568
_Myptr = _STD uninitialized_copy(_Map(), _Map() + _Myboff, _Myptr); // copy rest of old

stl/inc/sstream

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ protected:
261261
return _Traits::eof();
262262
}
263263

264-
const auto _Newptr = _Unfancy(_Allocate_at_least_helper(_Al, _Newsize));
264+
const auto _Newptr = _Unfancy(_Al.allocate(_Newsize));
265265
_Traits::copy(_Newptr, _Oldptr, _Oldsize);
266266

267267
const auto _New_pnext = _Newptr + _Oldsize;
@@ -430,7 +430,7 @@ protected:
430430
return pos_type{_Off};
431431
}
432432

433-
void _Init(const _Elem* _Ptr, const _Mysize_type _Count, int _State) {
433+
void _Init(const _Elem* _Ptr, _Mysize_type _Count, int _State) {
434434
// initialize buffer to [_Ptr, _Ptr + _Count), set state
435435
_State &= ~_From_rvalue;
436436

@@ -440,10 +440,9 @@ protected:
440440

441441
if (_Count != 0 && (_State & (_Noread | _Constant)) != (_Noread | _Constant)) {
442442
// finite buffer that can be read or written, set it up
443-
_Mysize_type _Newsize = _Count;
444-
const auto _Pnew = _Unfancy(_Allocate_at_least_helper(_Al, _Newsize));
443+
const auto _Pnew = _Unfancy(_Al.allocate(_Count));
445444
_Traits::copy(_Pnew, _Ptr, _Count);
446-
_Seekhigh = _Pnew + _Newsize;
445+
_Seekhigh = _Pnew + _Count;
447446

448447
if (!(_State & _Noread)) {
449448
_Mysb::setg(_Pnew, _Pnew, _Seekhigh); // setup read buffer

stl/inc/syncstream

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public:
120120
if (_Al != _Right_al) {
121121
_Tidy();
122122

123-
_Size_type _Right_buf_size = _Right._Get_buffer_size();
123+
const _Size_type _Right_buf_size = _Right._Get_buffer_size();
124124
const _Size_type _Right_data_size = _Right._Get_data_size();
125125

126-
_Elem* const _New_ptr = _Unfancy(_Allocate_at_least_helper(_Al, _Right_buf_size));
126+
_Elem* const _New_ptr = _Unfancy(_Al.allocate(_Right_buf_size));
127127
_Traits::copy(_New_ptr, _Right.pbase(), _Right_data_size);
128128

129129
streambuf_type::setp(_New_ptr, _New_ptr + _Right_data_size, _New_ptr + _Right_buf_size);
@@ -217,11 +217,11 @@ protected:
217217
return _Traits::eof();
218218
}
219219

220-
_Size_type _New_capacity = _Calculate_growth(_Buf_size, _Buf_size + 1, _Max_allocation);
220+
const _Size_type _New_capacity = _Calculate_growth(_Buf_size, _Buf_size + 1, _Max_allocation);
221221
_Elem* const _Old_ptr = streambuf_type::pbase();
222222
const _Size_type _Old_data_size = _Get_data_size();
223223

224-
_Elem* const _New_ptr = _Unfancy(_Allocate_at_least_helper(_Al, _New_capacity));
224+
_Elem* const _New_ptr = _Unfancy(_Al.allocate(_New_capacity));
225225
_Traits::copy(_New_ptr, _Old_ptr, _Old_data_size);
226226
if (0 < _Buf_size) {
227227
_Al.deallocate(_Refancy<_Pointer>(_Old_ptr), _Buf_size);
@@ -237,9 +237,8 @@ private:
237237
static constexpr _Size_type _Min_size = 32; // constant for minimum buffer size
238238

239239
void _Init() {
240-
_Size_type _New_capacity = _Min_size;
241-
_Elem* const _New_ptr = _Unfancy(_Allocate_at_least_helper(_Getal(), _New_capacity));
242-
streambuf_type::setp(_New_ptr, _New_ptr + _New_capacity);
240+
_Elem* const _New_ptr = _Unfancy(_Getal().allocate(_Min_size));
241+
streambuf_type::setp(_New_ptr, _New_ptr + _Min_size);
243242
}
244243

245244
void _Tidy() noexcept {

stl/inc/vector

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -825,10 +825,10 @@ private:
825825
_Xlength();
826826
}
827827

828-
const size_type _Newsize = _Oldsize + 1;
829-
size_type _Newcapacity = _Calculate_growth(_Newsize);
828+
const size_type _Newsize = _Oldsize + 1;
829+
const size_type _Newcapacity = _Calculate_growth(_Newsize);
830830

831-
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
831+
const pointer _Newvec = _Al.allocate(_Newcapacity);
832832
const pointer _Constructed_last = _Newvec + _Whereoff + 1;
833833
pointer _Constructed_first = _Constructed_last;
834834

@@ -912,10 +912,10 @@ private:
912912
_Xlength();
913913
}
914914

915-
const size_type _Newsize = _Oldsize + _Count;
916-
size_type _Newcapacity = _Calculate_growth(_Newsize);
915+
const size_type _Newsize = _Oldsize + _Count;
916+
const size_type _Newcapacity = _Calculate_growth(_Newsize);
917917

918-
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
918+
const pointer _Newvec = _Al.allocate(_Newcapacity);
919919
const pointer _Constructed_last = _Newvec + _Oldsize + _Count;
920920
pointer _Constructed_first = _Constructed_last;
921921

@@ -1033,10 +1033,10 @@ public:
10331033
_Xlength();
10341034
}
10351035

1036-
const size_type _Newsize = _Oldsize + _Count;
1037-
size_type _Newcapacity = _Calculate_growth(_Newsize);
1036+
const size_type _Newsize = _Oldsize + _Count;
1037+
const size_type _Newcapacity = _Calculate_growth(_Newsize);
10381038

1039-
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
1039+
const pointer _Newvec = _Al.allocate(_Newcapacity);
10401040
const pointer _Constructed_last = _Newvec + _Whereoff + _Count;
10411041
pointer _Constructed_first = _Constructed_last;
10421042

@@ -1128,10 +1128,10 @@ private:
11281128
_Xlength();
11291129
}
11301130

1131-
const size_type _Newsize = _Oldsize + _Count;
1132-
size_type _Newcapacity = _Calculate_growth(_Newsize);
1131+
const size_type _Newsize = _Oldsize + _Count;
1132+
const size_type _Newcapacity = _Calculate_growth(_Newsize);
11331133

1134-
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
1134+
const pointer _Newvec = _Al.allocate(_Newcapacity);
11351135
const auto _Whereoff = static_cast<size_type>(_Whereptr - _Oldfirst);
11361136
const pointer _Constructed_last = _Newvec + _Whereoff + _Count;
11371137
pointer _Constructed_first = _Constructed_last;
@@ -1518,10 +1518,10 @@ private:
15181518
pointer& _Myfirst = _My_data._Myfirst;
15191519
pointer& _Mylast = _My_data._Mylast;
15201520

1521-
const auto _Oldsize = static_cast<size_type>(_Mylast - _Myfirst);
1522-
size_type _Newcapacity = _Calculate_growth(_Newsize);
1521+
const auto _Oldsize = static_cast<size_type>(_Mylast - _Myfirst);
1522+
const size_type _Newcapacity = _Calculate_growth(_Newsize);
15231523

1524-
const pointer _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
1524+
const pointer _Newvec = _Al.allocate(_Newcapacity);
15251525
const pointer _Appended_first = _Newvec + _Oldsize;
15261526
pointer _Appended_last = _Appended_first;
15271527

@@ -1598,10 +1598,7 @@ public:
15981598
}
15991599

16001600
private:
1601-
enum class _Reallocation_policy { _At_least, _Exactly };
1602-
1603-
template <_Reallocation_policy _Policy>
1604-
_CONSTEXPR20 void _Reallocate(size_type& _Newcapacity) {
1601+
_CONSTEXPR20 void _Reallocate_exactly(const size_type _Newcapacity) {
16051602
// set capacity to _Newcapacity (without geometric growth), provide strong guarantee
16061603
auto& _Al = _Getal();
16071604
auto& _My_data = _Mypair._Myval2;
@@ -1610,13 +1607,7 @@ private:
16101607

16111608
const auto _Size = static_cast<size_type>(_Mylast - _Myfirst);
16121609

1613-
pointer _Newvec;
1614-
if constexpr (_Policy == _Reallocation_policy::_At_least) {
1615-
_Newvec = _Allocate_at_least_helper(_Al, _Newcapacity);
1616-
} else {
1617-
_STL_INTERNAL_STATIC_ASSERT(_Policy == _Reallocation_policy::_Exactly);
1618-
_Newvec = _Al.allocate(_Newcapacity);
1619-
}
1610+
const pointer _Newvec = _Al.allocate(_Newcapacity);
16201611

16211612
_TRY_BEGIN
16221613
if constexpr (is_nothrow_move_constructible_v<_Ty> || !is_copy_constructible_v<_Ty>) {
@@ -1684,14 +1675,14 @@ private:
16841675
}
16851676

16861677
public:
1687-
_CONSTEXPR20 void reserve(_CRT_GUARDOVERFLOW size_type _Newcapacity) {
1678+
_CONSTEXPR20 void reserve(_CRT_GUARDOVERFLOW const size_type _Newcapacity) {
16881679
// increase capacity to _Newcapacity (without geometric growth), provide strong guarantee
16891680
if (_Newcapacity > capacity()) { // something to do (reserve() never shrinks)
16901681
if (_Newcapacity > max_size()) {
16911682
_Xlength();
16921683
}
16931684

1694-
_Reallocate<_Reallocation_policy::_At_least>(_Newcapacity);
1685+
_Reallocate_exactly(_Newcapacity);
16951686
}
16961687
}
16971688

@@ -1703,8 +1694,7 @@ public:
17031694
if (_Oldfirst == _Oldlast) {
17041695
_Tidy();
17051696
} else {
1706-
size_type _Newcapacity = static_cast<size_type>(_Oldlast - _Oldfirst);
1707-
_Reallocate<_Reallocation_policy::_Exactly>(_Newcapacity);
1697+
_Reallocate_exactly(static_cast<size_type>(_Oldlast - _Oldfirst));
17081698
}
17091699
}
17101700
}
@@ -1986,7 +1976,7 @@ private:
19861976
return _Geometric; // geometric growth is sufficient
19871977
}
19881978

1989-
_CONSTEXPR20 void _Buy_raw(size_type _Newcapacity) {
1979+
_CONSTEXPR20 void _Buy_raw(const size_type _Newcapacity) {
19901980
// allocate array with _Newcapacity elements
19911981
auto& _My_data = _Mypair._Myval2;
19921982
pointer& _Myfirst = _My_data._Myfirst;
@@ -1996,10 +1986,10 @@ private:
19961986
_STL_INTERNAL_CHECK(!_Myfirst && !_Mylast && !_Myend); // check that *this is tidy
19971987
_STL_INTERNAL_CHECK(0 < _Newcapacity && _Newcapacity <= max_size());
19981988

1999-
const pointer _Newvec = _Allocate_at_least_helper(_Getal(), _Newcapacity);
2000-
_Myfirst = _Newvec;
2001-
_Mylast = _Newvec;
2002-
_Myend = _Newvec + _Newcapacity;
1989+
const auto _Newvec = _Getal().allocate(_Newcapacity);
1990+
_Myfirst = _Newvec;
1991+
_Mylast = _Newvec;
1992+
_Myend = _Newvec + _Newcapacity;
20031993
}
20041994

20051995
_CONSTEXPR20 void _Buy_nonzero(const size_type _Newcapacity) {

stl/inc/xmemory

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,18 +2174,6 @@ _NODISCARD constexpr bool _Allocators_equal(const _Alloc& _Lhs, const _Alloc& _R
21742174
}
21752175
}
21762176

2177-
template <class _Alloc>
2178-
_NODISCARD_RAW_PTR_ALLOC _CONSTEXPR20 typename allocator_traits<_Alloc>::pointer _Allocate_at_least_helper(
2179-
_Alloc& _Al, _CRT_GUARDOVERFLOW typename allocator_traits<_Alloc>::size_type& _Count) {
2180-
#if _HAS_CXX23
2181-
auto [_Ptr, _Allocated] = allocator_traits<_Alloc>::allocate_at_least(_Al, _Count);
2182-
_Count = _Allocated;
2183-
return _Ptr;
2184-
#else // _HAS_CXX23
2185-
return _Al.allocate(_Count);
2186-
#endif // _HAS_CXX23
2187-
}
2188-
21892177
_EXPORT_STD template <class _FwdIt, class _Ty>
21902178
_NODISCARD_REMOVE_ALG _CONSTEXPR20 _FwdIt remove(_FwdIt _First, const _FwdIt _Last, const _Ty& _Val) {
21912179
// remove each matching _Val

stl/inc/xstring

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,11 +2647,9 @@ private:
26472647
return;
26482648
}
26492649

2650-
_My_data._Myres = _BUF_SIZE - 1;
2651-
size_type _New_capacity = _Calculate_growth(_Count);
2652-
++_New_capacity;
2653-
const pointer _New_ptr = _Allocate_at_least_helper(_Al, _New_capacity); // throws
2654-
--_New_capacity;
2650+
_My_data._Myres = _BUF_SIZE - 1;
2651+
const size_type _New_capacity = _Calculate_growth(_Count);
2652+
const pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
26552653
_Construct_in_place(_My_data._Bx._Ptr, _New_ptr);
26562654

26572655
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_capacity + 1);
@@ -2693,10 +2691,8 @@ private:
26932691
}
26942692

26952693
if (_Count >= _BUF_SIZE) {
2696-
size_type _New_capacity = _Calculate_growth(_Count);
2697-
++_New_capacity;
2698-
const pointer _New_ptr = _Allocate_at_least_helper(_Al, _New_capacity); // throws
2699-
--_New_capacity;
2694+
const size_type _New_capacity = _Calculate_growth(_Count);
2695+
const pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
27002696
_Construct_in_place(_My_data._Bx._Ptr, _New_ptr);
27012697
_My_data._Myres = _New_capacity;
27022698

@@ -2712,11 +2708,9 @@ private:
27122708
_Xlen_string(); // result too long
27132709
}
27142710

2715-
const auto _Old_ptr = _My_data._Myptr();
2716-
size_type _New_capacity = _Calculate_growth(_My_data._Mysize);
2717-
++_New_capacity;
2718-
const pointer _New_ptr = _Allocate_at_least_helper(_Al, _New_capacity); // throws
2719-
--_New_capacity;
2711+
const auto _Old_ptr = _My_data._Myptr();
2712+
const size_type _New_capacity = _Calculate_growth(_My_data._Mysize);
2713+
const pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
27202714

27212715
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_capacity + 1);
27222716
_Traits::copy(_Unfancy(_New_ptr), _Old_ptr, _My_data._Mysize);
@@ -2798,11 +2792,9 @@ public:
27982792
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data); // throws
27992793

28002794
if (_New_capacity < _New_size) {
2801-
_New_capacity = _Calculate_growth(_New_size, _BUF_SIZE - 1, max_size());
2802-
++_New_capacity;
2803-
const pointer _Fancyptr = _Allocate_at_least_helper(_Getal(), _New_capacity); // throws
2804-
--_New_capacity;
2805-
_Ptr = _Unfancy(_Fancyptr);
2795+
_New_capacity = _Calculate_growth(_New_size, _BUF_SIZE - 1, max_size());
2796+
const pointer _Fancyptr = _Getal().allocate(_New_capacity + 1); // throws
2797+
_Ptr = _Unfancy(_Fancyptr);
28062798
_Construct_in_place(_My_data._Bx._Ptr, _Fancyptr);
28072799

28082800
_Start_element_lifetimes(_Ptr, _New_capacity + 1);
@@ -2871,12 +2863,10 @@ public:
28712863
_Xlen_string();
28722864
}
28732865

2874-
auto _New_capacity = _Calculate_growth(_New_size, _BUF_SIZE - 1, _Max);
2875-
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
2866+
const auto _New_capacity = _Calculate_growth(_New_size, _BUF_SIZE - 1, _Max);
2867+
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal());
28762868
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data); // throws
2877-
++_New_capacity;
2878-
const pointer _Fancyptr = _Allocate_at_least_helper(_Getal(), _New_capacity); // throws
2879-
--_New_capacity;
2869+
const pointer _Fancyptr = _Getal().allocate(_New_capacity + 1); // throws
28802870
// nothrow hereafter
28812871
_Start_element_lifetimes(_Unfancy(_Fancyptr), _New_capacity + 1);
28822872
_Construct_in_place(_My_data._Bx._Ptr, _Fancyptr);
@@ -2956,10 +2946,9 @@ public:
29562946
_Result._Res = _My_data._Myres + 1;
29572947
} else {
29582948
// use _BUF_SIZE + 1 to avoid SSO, if the buffer is assigned back
2959-
size_type _Allocated = _BUF_SIZE + 1;
2960-
_Result._Ptr = _Allocate_at_least_helper(_Al, _Allocated);
2949+
_Result._Ptr = _Al.allocate(_BUF_SIZE + 1);
29612950
_Traits::copy(_Unfancy(_Result._Ptr), _My_data._Bx._Buf, _BUF_SIZE);
2962-
_Result._Res = _Allocated;
2951+
_Result._Res = _BUF_SIZE + 1;
29632952
}
29642953
_My_data._Orphan_all();
29652954
_Tidy_init();
@@ -3178,13 +3167,11 @@ public:
31783167

31793168
if (_Right._Mypair._Myval2._Large_string_engaged()) {
31803169
const auto _New_size = _Right._Mypair._Myval2._Mysize;
3181-
auto _New_capacity = _Calculate_growth(_New_size, 0, _Right.max_size());
3170+
const auto _New_capacity = _Calculate_growth(_New_size, 0, _Right.max_size());
31823171
auto _Right_al_non_const = _Right_al;
3183-
++_New_capacity;
3184-
const auto _New_ptr = _Allocate_at_least_helper(_Right_al_non_const, _New_capacity); // throws
3185-
--_New_capacity;
3172+
const auto _New_ptr = _Right_al_non_const.allocate(_New_capacity + 1); // throws
31863173

3187-
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_capacity + 1);
3174+
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_size + 1);
31883175

31893176
_Traits::copy(_Unfancy(_New_ptr), _Unfancy(_Right._Mypair._Myval2._Bx._Ptr), _New_size + 1);
31903177
_Tidy_deallocate();
@@ -4749,11 +4736,9 @@ private:
47494736
}
47504737

47514738
const size_type _Old_capacity = _Mypair._Myval2._Myres;
4752-
size_type _New_capacity = _Calculate_growth(_New_size);
4739+
const size_type _New_capacity = _Calculate_growth(_New_size);
47534740
auto& _Al = _Getal();
4754-
++_New_capacity;
4755-
const pointer _New_ptr = _Allocate_at_least_helper(_Al, _New_capacity); // throws
4756-
--_New_capacity;
4741+
const pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
47574742

47584743
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_capacity + 1);
47594744
_Mypair._Myval2._Orphan_all();
@@ -4784,11 +4769,9 @@ private:
47844769

47854770
const size_type _New_size = _Old_size + _Size_increase;
47864771
const size_type _Old_capacity = _My_data._Myres;
4787-
size_type _New_capacity = _Calculate_growth(_New_size);
4772+
const size_type _New_capacity = _Calculate_growth(_New_size);
47884773
auto& _Al = _Getal();
4789-
++_New_capacity;
4790-
const pointer _New_ptr = _Allocate_at_least_helper(_Al, _New_capacity); // throws
4791-
--_New_capacity;
4774+
const pointer _New_ptr = _Al.allocate(_New_capacity + 1); // throws
47924775

47934776
_Start_element_lifetimes(_Unfancy(_New_ptr), _New_capacity + 1);
47944777
_My_data._Orphan_all();

tests/std/test.lst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ tests\GH_003022_substr_allocator
222222
tests\GH_003105_piecewise_densities
223223
tests\GH_003119_error_category_ctor
224224
tests\GH_003246_cmath_narrowing
225-
tests\GH_003570_allocate_at_least
226225
tests\GH_003617_vectorized_meow_element
227226
tests\GH_003676_format_large_hh_mm_ss_values
228227
tests\GH_003735_char_traits_signatures

tests/std/tests/GH_003570_allocate_at_least/env.lst

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)