Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp5093670ybb; Tue, 24 Mar 2020 10:50:24 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsUN4d2sofVdD11Sx1UtDZ/L9xIARXywUCJfSCYzclmQhdJLXqN5cxDVQZ7md85vvMTAyvX X-Received: by 2002:aca:558c:: with SMTP id j134mr4485630oib.102.1585072224013; Tue, 24 Mar 2020 10:50:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585072224; cv=none; d=google.com; s=arc-20160816; b=YSoX0hUQQUt5gxVEtVujLfNXLrsvImJ254lD3Z1SvsPFiDNc2TEpt/EPI6VG1hjkuN VJGs+XcTC740Fin8FrwgyuauIVk/r1yBrh+csQU49bf419Zw7bzteW+uv3gNjR2LyFJy UFm8eozo7fBqee/ZVt2Bj4/a26p56DiCIYfnyEKKuuIJbp7OiMehUvVlq7AeT0pbfp7o 4434Op7K++538UaVQwmwT3wEmSU2KDPK6274lzH/2bFWaiLrSH9/vd05lCLssxhgj/No e3HIogILLKZkhoCIJeD8pmmime+FMAuWpR5JNmBaewb6U5jXbutY0iufA3VACwzr+bKz Uf0Q== 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=TzOHB9g3KOGeLI2ijxAL8EBdgHfC4lrNr/2ltbuc77Y=; b=jsLh2llOF4hggb/zrTaXJtovnKsw+XvXh6oIaweP+fSkIMBmv+071bUGiCXFUplBrR LLf56/ABDBXmKaol3pgiqZ9I+vCLwpkuoBwHXb0TbK1tzzFbGK9JcGrPJFNTBsPDEhKY 1cSdR5HeCNRVriHZ2ELuUvCTBdPiV7FWCGY7Rl/LIgeed0JaEyrjxkEeX5w2ttlRCroY rPEAYejymjgQ0tXekj4ryUFzJjBJzXgRaoRBQJHUadZxi7FNoJEnOPDhtVy3FFypmH6t JmqwMRZqRG6sOCZ5zoJ7neJfGolzqDMOmdZyhjUY6OD4JwZK5zCzTxiEuLSYPN4AzWg2 NcAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=UzoH2I2F; 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 c4si9552879otp.218.2020.03.24.10.50.10; Tue, 24 Mar 2020 10:50:23 -0700 (PDT) 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=UzoH2I2F; 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 S1727903AbgCXRs4 (ORCPT + 99 others); Tue, 24 Mar 2020 13:48:56 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:38457 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727663AbgCXRs4 (ORCPT ); Tue, 24 Mar 2020 13:48:56 -0400 Received: by mail-wr1-f68.google.com with SMTP id s1so22635653wrv.5 for ; Tue, 24 Mar 2020 10:48:54 -0700 (PDT) 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=TzOHB9g3KOGeLI2ijxAL8EBdgHfC4lrNr/2ltbuc77Y=; b=UzoH2I2FTY/43x5y/+X27TaBy4MSRZk6YoxxSda9ZgsFSJJOavyd0PVOCwCFvLH2kf 1lwZOyArtktkS38izq82lULyNjOEUb5pCzaGZi9jYz6Dkh/tWBdF/g6qTtUI2XxnuTyf iUe6lrpgdTpXcKKtWcpg+9BG5GV/WWpLAdCnCQWBKr75baAN+LE+kRIbYidhfHK8WeCJ 41lcg4rSKPXSVU4OP2wXo/+jKQr098kKuTCdYlR/oZt3norpZYS0TtMex4ykrJt8cVGc hylCWZctBTRhgtBuZR8AJDE/EeEtlpAJeTNML1DH+USLxqkQUKu04gazc2WwrWHWkxl8 Uzrg== 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=TzOHB9g3KOGeLI2ijxAL8EBdgHfC4lrNr/2ltbuc77Y=; b=uVXmmA4Jpn4KwOjyit+ZhtqYMlSBeEui4m9ibZIv9BVUtySaZJhIBR/bFBLKKcaAwn ierDyoSeImDCcvleLbzXQmC1USqD05gWgsdFxd6f+KTQ5llBPJGEIlSna/QhTMn4GteQ xd/t4TpbgoRhyaIEvnIhainWAc/9cxfPlTURKJ+0kp8DhajudP7D0wkhhfCEV/6HKC+b P0hGPZ0MpIVxj4XYkxpFS4scMn4iXRVq1tS7EVCy4K1igdKamz58XK4Zt7ByO0JPs+s0 fbdtzvkSBlzscnx98hAa890o4ELdt9yBn+hlT5BqENKxHZ3ZXIN//MhbSGWhOyB6+x+Q eA5Q== X-Gm-Message-State: ANhLgQ0GtF7OZ9tm400prMxTB//MmDufVgVbEd55FJlD5GBxILPFDeyW XrY62BzJtZ4qlgWTgSmDATTaB386Ld98wU3feekz8Q== X-Received: by 2002:adf:efc9:: with SMTP id i9mr17429674wrp.23.1585072133363; Tue, 24 Mar 2020 10:48:53 -0700 (PDT) MIME-Version: 1.0 References: <20200319164227.87419-1-trishalfonso@google.com> <20200319164227.87419-3-trishalfonso@google.com> In-Reply-To: From: Patricia Alfonso Date: Tue, 24 Mar 2020 10:48:42 -0700 Message-ID: Subject: Re: [RFC PATCH v2 2/3] KUnit: KASAN Integration To: Alan Maguire Cc: David Gow , Brendan Higgins , Andrey Ryabinin , Dmitry Vyukov , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , LKML , kasan-dev , kunit-dev@googlegroups.com, "open list:KERNEL SELFTEST FRAMEWORK" 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 Tue, Mar 24, 2020 at 9:45 AM Alan Maguire wrote: > > > On Thu, 19 Mar 2020, Patricia Alfonso 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 > > - Expected KASAN reports pass tests and are still printed when run > > without kunit_tool (kunit_tool still bypasses the report due to the > > test passing) > > - KUnit struct in current task used to keep track of the current test > > from KASAN code > > > > Make use of "[RFC PATCH kunit-next 1/2] kunit: generalize > > kunit_resource API beyond allocated resources" and "[RFC PATCH > > kunit-next 2/2] kunit: add support for named resources" from Alan > > Maguire [1] > > - A named resource is added to a test when a KASAN report is > > expected > > - This resource contains a struct for kasan_data containing > > booleans representing if a KASAN report is expected and if a > > KASAN report is found > > > > [1] (https://lore.kernel.org/linux-kselftest/1583251361-12748-1-git-send-email-alan.maguire@oracle.com/T/#t) > > > > Signed-off-by: Patricia Alfonso > > --- > > include/kunit/test.h | 10 ++++++++++ > > lib/kunit/test.c | 10 +++++++++- > > lib/test_kasan.c | 37 +++++++++++++++++++++++++++++++++++++ > > mm/kasan/report.c | 33 +++++++++++++++++++++++++++++++++ > > 4 files changed, 89 insertions(+), 1 deletion(-) > > > > diff --git a/include/kunit/test.h b/include/kunit/test.h > > index 70ee581b19cd..2ab265f4f76c 100644 > > --- a/include/kunit/test.h > > +++ b/include/kunit/test.h > > @@ -19,9 +19,19 @@ > > > > struct kunit_resource; > > > > +#ifdef CONFIG_KASAN > > +/* kasan_data struct is used in KUnit tests for KASAN expected failures */ > > +struct kunit_kasan_expectation { > > + bool report_expected; > > + bool report_found; > > +}; > > +#endif /* CONFIG_KASAN */ > > + > > Above should be moved to mm/kasan/kasan.h I think. > > > 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); > > + > > Can you explain a bit more about why we need this exported? > I see where it's used but I'd just like to make sure I > understand what you're trying to do. Thanks! > I need the ability to set a KUnit test failure from outside the KUnit code so that a test that does not expect a KASAN failure, but reaches the point in the KASAN code where a report is printed will properly fail due to this found KASAN failure. > > /** > > * struct kunit_resource - represents a *test managed resource* > > * @data: for the user to store arbitrary data. > > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > > index 86a4d9ca0a45..3f927ef45827 100644 > > --- a/lib/kunit/test.c > > +++ b/lib/kunit/test.c > > @@ -10,11 +10,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); > > } > > @@ -237,6 +238,10 @@ static void kunit_try_run_case(void *data) > > struct kunit_suite *suite = ctx->suite; > > struct kunit_case *test_case = ctx->test_case; > > > > +#if (IS_ENABLED(CONFIG_KASAN) && IS_BUILTIN(CONFIG_KUNIT)) > > + current->kunit_test = test; > > +#endif /* IS_ENABLED(CONFIG_KASAN) && IS_BUILTIN(CONFIG_KUNIT) */ > > + > > /* > > * kunit_run_case_internal may encounter a fatal error; if it does, > > * abort will be called, this thread will exit, and finally the parent > > @@ -590,6 +595,9 @@ void kunit_cleanup(struct kunit *test) > > spin_unlock(&test->lock); > > kunit_remove_resource(test, res); > > } > > +#if (IS_ENABLED(CONFIG_KASAN) && IS_BUILTIN(CONFIG_KUNIT)) > > + current->kunit_test = NULL; > > As per patch 1, I'd suggest changing here and elsewhere to > "IS_ENABLED(CONFIG_KUNIT)". > "IS_ENABLED(CONFIG_KUNIT)" does not work because KASAN is built-in so it can't rely on modules so this patchset relies on KUnit being built-in. > > +#endif /* IS_ENABLED(CONFIG_KASAN) && IS_BUILTIN(CONFIG_KUNIT)*/ > > } > > EXPORT_SYMBOL_GPL(kunit_cleanup); > > > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > > index 3872d250ed2c..cf73c6bee81b 100644 > > --- a/lib/test_kasan.c > > +++ b/lib/test_kasan.c > > @@ -23,6 +23,43 @@ > > > > #include > > > > +#include > > + > > +struct kunit_resource resource; > > +struct kunit_kasan_expectation fail_data; > > + > > +#define KUNIT_SET_KASAN_DATA(test) do { \ > > + fail_data.report_expected = true; \ > > + fail_data.report_found = false; \ > > + kunit_add_named_resource(test, \ > > + NULL, \ > > + NULL, \ > > + &resource, \ > > + "kasan_data", &fail_data); \ > > +} while (0) > > + > > +#define KUNIT_DO_EXPECT_KASAN_FAIL(test, condition) do { \ > > + struct kunit_resource *resource; \ > > + struct kunit_kasan_expectation *kasan_data; \ > > + condition; \ > > + resource = kunit_find_named_resource(test, "kasan_data"); \ > > + kasan_data = resource->data; \ > > + KUNIT_EXPECT_EQ(test, \ > > + kasan_data->report_expected, \ > > + kasan_data->report_found); \ > > + kunit_put_resource(resource); \ > > +} while (0) > > + > > +/** > > + * 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 { \ > > + KUNIT_SET_KASAN_DATA(test); \ > > + KUNIT_DO_EXPECT_KASAN_FAIL(test, condition); \ > > +} while (0) > > + > > /* > > * Note: test functions are marked noinline so that their names appear in > > * reports. > > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > > index 5ef9f24f566b..ef3d0f54097e 100644 > > --- a/mm/kasan/report.c > > +++ b/mm/kasan/report.c > > @@ -32,6 +32,8 @@ > > > > #include > > > > +#include > > + > > #include "kasan.h" > > #include "../slab.h" > > > > @@ -455,12 +457,38 @@ static bool report_enabled(void) > > return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); > > } > > > > +#if IS_BUILTIN(CONFIG_KUNIT) > > again we could tweak this to IS_ENABLED(CONFIG_KUNIT); BTW > the reason we can compile kunit as a module for these tests > is the KASAN tests are tristate themselves. If they were > builtin only it wouldn't be possible to build kunit as > a module. > > > +void kasan_update_kunit_status(struct kunit *cur_test) > > +{ > > + struct kunit_resource *resource; > > + struct kunit_kasan_expectation *kasan_data; > > + > > + if (kunit_find_named_resource(cur_test, "kasan_data")) { > > + resource = kunit_find_named_resource(cur_test, "kasan_data"); > > + kasan_data = resource->data; > > + kasan_data->report_found = true; > > + > > + if (!kasan_data->report_expected) > > + kunit_set_failure(current->kunit_test); > > + else > > + return; > > + } else > > + kunit_set_failure(current->kunit_test); > > +} > > +#endif /* IS_BUILTIN(CONFIG_KUNIT) */ > > + > > void kasan_report_invalid_free(void *object, unsigned long ip) > > { > > unsigned long flags; > > u8 tag = get_tag(object); > > > > object = reset_tag(object); > > + > > +#if IS_BUILTIN(CONFIG_KUNIT) > > same comment as above. > > > + if (current->kunit_test) > > + kasan_update_kunit_status(current->kunit_test); > > +#endif /* IS_BUILTIN(CONFIG_KUNIT) */ > > + > > start_report(&flags); > > pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); > > print_tags(tag, object); > > @@ -481,6 +509,11 @@ void __kasan_report(unsigned long addr, size_t size, bool is_write, unsigned lon > > if (likely(!report_enabled())) > > return; > > > > +#if IS_BUILTIN(CONFIG_KUNIT) > > here too. > > > + if (current->kunit_test) > > + kasan_update_kunit_status(current->kunit_test); > > +#endif /* IS_BUILTIN(CONFIG_KUNIT) */ > > + > > disable_trace_on_warning(); > > > > tagged_addr = (void *)addr; > > -- > > 2.25.1.696.g5e7596f4ac-goog > > > > -- Best, Patricia