From patchwork Thu Dec 22 03:51:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079351 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A90FC4332F for ; Thu, 22 Dec 2022 03:52:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234829AbiLVDwW (ORCPT ); Wed, 21 Dec 2022 22:52:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229561AbiLVDwU (ORCPT ); Wed, 21 Dec 2022 22:52:20 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D2D0186A6; Wed, 21 Dec 2022 19:52:19 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 0E28E81A14; Thu, 22 Dec 2022 03:52:14 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681139; bh=kULCxpE25ChDF6QfgE9AQS3bo2E7ihos1VsI9mS/Wmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZlzUpeLpn+jVdP9jtnmx4mHFn13ToH+RIy528nBPlj+pDkEW9I1aKV46OdGeur4Y2 ALPyOg/8i1qcytsNTzW6sFmZMn48TXq7qCgS/tFV6RAo3xIT3y4xvGgo+AYYTWtYgv S7XpVIXU/LODBMv1SEaBvn8eWfoG3X7knJQaUE0SynhkH3IL5i2LVj5CUXstzDfc6B ehkhaAFPuAgMD2qyoEJO25qZG7ZVg5Zko+Tku+tl1J1fij8xadukBjAQKK8jxpql3o UnYEJoBsPa2GOK3+6WXkPUq3qPsvPndaUml7jVYUapTcw6RtgIybno8AojlUJYb/Fx ACr1p4G5G4CRw== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 1/8] nolibc/sys: Implement `sigaction(2)` function Date: Thu, 22 Dec 2022 10:51:27 +0700 Message-Id: <20221222035134.3467659-2-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi This commit adds the initial implementation of nolibc `sigaction()` function. Currently, this implementation is only available on the x86-64 arch. `sigaction()` needs an architecture-dependent "signal trampoline" function that invokes the __rt_sigreturn syscall to resume the process after a signal gets handled. On Linux x86-64, the "signal trampoline" function has to be written in inline Assembly to prevent the compiler from controlling the `%rsp` (e.g., with `-fno-omit-frame-pointer`, every function has a `pushq %rbp` that makes the `%rsp` no longer point to `struct rt_sigframe`). The "signal trampoline" function is called `__arch_restore_rt` in this implementation. Signed-off-by: Ammar Faizi --- tools/include/nolibc/arch-x86_64.h | 12 +++++ tools/include/nolibc/sys.h | 80 ++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h index 0e1e9eb8545d..b6470943e836 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -212,4 +212,16 @@ __asm__ (".section .text\n" "hlt\n" // ensure it does not return ""); +void __arch_restore_rt(void); + +__asm__ ( +".section .text\n" +"__arch_restore_rt:\n\t" + "movl $0xf, %eax\n\t" // __NR_rt_sigreturn == 0xf + "syscall\n\t" // %rsp must point to a valid struct rt_sigframe. + "int3" +); + +#define __HAVE_ARCH_RESTORE_RT + #endif // _NOLIBC_ARCH_X86_64_H diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 3db1dd8c74ee..91532a2fbe2c 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1026,6 +1026,86 @@ pid_t setsid(void) return ret; } +typedef void (*sighandler_t)(int sig); + +/* + * int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); + */ + +static __attribute__((unused)) +int sys_sigaction(int signum, const struct sigaction *act, + struct sigaction *oldact) +{ + return my_syscall4(__NR_rt_sigaction, signum, act, oldact, + sizeof(sigset_t)); +} + +static __attribute__((unused)) +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) +{ +#ifdef __HAVE_ARCH_RESTORE_RT + struct sigaction act2 = *act; + int ret; + + /* + * On Linux x86-64, libc's sigaction() always sets the + * @act->sa_restorer when the caller passes a NULL. + * + * @act->sa_restorer is an arch-specific function used + * as a "signal trampoline". + * + * @act->sa_handler is a signal handler provided by the + * user. + * + * When the handled signal is caught, the %rip jumps to + * @act->sa_handler with user stack already set by the + * kernel as below: + * + * |--------------------| + * %rsp -> | act->sa_restorer | (return address) + * |--------------------| + * | struct rt_sigframe | (process context info) + * | | + * | | + * .................... + * + * Once this signal handler executes the "ret" instruction, + * the %rip jumps to @act->sa_restorer. The sa_restorer + * function has to invoke the __rt_sigreturn syscall with + * %rsp pointing to the `struct rt_sigframe` that the kernel + * constructed previously to resume the process. + * + * The "signal trampoline" function has to be written in + * inline Assembly to prevent the compiler from controlling + * the %rsp (e.g., with -fno-omit-frame-pointer, every + * function has a `pushq %rbp` that makes the %rsp no longer + * point to `struct rt_sigframe`). + * + * `struct rt_sigframe` contains the registers' value before + * the signal is caught. + * + */ + if (!act2.sa_restorer) { + act2.sa_flags |= SA_RESTORER; + act2.sa_restorer = __arch_restore_rt; + } + + ret = sys_sigaction(signum, &act2, oldact); + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +#else + /* + * TODO: Implement sa_restorer ("signal trampoline") for + * other architectures. + */ + SET_ERRNO(ENOSYS); + return -1; +#endif +} + /* * int stat(const char *path, struct stat *buf); From patchwork Thu Dec 22 03:51:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079352 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAFE9C4332F for ; Thu, 22 Dec 2022 03:52:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234883AbiLVDw0 (ORCPT ); Wed, 21 Dec 2022 22:52:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234880AbiLVDwY (ORCPT ); Wed, 21 Dec 2022 22:52:24 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 641B4186A6; Wed, 21 Dec 2022 19:52:24 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id DBEC181A18; Thu, 22 Dec 2022 03:52:19 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681144; bh=VUWgZx5aCGOd+rMsLp7+4gysuZVXd6vPkv96NwcpuWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qtf2fx3CmLBFG2dkzGK7t/5NNmia4RS1/LNt9kPbJxG/kh+CHXxF4xfS+R1JP5zjo T2ownYIeIUoiSPXCY+95CalZU5s8QHXrEylRROILdjmjt5ApwCuhmaaiTDowobCGn5 22yHxF9gdcDoYephNcVsv02j8mNYGt9BlcL253JdgFHT+dVvOSPLct2G+VfMH6n0xi 9Dthi66U2qU3ioEx/bIEIAHvQ2m1uGrJrjIXjCvgQakPxLHI/f4tLGQ0jqCNv185ma 0cNrVJS0RHFzqrfcxpBSnn44vM6GHoNVqy+fTtwGhHJvd5VXOy2i/2S3PDhu5Anb2N U9jCvUdu96y8A== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 2/8] nolibc/sys: Implement `signal(2)` function Date: Thu, 22 Dec 2022 10:51:28 +0700 Message-Id: <20221222035134.3467659-3-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi signal() function is the simpler version of sigaction(). Unlike sigaction(), which fully controls the `struct sigaction`, the caller only cares about the sa_handler when calling the signal() function. signal() internally calls sigaction(). This implementation is currently only available on the x86-64 arch. Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 91532a2fbe2c..ca348939eb50 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1106,6 +1106,36 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) #endif } +/* + * sighandler_t signal(int signum, sighandler_t handler); + */ + +static __attribute__((unused)) +sighandler_t signal(int signum, sighandler_t handler) +{ +#ifdef __HAVE_ARCH_RESTORE_RT + const struct sigaction act = { + .sa_handler = handler, + .sa_flags = SA_RESTORER, + .sa_restorer = __arch_restore_rt + }; + sighandler_t old_sah; + struct sigaction old; + int ret; + + ret = sys_sigaction(signum, &act, &old); + if (ret < 0) { + SET_ERRNO(-ret); + old_sah = SIG_ERR; + } else { + old_sah = old.sa_handler; + } + return old_sah; +#else + SET_ERRNO(ENOSYS); + return SIG_ERR; +#endif +} /* * int stat(const char *path, struct stat *buf); From patchwork Thu Dec 22 03:51:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079353 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC334C4332F for ; Thu, 22 Dec 2022 03:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234908AbiLVDwg (ORCPT ); Wed, 21 Dec 2022 22:52:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234911AbiLVDwb (ORCPT ); Wed, 21 Dec 2022 22:52:31 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3972E1FCFA; Wed, 21 Dec 2022 19:52:29 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id C4CD981A1C; Thu, 22 Dec 2022 03:52:24 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681149; bh=0oAsj4hR6tGtSZQUyeczrzwt8e+u+XyWFzo2fs4a44U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nad1zX656AwgsE9Fq0uKy0Jt4X/EVIrEq18oxqubn4ZO+VqLqmQ7w2HmWh1bZmEpj FiDuMJWuOszZNMEblqzx7t2B+yizrGJLNYWsMnol/ZpWqOkWsVkI9WXNRC5V+EJ0ci VKjMCb8+ZAqPEdhTFTwGCLFyhmmAuVYQmXEDQYWEPNsZUtrYYt+vs5TbZFcs995ocL t2rAUIoVjJta2v2PC+siE4BB1nJzZgoXwR645VTwm5h8gIJSWQrZyXo7NOlOzYq7J2 Agqcofl741CQUeP9uPxFb0HNV5UVRl4Qv2i0rKUCM1/n+C7JP87Vg1R9RHNPxwRPal m38dmxGmBnRaQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 3/8] nolibc/sys: Implement `getpagesize(2)` function Date: Thu, 22 Dec 2022 10:51:29 +0700 Message-Id: <20221222035134.3467659-4-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi This commit adds getpagesize() function implementation. The getpagesize() syscall doesn't always exist on the Linux syscall table. Only specific architectures have this syscall. Implementation detail: Some architectures use a fixed page size, like x86. We can hard-code the page size value on such architectures. Some other architectures may use different page sizes. For example, Linux aarch64 supports three values of page size: 4K, 16K, and 64K which are selected at kernel compilation time. The kernel stores the used page size in the auxiliary vector. The auxiliary vector can be obtained from /proc/self/auxv at AT_PAGESZ key-value-pair. /proc/self/auxv is available on all architectures. Once we obtain the page size info, cache the value in a static variable to avoid traversing the auxiliary vector again in the next getpagesize() call. The page size should never change during kernel uptime. Link: https://lwn.net/Articles/519085 Link: https://github.com/torvalds/linux/blob/v6.1/fs/binfmt_elf.c#L260 Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 114 +++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index ca348939eb50..e9e3640c36e1 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "arch.h" #include "errno.h" @@ -407,6 +408,119 @@ int getdents64(int fd, struct linux_dirent64 *dirp, int count) return ret; } +/* + * The getpagesize() syscall doesn't always exist on the Linux syscall + * table. Only specific architectures have this syscall. + * + * Implementation detail: + * Some architectures use a fixed page size, like x86. We can hard-code + * the page size value on such architectures. + * + * Some other architectures may use different page sizes. For example, + * Linux aarch64 supports three values of page size: 4K, 16K, and 64K + * which are selected at kernel compilation time. The kernel stores the + * used page size in the auxiliary vector. The auxiliary vector can be + * obtained from /proc/self/auxv at AT_PAGESZ key-val-pair. + * /proc/self/auxv is available on all architectures. + * + * Once we obtain the page size info, cache the value in a static + * variable to avoid traversing the auxiliary vector again in the next + * getpagesize() call. The page size should never change during kernel + * uptime. + * + * Link: https://lwn.net/Articles/519085 + * Link: https://github.com/torvalds/linux/blob/v6.1/fs/binfmt_elf.c#L260 + * + * + * long getpagesize(void); + * + */ + +#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) +__attribute__((unused)) +static inline long getpagesize(void) +{ + /* + * x86 family is always 4K page. Don't bother + * reading the auxiliary vector. + */ + return 4096; +} +#else +static int sys_open(const char *path, int flags, mode_t mode); +static ssize_t sys_read(int fd, void *buf, size_t count); + +/* + * This function works for all architectures. + */ +static long sys_getpagesize(void) +{ + uint64_t buf[2] = {0, 0}; + long ret; + int fd; + + + fd = sys_open("/proc/self/auxv", O_RDONLY, 0); + if (fd < 0) + return fd; + + while (1) { + ssize_t x; + + x = sys_read(fd, buf, sizeof(buf)); + if (x < 0) { + ret = x; + break; + } + + if (__builtin_expect(x == 0, 0)) { + /* + * We've reached the end of the auxiliary + * vector, but can't find the AT_PAGESZ + * entry. + */ + ret = -ENOENT; + break; + } + + /* + * buf[0] is the key. + * buf[1] is the value. + */ + if (buf[0] == AT_PAGESZ) { + ret = buf[1]; + break; + } + } + + sys_close(fd); + return ret; +} + +__attribute__((unused)) +static long getpagesize(void) +{ + static long cached; + long ret; + + /* + * No need to read the auxv for the second + * getpagesize() call. + */ + if (__builtin_expect(cached != 0, 1)) + return cached; + + ret = sys_getpagesize(); + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } else { + cached = ret; + } + return ret; +} +#endif + /* * pid_t getpgid(pid_t pid); From patchwork Thu Dec 22 03:51:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079354 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B7A4C001B2 for ; Thu, 22 Dec 2022 03:52:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234933AbiLVDwg (ORCPT ); Wed, 21 Dec 2022 22:52:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234932AbiLVDwf (ORCPT ); Wed, 21 Dec 2022 22:52:35 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12AF723329; Wed, 21 Dec 2022 19:52:34 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 9D18F81A20; Thu, 22 Dec 2022 03:52:29 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681153; bh=dAu4u8hilpZPllAVwu00C5OS/7zywFlN7MRAKmmU8vY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=feCVYsbUUPipmlfjwXpY+Bispi3vMoCpuOKrfMEolGAzEiNO+8X0jywpx1358j0uD Vf46EbF7dN9cXkvscwqZwrFzkGnV9KUHcM5gUgfU4VY+VKTWJtEa7mE+8N2yvv6itn VLsJvT077b6ewF/6H+NfU8vEqorWBe2iv1NpPncxOjtwIwvDB2FVPn7Mizhi0yZHyn SM9MM0N5h5CbAl+JtmCeJQDnA6sAjMkzijfsL/VXsSWvACwAkWGYSdt61c/75sT3LQ cC/ACddnjE0EI85M/8jEJdLmhNw7lDOJnR8sgP+xHzY7y8YyPOyFZuBpL9oQ7v5cm1 sMou4sZAbNuOA== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 4/8] selftests/nolibc: Add `-Wall` and `-Wno-unsed-function` to the CFLAGS Date: Thu, 22 Dec 2022 10:51:30 +0700 Message-Id: <20221222035134.3467659-5-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Turn on more compiler warnings to help spot issues. Skip unused function warning as it's inevitable in the nolibc test suite. While adding these flags, remove unused variables in run_stdlib() warned by the `-Wall` flag. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/Makefile | 2 +- tools/testing/selftests/nolibc/nolibc-test.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile index 572fe72df68d..f6602a2c2bbd 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -66,7 +66,7 @@ else Q=@ endif -CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables +CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -Wall -Wno-unused-function LDFLAGS := -s help: diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index f14f5076fb6d..8d69f8a0f35a 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -542,9 +542,7 @@ int run_syscall(int min, int max) int run_stdlib(int min, int max) { int test; - int tmp; int ret = 0; - void *p1, *p2; for (test = min; test >= 0 && test <= max; test++) { int llen = 0; // line length From patchwork Thu Dec 22 03:51:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 395D5C4332F for ; Thu, 22 Dec 2022 03:52:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234949AbiLVDwn (ORCPT ); Wed, 21 Dec 2022 22:52:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234953AbiLVDwm (ORCPT ); Wed, 21 Dec 2022 22:52:42 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1444186A6; Wed, 21 Dec 2022 19:52:38 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 7729A81A0D; Thu, 22 Dec 2022 03:52:34 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681158; bh=7CHYFJ/fWvU3HmqUeRZ+VXRA4YRaz7Sx/PWqqaM5V34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OAr/7wyrio6E1/EaQ1M1rE1/PZVifyMfZI3ucHwy85zJYky5RY12Y+9aiE2RZw231 2feH9uuKuOWyZtteCOwapCVVLvuI4pfaxx2SkRugm8I2vXxfldLaoNWw+0XK7I1jR8 WGpOhumr90RujdFpnWDTg6xszQEaQaHGqRnSnAqZHgHESJJVTy609MpkLkvRcTjRaT 0WyC50LXJ7FYc1kIqOh+TvotowI7B1E/wMGdqKj5834q4SSpEfpOerJV+oL29Dx9f/ ZD1OA4+Qo5fMOmSUs9Uaq5Tkzt7AU8tzmBWfll7ppSBNLmC3pPG0vMs4oqmscV4dtN FRcqZfbRnochQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 5/8] selftests/nolibc: Add `fork(2)` selftest Date: Thu, 22 Dec 2022 10:51:31 +0700 Message-Id: <20221222035134.3467659-6-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Ensure the fork() function can create a child process. Also, when the child exits, the parent must be able to get the child's exit code via waitpid(). Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 8d69f8a0f35a..309cabbddeec 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -442,6 +442,50 @@ int test_getdents64(const char *dir) return ret; } +/* + * Test fork(). + * Make sure the exit code can be read from the parent process. + */ +static int test_fork_and_exit(int expected_code) +{ + int status; + int code; + pid_t ret; + pid_t p; + + p = fork(); + if (p < 0) + return p; + + if (!p) + exit(expected_code); + + do { + ret = waitpid(p, &status, 0); + if (ret < 0) + return ret; + } while (!WIFEXITED(status)); + + code = WEXITSTATUS(status); + if (code != expected_code) { + printf("test_fork_and_exit(): waitpid(): Invalid exit code: %d; expected = %d\n", code, expected_code); + return -1; + } + + return 0; +} + +static int test_fork(void) +{ + int i; + + for (i = 0; i < 255; i++) { + if (test_fork_and_exit(i)) + return -1; + } + return 0; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -494,6 +538,7 @@ int run_syscall(int min, int max) CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break; + CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break; 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; From patchwork Thu Dec 22 03:51:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079356 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5824DC001B2 for ; Thu, 22 Dec 2022 03:52:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234953AbiLVDwq (ORCPT ); Wed, 21 Dec 2022 22:52:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234961AbiLVDwo (ORCPT ); Wed, 21 Dec 2022 22:52:44 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAB8A183BB; Wed, 21 Dec 2022 19:52:43 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 51EF781A09; Thu, 22 Dec 2022 03:52:39 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681163; bh=sHkHhBasF8Ss/nuP4oociGJDOz1JciWpxGd76+s8DY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H6nFm/1gBfuoB8yKWLxfxG6Kbbby018S+O2bZItLnMcqylkQWTclWqHDUnL30A195 v5Su7gPABOFItiU1tj6bKiN0ZykO7rVAlx+bpeI2oVmXsRKKJeFD2wTTlc6HWvysjy sGqWEa4obA7Y460y3RzvxbJ424FFB6qGrkgKQfKLGEIVrlS/sLN6R/eZy/cEtaqzgF vouk9MRZpHdE88cGt++6mgYmDFUYjfYmk3hH/c2ajLVd9WoGEs020Lm57Ppank4O89 sMYE8gZzN/4btGuacDcaXmTFBvRAfd0Hgylh0OjOqr1ZLIsx9SEXGL7ZjihlxHrlhh 1UTO71M4jHsIQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 6/8] selftests/nolibc: Add `sigaction(2)` selftest Date: Thu, 22 Dec 2022 10:51:32 +0700 Message-Id: <20221222035134.3467659-7-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Test the sigaction() function implementation. Test steps: - Set a signal handler. - Then send a signal to itself using the kill() syscall. - The program has to survive and store the caught signal number in a volatile global variable. - Validate the volatile global variable value. - Restore the original signal handler. Only the x86-64 arch runs this test. Other architectures skip this test for now. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 309cabbddeec..562766e0f63c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -486,6 +486,86 @@ static int test_fork(void) return 0; } +static volatile int g_test_sig; + +static void test_sigaction_signal_handler(int sig) +{ + g_test_sig = sig; +} + +static int test_sigaction_sig(int sig) +{ + const struct sigaction new = { + .sa_handler = test_sigaction_signal_handler + }; + struct sigaction old; + int ret; + + /* + * Set the signal handler. + */ + ret = sigaction(sig, &new, &old); + if (ret) { + printf("test_sigaction_sig(%d): Failed to set a signal handler\n", sig); + return ret; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_sigaction_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_sigaction_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + ret = sigaction(sig, &old, NULL); + if (ret) { + printf("test_sigaction_sig(%d): Failed to restore the signal handler\n", sig); + return ret; + } + + return 0; +} + +static int test_sigaction(void) +{ + static const int sig_to_test[] = { + SIGINT, + SIGHUP, + SIGTERM, + SIGQUIT, + SIGSEGV + }; + size_t i; + int ret; + + for (i = 0; i < (sizeof(sig_to_test) / sizeof(sig_to_test[0])); i++) { + ret = test_sigaction_sig(sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + +static int should_test_sigaction(void) +{ +#if defined(__x86_64__) + return 1; +#else + return 0; +#endif +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -566,6 +646,7 @@ int run_syscall(int min, int max) CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; + CASE_TEST(sigaction); EXPECT_SYSZR(should_test_sigaction(), test_sigaction()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; From patchwork Thu Dec 22 03:51:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66E4FC4332F for ; Thu, 22 Dec 2022 03:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230462AbiLVDwx (ORCPT ); Wed, 21 Dec 2022 22:52:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234973AbiLVDwt (ORCPT ); Wed, 21 Dec 2022 22:52:49 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98ECF1FCFA; Wed, 21 Dec 2022 19:52:48 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 2AE9F81A0E; Thu, 22 Dec 2022 03:52:43 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681168; bh=Pbtd2HHhDQ00BHSBZ/yOcX52AXNgYKhg1gIsAxcbhSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qONtcf5VH641sO9ylZviZVjfcwZaksNMVTMBM1Pa/MzgIftb/CHu2rGiJT4S+fbYJ iddrlIvb+kOp2eEK82N0vGYGrxMHmcZStq8pSYCuG3i1EnZcyTQp1N507HS7CXNky2 OjMRQc3j0TfeSM3ServnH2owi3WXCdTfZVSi2dm3YAXMHUQk/RfcSupFwi4NAiXMqU /1AU8i80aXH58Cu3DufG4OmcIiiIVkkaXrk76Gwbhwnm9BtMCBuzFzTNVsgUiAjcAq gjy/0BM0owQXbuI8boSjgGgecyZJfpj60Qg0D9PFjU96dYdZvpTomgijhusEjsHo/B zF2Pb3mIClRsQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 7/8] selftests/nolibc: Add `signal(2)` selftest Date: Thu, 22 Dec 2022 10:51:33 +0700 Message-Id: <20221222035134.3467659-8-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Just like the sigaction() selftest, but for signal(). Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 84 +++++++++++++++++--- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 562766e0f63c..a65cc6b83779 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -488,7 +488,7 @@ static int test_fork(void) static volatile int g_test_sig; -static void test_sigaction_signal_handler(int sig) +static void test_signal_handler(int sig) { g_test_sig = sig; } @@ -496,7 +496,7 @@ static void test_sigaction_signal_handler(int sig) static int test_sigaction_sig(int sig) { const struct sigaction new = { - .sa_handler = test_sigaction_signal_handler + .sa_handler = test_signal_handler }; struct sigaction old; int ret; @@ -517,7 +517,7 @@ static int test_sigaction_sig(int sig) kill(getpid(), sig); /* - * test_sigaction_signal_handler() must set @g_test_sig to @sig. + * test_signal_handler() must set @g_test_sig to @sig. */ if (g_test_sig != sig) { printf("test_sigaction_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); @@ -536,20 +536,74 @@ static int test_sigaction_sig(int sig) return 0; } +static int test_signal_sig(int sig) +{ + sighandler_t old; + + /* + * Set the signal handler. + */ + old = signal(sig, test_signal_handler); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to set a signal handler\n", sig); + return -1; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_signal_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + old = signal(sig, old); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to restore the signal handler\n", sig); + return -1; + } + + return 0; +} + +static const int g_sig_to_test[] = { + SIGINT, + SIGHUP, + SIGTERM, + SIGQUIT, + SIGSEGV +}; + static int test_sigaction(void) { - static const int sig_to_test[] = { - SIGINT, - SIGHUP, - SIGTERM, - SIGQUIT, - SIGSEGV - }; size_t i; int ret; - for (i = 0; i < (sizeof(sig_to_test) / sizeof(sig_to_test[0])); i++) { - ret = test_sigaction_sig(sig_to_test[i]); + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_sigaction_sig(g_sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + +static int test_signal(void) +{ + size_t i; + int ret; + + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_signal_sig(g_sig_to_test[i]); if (ret) return ret; } @@ -566,6 +620,11 @@ static int should_test_sigaction(void) #endif } +static int should_test_signal(void) +{ + return should_test_sigaction(); +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -647,6 +706,7 @@ int run_syscall(int min, int max) CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; CASE_TEST(sigaction); EXPECT_SYSZR(should_test_sigaction(), test_sigaction()); break; + CASE_TEST(signal); EXPECT_SYSZR(should_test_signal(), test_signal()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; From patchwork Thu Dec 22 03:51:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13079358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 600CBC001B2 for ; Thu, 22 Dec 2022 03:52:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234973AbiLVDw4 (ORCPT ); Wed, 21 Dec 2022 22:52:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234968AbiLVDwy (ORCPT ); Wed, 21 Dec 2022 22:52:54 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7211918B24; Wed, 21 Dec 2022 19:52:53 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 0652881A11; Thu, 22 Dec 2022 03:52:48 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681173; bh=yq8GJZ+I6uJDzwFMLP2mAHZVUUL3XN13II1COgW8aFc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o4i7RoeKrTw8/sVfcITMdNGxBcpXoHEFGyTASrZyigmDqnUB2UeeQ0L8Ydl1f1otK eTNJ+/cXNHJ5NKNy69Pz25Fl3OaL6OueWiVb8AEJwNuxazEGyxm5sWouANg2TvnyH9 DsI+/pWhbWe8vAviG2ppCY87dTzC7hnHRzKvwRI3h6YQ1HHWVMxT3t8MZ+kJT1MmV0 97BfkrClE7Q6ln+pRzoKPEYnTxUtl95jGDpMaGhYeTLfhwyhaO0m8DHsIUl5S+KmZX NyBt1/W3n8FNs27j5ONGmIIbHRrcyeXsDk507wEezPdVUbfbBbQl0bsZEhKLkUqqA1 4y2EY1xQCS4FQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 8/8] selftests/nolibc: Add `getpagesize(2)` selftest Date: Thu, 22 Dec 2022 10:51:34 +0700 Message-Id: <20221222035134.3467659-9-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Test the getpagesize() function. Make sure it returns the correct value. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index a65cc6b83779..19b3ed5f2356 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -625,6 +625,36 @@ static int should_test_signal(void) return should_test_sigaction(); } +static int test_getpagesize(void) +{ + long x = getpagesize(); + int c; + + if (x < 0) + return x; + +#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) + /* + * x86 family is always 4K page. + */ + c = (x == 4096); +#elif defined(__aarch64__) + /* + * Linux aarch64 supports three values of page size: 4K, 16K, and 64K + * which are selected at kernel compilation time. + */ + c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024)); +#else + /* + * Assuming other architectures must have at + * least 4K page. + */ + c = (x >= 4096); +#endif + + return !c; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -686,6 +716,7 @@ int run_syscall(int min, int max) 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(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; 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;