A few race conditions in the testsuite

Howard Hinnant hhinnant@apple.com
Sat Mar 11 18:08:00 GMT 2006


Recently it was brought to my attention that there are a few race  
conditions in some of the libstdc++ tests.  I'm specifically  
concerned about:

libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/char/10097.cc
libstdc++-v3/testsuite/27_io/objects/char/9661-1.cc
libstdc++-v3/testsuite/27_io/objects/wchar_t/9661-1.cc

Each of these tests forks a child process and uses semaphores to  
synchronize with it.  The parent process is responsible for  
destructing the semaphores in each case, which it does upon returning  
from main.  Therefore it is imperative that the child process be done  
with the semaphore use before the parent returns from main.  However  
in each of these three tests, it is possible (and does happen  
sometimes in my testing), that the parent process returns from main,  
destructing the semaphore, while the child process is still  
interacting with it.

The fix is simple:  Have the child process signal the parent via a  
semaphore when it is done using the semaphores.  The parent process  
waits on this final signal before returning from main.

A diff is provided below.

-Howard

Index: libstdc++-v3/testsuite/27_io/objects/wchar_t/9661-1.cc
===================================================================
--- libstdc++-v3/testsuite/27_io/objects/wchar_t/9661-1.cc       
(revision 111965)
+++ libstdc++-v3/testsuite/27_io/objects/wchar_t/9661-1.cc       
(working copy)
@@ -59,6 +59,7 @@
        s1.signal ();
        s2.wait ();
        fclose(file);
+      s1.signal();
        exit(0);
      }

@@ -82,6 +83,7 @@
    VERIFY( c5 != WEOF );
    VERIFY( c5 == c4 );
    s2.signal ();
+  s1.wait();
}
int main()
Index: libstdc++-v3/testsuite/27_io/objects/char/9661-1.cc
===================================================================
--- libstdc++-v3/testsuite/27_io/objects/char/9661-1.cc (revision  
111965)
+++ libstdc++-v3/testsuite/27_io/objects/char/9661-1.cc (working copy)
@@ -60,6 +60,7 @@
        s1.signal ();
        s2.wait ();
        fclose(file);
+      s1.signal();
        exit(0);
      }

@@ -83,6 +84,7 @@
    VERIFY( c5 != EOF );
    VERIFY( c5 == c4 );
    s2.signal ();
+  s1.wait();
}
int main()
Index: libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/char/ 
10097.cc
===================================================================
--- libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/char/ 
10097.cc  (revision 111965)
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/char/ 
10097.cc  (working copy)
@@ -61,7 +61,7 @@
        VERIFY( false );
      }

-  semaphore s1;
+  semaphore s1, s2;
    int fval = fork();
    if (fval == -1)
      {
@@ -77,6 +77,7 @@
        fbout.pubsync();
        s1.wait ();
        fbout.close();
+      s2.signal();
        exit(0);
      }
@@ -98,6 +99,7 @@
    fb.close();
    s1.signal ();
+  s2.wait();
}
int main()



More information about the Libstdc++ mailing list