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: 13064480 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 99E77C4321E for ; Mon, 5 Dec 2022 13:26:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB62A8E0002; Mon, 5 Dec 2022 08:26:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C665F8E0001; Mon, 5 Dec 2022 08:26:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2DD78E0002; Mon, 5 Dec 2022 08:26:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A0BC78E0001 for ; Mon, 5 Dec 2022 08:26:05 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 752CB1C633D for ; Mon, 5 Dec 2022 13:26:05 +0000 (UTC) X-FDA: 80208325890.22.08A432C Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf18.hostedemail.com (Postfix) with ESMTP id 1CBB21C0009 for ; Mon, 5 Dec 2022 13:26:03 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Yhzw2muF; spf=pass (imf18.hostedemail.com: domain of 3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670246764; a=rsa-sha256; cv=none; b=gwFl7zTqBvriMUgZW65QVOMBXEma9y8Ta1itQqy44C75y1zxclAlYUf7mu8vNwwoS6s81H 9NIs5zCkcVkApBSylhq5v+1jqJbi+9VmT0gj0uX3kVMUaoUkH5q+Uy/b4iFFm70gDZpLb4 8a4jLys/L4MU/pcqWhK8Nz4TvaiUHBg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Yhzw2muF; spf=pass (imf18.hostedemail.com: domain of 3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.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=1670246764; 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: references:dkim-signature; bh=6Q1FOgaXJCU20iA2bNkMa3PVXV/y1f/l7XBgDZn5WKg=; b=w5/rMBu/BKEba6QKwSBJU+cMbUcFpElhqeCJ2pwKLwGV5JcMLlkyY/hIZl33qhNpvwnWYs /lmOKq9Awb+jmw2oarqKpOoSf74keOeGLj4UyxJOpmt7k1WRqenJ2/z0H6UwjWUKg0ise+ ogEpTu6UmC4gPmKMZYZPQnvFiNt7HIM= Received: by mail-ed1-f73.google.com with SMTP id m13-20020a056402510d00b0046913fa9291so5816119edd.6 for ; Mon, 05 Dec 2022 05:26:03 -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=6JcV2bOBoloZdTLaFnYgGVGN8gPiKFtXtfXg342rREy395Dk9LRXYndXRHfVkyA/L0 TDPq2vweLO23FkRGYfmoYV+xkt3nvQLU53SMIIzz5UIfddSmAAE0tlSdmw7E4i32GkFT +MNc666sl0JcAlTEHFzWPevZuCGrl6UMu3O1fn/0fH1cYcSF3GWok/Bww+T4bTjYMrwx d5x21EKSlgznjz1rGBvleTVaIPBfmf/hYJZlrUjxNeYNW+4PZBfbmJEJbMmIocexa75Y A7Jc+92cU6keXYkvl2CPPPQaK5czPwUJInTF2Qt/u1sOh7YSUJbtNWQzJULw3o+keJVm 0+Vg== X-Gm-Message-State: ANoB5pkhhcR8MudiUJplRDueCtV5tanP1kyuaFMiX9NrSR/m6hflFGkA nI90qOz3x0AFvjRDXHGRAb0Zt58nv/M= X-Google-Smtp-Source: AA0mqf7+LL7XxZUEb5XnNNcoladIKFWRiEDvClcGAXQbEL4/Xv1kfM4x36B5EWFDxzURqPf8uaOc0T3i24A= 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-Rspam-User: X-Spamd-Result: default: False [-4.50 / 9.00]; BAYES_HAM(-5.40)[98.68%]; MID_CONTAINS_FROM(1.00)[]; DMARC_POLICY_ALLOW(-0.50)[google.com,reject]; MV_CASE(0.50)[]; FORGED_SENDER(0.30)[glider@google.com,3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17]; R_DKIM_ALLOW(-0.20)[google.com:s=20210112]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; ARC_NA(0.00)[]; RCPT_COUNT_FIVE(0.00)[6]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FROM_HAS_DN(0.00)[]; DKIM_TRACE(0.00)[google.com:+]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; FROM_NEQ_ENVFROM(0.00)[glider@google.com,3avGNYwYKCKcNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com]; TO_DN_NONE(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1] X-Rspamd-Queue-Id: 1CBB21C0009 X-Rspamd-Server: rspam01 X-Stat-Signature: 1o9m3j1wdqygnztmz3g9ccrrsusa61jx X-HE-Tag: 1670246763-259117 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: 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)); }