From owner-svn-src-all@freebsd.org Wed Jan 29 05:25:20 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E0C0D1C1392; Wed, 29 Jan 2020 05:25:20 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 486sPr5fJJz48br; Wed, 29 Jan 2020 05:25:20 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BB7557231; Wed, 29 Jan 2020 05:25:20 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 00T5PKfe017114; Wed, 29 Jan 2020 05:25:20 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 00T5PKKh017113; Wed, 29 Jan 2020 05:25:20 GMT (envelope-from cem@FreeBSD.org) Message-Id: <202001290525.00T5PKKh017113@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Wed, 29 Jan 2020 05:25:20 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r357246 - head/tests/sys/sys X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/tests/sys/sys X-SVN-Commit-Revision: 357246 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jan 2020 05:25:20 -0000 Author: cem Date: Wed Jan 29 05:25:20 2020 New Revision: 357246 URL: https://svnweb.freebsd.org/changeset/base/357246 Log: qmath(3) test: Replace overcomplicated abomination with arc4random(3) The horrific GENRAND construction bent over backwards to construct 64-bit signed integers from the 31-bit output of random(3) for about 20 numbers per test. Reproducibility wasn't a goal: random(3) was seeded with srandomdev(3). Speed is not a factor for generating 20 integers with arc4random(3). Range is not a factor: all uses did not bound the range beyond that of the full [INT64_MIN, INT64_MAX]. Just use arc4random(3). Reported by: Coverity CIDs: 1404809, 1404817, 1404838, 1404840 and about 6x other identical reports of dubious code relating to the construction Modified: head/tests/sys/sys/qmath_test.c Modified: head/tests/sys/sys/qmath_test.c ============================================================================== --- head/tests/sys/sys/qmath_test.c Wed Jan 29 04:42:46 2020 (r357245) +++ head/tests/sys/sys/qmath_test.c Wed Jan 29 05:25:20 2020 (r357246) @@ -47,25 +47,7 @@ #define QTEST_QITRUNC(q, iv) ((iv) >> Q_RPSHFT(q)) #define QTEST_FFACTOR 32.0 -#define bitsperrand 31 -#define GENRAND(a, lb, ub) \ -({ \ - int _rembits; \ - do { \ - _rembits = Q_BITSPERBASEUP(ub) + Q_LTZ(lb); \ - *(a) = (__typeof(*(a)))0; \ - while (_rembits > 0) { \ - *(a) |= (((uint64_t)random()) & \ - ((1ULL << (_rembits > bitsperrand ? \ - bitsperrand : _rembits)) - 1)); \ - *(a) <<= (_rembits - (_rembits > bitsperrand ? \ - bitsperrand : _rembits)); \ - _rembits -= bitsperrand; \ - } \ - *(a) += lb; \ - } while (*(a) < (lb) || (uint64_t)*(a) > (ub)); \ - *(a); \ -}) +#define GENRAND(a) arc4random_buf((a), sizeof(*(a))) /* * Smoke tests for basic qmath operations, such as initialization @@ -213,11 +195,9 @@ ATF_TC_BODY(qmulq_s64q, tc) #endif int error; - srandomdev(); - for (int i = 0; i < 10;) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXX: We cheat a bit, to stand any chance of multiplying @@ -278,12 +258,9 @@ ATF_TC_BODY(qdivq_s64q, tc) if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false)) atf_tc_skip("https://bugs.freebsd.org/240219"); - - srandomdev(); - for (int i = 0; i < 10; i++) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -324,11 +301,9 @@ ATF_TC_BODY(qaddq_s64q, tc) double a_dbl, b_dbl, r_dbl, maxe_dbl, delta_dbl; int error; - srandomdev(); - for (int i = 0; i < 10;) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -372,11 +347,9 @@ ATF_TC_BODY(qsubq_s64q, tc) double a_dbl, b_dbl, r_dbl, maxe_dbl, delta_dbl; int error; - srandomdev(); - for (int i = 0; i < 10; i++) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -418,11 +391,9 @@ ATF_TC_BODY(qfraci_s64q, tc) int64_t a_int, b_int; int error; - srandomdev(); - for (int i = 0; i < 10;) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -465,11 +436,9 @@ ATF_TC_BODY(qmuli_s64q, tc) int64_t a_int, b_int; int error; - srandomdev(); - for (int i = 0; i < 10;) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -512,11 +481,9 @@ ATF_TC_BODY(qaddi_s64q, tc) int64_t a_int, b_int; int error; - srandomdev(); - for (int i = 0; i < 10;) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision. @@ -563,11 +530,9 @@ ATF_TC_BODY(qsubi_s64q, tc) int64_t a_int, b_int; int error; - srandomdev(); - for (int i = 0; i < 10; i++) { - GENRAND(&a_s64q, INT64_MIN, UINT64_MAX); - GENRAND(&b_s64q, INT64_MIN, UINT64_MAX); + GENRAND(&a_s64q); + GENRAND(&b_s64q); /* * XXXLAS: Until Qmath handles precision normalisation, only * test with equal precision.