Skip to contentSkip to conversation

selftests: add tests for clone3()

PATCH Superseded Adrian Reber 2 participants 2 messages Nov 8, 2019 – Nov 11, 2019

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 selftests: add tests for clone3()

This adds tests for clone3() with different values and sizes of struct clone_args.

This selftest was initially part of of the clone3() with PID selftest. After that patch was almost merged Eugene sent out a couple of patches to fix problems with these test.

This commit now only contains the clone3() selftest after the LPC decision to rework clone3() with PID to allow setting the PID in multiple PID namespaces including all of Eugene's patches.

---
v2:
- Applied Christian's suggestions
- Skip root-only tests when running as non-root
v3:
- Removed unnecessary test case (everything set to 1)
- Correctly handle children without SIGCHLD

---
 MAINTAINERS                               |   1 +
 tools/testing/selftests/Makefile          |   1 +
 tools/testing/selftests/clone3/.gitignore |   1 +
 tools/testing/selftests/clone3/Makefile   |   7 +
 tools/testing/selftests/clone3/clone3.c   | 203 ++++++++++++++++++++++
 5 files changed, 213 insertions(+)
 create mode 100644 tools/testing/selftests/clone3/.gitignore
 create mode 100644 tools/testing/selftests/clone3/Makefile
 create mode 100644 tools/testing/selftests/clone3/clone3.c
MAINTAINERS +1 -0
index cba1095547fd..0040b7a6410b 100644
@@ -12829,6 +12829,7 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git
 F:	samples/pidfd/
 F:	tools/testing/selftests/pidfd/
+F:	tools/testing/selftests/clone3/
 K:	(?i)pidfd
 K:	(?i)clone3
 K:	\b(clone_args|kernel_clone_args)\b
tools/testing/selftests/Makefile +1 -0
index 4cdbae6f4e61..ad442364218a 100644
@@ -4,6 +4,7 @@ TARGETS += bpf
 TARGETS += breakpoints
 TARGETS += capabilities
 TARGETS += cgroup
+TARGETS += clone3
 TARGETS += cpufreq
 TARGETS += cpu-hotplug
 TARGETS += drivers/dma-buf
tools/testing/selftests/clone3/.gitignore new file +1 -0
new file mode 100644
index 000000000000..85d9d3ba2524
@@ -0,0 +1 @@
+clone3
tools/testing/selftests/clone3/Makefile new file +7 -0
new file mode 100644
index 000000000000..ea922c014ae4
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -I../../../../usr/include/
+
+TEST_GEN_PROGS := clone3
+
+include ../lib.mk
tools/testing/selftests/clone3/clone3.c new file +203 -0

203 lines not on this page · raw message

Signature · 1 line
2.23.0
#1 lore raw
Quoted 11 lines · Adrian Reber
tools/testing/selftests/clone3/clone3.c new file +11 -0
+static void test_clone3(uint64_t flags, size_t size, int expected,
+		       enum test_mode test_mode)
+{
+	int ret;
+
+	ksft_print_msg(
+		"[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n",
+		getpid(), flags, size);
+	ret = call_clone3(flags, size, test_mode);
+	ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n",
+			getpid(), ret, expected);

tools/testing/selftests/clone3/clone3.c line 131

Quoted 10 lines
tools/testing/selftests/clone3/clone3.c new file +10 -0
+	if (ret != expected)
+		ksft_test_result_fail(
+			"[%d] Result (%d) is different than expected (%d)\n",
+			getpid(), ret, expected);
+	else
+		ksft_test_result_pass(
+			"[%d] Result (%d) matches expectation (%d)\n",
+			getpid(), ret, expected);
+}
+int main(int argc, char *argv[])

Nit: missing \n between } and int main().
I'll just fix that up myself.

tools/testing/selftests/clone3/clone3.c line 173

Quoted 4 lines
tools/testing/selftests/clone3/clone3.c new file +4 -0
+	/*
+	 * Do a clone3() with sizeof(struct clone_args) + 8
+	 * and all members set to 0. This resets exit_signal and wait()
+	 * will not get a result.

That comment is not true and now also misleading since you now pass _WALL to waitpid() above. I'll just remove it when applying.

Quoted 11 lines
tools/testing/selftests/clone3/clone3.c new file +11 -0
+	 */
+	test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
+
+	/* Do a clone3() with > page size */
+	test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
+
+	/* Do a clone3() with CLONE3_ARGS_SIZE_V0 in a new PID NS. */
+	if (uid == 0)
+		test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0, 0,
+				CLONE3_ARGS_NO_TEST);
+	else

tools/testing/selftests/clone3/clone3.c line 194

Quoted 10 lines
tools/testing/selftests/clone3/clone3.c new file +10 -0
+		ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
+
+	/* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 in a new PID NS */
+	test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL,
+			CLONE3_ARGS_NO_TEST);
+
+	/* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
+	if (uid == 0)
+		test_clone3(CLONE_NEWPID, sizeof(struct clone_args) + 8, 0,
+				CLONE3_ARGS_NO_TEST);

So there's a missing test condition here, no? I've just realized you're passing in sizeof(struct clone_args) + 8 which hits the first excess space 64 bit value which is 0. That's good and the reason why this test passes.
But I don't see any test for sizoef(struct clone_args_extended) or at least sizeof(struct clone_args) + 16 such that you actually hit the second 64 bit integer which is initialized to 1 and thus clone3() should fail with -E2BIG.If I haven't overlooked this test, can you please add it? It's quite important since it's a different codepath than the sizeof(PAGE_SIZE) + 8 codepath.

Thanks!
Christian