Received: from eggs.gnu.org ([2001:4830:134:3::10]:56936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YOowT-0007v8-QC for qemu-devel@nongnu.org; Fri, 20 Feb 2015 09:52:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YOowQ-0000xm-CL for qemu-devel@nongnu.org; Fri, 20 Feb 2015 09:52:33 -0500 Received: from ssl.serverraum.org ([88.198.40.39]:50715) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YOowQ-0000xV-1k for qemu-devel@nongnu.org; Fri, 20 Feb 2015 09:52:30 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Date: Fri, 20 Feb 2015 15:52:27 +0100 From: Michael Walle In-Reply-To: <54E74662.5090203@redhat.com> References: <1424441905-5735-1-git-send-email-rkrcmar@redhat.com> <1424441905-5735-3-git-send-email-rkrcmar@redhat.com> <54E74662.5090203@redhat.com> Message-ID: <31761482c674426717c95680d6e1ba02@walle.cc> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: Paolo Bonzini , qemu-devel@nongnu.org, =?UTF-8?Q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Am 2015-02-20 15:36, schrieb Paolo Bonzini: > On 20/02/2015 15:18, Radim Kr=C4=8Dm=C3=A1=C5=99 wrote: >> man gcc: >> Warn if in a loop with constant number of iterations the compiler >> detects undefined behavior in some statement during one or more of >> the iterations. >>=20 >> Refactored the code a bit to avoid the GCC warning, in an=20 >> objectionable >> way, >> hw/misc/milkymist-pfpu.c: In function =E2=80=98pfpu_write=E2=80=99: >> hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be=20 >> reached after undefined behavior=20 >> [-Werror=3Daggressive-loop-optimizations] >> if (i++ >=3D MICROCODE_WORDS) { >> ^ >> hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement=20 >> is here >> uint32_t insn =3D s->microcode[pc]; >> ^ >>=20 >> Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 >> --- >> hw/misc/milkymist-pfpu.c | 18 ++++++++---------- >> 1 file changed, 8 insertions(+), 10 deletions(-) >>=20 >> diff --git a/hw/misc/milkymist-pfpu.c b/hw/misc/milkymist-pfpu.c >> index 609f33f9cd14..133f5b8c5153 100644 >> --- a/hw/misc/milkymist-pfpu.c >> +++ b/hw/misc/milkymist-pfpu.c >> @@ -164,6 +164,13 @@ output_queue_advance(MilkymistPFPUState *s) >> static int pfpu_decode_insn(MilkymistPFPUState *s) >> { >> uint32_t pc =3D s->regs[R_PC]; >> + >> + if (pc > MICROCODE_WORDS) { >> + error_report("milkymist_pfpu: too many instructions " >> + "executed in microcode. No VECTOUT?"); >> + return 0; >> + } >> + I don't like this syntax, eg declaration, statements, declaration. Can=20 you just declare the variable first and then assign them? Also the error=20 message is then misleading. I'd prefer something like "milkymist_pfpu:=20 program counter out of bounds. No VECTOUT?" >> uint32_t insn =3D s->microcode[pc]; >> uint32_t reg_a =3D (insn >> 18) & 0x7f; >> uint32_t reg_b =3D (insn >> 11) & 0x7f; >> @@ -348,7 +355,6 @@ static int pfpu_decode_insn(MilkymistPFPUState *s) >> static void pfpu_start(MilkymistPFPUState *s) >> { >> int x, y; >> - int i; >>=20 >> for (y =3D 0; y <=3D s->regs[R_VMESHLAST]; y++) { >> for (x =3D 0; x <=3D s->regs[R_HMESHLAST]; x++) { >> @@ -359,15 +365,7 @@ static void pfpu_start(MilkymistPFPUState *s) >> s->gp_regs[GPR_Y] =3D y; >>=20 >> /* run microcode on this position */ >> - i =3D 0; >> - while (pfpu_decode_insn(s)) { >> - /* decode at most MICROCODE_WORDS instructions */ >> - if (i++ >=3D MICROCODE_WORDS) { >=20 > Isn't the fix just to say "++i" instead of "i++"? In the first run, s->regs[R_PC] may have any value, therefore the "insn=20 =3D s->microcode[pc]" from above may access out of bounds. -michael