Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp511407rwe; Wed, 19 Apr 2023 01:57:09 -0700 (PDT) X-Google-Smtp-Source: AKy350aToJGlLcBxMjFfv32mCOqqDDkLzbg5U5rD/uv6YGkZ977ozn+RxwQaVCF2iDPWjJNyvvqk X-Received: by 2002:a05:6a20:a11d:b0:db:1b41:704 with SMTP id q29-20020a056a20a11d00b000db1b410704mr3260055pzk.16.1681894629251; Wed, 19 Apr 2023 01:57:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681894629; cv=none; d=google.com; s=arc-20160816; b=JDzyF3jKwyz5M28LmTUfY1H5o/dflKTRpXTnJ6wu/eKvsejWr5qtA9UZ91jwL7Lzq9 NKCJYxHJqc/v/ifHr7jmTycof64CJtNHTPMatCneo6MpR4qhiN2JiVNNB31KSP1Zb7fj jwdCDPxz9HsnzofcNa/f1MTyQ0EN+aopidn46POONOFGDWoLpQOw+oqvbCNi4A8HmDg0 keJkAldwk4vkbYqdtvGTyWR9Mgo9wn7OEervxNSwQL2+c5wBLo63QbHIn3mekD8P4Eai WSOheYR4bLFBbdr0e26SQDtJ35u1I2ffaqMd+3b+hZhBbCFrcMiBaW9nGyazju3UdxtG 8N/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Vsadv1gG3CDyEQYTrAe8ESn/fsRH88WCdjO7AUKZ6mI=; b=gpDGDuDiVwOJ7G+ZHs3vtD7MgXNqe8YOzffw++2dMcCPnygqUx8VgehNQXae2soTAB NbxYrXIAOL7zJBPc/ZQPawqblKwEli9VSyZ7piiDbA5LBIvFL2wIabpI+Xt0eH+UdAGp EbXog8P6FTrXn70u1TjQk8AePbjOeQvV4WjMAeAiAVgrAPzgcIHxic70TtM1bwLEQnvP mQYhNPn9jOHPMQl5GDPkt7++JZT2b4Taed3yiZHdpAsFCzvCok4M7AfthdlwauTBRkwc pML5M9xfskzCCA9EE7MgMTCScyyCb8Z/IrLxvy1Djgk92TUJ8iikbk/bvscrCHdMrFC0 6XHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=GC9NXcZv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r134-20020a632b8c000000b005096eb1dabbsi16393585pgr.716.2023.04.19.01.56.57; Wed, 19 Apr 2023 01:57:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=GC9NXcZv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S232642AbjDSIyr (ORCPT + 99 others); Wed, 19 Apr 2023 04:54:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232650AbjDSIyo (ORCPT ); Wed, 19 Apr 2023 04:54:44 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D8E87EDD for ; Wed, 19 Apr 2023 01:54:42 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e63-20020a255042000000b00b953ca0cf4eso3787001ybb.12 for ; Wed, 19 Apr 2023 01:54:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681894481; x=1684486481; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vsadv1gG3CDyEQYTrAe8ESn/fsRH88WCdjO7AUKZ6mI=; b=GC9NXcZveJJfKa28bmp/brjsU9DlnA7il8SpiO2vjXq165ktn7pY8f3a9gKPbQsYFh rDnG8lbt0KQq43V+J4LPoag8yycHse5JYdFnD8weeosCSO4NgPn8AqYkCyaOjQo43if4 jZKizq59s9pbd1YWcDuWl5ZDDxRXhnFugR8FX10BLK3Lp2MpdcR1AGFboL5FCERPVq9M 4I02vg7QmzhsO4hg289OEjfU06p+Nj4iWFx6N78jRj04NpkyeVaNsiI/kCmBfnaSWUq1 IyfTouUETKPnwJoLbNav6OfMwg0kGE0RWlia9a98mVGQAT3+y+EHbsxUjhU31y3JxkVQ rueA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681894481; x=1684486481; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vsadv1gG3CDyEQYTrAe8ESn/fsRH88WCdjO7AUKZ6mI=; b=GUiWaKLDnJLJien0oazxsHYIXdxmCxeGKCyEAROkTuCSlAVW+xUh+v6ZfiimBZLpV9 6Ub7kp0M1x7bwSzNP5643P3cMMkaUhEtimGcqSQHaYW9/31rVNb8in6rI25coXK8vsGf A2t3NHCqeTOCX2U3+MtB4LS3EAvkBfCEF1P/t/9chAv2ejX8jGSzRuja1x1LFdc6DlI8 YQpjOcklu1PYD8LLhhG8yGwLZPDK1XmM18ZHzwKSaxZIIGEJyeVG4bk3cN2UJr3+BC97 Ae7ape4Is+G5RCLBjXNSdJdlyvGRfWwuIqbj7QHvi8XYsqpdogo9wL+zwMen3mk+dXqA I6Aw== X-Gm-Message-State: AAQBX9eazlv10cQ2I2RyN6nVBhljI9HFlhdNEpeob/lS4qfb4v0oGoVW jRXoEV2GrqAkk1+1aNMhmCBoQ4DXurrncQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:9f01:0:b0:545:1d7f:acbf with SMTP id s1-20020a819f01000000b005451d7facbfmr1501533ywn.10.1681894481486; Wed, 19 Apr 2023 01:54:41 -0700 (PDT) Date: Wed, 19 Apr 2023 16:54:26 +0800 In-Reply-To: <20230419085426.1671703-1-davidgow@google.com> Mime-Version: 1.0 References: <20230419085426.1671703-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230419085426.1671703-3-davidgow@google.com> Subject: [PATCH v2 3/3] Documentation: kunit: Warn that exit functions run even if init fails From: David Gow To: Benjamin Berg , Brendan Higgins , Shuah Khan , Rae Moar , Daniel Latypov Cc: David Gow , maxime@cerno.tech, Stephen Boyd , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Sadiya Kazi Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KUnit's exit functions will run even if the corresponding init function fails. It's easy, when writing an exit function, to assume the init function succeeded, and (for example) access uninitialised memory or dereference NULL pointers. Note that this case exists and should be handled in the documentation. Suggested-by: Benjamin Berg Link: https://lore.kernel.org/linux-kselftest/a39af0400abedb2e9b31d84c37551cecc3eed0e1.camel@sipsolutions.net/ Signed-off-by: David Gow --- This patch is new in v2. --- Documentation/dev-tools/kunit/usage.rst | 12 ++++++++++-- include/kunit/test.h | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 9f720f1317d3..f6d6c9a9ff54 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -166,7 +166,12 @@ many similar tests. In order to reduce duplication in these closely related tests, most unit testing frameworks (including KUnit) provide the concept of a *test suite*. A test suite is a collection of test cases for a unit of code with optional setup and teardown functions that run before/after the whole -suite and/or every test case. For example: +suite and/or every test case. + +.. note:: + A test case will only run if it is associated with a test suite. + +For example: .. code-block:: c @@ -196,7 +201,10 @@ after everything else. ``kunit_test_suite(example_test_suite)`` registers the test suite with the KUnit test framework. .. note:: - A test case will only run if it is associated with a test suite. + The ``exit`` and ``suite_exit`` functions will run even if ``init`` or + ``suite_init`` fail. Make sure that they can handle any inconsistent + state which may result from ``init`` or ``suite_init`` encoutering errors + or exiting early. ``kunit_test_suite(...)`` is a macro which tells the linker to put the specified test suite in a special linker section so that it can be run by KUnit diff --git a/include/kunit/test.h b/include/kunit/test.h index 57b309c6ca27..3028a1a3fcad 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -168,6 +168,9 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) * test case, similar to the notion of a *test fixture* or a *test class* * in other unit testing frameworks like JUnit or Googletest. * + * Note that @exit and @suite_exit will run even if @init or @suite_init + * fail: make sure they can handle any inconsistent state which may result. + * * Every &struct kunit_case must be associated with a kunit_suite for KUnit * to run it. */ -- 2.40.0.634.g4ca3ef3211-goog