]> git.proxmox.com Git - rustc.git/blobdiff - src/compiler-rt/lib/asan/tests/asan_asm_test.cc
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / lib / asan / tests / asan_asm_test.cc
index adb98de1fb83919abf0747a09ea05584671ae84c..09af5c38607954aa23d25886fe3acc79c30e0515 100644 (file)
 //===----------------------------------------------------------------------===//
 #include "asan_test_utils.h"
 
-// Tests for __sanitizer_sanitize_(store|load)N functions in compiler-rt.
-
 #if defined(__linux__)
 
-#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__))
+// Assembly instrumentation is broken on x86 Android (x86 + PIC + shared runtime
+// library). See https://github.com/google/sanitizers/issues/353
+#if defined(__x86_64__) ||                                                     \
+    (defined(__i386__) && defined(__SSE2__) && !defined(__ANDROID__))
 
 #include <emmintrin.h>
 
@@ -24,6 +25,7 @@ namespace {
 
 template<typename T> void asm_write(T *ptr, T val);
 template<typename T> T asm_read(T *ptr);
+template<typename T> void asm_rep_movs(T *dst, T *src, size_t n);
 
 } // End of anonymous namespace
 
@@ -36,13 +38,10 @@ namespace {
 #define DECLARE_ASM_WRITE(Type, Size, Mov, Reg)        \
 template<> void asm_write<Type>(Type *ptr, Type val) { \
   __asm__(                                             \
-    "leaq (%[ptr]), %%rdi  \n\t"                       \
-    "movabsq $__sanitizer_sanitize_store" Size ", %%r11  \n\t" \
-    "call *%%r11  \n\t"                                 \
     Mov " %[val], (%[ptr])  \n\t"                      \
     :                                                  \
     : [ptr] "r" (ptr), [val] Reg (val)                 \
-    : "memory", "rdi", "r11"                           \
+    : "memory"                                         \
   );                                                   \
 }
 
@@ -50,39 +49,41 @@ template<> void asm_write<Type>(Type *ptr, Type val) { \
 template<> Type asm_read<Type>(Type *ptr) {        \
   Type res;                                        \
   __asm__(                                         \
-    "leaq (%[ptr]), %%rdi  \n\t"                   \
-    "movabsq $__sanitizer_sanitize_load" Size ", %%r11  \n\t" \
-    "callq *%%r11  \n\t"                           \
     Mov " (%[ptr]), %[res]  \n\t"                  \
     : [res] Reg (res)                              \
     : [ptr] "r" (ptr)                              \
-    : "memory", "rdi", "r11"                       \
+    : "memory"                                     \
   );                                               \
   return res;                                      \
 }
 
+#define DECLARE_ASM_REP_MOVS(Type, Movs)                                       \
+  template <> void asm_rep_movs<Type>(Type * dst, Type * src, size_t size) {   \
+    __asm__("rep " Movs " \n\t"                                                \
+            :                                                                  \
+            : "D"(dst), "S"(src), "c"(size)                                    \
+            : "rsi", "rdi", "rcx", "memory");                                  \
+  }
+
 DECLARE_ASM_WRITE(U8, "8", "movq", "r");
 DECLARE_ASM_READ(U8, "8", "movq", "=r");
+DECLARE_ASM_REP_MOVS(U8, "movsq");
 
 } // End of anonymous namespace
 
 #endif // defined(__x86_64__)
 
-#if defined(__i386__) && defined(__SSE2__)
+#if defined(__i386__) && defined(__SSE2__) && !defined(__ANDROID__)
 
 namespace {
 
 #define DECLARE_ASM_WRITE(Type, Size, Mov, Reg)        \
 template<> void asm_write<Type>(Type *ptr, Type val) { \
   __asm__(                                             \
-    "leal (%[ptr]), %%eax  \n\t"                       \
-    "pushl %%eax  \n\t"                                \
-    "call __sanitizer_sanitize_store" Size "  \n\t"    \
-    "popl %%eax  \n\t"                                 \
     Mov " %[val], (%[ptr])  \n\t"                      \
     :                                                  \
     : [ptr] "r" (ptr), [val] Reg (val)                 \
-    : "memory", "eax", "esp"                           \
+    : "memory"                                         \
   );                                                   \
 }
 
@@ -90,57 +91,28 @@ template<> void asm_write<Type>(Type *ptr, Type val) { \
 template<> Type asm_read<Type>(Type *ptr) {        \
   Type res;                                        \
   __asm__(                                         \
-    "leal (%[ptr]), %%eax  \n\t"                   \
-    "pushl %%eax  \n\t"                            \
-    "call __sanitizer_sanitize_load" Size "  \n\t" \
-    "popl %%eax  \n\t"                             \
     Mov " (%[ptr]), %[res]  \n\t"                  \
     : [res] Reg (res)                              \
     : [ptr] "r" (ptr)                              \
-    : "memory", "eax", "esp"                       \
+    : "memory"                                     \
   );                                               \
   return res;                                      \
 }
 
-template<> void asm_write<U8>(U8 *ptr, U8 val) {
-  __asm__(
-    "leal (%[ptr]), %%eax  \n\t"
-    "pushl %%eax  \n\t"
-    "call __sanitizer_sanitize_store8  \n\t"
-    "popl %%eax  \n\t"
-    "movl (%[val]), %%eax  \n\t"
-    "movl %%eax, (%[ptr])  \n\t"
-    "movl 0x4(%[val]), %%eax  \n\t"
-    "movl %%eax, 0x4(%[ptr])  \n\t"
-    :
-    : [ptr] "r" (ptr), [val] "r" (&val)
-    : "memory", "eax", "esp"
-  );
-}
-
-template<> U8 asm_read(U8 *ptr) {
-  U8 res;
-  __asm__(
-    "leal (%[ptr]), %%eax  \n\t"
-    "pushl %%eax  \n\t"
-    "call __sanitizer_sanitize_load8  \n\t"
-    "popl  %%eax  \n\t"
-    "movl (%[ptr]), %%eax  \n\t"
-    "movl %%eax, (%[res])  \n\t"
-    "movl 0x4(%[ptr]), %%eax  \n\t"
-    "movl %%eax, 0x4(%[res])  \n\t"
-    :
-    : [ptr] "r" (ptr), [res] "r" (&res)
-    : "memory", "eax", "esp"
-  );
-  return res;
-}
+#define DECLARE_ASM_REP_MOVS(Type, Movs)                                       \
+  template <> void asm_rep_movs<Type>(Type * dst, Type * src, size_t size) {   \
+    __asm__("rep " Movs " \n\t"                                                \
+            :                                                                  \
+            : "D"(dst), "S"(src), "c"(size)                                    \
+            : "esi", "edi", "ecx", "memory");                                  \
+  }
 
 } // End of anonymous namespace
 
 #endif  // defined(__i386__) && defined(__SSE2__)
 
-#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__))
+#if defined(__x86_64__) ||                                                     \
+    (defined(__i386__) && defined(__SSE2__) && !defined(__ANDROID__))
 
 namespace {
 
@@ -154,6 +126,10 @@ DECLARE_ASM_READ(U2, "2", "movw", "=r");
 DECLARE_ASM_READ(U4, "4", "movl", "=r");
 DECLARE_ASM_READ(__m128i, "16", "movaps", "=x");
 
+DECLARE_ASM_REP_MOVS(U1, "movsb");
+DECLARE_ASM_REP_MOVS(U2, "movsw");
+DECLARE_ASM_REP_MOVS(U4, "movsl");
+
 template<typename T> void TestAsmWrite(const char *DeathPattern) {
   T *buf = new T;
   EXPECT_DEATH(asm_write(&buf[1], static_cast<T>(0)), DeathPattern);
@@ -197,22 +173,99 @@ template<> void TestAsmRead<__m128i>(const char *DeathPattern) {
   delete [] buf;
 }
 
+U4 AsmLoad(U4 *a) {
+  U4 r;
+  __asm__("movl (%[a]), %[r]  \n\t" : [r] "=r" (r) : [a] "r" (a) : "memory");
+  return r;
+}
+
+void AsmStore(U4 r, U4 *a) {
+  __asm__("movl %[r], (%[a])  \n\t" : : [a] "r" (a), [r] "r" (r) : "memory");
+}
+
+template <typename T>
+void TestAsmRepMovs(const char *DeathPatternRead,
+                    const char *DeathPatternWrite) {
+  T src_good[4] = { 0x0, 0x1, 0x2, 0x3 };
+  T dst_good[4] = {};
+  asm_rep_movs(dst_good, src_good, 4);
+  ASSERT_EQ(static_cast<T>(0x0), dst_good[0]);
+  ASSERT_EQ(static_cast<T>(0x1), dst_good[1]);
+  ASSERT_EQ(static_cast<T>(0x2), dst_good[2]);
+  ASSERT_EQ(static_cast<T>(0x3), dst_good[3]);
+
+  T dst_bad[3];
+  EXPECT_DEATH(asm_rep_movs(dst_bad, src_good, 4), DeathPatternWrite);
+
+  T src_bad[3] = { 0x0, 0x1, 0x2 };
+  EXPECT_DEATH(asm_rep_movs(dst_good, src_bad, 4), DeathPatternRead);
+
+  T* dp = dst_bad + 4;
+  T* sp = src_bad + 4;
+  asm_rep_movs(dp, sp, 0);
+}
+
 } // End of anonymous namespace
 
+TEST(AddressSanitizer, asm_load_store) {
+  U4* buf = new U4[2];
+  EXPECT_DEATH(AsmLoad(&buf[3]), "READ of size 4");
+  EXPECT_DEATH(AsmStore(0x1234, &buf[3]), "WRITE of size 4");
+  delete [] buf;
+}
+
 TEST(AddressSanitizer, asm_rw) {
   TestAsmWrite<U1>("WRITE of size 1");
   TestAsmWrite<U2>("WRITE of size 2");
   TestAsmWrite<U4>("WRITE of size 4");
+#if defined(__x86_64__)
   TestAsmWrite<U8>("WRITE of size 8");
+#endif // defined(__x86_64__)
   TestAsmWrite<__m128i>("WRITE of size 16");
 
   TestAsmRead<U1>("READ of size 1");
   TestAsmRead<U2>("READ of size 2");
   TestAsmRead<U4>("READ of size 4");
+#if defined(__x86_64__)
   TestAsmRead<U8>("READ of size 8");
+#endif // defined(__x86_64__)
   TestAsmRead<__m128i>("READ of size 16");
 }
 
+TEST(AddressSanitizer, asm_flags) {
+  long magic = 0x1234;
+  long r = 0x0;
+
+#if defined(__x86_64__) && !defined(__ILP32__)
+  __asm__("xorq %%rax, %%rax  \n\t"
+          "movq (%[p]), %%rax \n\t"
+          "sete %%al          \n\t"
+          "movzbq %%al, %[r]  \n\t"
+          : [r] "=r"(r)
+          : [p] "r"(&magic)
+          : "rax", "memory");
+#else
+  __asm__("xorl %%eax, %%eax  \n\t"
+          "movl (%[p]), %%eax \n\t"
+          "sete %%al          \n\t"
+          "movzbl %%al, %[r]  \n\t"
+          : [r] "=r"(r)
+          : [p] "r"(&magic)
+          : "eax", "memory");
+#endif // defined(__x86_64__) && !defined(__ILP32__)
+
+  ASSERT_EQ(0x1, r);
+}
+
+TEST(AddressSanitizer, asm_rep_movs) {
+  TestAsmRepMovs<U1>("READ of size 1", "WRITE of size 1");
+  TestAsmRepMovs<U2>("READ of size 2", "WRITE of size 2");
+  TestAsmRepMovs<U4>("READ of size 4", "WRITE of size 4");
+#if defined(__x86_64__)
+  TestAsmRepMovs<U8>("READ of size 8", "WRITE of size 8");
+#endif  // defined(__x86_64__)
+}
+
 #endif // defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__))
 
 #endif // defined(__linux__)