llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Allocator propagation broken for std::string move assignment

Open ojrosten opened this issue 5 years ago • 12 comments

Bugzilla Link 48439
Version 10.0
OS All
CC @mclow

Extended Description

Consider the case where

propagate on container copy assignment = true propagate on container move assignment = false

and focus on move assignment.

Suppose that the (stateful) instances of the allocators are not equal to one another.

In this case, __move_assign delegates to assign. As far as I can tell, this delegates to copy assignment. However, the latter considers propagation on container copy and not container move.

This gives the wrong outcome in the case described.

ojrosten avatar Dec 08 '20 11:12 ojrosten

Do you have a reproducer?

philnik777 avatar Aug 09 '24 17:08 philnik777

@ojrosten Any chance of getting a reproducer? That would be really helpful.

ldionne avatar Aug 29 '24 13:08 ldionne

Hi Louis,

Here's a minimal reproduction: https://godbolt.org/z/a9oTohd8K

ojrosten avatar Sep 07 '24 13:09 ojrosten

Here is a smaller (and IMO less confusing) reproducer: https://godbolt.org/z/eP1Tz7TKW

philnik777 avatar Sep 07 '24 13:09 philnik777

Thanks!

ojrosten avatar Sep 07 '24 14:09 ojrosten

@jwakely It looks like there's a similar problem with libstdc++, but it takes bit more effort to reveal it:

https://godbolt.org/z/h7avPfqhz

I think what's going on is as follows. Referring to basic_string.h [a523c2b]:

  1. Looking at move assignment, because the allocators aren't equal, we're not propagating on move assignment, and _M_is_local() returns false, we end up in the final else.

  2. This dispatches to assign(const basic_string& __str) which is a problem because POCCA is true. The predicates for both nested ifs are false, so we go into the else on line 1607. The bug is harder to expose compared to libc++ because of the copy of the allocator taken on line 1610. But using a shared_ptr reveals the issue.

ojrosten avatar Sep 07 '24 19:09 ojrosten

propagate on container copy assignment = true
propagate on container move assignment = false

Don't do that.

This gives the wrong outcome in the case described.

Your propagation traits are meaningless.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0177r2.html#2.0 describes why doing that is bad, but changes to the standard to say "don't do that" were deferred (indefinitely).

jwakely avatar Sep 07 '24 20:09 jwakely

Looks like the libstdc++ bug was introduced by https://gcc.gnu.org/g:db33efde1793 because before that, assign did not consider POCCA, so calling assign from the move assignment op was fine. The fix for libstdc++ is for the move assignment op to call _M_assign(__str) instead of assign(__str), as _M_assign does the actual deep copy and doesn't consider POCCA.

But it's still a meaningless combination of propagation traits.

jwakely avatar Sep 07 '24 20:09 jwakely

Yeah, I had a feeling it was a regression but wasn't 100% sure. I do agree that it's hard to imagine a use case for this combination of propagation traits. Even so, it's not obvious to me that Alisdair's paper is saying that it shouldn't work in this case, since basic_string does have a move assignment operator.

"The issue with copy-assignment is a little more subtle, but for a type that provides a copy-assignment operator but no move- assignment operator, then, when move-assignment is requested, the copy-assignment operator will be called. If the propagation traits of the user-supplied allocator differ, then problems will follow."

Then again, having all three traits be equivalent would be a huge simplification that would certainly have saved me a lot of pain in the past!

ojrosten avatar Sep 07 '24 20:09 ojrosten

The seemingly incorrect call to assign is currently here:

https://github.com/llvm/llvm-project/blob/caebb4562ce634a22f7b13480b19cffc2a6a6730/libcxx/include/string#L2678-L2685

IIUC there's a one-liner fix: changing assign(__str) to assign(__str.data(), __str.size()). It seems that the change will also make ~debug builds a bit faster~ debugging a bit simpler as the call stack will be made smaller.

frederick-vs-ja avatar Sep 08 '24 02:09 frederick-vs-ja

propagate on container copy assignment = true propagate on container move assignment = false

Don't do that.

This gives the wrong outcome in the case described.

Your propagation traits are meaningless.

It seems to me that the most performant propagation traits would be inconsistent.

Given vector<T, A> where A is stateful, if we want move and swap to transfer memory recources, POCMA and POCS should be true. Then consider copy assignment between two vector<T, A>s, we might want to avoid reallocation for elements when lhs.capacity() >= rhs.size(). This can always be done only if POCCA is false.

frederick-vs-ja avatar Sep 08 '24 08:09 frederick-vs-ja

But POCCA true and POCMA false is not useful

jwakely avatar Sep 08 '24 11:09 jwakely