Skip to content

Commit 50dd545

Browse files
committed
[DFSan] Add bcmp wrapper.
Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D87801
1 parent 1e19165 commit 50dd545

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

compiler-rt/lib/dfsan/dfsan_custom.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,9 @@ SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strchr(const char *s, int c,
9595
}
9696
}
9797

98-
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, uptr caller_pc,
99-
const void *s1, const void *s2, size_t n,
100-
dfsan_label s1_label, dfsan_label s2_label,
101-
dfsan_label n_label)
102-
103-
SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
104-
size_t n, dfsan_label s1_label,
105-
dfsan_label s2_label,
106-
dfsan_label n_label,
107-
dfsan_label *ret_label) {
108-
CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, GET_CALLER_PC(), s1, s2, n,
109-
s1_label, s2_label, n_label);
98+
static int dfsan_memcmp_bcmp(const void *s1, const void *s2, size_t n,
99+
dfsan_label s1_label, dfsan_label s2_label,
100+
dfsan_label n_label, dfsan_label *ret_label) {
110101
const char *cs1 = (const char *) s1, *cs2 = (const char *) s2;
111102
for (size_t i = 0; i != n; ++i) {
112103
if (cs1[i] != cs2[i]) {
@@ -129,6 +120,29 @@ SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
129120
return 0;
130121
}
131122

123+
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, uptr caller_pc,
124+
const void *s1, const void *s2, size_t n,
125+
dfsan_label s1_label, dfsan_label s2_label,
126+
dfsan_label n_label)
127+
128+
SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
129+
size_t n, dfsan_label s1_label,
130+
dfsan_label s2_label,
131+
dfsan_label n_label,
132+
dfsan_label *ret_label) {
133+
CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, GET_CALLER_PC(), s1, s2, n,
134+
s1_label, s2_label, n_label);
135+
return dfsan_memcmp_bcmp(s1, s2, n, s1_label, s2_label, n_label, ret_label);
136+
}
137+
138+
SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_bcmp(const void *s1, const void *s2,
139+
size_t n, dfsan_label s1_label,
140+
dfsan_label s2_label,
141+
dfsan_label n_label,
142+
dfsan_label *ret_label) {
143+
return dfsan_memcmp_bcmp(s1, s2, n, s1_label, s2_label, n_label, ret_label);
144+
}
145+
132146
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, uptr caller_pc,
133147
const char *s1, const char *s2,
134148
dfsan_label s1_label, dfsan_label s2_label)

compiler-rt/lib/dfsan/done_abilist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ fun:strtoull=custom
183183

184184
# Functions that produce an output that is computed from the input, but is not
185185
# necessarily data dependent.
186+
fun:bcmp=custom
186187
fun:memchr=custom
187188
fun:memcmp=custom
188189
fun:strcasecmp=custom

compiler-rt/test/dfsan/custom.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
#include <pwd.h>
1818
#include <sched.h>
1919
#include <signal.h>
20-
#include <stdio.h>
2120
#include <stdint.h>
21+
#include <stdio.h>
2222
#include <stdlib.h>
2323
#include <string.h>
24-
#include <sys/select.h>
24+
#include <strings.h>
2525
#include <sys/resource.h>
26+
#include <sys/select.h>
2627
#include <sys/stat.h>
2728
#include <sys/time.h>
2829
#include <sys/types.h>
@@ -86,6 +87,24 @@ void test_memcmp() {
8687
#endif
8788
}
8889

90+
void test_bcmp() {
91+
char str1[] = "str1", str2[] = "str2";
92+
dfsan_set_label(i_label, &str1[3], 1);
93+
dfsan_set_label(j_label, &str2[3], 1);
94+
95+
int rv = bcmp(str1, str2, sizeof(str1));
96+
assert(rv != 0);
97+
#ifdef STRICT_DATA_DEPENDENCIES
98+
ASSERT_ZERO_LABEL(rv);
99+
#else
100+
ASSERT_LABEL(rv, i_j_label);
101+
#endif
102+
103+
rv = bcmp(str1, str2, sizeof(str1) - 2);
104+
assert(rv == 0);
105+
ASSERT_ZERO_LABEL(rv);
106+
}
107+
89108
void test_memcpy() {
90109
char str1[] = "str1";
91110
char str2[sizeof(str1)];
@@ -967,6 +986,7 @@ int main(void) {
967986
assert(i_j_label != j_label);
968987
assert(i_j_label != k_label);
969988

989+
test_bcmp();
970990
test_calloc();
971991
test_clock_gettime();
972992
test_ctime_r();

0 commit comments

Comments
 (0)