[PATCH] [dfsan] Add custom function for pthread_create.

Peter Collingbourne peter at pcc.me.uk
Fri Aug 23 18:59:53 PDT 2013


Hi eugenis,

http://llvm-reviews.chandlerc.com/D1504

Files:
  lib/dfsan/dfsan_custom.cc
  lib/dfsan/done_abilist.txt
  lib/dfsan/lit_tests/custom.c

Index: lib/dfsan/dfsan_custom.cc
===================================================================
--- lib/dfsan/dfsan_custom.cc
+++ lib/dfsan/dfsan_custom.cc
@@ -19,6 +19,7 @@
 
 #include <ctype.h>
 #include <dlfcn.h>
+#include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -272,4 +273,37 @@
   return (void *)map;
 }
 
+struct pthread_create_info {
+  void *(*start_routine_trampoline)(void *, void *, dfsan_label, dfsan_label *);
+  void *start_routine;
+  void *arg;
+};
+
+static void *pthread_create_cb(void *p) {
+  pthread_create_info pci(*(pthread_create_info *)p);
+  free(p);
+  dfsan_label ret_label;
+  return pci.start_routine_trampoline(pci.start_routine, pci.arg, 0,
+                                      &ret_label);
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_pthread_create(
+    pthread_t *thread, const pthread_attr_t *attr,
+    void *(*start_routine_trampoline)(void *, void *, dfsan_label,
+                                      dfsan_label *),
+    void *start_routine, void *arg, dfsan_label thread_label,
+    dfsan_label attr_label, dfsan_label start_routine_label,
+    dfsan_label arg_label, dfsan_label *ret_label) {
+  pthread_create_info *pci =
+      (pthread_create_info *)malloc(sizeof(pthread_create_info));
+  pci->start_routine_trampoline = start_routine_trampoline;
+  pci->start_routine = start_routine;
+  pci->arg = arg;
+  int rv = pthread_create(thread, attr, pthread_create_cb, (void *)pci);
+  if (rv != 0)
+    free(pci);
+  *ret_label = 0;
+  return rv;
+}
+
 }
Index: lib/dfsan/done_abilist.txt
===================================================================
--- lib/dfsan/done_abilist.txt
+++ lib/dfsan/done_abilist.txt
@@ -102,6 +102,7 @@
 fun:read=custom
 fun:pread=custom
 fun:clock_gettime=custom
+fun:pthread_create=custom
 
 # TODO: custom
 fun:snprintf=discard
Index: lib/dfsan/lit_tests/custom.c
===================================================================
--- lib/dfsan/lit_tests/custom.c
+++ lib/dfsan/lit_tests/custom.c
@@ -5,13 +5,19 @@
 
 #include <sanitizer/dfsan_interface.h>
 #include <assert.h>
+#include <pthread.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 
+void *ptcb(void *p) {
+  assert(p == (void *)1);
+  return (void *)2;
+}
+
 int main(void) {
   int i = 1;
   dfsan_label i_label = dfsan_create_label("i", 0);
@@ -125,5 +131,11 @@
   assert(dfsan_get_label(buf[0]) == 0);
   assert(dfsan_get_label(buf[15]) == 0);
 
+  pthread_t pt;
+  pthread_create(&pt, 0, ptcb, (void *)1);
+  void *cbrv;
+  pthread_join(pt, &cbrv);
+  assert(cbrv == (void *)2);
+
   return 0;
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1504.1.patch
Type: text/x-patch
Size: 2688 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130823/383874a8/attachment.bin>


More information about the llvm-commits mailing list