From patchwork Sun Oct 25 21:48:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11855411 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2969E61C for ; Sun, 25 Oct 2020 21:48:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0436E222EA for ; Sun, 25 Oct 2020 21:48:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="RW8QN6Bs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418970AbgJYVsu (ORCPT ); Sun, 25 Oct 2020 17:48:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2409271AbgJYVst (ORCPT ); Sun, 25 Oct 2020 17:48:49 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B486C0613CE for ; Sun, 25 Oct 2020 14:48:49 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id c15so10708382ejs.0 for ; Sun, 25 Oct 2020 14:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H5U5b0c60OiFhVBev3K4PZ1W16Gukohw1HLrLU313w4=; b=RW8QN6BsYUCXyComtBNVPU+Wuq7tGZce9IKJCqRFMeJFb4UOTB0H+p7OtuKYDOxloX SGy4VJSUpLgwmepKy5TB3bPmvKpAf0UdY80LU3u6PbEWPvsBAHGld5F1Tl6wDU1/WBZA ieEFEZbBWeNSZo246dtVlZPew5w9JoD84iN9Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=H5U5b0c60OiFhVBev3K4PZ1W16Gukohw1HLrLU313w4=; b=RvNrRdoqWD/Be2gQMBJ0fxDiKt+hn9CAxATS0maKIbggfoZ21Iqt+edSEqoX89L2c3 7kMqqcs+KKOSNVDZ7EdRkhTwGXgSlEqCR8cYoMn8Hs0gsQWEisHAz5+o1fCTMGA5Sv7n oJKXxQGcQ1r5fC0r7+DPfh8xzj8/X8K0J4i2+WejEkkmtLoVqmf0eggSsUchAIJ7wq5D wSevUZrWS0Xax8LTA2j3AvpvQ4vC1D4XDdM2yyMGPgtNNPcCX7uRqFoaVzj4nKC2Abna 9ue40RsIT/Ajr656cBEwTTfvevzPxn+HISaXvrIs0htxevbUHN4JyaF+ksnHEu7kKkoj MYVQ== X-Gm-Message-State: AOAM5321Mq5xpHgAtL5SYchmX0M27U6HGW3NGFFfhoUm4Y3psXFPKWFO cAmKGvAXYPEVzIABFWNhWR+7Lg== X-Google-Smtp-Source: ABdhPJwxybB6/16DSKWT5fFJnAy73ROXTKxIaOw0NFDYYHOGwzcOCa8wK+ww7arm7DyMpMYdOTC8YQ== X-Received: by 2002:a17:906:2987:: with SMTP id x7mr12651220eje.301.1603662527946; Sun, 25 Oct 2020 14:48:47 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:47 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 1/4] prandom.h: add *_state variant of prandom_u32_max Date: Sun, 25 Oct 2020 22:48:39 +0100 Message-Id: <20201025214842.5924-2-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org It is useful for test modules that make use of random numbers to allow the exact same series of test cases to be repeated (e.g., after fixing a bug in the code being tested). For that, the test module needs to obtain its random numbers from a private state that can be seeded by a known seed, e.g. given as a module parameter (and using a random seed when that parameter is not given). There's a few test modules I'm going to modify to follow that scheme. As preparation, add a _state variant of the existing prandom_u32_max(), and for convenience, also add a variant that produces a value in a given range. Signed-off-by: Rasmus Villemoes Reviewed-by: Petr Mladek --- include/linux/prandom.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/linux/prandom.h b/include/linux/prandom.h index aa16e6468f91e79e1f31..58ffcd56c705be34fb98 100644 --- a/include/linux/prandom.h +++ b/include/linux/prandom.h @@ -46,6 +46,35 @@ static inline u32 prandom_u32_max(u32 ep_ro) return (u32)(((u64) prandom_u32() * ep_ro) >> 32); } +/** + * prandom_u32_max_state - get pseudo-random number in internal [0, hi) + * + * Like prandom_u32_max, but use the given state structure. + * @state: pointer to state structure + * @hi: (exclusive) upper bound + * + * Exception: If @hi == 0, this returns 0. + */ +static inline u32 prandom_u32_max_state(struct rnd_state *state, u32 hi) +{ + return ((u64)prandom_u32_state(state) * hi) >> 32; +} + +/** + * prandom_u32_range_state - get pseudo-random number in internal [lo, hi) + * + * @state: pointer to state structure + * @lo: (inclusive) lower bound + * @hi: (exclusive) upper bound + * + * Exception: If @lo == @hi, this returns @lo. Results are unspecified + * for @lo > @hi. + */ +static inline u32 prandom_u32_range_state(struct rnd_state *state, u32 lo, u32 hi) +{ + return lo + prandom_u32_max_state(state, hi - lo); +} + /* * Handle minimum values for seeds */ From patchwork Sun Oct 25 21:48:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11855417 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DA1D814B7 for ; Sun, 25 Oct 2020 21:49:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BD143223BE for ; Sun, 25 Oct 2020 21:49:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="SgKOIcp7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418997AbgJYVs4 (ORCPT ); Sun, 25 Oct 2020 17:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418983AbgJYVsv (ORCPT ); Sun, 25 Oct 2020 17:48:51 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43C17C0613CE for ; Sun, 25 Oct 2020 14:48:50 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id h24so10656427ejg.9 for ; Sun, 25 Oct 2020 14:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/8eMXvsvg9BKuvbiMtTV/x1wTbSkW08nldOMRM04gVE=; b=SgKOIcp7ljjN/k7H6UCS0Y7NynkbWvZVbGPBv2tLe/0Osgbn5xPavA2c4yMo8Y/+kW 59BpfB8dySKXPrlLmTbc59XTfeHXbzopfaF6Ewe8XJsfr7c7DZSKDQREQ/Qa3ohHDYXq ECTPXGt0EIgCUk7P4IwGf/HFPNRoZSKzZVZS8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/8eMXvsvg9BKuvbiMtTV/x1wTbSkW08nldOMRM04gVE=; b=q3vYKSkEFPNqSNg/90OKECOzJMEpC6bKe/35PlgC58yeDNByWUic7K2e+3Mqb97Wah GcA1KxSZZE0EdbAcJ+0j4yuUAPRAr6LQgmuRRlp8ndwJLRcaopZx/GLIEcI53cAMEqqh bs7ZxZX1mDOyDbYiCVpZzDnE4SebE7yq7zFm4ijC7cwGMWL+yDuqbljqBLVN5qgOxAlN +5dOHjEfJfPinuJV+5XPd7j3A2gXqtKKTFNA/BvfQjBRrkmKtebV8uZzX47hjzuQnSSz xUuMq9q/L9kqg1U92qJ4ArR3uNRNb+CzLlpGA5p6Bd7EAZ4GK2xWUhGF2LxNHy5D32x7 xbkg== X-Gm-Message-State: AOAM533JUhqea3yW/aSHqKRkPecg/AbL4Uk2PyBVYrlz6ZVzJhtvH4nt YKUKR8YCmja7KVwEn6ueHybxrw== X-Google-Smtp-Source: ABdhPJzl65jjUTXLnuyUB4F723Zhjro7B1zyAUTcs9chy30IidECBGQyUpaZmpGerwwbRz0inncydA== X-Received: by 2002:a17:906:5f96:: with SMTP id a22mr12604437eju.168.1603662528956; Sun, 25 Oct 2020 14:48:48 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:48 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 2/4] kselftest_module.h: unconditionally expand the KSTM_MODULE_GLOBALS() macro Date: Sun, 25 Oct 2020 22:48:40 +0100 Message-Id: <20201025214842.5924-3-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Two out of three users of the kselftest_module.h header manually define the failed_tests/total_tests variables instead of making use of the KSTM_MODULE_GLOBALS() macro. However, instead of just replacing those definitions with an invocation of that macro, just unconditionally define them in the header file itself. A coming change will add a few more global variables, and at least one of those will be referenced from kstm_report() - however, that's not possible currently, since when the definition is postponed until the test module invokes KSTM_MODULE_GLOBALS(), the variable is not defined by the time the compiler parses kstm_report(). Signed-off-by: Rasmus Villemoes Reviewed-by: Petr Mladek --- Documentation/dev-tools/kselftest.rst | 2 -- lib/test_bitmap.c | 3 --- lib/test_printf.c | 2 -- lib/test_strscpy.c | 2 -- tools/testing/selftests/kselftest_module.h | 5 ++--- 5 files changed, 2 insertions(+), 12 deletions(-) diff --git a/Documentation/dev-tools/kselftest.rst b/Documentation/dev-tools/kselftest.rst index a901def730d95ca4c2c1..9899e86ed470ae527fdc 100644 --- a/Documentation/dev-tools/kselftest.rst +++ b/Documentation/dev-tools/kselftest.rst @@ -281,8 +281,6 @@ A bare bones test module might look like this: #include "../tools/testing/selftests/kselftest/module.h" - KSTM_MODULE_GLOBALS(); - /* * Kernel module for testing the foobinator */ diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index 4425a1dd4ef1c7d85973..02fc667a9b3d5d7de7eb 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -16,9 +16,6 @@ #include "../tools/testing/selftests/kselftest_module.h" -static unsigned total_tests __initdata; -static unsigned failed_tests __initdata; - static char pbl_buffer[PAGE_SIZE] __initdata; static const unsigned long exp1[] __initconst = { diff --git a/lib/test_printf.c b/lib/test_printf.c index 7ac87f18a10ff8209ad5..1ed4a27390cb621715ab 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -30,8 +30,6 @@ #define PAD_SIZE 16 #define FILL_CHAR '$' -static unsigned total_tests __initdata; -static unsigned failed_tests __initdata; static char *test_buffer __initdata; static char *alloced_buffer __initdata; diff --git a/lib/test_strscpy.c b/lib/test_strscpy.c index a827f94601f5d945b163..be477a52d87185ee6a01 100644 --- a/lib/test_strscpy.c +++ b/lib/test_strscpy.c @@ -10,8 +10,6 @@ * Kernel module for testing 'strscpy' family of functions. */ -KSTM_MODULE_GLOBALS(); - /* * tc() - Run a specific test case. * @src: Source string, argument to strscpy_pad() diff --git a/tools/testing/selftests/kselftest_module.h b/tools/testing/selftests/kselftest_module.h index e8eafaf0941aa716d9dc..c81c0b0c054befaf665b 100644 --- a/tools/testing/selftests/kselftest_module.h +++ b/tools/testing/selftests/kselftest_module.h @@ -9,9 +9,8 @@ * See Documentation/dev-tools/kselftest.rst for an example test module. */ -#define KSTM_MODULE_GLOBALS() \ -static unsigned int total_tests __initdata; \ -static unsigned int failed_tests __initdata +static unsigned int total_tests __initdata; +static unsigned int failed_tests __initdata; #define KSTM_CHECK_ZERO(x) do { \ total_tests++; \ From patchwork Sun Oct 25 21:48:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11855415 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 83A5D14B4 for ; Sun, 25 Oct 2020 21:49:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 64106222C2 for ; Sun, 25 Oct 2020 21:49:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="YVUcgWiM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418983AbgJYVs4 (ORCPT ); Sun, 25 Oct 2020 17:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418981AbgJYVsv (ORCPT ); Sun, 25 Oct 2020 17:48:51 -0400 Received: from mail-ed1-x541.google.com (mail-ed1-x541.google.com [IPv6:2a00:1450:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B28DC061755 for ; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) Received: by mail-ed1-x541.google.com with SMTP id v19so7384993edx.9 for ; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cCvtSrWUoPfjEX3MMgBAPpFowxLdp4ZIN2gqdndxr2k=; b=YVUcgWiMnFEVymldu6NwvFVQ/iODVY2Me/b603T8P+yLTFzayAhQ4TcXi6GVUOGlXT C5e5z4KjVtabsaTt+bW1YrqRjERzD41hasjbeCPU8ka+c9X9qSZk2k0GCEgK2/G1I24/ YpFolPYq4MNEte2aEV8C/AnTtCKsZX3uGXoBI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cCvtSrWUoPfjEX3MMgBAPpFowxLdp4ZIN2gqdndxr2k=; b=rXLHk2E55UrSbdCdLA6FH+hoxEA4qGX8DxdsOv5LdixT5QlGbMOIogWLgGr8un1FRT gRJYgmQVaeJWPeTNtrISPmrkDg/gryaSPrF5K+bEDw28hdS+GfKGAZF8SzVzmq4oJgks sLiO2Vh9ZFiohPypfxKLF1PoyaE85Y9iForyREfL+A1lNGY8q5N0QpO8VLIJI4Dcf3kI 3FXO+dlqi97Kb65mxWGrArgg7GL3rblo2gr5fBCYrfBMJ5wKv+JRAE5IBDPucJJHOgbG K8H9eP2VPl7a/Rr7IeTUHzgQ7C0Am60Tzv+jnNZqNHRUbAIOtLjcqENf+tUNTPGhu/WT ztvQ== X-Gm-Message-State: AOAM5335mqrCnXh5B3EyTAYx9xmhVCiCrA9Zr5u+mmkRIVOZ+VbaUUBh R/KBB+D/QjCTKUVYpruQRjHosg== X-Google-Smtp-Source: ABdhPJyRXxvVb1mq0cFESQTCfX7TrThR73A0MfZ4vV7HWiF8yz2HPztG24hB4iqv4YSAK8TqoiKldw== X-Received: by 2002:aa7:cd42:: with SMTP id v2mr10886769edw.191.1603662530159; Sun, 25 Oct 2020 14:48:50 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:49 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 3/4] kselftest_module.h: add struct rnd_state and seed parameter Date: Sun, 25 Oct 2020 22:48:41 +0100 Message-Id: <20201025214842.5924-4-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Some test suites make use of random numbers to increase the test coverage when the test suite gets run on different machines and increase the chance of some corner case bug being discovered - and I'm planning on extending some existing ones in that direction as well. However, should a bug be found this way, it's important that the exact same series of tests can be repeated to verify the bug is fixed. That means the random numbers must be obtained deterministically from a generator private to the test module. To avoid adding boilerplate to various test modules, put some logic into kselftest_module.h: If the module declares that it will use random numbers, add a "seed" module parameter. If not explicitly given when the module is loaded (or via kernel command line), obtain a random one. In either case, print the seed used, and repeat that information if there was at least one test failing. Signed-off-by: Rasmus Villemoes Reviewed-by: Petr Mladek --- tools/testing/selftests/kselftest_module.h | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kselftest_module.h b/tools/testing/selftests/kselftest_module.h index c81c0b0c054befaf665b..43f3ca58fcd550b8ac83 100644 --- a/tools/testing/selftests/kselftest_module.h +++ b/tools/testing/selftests/kselftest_module.h @@ -3,14 +3,31 @@ #define __KSELFTEST_MODULE_H #include +#include +#include /* * Test framework for writing test modules to be loaded by kselftest. * See Documentation/dev-tools/kselftest.rst for an example test module. */ +/* + * If the test module makes use of random numbers, define KSTM_RANDOM + * to 1 before including this header. Then a module parameter "seed" + * will be defined. If not given, a random one will be obtained. In + * either case, the used seed is reported, so the exact same series of + * tests can be repeated by loading the module with that seed + * given. + */ + +#ifndef KSTM_RANDOM +#define KSTM_RANDOM 0 +#endif + static unsigned int total_tests __initdata; static unsigned int failed_tests __initdata; +static struct rnd_state rnd_state __initdata; +static u64 seed __initdata; #define KSTM_CHECK_ZERO(x) do { \ total_tests++; \ @@ -22,11 +39,13 @@ static unsigned int failed_tests __initdata; static inline int kstm_report(unsigned int total_tests, unsigned int failed_tests) { - if (failed_tests == 0) + if (failed_tests == 0) { pr_info("all %u tests passed\n", total_tests); - else + } else { pr_warn("failed %u out of %u tests\n", failed_tests, total_tests); - + if (KSTM_RANDOM) + pr_info("random seed used was 0x%016llx\n", seed); + } return failed_tests ? -EINVAL : 0; } @@ -34,6 +53,12 @@ static inline int kstm_report(unsigned int total_tests, unsigned int failed_test static int __init __module##_init(void) \ { \ pr_info("loaded.\n"); \ + if (KSTM_RANDOM) { \ + if (!seed) \ + seed = get_random_u64(); \ + prandom_seed_state(&rnd_state, seed); \ + pr_info("random seed = 0x%016llx\n", seed); \ + } \ selftest(); \ return kstm_report(total_tests, failed_tests); \ } \ @@ -44,4 +69,8 @@ static void __exit __module##_exit(void) \ module_init(__module##_init); \ module_exit(__module##_exit) +#if KSTM_RANDOM +module_param(seed, ullong, 0444); +#endif + #endif /* __KSELFTEST_MODULE_H */ From patchwork Sun Oct 25 21:48:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11855419 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A25A061C for ; Sun, 25 Oct 2020 21:49:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 86F97222D9 for ; Sun, 25 Oct 2020 21:49:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="DO8DQqwb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418982AbgJYVtA (ORCPT ); Sun, 25 Oct 2020 17:49:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418975AbgJYVsy (ORCPT ); Sun, 25 Oct 2020 17:48:54 -0400 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90B47C0613CE for ; Sun, 25 Oct 2020 14:48:52 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id o18so7386598edq.4 for ; Sun, 25 Oct 2020 14:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FOzxSXm+DGRoQV0sCFeUY614U62H2fJhtP9E6Tri+CI=; b=DO8DQqwbYycUzHRUdNhG9dr1ti5lEGR2HO916Oncn+gYlMV1VTWMH2xiOwy0qP+bs4 PAu/zKktPCtkEtJJufdHiqxkftnY+eEyCsGMpqaXmZvr8LrLyoeupnX+ds1aevZKLlY6 SNj6kz6lI7sd9YTv88UQ1k+yHDlMIcrzo/yag= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FOzxSXm+DGRoQV0sCFeUY614U62H2fJhtP9E6Tri+CI=; b=SbgxdU1A7m3xPEIai5jeUW3KvvOs1OkQs970qlkDIR2kFK3jUigN0pky5WtGyEdMj2 IQkGed13TMwNZiSCfFyiJFJk1NUNe74P1zvI6RWnYQFdxkOXReXB9f+ctIV/AS19Kja/ pGx5388vicK9ZUY8ugFIjOtRWUqLVYhSpcpepBSuMBFvf5Z5XQf4+iAuT+1MgTbxRVmw d+tVFUaJBCO8W6uJ51Wzv0/RtgU6xMcMfqwbcssOTU1I+pRRH6IBlKpVA3lJc7bAw2nh FlTuJGK1PbWG4u6nhBpbe2ezZqutVg5/QAZQD6HLdl2OYtHkuXYviBkqn0fAfWZvxvzT xz4Q== X-Gm-Message-State: AOAM532NpUVLOr8ps82SorESW+3CNTWVR/DbMYaqx60d2jrhk9vac8i4 Ns9KcbY5PSisETCSdf/0CaBhaQ== X-Google-Smtp-Source: ABdhPJy2e5d/k6Zop8cdZM0rcepaSTsDLRCzyjrELc9c0jo8dyv11PtAaUz66tipa28fG3ixChKW9w== X-Received: by 2002:aa7:d28a:: with SMTP id w10mr13156098edq.192.1603662531235; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:50 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 4/4] lib/test_printf.c: use deterministic sequence of random numbers Date: Sun, 25 Oct 2020 22:48:42 +0100 Message-Id: <20201025214842.5924-5-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The printf test suite does each test with a few different buffer sizes to ensure vsnprintf() behaves correctly with respect to truncation and size reporting. It calls vsnprintf() with a buffer size that is guaranteed to be big enough, a buffer size of 0 to ensure that nothing gets written to the buffer, but it also calls vsnprintf() with a buffer size chosen to guarantee the output gets truncated somewhere in the middle. That buffer size is chosen randomly to increase the chance of finding some corner case bug (for example, there used to be some %p extension that would fail to produce any output if there wasn't room enough for it all, despite the requirement of producing as much as there's room for). I'm not aware of that having found anything yet, but should it happen, it's annoying not to be able to repeat the test with the same sequence of truncated lengths. For demonstration purposes, if we break one of the test cases deliberately, we still get different buffer sizes if we don't pass the seed parameter: root@(none):/# modprobe test_printf [ 15.317783] test_printf: vsnprintf(buf, 18, "%piS|%pIS", ...) wrote '127.000.000.001|1', expected '127-000.000.001|1' [ 15.323182] test_printf: failed 3 out of 388 tests [ 15.324034] test_printf: random seed used was 0x278bb9311979cc91 modprobe: ERROR: could not insert 'test_printf': Invalid argument root@(none):/# modprobe test_printf [ 13.940909] test_printf: vsnprintf(buf, 22, "%piS|%pIS", ...) wrote '127.000.000.001|127.0', expected '127-000.000.001|127.0' [ 13.944744] test_printf: failed 3 out of 388 tests [ 13.945607] test_printf: random seed used was 0x9f72eee1c9dc02e5 modprobe: ERROR: could not insert 'test_printf': Invalid argument but to repeat a specific sequence of tests, we can do root@(none):/# modprobe test_printf seed=0x9f72eee1c9dc02e5 [ 448.328685] test_printf: vsnprintf(buf, 22, "%piS|%pIS", ...) wrote '127.000.000.001|127.0', expected '127-000.000.001|127.0' [ 448.331650] test_printf: failed 3 out of 388 tests [ 448.332295] test_printf: random seed used was 0x9f72eee1c9dc02e5 modprobe: ERROR: could not insert 'test_printf': Invalid argument Signed-off-by: Rasmus Villemoes Reviewed-by: Petr Mladek --- lib/test_printf.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index 1ed4a27390cb621715ab..bbea8b807d1eafe67e01 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -24,6 +24,7 @@ #include +#define KSTM_RANDOM 1 #include "../tools/testing/selftests/kselftest_module.h" #define BUF_SIZE 256 @@ -111,8 +112,14 @@ __test(const char *expect, int elen, const char *fmt, ...) * be able to print it as expected. */ failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap); - rand = 1 + prandom_u32_max(elen+1); - /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ + rand = prandom_u32_range_state(&rnd_state, 1, elen + 1); + /* + * Except for elen == 0, we have 1 <= rand <= elen < BUF_SIZE, + * i.e., the output is guaranteed to be truncated somewhere in + * the middle, and we're not pretending the buffer to be + * larger than it really is. For the boring case of elen == 0, + * rand is 1, which is of course also <= BUF_SIZE. + */ failed_tests += do_test(rand, expect, elen, fmt, ap); failed_tests += do_test(0, expect, elen, fmt, ap);