From patchwork Mon Dec 5 13:25:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 29679 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2253466wrr; Mon, 5 Dec 2022 05:30:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf4rIL2t+gxdObsgAUhjMw+cEzSGIYeFXa0Yqf9B0Ar+KYBDo0TwYYK4fFK9tdQFcvFl2T/X X-Received: by 2002:a05:6402:2409:b0:45c:935b:ae15 with SMTP id t9-20020a056402240900b0045c935bae15mr51209920eda.357.1670247051983; Mon, 05 Dec 2022 05:30:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670247051; cv=none; d=google.com; s=arc-20160816; b=a0exUfvQxeW67huh0djSWU3bs/lWNdeRkoZSRSXGLfpLSt6npeDh6nBWnBwiU9o/Tu nTtKjy1LEIA/c4K7PAuvSiC72+V3soLSUC/fH5QiaBaWLHUSd8lXbfbnbo/w5iBweR3s 4rQILqUftbXcnu3A1Ey2pRVftzJTKUeIFXSksH++mDAD4VJbaZq0EMKWdZdu2d31wA+H r9sdh3KOFB39Cf4cEW5ezf7Jg9bZ3rU6GcHmhGDNYidGOWokftec0AHwS8W1tauP+fHn xfA2ISCU8fOJali4DTxLh0ZgQy7PyycuG2qo/808GUbpc5gwx6kPXSPo5j3a4oVWAMXT bZyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=6Q1FOgaXJCU20iA2bNkMa3PVXV/y1f/l7XBgDZn5WKg=; b=r5L6zJRJLrava5lrR5QbL4k/sEE/TEeSzaUTcWK3sDmPklVKJ3SGy8+SpKOdXn48I1 bF19fdLtrtXAgC35HzI31t0m1Y3TMjLgQk6ndKsic0WoMacZandOnMarSPiNfwPbRYRU wOFxO6BXcA+Hx3C+AMr+ZZF+7YjYxbmkrv89rZ0/hN25Ob9Zz6+Rs9jflxVWj6fWSNet CZggDfriiWPuGfVs52AoopOe0PO9ZitmwxHeWx9osxycawq3OumIC/nQ6m8+T/TphXWi N7zZFEvaotV+j/r30bdq0k12HexeeH31CpEWr5cE31sDDNH2fE1E2MeM9il/Y3KGPPdT Q1Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Yhzw2muF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm19-20020a05640222d300b0046aee4c4ebfsi10671873edb.105.2022.12.05.05.30.27; Mon, 05 Dec 2022 05:30:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Yhzw2muF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232024AbiLEN0G (ORCPT + 99 others); Mon, 5 Dec 2022 08:26:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230170AbiLEN0F (ORCPT ); Mon, 5 Dec 2022 08:26:05 -0500 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22E4C1AF22 for ; Mon, 5 Dec 2022 05:26:04 -0800 (PST) Received: by mail-ej1-x649.google.com with SMTP id sa20-20020a1709076d1400b007bbe8699c2eso7352517ejc.6 for ; Mon, 05 Dec 2022 05:26:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=6Q1FOgaXJCU20iA2bNkMa3PVXV/y1f/l7XBgDZn5WKg=; b=Yhzw2muFhkrSpctjHMrj8SsAHFmyGJCXw6GpFFpUvURi+fmuds1oRDxQ8dgOH2DD4Q ap73GlrkaWuJQxDSzFxkDTSFaj5dZj3G1U3sQBuQkjC9Guoa8p8bjlHn7RcveQWdIC2o VFNSlvksKYP+RcyshKu3SzHfNTYlt+HarAf6plGqFrftjo8al/e47fDFjewh1ch5k280 q+NSy4Gc/MFAVWRmK3XBzk1yObLS9EUexj6mLwQF9/38lU/H2eN1ngHaNI7Qm4qTmCji f84MRIQIsobvFVOtgzMkgRh4dzqQPYxncRp+ZeC03VP3KPo5x+Ki+XmF+xzWCd6uMGAe HjPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=6Q1FOgaXJCU20iA2bNkMa3PVXV/y1f/l7XBgDZn5WKg=; b=qs6RzxzH28Bo14C/qGUII9trAjP0XTXoyNEM/66ssSYbX0mwS+gtDo4NnnYB2DGHR+ 0sonnWWAmN9yT+eO+tEQ7gm/ohsxiiv+DazCr5jLTBDQ1M1w69njS8rx5rSNXDjjEtFd FfN//YAAl1T6L1D5md8bN77sOpbJEO+ZaEAgmJysSIUv1uaD1RM/jybeoBXAsH733g2Q pHFRK7tu8TxbcicSIPzanc3364DQrOztoHnkuV4gX2uzHAv0gpSM3VkUFpENbvOLKcLt WxgP/I2Jhz9Hw8OscOiTSETeEPK8CH9h0EdP9ZEeeWYI5e0J4hbgi0pKs8SrR4frqQLA mH7w== X-Gm-Message-State: ANoB5pkQrcvqO7zXekqPlaD8rvbYcDnRxLeGb9nvQ37sO+xYE3Ei4GHb 04fJIKfHb1TNl9qcerGn1R4/UPyKYRM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:961a:360d:6816:d7ba]) (user=glider job=sendgmr) by 2002:a05:6402:790:b0:46c:cd6e:811a with SMTP id d16-20020a056402079000b0046ccd6e811amr2821841edy.352.1670246762530; Mon, 05 Dec 2022 05:26:02 -0800 (PST) Date: Mon, 5 Dec 2022 14:25:58 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221205132558.63484-1-glider@google.com> Subject: [PATCH] kmsan: fix memcpy tests From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, elver@google.com, dvyukov@google.com, linux-mm@kvack.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751380972588053512?= X-GMAIL-MSGID: =?utf-8?q?1751380972588053512?= 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 --- 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)); }