From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.92.2 #3 (Red Hat Linux)) id 1iBd6i-0005qw-Tz for barebox@lists.infradead.org; Sat, 21 Sep 2019 10:59:19 +0000 References: <20190920075813.22471-1-ahmad@a3f.at> <20190920075813.22471-3-ahmad@a3f.at> From: Ahmad Fatoum Message-ID: <2c96cd7b-94eb-a4c6-106c-e4f33365e476@pengutronix.de> Date: Sat, 21 Sep 2019 12:59:12 +0200 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: Re: [PATCH v1 3/4] common: ubsan: ignore shifting one into sign bit To: barebox@lists.infradead.org, rcz@pengutronix.de Hello Rouven, On 9/21/19 9:47 AM, Rouven Czerwinski wrote: > Hi Ahmad, > > On Fri, 2019-09-20 at 09:58 +0200, Ahmad Fatoum wrote: >> The __ubsan_handle_shift_out_of_bounds handler would be called for >> code >> shifting a one into the sign bit like (1 << 31), which is all too >> common >> in barebox. It's technically UB, but it's so prevalent that it's >> highly >> unlikely to be treated by a compiler as anything else than the >> standard-compliant (1U << 31). >> >> Check for this case here and ignore it selectively. > > Shouldn't we rather fix the the (1 << 31) to be (1U <<31)? Well, we could of course convert all bitwise operations to act on unsigned integers or to use the BIT() macro, but that's possibly thousands of instances. A very tedious work I'd rather not be doing... :D > >> Signed-off-by: Ahmad Fatoum >> --- >> lib/ubsan.c | 20 ++++++++++++++++++++ >> 1 file changed, 20 insertions(+) >> >> diff --git a/lib/ubsan.c b/lib/ubsan.c >> index 89ca6e580bce..41a5731dda66 100644 >> --- a/lib/ubsan.c >> +++ b/lib/ubsan.c >> @@ -382,6 +382,26 @@ void __ubsan_handle_shift_out_of_bounds(struct >> shift_out_of_bounds_data *data, >> if (suppress_report(&data->location)) >> return; >> >> + /* This handler would be called for code shifting a one into >> the >> + * sign bit like (1 << 31), which is all too common in barebox. >> + * It's technically UB, but it's so prevalent that it's highly >> + * unlikely to be treated by a compiler as anything else than >> the >> + * standard-compliant (1U << 31). Thus check for this case here >> + * and ignore it selectively >> + */ >> + if (type_is_signed(lhs_type)) { >> + s_max lhs_int, rhs_int; >> + >> + lhs_int = get_signed_val(lhs_type, lhs); >> + rhs_int = get_signed_val(rhs_type, rhs); >> + >> + if (fls(lhs_int) + rhs_int == type_bit_width(lhs_type)) >> { >> + pr_debug("signed left shift of %lld by %lld >> ignored.\n", >> + (s64)lhs_int, (s64)rhs_int); >> + return; >> + } >> + } >> + >> ubsan_prologue(&data->location, &flags); >> >> val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs); > > - rcz > > > _______________________________________________ > barebox mailing list > barebox@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/barebox > -- Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox