Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp843107rwe; Wed, 31 Aug 2022 12:03:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR7nVw7gOlVTfj5oppBoFTOxsAOJjwWZ/wpQs0V5uXgzUgoYDNvpX2BBSfFxKnEL+Yo1/Ddg X-Received: by 2002:a17:902:eb46:b0:174:b04f:2ddc with SMTP id i6-20020a170902eb4600b00174b04f2ddcmr16954665pli.126.1661972582937; Wed, 31 Aug 2022 12:03:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661972582; cv=none; d=google.com; s=arc-20160816; b=pAUgTs4OkvKBHKXA5ykT673nj3zmbRBHFRAVRCwWtWbVccSbD9srScirJDnNPpfasZ NMnW6atIVbfDFo3/t3NLXnC9lRlw56vfVsONejmc0Qvg3d6XG8ET19cjFOVqe4FeHEqE 4FkNdw9NPI3LsEU/YjwV0fD6NM9JtwafdgRUoKmNSxF43dp+4HwtzdAjkV9PWG8YITaf V9IbczfvDMc0/OMFvW532OUYiQu6EojcV26S32I2gsaZhS5giuvThwk74anlrkJXjdzN w1jAcIPXUq+lFHdMfxd4Ax2qeZJifQZesb/P5ki/z7bExrRJnxpnywf/7GW0s5cFxW71 9Pkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OYG6fIN2bDR/DwN6UhTVrWq+0YSNbmLyVnC8HU8Gbzc=; b=HaP8c4RU5x7aFDZKunSyoKygXrvumJqi7mQRRdmKCu/FrukMoLGzgko5iRCBJGU8vB RNsihSKWo7Eiww4s14I+Ec8gBY0mwa3lMOeJ14+wv/HCPvAHmj+h4C3DAGnYBIJc/muB 6HyDygDzVvSZTrnCtb5dr3wSicfIggdnNew4cZLd/eeFeTQuH5GmMEobxjodOWJP4VZh /z/s4NKEoJ0a9S5nmbrqYwAqhY9zv75Wrg30qp8nFdT9LW13B6AddQPzuQgdpjzTcAb9 Td8n4bvQVvd86zosK/I3unJ8MQmZ1Euis2g4UmHYhXthltFFfXvzosr1JxhZNX+UO6jT 5emA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UEgb7Sbw; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l186-20020a6391c3000000b0041603077be7si5315323pge.612.2022.08.31.12.02.50; Wed, 31 Aug 2022 12:03:02 -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=@kernel.org header.s=k20201202 header.b=UEgb7Sbw; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232432AbiHaS0s (ORCPT + 99 others); Wed, 31 Aug 2022 14:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232740AbiHaSZm (ORCPT ); Wed, 31 Aug 2022 14:25:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADE87FBA56 for ; Wed, 31 Aug 2022 11:21:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A2A3B61CE8 for ; Wed, 31 Aug 2022 18:21:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98829C43150; Wed, 31 Aug 2022 18:21:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661970110; bh=Aozz16IgN8xofEXnp7WD6afvGqRXQ72TKTm8qXa/snM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UEgb7Sbw+XginjhLeDEeNxbBp2JEgr4wWdYgLImEAweIEbVk8bKNSlc+RSMX9ilns ZuHhUkKubV6fyvr+0kC4CspPkMk/ozjY4C3s4ensT3hn3UpaCYeOJtoA+Ht0pf1u4t r+my3LRjClmIeLQI5VGeQfgXquy3M1ogkdOgV0ukr35cF7A10F7Tf7L2Zp9dpwlGPV Lkn53Bzuprnkp5vru1TaSh5kYCK6ABIRF8/fDblQV/uv5cNxYM4V4OdmuI4EbeUQAW MYiPVtYh6R9dVZq9Sg1xi8jN6sVpfjvSZowwKBz1Nq1a3ppZCRJmnJUCbvXR+8JJXE XopclT0kZFzLA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id E77DC5C0B54; Wed, 31 Aug 2022 11:21:49 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: gwml@vger.gnuweeb.org, kernel-team@fb.com, w@lwt.eu, Willy Tarreau , "Paul E . McKenney" Subject: [PATCH nolibc 12/18] selftests/nolibc: support glibc as well Date: Wed, 31 Aug 2022 11:21:42 -0700 Message-Id: <20220831182148.2698489-14-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20220831182113.GA2697286@paulmck-ThinkPad-P17-Gen-1> References: <20220831182113.GA2697286@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Willy Tarreau Adding support for glibc can be useful to distinguish between bugs in nolibc and bugs in the kernel when a syscall reports an unusual value. It's not that much work and should not affect the long term maintainability of the tests. The necessary changes can essentially be summed up like this: - set _GNU_SOURCE a the top to access some definitions - many includes added when we know we don't come from nolibc (missing the stdio include guard) - disable gettid() which is not exposed by glibc - disable gettimeofday's support of bad pointers since these crash in glibc - add a simple itoa() for errorname(); strerror() is too verbose (no way to get short messages). strerrorname_np() was added in modern glibc (2.32) to do exactly this but that 's too recent to be usable as the default fallback. - use the standard ioperm() definition. May be we need to implement ioperm() in nolibc if that's useful. Signed-off-by: Willy Tarreau Signed-off-by: Paul E. McKenney --- tools/testing/selftests/nolibc/nolibc-test.c | 47 +++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 662dea691749c..78bced95ac630 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1,17 +1,41 @@ // SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + /* platform-specific include files coming from the compiler */ #include /* libc-specific include files - * The program may be built in 2 ways: + * The program may be built in 3 ways: * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined - * $(CC) -nostdlib -I/path/to/nolibc/sysroot + * $(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present + * $(CC) with default libc => NOLIBC* never defined */ #ifndef NOLIBC #include #include #include +#ifndef _NOLIBC_STDIO_H +/* standard libcs need more includes */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif #endif /* will be used by nolibc by getenv() */ @@ -23,6 +47,17 @@ struct test { int (*func)(int min, int max); // handler }; +#ifndef _NOLIBC_STDLIB_H +char *itoa(int i) +{ + static char buf[12]; + int ret; + + ret = snprintf(buf, sizeof(buf), "%d", i); + return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err"; +} +#endif + #define CASE_ERR(err) \ case err: return #err @@ -431,7 +466,9 @@ int run_syscall(int min, int max) switch (test + __LINE__ + 1) { CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; +#ifdef NOLIBC CASE_TEST(gettid); EXPECT_SYSNE(1, gettid(), -1); break; +#endif CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break; CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break; CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break; @@ -460,9 +497,11 @@ int run_syscall(int min, int max) CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; CASE_TEST(gettimeofday_null); EXPECT_SYSZR(1, gettimeofday(NULL, NULL)); break; +#ifdef NOLIBC CASE_TEST(gettimeofday_bad1); EXPECT_SYSER(1, gettimeofday((void *)1, NULL), -1, EFAULT); break; CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break; CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break; +#endif CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break; @@ -703,7 +742,11 @@ int main(int argc, char **argv, char **envp) * exit with status code 2N+1 when N is written to 0x501. We * hard-code the syscall here as it's arch-dependent. */ +#if defined(_NOLIBC_SYS_H) else if (my_syscall3(__NR_ioperm, 0x501, 1, 1) == 0) +#else + else if (ioperm(0x501, 1, 1) == 0) +#endif asm volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0)); /* if it does nothing, fall back to the regular panic */ #endif -- 2.31.1.189.g2e36527f23