Skip to contentSkip to conversation

selftests/harness: Flush stdout before forking

PATCH In review Michael Ellerman 4 participants 8 messages Sep 17, 2020 – Sep 18, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/harness: Flush stdout before forking

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

  $ ./seccomp_bpf | tee test.log
  $ grep -c "TAP version 13" test.log
  2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

  $ stdbuf -o 1MB ./seccomp_bpf > test.log
  $ grep -c "TAP version 13" test.log
  13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

---
 tools/testing/selftests/kselftest_harness.h | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/kselftest_harness.h +5 -0
index 4f78e4805633..f19804df244c 100644
@@ -971,6 +971,11 @@ void __run_test(struct __fixture_metadata *f,
 
 	ksft_print_msg(" RUN           %s%s%s.%s ...\n",
 	       f->name, variant->name[0] ? "." : "", variant->name, t->name);
+
+	/* Make sure output buffers are flushed before fork */
+	fflush(stdout);
+	fflush(stderr);
+
 	t->pid = fork();
 	if (t->pid < 0) {
 		ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
Signature · 1 line
2.25.1
Tested-by on v1 1/1
#1 lore raw
Quoted 23 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

  $ ./seccomp_bpf | tee test.log
  $ grep -c "TAP version 13" test.log
  2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

  $ stdbuf -o 1MB ./seccomp_bpf > test.log
  $ grep -c "TAP version 13" test.log
  13
Quoted 8 lines

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

---
 tools/testing/selftests/kselftest_harness.h | 5 +++++
 1 file changed, 5 insertions(+)
Signature · 2 lines
Thanks.
-- Max
maintainer
#2 lore raw
Quoted 28 lines · Max Filippov
Quoted 27 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

   $ ./seccomp_bpf | tee test.log
   $ grep -c "TAP version 13" test.log
   2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

   $ stdbuf -o 1MB ./seccomp_bpf > test.log
   $ grep -c "TAP version 13" test.log
   13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

Quoted 7 lines
Quoted 4 lines
---
  tools/testing/selftests/kselftest_harness.h | 5 +++++
  1 file changed, 5 insertions(+)

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

thanks,
-- Shuah

maintainer Acked-by
#3 lore raw
Quoted 34 lines · Shuah Khan
Quoted 33 lines · Max Filippov
Quoted 31 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

   $ ./seccomp_bpf | tee test.log
   $ grep -c "TAP version 13" test.log
   2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

   $ stdbuf -o 1MB ./seccomp_bpf > test.log
   $ grep -c "TAP version 13" test.log
   13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

---
  tools/testing/selftests/kselftest_harness.h | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 5 lines
Quoted 2 lines

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

Kees,

I haven't pulled this in yet. If you want to take this through seccomp tree for dependencies:

thanks,
-- Shuah

#4 lore raw
Quoted 42 lines · Shuah Khan
Quoted 35 lines · Shuah Khan
Quoted 34 lines · Max Filippov
Quoted 31 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

   $ ./seccomp_bpf | tee test.log
   $ grep -c "TAP version 13" test.log
   2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

   $ stdbuf -o 1MB ./seccomp_bpf > test.log
   $ grep -c "TAP version 13" test.log
   13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

---
  tools/testing/selftests/kselftest_harness.h | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 5 lines
Quoted 2 lines

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

Quoted 6 lines

Kees,

I haven't pulled this in yet. If you want to take this through seccomp tree for dependencies:

You can have it -- it's a global fix and more than the seccomp selftest is likely affected. :)

Signature · 1 line
Kees Cook
#5 lore raw
In reply to Shuah Khan
Quoted 29 lines · Shuah Khan
Quoted 28 lines · Max Filippov
Quoted 27 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

   $ ./seccomp_bpf | tee test.log
   $ grep -c "TAP version 13" test.log
   2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

   $ stdbuf -o 1MB ./seccomp_bpf > test.log
   $ grep -c "TAP version 13" test.log
   13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
---
  tools/testing/selftests/kselftest_harness.h | 5 +++++
  1 file changed, 5 insertions(+)

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

It can wait for v5.10 IMHO, but up to you.

cheers

maintainer
#6 lore raw
Quoted 35 lines · Michael Ellerman
Quoted 34 lines · Shuah Khan
Quoted 33 lines · Max Filippov
Quoted 31 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

    $ ./seccomp_bpf | tee test.log
    $ grep -c "TAP version 13" test.log
    2

But we only expect the TAP header to appear once.

It can be exacerbated using stdbuf to increase the buffer size:

    $ stdbuf -o 1MB ./seccomp_bpf > test.log
    $ grep -c "TAP version 13" test.log
    13

The fix is simple, we just flush stdout & stderr before fork. Usually stderr is unbuffered, but that can be changed, so flush it as well just to be safe.

---
   tools/testing/selftests/kselftest_harness.h | 5 +++++
   1 file changed, 5 insertions(+)
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Thank you both. Applying to linux-kselftest fixes for 5.9-rc7

It can wait for v5.10 IMHO, but up to you.

Good to know. I will send this for 5.10-rc1 then.

thanks,
-- Shuah

Acked-by on v1 1/1
#7 lore raw
In reply to Michael Ellerman
Quoted 5 lines · Michael Ellerman

The test harness forks() a child to run each test. Both the parent and the child print to stdout using libc functions. That can lead to duplicated (or more) output if the libc buffers are not flushed before forking.

Quoted 8 lines

It's generally not seen when running programs directly, because stdout will usually be line buffered when it's pointing to a terminal.

This was noticed when running the seccomp_bpf test, eg:

  $ ./seccomp_bpf | tee test.log
  $ grep -c "TAP version 13" test.log
  2

Oh thank you for tracking this down! I thought seccomp wasn't killing a child or something, and hadn't found it yet. :)

Signature · 1 line
Kees Cook