This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Detect chdir races.


Another application is race detection. Here we can detect unsynchronized
chdirs by adding a mutex and aborting when second thread tries to lock
it.

A possible enchancement would be storing backtrace of first thread that
did locking to make race clear.

Comments?

	* preload/chdir.c: New file.

---
 preload/chdir.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 preload/chdir.c

diff --git a/preload/chdir.c b/preload/chdir.c
new file mode 100644
index 0000000..19683db
--- /dev/null
+++ b/preload/chdir.c
@@ -0,0 +1,47 @@
+#include <unistd.h>
+#include <pthread.h>
+#include "common.h"
+static pthread_mutex_t mutex;
+static void __attribute__((constructor)) init(){
+   pthread_mutex_init (&mutex, NULL);
+}
+
+int chdir(const char *path)
+{
+  int (*fp)(const char *) = dlsym (__libc_handle, "chdir");
+  if (pthread_mutex_trylock (&mutex)) {
+   log ("race condition in chdir(%s)\n", path);
+  }
+  int ret = fp (path);
+  pthread_mutex_unlock (&mutex);
+  return ret;
+}
+int fchdir(int fd)
+{
+  int (*fp)(int) = dlsym (__libc_handle, "fchdir");
+  if (pthread_mutex_trylock (&mutex)) {
+   log ("race condition in fchdir(%i)\n", fd);
+  }
+  int ret = fp (fd);
+  pthread_mutex_unlock (&mutex);
+  return ret;
+}
+
+/*
+FILE *fopen(const char *path, const char *mode)
+{ 
+  FILE *(*fp)(const char *, const char *) = dlsym (__libc_handle, "fopen");
+  FILE *ret;
+ 
+  if (path[0]!='/')
+    return fp (path, mode);
+  else {
+    if (0 && !pthread_mutex_trylock (&mutex)) {
+      ret = fp (path, mode);
+      pthread_mutex_unlock (&mutex);
+    } else
+      ret = fp (path, mode);
+    return ret;
+  }
+}
+*/
-- 
1.8.4.rc3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]