From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44447) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zw7Ym-0003wk-U5 for qemu-devel@nongnu.org; Tue, 10 Nov 2015 06:58:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zw7Yk-00007g-Gz for qemu-devel@nongnu.org; Tue, 10 Nov 2015 06:58:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58387) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zw7Yk-00007b-BZ for qemu-devel@nongnu.org; Tue, 10 Nov 2015 06:57:58 -0500 Date: Tue, 10 Nov 2015 13:57:55 +0200 From: "Michael S. Tsirkin" Message-ID: <20151110135749-mutt-send-email-mst@redhat.com> References: <1447080991-24995-1-git-send-email-peter.maydell@linaro.org> <20151109221638-mutt-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [PATCH for-2.5] hw/timer/hpet.c: Avoid signed integer overflow which results in bugs on OSX List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: Aaron Elkins , Paolo Bonzini , QEMU Developers , Patch Tracking On Tue, Nov 10, 2015 at 10:04:40AM +0000, Peter Maydell wrote: > On 9 November 2015 at 20:17, Michael S. Tsirkin wrote: > > On Mon, Nov 09, 2015 at 02:56:31PM +0000, Peter Maydell wrote: > >> Signed integer overflow in C is undefined behaviour, and the compiler > >> is at liberty to assume it can never happen and optimize accordingly. > >> In particular, the subtractions in hpet_time_after() and hpet_time_after64() > >> were causing OSX clang to optimize the code such that it was prone to > >> hangs and complaints about the main loop stalling (presumably because > >> we were spending all our time trying to service very high frequency > >> HPET timer callbacks). The clang sanitizer confirms the UB: > >> > >> hw/timer/hpet.c:119:26: runtime error: signed integer overflow: -2146967296 - 2147003978 cannot be represented in type 'int' > >> > >> Fix this by doing the subtraction as an unsigned operation and then > >> converting to signed for the comparison. > >> > >> Reported-by: Aaron Elkins > >> Signed-off-by: Peter Maydell > > > > Agree, this makes no sense the way it's written. > > > > Reviewed-by: Michael S. Tsirkin > > > > I'll pick this up in the next pull if Paolo doesn't > > beat me to it. > > I went ahead and committed it to master yesterday; sorry > if that was a bit hasty of me. That's fine too. > thanks > -- PMM