From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87D83C4321E for ; Mon, 5 Dec 2022 14:12:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 875D38E0002; Mon, 5 Dec 2022 09:12:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 826308E0001; Mon, 5 Dec 2022 09:12:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6EDBA8E0002; Mon, 5 Dec 2022 09:12:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5F5108E0001 for ; Mon, 5 Dec 2022 09:12:04 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 331E34036F for ; Mon, 5 Dec 2022 14:12:04 +0000 (UTC) X-FDA: 80208441768.10.0BCBF81 Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) by imf12.hostedemail.com (Postfix) with ESMTP id D506F4001D for ; Mon, 5 Dec 2022 14:12:03 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LutcpLDl; spf=pass (imf12.hostedemail.com: domain of elver@google.com designates 209.85.219.182 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670249523; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xkjm5Izlgm5W/aCda9yUcdLforY0vsBW8J+dAEU4QI0=; b=riJzwOrlg8jrGfCWiwFDGpP8cBisv9ZwF4QADRfXc8TBad0VO8CO+iUyosafyWVyjuVjwp m1xpRJ3AMlU6I5Zl1yUU1Vzv+MMT3Vl4aqtSdNoTlcv+qLnFzlCLBxTbG+yKSgJSNGpW98 mAGz0AdYaLL6HBYPz0nMqt3yUfWxwtI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LutcpLDl; spf=pass (imf12.hostedemail.com: domain of elver@google.com designates 209.85.219.182 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670249523; a=rsa-sha256; cv=none; b=J278ZTS7smjF5N1O7S8BLiGclC7c6WiRLzoiFPp3PvjuRZHNOKt8or3ZTguBAZ5R2zZLdS yEpLSDXs9BbRePxSlgp0JAJ2ifg1v3c/aLsr6HyjQwbpl2RHAtE2EQhbmj+cDm+XqSOOaJ DqAdfnb6UQn5aM4eYWUbTA1c70mbunQ= Received: by mail-yb1-f182.google.com with SMTP id v206so14569719ybv.7 for ; Mon, 05 Dec 2022 06:12:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=xkjm5Izlgm5W/aCda9yUcdLforY0vsBW8J+dAEU4QI0=; b=LutcpLDlD1QUswgOL8FLDDs2fNhaFdhLT+t5r1Ri2e4Xf36LuAn+hl2F/MLSHqV6iQ d4Ip0sujOw2tB9ENYLU1bJxv6P9jpniNrgssolh9jO+QMVbXuVKxpqWdc3c56rBTs4Uq xIqysrgRzmIghwnEt9LHkN0hdZn3iogj2LnVxyefjyPFwjn6xjStqQyXzD6lZolJCsr6 G+UIyj4pn6Rp9c+CzKLlm/RUXDWAdK4jhlGyD9CUFUYaJAvdU5r8F7dIB0sZzY1pnv9r p1uFaSgW7qXoaZWgzaCDrjMb6xsK3NIg1T8fTsBbxR9agtwhqfqfUErRSE3WNP69gS59 S2wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xkjm5Izlgm5W/aCda9yUcdLforY0vsBW8J+dAEU4QI0=; b=chCOpyE3yNAcJ2pZt3BKjXSw/T27AK3iVP8wDmrd5dcZeBDHokIj8k88Gf2TMmYV9i 3RbaBFneq80qow7Li2ffKYjaed0rhI0sIH6hX37E8D+9vtbRnQj2m3Xc61rZBTVLQv2H jtHbNgV7glApz18rtW6AR8Er4sHATcjG5GE6vdq5pHQ2q19cm1mfHQ0wMN8A40M+Awx9 NynCJZzucBgr45sQnWmjWu1ItW6Jq85ykuqQULYXElw+LIwZQXKMO2za5PXa1vjFTkLc hUKl8oAcjvkzn184i5c07HBrqUvgJf8OVq9JkfS4cDLtDQSVDsqNEZi6zcIn8FFv+OAc Wcqg== X-Gm-Message-State: ANoB5pkKFf/tVrVD1tz0qW1LmwxFOG52VvBHzQdeiGI14EqHKL5aIAiA /WdkMnGmKMXBMXJnoAvNmBd3X4SDiv+n+XOT8lX3gA== X-Google-Smtp-Source: AA0mqf66InUYQ4jV/zsNVu7Ndu4kgqu5ytNd5eYn74cJmXovyhEswE58lpm+TRdzeAqh5oHcIJQkslIJPyLM6PyqGes= X-Received: by 2002:a25:3851:0:b0:6f0:6175:2cc7 with SMTP id f78-20020a253851000000b006f061752cc7mr53224010yba.93.1670249522735; Mon, 05 Dec 2022 06:12:02 -0800 (PST) MIME-Version: 1.0 References: <20221205132558.63484-1-glider@google.com> In-Reply-To: <20221205132558.63484-1-glider@google.com> From: Marco Elver Date: Mon, 5 Dec 2022 15:11:26 +0100 Message-ID: Subject: Re: [PATCH] kmsan: fix memcpy tests To: Alexander Potapenko Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, dvyukov@google.com, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: D506F4001D X-Stat-Signature: f83djaq4irx3c4bpdodu8ggygn8k4pxd X-Rspam-User: X-Spamd-Result: default: False [-2.08 / 9.00]; BAYES_HAM(-5.18)[98.17%]; SORBS_IRL_BL(3.00)[209.85.219.182:from]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; TO_DN_SOME(0.00)[]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; DKIM_TRACE(0.00)[google.com:+]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; RCPT_COUNT_FIVE(0.00)[5]; ARC_NA(0.00)[] X-Rspamd-Server: rspam08 X-HE-Tag: 1670249523-590190 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 5 Dec 2022 at 14:26, Alexander Potapenko wrote: > > Recent Clang changes may cause it to delete calls of memcpy(), if the > source is an uninitialized volatile local. > This happens because passing a pointer to a volatile local into memcpy() > discards the volatile qualifier, giving the compiler a free hand to > optimize the memcpy() call away. > > To outsmart the compiler, we call __msan_memcpy() instead of memcpy() > in test_memcpy_aligned_to_aligned(), test_memcpy_aligned_to_unaligned() > and test_memcpy_aligned_to_unaligned2(), because it's the behavior of > __msan_memcpy() we are testing here anyway. > > Signed-off-by: Alexander Potapenko It might be nice to retain memcpy() calls somehow, as that tests end-to-end that the compiler does the right thing here i.e. replacing the memcpy() calls with instrumented versions. Does OPTIMIZER_HIDE_VAR() help? This should prevent the compiler from seeing it's uninitialized. > --- > mm/kmsan/kmsan_test.c | 16 +++++++++++++--- > 1 file changed, 13 insertions(+), 3 deletions(-) > > diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c > index 9a29ea2dbfb9b..8e4f206a900ae 100644 > --- a/mm/kmsan/kmsan_test.c > +++ b/mm/kmsan/kmsan_test.c > @@ -406,6 +406,16 @@ static void test_printk(struct kunit *test) > KUNIT_EXPECT_TRUE(test, report_matches(&expect)); > } > > +/* > + * The test_memcpy_xxx tests below should be calling memcpy() to copy an > + * uninitialized value from a volatile int. But such calls discard the volatile > + * qualifier, so Clang may optimize them away, breaking the tests. > + * Because KMSAN instrumentation pass would just replace memcpy() with > + * __msan_memcpy(), do that explicitly to trick the optimizer into preserving > + * the calls. > + */ > +void *__msan_memcpy(void *, const void *, size_t); > + > /* > * Test case: ensure that memcpy() correctly copies uninitialized values between > * aligned `src` and `dst`. > @@ -419,7 +429,7 @@ static void test_memcpy_aligned_to_aligned(struct kunit *test) > kunit_info( > test, > "memcpy()ing aligned uninit src to aligned dst (UMR report)\n"); > - memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src)); > + __msan_memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src)); > kmsan_check_memory((void *)&dst, sizeof(dst)); > KUNIT_EXPECT_TRUE(test, report_matches(&expect)); > } > @@ -441,7 +451,7 @@ static void test_memcpy_aligned_to_unaligned(struct kunit *test) > kunit_info( > test, > "memcpy()ing aligned uninit src to unaligned dst (UMR report)\n"); > - memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); > + __msan_memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); > kmsan_check_memory((void *)dst, 4); > KUNIT_EXPECT_TRUE(test, report_matches(&expect)); > } > @@ -464,7 +474,7 @@ static void test_memcpy_aligned_to_unaligned2(struct kunit *test) > kunit_info( > test, > "memcpy()ing aligned uninit src to unaligned dst - part 2 (UMR report)\n"); > - memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); > + __msan_memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); > kmsan_check_memory((void *)&dst[4], sizeof(uninit_src)); > KUNIT_EXPECT_TRUE(test, report_matches(&expect)); > } > -- > 2.39.0.rc0.267.gcb52ba06e7-goog >