[compiler-rt] r214396 - [asan] End-to-end test for REP MOVS instrumentation.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Thu Jul 31 02:12:20 PDT 2014


Author: eugenis
Date: Thu Jul 31 04:12:20 2014
New Revision: 214396

URL: http://llvm.org/viewvc/llvm-project?rev=214396&view=rev
Log:
[asan] End-to-end test for REP MOVS instrumentation.

Patch by Yuri Gorshenin.

Modified:
    compiler-rt/trunk/lib/asan/tests/asan_asm_test.cc

Modified: compiler-rt/trunk/lib/asan/tests/asan_asm_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_asm_test.cc?rev=214396&r1=214395&r2=214396&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_asm_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_asm_test.cc Thu Jul 31 04:12:20 2014
@@ -22,6 +22,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
 
@@ -53,8 +54,17 @@ template<> Type asm_read<Type>(Type *ptr
   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
 
@@ -86,6 +96,14 @@ template<> Type asm_read<Type>(Type *ptr
   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__)
@@ -104,6 +122,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);
@@ -157,6 +179,28 @@ 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) {
@@ -209,6 +253,15 @@ TEST(AddressSanitizer, asm_flags) {
   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__)





More information about the llvm-commits mailing list