Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp490370pxb; Wed, 13 Jan 2021 08:27:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwJzi7KXXZSHSDTFBG6/hjabeIXjYQXfsiwSQ9RqljokSWDUT1v8kztN4uecDwknVnTdSxs X-Received: by 2002:a17:906:9388:: with SMTP id l8mr2145056ejx.22.1610555231452; Wed, 13 Jan 2021 08:27:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610555231; cv=none; d=google.com; s=arc-20160816; b=wIhlJShdab+ZwjWRtyNzHTA7/FSunDLS4fQ7Hrj2UX+yooup2GKgjjNdevlbIx4oh1 nSryE6KUKSkAuHPkyuzKgyKTJCKdp/9fsXtYrEAqdibhCrIOl4fkQRPERpEPVq6n2Lv/ C59bRjFqCLFjm2LKJGXpvtDq+GE/22db8qWBiM1SdYK50uJvxICzaZe9jtVzK18B+2y/ r03/cB3ltLj+mnWAGPmNxvY9VMC9ArSCmnr+aKlgBLRChuaN1bFXW+9mcjrBTHvW1fvu LJ3numA8oAnt1jPANHJ5wGY7UzQVxYqsSa4xJPLe83/mdd3w8DvugSy58OoprWaNP1qr ivnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GyTHDPIRlWLUaLwyKAW0evEpBngzgktQyEH3p/lXCIQ=; b=IFRC454wyQcHJD5+IBUIMZr80nUxwvvuJ239NyEh9fva3S9OJAD8NOUiuwfnQ74Zg2 L3wlGTPkVCXZlWmE7HfeXV2pGpnk/6xvSPAVZkKq2+ZJsF30pNNuBNT3Nb/xFFQVZ4ZZ Qj684TSuJMEHKeg2nRlYtXg/pzsoWJc86zdafOeZ5CIB//QBDuyA85+IoQG99hcRDAgF FUW57ATSsdkOjmXu675NKtfO3Eh9WxEReBoD0YnClVyBJEsQrh7NBp9mNsmIe19mpcrb aJxaqvkUqf0e2LgLvel43YqYuIuXxNsbe+ptg8JmNYbB4ceisTHqg6BnBB/K7+Owkyo3 Uc9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tlmb7Dtm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ho43si1063254ejc.421.2021.01.13.08.26.47; Wed, 13 Jan 2021 08:27:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tlmb7Dtm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1727531AbhAMQY5 (ORCPT + 99 others); Wed, 13 Jan 2021 11:24:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727309AbhAMQY4 (ORCPT ); Wed, 13 Jan 2021 11:24:56 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66C6EC061575 for ; Wed, 13 Jan 2021 08:24:16 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id d189so2673238oig.11 for ; Wed, 13 Jan 2021 08:24:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GyTHDPIRlWLUaLwyKAW0evEpBngzgktQyEH3p/lXCIQ=; b=tlmb7Dtm51MoH15iZg/lhbLEU6E68eF7hqHr1ps+iDOD9RNrV57MUjlTOEoiDYpx69 2YAEmHX3h7QQSxfCGnPtws3q+v+8qoDfQgCAX/6c5bnqVjNrNclTcfs88p4Ts0qyke/v ZlCAK0KNdVFHCc/68pduQuHGmlZ+rAqUP1e16Pxj2qGysyZQe1ElKbcaJYO/2BQrFCKa BwcWhOlEbJPZrmymNTXoiudyzG6dgf19wjbt8WJxM0qyLERrMKqDZgCfl8rEZlDio16m GRYglcqBDhqtJofwb28SiGQ3j6arsVBTJccjuQnqdI/ztty+oWz6GQNyTFbNlwdDE1qX 3kvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GyTHDPIRlWLUaLwyKAW0evEpBngzgktQyEH3p/lXCIQ=; b=mvx3BK1ogN6SJ13u2h+OWAj9YbEVfvVa5/YoFH7lwvCvKrXW3/QT3sxn8oIr9+J4F3 jRKD7DUmEwCDndZAOsHfWi4rn9uGb0bCp2PQZB6cvU9+CT3dYGQwCsuOGdzURgIoRxdA Ez5yew98EzS/V1BcZDQPx8h2OQ4sA91BlVGiYcjCM1pBxexqjEQ8+UTQdnW3Y3q6tp+z 78gTRsQx7XJcPdiDqUxGOnR+GjzlKH9zllJ9IPS05Q8FyOryhpNBoGeETpdPWpgHPFCX mXIHgz5yF1GLcxQSIF7ViSJ5qCfrQQ24KGUEwAMokqUIdD3dOIFWp8EVQPF1JD5QYNAE 4R6g== X-Gm-Message-State: AOAM531RIFXk8ET3f8sqAu9e6SlirD2pvcg3lB0FYiT9roUwV6DrrKz0 xK41A2rOZkOvqTx/WYbkwZgTAY1gxIwTdF250N18jA== X-Received: by 2002:aca:c085:: with SMTP id q127mr58645oif.70.1610555055524; Wed, 13 Jan 2021 08:24:15 -0800 (PST) MIME-Version: 1.0 References: <2b43049e25dcd04850ba6c205cd6dcc7caa4a886.1610554432.git.andreyknvl@google.com> In-Reply-To: <2b43049e25dcd04850ba6c205cd6dcc7caa4a886.1610554432.git.andreyknvl@google.com> From: Marco Elver Date: Wed, 13 Jan 2021 17:24:03 +0100 Message-ID: Subject: Re: [PATCH v2 03/14] kasan: clean up comments in tests To: Andrey Konovalov Cc: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Andrew Morton , Will Deacon , Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev , Linux ARM , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 13 Jan 2021 at 17:21, Andrey Konovalov wrote: > > Clarify and update comments in KASAN tests. > > Link: https://linux-review.googlesource.com/id/I6c816c51fa1e0eb7aa3dead6bda1f339d2af46c8 > Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver > --- > lib/test_kasan.c | 59 +++++++++++++++++++++++++---------------- > lib/test_kasan_module.c | 5 ++-- > 2 files changed, 39 insertions(+), 25 deletions(-) > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > index 2947274cc2d3..6f46e27c2af7 100644 > --- a/lib/test_kasan.c > +++ b/lib/test_kasan.c > @@ -28,10 +28,9 @@ > #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE) > > /* > - * We assign some test results to these globals to make sure the tests > - * are not eliminated as dead code. > + * Some tests use these global variables to store return values from function > + * calls that could otherwise be eliminated by the compiler as dead code. > */ > - > void *kasan_ptr_result; > int kasan_int_result; > > @@ -39,14 +38,13 @@ static struct kunit_resource resource; > static struct kunit_kasan_expectation fail_data; > static bool multishot; > > +/* > + * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the > + * first detected bug and panic the kernel if panic_on_warn is enabled. > + */ > static int kasan_test_init(struct kunit *test) > { > - /* > - * Temporarily enable multi-shot mode and set panic_on_warn=0. > - * Otherwise, we'd only get a report for the first case. > - */ > multishot = kasan_save_enable_multi_shot(); > - > return 0; > } > > @@ -56,12 +54,12 @@ static void kasan_test_exit(struct kunit *test) > } > > /** > - * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does > - * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do > - * Do not use this name for a KUnit resource outside here. > - * > + * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a > + * KASAN report; causes a test failure otherwise. This relies on a KUnit > + * resource named "kasan_data". Do not use this name for KUnit resources > + * outside of KASAN tests. > */ > -#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \ > +#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \ > fail_data.report_expected = true; \ > fail_data.report_found = false; \ > kunit_add_named_resource(test, \ > @@ -69,7 +67,7 @@ static void kasan_test_exit(struct kunit *test) > NULL, \ > &resource, \ > "kasan_data", &fail_data); \ > - condition; \ > + expression; \ > KUNIT_EXPECT_EQ(test, \ > fail_data.report_expected, \ > fail_data.report_found); \ > @@ -121,7 +119,8 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test) > return; > } > > - /* Allocate a chunk that does not fit into a SLUB cache to trigger > + /* > + * Allocate a chunk that does not fit into a SLUB cache to trigger > * the page allocator fallback. > */ > ptr = kmalloc(size, GFP_KERNEL); > @@ -168,7 +167,9 @@ static void kmalloc_large_oob_right(struct kunit *test) > { > char *ptr; > size_t size = KMALLOC_MAX_CACHE_SIZE - 256; > - /* Allocate a chunk that is large enough, but still fits into a slab > + > + /* > + * Allocate a chunk that is large enough, but still fits into a slab > * and does not trigger the page allocator fallback in SLUB. > */ > ptr = kmalloc(size, GFP_KERNEL); > @@ -469,10 +470,13 @@ static void ksize_unpoisons_memory(struct kunit *test) > ptr = kmalloc(size, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); > real_size = ksize(ptr); > - /* This access doesn't trigger an error. */ > + > + /* This access shouldn't trigger a KASAN report. */ > ptr[size] = 'x'; > - /* This one does. */ > + > + /* This one must. */ > KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y'); > + > kfree(ptr); > } > > @@ -568,7 +572,7 @@ static void kmem_cache_invalid_free(struct kunit *test) > return; > } > > - /* Trigger invalid free, the object doesn't get freed */ > + /* Trigger invalid free, the object doesn't get freed. */ > KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1)); > > /* > @@ -585,7 +589,10 @@ static void kasan_memchr(struct kunit *test) > char *ptr; > size_t size = 24; > > - /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */ > + /* > + * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. > + * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. > + */ > if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { > kunit_info(test, > "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT"); > @@ -610,7 +617,10 @@ static void kasan_memcmp(struct kunit *test) > size_t size = 24; > int arr[9]; > > - /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */ > + /* > + * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. > + * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. > + */ > if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { > kunit_info(test, > "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT"); > @@ -634,7 +644,10 @@ static void kasan_strings(struct kunit *test) > char *ptr; > size_t size = 24; > > - /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */ > + /* > + * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. > + * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details. > + */ > if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { > kunit_info(test, > "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT"); > @@ -706,7 +719,7 @@ static void kasan_bitops_generic(struct kunit *test) > } > > /* > - * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes; > + * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes; > * this way we do not actually corrupt other memory. > */ > bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL); > diff --git a/lib/test_kasan_module.c b/lib/test_kasan_module.c > index 3b4cc77992d2..eee017ff8980 100644 > --- a/lib/test_kasan_module.c > +++ b/lib/test_kasan_module.c > @@ -123,8 +123,9 @@ static noinline void __init kasan_workqueue_uaf(void) > static int __init test_kasan_module_init(void) > { > /* > - * Temporarily enable multi-shot mode. Otherwise, we'd only get a > - * report for the first case. > + * Temporarily enable multi-shot mode. Otherwise, KASAN would only > + * report the first detected bug and panic the kernel if panic_on_warn > + * is enabled. > */ > bool multishot = kasan_save_enable_multi_shot(); > > -- > 2.30.0.284.gd98b1dd5eaa7-goog >