Fix unhandled syscall: unix:348 (__pthread_chdir) and unhandled syscall: unix:349 (__pthread_fchdir) on OS X
bz#344512
- Support these two undocumented syscalls.
- New regression test case added.

Before:

== 588 tests, 239 stderr failures, 22 stdout failures, 0 stderrB failures, 0 stdoutB failures, 31 post failures ==

After:

== 589 tests, 239 stderr failures, 22 stdout failures, 0 stderrB failures, 0 stdoutB failures, 31 post failures ==

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14987 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/NEWS b/NEWS
index b7d2680..dbef0d4 100644
--- a/NEWS
+++ b/NEWS
@@ -115,6 +115,8 @@
 344499  Fix compilation for Linux kernel >= 4. With this, also require
         a Linux kernel >= 2.6 as 2.4 is mostly untested and might trigger
         obvious and non-obvious issues
+344512  Fix unhandled syscall: unix:348 (__pthread_chdir) and unhandled 
+        syscall: unix:349 (__pthread_fchdir) on OS X
 344560  Fix stack traces missing penultimate frame on OS X
 344621  Fix memcheck/tests/err_disable4 test on OS X
 344686  Fix suppression for pthread_rwlock_init on OS X 10.10
diff --git a/coregrind/m_syswrap/priv_syswrap-darwin.h b/coregrind/m_syswrap/priv_syswrap-darwin.h
index 9351b81..1360719 100644
--- a/coregrind/m_syswrap/priv_syswrap-darwin.h
+++ b/coregrind/m_syswrap/priv_syswrap-darwin.h
@@ -416,8 +416,8 @@
 DECL_TEMPLATE(darwin, statfs64);                // 345
 DECL_TEMPLATE(darwin, fstatfs64);               // 346
 DECL_TEMPLATE(darwin, getfsstat64);             // 347
-// NYI __pthread_chdir 348
-// NYI __pthread_fchdir 349
+DECL_TEMPLATE(darwin, __pthread_chdir);         // 348
+DECL_TEMPLATE(darwin, __pthread_fchdir);        // 349
 // NYI audit 350
 DECL_TEMPLATE(darwin, auditon);                 // 351
 // 352
diff --git a/coregrind/m_syswrap/syswrap-darwin.c b/coregrind/m_syswrap/syswrap-darwin.c
index cd4043a..84e2ba1 100644
--- a/coregrind/m_syswrap/syswrap-darwin.c
+++ b/coregrind/m_syswrap/syswrap-darwin.c
@@ -2186,6 +2186,22 @@
 }
 
 
+PRE(__pthread_chdir)
+{
+    PRINT("__pthread_chdir ( %#lx(%s) )", ARG1, (char*)ARG1);
+    PRE_REG_READ1(long, "__pthread_chdir", const char *, path);
+    PRE_MEM_RASCIIZ( "__pthread_chdir(path)", ARG1 );
+}
+
+
+
+PRE(__pthread_fchdir)
+{
+    PRINT("__pthread_fchdir ( %ld )", ARG1);
+    PRE_REG_READ1(long, "__pthread_fchdir", unsigned int, fd);
+}
+
+
 PRE(kdebug_trace)
 {
    PRINT("kdebug_trace(%ld, %ld, %ld, %ld, %ld, %ld)", 
@@ -9671,8 +9687,8 @@
    MACXY(__NR_statfs64,    statfs64), 
    MACXY(__NR_fstatfs64,   fstatfs64), 
    MACXY(__NR_getfsstat64, getfsstat64), 
-// _____(__NR___pthread_chdir), 
-// _____(__NR___pthread_fchdir), 
+   MACX_(__NR___pthread_chdir,  __pthread_chdir),
+   MACX_(__NR___pthread_fchdir, __pthread_fchdir),
 // _____(__NR_audit), 
    MACXY(__NR_auditon,     auditon), 
    _____(VG_DARWIN_SYSCALL_CONSTRUCT_UNIX(352)),   // ???
diff --git a/memcheck/tests/darwin/Makefile.am b/memcheck/tests/darwin/Makefile.am
index b3d2508..f5deb34 100644
--- a/memcheck/tests/darwin/Makefile.am
+++ b/memcheck/tests/darwin/Makefile.am
@@ -10,6 +10,7 @@
 	deep_badparam.stderr.exp deep_badparam.stdout.exp deep_badparam.vgtest \
 	env.stderr.exp env.vgtest \
 	pth-supp.stderr.exp pth-supp.vgtest \
+	pth-undocumented.stderr.exp pth-undocumented.stdout.exp pth-undocumented.vgtest \
 	scalar.stderr.exp scalar.vgtest \
 	scalar_fork.stderr.exp scalar_fork.vgtest \
 	scalar_nocancel.stderr.exp scalar_nocancel.vgtest \
@@ -20,6 +21,7 @@
 	deep_badparam \
 	env \
 	pth-supp \
+	pth-undocumented \
 	scalar \
 	scalar_fork \
 	scalar_nocancel \
diff --git a/memcheck/tests/darwin/pth-undocumented.c b/memcheck/tests/darwin/pth-undocumented.c
new file mode 100644
index 0000000..d9a5192
--- /dev/null
+++ b/memcheck/tests/darwin/pth-undocumented.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+ 
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <fcntl.h>
+ 
+#ifndef SYS___pthread_chdir
+# define SYS___pthread_chdir 348
+#endif
+ 
+#ifndef SYS___pthread_fchdir
+# define SYS___pthread_fchdir 349
+#endif
+ 
+int __pthread_chdir(const char *path)
+{
+   return syscall(SYS___pthread_chdir, path);
+}
+ 
+int __pthread_fchdir(int dirfd)
+{
+   return syscall(SYS___pthread_fchdir, dirfd);
+}
+ 
+int main(void)
+{
+   int dirfd;
+ 
+   dirfd = open("/", O_RDONLY);
+   if (dirfd == -1)
+      perror("open"), exit(1);
+ 
+   if (__pthread_chdir("/"))
+      perror("__pthread_chdir");
+ 
+   if (__pthread_fchdir(dirfd))
+      perror("__pthread_fchdir");
+ 
+   return 0;
+}
diff --git a/memcheck/tests/darwin/pth-undocumented.stderr.exp b/memcheck/tests/darwin/pth-undocumented.stderr.exp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/memcheck/tests/darwin/pth-undocumented.stderr.exp
diff --git a/memcheck/tests/darwin/pth-undocumented.stdout.exp b/memcheck/tests/darwin/pth-undocumented.stdout.exp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/memcheck/tests/darwin/pth-undocumented.stdout.exp
diff --git a/memcheck/tests/darwin/pth-undocumented.vgtest b/memcheck/tests/darwin/pth-undocumented.vgtest
new file mode 100644
index 0000000..58d09df
--- /dev/null
+++ b/memcheck/tests/darwin/pth-undocumented.vgtest
@@ -0,0 +1,2 @@
+prog: pth-undocumented
+vgopts: -q