[compiler-rt] r196388 - [DFSan] Change the way labels are propagated when comparing memory through libc functions.
Lorenzo Martignoni
martignlo at google.com
Wed Dec 4 08:48:09 PST 2013
Author: martignlo
Date: Wed Dec 4 10:48:09 2013
New Revision: 196388
URL: http://llvm.org/viewvc/llvm-project?rev=196388&view=rev
Log:
[DFSan] Change the way labels are propagated when comparing memory through libc functions.
Differential Revision: http://llvm-reviews.chandlerc.com/D2252
Modified:
compiler-rt/trunk/lib/dfsan/dfsan.cc
compiler-rt/trunk/lib/dfsan/dfsan.h
compiler-rt/trunk/lib/dfsan/dfsan_custom.cc
compiler-rt/trunk/lib/dfsan/lit_tests/custom.c
Modified: compiler-rt/trunk/lib/dfsan/dfsan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/dfsan/dfsan.cc?rev=196388&r1=196387&r2=196388&view=diff
==============================================================================
--- compiler-rt/trunk/lib/dfsan/dfsan.cc (original)
+++ compiler-rt/trunk/lib/dfsan/dfsan.cc Wed Dec 4 10:48:09 2013
@@ -233,9 +233,11 @@ dfsan_has_label_with_desc(dfsan_label la
static void InitializeFlags(Flags &f, const char *env) {
f.warn_unimplemented = true;
f.warn_nonzero_labels = false;
+ f.strict_data_dependencies = true;
ParseFlag(env, &f.warn_unimplemented, "warn_unimplemented");
ParseFlag(env, &f.warn_nonzero_labels, "warn_nonzero_labels");
+ ParseFlag(env, &f.strict_data_dependencies, "strict_data_dependencies");
}
#ifdef DFSAN_NOLIBC
Modified: compiler-rt/trunk/lib/dfsan/dfsan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/dfsan/dfsan.h?rev=196388&r1=196387&r2=196388&view=diff
==============================================================================
--- compiler-rt/trunk/lib/dfsan/dfsan.h (original)
+++ compiler-rt/trunk/lib/dfsan/dfsan.h Wed Dec 4 10:48:09 2013
@@ -55,6 +55,11 @@ struct Flags {
bool warn_unimplemented;
// Whether to warn on non-zero labels.
bool warn_nonzero_labels;
+ // Whether to propagate labels only when there is an obvious data dependency
+ // (e.g., when comparing strings, ignore the fact that the output of the
+ // comparison might be data-dependent on the content of the strings). This
+ // applies only to the custom functions defined in 'custom.c'.
+ bool strict_data_dependencies;
};
extern Flags flags_data;
Modified: compiler-rt/trunk/lib/dfsan/dfsan_custom.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/dfsan/dfsan_custom.cc?rev=196388&r1=196387&r2=196388&view=diff
==============================================================================
--- compiler-rt/trunk/lib/dfsan/dfsan_custom.cc (original)
+++ compiler-rt/trunk/lib/dfsan/dfsan_custom.cc Wed Dec 4 10:48:09 2013
@@ -67,7 +67,11 @@ SANITIZER_INTERFACE_ATTRIBUTE char *__df
dfsan_label *ret_label) {
for (size_t i = 0;; ++i) {
if (s[i] == c || s[i] == 0) {
- *ret_label = dfsan_union(dfsan_read_label(s, i+1), c_label);
+ if (flags().strict_data_dependencies) {
+ *ret_label = s_label;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(s, i + 1), c_label);
+ }
return s[i] == 0 ? 0 : const_cast<char *>(s+i);
}
}
@@ -81,13 +85,22 @@ SANITIZER_INTERFACE_ATTRIBUTE int __dfsw
const char *cs1 = (const char *) s1, *cs2 = (const char *) s2;
for (size_t i = 0; i != n; ++i) {
if (cs1[i] != cs2[i]) {
- *ret_label = dfsan_union(dfsan_read_label(cs1, i+1),
- dfsan_read_label(cs2, i+1));
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(cs1, i + 1),
+ dfsan_read_label(cs2, i + 1));
+ }
return cs1[i] - cs2[i];
}
}
- *ret_label = dfsan_union(dfsan_read_label(cs1, n),
- dfsan_read_label(cs2, n));
+
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(cs1, n),
+ dfsan_read_label(cs2, n));
+ }
return 0;
}
@@ -97,8 +110,12 @@ SANITIZER_INTERFACE_ATTRIBUTE int __dfsw
dfsan_label *ret_label) {
for (size_t i = 0;; ++i) {
if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0) {
- *ret_label = dfsan_union(dfsan_read_label(s1, i+1),
- dfsan_read_label(s2, i+1));
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
+ dfsan_read_label(s2, i + 1));
+ }
return s1[i] - s2[i];
}
}
@@ -110,8 +127,12 @@ __dfsw_strcasecmp(const char *s1, const
dfsan_label s2_label, dfsan_label *ret_label) {
for (size_t i = 0;; ++i) {
if (tolower(s1[i]) != tolower(s2[i]) || s1[i] == 0 || s2[i] == 0) {
- *ret_label = dfsan_union(dfsan_read_label(s1, i+1),
- dfsan_read_label(s2, i+1));
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
+ dfsan_read_label(s2, i + 1));
+ }
return s1[i] - s2[i];
}
}
@@ -129,9 +150,13 @@ SANITIZER_INTERFACE_ATTRIBUTE int __dfsw
}
for (size_t i = 0;; ++i) {
- if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0 || i == n-1) {
- *ret_label = dfsan_union(dfsan_read_label(s1, i+1),
- dfsan_read_label(s2, i+1));
+ if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0 || i == n - 1) {
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
+ dfsan_read_label(s2, i + 1));
+ }
return s1[i] - s2[i];
}
}
@@ -150,8 +175,12 @@ __dfsw_strncasecmp(const char *s1, const
for (size_t i = 0;; ++i) {
if (tolower(s1[i]) != tolower(s2[i]) || s1[i] == 0 || s2[i] == 0 ||
i == n - 1) {
- *ret_label = dfsan_union(dfsan_read_label(s1, i+1),
- dfsan_read_label(s2, i+1));
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
+ dfsan_read_label(s2, i + 1));
+ }
return s1[i] - s2[i];
}
}
@@ -171,7 +200,11 @@ SANITIZER_INTERFACE_ATTRIBUTE void *__df
SANITIZER_INTERFACE_ATTRIBUTE size_t
__dfsw_strlen(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
size_t ret = strlen(s);
- *ret_label = dfsan_read_label(s, ret+1);
+ if (flags().strict_data_dependencies) {
+ *ret_label = 0;
+ } else {
+ *ret_label = dfsan_read_label(s, ret + 1);
+ }
return ret;
}
@@ -191,7 +224,7 @@ SANITIZER_INTERFACE_ATTRIBUTE
void *__dfsw_memcpy(void *dest, const void *src, size_t n,
dfsan_label dest_label, dfsan_label src_label,
dfsan_label n_label, dfsan_label *ret_label) {
- *ret_label = 0;
+ *ret_label = dest_label;
return dfsan_memcpy(dest, src, n);
}
@@ -200,7 +233,7 @@ void *__dfsw_memset(void *s, int c, size
dfsan_label s_label, dfsan_label c_label,
dfsan_label n_label, dfsan_label *ret_label) {
dfsan_memset(s, c, c_label, n);
- *ret_label = 0;
+ *ret_label = s_label;
return s;
}
Modified: compiler-rt/trunk/lib/dfsan/lit_tests/custom.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/dfsan/lit_tests/custom.c?rev=196388&r1=196387&r2=196388&view=diff
==============================================================================
--- compiler-rt/trunk/lib/dfsan/lit_tests/custom.c (original)
+++ compiler-rt/trunk/lib/dfsan/lit_tests/custom.c Wed Dec 4 10:48:09 2013
@@ -1,5 +1,7 @@
-// RUN: %clang_dfsan -m64 %s -o %t && %t
-// RUN: %clang_dfsan -mllvm -dfsan-args-abi -m64 %s -o %t && %t
+// RUN: %clang_dfsan -m64 %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %t
+// RUN: %clang_dfsan -mllvm -dfsan-args-abi -m64 %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %t
+// RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES -m64 %s -o %t && %t
+// RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES -mllvm -dfsan-args-abi -m64 %s -o %t && %t
// Tests custom implementations of various glibc functions.
@@ -75,7 +77,11 @@ void test_memcmp() {
int rv = memcmp(str1, str2, sizeof(str1));
assert(rv < 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, i_j_label);
+#endif
}
void test_memcpy() {
@@ -108,7 +114,11 @@ void test_strcmp() {
int rv = strcmp(str1, str2);
assert(rv < 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, i_j_label);
+#endif
}
void test_strlen() {
@@ -117,7 +127,11 @@ void test_strlen() {
int rv = strlen(str1);
assert(rv == 4);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, i_label);
+#endif
}
void test_strdup() {
@@ -160,7 +174,11 @@ void test_strncmp() {
int rv = strncmp(str1, str2, sizeof(str1));
assert(rv < 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
+#endif
rv = strncmp(str1, str2, 3);
assert(rv == 0);
@@ -175,11 +193,19 @@ void test_strcasecmp() {
int rv = strcasecmp(str1, str2);
assert(rv < 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
+#endif
rv = strcasecmp(str1, str3);
assert(rv == 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
+#endif
}
void test_strncasecmp() {
@@ -189,7 +215,11 @@ void test_strncasecmp() {
int rv = strncasecmp(str1, str2, sizeof(str1));
assert(rv < 0);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(rv);
+#else
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
+#endif
rv = strncasecmp(str1, str2, 3);
assert(rv == 0);
@@ -206,11 +236,19 @@ void test_strchr() {
crv = strchr(str1, '1');
assert(crv == &str1[3]);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(crv);
+#else
ASSERT_LABEL(crv, i_label);
+#endif
crv = strchr(str1, 'x');
assert(!crv);
+#ifdef STRICT_DATA_DEPENDENCIES
+ ASSERT_ZERO_LABEL(crv);
+#else
ASSERT_LABEL(crv, i_label);
+#endif
}
void test_calloc() {
More information about the llvm-commits
mailing list