From patchwork Wed Mar 1 14:39:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13156068 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 44F49C7EE36 for ; Wed, 1 Mar 2023 14:39:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0EAE6B0074; Wed, 1 Mar 2023 09:39:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BBEA66B0075; Wed, 1 Mar 2023 09:39:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A38866B007B; Wed, 1 Mar 2023 09:39:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9621C6B0074 for ; Wed, 1 Mar 2023 09:39:43 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5FC5316019D for ; Wed, 1 Mar 2023 14:39:43 +0000 (UTC) X-FDA: 80520588246.08.FC7BBB0 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf16.hostedemail.com (Postfix) with ESMTP id 7DD7A180004 for ; Wed, 1 Mar 2023 14:39:41 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TZbcESbs; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3q2P_YwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3q2P_YwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677681581; 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=GvEmc+TQ/70wBv4c0+3F8h651+3YM9ltVwHdaJENm+0=; b=WWkmV9EvnWCeFxRWJvcP2Aq/M08EdObPy4jnmj6h14UtlTA9MO4oYFFa7DS4qtizQltwxV OESHqFhNR8qGLUyY3dU1ccRCYOM0NX4duwrrKqeY/jXfSh8lTYMudrI/qjWv2kbuuVWtXL VwjOV7DvZsFjmCrV6lRox2Cy7vMxTmo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TZbcESbs; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3q2P_YwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3q2P_YwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677681581; a=rsa-sha256; cv=none; b=CQWUMUAFdIV9ehKdzwEV21PSpOY+LpnI05ItCORNsHaWywCVcygjQAv4/nvbVp68Ey9F8L AXCP4dIkGPCw6UjOG8yj2VKnk4erbft5zkOzpCGce1YIbITgfmjiz/4z4p2Y10OuTfhb+a g/tNXbJ2Hnohs5WTlaGLsLQjlEINKgA= Received: by mail-ed1-f73.google.com with SMTP id p36-20020a056402502400b004bb926a3d54so4263397eda.2 for ; Wed, 01 Mar 2023 06:39:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GvEmc+TQ/70wBv4c0+3F8h651+3YM9ltVwHdaJENm+0=; b=TZbcESbsWwNX7ITUi/aZMlklXQLIxXSFS8qC4CBgfWLzgdWV4sw7TnmemklpH6W7Bc A7y93ECEz75USteW8FD3Y1XqRtJk5z3dpdOjwx44G2Jjuz+Z4hitvfk4TnGhEpzcwscE ecg0YS6TcupDfdmQ4FiWgzFt1XEr6a7zKBTWdlnNhV5j7eNaBRDBF61cN2PiP+iE5mTh 91+plLrvqvm2QV627pn0K767iy62E8FL4Syt1zWC06tG2/CxQdIgC4GhP+p/aeN6q3xd 8FPlln8dfQ68QkVsu4QnH0TBtMDPHUtGDPr6mhY3CY6Efu84yLWgr+mHbxwvH+VHgIa6 MHVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GvEmc+TQ/70wBv4c0+3F8h651+3YM9ltVwHdaJENm+0=; b=CbNbR+56xMyB3NCAV2sA0y/YYLW/B7/CrEjVGRDYB7xv07Q3IiU8QFOxt7HDIoYXgx a5218ZP1I3cziCWiMAQD4yo09LjWRYiw1fUJ+VsAxHivAJH4f10QI9kWcA2Bkl7A4bqD 89Qzmey/9MPiHvGvwD8ie4JdU0btGPW7MRQk906BB5HISZelexqIoKfWpSa5RLijsNVN iBjVD8HfLJVnCVFtgHl0anu7gztDCeXprEKKztH2d6Vsn+89QVKTNOqhCAj+MLmt5Y37 cV+s/p/EmdMI8s//mR8pd8rDa+aEEYwI4edJR1NOiOa/HmpDrup4JQf9cm6f4HCVI0zO qxkg== X-Gm-Message-State: AO0yUKX/e1THBnAdwIGZY30toob4FBX2/Vrg4hOpTS71ghur6YmaxF8K NGfP0nvrQZCxRqXoDvFBSlsVNuZTixA= X-Google-Smtp-Source: AK7set+/E1TqZIMkRHLQhUF/c0Crs4CPmSPBSgdNL2StGzl+To/OODmTIOi2hun6qk3SVyMlWubpsRLL6g8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:3c31:b0cf:1498:e916]) (user=glider job=sendgmr) by 2002:a50:d6db:0:b0:4aa:a4df:23fc with SMTP id l27-20020a50d6db000000b004aaa4df23fcmr4014799edj.1.1677681579977; Wed, 01 Mar 2023 06:39:39 -0800 (PST) Date: Wed, 1 Mar 2023 15:39:31 +0100 In-Reply-To: <20230301143933.2374658-1-glider@google.com> Mime-Version: 1.0 References: <20230301143933.2374658-1-glider@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog Message-ID: <20230301143933.2374658-2-glider@google.com> Subject: [PATCH 2/4] kmsan: another take at fixing memcpy tests From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, dave.hansen@linux.intel.com, hpa@zytor.com, akpm@linux-foundation.org, elver@google.com, dvyukov@google.com, nathan@kernel.org, ndesaulniers@google.com, kasan-dev@googlegroups.com X-Rspamd-Queue-Id: 7DD7A180004 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 84k6iowfh7r9b4xu4fageeohqniixz3z X-HE-Tag: 1677681581-247856 X-HE-Meta: U2FsdGVkX1/Tkpgg9su4vJJiDDted05lduR/PplHgNdRAO2dFOSUA1Wy3wd9eURjn6O9a6smPeAAeQONbHZ+5wqMk3SdlhwkNrlEHZU/HVXB96hxHm2uI3sqzkQekNiJJEtayei/cyjq2hKngiprWnyDcWKOW12t02OQ39EyvCncnx0Rv9OBpxeIIRWgtnX/1tFrwLImXVGR6WAULvSUlV7ys3icZohPu8m4P1V4I0gHkzbqtpTTDDP/NYuC4sEWFLLlY8eOlKV3BHIDeNHidKQbjtrhiZo9FphJZYrrIE0KkZ7NJy94S+rChRjHmt02Z9u5NppgCYCLRtLHEXdZXITGq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LnzHNOFKmAaTVvob1Z7NbUX7lx0Q6H1b+L0Mg+hB3W3Di7fFGZOw4gtg435rOoLzR6+KR6CRzehj5q0p7mSbU1JImx5q5KoJiAMuCKLUBAo6PdZz3n5O6U8MMYiwaO5YDwJPJ9ifbpdbGPJpIgmLJGDD6Cd0+9EwRqwUyJJKOO2s0h7RT6E0xn1lpiOAWUPr26FHILtfaG0Gw== 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: 5478afc55a21 ("kmsan: fix memcpy tests") uses OPTIMIZER_HIDE_VAR() to hide the uninitialized var from the compiler optimizations. However OPTIMIZER_HIDE_VAR(uninit) enforces an immediate check of @uninit, so memcpy tests did not actually check the behavior of memcpy(), because they always contained a KMSAN report. Replace OPTIMIZER_HIDE_VAR() with a file-local asm macro that just clobbers the memory, and add a test case for memcpy() that does not expect an error report. Also reflow kmsan_test.c with clang-format. Signed-off-by: Alexander Potapenko --- mm/kmsan/kmsan_test.c | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index 088e21a48dc4b..cc98a3f4e0899 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -407,6 +407,36 @@ static void test_printk(struct kunit *test) KUNIT_EXPECT_TRUE(test, report_matches(&expect)); } +/* + * Prevent the compiler from optimizing @var away. Without this, Clang may + * notice that @var is uninitialized and drop memcpy() calls that use it. + * + * There is OPTIMIZER_HIDE_VAR() in linux/compier.h that we cannot use here, + * because it is implemented as inline assembly receiving @var as a parameter + * and will enforce a KMSAN check. + */ +#define DO_NOT_OPTIMIZE(var) asm("" ::: "memory") + +/* + * Test case: ensure that memcpy() correctly copies initialized values. + */ +static void test_init_memcpy(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + volatile int src; + volatile int dst = 0; + + // Ensure DO_NOT_OPTIMIZE() does not cause extra checks. + DO_NOT_OPTIMIZE(src); + src = 1; + kunit_info( + test, + "memcpy()ing aligned initialized src to aligned dst (no reports)\n"); + memcpy((void *)&dst, (void *)&src, sizeof(src)); + kmsan_check_memory((void *)&dst, sizeof(dst)); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + /* * Test case: ensure that memcpy() correctly copies uninitialized values between * aligned `src` and `dst`. @@ -420,7 +450,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"); - OPTIMIZER_HIDE_VAR(uninit_src); + DO_NOT_OPTIMIZE(uninit_src); memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src)); kmsan_check_memory((void *)&dst, sizeof(dst)); KUNIT_EXPECT_TRUE(test, report_matches(&expect)); @@ -443,7 +473,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"); - OPTIMIZER_HIDE_VAR(uninit_src); + DO_NOT_OPTIMIZE(uninit_src); memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); kmsan_check_memory((void *)dst, 4); KUNIT_EXPECT_TRUE(test, report_matches(&expect)); @@ -467,13 +497,14 @@ 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"); - OPTIMIZER_HIDE_VAR(uninit_src); + DO_NOT_OPTIMIZE(uninit_src); 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)); } -static noinline void fibonacci(int *array, int size, int start) { +static noinline void fibonacci(int *array, int size, int start) +{ if (start < 2 || (start == size)) return; array[start] = array[start - 1] + array[start - 2]; @@ -482,8 +513,7 @@ static noinline void fibonacci(int *array, int size, int start) { static void test_long_origin_chain(struct kunit *test) { - EXPECTATION_UNINIT_VALUE_FN(expect, - "test_long_origin_chain"); + EXPECTATION_UNINIT_VALUE_FN(expect, "test_long_origin_chain"); /* (KMSAN_MAX_ORIGIN_DEPTH * 2) recursive calls to fibonacci(). */ volatile int accum[KMSAN_MAX_ORIGIN_DEPTH * 2 + 2]; int last = ARRAY_SIZE(accum) - 1; @@ -515,6 +545,7 @@ static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_uaf), KUNIT_CASE(test_percpu_propagate), KUNIT_CASE(test_printk), + KUNIT_CASE(test_init_memcpy), KUNIT_CASE(test_memcpy_aligned_to_aligned), KUNIT_CASE(test_memcpy_aligned_to_unaligned), KUNIT_CASE(test_memcpy_aligned_to_unaligned2),