[PATCH] [msan] Intercept *getxattr and *listxattr.

Sergey Matveev earthdok at google.com
Tue Jan 28 09:55:31 PST 2014


Hi eugenis,

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

Files:
  lib/msan/lit_tests/Linux/xattr.cc
  lib/msan/lit_tests/Linux/xattr_test_root/a
  lib/msan/msan_interceptors.cc

Index: lib/msan/lit_tests/Linux/xattr.cc
===================================================================
--- /dev/null
+++ lib/msan/lit_tests/Linux/xattr.cc
@@ -0,0 +1,129 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t %p 2>&1
+// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %t %p 2>&1
+// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t %p 2>&1
+
+#include <argz.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <attr/xattr.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sanitizer/msan_interface.h>
+
+char g_path[1024];
+int g_fd;
+
+// Life before closures...
+ssize_t listxattr_wrapper(char *buf, size_t size) {
+  return listxattr(g_path, buf, size);
+}
+
+ssize_t llistxattr_wrapper(char *buf, size_t size) {
+  return llistxattr(g_path, buf, size);
+}
+
+ssize_t flistxattr_wrapper(char *buf, size_t size) {
+  return flistxattr(g_fd, buf, size);
+}
+
+ssize_t getxattr_wrapper(const char *name, char *buf, size_t size) {
+  return getxattr(g_path, name, buf, size);
+}
+
+ssize_t lgetxattr_wrapper(const char *name, char *buf, size_t size) {
+  return lgetxattr(g_path, name, buf, size);
+}
+
+ssize_t fgetxattr_wrapper(const char *name, char *buf, size_t size) {
+  return fgetxattr(g_fd, name, buf, size);
+}
+
+size_t test_list(ssize_t fun(char*, size_t), char **buf) {
+  int buf_size = 1024;
+  while (true) {
+    *buf = (char *)malloc(buf_size);
+    assert(__msan_test_shadow(*buf, buf_size) != -1);
+    ssize_t res = fun(*buf, buf_size);
+    if (res >= 0) {
+      assert(__msan_test_shadow(*buf, buf_size) == -1);
+      return res;
+    }
+    if (errno == ENOTSUP) {
+      printf("Extended attributes are disabled. *xattr test is a no-op.\n");
+      exit(0);
+    }
+    assert(errno == ERANGE);
+    free(*buf);
+    buf_size *= 2;
+  }
+}
+
+// True means success. False means result inconclusive because we don't have
+// access to this attribute.
+bool test_get_single_attr(ssize_t fun(const char *, char *, size_t),
+                          const char *attr_name) {
+  char *buf;
+  int buf_size = 1024;
+  while (true) {
+    buf = (char *)malloc(buf_size);
+    assert(__msan_test_shadow(buf, buf_size) != -1);
+    ssize_t res = fun(attr_name, buf, buf_size);
+    if (res >= 0) {
+      assert(__msan_test_shadow(buf, buf_size) == -1);
+      free(buf);
+      return true;
+    }
+    if (errno == ENOTSUP) {
+      printf("Extended attributes are disabled. *xattr test is a no-op.\n");
+      exit(0);
+    }
+    if (errno == ENOATTR)
+      return false;
+    assert(errno == ERANGE);
+    free(buf);
+    buf_size *= 2;
+  }
+}
+
+void test_get(ssize_t fun(const char *, char *, size_t), const char *attr_list,
+              size_t attr_list_size) {
+  // Try every attribute, until we see one we can access. Attribute names are
+  // null-separated strings in attr_list.
+  size_t attr_list_len = argz_count(attr_list, attr_list_size);
+  char **attrs = (char **)malloc((attr_list_len + 1) * sizeof(char *));
+  size_t i;
+  for (i = 0; (i < attr_list_len) && attrs[i]; i++) {
+    if (test_get_single_attr(fun, attrs[i]))
+      return;
+  }
+  printf("*xattr test could not access any attributes.\n");
+}
+
+// TODO: set some attributes before trying to retrieve them with *getxattr.
+// Currently the list is empty, so *getxattr is not tested.
+int main(int argc, char *argv[]) {
+  assert(argc == 2);
+  snprintf(g_path, sizeof(g_path), "%s/%s", argv[1], "xattr_test_root/a");
+
+  g_fd = open(g_path, O_RDONLY);
+  assert(g_fd);
+
+  char *attr_list;
+  size_t attr_list_size;
+  attr_list_size = test_list(listxattr_wrapper, &attr_list);
+  free(attr_list);
+  attr_list_size = test_list(llistxattr_wrapper, &attr_list);
+  free(attr_list);
+  attr_list_size = test_list(flistxattr_wrapper, &attr_list);
+
+  test_get(getxattr_wrapper, attr_list, attr_list_size);
+  test_get(lgetxattr_wrapper, attr_list, attr_list_size);
+  test_get(fgetxattr_wrapper, attr_list, attr_list_size);
+
+  free(attr_list);
+  return 0;
+}
Index: lib/msan/msan_interceptors.cc
===================================================================
--- lib/msan/msan_interceptors.cc
+++ lib/msan/msan_interceptors.cc
@@ -1158,6 +1158,39 @@
   return 0;
 }
 
+#define INTERCEPTOR_XATTR_BODY(ret_type, func, ...) \
+  ENSURE_MSAN_INITED();                             \
+  ret_type res = REAL(func)(__VA_ARGS__);           \
+  if (res >= 0) __msan_unpoison(buf, size);         \
+  return res;
+
+INTERCEPTOR(SSIZE_T, listxattr, const char *path, void *buf, SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, listxattr, path, buf, size);
+}
+
+INTERCEPTOR(SSIZE_T, llistxattr, const char *path, void *buf, SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, llistxattr, path, buf, size);
+}
+
+INTERCEPTOR(SSIZE_T, flistxattr, int fd, void *buf, SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, flistxattr, fd, buf, size);
+}
+
+INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, void *buf,
+            SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, getxattr, path, name, buf, size);
+}
+
+INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, void *buf,
+            SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, lgetxattr, path, name, buf, size);
+}
+
+INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, void *buf,
+            SIZE_T size) {
+  INTERCEPTOR_XATTR_BODY(SSIZE_T, fgetxattr, fd, name, buf, size);
+}
+
 struct MSanInterceptorContext {
   bool in_interceptor_scope;
 };
@@ -1479,6 +1512,12 @@
   INTERCEPT_FUNCTION(tzset);
   INTERCEPT_FUNCTION(__cxa_atexit);
   INTERCEPT_FUNCTION(shmat);
+  INTERCEPT_FUNCTION(listxattr);
+  INTERCEPT_FUNCTION(llistxattr);
+  INTERCEPT_FUNCTION(flistxattr);
+  INTERCEPT_FUNCTION(getxattr);
+  INTERCEPT_FUNCTION(lgetxattr);
+  INTERCEPT_FUNCTION(fgetxattr);
 
   if (REAL(pthread_key_create)(&g_thread_finalize_key, &thread_finalize)) {
     Printf("MemorySanitizer: failed to create thread key\n");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2642.1.patch
Type: text/x-patch
Size: 6019 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140128/68ee26fd/attachment.bin>


More information about the llvm-commits mailing list