2022-12-22 04:43:07

by Ammar Faizi

[permalink] [raw]
Subject: [RFC PATCH v1 0/8] nolibc signal handling support

From: Ammar Faizi <[email protected]>

Hi,

This series adds signal handling support to the nolibc subsystem.

1) Initial implementation of nolibc sigaction(2) 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.

2) signal(2) function.

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. When the sigaction()
function support is expanded to other architectures, this function
will automatically support those architectures. It's basically just
a sigaction() wrapper.

3) Extra nolibc updates.

Apart from the signal handling support. This series also contains
nolibc updates, they are:

- getpagesize() support.
- CFLAGS update.
- fork(2) selftest.
- sigaction(2) selftest.
- signal(2) selftest.
- getpagesize(2) selftest.

There 8 patches in this series. It has been tested on Linux x86-64 arch
and all tests OK.

$ sudo ./nolibc-test
Running test 'syscall'
...
...
66 wait_child = -1 ECHILD [OK]
67 waitpid_min = -1 ESRCH [OK]
68 waitpid_child = -1 ECHILD [OK]
69 write_badf = -1 EBADF [OK]
70 write_zero = 0 [OK]
Errors during this test: 0


Running test 'stdlib'
...
...
14 memcmp_60_20 = 64 [OK]
15 memcmp_20_e0 = -192 [OK]
16 memcmp_e0_20 = 192 [OK]
17 memcmp_80_e0 = -96 [OK]
18 memcmp_e0_80 = 96 [OK]
Errors during this test: 0

Total number of errors: 0
Exiting with status 0

$ make run -j8
Kernel: arch/x86/boot/bzImage is ready (#3)
...
82 test(s) passed.

Signed-off-by: Ammar Faizi <[email protected]>
---

It's also available in the Git repository.

The following changes since commit caf5c36025ec9395c8d7c78957b016a284812d23:

srcu: Update comment after the index flip (2022-12-21 09:01:53 -0800)

are available in the Git repository at:

https://github.com/ammarfaizi2/linux-block testing/rfc.v1.2022-12-22.nolibc

for you to fetch changes up to ac79aca684125907bfbefadfd6c6be0ccdfe8b33:

selftests/nolibc: Add `getpagesize(2)` selftest (2022-12-22 09:57:31 +0700)

----------------------------------------------------------------
Ammar Faizi (8):
nolibc/sys: Implement `sigaction(2)` function
nolibc/sys: Implement `signal(2)` function
nolibc/sys: Implement `getpagesize(2)` function
selftests/nolibc: Add `-Wall` and `-Wno-unsed-function` to the CFLAGS
selftests/nolibc: Add `fork(2)` selftest
selftests/nolibc: Add `sigaction(2)` selftest
selftests/nolibc: Add `signal(2)` selftest
selftests/nolibc: Add `getpagesize(2)` selftest

tools/include/nolibc/arch-x86_64.h | 12 +
tools/include/nolibc/sys.h | 224 +++++++++++++++++++
tools/testing/selftests/nolibc/Makefile | 2 +-
tools/testing/selftests/nolibc/nolibc-test.c | 219 +++++++++++++++++-
4 files changed, 454 insertions(+), 3 deletions(-)


base-commit: caf5c36025ec9395c8d7c78957b016a284812d23
--
Ammar Faizi


2023-01-03 04:10:10

by Willy Tarreau

[permalink] [raw]
Subject: Re: [RFC PATCH v1 0/8] nolibc signal handling support

On Tue, Jan 03, 2023 at 10:51:35AM +0700, Alviro Iskandar Setiawan wrote:
> On Thu, Dec 29, 2022 at 6:42 PM Ammar Faizi wrote:
> > On 12/28/22 8:35 PM, Willy Tarreau wrote:
> > > It gives me the correct code for x86_64 and i586. I don't know if other
> > > architectures will want to add a prologue. I tried with "naked" but it's
> > > ignored by the compiler since the function is not purely asm. Not very
> > > important but given that we already have everything to perform our calls
> > > it would make sense to stay on this. By the way, for the sake of
> > > consistency with other syscalls, I do think the function (or label if
> > > we can't do otherwise) should be called "sys_rt_sigreturn" as it just
> > > performs a syscall.
> >
> > Will call that 'sys_rt_sigreturn' in the next series.
>
> >From glibc source code says:
> GDB needs some intimate knowledge about it to recognize them as signal
> trampolines, and make backtraces through signal handlers work right.
> Important are both the names (__restore_rt) and the exact instruction
> sequence.
>
> link: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/sigaction.c;h=4e6d9cc32e1e18746726fa430d092de9a19ba6c6;hb=b4a5d26d8835d972995f0a0a2f805a8845bafa0b#l34
>
> glibc does this:
>
> " .type __" #name ",@function\n" \
> "__" #name ":\n" \
> " movq $" #syscall ", %rax\n" \
> " syscall\n" \
>
> where
>
> #name = "restore_rt"
> #syscall = __NR_rt_sigreturn
>
> I think it should be called "__restore_rt" instead of "sys_rt_sigreturn"?
> glibc also has unwind information, but we probably don't need to care
> with that much

OK, I wasn't aware of this. Of course, if there are some strict rules
for this, let's follow them!

Thanks,
Willy

2023-01-03 04:16:51

by Ammar Faizi

[permalink] [raw]
Subject: Re: [RFC PATCH v1 0/8] nolibc signal handling support

On 1/3/23 10:54 AM, Willy Tarreau wrote:
> On Tue, Jan 03, 2023 at 10:51:35AM +0700, Alviro Iskandar Setiawan wrote:
>> GDB needs some intimate knowledge about it to recognize them as signal
>> trampolines, and make backtraces through signal handlers work right.
>> Important are both the names (__restore_rt) and the exact instruction
>> sequence.

Will follow it, thanks!

--
Ammar Faizi

2023-01-08 13:26:56

by Ammar Faizi

[permalink] [raw]
Subject: [PATCH v1 0/3] nolibc auxiliary vector retrieval support

From: Ammar Faizi <[email protected]>

Hi Willy,

This series is a follow up of our previous discussion about getauxval()
and getpagesize() functions.

It will apply cleanly on top of your "20221227-nolibc-weak-4" branch.
Base commit: b6887ec8b0b0 ("tools/nolibc: add auxiliary vector
retrieval for mips").

I have added a selftest for the getpagesize() function, but I am not
sure how to assert the correctness of getauxval(). I think it is fine
not to add a selftest for getauxval(). If you think we should, please
give some advice on the test mechanism.

Thanks!

Signed-off-by: Ammar Faizi <[email protected]>
---

Ammar Faizi (3):
nolibc/stdlib: Implement `getauxval(3)` function
nolibc/sys: Implement `getpagesize(2)` function
selftests/nolibc: Add `getpagesize(2)` selftest

tools/include/nolibc/stdlib.h | 27 ++++++++++++++++++
tools/include/nolibc/sys.h | 21 ++++++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 30 ++++++++++++++++++++
3 files changed, 78 insertions(+)


base-commit: b6887ec8b0b0c78db414b78e329bf2ce234dedd5
--
Ammar Faizi

2023-01-08 13:27:12

by Ammar Faizi

[permalink] [raw]
Subject: [PATCH v1 2/3] nolibc/sys: Implement `getpagesize(2)` function

From: Ammar Faizi <[email protected]>

This function returns the page size used by the running kernel. The
page size value is taken from the auxiliary vector at 'AT_PAGESZ' key.

'getpagesize(2)' is assumed as a syscall becuase the manpage placement
of this function is in entry 2 ('man 2 getpagesize') despite there is
no real 'getpagesize(2)' syscall in the Linux syscall table. Define
this function in 'sys.h'.

Signed-off-by: Ammar Faizi <[email protected]>
---

Side note:
This function calls 'getauxval(3)' function that's defined in
'stdlib.h', but since most functions in 'stdlib.h' needs 'sys.h', the
'sys.h' is always included first. Therefore, we need a forward
declaration of 'getauxval(3)' in sys.h.

tools/include/nolibc/sys.h | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 3db1dd8c74ee..acf7cf438010 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -18,6 +18,7 @@
#include <linux/fs.h>
#include <linux/loop.h>
#include <linux/time.h>
+#include <linux/auxvec.h>

#include "arch.h"
#include "errno.h"
@@ -498,6 +499,26 @@ pid_t gettid(void)
return sys_gettid();
}

+static unsigned long getauxval(unsigned long key);
+
+/*
+ * long getpagesize(void);
+ */
+
+static __attribute__((unused))
+long getpagesize(void)
+{
+ long ret;
+
+ ret = getauxval(AT_PAGESZ);
+ if (!ret) {
+ SET_ERRNO(ENOENT);
+ return -1;
+ }
+
+ return ret;
+}
+

/*
* int gettimeofday(struct timeval *tv, struct timezone *tz);
--
Ammar Faizi

2023-01-08 13:48:29

by Ammar Faizi

[permalink] [raw]
Subject: [PATCH v1 3/3] selftests/nolibc: Add `getpagesize(2)` selftest

From: Ammar Faizi <[email protected]>

Test the getpagesize() function. Make sure it returns the correct
value.

Signed-off-by: Ammar Faizi <[email protected]>
---
tools/testing/selftests/nolibc/nolibc-test.c | 30 ++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 6da17612251c..3a78399f4624 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -442,6 +442,35 @@ int test_getdents64(const char *dir)
return ret;
}

+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 <min> and <max>.
* Return 0 on success, non-zero on failure.
*/
@@ -502,6 +531,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;
--
Ammar Faizi

2023-01-08 13:49:54

by Ammar Faizi

[permalink] [raw]
Subject: [PATCH v1 1/3] nolibc/stdlib: Implement `getauxval(3)` function

From: Ammar Faizi <[email protected]>

Previous commits save the address of the auxiliary vector into a global
variable @_auxv. This commit creates a new function 'getauxval()' as a
helper function to get the auxv value based on the given key.

The behavior of this function is identic with the function documented
in 'man 3 getauxval'. This function is also needed to implement
'getpagesize()' function that we will wire up in the next patches.

Signed-off-by: Ammar Faizi <[email protected]>
---
tools/include/nolibc/stdlib.h | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 92378c4b9660..cdca557c4013 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -12,6 +12,7 @@
#include "types.h"
#include "sys.h"
#include "string.h"
+#include <linux/auxvec.h>

struct nolibc_heap {
size_t len;
@@ -108,6 +109,32 @@ char *getenv(const char *name)
return _getenv(name, environ);
}

+static __attribute__((unused))
+unsigned long getauxval(unsigned long type)
+{
+ const unsigned long *auxv = _auxv;
+ unsigned long ret;
+
+ if (!auxv)
+ return 0;
+
+ while (1) {
+ if (!auxv[0] && !auxv[1]) {
+ ret = 0;
+ break;
+ }
+
+ if (auxv[0] == type) {
+ ret = auxv[1];
+ break;
+ }
+
+ auxv += 2;
+ }
+
+ return ret;
+}
+
static __attribute__((unused))
void *malloc(size_t len)
{
--
Ammar Faizi

2023-01-08 13:58:49

by Ammar Faizi

[permalink] [raw]
Subject: [PATCH v2 0/4] nolibc signal handling support

From: Ammar Faizi <[email protected]>

Hi Willy,

On top of the series titled "nolibc auxiliary vector retrieval support".
The prerequisite patches of this series are in that series.

This is v2 of nolibc signal handling support. It adds signal handling
support to the nolibc subsystem:

1) Initial implementation of nolibc sigaction(2) function.

`sigaction()` needs an architecture-dependent "signal trampoline"
function that invokes __rt_sigreturn syscall to resume the process
after a signal gets handled.

The "signal trampoline" function is called `__restore_rt` in this
implementation. The naming `__restore_rt` is important for GDB. It
also has to be given a special optimization attribute
"omit-frame-pointer" to prevent the compiler from creating a stack
frame that makes the `%rsp` value no longer points to the `struct
rt_sigframe` that the kernel constructed.


2) signal(2) function.

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().


3) More selftests.

This series also adds selftests for:
- fork(2)
- sigaction(2)
- signal(2)


Side note for __restore_rt:
This has been tested on x86-64 arch and `__restore_rt` generates the
correct code. The `__restore_rt` codegen correctness on other
architectures need to be evaluated as well. If it can't generate the
correct code, it has to be written in inline Assembly.

The current codegen for __restore_rt looks like this (gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0):

00000000004038e3 <__restore_rt>:
4038e3: endbr64
4038e7: mov $0xf,%eax
4038ec: syscall


## Changes since RFC v1:
- Separate getpagesize() series.
- Write __restore_rt function in C instead of in inline Assembly.


Signed-off-by: Ammar Faizi <[email protected]>
---

Ammar Faizi (4):
nolibc/sys: Implement `sigaction(2)` function
nolibc/sys: Implement `signal(2)` function
selftests/nolibc: Add `fork(2)` selftest
selftests/nolibc: Add `sigaction(2)` selftest

tools/include/nolibc/sys.h | 97 +++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 172 +++++++++++++++++++
2 files changed, 269 insertions(+)


base-commit: b6887ec8b0b0c78db414b78e329bf2ce234dedd5
prerequisite-patch-id: 8dd0ca8ecee1732d8f5c0b233f8231dda6ab0d22
prerequisite-patch-id: ff4c08615ebbdc1a04ce39f39f99387ee46b2b31
prerequisite-patch-id: af837a829263849331eb6d73701afd7903146055
--
Ammar Faizi