Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3209848lqp; Tue, 26 Mar 2024 02:52:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUbX3B6HIgE/1J6xyPUh9LNW0U9tBZzl/LlfXxt1S0nac/uoPgsByW4ZQm1rDH/KhcRj7H4Ru/myuwAyFjAFfFPIw9t8ENLyZo11UaDOA== X-Google-Smtp-Source: AGHT+IFh6RfT+W/6nME6poBUVgZPiSyOcd/gCqDHZksDISiQ+vNfGcA/2uM5s4epx/Jo49H7f/u4 X-Received: by 2002:a05:6808:2e93:b0:3c3:d73f:972b with SMTP id gt19-20020a0568082e9300b003c3d73f972bmr3332218oib.25.1711446742173; Tue, 26 Mar 2024 02:52:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711446742; cv=pass; d=google.com; s=arc-20160816; b=NhbFcWAoAmPEjH9bAnf4WT9VOy9XRrkCheatuqL9O4TkGgxFX+BbJZKj1S7QJvIIvp twasboA2oFmaVMHb5o4qZ+ota1Ypbo/CF7ht1JbOKtDvdQX3rq6te3W5cAqXA1eyr7Ot 4Qaef5n6ZOlmHeXXXHxVN4cIRkD8jaDSWLAGR1OOc5x0QmCxIAjNl0fL08r5dfp+28n3 rNGPJihtceG4LsP7/bfgDdh9JOzJBIK1otzs4tCvvpJ4p6xJwIPrYLLsY3WJ4YkZUipF YL/iUPEEhCMAdYGl3wjdkMbbDB+kMrum//52L3O8PbotZ02X1m4gZF9OQTjuVeK0bwXT 3A1Q== 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=il8rn8osJnLqQl2Euq+N5nsDjwO7o1nmnOpjt3aGDwY=; fh=onDSeV07aUbQUxh6ZRJssLrvo0jc0O2SovuD7/EFOxc=; b=htOElwoN2OtjHGIn5X/5tNA8MjqvmDPEXMKSAMB1f6yaU5etFqgeohdQwWdTjf2cbU UArLbdKSU0y8pMxre1yiLLPaIIPfNvyvsjHbWzQrt1ynmNuva6k8QTWmCj9E1tDKqcOQ YlW4oRsDPUWweygmeksLeJjKhxX3LJbo0JdFt8gorIFiteIzOAR6A2VBWhAEJfVbu2zT cveEkE70eaxLh1wTj5Ym+ZGWABELNyzmRblgDInuMTFoiV1/G5nJa7/GTzUAvLV1WKbM 2DKNb7k5hqQZ5efLJuVCmu/vyqxswITiHLbxx7ZW1+z09HxhE1EKaJddfytRVU5unm+6 02Mw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=SFJMlria; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-118717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118717-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id i1-20020ac85c01000000b0043130f48de1si7096068qti.233.2024.03.26.02.52.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 02:52:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=SFJMlria; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-118717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118717-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EDB391C252A0 for ; Tue, 26 Mar 2024 09:52:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1A1896BFAB; Tue, 26 Mar 2024 09:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="SFJMlria" Received: from smtp-8fa9.mail.infomaniak.ch (smtp-8fa9.mail.infomaniak.ch [83.166.143.169]) (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 A08FF5466D for ; Tue, 26 Mar 2024 09:51:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711446705; cv=none; b=EcWZurckwKYPMkPgPyHfGZueFEuDY5T02EJ8gU7Qi5o7FuieGNWxGasSVM+5A/CcTh2BndY1+eRfHQh9OBlvsdnPyBPAYmLLYU4/y2Zy347Q+SvTd3gL9DBdfsQsyHkTUIwqpURRt8AR7u6+puo7A8rdKbfIEMCPIUiOFSAB+A8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711446705; c=relaxed/simple; bh=Sr1caux620dLkN80hIUUGInJ9ZYXWuNl1zRVFNFI4jc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s6aX2BjRg1u8XUAre/8wh5yI9zMBgFHBd9yevm4T042OERyi4hiY9yeBKO++A84FPz3jvPtEUHA+q13VkKPVt6/f5I4ERUj9Xbsy2A1jz6hdJNsmxlPvmGyXWfdBzUfXxZyXzBdK5R/uzccfoNNvQqR4kNj0a0K+EjZ2R8fTglA= 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=SFJMlria; arc=none smtp.client-ip=83.166.143.169 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-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4V3lSY6gXQzcRv; Tue, 26 Mar 2024 10:51:41 +0100 (CET) Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4V3lSY1h5Dzr0; Tue, 26 Mar 2024 10:51:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1711446701; bh=Sr1caux620dLkN80hIUUGInJ9ZYXWuNl1zRVFNFI4jc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SFJMlria6c6WWYrRJJ0FcNpGUywmgumDPj0DYWlW0LkJgDrxSzp0uSpDiR/yjQ2o7 sOTXjL5Us8QjQmNodvT5I2Ma+fgEhmzZzlYwfHuF3kM9G4vt6ybt/BSnKmwIW9WkdK P7ut+4oKbJdS2QjIvjv/I/42MgDj7m/3PKG1nY90= From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Brendan Higgins , David Gow , Rae Moar , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alan Maguire , Borislav Petkov , Dave Hansen , "Eric W . Biederman" , "H . Peter Anvin" , Ingo Molnar , James Morris , Kees Cook , Luis Chamberlain , "Madhavan T . Venkataraman" , Marco Pagani , Paolo Bonzini , Sean Christopherson , Stephen Boyd , Thara Gopinath , Thomas Gleixner , Vitaly Kuznetsov , 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 v4 6/7] kunit: Print last test location on fault Date: Tue, 26 Mar 2024 10:51:17 +0100 Message-ID: <20240326095118.126696-7-mic@digikod.net> In-Reply-To: <20240326095118.126696-1-mic@digikod.net> References: <20240326095118.126696-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 with opportunistic calls to _KUNIT_SAVE_LOC(). This can be useful while writing tests or debugging them. It is possible to call KUNIT_SUCCESS() to explicit save last location. 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/20240326095118.126696-7-mic@digikod.net --- Changes since v3: * Improve the try-fault error message as suggested by David. Changes since v2: * Extend the commit message according to discussion with David. Changes since v1: * Add 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 7a3910dd78a6..fa687278ccc9 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -96,9 +96,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: last line seen %s:%d\n", + test->last_seen.file, test->last_seen.line); + else + kunit_err(test, "try faulted\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