Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4198160ybf; Tue, 3 Mar 2020 22:36:30 -0800 (PST) X-Google-Smtp-Source: ADFU+vt/G+bvsdv2VwlESQQHk3KeCJIbMdCfOkZ/kZAw00ONs+TyjdkkuEEd9Mbe2v1TbpWpoSiX X-Received: by 2002:a9d:7696:: with SMTP id j22mr1284110otl.188.1583303790601; Tue, 03 Mar 2020 22:36:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583303790; cv=none; d=google.com; s=arc-20160816; b=zTfB7L1j5rv/6TzYDzrfQu6SUl8EDS7kjRbMlvQlqCXV1i4RPX9abPkCf4vV24RxBP 3O/DMQj7xVJ054PAs6IuGFVi9EEj2lkR2Vlq0lGWLGHPpieBShC3BgNi4P6oMJLOsNOC K58IwInqnEvmwoBO9t/v7+nj201G5Itl0zWsXjs8q/0J+DmVEAiTceiuW0EXubzzy0rW qgA9XuwrOisOEFUzVEXybY7vHRKH9jBK3W73kALgD60wKG3jXItWPRRUAK1Ddj9Ow3cJ qLpttijOinhULHAHS1icnjSQxEe+jWKpmkeW1gh+oBf7bSpBYTvlI+RuKW2eWO3/3Df1 yBCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=pRV4q0RECloAIigH1EtXD7ojzqVokhP4l3P7T2qRJSc=; b=dDAXcD0cllqyX6mfv5uO8tsmN03s6Y/Pp1uXTZh9EP+143e8d9gDZ7Vo9d/BrpZ/Gz il3ukE3CtpArjGMDXsqaLHhRxswdFPWrLV/0JOIO+lQUt+/i+55avyaetH0UzDYvRyYw elY4R6svwhpN4GT111LOFOvKiEmxm11TDLifAXdN/GFuxRYBHhzpKzhGUzSph0VgvYCq 4YSK4MlmV5JCAWvFqzAUsU7ViRvKzJExXvpFUL9uvzYaNitMn713X81NXBnK1eBcsZW8 9ts+qBE2WBhdCl9SMCiJeYuRpqdr6KfNHedKAjvj1EdGqSqUN4UWdqVukT+XzJkKP0jE 5Y4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vHlVS1j7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id m1si656580otn.271.2020.03.03.22.36.19; Tue, 03 Mar 2020 22:36:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vHlVS1j7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728244AbgCDGfQ (ORCPT + 99 others); Wed, 4 Mar 2020 01:35:16 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:35000 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725797AbgCDGfP (ORCPT ); Wed, 4 Mar 2020 01:35:15 -0500 Received: by mail-qt1-f194.google.com with SMTP id v15so591679qto.2 for ; Tue, 03 Mar 2020 22:35:14 -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=pRV4q0RECloAIigH1EtXD7ojzqVokhP4l3P7T2qRJSc=; b=vHlVS1j783AEDx9vp9QAk6qirR5NrKJfcrCnKHY9716hTKVBuGENHJ14Y6hDFA4bGt L0tAKuz33Ryfvl4ZpwzrDoXROn+IikVwXuMOLgHPpiMJ8u2TCfF321HUj+ShcjgkfTVn MySpj96WSVvRzspSZu2MhTqlDDlHIIKuJPpFzcP/wz1oJsTPIV20H/eJfxyZTDMVaGzQ 1vJ0BHr83tLkSwevxEZPqEIItbiRyPNTTYNi7D7PsbpWXQr8ghN3gqKkwf9eSgsBP0eR jCKC2Ccwa+0jtT7gjfyko9MJp91G2awKYC3zWZh1qdbVFYYRDpkeJruFqvXyc6HYjRo9 Jong== 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=pRV4q0RECloAIigH1EtXD7ojzqVokhP4l3P7T2qRJSc=; b=PTkLr68GcBY4CU5oRNlFNn2fsQ2nxQ9BllvzSIXXQTL8qhqbtLeBlgGQmvc3M5XIGh 05/5A98w31WkVo7Vj5LUe1daimXRxAlVodpbD04wCpG3dvcTq5fo+zD35RiXARVMiQPg vdSpYy1bk/LEolT53Q2/GavYEtlkrVTpmDJ+Mxw3nAk3o2h7FRWtiHu3zC5BUCQQERxH fU5AgaLrdLqk59ALLPNnfmN7lqzaXBPJRyGINZDMsnnREYy3kK9OFmuUlojgIL3D+IBa S89siP0QOWnwptWFeLHXWbhvt6/TU9egMjqn0qy1cj/4Wk58O8cCXuPtKZE7gwf0j+O3 wVCA== X-Gm-Message-State: ANhLgQ2Ews9WRz/Ylj7hm5bHVO1GOEkkBGzPVh5fwJ/Qm8rht8pvt0wq etlwhcoU0RoKgI+uyfAcUW9pQ+p3Bvgo181Y8GC3gA== X-Received: by 2002:ac8:1846:: with SMTP id n6mr1102349qtk.257.1583303714054; Tue, 03 Mar 2020 22:35:14 -0800 (PST) MIME-Version: 1.0 References: <20200227024301.217042-1-trishalfonso@google.com> <20200227024301.217042-2-trishalfonso@google.com> In-Reply-To: <20200227024301.217042-2-trishalfonso@google.com> From: Dmitry Vyukov Date: Wed, 4 Mar 2020 07:35:02 +0100 Message-ID: Subject: Re: [RFC PATCH 2/2] KUnit: KASAN Integration To: Patricia Alfonso Cc: Andrey Ryabinin , Brendan Higgins , David Gow , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , LKML , kasan-dev , "open list:KERNEL SELFTEST FRAMEWORK" , kunit-dev@googlegroups.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 27, 2020 at 3:44 AM 'Patricia Alfonso' via kasan-dev wrote: > > Integrate KASAN into KUnit testing framework. > - Fail tests when KASAN reports an error that is not expected > - Use KUNIT_EXPECT_KASAN_FAIL to expect a KASAN error in KASAN tests > - KUnit struct added to current task to keep track of the current test > from KASAN code > - Booleans representing if a KASAN report is expected and if a KASAN > report is found added to kunit struct > - This prints "line# has passed" or "line# has failed" > > Signed-off-by: Patricia Alfonso > --- > If anyone has any suggestions on how best to print the failure > messages, please share! > > One issue I have found while testing this is the allocation fails in > kmalloc_pagealloc_oob_right() sometimes, but not consistently. This > does cause the test to fail on the KUnit side, as expected, but it > seems to skip all the tests before this one because the output starts > with this failure instead of with the first test, kmalloc_oob_right(). > > include/kunit/test.h | 24 ++++++++++++++++++++++++ > include/linux/sched.h | 7 ++++++- > lib/kunit/test.c | 7 ++++++- > mm/kasan/report.c | 19 +++++++++++++++++++ > tools/testing/kunit/kunit_kernel.py | 2 +- > 5 files changed, 56 insertions(+), 3 deletions(-) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 2dfb550c6723..2e388f8937f3 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -21,6 +21,8 @@ struct kunit_resource; > typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *); > typedef void (*kunit_resource_free_t)(struct kunit_resource *); > > +void kunit_set_failure(struct kunit *test); > + > /** > * struct kunit_resource - represents a *test managed resource* > * @allocation: for the user to store arbitrary data. > @@ -191,6 +193,9 @@ struct kunit { > * protect it with some type of lock. > */ > struct list_head resources; /* Protected by lock. */ > + > + bool kasan_report_expected; > + bool kasan_report_found; > }; > > void kunit_init_test(struct kunit *test, const char *name); > @@ -941,6 +946,25 @@ do { \ > ptr, \ > NULL) > > +/** > + * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does > + * not cause a KASAN error. > + * > + */ > +#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \ > + test->kasan_report_expected = true; \ > + test->kasan_report_found = false; \ > + condition; \ > + if (test->kasan_report_found == test->kasan_report_expected) { \ > + pr_info("%d has passed", __LINE__); \ > + } else { \ > + kunit_set_failure(test); \ > + pr_info("%d has failed", __LINE__); \ > + } \ > + test->kasan_report_expected = false; \ > + test->kasan_report_found = false; \ > +} while (0) > + > /** > * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. > * @test: The test context object. > diff --git a/include/linux/sched.h b/include/linux/sched.h > index 04278493bf15..db23d56061e7 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -32,6 +32,8 @@ > #include > #include > > +#include > + > /* task_struct member predeclarations (sorted alphabetically): */ > struct audit_context; > struct backing_dev_info; > @@ -1178,7 +1180,10 @@ struct task_struct { > > #ifdef CONFIG_KASAN > unsigned int kasan_depth; > -#endif > +#ifdef CONFIG_KUNIT > + struct kunit *kasan_kunit_test; > +#endif /* CONFIG_KUNIT */ > +#endif /* CONFIG_KASAN */ > > #ifdef CONFIG_FUNCTION_GRAPH_TRACER > /* Index of current stored address in ret_stack: */ > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 9242f932896c..d266b9495c67 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -9,11 +9,12 @@ > #include > #include > #include > +#include > > #include "string-stream.h" > #include "try-catch-impl.h" > > -static void kunit_set_failure(struct kunit *test) > +void kunit_set_failure(struct kunit *test) > { > WRITE_ONCE(test->success, false); > } > @@ -236,6 +237,10 @@ static void kunit_try_run_case(void *data) > struct kunit_suite *suite = ctx->suite; > struct kunit_case *test_case = ctx->test_case; > > +#ifdef CONFIG_KASAN > + current->kasan_kunit_test = test; > +#endif > + > /* > * kunit_run_case_internal may encounter a fatal error; if it does, > * abort will be called, this thread will exit, and finally the parent > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index 5ef9f24f566b..5554d23799a5 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -32,6 +32,8 @@ > > #include > > +#include > + > #include "kasan.h" > #include "../slab.h" > > @@ -461,6 +463,15 @@ void kasan_report_invalid_free(void *object, unsigned long ip) > u8 tag = get_tag(object); > > object = reset_tag(object); > + > + if (current->kasan_kunit_test) { > + if (current->kasan_kunit_test->kasan_report_expected) { > + current->kasan_kunit_test->kasan_report_found = true; > + return; I think we need to continue and print KASAN report even in this case. 2 reasons: - tests don't check validity of printed reports, but at least human can verify sanity of the report - report printing code also needs to be tested, at least that it does not crash/hang If we don't print reports, it may look nicer, but will be less useful. > + } > + kunit_set_failure(current->kasan_kunit_test); > + } > +