Skip to contentSkip to conversation

selftests/powerpc: Add a test of sigreturn vs VDSO

PATCH Applied Michael Ellerman 4 participants 6 messages Mar 4, 2020 – Jun 17, 2021

Subsystems (2)
lore
Share & tools
lore

Michael Ellerman

author reviewer
#0 lore raw

PATCH 1/1 selftests/powerpc: Add a test of sigreturn vs VDSO

There's two different paths through the sigreturn code, depending on whether the VDSO is mapped or not. We recently discovered a bug in the unmapped case, because it's not commonly used these days.

So add a test that sends itself a signal, then moves the VDSO, takes another signal and finally unmaps the VDSO before sending itself another signal. That tests the standard signal path, the code that handles the VDSO being moved, and also the signal path in the case where the VDSO is unmapped.

---
 .../selftests/powerpc/signal/.gitignore       |   1 +
 .../testing/selftests/powerpc/signal/Makefile |   2 +-
 .../selftests/powerpc/signal/sigreturn_vdso.c | 127 ++++++++++++++++++
 3 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/powerpc/signal/sigreturn_vdso.c
tools/testing/selftests/powerpc/signal/.gitignore +1 -0
index dca5852a1546..03dafa795255 100644
@@ -1,3 +1,4 @@
 signal
 signal_tm
 sigfuz
+sigreturn_vdso
tools/testing/selftests/powerpc/signal/Makefile +1 -1
index 113838fbbe7f..63b57583e07d 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-TEST_GEN_PROGS := signal signal_tm sigfuz
+TEST_GEN_PROGS := signal signal_tm sigfuz sigreturn_vdso
 
 CFLAGS += -maltivec
 $(OUTPUT)/signal_tm: CFLAGS += -mhtm
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
2.21.1
#1 lore raw
Quoted 1 line · Michael Ellerman

+static int search_proc_maps(char *needle, unsigned long *low, unsigned long *high)

                               ^^ const?
Quoted 6 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +6 -0
+{
+	unsigned long start, end;
+	static char buf[4096];
+	char name[128];
+	FILE *f;
+	int rc = -1;

tools/testing/selftests/powerpc/signal/sigreturn_vdso.c line 42

Quoted 10 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +10 -0
+
+	f = fopen("/proc/self/maps", "r");
+	if (!f) {
+		perror("fopen");
+		return -1;
+	}
+
+	while (fgets(buf, sizeof(buf), f)) {
+		rc = sscanf(buf, "%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\n",
+			    &start, &end, name);

I suspect it doesn't matter in practice for this particular test, but since this looks like a generally useful function that could gain users in the future: does this spuriously fail if the matching line straddles a 4096-byte boundary? Maybe fscanf(3) should be used instead?

Quoted 39 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +39 -0
+		if (rc == 2)
+			continue;
+
+		if (rc != 3) {
+			printf("sscanf errored\n");
+			rc = -1;
+			break;
+		}
+
+		if (strstr(name, needle)) {
+			*low = start;
+			*high = end - 1;
+			rc = 0;
+			break;
+		}
+	}
+
+	fclose(f);
+
+	return rc;
+}
+
+static volatile sig_atomic_t took_signal = 0;
+
+static void sigusr1_handler(int sig)
+{
+	took_signal++;
+}
+
+int test_sigreturn_vdso(void)
+{
+	unsigned long low, high, size;
+	struct sigaction act;
+	char *p;
+
+	act.sa_handler = sigusr1_handler;
+	act.sa_flags = 0;
+	sigemptyset(&act.sa_mask);
+

tools/testing/selftests/powerpc/signal/sigreturn_vdso.c line 91

Quoted 10 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +10 -0
+	assert(sigaction(SIGUSR1, &act, NULL) == 0);
+
+	// Confirm the VDSO is mapped, and work out where it is
+	assert(search_proc_maps("[vdso]", &low, &high) == 0);
+	size = high - low + 1;
+	printf("VDSO is at 0x%lx-0x%lx (%lu bytes)\n", low, high, size);
+
+	kill(getpid(), SIGUSR1);
+	assert(took_signal == 1);
+	printf("Signal delivered OK with VDSO mapped\n");

I haven't looked at the test harness in detail but this should be reliable if the program is a single thread - lgtm.

#2 lore raw
Quoted 19 lines · Nathan Lynch
Quoted 1 line · Michael Ellerman

+static int search_proc_maps(char *needle, unsigned long *low, unsigned long *high)

                               ^^ const?
Quoted 13 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +13 -0
+{
+	unsigned long start, end;
+	static char buf[4096];
+	char name[128];
+	FILE *f;
+	int rc = -1;
+
+	f = fopen("/proc/self/maps", "r");
+	if (!f) {
+		perror("fopen");
+		return -1;
+	}
+
Quoted 8 lines

tools/testing/selftests/powerpc/signal/sigreturn_vdso.c line 42

Quoted 3 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +3 -0
+	while (fgets(buf, sizeof(buf), f)) {
+		rc = sscanf(buf, "%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\n",
+			    &start, &end, name);

I suspect it doesn't matter in practice for this particular test, but since this looks like a generally useful function that could gain users in the future: does this spuriously fail if the matching line straddles a 4096-byte boundary? Maybe fscanf(3) should be used instead?

Or maybe I should read the fgets man page more closely :-)

"Reading stops after an EOF or a newline."

Sorry for the noise.

author reviewer
#3 lore raw
Quoted 5 lines · Nathan Lynch
Quoted 4 lines · Nathan Lynch
Quoted 1 line · Michael Ellerman

+static int search_proc_maps(char *needle, unsigned long *low, unsigned long *high)

                               ^^ const?

Sorry I meant to do this but then forgot.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +13 -0
+{
+	unsigned long start, end;
+	static char buf[4096];
+	char name[128];
+	FILE *f;
+	int rc = -1;
+
+	f = fopen("/proc/self/maps", "r");
+	if (!f) {
+		perror("fopen");
+		return -1;
+	}
+
Quoted 8 lines

tools/testing/selftests/powerpc/signal/sigreturn_vdso.c line 42

Quoted 3 lines
tools/testing/selftests/powerpc/signal/sigreturn_vdso.c new file +3 -0
+	while (fgets(buf, sizeof(buf), f)) {
+		rc = sscanf(buf, "%lx-%lx %*c%*c%*c%*c %*x %*d:%*d %*d %127s\n",
+			    &start, &end, name);

I suspect it doesn't matter in practice for this particular test, but since this looks like a generally useful function that could gain users in the future: does this spuriously fail if the matching line straddles a 4096-byte boundary? Maybe fscanf(3) should be used instead?

Quoted 5 lines

Or maybe I should read the fgets man page more closely :-)

"Reading stops after an EOF or a newline."

Sorry for the noise.

No worries, thanks for reviewing.

cheers

#4 lore raw
In reply to Michael Ellerman
Quoted 11 lines · Michael Ellerman

There's two different paths through the sigreturn code, depending on whether the VDSO is mapped or not. We recently discovered a bug in the unmapped case, because it's not commonly used these days.

So add a test that sends itself a signal, then moves the VDSO, takes another signal and finally unmaps the VDSO before sending itself another signal. That tests the standard signal path, the code that handles the VDSO being moved, and also the signal path in the case where the VDSO is unmapped.

Applied to powerpc next.

https://git.kernel.org/powerpc/c/a0968a025c04702427a4aee2c618f451a5098cd8

cheers

#5 lore raw

Le 26/03/2020 à 13:06, Michael Ellerman a écrit :

Quoted 11 lines · Michael Ellerman
Quoted 10 lines

There's two different paths through the sigreturn code, depending on whether the VDSO is mapped or not. We recently discovered a bug in the unmapped case, because it's not commonly used these days.

So add a test that sends itself a signal, then moves the VDSO, takes another signal and finally unmaps the VDSO before sending itself another signal. That tests the standard signal path, the code that handles the VDSO being moved, and also the signal path in the case where the VDSO is unmapped.

Quoted 8 lines
Quoted 1 line

Applied to powerpc next.

https://git.kernel.org/powerpc/c/a0968a025c04702427a4aee2c618f451a5098cd8

cheers

Doesn't work anymore since the split of VDSO and VVAR.

Christophe