[{"id":3684282,"web_url":"http://patchwork.ozlabs.org/comment/3684282/","msgid":"<ed3d3306-5bd0-4d9c-bc27-8f3e091c8171@linaro.org>","list_archive_url":null,"date":"2026-04-29T21:33:00","subject":"Re: [PATCH 2/2] target/or1k: fix gen_div INT_MIN / -1 undefined\n behavior","submitter":{"id":72104,"url":"http://patchwork.ozlabs.org/api/people/72104/","name":"Richard Henderson","email":"richard.henderson@linaro.org"},"content":"On 4/30/26 03:03, joel--- via qemu development wrote:\n> From: Joel Holdsworth <jholdsworth@nvidia.com>\n> \n> Signed division of INT_MIN (0x80000000) by -1 produces a result that\n> overflows a 32-bit signed integer.  On many host architectures (x86,\n> ARM) this triggers a hardware exception.  The existing code only guards\n> against divide-by-zero but not this case.\n> \n> Add an explicit check for the INT_MIN / -1 pattern and suppress the\n> host-side exception by substituting a divisor of 1, setting the\n> overflow flag instead.\n> \n> Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>\n> ---\n>   target/or1k/translate.c | 22 ++++++++++++++++++----\n>   1 file changed, 18 insertions(+), 4 deletions(-)\n> \n> diff --git a/target/or1k/translate.c b/target/or1k/translate.c\n> index 1602e204fe..68d40111f0 100644\n> --- a/target/or1k/translate.c\n> +++ b/target/or1k/translate.c\n> @@ -279,13 +279,27 @@ static void gen_div(DisasContext *dc, TCGv_i32 dest,\n>                       TCGv_i32 srca, TCGv_i32 srcb)\n>   {\n>       TCGv_i32 t0 = tcg_temp_new_i32();\n> +    TCGv_i32 ov = tcg_temp_new_i32();\n>   \n> -    tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_sr_ov, srcb, 0);\n> -    /* The result of divide-by-zero is undefined.\n> -       Suppress the host-side exception by dividing by 1. */\n> -    tcg_gen_or_i32(t0, srcb, cpu_sr_ov);\n> +    /* Check for divide-by-zero. */\n> +    tcg_gen_setcondi_i32(TCG_COND_EQ, ov, srcb, 0);\n> +\n> +    /* Check for INT_MIN / -1 overflow (0x80000000 / -1). */\n> +    TCGv_i32 t_is_min = tcg_temp_new_i32();\n> +    TCGv_i32 t_is_neg1 = tcg_temp_new_i32();\n> +    TCGv_i32 t_intmin_ov = tcg_temp_new_i32();\n> +\n> +    tcg_gen_setcondi_i32(TCG_COND_EQ, t_is_min, srca, (int32_t)0x80000000);\n\nJust use INT32_MIN here.\n\n> +    tcg_gen_setcondi_i32(TCG_COND_EQ, t_is_neg1, srcb, -1);\n> +    tcg_gen_and_i32(t_intmin_ov, t_is_min, t_is_neg1);\n> +    tcg_gen_or_i32(ov, ov, t_intmin_ov);\n> +\n> +    /* Suppress the host-side exception by dividing by 1 on overflow. */\n> +    tcg_gen_movcond_i32(TCG_COND_NE, t0, ov, tcg_constant_i32(0),\n> +                        tcg_constant_i32(1), srcb);\n>       tcg_gen_div_i32(dest, srca, t0);\n>   \n> +    tcg_gen_mov_i32(cpu_sr_ov, ov);\n>       tcg_gen_neg_i32(cpu_sr_ov, cpu_sr_ov);\n>       gen_ove_ov(dc);\n>   }\n\nIt doesn't make much difference, but you can write this without adding new temps, \nespecially since you don't need to avoid cpu_sr_ov like you're doing.\n\n     /* Check for INT_MIN / -1 overflow */\n     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_sr_ov, srca, INT_MIN);\n     tcg_gen_setcondi_i32(TCG_COND_EQ, t0, srcb, -1);\n     tcg_gen_and_i32(cpu_sr_ov, cpu_sr_ov, t0);\n\n     /* Check for divide-by-zero. */\n     tcg_gen_setcondi_i32(TCG_COND_EQ, t0, srcb, 0);\n     tcg_gen_or_i32(cpu_sr_ov, cpu_sr_ov, t0);\n\n     /* Suppress the host-side exception by dividing by 1 on overflow. */\n     tcg_gen_movcond_i32(TCG_COND_NE, t0, cpu_sr_ov, tcg_constant_i32(0),\n                         tcg_constant_i32(1), srcb);\n     tcg_gen_div_i32(dest, srca, t0);\n\n     tcg_gen_neg_i32(cpu_sr_ov, cpu_sr_ov);\n\n\nr~","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=LRkvn0Ki;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5Vt24kVhz1yGq\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 07:33:45 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wICWu-0006Rj-Ql; Wed, 29 Apr 2026 17:33:12 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <richard.henderson@linaro.org>)\n id 1wICWr-0006RX-FV\n for qemu-devel@nongnu.org; Wed, 29 Apr 2026 17:33:10 -0400","from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <richard.henderson@linaro.org>)\n id 1wICWp-0000BU-1Q\n for qemu-devel@nongnu.org; Wed, 29 Apr 2026 17:33:08 -0400","by mail-pf1-x42b.google.com with SMTP id\n d2e1a72fcca58-82418b0178cso156446b3a.1\n for <qemu-devel@nongnu.org>; Wed, 29 Apr 2026 14:33:06 -0700 (PDT)","from [192.168.10.140] ([180.233.125.15])\n by smtp.gmail.com with ESMTPSA id\n d2e1a72fcca58-834ed80dfbfsm3039920b3a.53.2026.04.29.14.33.03\n for <qemu-devel@nongnu.org>\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Wed, 29 Apr 2026 14:33:04 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1777498385; x=1778103185; darn=nongnu.org;\n h=content-transfer-encoding:in-reply-to:content-language:from\n :references:to:subject:user-agent:mime-version:date:message-id:from\n :to:cc:subject:date:message-id:reply-to;\n bh=APrdlNOVOzTzNsfgJfjshYUX+ge9YC/XpNtcfI09iYo=;\n b=LRkvn0Kil+KxoGzepHXzHY52lpF1WwvS52HoO3+Rt49qODM60yWSQZGrmOVhGwX6KK\n MkdT2I3XCXKsWSmz7QPlfc4WPh8xP9LhH6ByxaxqW/MCPxUepVKwqtXVBN+eottor+6O\n jJqEPQ031gi4f+UI9IOxqh0XK/DdU+MLXofLQU/Tkd0ej3MEyYFJPDqUgBXHaAlQ1rYC\n mKHxhiI1zfkKosYdMaBgjIIEviHC4nGRL2xvfAjXjjneTQZCJ8TQbg+KSEL1dBeqGCbC\n kT+ACtIQrxE79q88bPQrL4xcPeOFJ7AJtSQ3VE4WxBlpP5S/qHnE7rLNPicQkeWCRW/w\n mhPA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777498385; x=1778103185;\n h=content-transfer-encoding:in-reply-to:content-language:from\n :references:to:subject:user-agent:mime-version:date:message-id\n :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id\n :reply-to;\n bh=APrdlNOVOzTzNsfgJfjshYUX+ge9YC/XpNtcfI09iYo=;\n b=YUa40MqLT/VtTP7tC3OjU76dw2uWxobmFwmGkcL5yms36urzR/ZK+dsYbCZPPazxZ8\n /d0LMActZgDZvS0CCgcBwYgI/SssaUTjFwC37FLr1idJTH/2iUabQbqVDwDLeo432gxW\n Uuf+WIN748l8Xw+d4ZP88fwbwRSq3WmPeuC5FuYE9faStu+gd6rZsqoXNjT9iVxVAaM8\n Sx50Y6wBP/VMAukdYZQ0d/rkewNiuGWz4QIToCdhjwVz0MGfznkZXgW38HrgaQDtBcL1\n b/MS0RfpujbhWAuMbUB1zFmgOdOT7qZOqSFqOkwDgP1CyUobupk2mCVuTYpw3KfYIYfk\n 4OzA==","X-Gm-Message-State":"AOJu0YwbKcekUMvQRuiAwSvjDBwdfFLVWUnHftc66Z9qLy1hTfVDIKYe\n Yc1lfuI21nrfiZMIEfAJ8UgKuD27F0ms1iqklB1u4mXIqEMt++iRGc9g2evXxqbNNzOSlWnrg0t\n NCnV6klQ=","X-Gm-Gg":"AeBDies8P/h5pzgxSwrI0KIfaiXiZ9rw0YJQdKfEuGN3uNl7BKq157iwsk174gpkJIS\n Jthm6ZqW1kzYJ12Zd91tprWMY13MYvv4O0qyrGc0ysaadsbpAnwNfmpcKCtbcB9vAqK9Mw/ROGg\n 3wvm807JqJOxMeQ/IL7jnEVrYSRcjIqJ3G2CA82FMaA5XmF3aGCUSZ5r/Yxq2v1VoLZ8nRtf68K\n nek3PBilHOeiUIj12Fmyn8YB4WG9CQks9ISwjThErNI6m7Ocn1QVqb93tY1Nu2b7BT0xKmurUzD\n nkgiOl1211W7w+k9tg5X67TnpgND71dKLDWfZml2qKI11BLIC/CzIolAJvsJbGBxYUulwgsO6Ie\n 4EAhrD1bBgSWlbeHT4KrZQYjkiMxubplZfCDblRc3h1CO5ZZAs6up1cIg59mPhVugWUHwrzNp1M\n 0iP72y2gsOyTDkJdtOwu+bg6LINh0GzD6tibhHdN6Xy1IRuuw3WWTC8N0RGQVxHbM=","X-Received":"by 2002:a05:6a00:369a:b0:82f:6bd2:eac5 with SMTP id\n d2e1a72fcca58-834fdb16d75mr359612b3a.2.1777498384968;\n Wed, 29 Apr 2026 14:33:04 -0700 (PDT)","Message-ID":"<ed3d3306-5bd0-4d9c-bc27-8f3e091c8171@linaro.org>","Date":"Thu, 30 Apr 2026 07:33:00 +1000","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH 2/2] target/or1k: fix gen_div INT_MIN / -1 undefined\n behavior","To":"qemu-devel@nongnu.org","References":"<20260429170519.2137725-1-joel@airwebreathe.org.uk>\n <20260429170519.2137725-3-joel@airwebreathe.org.uk>","From":"Richard Henderson <richard.henderson@linaro.org>","Content-Language":"en-US","In-Reply-To":"<20260429170519.2137725-3-joel@airwebreathe.org.uk>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","Received-SPF":"pass client-ip=2607:f8b0:4864:20::42b;\n envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42b.google.com","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}}]