<div dir="ltr"><div>I've reverted this change, r354852, r354853 and r354888 in r354906. It was causing build failures on the android sanitizer bot:</div><div>/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm/projects/compiler-rt/lib/asan/tests/asan_mem_test.cc:249:20: error: use of undeclared identifier 'bcmp'</div><div>  CmpOOBTestCommon<bcmp>();</div><div><br></div><div><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-android/builds/19957/steps/build%20android%2Faarch64/logs/stdio">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-android/builds/19957/steps/build%20android%2Faarch64/logs/stdio</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 25, 2019 at 11:42 PM Clement Courbet via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: courbet<br>
Date: Mon Feb 25 23:43:01 2019<br>
New Revision: 354851<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=354851&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=354851&view=rev</a><br>
Log:<br>
[compiler-rt] Intercept the bcmp() function.<br>
<br>
Summary:<br>
I have not introduced a separate hook for `bcmp()` as I don't think there<br>
should be any reason for a sanitizer to treat it differently from `memcmp()`.<br>
<br>
This is only enabled when building on POSIX with GNU extensions.<br>
<br>
Context: this is to avoid losing coverage when emitting `bcmp() == 0` instead<br>
of `memcmp() == 0` in llvm, see <a href="https://reviews.llvm.org/D56593" rel="noreferrer" target="_blank">https://reviews.llvm.org/D56593</a>.<br>
<br>
Reviewers: mgorny, krytarowski, vitalybuka, dvyukov<br>
<br>
Subscribers: kubamracek, dberris, delcypher, jdoerfert, #sanitizers, llvm-commits, jyknight<br>
<br>
Tags: #llvm, #sanitizers<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D58379" rel="noreferrer" target="_blank">https://reviews.llvm.org/D58379</a><br>
<br>
Added:<br>
    compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc<br>
      - copied, changed from r354014, compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc<br>
Modified:<br>
    compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h<br>
    compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc<br>
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc<br>
<br>
Modified: compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc?rev=354851&r1=354850&r2=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc?rev=354851&r1=354850&r2=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc (original)<br>
+++ compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc Mon Feb 25 23:43:01 2019<br>
@@ -9,7 +9,11 @@<br>
 // This file is a part of AddressSanitizer, an address sanity checker.<br>
 //<br>
 //===----------------------------------------------------------------------===//<br>
+#include <string.h><br>
 #include "asan_test_utils.h"<br>
+#if defined(_GNU_SOURCE)<br>
+#include <strings.h>  // for bcmp<br>
+#endif<br>
 #include <vector><br>
<br>
 template<typename T><br>
@@ -205,37 +209,43 @@ TEST(AddressSanitizer, MemMoveOOBTest) {<br>
   MemTransferOOBTestTemplate<int, MemMoveWrapper>(1024);<br>
 }<br>
<br>
-<br>
-TEST(AddressSanitizer, MemCmpOOBTest) {<br>
+template <int (*cmpfn)(const void *, const void *, size_t)><br>
+void CmpOOBTestCommon() {<br>
   size_t size = Ident(100);<br>
   char *s1 = MallocAndMemsetString(size);<br>
   char *s2 = MallocAndMemsetString(size);<br>
-  // Normal memcmp calls.<br>
-  Ident(memcmp(s1, s2, size));<br>
-  Ident(memcmp(s1 + size - 1, s2 + size - 1, 1));<br>
-  Ident(memcmp(s1 - 1, s2 - 1, 0));<br>
+  // Normal cmpfn calls.<br>
+  Ident(cmpfn(s1, s2, size));<br>
+  Ident(cmpfn(s1 + size - 1, s2 + size - 1, 1));<br>
+  Ident(cmpfn(s1 - 1, s2 - 1, 0));<br>
   // One of arguments points to not allocated memory.<br>
-  EXPECT_DEATH(Ident(memcmp)(s1 - 1, s2, 1), LeftOOBReadMessage(1));<br>
-  EXPECT_DEATH(Ident(memcmp)(s1, s2 - 1, 1), LeftOOBReadMessage(1));<br>
-  EXPECT_DEATH(Ident(memcmp)(s1 + size, s2, 1), RightOOBReadMessage(0));<br>
-  EXPECT_DEATH(Ident(memcmp)(s1, s2 + size, 1), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1 - 1, s2, 1), LeftOOBReadMessage(1));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1, s2 - 1, 1), LeftOOBReadMessage(1));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1 + size, s2, 1), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1, s2 + size, 1), RightOOBReadMessage(0));<br>
   // Hit unallocated memory and die.<br>
-  EXPECT_DEATH(Ident(memcmp)(s1 + 1, s2 + 1, size), RightOOBReadMessage(0));<br>
-  EXPECT_DEATH(Ident(memcmp)(s1 + size - 1, s2, 2), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1 + 1, s2 + 1, size), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1 + size - 1, s2, 2), RightOOBReadMessage(0));<br>
   // Zero bytes are not terminators and don't prevent from OOB.<br>
   s1[size - 1] = '\0';<br>
   s2[size - 1] = '\0';<br>
-  EXPECT_DEATH(Ident(memcmp)(s1, s2, size + 1), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1, s2, size + 1), RightOOBReadMessage(0));<br>
<br>
   // Even if the buffers differ in the first byte, we still assume that<br>
-  // memcmp may access the whole buffer and thus reporting the overflow here:<br>
+  // cmpfn may access the whole buffer and thus reporting the overflow here:<br>
   s1[0] = 1;<br>
   s2[0] = 123;<br>
-  EXPECT_DEATH(Ident(memcmp)(s1, s2, size + 1), RightOOBReadMessage(0));<br>
+  EXPECT_DEATH(Ident(cmpfn)(s1, s2, size + 1), RightOOBReadMessage(0));<br>
<br>
   free(s1);<br>
   free(s2);<br>
 }<br>
<br>
+TEST(AddressSanitizer, MemCmpOOBTest) { CmpOOBTestCommon<memcmp>(); }<br>
<br>
-<br>
+TEST(AddressSanitizer, BCmpOOBTest) {<br>
+#if defined(_GNU_SOURCE) || defined(__NetBSD__) || defined(__FreeBSD__) || \<br>
+    defined(__OpenBSD__)<br>
+  CmpOOBTestCommon<bcmp>();<br>
+#endif<br>
+}<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=354851&r1=354850&r2=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=354851&r1=354850&r2=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Mon Feb 25 23:43:01 2019<br>
@@ -819,16 +819,14 @@ INTERCEPTOR(void *, memcpy, void *dst, c<br>
 #endif<br>
<br>
 #if SANITIZER_INTERCEPT_MEMCMP<br>
-<br>
 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,<br>
                               const void *s1, const void *s2, uptr n,<br>
                               int result)<br>
<br>
-INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {<br>
-  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)<br>
-    return internal_memcmp(a1, a2, size);<br>
-  void *ctx;<br>
-  COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);<br>
+// Common code for `memcmp` and `bcmp`.<br>
+int MemcmpInterceptorCommon(void *ctx,<br>
+                            int (*real_fn)(const void *, const void *, uptr),<br>
+                            const void *a1, const void *a2, uptr size) {<br>
   if (common_flags()->intercept_memcmp) {<br>
     if (common_flags()->strict_memcmp) {<br>
       // Check the entire regions even if the first bytes of the buffers are<br>
@@ -854,17 +852,39 @@ INTERCEPTOR(int, memcmp, const void *a1,<br>
       return r;<br>
     }<br>
   }<br>
-  int result = REAL(memcmp(a1, a2, size));<br>
+  int result = real_fn(a1, a2, size);<br>
   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,<br>
                              a2, size, result);<br>
   return result;<br>
 }<br>
<br>
+INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {<br>
+  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)<br>
+    return internal_memcmp(a1, a2, size);<br>
+  void *ctx;<br>
+  COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);<br>
+  return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);<br>
+}<br>
+<br>
 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)<br>
 #else<br>
 #define INIT_MEMCMP<br>
 #endif<br>
<br>
+#if SANITIZER_INTERCEPT_BCMP<br>
+INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {<br>
+  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)<br>
+    return internal_memcmp(a1, a2, size);<br>
+  void *ctx;<br>
+  COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);<br>
+  return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);<br>
+}<br>
+<br>
+#define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)<br>
+#else<br>
+#define INIT_BCMP<br>
+#endif<br>
+<br>
 #if SANITIZER_INTERCEPT_MEMCHR<br>
 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {<br>
   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)<br>
@@ -9473,6 +9493,7 @@ static void InitializeCommonInterceptors<br>
   INIT_MEMCPY;<br>
   INIT_MEMCHR;<br>
   INIT_MEMCMP;<br>
+  INIT_BCMP;<br>
   INIT_MEMRCHR;<br>
   INIT_MEMMEM;<br>
   INIT_READ;<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=354851&r1=354850&r2=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=354851&r1=354850&r2=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Mon Feb 25 23:43:01 2019<br>
@@ -142,6 +142,9 @@<br>
 #define SANITIZER_INTERCEPT_MEMMOVE 1<br>
 #define SANITIZER_INTERCEPT_MEMCPY 1<br>
 #define SANITIZER_INTERCEPT_MEMCMP SI_NOT_FUCHSIA<br>
+#define SANITIZER_INTERCEPT_BCMP \<br>
+  SANITIZER_INTERCEPT_MEMCMP &&  \<br>
+      ((SI_POSIX && _GNU_SOURCE) || SI_NETBSD || SI_OPENBSD || SI_FREEBSD)<br>
 #define SANITIZER_INTERCEPT_STRNDUP SI_POSIX<br>
 #define SANITIZER_INTERCEPT___STRNDUP SI_LINUX_NOT_FREEBSD<br>
 #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \<br>
<br>
Copied: compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc (from r354014, compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc)<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc?p2=compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc&p1=compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc&r1=354014&r2=354851&rev=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc?p2=compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc&p1=compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc&r1=354014&r2=354851&rev=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc (original)<br>
+++ compiler-rt/trunk/test/asan/TestCases/Posix/bcmp_test.cc Mon Feb 25 23:43:01 2019<br>
@@ -8,10 +8,10 @@<br>
 #include <string.h><br>
 int main(int argc, char **argv) {<br>
   char a1[] = {static_cast<char>(argc), 2, 3, 4};<br>
-  char a2[] = {1, static_cast<char>(2*argc), 3, 4};<br>
-  int res = memcmp(a1, a2, 4 + argc);  // BOOM<br>
+  char a2[] = {1, static_cast<char>(2 * argc), 3, 4};<br>
+  int res = bcmp(a1, a2, 4 + argc); // BOOM<br>
   // CHECK: AddressSanitizer: stack-buffer-overflow<br>
-  // CHECK: {{#0.*memcmp}}<br>
-  // CHECK: {{#1.*main}}<br>
+  // CHECK: {{#1.*bcmp}}<br>
+  // CHECK: {{#2.*main}}<br>
   return res;<br>
 }<br>
<br>
Modified: compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc?rev=354851&r1=354850&r2=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc?rev=354851&r1=354850&r2=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc (original)<br>
+++ compiler-rt/trunk/test/asan/TestCases/memcmp_test.cc Mon Feb 25 23:43:01 2019<br>
@@ -11,7 +11,7 @@ int main(int argc, char **argv) {<br>
   char a2[] = {1, static_cast<char>(2*argc), 3, 4};<br>
   int res = memcmp(a1, a2, 4 + argc);  // BOOM<br>
   // CHECK: AddressSanitizer: stack-buffer-overflow<br>
-  // CHECK: {{#0.*memcmp}}<br>
-  // CHECK: {{#1.*main}}<br>
+  // CHECK: {{#1.*memcmp}}<br>
+  // CHECK: {{#2.*main}}<br>
   return res;<br>
 }<br>
<br>
Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc?rev=354851&r1=354850&r2=354851&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc?rev=354851&r1=354850&r2=354851&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc (original)<br>
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/weak_hook_test.cc Mon Feb 25 23:43:01 2019<br>
@@ -6,8 +6,11 @@<br>
 // XFAIL: lsan<br>
 // XFAIL: ubsan<br>
<br>
-#include <string.h><br>
 #include <assert.h><br>
+#include <string.h><br>
+#if defined(_GNU_SOURCE)<br>
+#include <strings.h> // for bcmp<br>
+#endif<br>
<br>
 bool seen_memcmp, seen_strncmp, seen_strncasecmp, seen_strcmp, seen_strcasecmp,<br>
     seen_strstr, seen_strcasestr, seen_memmem;<br>
@@ -59,6 +62,13 @@ int main() {<br>
   int_sink = memcmp(s1, s2, sizeof(s2));<br>
   assert(seen_memcmp);<br>
<br>
+#if defined(_GNU_SOURCE) || defined(__NetBSD__) || defined(__FreeBSD__) || \<br>
+    defined(__OpenBSD__)<br>
+  seen_memcmp = false;<br>
+  int_sink = bcmp(s1, s2, sizeof(s2));<br>
+  assert(seen_memcmp);<br>
+#endif<br>
+<br>
   int_sink = strncmp(s1, s2, sizeof(s2));<br>
   assert(seen_strncmp);<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>