Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1727770rwr; Thu, 20 Apr 2023 21:23:05 -0700 (PDT) X-Google-Smtp-Source: AKy350Y1DGiTmfCk8ikx+TWAQrY7krbYNsbOovKwMqBhZdx7jrPRb7Cpzy+ZcmkgcqFd/Y3OhFny X-Received: by 2002:a05:6a20:3d24:b0:f0:ae73:c58a with SMTP id y36-20020a056a203d2400b000f0ae73c58amr6189432pzi.45.1682050985209; Thu, 20 Apr 2023 21:23:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682050985; cv=none; d=google.com; s=arc-20160816; b=C0kdJphr9+NnYC5CnrsBlUdpEZD5BD79QsjuakSEEzbvMm264niKTEtarxOEUzInZV Aa8ACl4FsC0sGqKsL2rxFP5Jb+tR3i1JI86GtwaSFs0oX4dUQP9wh/BQgIodeCvCX0ao 5udumQmFH7C6YyvfUN1Rmn636ietdQgEcTm0y7GU7jWTlx6iE8Mjj5X8MYZVA1Uku+oJ GMEjSG90LQziAy/qMtNdDIfrGstCoQ9vvJZRvwv2w2VuOtNkCbj8rW+hNt0C2YW1IE4T mHTGWSK3nB8N/dTwj+O8R4AEUQWWdbnjen6IphijurqTdnwLf2NFBpWq6mrf9dl3V2+m ZTLg== 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=ZEBiF7ByWZHT8hk8CTAs7S8P0dMCig1qp9ER8e9wTTI=; b=Esgn0gB7+WgIQkdlMGKh9kYSvDoP2W9MUpmOEva2Oqyf2WNLYh8FbEIQyM3cEOiJFn Tz6bvbfTZsxCVdaKaPPcmXY6HxBCE2g+7u9+y7d/aSkCB37c8W2VjpqTnlAinJfClv0c EfmsDrM+M6zAEd3yChf/WBtbw3/+nHDSgmTTCk9PiVb9J6EsZVipvsQfB0b5w2fVptDk vXX8c9AH+jDkeAlSpfHnf2Jx+w52aPOVAM8NjpTEJ208VftH5SbALaYVO3ywUYYJDIoW 2S40XGEkK+zQYiLLlPzcDw/kEmgBS5PjHaS0AZBP2P5bso7WLt4Tgl6ynpj9KhLlcBlz yCWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=zaKGvpPP; 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 b9-20020a63d309000000b0051b8ecaffd0si3524216pgg.395.2023.04.20.21.22.51; Thu, 20 Apr 2023 21:23:05 -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=zaKGvpPP; 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 S233824AbjDUECo (ORCPT + 99 others); Fri, 21 Apr 2023 00:02:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233520AbjDUECg (ORCPT ); Fri, 21 Apr 2023 00:02:36 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35E744EEE for ; Thu, 20 Apr 2023 21:02:34 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b8f6bef3d4aso1939572276.0 for ; Thu, 20 Apr 2023 21:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682049753; x=1684641753; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZEBiF7ByWZHT8hk8CTAs7S8P0dMCig1qp9ER8e9wTTI=; b=zaKGvpPPkSpchoZHU6U0I24jBX2qIKZqPmCqLKQwxNsTvgcZmojAMcmLO77HZwoA2Q 5PUv8FWzIKAwO240MbvzUMu1QzqMgdq3d5WqERH/Hdv8lsL3xy/hdKDD63Pm3IzQqwrI gsqPoRnR/DC0HzKjOoaX2jp9LOfsnJerUmo1ZVU+dsM8uAMc8gqqZyUUTPMgBnW/t69x qcb4IqlcuqLlfka5CuirZs99gUDidt8kf3a0DXbNZHAJsStpaVx9/w9FVVBQhONmB82C GpyMdm0ZR+qTmteIe5XzfNe3lffxuOTR2xK/Y3B6tQ1v9dMVZEciYyM+grJaVeMepiOD qNkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682049753; x=1684641753; 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=ZEBiF7ByWZHT8hk8CTAs7S8P0dMCig1qp9ER8e9wTTI=; b=a+Y1WlYzcUbOrwj6APzMmRqEi70IjauUel/3oK2IAAUSGp97FxFqhwoUuctaZOPcjr k11TpqjdkfiiaOvzTAnW/xDatRMPRhbCf9fbktVB6A4LMWESKs/rlftFDvPHqf4vHXzc f2UVOGntPAcYD5tRWI0YL9TaJKEmM6fGQhpeohVaBk/TtWofv3QJogzrtLgwq0/wLwl2 y1FDJFzDcpMcbTJSoVZ61Rkyd9Js7Dpc/K3+bfChiTlEJ4Mx7T8Yet4Y/iXE1M7SuHHl fzaYr3g6FQQt+Z8Y6Jlv0fEadfSdaE+Yah736EiNIc8vWaulvspCO96eE14L2qRF1iUH N+LQ== X-Gm-Message-State: AAQBX9dELcrz7STflrf2wCZMiBnnh6/ZCQNjGe6jAHYZ9lMzNwX5LHTt VbuUfXEESByLxacnJ62lLfdpf13ytNN9Ww== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:ca12:0:b0:b96:347d:46e7 with SMTP id a18-20020a25ca12000000b00b96347d46e7mr905278ybg.1.1682049753474; Thu, 20 Apr 2023 21:02:33 -0700 (PDT) Date: Fri, 21 Apr 2023 12:02:17 +0800 In-Reply-To: <20230421040218.2156548-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421040218.2156548-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421040218.2156548-3-davidgow@google.com> Subject: [PATCH v3 3/4] 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=ham 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 --- No changes since v2: https://lore.kernel.org/linux-kselftest/20230419085426.1671703-3-davidgow@google.com/ This patch was introduced 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