Message ID | 1492050475-9238-3-git-send-email-palves@redhat.com |
---|---|
State | New, archived |
Headers |
Received: (qmail 106275 invoked by alias); 13 Apr 2017 02:28:02 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: <gdb-patches.sourceware.org> List-Unsubscribe: <mailto:gdb-patches-unsubscribe-##L=##H@sourceware.org> List-Subscribe: <mailto:gdb-patches-subscribe@sourceware.org> List-Archive: <http://sourceware.org/ml/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-help@sourceware.org>, <http://sourceware.org/ml/#faqs> Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 104744 invoked by uid 89); 13 Apr 2017 02:27:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=sk:delete X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 13 Apr 2017 02:27:58 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 57D868046A for <gdb-patches@sourceware.org>; Thu, 13 Apr 2017 02:27:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 57D868046A Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 57D868046A Received: from cascais.lan (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id D913417175 for <gdb-patches@sourceware.org>; Thu, 13 Apr 2017 02:27:57 +0000 (UTC) From: Pedro Alves <palves@redhat.com> To: gdb-patches@sourceware.org Subject: [PATCH 2/5] Don't memcpy non-trivially-copyable types: Make enum_flags triv. copyable Date: Thu, 13 Apr 2017 03:27:52 +0100 Message-Id: <1492050475-9238-3-git-send-email-palves@redhat.com> In-Reply-To: <1492050475-9238-1-git-send-email-palves@redhat.com> References: <1492050475-9238-1-git-send-email-palves@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit |
Commit Message
Pedro Alves
April 13, 2017, 2:27 a.m. UTC
The delete-memcpy-with-non-trivial-types patch exposed many instances of this problem: src/gdb/btrace.h: In function ‘btrace_insn_s* VEC_btrace_insn_s_quick_insert(VEC_btrace_insn_s*, unsigned int, const btrace_insn_s*, const char*, unsigned int)’: src/gdb/common/vec.h:948:62: error: use of deleted function ‘void* memmove(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; <template-parameter-1-3> = void; size_t = long unsigned int]’ memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \ ^ src/gdb/common/vec.h:436:1: note: in expansion of macro ‘DEF_VEC_FUNC_O’ DEF_VEC_FUNC_O(T) \ ^ src/gdb/btrace.h:84:1: note: in expansion of macro ‘DEF_VEC_O’ DEF_VEC_O (btrace_insn_s); ^ [...] src/gdb/common/vec.h:1060:31: error: use of deleted function ‘void* memcpy(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; <template-parameter-1-3> = void; size_t = long unsigned int]’ sizeof (T) * vec2_->num); \ ^ src/gdb/common/vec.h:437:1: note: in expansion of macro ‘DEF_VEC_ALLOC_FUNC_O’ DEF_VEC_ALLOC_FUNC_O(T) \ ^ src/gdb/btrace.h:84:1: note: in expansion of macro ‘DEF_VEC_O’ DEF_VEC_O (btrace_insn_s); ^ So, VECs (given it's C roots) rely on memcpy/memcpy of VEC elements to be well defined, in order to grow/reallocate its internal elements array. This means that we can only put trivially copyable types in VECs. E.g., if a type requires using a custom copy/move ctor to relocate, then we can't put it in a VEC (so we use std::vector instead). But, as shown above, we're violating that requirement. btrace_insn is currently not trivially copyable, because it contains an enum_flags field, and that is itself not trivially copyable. This patch corrects that. Note that std::vector relies on std::is_trivially_copyable too to know whether it can reallocate its elements with memcpy/memmove instead of having to call copy/move ctors and dtors, so if we have types in std::vectors that weren't trivially copyable because of enum_flags, this will make such vectors more efficient. gdb/ChangeLog: yyyy-mm-dd Pedro Alves <palves@redhat.com> * common/enum-flags.h (enum_flags): Define copy/move ctors/op= as defaulted. --- gdb/common/enum-flags.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
Comments
On 2017-04-12 22:27, Pedro Alves wrote: > The delete-memcpy-with-non-trivial-types patch exposed many instances > of this problem: > > src/gdb/btrace.h: In function ‘btrace_insn_s* > VEC_btrace_insn_s_quick_insert(VEC_btrace_insn_s*, unsigned int, const > btrace_insn_s*, const char*, unsigned int)’: > src/gdb/common/vec.h:948:62: error: use of deleted function ‘void* > memmove(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; > <template-parameter-1-3> = void; size_t = long unsigned int]’ > memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \ > ^ > src/gdb/common/vec.h:436:1: note: in expansion of macro > ‘DEF_VEC_FUNC_O’ > DEF_VEC_FUNC_O(T) \ > ^ > src/gdb/btrace.h:84:1: note: in expansion of macro ‘DEF_VEC_O’ > DEF_VEC_O (btrace_insn_s); > ^ > [...] > src/gdb/common/vec.h:1060:31: error: use of deleted function ‘void* > memcpy(T*, const U*, size_t) [with T = btrace_insn; U = btrace_insn; > <template-parameter-1-3> = void; size_t = long unsigned int]’ > sizeof (T) * vec2_->num); \ > ^ > src/gdb/common/vec.h:437:1: note: in expansion of macro > ‘DEF_VEC_ALLOC_FUNC_O’ > DEF_VEC_ALLOC_FUNC_O(T) \ > ^ > src/gdb/btrace.h:84:1: note: in expansion of macro ‘DEF_VEC_O’ > DEF_VEC_O (btrace_insn_s); > ^ > > So, VECs (given it's C roots) rely on memcpy/memcpy of VEC elements to > be well defined, in order to grow/reallocate its internal elements > array. This means that we can only put trivially copyable types in > VECs. E.g., if a type requires using a custom copy/move ctor to > relocate, then we can't put it in a VEC (so we use std::vector > instead). But, as shown above, we're violating that requirement. > > btrace_insn is currently not trivially copyable, because it contains > an enum_flags field, and that is itself not trivially copyable. This > patch corrects that. > > Note that std::vector relies on std::is_trivially_copyable too to know > whether it can reallocate its elements with memcpy/memmove instead of > having to call copy/move ctors and dtors, so if we have types in > std::vectors that weren't trivially copyable because of enum_flags, > this will make such vectors more efficient. > > gdb/ChangeLog: > yyyy-mm-dd Pedro Alves <palves@redhat.com> > > * common/enum-flags.h (enum_flags): Define copy/move ctors/op= as > defaulted. > --- > gdb/common/enum-flags.h | 15 ++++++--------- > 1 file changed, 6 insertions(+), 9 deletions(-) > > diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h > index e63c8a4..bea0ad5 100644 > --- a/gdb/common/enum-flags.h > +++ b/gdb/common/enum-flags.h > @@ -120,15 +120,12 @@ public: > : m_enum_value ((enum_type) 0) > {} > > - enum_flags (const enum_flags &other) > - : m_enum_value (other.m_enum_value) > - {} > - > - enum_flags &operator= (const enum_flags &other) > - { > - m_enum_value = other.m_enum_value; > - return *this; > - } > + /* Define copy/move ctor/op= as defaulted so that enum_flags is > + trivially copyable. */ > + enum_flags (const enum_flags &other) = default; > + enum_flags (enum_flags &&) noexcept = default; > + enum_flags &operator= (const enum_flags &other) = default; > + enum_flags &operator= (enum_flags &&) = default; What's the difference between defining these as default, and not defining them at all (which I assume will still make the compiler emit the default versions)? Do you want to add a static_assert(is_pod...) for that class? For now we have the VECs that will warn us if it becomes non-POD, but eventually they'll be gone. Simon
diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h index e63c8a4..bea0ad5 100644 --- a/gdb/common/enum-flags.h +++ b/gdb/common/enum-flags.h @@ -120,15 +120,12 @@ public: : m_enum_value ((enum_type) 0) {} - enum_flags (const enum_flags &other) - : m_enum_value (other.m_enum_value) - {} - - enum_flags &operator= (const enum_flags &other) - { - m_enum_value = other.m_enum_value; - return *this; - } + /* Define copy/move ctor/op= as defaulted so that enum_flags is + trivially copyable. */ + enum_flags (const enum_flags &other) = default; + enum_flags (enum_flags &&) noexcept = default; + enum_flags &operator= (const enum_flags &other) = default; + enum_flags &operator= (enum_flags &&) = default; /* If you get an error saying these two overloads are ambiguous, then you tried to mix values of different enum types. */