From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 41349 invoked by alias); 23 Jun 2017 16:13:26 -0000 Mailing-List: contact jit-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: Sender: jit-owner@gcc.gnu.org Received: (qmail 41302 invoked by uid 89); 23 Jun 2017 16:13:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.99.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=mental, assigning, lieu, preferably X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-qk0-f170.google.com Received: from mail-qk0-f170.google.com (HELO mail-qk0-f170.google.com) (209.85.220.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 23 Jun 2017 16:13:22 +0000 Received: by mail-qk0-f170.google.com with SMTP id p21so39256293qke.3; Fri, 23 Jun 2017 09:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:references:cc:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=rozQHjy9AOXjwktPbQxYz7P2rAhIpFpFXqpPg8FaV9o=; b=qqIwPsnzc+nvcbte/4H409I0qcRpJPHts9X7B6epHP3glWaMgSj0/lKBfKHrwKVfEB l2PMmUsEujglm6FTGfl2BCVKw4/i5JCNkvJYEee2FswK5iCTzvyIqV/kXj0vzhrLcYQx pP404xvG3iUd4Xn5/k0NoHySSpwBTUfLNY+ZL7CBFXY+jR5QkYuyFWNSgULGXXChquCm 3Y0OpZT2SlHK0ISsbyA0dI9D9b1nK4vcxK1VK+MtjIFfshNSp+uwXFJKbPBuH9gHuNip WXEUbCNhE4TbOW6Dv+d24yfzaFtD7sxBWWfTNvl5YRhfB+WH9l7iUiX6KO2qBvkiDeSv tKkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:references:cc:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=rozQHjy9AOXjwktPbQxYz7P2rAhIpFpFXqpPg8FaV9o=; b=WddG7xB7ZVRCoV9n15I3R1UlaHoTx0asH9xxrHdhJZ8Vn10DD6Ch3hyCm6UWxTJoEK UdnoLTHSNx+LDb+5QdzrL1q3UjFtW3381lgFo0nv6NeIi5PZblnEKB6HT8iK98ih/TgC 89rwhrGFNhJm3Y8RKPJTwArdOtYVZi9gjqTXvriVPwBTxSVbwOcdKeOFgEn8Pi6bnj5l tA+sHMqpM/adtQJvFsENNAxoPGKYej/75hgSYptkoPcl95tpvyPXftJJuFNV6POVEOTx cZb55RpxK1HyddzHZugkTMdDEqB9rNOhCv61LzfFvnDvq8XWUYzS3qoYeFIGH28NMwZC UWog== X-Gm-Message-State: AKS2vOy859KmwKJD3U80dE88uXziLpElkG5roQ2RtPZh4Q6FFnNSQNs0 jJTMWUFhvC6KnaLd X-Received: by 10.55.87.198 with SMTP id l189mr10396779qkb.187.1498234400223; Fri, 23 Jun 2017 09:13:20 -0700 (PDT) Received: from localhost.localdomain (184-96-144-212.hlrn.qwest.net. [184.96.144.212]) by smtp.gmail.com with ESMTPSA id p36sm3806623qtf.21.2017.06.23.09.13.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jun 2017 09:13:19 -0700 (PDT) From: Martin Sebor Subject: Re: [committed] Fix -Werror=class-memaccess failures in jit testsuite (PR jit/81144) To: David Malcolm , gcc-patches@gcc.gnu.org, jit@gcc.gnu.org References: <1497993952-49602-1-git-send-email-dmalcolm@redhat.com> <95b93bd9-6940-6a10-d55f-92e61bbb872e@gmail.com> <1498006465.7551.194.camel@redhat.com> Cc: msebor@redhat.com Message-ID: <4caf3058-199d-c066-1d60-801f8d7fd1b3@gmail.com> Date: Sun, 01 Jan 2017 00:00:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <1498006465.7551.194.camel@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2017-q2/txt/msg00005.txt.bz2 On 06/20/2017 06:54 PM, David Malcolm wrote: > On Tue, 2017-06-20 at 17:15 -0600, Martin Sebor wrote: >> On 06/20/2017 03:25 PM, David Malcolm wrote: >>> This patch fixes a couple of failures of the form: >>> >>> error: 'void* memset(void*, int, size_t)' clearing an object of >>> non-trivial >>> type 'struct quadratic_test'; use assignment or value >>> -initialization >>> instead [-Werror=class-memaccess] >>> note: 'struct quadratic_test' declared here >>> cc1plus: all warnings being treated as errors >>> >>> seen within the jit testsuite, by using zero-initialization instead >>> of memset. >>> >>> (presumably introduced by r249234 aka >>> a324786b4ded9047d05463b4bce9d238b6c6b3ef) >>> >>> Successfully tested on x86_64-pc-linux-gnu; takes jit.sum from: >>> # of expected passes 9211 >>> # of unexpected failures 2 >>> to: >>> # of expected passes 9349 >>> >>> Martin: it's unclear to me what the benefit of the warning is for >>> these >>> cases. AIUI, it's complaining because the code is calling >>> the default ctor for struct quadratic_test, and then that object is >>> being clobbered by the memset. >>> But if I'm reading things right, the default ctor for this struct >>> zero-initializes all fields. Can't the compiler simply optimize >>> away >>> the redundant memset, and not issue a warning? > > Thanks for the info. > >> -Wclass-memaccess is issued because struct quadratic_test contains >> members of classes that define a default ctor to initialize their >> private members. >> The premise behind the warning is that objects >> of types with user-defined default and copy ctors should be >> initialized by making use of their ctors, and those with private >> data members manipulated via member functions rather than by >> directly modifying their raw representation. Using memset to >> bypass the default ctor doesn't begin the lifetime of an object, >> can violate invariants set up by it, and using it to overwrite >> private members breaks encapsulation. Examples of especially >> insidious errors include overwriting const data, references, or >> pointer to data members for which zero-initialization isn't >> the same as clearing their bytes. > > If I'm reading my code correctly, all of the default ctors of all of > the members of this struct are "merely" initializing the pointer they > wrap to NULL. Yes, that's my reading as well. > > So the ctors are initializing everything to NULL, and then the memset > redundant re-init's everything to 0 bits (I guess I was going for a > "belt and braces" approach to ensure that things are initialized). > >> The warning runs early on in the C++ front end and has no knowledge >> of either the effects of the type's ctors, dtor, and copy assignment >> operator, or whether the raw memory function is called in lieu of >> initializing an object (e.g., in storage obtained from malloc or >> operator new), or as a shortcut to zero out its members, or when >> zeroing them out happens to be safe and doesn't actually do any >> of those bad things I mentioned above. > > Aha: so at the place where the warning runs it's not possible to access > the ctors and tell that they're assigning NULL everywhere? Right, though I view it less as a limitation of the choice to implement the warning in the FE and more as a feature. > > Might it be possible to convert the warning to work in a two-phase way > where it first gathers up a vec of suspicious-looking modifications, > and then flushes them later, filtering against ctor information when it > has the latter? (so that we don't have to warn for this case at > -Wall?) With some effort I suppose it might be possible to do something sophisticated like that but based on the warnings we've seen so far I'm not convinced it's necessary or that it would time well spent. In my view, classes with user-defined ctors and other special functions (dtors, copy assignment), i.e., basically non trivial types, are preferably manipulated using these special functions, and memset and friends should only be used only for raw memory operations, not as a substitute for the former. In this case (as in most others I've seen, including the one in your patch), the code is clearer, more concise, and in general, also safer (and much easier for GCC to analyze for correctness than calls to memset et al.) > Alternatively maybe this is PEBCAK at my end; if so, maybe a case for adding this to the changes.html page? (and maybe adding some notes on workarounds there, and/or to invoke.texi?) Sure, that sounds good to me. Let me make a mental note to add something to the manual. > >> >> That said, I'm sorry (and a little surprised) that I missed these >> errors in my tests. I thought I had all the languages covered by >> using >> >> --enable-languages=all,ada,c,c++,fortran,go,lto,objc,obj-c++ >> >> but I guess jit still isn't implied by all, even after Nathan's >> recent change to it. Let me add jit to my script (IIRC, I once >> had it there but it was causing some trouble and I took it out.) > > Reading r248454 (aka 01b4453cde8f1871495955298043d9fb589e4a36), it > looks like "jit" is only included in "all" if you also pass > --enable-host-shared > Presumably that's what happened. Bother. Ah. I forgot about that bit. Thanks. Martin