[PATCH] Add function to get the number of DFSan labels allocated.

Sam Kerner skerner at chromium.org
Wed Mar 19 16:25:01 PDT 2014


  Comment wording updates.

Hi pcc, nlewycky,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D3109?vs=7966&id=7967#toc

Files:
  lib/dfsan/done_abilist.txt
  lib/dfsan/dfsan.cc
  lib/dfsan/dfsan.h
  test/dfsan/lib
  test/dfsan/lib/other_transactional_unit.c
  test/dfsan/lib/other_transactional_unit.h
  test/dfsan/label_count.c
  include/sanitizer/dfsan_interface.h

Index: lib/dfsan/done_abilist.txt
===================================================================
--- lib/dfsan/done_abilist.txt
+++ lib/dfsan/done_abilist.txt
@@ -16,6 +16,8 @@
 fun:dfsan_get_label=custom
 fun:dfsan_read_label=uninstrumented
 fun:dfsan_read_label=discard
+fun:dfsan_get_label_count=uninstrumented
+fun:dfsan_get_label_count=discard
 fun:dfsan_get_label_info=uninstrumented
 fun:dfsan_get_label_info=discard
 fun:dfsan_has_label=uninstrumented
Index: lib/dfsan/dfsan.cc
===================================================================
--- lib/dfsan/dfsan.cc
+++ lib/dfsan/dfsan.cc
@@ -230,6 +230,14 @@
   }
 }
 
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label_count_t
+dfsan_get_label_count(void) {
+  dfsan_label max_label_allocated =
+      atomic_load(&__dfsan_last_label, memory_order_relaxed);
+
+  return static_cast<int>(max_label_allocated);
+}
+
 static void InitializeFlags(Flags &f, const char *env) {
   f.warn_unimplemented = true;
   f.warn_nonzero_labels = false;
Index: lib/dfsan/dfsan.h
===================================================================
--- lib/dfsan/dfsan.h
+++ lib/dfsan/dfsan.h
@@ -19,6 +19,7 @@
 
 // Copy declarations from public sanitizer/dfsan_interface.h header here.
 typedef u16 dfsan_label;
+typedef u16 dfsan_label_count_t;
 
 struct dfsan_label_info {
   dfsan_label l1;
Index: test/dfsan/lib/other_transactional_unit.c
===================================================================
--- /dev/null
+++ test/dfsan/lib/other_transactional_unit.c
@@ -0,0 +1,12 @@
+// This file is used by other tests.  Compile it to verify that it can be built.
+// RUN: %clang_dfsan -m64 -c %s -o %t.o
+
+#include "other_transactional_unit.h"
+
+int add_in_separate_transactional_unit(int a, int b) {
+  return a + b;
+}
+
+int multiply_in_separate_transactional_unit(int a, int b) {
+  return a * b;
+}
Index: test/dfsan/lib/other_transactional_unit.h
===================================================================
--- /dev/null
+++ test/dfsan/lib/other_transactional_unit.h
@@ -0,0 +1,14 @@
+// Functions used in tests which must be in a separate transactional unit.
+// This prevents the compiler from optimizing code in a way that removes
+// behavior we wish to test.  For example, computing a value should cause
+// labels to be allocated only if the computation is actually done.  Putting
+// the computation here prevents the compiler from optimizing away the
+// computation (and labeling) that tests wish to verify.
+
+#ifndef OTHER_TRANSACTIONAL_UNIT_H
+#define OTHER_TRANSACTIONAL_UNIT_H
+
+int add_in_separate_transactional_unit(int i, int j);
+int multiply_in_separate_transactional_unit(int i, int j);
+
+#endif  // OTHER_TRANSACTIONAL_UNIT_H
Index: test/dfsan/label_count.c
===================================================================
--- /dev/null
+++ test/dfsan/label_count.c
@@ -0,0 +1,53 @@
+// RUN: %clang_dfsan -m64 -c %S/lib/other_transactional_unit.c -o %t.lib.o && \
+// RUN:   %clang_dfsan -m64 -c %s -o %t.o    && \
+// RUN:   %clang_dfsan -m64 %t.o %t.lib.o -o %t.bin && \
+// RUN:   %t.bin
+// RUN: %clang_dfsan -mllvm -dfsan-args-abi -m64 -c %S/lib/other_transactional_unit.c -o %t.lib.o && \
+// RUN:   %clang_dfsan -mllvm -dfsan-args-abi -m64 -c %s -o %t.o    && \
+// RUN:   %clang_dfsan -mllvm -dfsan-args-abi -m64 %t.o %t.lib.o -o %t.bin && \
+// RUN:   %t.bin
+
+#include <sanitizer/dfsan_interface.h>
+#include <assert.h>
+
+#include "lib/other_transactional_unit.h"
+
+int main(void) {
+  dfsan_label_count_t label_count;
+
+  // No labels allocated yet.
+  label_count = dfsan_get_label_count();
+  assert(0 == label_count);
+
+  int i = 1;
+  dfsan_label i_label = dfsan_create_label("i", 0);
+  dfsan_set_label(i_label, &i, sizeof(i));
+
+  // One label allocated for i.
+  label_count = dfsan_get_label_count();
+  assert(1u == label_count);
+
+  int j = 2;
+  dfsan_label j_label = dfsan_create_label("j", 0);
+  dfsan_set_label(j_label, &j, sizeof(j));
+
+  // Check that a new label was allocated for j.
+  label_count = dfsan_get_label_count();
+  assert(2u == label_count);
+
+  // Create a value that combines i and j.
+  int i_plus_j = add_in_separate_transactional_unit(i, j);  
+
+  // Check that a label was created for the union of i and j.
+  label_count = dfsan_get_label_count();
+  assert(3u == label_count);
+
+  // Combine i and j in a different way.  Check that the existing label is
+  // reused, and a new label is not created.
+  int j_times_i = multiply_in_separate_transactional_unit(j, i);
+  label_count = dfsan_get_label_count();
+  assert(3u == label_count);
+  assert(dfsan_get_label(i_plus_j) == dfsan_get_label(j_times_i));
+
+  return 0;
+}
Index: include/sanitizer/dfsan_interface.h
===================================================================
--- include/sanitizer/dfsan_interface.h
+++ include/sanitizer/dfsan_interface.h
@@ -23,6 +23,7 @@
 #endif
 
 typedef uint16_t dfsan_label;
+typedef uint16_t dfsan_label_count_t;
 
 /// Stores information associated with a specific label identifier.  A label
 /// may be a base label created using dfsan_create_label, with associated
@@ -74,6 +75,9 @@
 /// that label, else returns 0.
 dfsan_label dfsan_has_label_with_desc(dfsan_label label, const char *desc);
 
+/// Returns the number of labels allocated.
+dfsan_label_count_t dfsan_get_label_count(void);
+
 #ifdef __cplusplus
 }  // extern "C"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3109.3.patch
Type: text/x-patch
Size: 5401 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140319/b5ea55d1/attachment.bin>


More information about the llvm-commits mailing list