gcc.dg/tree-prof/time-profiler-2.c and gcc.misc-tests/gcov-pr86536.c call fork, 
and the parent returns from main without waiting for the child.  DejaGnu runs 
the test program on a pty, as the session leader.
When the parent exits, the kernel hangs up the pty and sends SIGHUP to its 
foreground process group, so a child that is still running is killed before it 
writes its profile counts.  time-profiler-2.c then misses the child's call to 
bar, and its tp_first_run checks fail; gcov-pr86536.c reports a count of 1 
instead of 2 for the lines after the fork.

Natively the child usually finishes first, so this rarely shows.  Under a 
simulator it often does not: with user-mode QEMU both tests fail on every run, 
and with another simulator time-profiler-2.c fails intermittently.  Running the 
same programs on a pty with SIGHUP ignored, or without a pty, always keeps the 
child's counts.

Make the parent wait for the child in both tests.  In gcov-pr86536.c the call 
goes on the blank line before the first count(2) line, so no annotated line 
moves: gcov.exp names its checks by line number, and the expected counts are 
unchanged.

Assisted-by: Claude Opus 5.5

            PR testsuite/111298

gcc/testsuite/ChangeLog:

            * gcc.dg/tree-prof/time-profiler-2.c: Include <sys/wait.h>.
            (main): Wait for the child process.
            * gcc.misc-tests/gcov-pr86536.c (main): Likewise.

Signed-off-by: Michael Morrell <[email protected]>
---
Bootstrapped and regtested on x86_64-pc-linux-gnu (-m32/-m64), no new 
regressions.

This is PR testsuite/111298 (the same three time-profiler-2.c failures under 
qemu-user on RISC-V and aarch64), which was closed as INVALID on the assumption 
that it was a QEMU bug.  The SIGHUP above shows it is the test itself: QEMU 
only makes the child slow enough to lose the race.

In gcov-pr86536.c, the more obvious placement of the waitpid, just before 
"return 0;", works too, but it moves that line down.  gcov.exp names its checks 
by line number, so "count for line 22" would disappear and "count for line 24" 
appear, which looks like a lost PASS to anyone comparing results across the 
commit.

I also considered ignoring SIGHUP in the tests instead.  That works under 
DejaGnu today, but only because the child keeps the pty open, so expect doesn't 
see EOF until the child has exited.  waitpid doesn't depend on how the harness 
runs the program.

Also tested on a cross target under user-mode QEMU: before the patch both tests 
failed on every run, after it both passed on every run
(5 runs of time-profiler-2.c, 3 of gcov-pr86536.c).

gcc/testsuite/gcc.dg/tree-prof/time-profiler-2.c | 6 +++++-
gcc/testsuite/gcc.misc-tests/gcov-pr86536.c      | 4 ++--
2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-prof/time-profiler-2.c 
b/gcc/testsuite/gcc.dg/tree-prof/time-profiler-2.c
index eed0b1dd08d..b9eb5520624 100644
--- a/gcc/testsuite/gcc.dg/tree-prof/time-profiler-2.c
+++ b/gcc/testsuite/gcc.dg/tree-prof/time-profiler-2.c
@@ -1,6 +1,7 @@
/* { dg-options "-O2 -fdump-ipa-profile -fno-ipa-vrp" } */
 #include <unistd.h>
+#include <sys/wait.h>
 __attribute__ ((noinline))
int foo()
@@ -39,7 +40,10 @@ int main ()
   if(f == 0) /* Child process.  */
     r = bar() - foo();
   else /* Parent process.  */
-    r = foo() - foo();
+    {
+      r = foo() - foo();
+      waitpid (f, NULL, 0); /* Wait for child to flush gcov data.  */
+    }
   return r;
}
diff --git a/gcc/testsuite/gcc.misc-tests/gcov-pr86536.c 
b/gcc/testsuite/gcc.misc-tests/gcov-pr86536.c
index febfacd9243..03b5219fa65 100644
--- a/gcc/testsuite/gcc.misc-tests/gcov-pr86536.c
+++ b/gcc/testsuite/gcc.misc-tests/gcov-pr86536.c
@@ -15,9 +15,9 @@ main (void)
   int j = 22;                    /* count(1) */
                                      /* returns(200) */
-  fork ();                        /* count(1)  */
+  pid_t pid = fork ();     /* count(1)  */
                                     /* returns(end) */
-
+  if (pid > 0) waitpid (pid, NULL, 0); /* Wait for the child's counts.
+ */
   int i = 7;                      /* count(2) */
   return 0;                     /* count(2) */
}
--
2.43.0

Reply via email to