summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2014-03-26 21:09:53 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2014-03-26 21:09:53 +0000
commit1bfc0cc4c68a51dfa2c24f9110123881e9ff2edf (patch)
treeec9711703b4db231ad00ca485b49ce00f37693d0
parent77f8bbf25353441278bd6a6f5a2fdb03c7475493 (diff)
downloadcompiler-rt-1bfc0cc4c68a51dfa2c24f9110123881e9ff2edf.tar.gz
Add function to get the number of DFSan labels allocated.
Expose the number of DFSan labels allocated by adding function dfsan_get_label_count(). Patch by Sam Kerner! Differential Revision: http://llvm-reviews.chandlerc.com/D3109 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@204854 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/sanitizer/dfsan_interface.h3
-rw-r--r--lib/dfsan/dfsan.cc8
-rw-r--r--lib/dfsan/done_abilist.txt2
-rw-r--r--test/dfsan/label_count.c75
4 files changed, 88 insertions, 0 deletions
diff --git a/include/sanitizer/dfsan_interface.h b/include/sanitizer/dfsan_interface.h
index f14d45a2b..913e7f3d0 100644
--- a/include/sanitizer/dfsan_interface.h
+++ b/include/sanitizer/dfsan_interface.h
@@ -74,6 +74,9 @@ int dfsan_has_label(dfsan_label label, dfsan_label elem);
/// that label, else returns 0.
dfsan_label dfsan_has_label_with_desc(dfsan_label label, const char *desc);
+/// Returns the number of labels allocated.
+size_t dfsan_get_label_count(void);
+
#ifdef __cplusplus
} // extern "C"
diff --git a/lib/dfsan/dfsan.cc b/lib/dfsan/dfsan.cc
index 598d81c2b..076ec58d4 100644
--- a/lib/dfsan/dfsan.cc
+++ b/lib/dfsan/dfsan.cc
@@ -230,6 +230,14 @@ dfsan_has_label_with_desc(dfsan_label label, const char *desc) {
}
}
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
+dfsan_get_label_count(void) {
+ dfsan_label max_label_allocated =
+ atomic_load(&__dfsan_last_label, memory_order_relaxed);
+
+ return static_cast<uptr>(max_label_allocated);
+}
+
static void InitializeFlags(Flags &f, const char *env) {
f.warn_unimplemented = true;
f.warn_nonzero_labels = false;
diff --git a/lib/dfsan/done_abilist.txt b/lib/dfsan/done_abilist.txt
index 51b8212be..a5a7acaa3 100644
--- a/lib/dfsan/done_abilist.txt
+++ b/lib/dfsan/done_abilist.txt
@@ -16,6 +16,8 @@ fun:dfsan_get_label=uninstrumented
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
diff --git a/test/dfsan/label_count.c b/test/dfsan/label_count.c
new file mode 100644
index 000000000..5e368ca1b
--- /dev/null
+++ b/test/dfsan/label_count.c
@@ -0,0 +1,75 @@
+// RUN: %clang_dfsan -DLIB -m64 -c %s -o %t.lib.o && \
+// RUN: %clang_dfsan -m64 -c %s -o %t.o && \
+// RUN: %clang_dfsan -m64 %t.lib.o %t.o -o %t.bin && \
+// RUN: %t.bin
+
+// RUN: %clang_dfsan -mllvm -dfsan-args-abi -m64 -DLIB -c %s -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>
+
+#ifdef LIB
+// Compiling this file with and without LIB defined allows this file to be
+// built as two separate translation units. This ensures that the code
+// can not be optimized 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.
+
+int add_in_separate_translation_unit(int a, int b) {
+ return a + b;
+}
+
+int multiply_in_separate_translation_unit(int a, int b) {
+ return a * b;
+}
+
+#else
+
+int add_in_separate_translation_unit(int i, int j);
+int multiply_in_separate_translation_unit(int i, int j);
+
+int main(void) {
+ size_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_translation_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_translation_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;
+}
+#endif // #ifdef LIB