From patchwork Thu Feb 29 17:04:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 208430 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp543562dyb; Thu, 29 Feb 2024 09:07:57 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX39vP9cfLsTS1j5QSpEeTjrfGOP+PEe5AuRMX0KASF4GHNeU2jEBhHWNjG0h7XLYtUyR6QIW5k03qCm8r91W94Rh3q1Q== X-Google-Smtp-Source: AGHT+IHsHQfhp20wla4Tfx8sX2OOe9jrE945J6beIXHiDh0AkP2CpzjLB2uh0sP9STtoXm7sFsEy X-Received: by 2002:a05:6402:b59:b0:566:ab45:e6cb with SMTP id bx25-20020a0564020b5900b00566ab45e6cbmr1429119edb.30.1709226477201; Thu, 29 Feb 2024 09:07:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709226477; cv=pass; d=google.com; s=arc-20160816; b=n+oqjP5iliq+AXUQVOLWMJDSZm5ayP9XSu/CoS3e8YhH058nTGPEoor3SxG1rEHTNT SXQGNYs+zJlaMk5RqwdTR2r6/t+QvnLQvq+dMiomhAbJydBJNl7NP1ccyTKOJxRo4xAZ IWTwxFsKyxQ4wPBHZ4DBzGYcD6TAskqXRK2UrZVLUWb/ysWm44F0A4FYUvonC+H6nh2A v1qcu9Qgc09vGUSidgWiVxELzxcibDVN3iqzPqmn0mMSALAtP5/HXHB7NK+i3wOmLOOv ClF9d8ayy8fgLFJfS6kCqSYKX99MawjwZAe1MVyAXPJMC+VuApdYH5xSd8EVJDDnqZXX t33g== 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=o9O9qQC29UPSZFUuzxPJ1cmY6pHw8tBcMxt7wS0gks8=; fh=MR/wbEn34JHmT+rtzWNq5RVU/RKi8DhjSvb3ywiuLkE=; b=w7z15RuqYHrvphf68yKe4//hdDMAiq+pprwi8ErYJHCfIk8FcRstDZ/X1MyHiUd84T FgooZoYm3/8kXa3pZh0fNuxErSbgSNe6mfh1kWoppeLhmgxfFIXNNfIRjf/q2G5GZnCS z46Uqpbpsn4icaHbn7pyf2POgEJa/briPIReUMbiLhCOIvnnnu0zGgsc8wPwETvgzNVe 8Rcr4+C1mN4P2Y0kO6wr5EJQs2lRFNAsVYI8GbX8WXztVOsApFKVhpA0DHf9H1TeoTnS AyAWva6ph2i6/qpAEENj4DJz2/CA8C8wDUzOX+wuv2C73lVaD57tO+MvBwQR/AjFlqCa 9ijQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=v0ThMtvM; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-87141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87141-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e9-20020a056402190900b00565a8319313si748114edz.141.2024.02.29.09.07.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 09:07:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=v0ThMtvM; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-87141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87141-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 06E191F21D75 for ; Thu, 29 Feb 2024 17:07:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6ED5A4AEFE; Thu, 29 Feb 2024 17:04:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="v0ThMtvM" Received: from smtp-bc09.mail.infomaniak.ch (smtp-bc09.mail.infomaniak.ch [45.157.188.9]) (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 6A1C970AE1; Thu, 29 Feb 2024 17:04:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709226281; cv=none; b=NHMAm15/mklYIirJPvrt7p5Ro730urNxlZdF9yFAh2jzHjdtTCw658X5hwZ9cYVSCYvGN+X7yP35aJP3cVJn/1gP7+9oKGt7aSJ+Xyc2X/+ZinB9xRxDBPoSS8vKNs7T6XtLLkDHR3izsJRHANYrdZvjkbwBZjcigV0QuNFFgoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709226281; c=relaxed/simple; bh=NVotNwUXraMtMy/B110wUmXv9rleT0r5CtHb/0J/Dyw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qQuu+EfGpDjekWiVbyblfXK+YbsJncQZxSrae1vWETMCF+/UDx7WFHQmb76vyzUL+dMeNeeEDqIUfQVC7VyU2zW1QpdVIEcpRF0R2P+qdkhH5VKmJRF4WpANcpxbXa/5tmWUomdHYxxgssSmQECJtCr/BCVHyQHLr8Rrd7CflhA= 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=v0ThMtvM; arc=none smtp.client-ip=45.157.188.9 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-3-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4TlyHz56S6z4gr; Thu, 29 Feb 2024 18:04:31 +0100 (CET) Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4TlyHy6lfzzMpnPg; Thu, 29 Feb 2024 18:04:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1709226271; bh=NVotNwUXraMtMy/B110wUmXv9rleT0r5CtHb/0J/Dyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v0ThMtvMj3vwpEvud2gvTCdQ6gPp6BUqR0ZH1b2ga4w+aG1idRjI+PdHZvDQlNReK WSAYLTjdGaQDAiMpkzddcFFklAPbvKVr5xyFUPQTemYKqVNu8cofKAuqTxW37iWLme wzfUX2mHyq7IPKwAvyHc6yD8MU/k+i5XrxB4/pKE= From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: Brendan Higgins , David Gow , Kees Cook , Rae Moar , Shuah Khan Cc: =?utf-8?q?Micka=C3=ABl_Sala=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 v1 7/8] kunit: Print last test location on fault Date: Thu, 29 Feb 2024 18:04:08 +0100 Message-ID: <20240229170409.365386-8-mic@digikod.net> In-Reply-To: <20240229170409.365386-1-mic@digikod.net> References: <20240229170409.365386-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792253862541404909 X-GMAIL-MSGID: 1792253862541404909 This helps identify the location of test faults. Cc: Brendan Higgins Cc: David Gow Cc: Rae Moar Cc: Shuah Khan Signed-off-by: Mickaël Salaün Reviewed-by: Kees Cook --- 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);