Skip to contentSkip to conversation

getcpu: Add testcase for EFAULT

PATCH Superseded Ma Xinjian via ltp 2 participants 3 messages Aug 5, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 getcpu: Add testcase for EFAULT

Add a testcase with the arguments point to an invalid address.

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/getcpu/getcpu02.c | 97 +++++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 testcases/kernel/syscalls/getcpu/getcpu02.c
runtest/syscalls +1 -0
index b8728c1c5..1537b5022 100644
@@ -448,6 +448,7 @@ futimesat01 futimesat01
 getcontext01 getcontext01
 
 getcpu01 getcpu01
+getcpu02 getcpu02
 
 getcwd01 getcwd01
 getcwd02 getcwd02
testcases/kernel/syscalls/getcpu/getcpu02.c new file +97 -0

97 lines not on this page · raw message

Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 34 lines · Ma Xinjian via ltp

Add a testcase with the arguments point to an invalid address.

---
  runtest/syscalls                            |  1 +
  testcases/kernel/syscalls/getcpu/getcpu02.c | 97 +++++++++++++++++++++
  2 files changed, 98 insertions(+)

create mode 100644 testcases/kernel/syscalls/getcpu/getcpu02.c

runtest/syscalls +1 -0
index b8728c1c5..1537b5022 100644
@@ -448,6 +448,7 @@ futimesat01 futimesat01
  getcontext01 getcontext01
 
  getcpu01 getcpu01
+getcpu02 getcpu02
 
  getcwd01 getcwd01
  getcwd02 getcwd02
testcases/kernel/syscalls/getcpu/getcpu02.c new file +5 -0
new file mode 100644
index 000000000..f32660ef9
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2024
+ * Author: Ma Xinjian <[email protected]>

testcases/kernel/syscalls/getcpu/getcpu02.c line 15

Quoted 10 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +10 -0
+ *
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that getcpu(2) fails with
+ *
+ * - EFAULT arguments point outside the calling process's address
+ *          space.

We can squeeze the description in one single line since EFAULT is the only one we are gonna test.

testcases/kernel/syscalls/getcpu/getcpu02.c line 23

Quoted 8 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +8 -0
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <stdlib.h>

tst_test.h is importing these already.

Quoted 57 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +57 -0
+
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static void *bad_addr;
+
+static void setup(void)
+{
+	bad_addr = tst_get_bad_addr(NULL);
+}
+
+static void check_bad_cpu_id(void *bad_addr, unsigned int *node_id)
+{
+	int status;
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		TST_EXP_FAIL(getcpu(bad_addr, node_id), EFAULT);
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "getcpu() caused SIGSEGV");
+		return;
+	}
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+		return;
+
+	tst_res(TFAIL, "child %s", tst_strstatus(status));
+}
+
+static void check_bad_node_id(unsigned int *cpu_id, void *bad_addr)
+{
+	int status;
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		TST_EXP_FAIL(getcpu(cpu_id, bad_addr), EFAULT);
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "getcpu() caused SIGSEGV");
+		return;
+	}
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+		return;

testcases/kernel/syscalls/getcpu/getcpu02.c line 90

Quoted 10 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +10 -0
+
+	tst_res(TFAIL, "child %s", tst_strstatus(status));
+}
+
+static void run_test(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	check_bad_cpu_id(bad_addr, &node_id);
+	check_bad_node_id(&cpu_id, bad_addr);

Here we can use .test/.tcnt , defining 2 testcases with different getcpu() input values.

testcases/kernel/syscalls/getcpu/getcpu02.c line 97

Quoted 7 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +7 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run_test,
+	.forks_child = 1,
+};

Best regards,
Andrea Cervesato

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 39 lines

Hi!

Quoted 36 lines · Ma Xinjian via ltp

Add a testcase with the arguments point to an invalid address.

---
  runtest/syscalls                            |  1 +
  testcases/kernel/syscalls/getcpu/getcpu02.c | 97 +++++++++++++++++++++
  2 files changed, 98 insertions(+)

create mode 100644 testcases/kernel/syscalls/getcpu/getcpu02.c

runtest/syscalls +1 -0
index b8728c1c5..1537b5022 100644
@@ -448,6 +448,7 @@ futimesat01 futimesat01
  getcontext01 getcontext01
  getcpu01 getcpu01
+getcpu02 getcpu02
  getcwd01 getcwd01
  getcwd02 getcwd02
testcases/kernel/syscalls/getcpu/getcpu02.c new file +9 -0
new file mode 100644
index 000000000..f32660ef9
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2024
+ * Author: Ma Xinjian <[email protected]>
+ *
+ */
+
+/*\
Quoted 8 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 15

Quoted 6 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +6 -0
+ * [Description]
+ *
+ * Verify that getcpu(2) fails with
+ *
+ * - EFAULT arguments point outside the calling process's address
+ *          space.

We can squeeze the description in one single line since EFAULT is the only one we are gonna test.

Good point. As Andrea suggested to use .tcnt, defining 2 testcases with different getcpu() input values, these two would deserve to be described.

Also it'd be nice to add some tst_res(TINFO, ...) description in the testing function (similar to one above in /*\.

Kind regards,
Petr

Quoted 61 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 23

Quoted 8 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +8 -0
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <stdlib.h>

tst_test.h is importing these already.

Quoted 52 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +52 -0
+
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static void *bad_addr;
+
+static void setup(void)
+{
+	bad_addr = tst_get_bad_addr(NULL);
+}
+
+static void check_bad_cpu_id(void *bad_addr, unsigned int *node_id)
+{
+	int status;
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		TST_EXP_FAIL(getcpu(bad_addr, node_id), EFAULT);
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "getcpu() caused SIGSEGV");
+		return;
+	}
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+		return;
+
+	tst_res(TFAIL, "child %s", tst_strstatus(status));
+}
+
+static void check_bad_node_id(unsigned int *cpu_id, void *bad_addr)
+{
+	int status;
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		TST_EXP_FAIL(getcpu(cpu_id, bad_addr), EFAULT);
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "getcpu() caused SIGSEGV");
Quoted 8 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 58

Quoted 8 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +8 -0
+		return;
+	}
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+		return;
+
+	tst_res(TFAIL, "child %s", tst_strstatus(status));
+}

These two testing functions are nearly the same, only parameters are different. How about creating a single function and just pass a different params? Also, only bad_addr and one of the functions holding "0" as id is needed.

Quoted 10 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 90

Quoted 7 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +7 -0
+
+static void run_test(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	check_bad_cpu_id(bad_addr, &node_id);
+	check_bad_node_id(&cpu_id, bad_addr);

Here we can use .test/.tcnt , defining 2 testcases with different getcpu() input values.

+1

NOTE: sometimes we create struct tcase, but it'd be overkill for these 2 functions.

Kind regards,
Petr

Quoted 10 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 97

Quoted 7 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +7 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run_test,
+	.forks_child = 1,
+};

Best regards,
Andrea Cervesato

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp