-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++] Use correct size for deallocation of arrays in shared_ptr #68233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+30
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23e6671
[libc++] Use correct size for deallocation of arrays in shared_ptr
ilya-biryukov 000d933
fixup! [libc++] Use correct size for deallocation of arrays in shared…
ilya-biryukov 7e79214
fixup! [libc++] Use correct size for deallocation of arrays in shared…
ilya-biryukov 049fa20
fixup! [libc++] Use correct size for deallocation of arrays in shared…
ilya-biryukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1137,7 +1137,8 @@ struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count | |
__alloc_.~_Alloc(); | ||
size_t __size = __unbounded_array_control_block::__bytes_for(__count_); | ||
_AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this); | ||
allocator_traits<_StorageAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*__storage), __size); | ||
allocator_traits<_StorageAlloc>::deallocate( | ||
__tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage)); | ||
} | ||
|
||
_LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; | ||
|
@@ -1220,7 +1221,7 @@ struct __bounded_array_control_block<_Tp[_Count], _Alloc> | |
|
||
_ControlBlockAlloc __tmp(__alloc_); | ||
__alloc_.~_Alloc(); | ||
allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), sizeof(*this)); | ||
allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oof, that's a nasty bug. Thanks for the fix. |
||
} | ||
|
||
_LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// REQUIRES: -fsized-deallocation | ||
// ADDITIONAL_COMPILE_FLAGS: -fsized-deallocation | ||
|
||
// This test will fail with ASan if the implementation passes different sizes | ||
// to corresponding allocation and deallocation functions. | ||
|
||
#include <memory> | ||
|
||
int main(int, char**) { | ||
std::allocate_shared<int[]>(std::allocator<int>{}, 10); | ||
std::make_shared<int[]>(10); | ||
|
||
std::allocate_shared<int[10]>(std::allocator<int>{}); | ||
std::make_shared<int[10]>(); | ||
|
||
return 0; | ||
} | ||
ilya-biryukov marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not always
__count_ + 1
? i.e. the number of elements in the array + the size of the control block at the beginning? I guess I'm kinda confused with my own code now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually seems to be more complicated.
The aligned storage has the size equal to the alignment of the control block (which is 8 bytes on my machine), so, e.g., we allocate and deallocate 5 elements of
AlignedStorage
forint[]
when__count_
is0
(as the control block is 40 bytes on my machine).In any case, I believe that reproducing exactly what gets passed to
allocate
is the right call here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
alignof(size_t) < sizeof(size_t)
, it will not be__count_ + 1
.