Received: from eggs.gnu.org ([2001:4830:134:3::10]:48667) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZykDu-0003Du-4J for qemu-devel@nongnu.org; Tue, 17 Nov 2015 12:39:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZykDq-0005EP-22 for qemu-devel@nongnu.org; Tue, 17 Nov 2015 12:39:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60966) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZykDp-0005EL-Qq for qemu-devel@nongnu.org; Tue, 17 Nov 2015 12:39:13 -0500 From: Markus Armbruster References: <1447769349-1767-1-git-send-email-pbonzini@redhat.com> Date: Tue, 17 Nov 2015 18:39:10 +0100 In-Reply-To: <1447769349-1767-1-git-send-email-pbonzini@redhat.com> (Paolo Bonzini's message of "Tue, 17 Nov 2015 15:09:09 +0100") Message-ID: <874mgkh6f5.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v2 for 2.5] QEMU does not care about left shifts of signed negative values List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: Peter Maydell , Laszlo Ersek , qemu-devel@nongnu.org Paolo Bonzini writes: > It seems like there's no good reason for the compiler to exploit the > undefinedness of left shifts. GCC explicitly documents that they do not > use at all this possibility and, while they also say this is subject > to change, they have been saying this for 10 years (since the wording > appeared in the GCC 4.0 manual). > > Any workaround for this particular case of undefined behavior uglifies the > code. Using unsigned is unsafe (https://github.com/madler/zlib/pull/112 > is the proof) because the value becomes positive when extended; -(a << b) > works but does not express that the intention is to compute -a * 2^N, > especially if "a" is a constant. > > > The straw that broke the camel back is Clang's latest obnoxious, > pointless, unsafe---and did I mention *totally* useless---warning about > left shifting a negative integer. It's obnoxious and pointless because > the compiler is not using the latitude that the standard gives it, so > the warning just adds noise. It is useless and unsafe because it does > not catch the widely more common case where the LHS is a variable, and > thus gives a false sense of security. The noisy nature of the warning > means that it should have never been added to -Wall. The uselessness > means that it probably should not have even been added to -Wextra. > > (It would have made sense to enable the warning for -fsanitize=shift, > as the program would always crash if the point is reached. But this was > probably too sophisticated to come up with, when you're so busy giving > birth to gems such as -Wabsolute-value). > > > Ubsan also has warnings for undefined behavior of left shifts. Checks for > left shift overflow and left shift of negative numbers, unfortunately, > cannot be silenced without also silencing the useful ones about out-of-range > shift amounts. -fwrapv ought to shut them up, but doesn't yet > (https://llvm.org/bugs/show_bug.cgi?id=25552; I am taking care of fixing > the same issues in GCC). Luckily ubsan is optional, and the easy > workaround is to use -fsanitize-recover. > > Anyhow, this patch documents our assumptions explicitly, and shuts up the > stupid warning. -fwrapv is a bit of a heavy hammer, but it is the safest > option and it ought to just work long term as the compilers improve. The kernel switched from -fwrapv to -fno-strict-overflow in '09, because -fwrapv was buggy in gcc 4.1 (already old then, completely irrelevant now), and because it "seems to be much less disturbing to gcc too: the difference in the generated code by -fno-strict-overflow are smaller (compared to using neither flag) than when using -fwrapv", which may or may not be still the case with compilers that matter today. Could you briefly explain why you picked -fwrapv and not -fno-strict-overflow? > Thanks to everyone involved in the discussion! > > Cc: Peter Maydell > Cc: Markus Armbruster > Cc: Laszlo Ersek > Signed-off-by: Paolo Bonzini