Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp509061lqg; Fri, 1 Mar 2024 11:44:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUF1fpjXo6k6Ied9qFwu/2r0pZtXxu1i6VYj5A1iQBZUvTnI6T+HD4x8N7oyz1b3748szvew22sXz6Wrvf9+2uA6ym7cdZbbPgzdWohqA== X-Google-Smtp-Source: AGHT+IH3TfM3sl/+KCkuHxcwloOm3FX/4vnVPnQsYM7GNOlkQvE49QiueA3/A1YkateXVqD7dMfi X-Received: by 2002:a17:90a:d347:b0:299:6a7f:cc09 with SMTP id i7-20020a17090ad34700b002996a7fcc09mr2474688pjx.33.1709322249327; Fri, 01 Mar 2024 11:44:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709322249; cv=pass; d=google.com; s=arc-20160816; b=n83y9THYshb5oSuHyyxzgbxt29i6INix0zJmlZi6u/n+5Lp5y98lRoJSFCgc4Wlo36 hQnGC6A8Pq2E61MDYP6RQkRXjR32pwGSpQ8elgYAkBdcyvFX5er1hBFuobcNuAK58SBR PrzVQyz0LLmcBTMVESxTsnOMPrCuO3dHMIxP5vDhp1sNku9kBoY1/m54nrBHWpfFX+Xl oX9bRRHQoaRMegPCixSEFishCfva+vEywWaC6RmnM0N727EByPQxfFuBGbsNcb4FujnI 1daUEUohYUBu01KSuy53nlHq3ecWoC91iTLTmVTvChQArWiQXeykFHFYEvlmR7BOEXUT Ykqg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=p72QOz3wFRa91AkxbB/n7DdTFt5yaJX+U0fmnZedfDU=; fh=MR/wbEn34JHmT+rtzWNq5RVU/RKi8DhjSvb3ywiuLkE=; b=kS8cDYuehXdstlCd+5G7oeerW/9yv4EbDXnBMp++wWbZfMcw2NAkAi90TfAlg3KKv2 ZM8HXLVJJOZn4sNcdNoxIPqQJ6AHxUzoqYl+453D39AjIScE3EgotvlUnXJMfueoyrRS Brn3lLktsDbscPEgbHZkCtskRdEiUqaE5udNzuAROp/gbVBuHhQrp1ygmZ0xUXVNWOyU Y8yE4pUFXxJSwSOzbD2OmN/HfbCbgJU2hXT/4jzdtyQ3o2qPCvAkcxI+BULctvUiuCi2 3NR+JRnP/j532ZFFciGFBsLiIw9wYiygYj0eVu3ceiF19NMVc+CETh7aoKNc6YfT8C4o KTsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=lBJdL7UV; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-89044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89044-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ot7-20020a17090b3b4700b0029b16645a66si3388417pjb.17.2024.03.01.11.44.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 11:44:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=lBJdL7UV; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-89044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89044-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D11D8281782 for ; Fri, 1 Mar 2024 19:43:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DEDAD3F9D5; Fri, 1 Mar 2024 19:41:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="lBJdL7UV" Received: from smtp-42a8.mail.infomaniak.ch (smtp-42a8.mail.infomaniak.ch [84.16.66.168]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B68AD3F9D3; Fri, 1 Mar 2024 19:41:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.168 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709322068; cv=none; b=RAoWLOX7pSC/W+4P9VcGm8yJU21Y8B70t7fX1DKT9C9bA9AzwGwcCUq5JB9uaVzjuhUySlwsGIDujXJP8LC0Vak2MlNQ4KG04FZl3a00euiZxZVDlKSXk49meuYk0RhFMo/dQ7HDkdHVXhbpM2xJmz9kglkBHWTAQH/Bpm48uiQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709322068; c=relaxed/simple; bh=T482kcAqqG+Z/c6XdAmA7XMnvCxRkpebUumR1eSGE/Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qif9q43h0OCgEdX5A6hhQj+ReNUMLRie4GZJ3ha071v+ngFV6f91usddqeXtfJ7AC/kdtM0+1nFABOgUi0IHqyW/X1vQ8cH115fhS3EXdPSZh7KXCxbq3w7Ui3F7BqVxmRSzgnE89yhoL8MYJI8h68Yp4+wRbAAVTJloWTAtleM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b=lBJdL7UV; arc=none smtp.client-ip=84.16.66.168 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Received: from smtp-4-0001.mail.infomaniak.ch (unknown [10.7.10.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4Tmdk11ZtLzMsbwl; Fri, 1 Mar 2024 20:40:57 +0100 (CET) Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4Tmdk033PhzpcK; Fri, 1 Mar 2024 20:40:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1709322057; bh=T482kcAqqG+Z/c6XdAmA7XMnvCxRkpebUumR1eSGE/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lBJdL7UV/RN2rRGA8AtA/915szsVDR3MHD7wQr/qWA1NijKQB4Bh++Li0SyIotgM1 G0V8jFvqCawhwKpFuRKJfXFeTBKEQLyOijVQ4Yzb59pFadjq6Hsc2XuJgTYPG3Ca7E PYK9JspQNUsz1BWbkTdQKX04+Z95Sa17OYBmW+QY= From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Brendan Higgins , David Gow , Kees Cook , Rae Moar , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alan Maguire , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , James Morris , Luis Chamberlain , "Madhavan T . Venkataraman" , Marco Pagani , Paolo Bonzini , Sean Christopherson , Stephen Boyd , Thara Gopinath , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Zahra Tarkhani , kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org Subject: [PATCH v2 6/7] kunit: Print last test location on fault Date: Fri, 1 Mar 2024 20:40:36 +0100 Message-ID: <20240301194037.532117-7-mic@digikod.net> In-Reply-To: <20240301194037.532117-1-mic@digikod.net> References: <20240301194037.532117-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Infomaniak-Routing: alpha This helps identify the location of test faults. Cc: Brendan Higgins Cc: David Gow Cc: Rae Moar Cc: Shuah Khan Reviewed-by: Kees Cook Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240301194037.532117-7-mic@digikod.net --- Changes since v1: * Added Kees's Reviewed-by. --- include/kunit/test.h | 24 +++++++++++++++++++++--- lib/kunit/try-catch.c | 10 +++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index fcb4a4940ace..f3aa66eb0087 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -301,6 +301,8 @@ struct kunit { struct list_head resources; /* Protected by lock. */ char status_comment[KUNIT_STATUS_COMMENT_SIZE]; + /* Saves the last seen test. Useful to help with faults. */ + struct kunit_loc last_seen; }; static inline void kunit_set_failure(struct kunit *test) @@ -567,6 +569,15 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, #define kunit_err(test, fmt, ...) \ kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) +/* + * Must be called at the beginning of each KUNIT_*_ASSERTION(). + * Cf. KUNIT_CURRENT_LOC. + */ +#define _KUNIT_SAVE_LOC(test) do { \ + WRITE_ONCE(test->last_seen.file, __FILE__); \ + WRITE_ONCE(test->last_seen.line, __LINE__); \ +} while (0) + /** * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. * @test: The test context object. @@ -575,7 +586,7 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, * words, it does nothing and only exists for code clarity. See * KUNIT_EXPECT_TRUE() for more information. */ -#define KUNIT_SUCCEED(test) do {} while (0) +#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test) void __noreturn __kunit_abort(struct kunit *test); @@ -601,14 +612,16 @@ void __kunit_do_failed_assertion(struct kunit *test, } while (0) -#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ +#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \ + _KUNIT_SAVE_LOC(test); \ _KUNIT_FAILED(test, \ assert_type, \ kunit_fail_assert, \ kunit_fail_assert_format, \ {}, \ fmt, \ - ##__VA_ARGS__) + ##__VA_ARGS__); \ +} while (0) /** * KUNIT_FAIL() - Always causes a test to fail when evaluated. @@ -637,6 +650,7 @@ void __kunit_do_failed_assertion(struct kunit *test, fmt, \ ...) \ do { \ + _KUNIT_SAVE_LOC(test); \ if (likely(!!(condition_) == !!expected_true_)) \ break; \ \ @@ -698,6 +712,7 @@ do { \ .right_text = #right, \ }; \ \ + _KUNIT_SAVE_LOC(test); \ if (likely(__left op __right)) \ break; \ \ @@ -758,6 +773,7 @@ do { \ .right_text = #right, \ }; \ \ + _KUNIT_SAVE_LOC(test); \ if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \ break; \ \ @@ -791,6 +807,7 @@ do { \ .right_text = #right, \ }; \ \ + _KUNIT_SAVE_LOC(test); \ if (likely(__left && __right)) \ if (likely(memcmp(__left, __right, __size) op 0)) \ break; \ @@ -815,6 +832,7 @@ do { \ do { \ const typeof(ptr) __ptr = (ptr); \ \ + _KUNIT_SAVE_LOC(test); \ if (!IS_ERR_OR_NULL(__ptr)) \ break; \ \ diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c index c6ee4db0b3bd..2ec21c6918f3 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -91,9 +91,13 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) if (exit_code == -EFAULT) try_catch->try_result = 0; - else if (exit_code == -EINTR) - kunit_err(test, "try faulted\n"); - else if (exit_code == -ETIMEDOUT) + else if (exit_code == -EINTR) { + if (test->last_seen.file) + kunit_err(test, "try faulted after %s:%d\n", + test->last_seen.file, test->last_seen.line); + else + kunit_err(test, "try faulted before the first test\n"); + } else if (exit_code == -ETIMEDOUT) kunit_err(test, "try timed out\n"); else if (exit_code) kunit_err(test, "Unknown error: %d\n", exit_code); -- 2.44.0