public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
@ 2022-04-13  8:37 wielkiegie at gmail dot com
  2022-04-13 11:31 ` [Bug libstdc++/105258] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: wielkiegie at gmail dot com @ 2022-04-13  8:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

            Bug ID: 105258
           Summary: std::get_temporary_buffer() does not respect alignment
                    (affects std::stable_sort())
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wielkiegie at gmail dot com
  Target Milestone: ---

Created attachment 52795
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52795&action=edit
The reproducer

The `std::get_temporary_buffer()` function uses raw `operator new()` to
allocate enough memory for the `sizeof(T) * count` bytes. However, it does not
take `alignof(T)` into account.

While `std::get_temporary_buffer()` is deprecated in C++17 [1], it is still
used internally by `std::stable_sort()`.

The issue is reproducible on the gcc HEAD [2]. Please note that the alignment
issues might not be deterministic as the allocator can return an allocation of
correct alignment by accident.

For convenience, I have also attached the reproducer to this bug report.

[1] https://en.cppreference.com/w/cpp/memory/get_temporary_buffer
[2] https://wandbox.org/permlink/zZt6Weh3V1XWmUWt

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
@ 2022-04-13 11:31 ` redi at gcc dot gnu.org
  2022-04-13 11:33 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2022-04-13 11:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-04-13

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
  2022-04-13 11:31 ` [Bug libstdc++/105258] " redi at gcc dot gnu.org
@ 2022-04-13 11:33 ` redi at gcc dot gnu.org
  2022-04-13 12:11 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2022-04-13 11:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The simplest solution would be to just use std::allocator<T> which already does
the right thing, but that would break if there is a program-defined
specialization of std::allocator<T>.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
  2022-04-13 11:31 ` [Bug libstdc++/105258] " redi at gcc dot gnu.org
  2022-04-13 11:33 ` redi at gcc dot gnu.org
@ 2022-04-13 12:11 ` redi at gcc dot gnu.org
  2022-04-13 12:11 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2022-04-13 12:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Created attachment 52799
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52799&action=edit
Patch to support extended alignment

This needs a testsuite addition, and should probably wait for GCC 13 now.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (2 preceding siblings ...)
  2022-04-13 12:11 ` redi at gcc dot gnu.org
@ 2022-04-13 12:11 ` redi at gcc dot gnu.org
  2023-04-26  6:55 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2022-04-13 12:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (3 preceding siblings ...)
  2022-04-13 12:11 ` redi at gcc dot gnu.org
@ 2023-04-26  6:55 ` rguenth at gcc dot gnu.org
  2023-07-27  9:22 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-26  6:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.0                        |13.2

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.1 is being released, retargeting bugs to GCC 13.2.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (4 preceding siblings ...)
  2023-04-26  6:55 ` rguenth at gcc dot gnu.org
@ 2023-07-27  9:22 ` rguenth at gcc dot gnu.org
  2024-05-21  9:11 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-27  9:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.2                        |13.3

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.2 is being released, retargeting bugs to GCC 13.3.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (5 preceding siblings ...)
  2023-07-27  9:22 ` rguenth at gcc dot gnu.org
@ 2024-05-21  9:11 ` jakub at gcc dot gnu.org
  2024-05-31  7:39 ` Marco.Porsch at intenta dot de
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-05-21  9:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.3                        |13.4

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 13.3 is being released, retargeting bugs to GCC 13.4.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (6 preceding siblings ...)
  2024-05-21  9:11 ` jakub at gcc dot gnu.org
@ 2024-05-31  7:39 ` Marco.Porsch at intenta dot de
  2024-05-31  9:58 ` redi at gcc dot gnu.org
  2024-06-01 10:32 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: Marco.Porsch at intenta dot de @ 2024-05-31  7:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Marco Porsch <Marco.Porsch at intenta dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Marco.Porsch at intenta dot de

--- Comment #6 from Marco Porsch <Marco.Porsch at intenta dot de> ---
When compiled to ARM NEON vector operations, which have strict alignment
requirements, this issue causes a kernel alignment trap and SIGBUS.

See godbolt [1] for an example using Eigen matrix library. Compiled with clang,
the instruction `vst1.64 {d16, d17}, [r0:128]` is generated which crashes if
the pointer value is not aligned properly.

I created a corresponding ticket with Eigen [2] which points to this issue and
has extra info.

Looking at the stdlib implementation of libc++ [3] and MSVC [4] I see a similar
approach as proposed in the attached patch.

[1] https://godbolt.org/z/1TxcTzz53
[2] https://gitlab.com/libeigen/eigen/-/issues/2826
[3]
https://github.com/llvm/llvm-project/blob/main/libcxx/include/__memory/temporary_buffer.h#L26
[4] https://github.com/microsoft/STL/blob/main/stl/inc/xmemory#L1541

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (7 preceding siblings ...)
  2024-05-31  7:39 ` Marco.Porsch at intenta dot de
@ 2024-05-31  9:58 ` redi at gcc dot gnu.org
  2024-06-01 10:32 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-05-31  9:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The patch needs a little refactoring to share code with std::stacktrace, but it
will be fixed soon.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/105258] std::get_temporary_buffer() does not respect alignment (affects std::stable_sort())
  2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
                   ` (8 preceding siblings ...)
  2024-05-31  9:58 ` redi at gcc dot gnu.org
@ 2024-06-01 10:32 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-01 10:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105258

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2024-June/65
                   |                            |3296.html

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Patch posted: https://gcc.gnu.org/pipermail/gcc-patches/2024-June/653296.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-06-01 10:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-13  8:37 [Bug libstdc++/105258] New: std::get_temporary_buffer() does not respect alignment (affects std::stable_sort()) wielkiegie at gmail dot com
2022-04-13 11:31 ` [Bug libstdc++/105258] " redi at gcc dot gnu.org
2022-04-13 11:33 ` redi at gcc dot gnu.org
2022-04-13 12:11 ` redi at gcc dot gnu.org
2022-04-13 12:11 ` redi at gcc dot gnu.org
2023-04-26  6:55 ` rguenth at gcc dot gnu.org
2023-07-27  9:22 ` rguenth at gcc dot gnu.org
2024-05-21  9:11 ` jakub at gcc dot gnu.org
2024-05-31  7:39 ` Marco.Porsch at intenta dot de
2024-05-31  9:58 ` redi at gcc dot gnu.org
2024-06-01 10:32 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).