From patchwork Fri May 24 23:28:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Johannesmeyer X-Patchwork-Id: 13673807 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 7D3C7C25B74 for ; Fri, 24 May 2024 23:28:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1643F6B008C; Fri, 24 May 2024 19:28:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 114976B0092; Fri, 24 May 2024 19:28:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02A986B0093; Fri, 24 May 2024 19:28:12 -0400 (EDT) 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 D927B6B008C for ; Fri, 24 May 2024 19:28:12 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8BB1D1C15D6 for ; Fri, 24 May 2024 23:28:12 +0000 (UTC) X-FDA: 82154880024.27.8D6DE81 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by imf27.hostedemail.com (Postfix) with ESMTP id D138140008 for ; Fri, 24 May 2024 23:28:10 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CcCYiIx6; spf=pass (imf27.hostedemail.com: domain of bjohannesmeyer@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=bjohannesmeyer@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716593290; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=bt12rpmlnyDZwJcOrWI/kuwJtRd0gfXYj/S0LGrI6gk=; b=Z//sSemHvkAkR0NRtsQxKmTNYNs0Mi7/3VjOWGakRBhhHR6t7jTW0C6H7Tjg94ub0uR/C9 0f317IWH+8fcYie1VrXJYdUbJcjM/G5HpQS+xAqr9uJsRVGLSKG4pRWs7HXQDvpUYztrKG Xe+fdHuSvG/g3TohgbobIZ+N/4uwGS0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716593290; a=rsa-sha256; cv=none; b=tG4eslsZ1ufkdCO1YkSMtdjYVrYthAKucgw+Zmy9QAGpeeLTDkR0/epELCM4HzmDfkE8Xk Xlo0x+qvNFHejs+ALBqkEJ2STAQ3P61/lA9oY445z7bFAIhZ4KLDYXgOpMGq58TZ7d8oAd F6bOK7uuhJ4tQuNH+LAhY/AujEZFql0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CcCYiIx6; spf=pass (imf27.hostedemail.com: domain of bjohannesmeyer@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=bjohannesmeyer@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a62614b9ae1so192119666b.0 for ; Fri, 24 May 2024 16:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716593289; x=1717198089; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=bt12rpmlnyDZwJcOrWI/kuwJtRd0gfXYj/S0LGrI6gk=; b=CcCYiIx6Ku7VU3pMy1A3tGyrxWpSEkxaLQZDkChS/j2jAcOovuHHsrWFbjnjDrgveZ GWZdApjO0YNX48y+Sq9ouIZ1FIz3KY78WzcFQwjlbtXzOZ9OuSqdFzkLpMqp/T+u9s9+ 1AOraNQ/A2854j9klWF/CohzxCmUZSxtry0CtW0GCs8nxTYl3fqUPWaXtTlrQd5r+5EI 2Vr4H5mK4UH+S1borWa8ZZP+JfzBiG7gE0DM6t6LdRn+aA/33Cpx7gLa75oN7b2hDIel iZagzgx+CdI80UMfFV3Stdeq1e/8kqXw9KwFeYtxleUh0a0Afg2DFUF9LuSi9V8325Lc s69w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716593289; x=1717198089; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bt12rpmlnyDZwJcOrWI/kuwJtRd0gfXYj/S0LGrI6gk=; b=KxdQy2PX8zn6TzbOKalWlOAaJQ0M3daNgN+Mw7LISiy/FAzjH3DH3li5BQp1PkE2SN HdmNMpMBb5mY6De1tJNrr3AcmDdl2cqOKWxj203I9tLjCuwa/MauNPEmQpnoRYiyqnit jZW8iIZlOSKv0nieI/VDr9Wtp7KbZXh/UlgaJFEa5ivLfxwc2pZKOYJwzr5kW68zD0FW /wLBxOCkbEIOAqHKQXybJkbfZZJyRcNFHFSHqSZbeDRHUMxapNKZCNVkwMxK49xGs6O0 9gzLrYli1h7RY1uKs3prp8a6R0JbV8fa9EvF9HiB0c1TECj0+/cJJoTp7I52S1gJwCc1 oqbw== X-Forwarded-Encrypted: i=1; AJvYcCVztcQ4KhLdMOzCXwpa+15SSM5QjwKKuHyIw4Sbw+LV2rkuZuXR711nYRQCPdaCy72LLu9crwwb6EALoX1vzxMvOVM= X-Gm-Message-State: AOJu0YzBNTnNbBDsq0HANPy/LTzu5bcm2Jf2E7qB0rVVy+S01Wcl+V90 cUfmhKofBbB1UWr50n8LYP6BMVDdylvhEMUbOlNBOfHF9B7PuqdA X-Google-Smtp-Source: AGHT+IGpfslAbfzk7VNl/6HnBSnB0wsBEY6xQRIZ0SSqJz5JQtuVi6nolKCIvGFzY4MR5Ttl539EoQ== X-Received: by 2002:a17:906:c214:b0:a5a:8b8c:6203 with SMTP id a640c23a62f3a-a62646d7f32mr231676666b.45.1716593289010; Fri, 24 May 2024 16:28:09 -0700 (PDT) Received: from rex.hwlab.vusec.net (lab-4.lab.cs.vu.nl. [192.33.36.4]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c817714sm191389366b.29.2024.05.24.16.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 16:28:08 -0700 (PDT) From: Brian Johannesmeyer To: Brian Johannesmeyer , Alexander Potapenko , Marco Elver , Dmitry Vyukov , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] kmsan: introduce test_unpoison_memory() Date: Sat, 25 May 2024 01:28:04 +0200 Message-Id: <20240524232804.1984355-1-bjohannesmeyer@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D138140008 X-Rspam-User: X-Stat-Signature: m9n3kucm7spm6wfmxxrc7i6ky765gtid X-HE-Tag: 1716593290-720696 X-HE-Meta: U2FsdGVkX1/oQPDyvcbf7f4S3Vsd/V7SiJBeHxRHmqdvE2MJV/L+U2t56ZgpMg5n7bFR+wl3xRYB3HNIC2+b4/QEZ/A2j9zvuaDucb/5XxQBO7Nfv5m+otLt+5x9ikQE6EZogA4EefXzDbHCwoKaEq+/cj/GHTQxUxAufhhBcgfhxACCXvOv7hucxMpySWx5FapPj59USgA1LTvpfXaVOqnIUra7JbtmZnb9UGBIOVM6nYLmGrB1C6F6UkY4iOHCKlibd5902B/HMdgHvIom9Ya1K6VbSybsiV54pxd5/xjgm5li/7/nHW2J4gMI3Sd91GmWYHpDm4BWttCRN7UI4SsHKTBpU95WJbHxQKVIMPbK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S03qRgZf/v19UjAIonXC1aqOMPdK7LQxEB/0ZaFapgXrBrTc= 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: List-Subscribe: List-Unsubscribe: Add a regression test to ensure that kmsan_unpoison_memory() works the same as an unpoisoning operation added by the instrumentation. (Of course, please correct me if I'm misunderstanding how these should work). The test has two subtests: one that checks the instrumentation, and one that checks kmsan_unpoison_memory(). Each subtest initializes the first byte of a 4-byte buffer, then checks that the other 3 bytes are uninitialized. Unfortunately, the test for kmsan_unpoison_memory() fails to identify the 3 bytes as uninitialized (i.e., the line with the comment "Fail: No UMR report"). As to my guess why this is happening: From kmsan_unpoison_memory(), the backing shadow is indeed correctly overwritten in kmsan_internal_set_shadow_origin() via `__memset(shadow_start, b, size);`. Instead, the issue seems to stem from overwriting the backing origin, in the following `origin_start[i] = origin;` loop; if we return before that loop on this specific call to kmsan_unpoison_memory(), then the test passes. Signed-off-by: Brian Johannesmeyer --- mm/kmsan/kmsan_test.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index 07d3a3a5a9c5..c3ab90df0abf 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -614,6 +614,30 @@ static void test_stackdepot_roundtrip(struct kunit *test) KUNIT_EXPECT_TRUE(test, report_matches(&expect)); } +/* + * Test case: ensure that kmsan_unpoison_memory() and the instrumentation work + * the same + */ +static void test_unpoison_memory(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_unpoison_memory"); + volatile char a[4], b[4]; + + kunit_info( + test, + "unpoisoning via the instrumentation vs. kmsan_unpoison_memory() (2 UMR reports)\n"); + + a[0] = 0; // Initialize a[0] + kmsan_check_memory((char *)&a[1], 3); // Check a[1]--a[3] + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); // Pass: UMR report + + report_reset(); + + kmsan_unpoison_memory((char *)&b[0], 1); // Initialize b[0] + kmsan_check_memory((char *)&b[1], 3); // Check b[1]--b[3] + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); // Fail: No UMR report +} + static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_uninit_kmalloc), KUNIT_CASE(test_init_kmalloc), @@ -637,6 +661,7 @@ static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_memset64), KUNIT_CASE(test_long_origin_chain), KUNIT_CASE(test_stackdepot_roundtrip), + KUNIT_CASE(test_unpoison_memory), {}, };