Index: lib/asan/tests/asan_asm_test.cc =================================================================== --- lib/asan/tests/asan_asm_test.cc +++ lib/asan/tests/asan_asm_test.cc @@ -22,6 +22,7 @@ template void asm_write(T *ptr, T val); template T asm_read(T *ptr); +template void asm_rep_movs(T *dst, T *src, size_t n); } // End of anonymous namespace @@ -53,8 +54,17 @@ return res; \ } +#define DECLARE_ASM_REP_MOVS(Type, Movs) \ + template <> void asm_rep_movs(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 @@ return res; \ } +#define DECLARE_ASM_REP_MOVS(Type, Movs) \ + template <> void asm_rep_movs(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(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 void TestAsmWrite(const char *DeathPattern) { T *buf = new T; EXPECT_DEATH(asm_write(&buf[1], static_cast(0)), DeathPattern); @@ -157,6 +179,28 @@ __asm__("movl %[r], (%[a]) \n\t" : : [a] "r" (a), [r] "r" (r) : "memory"); } +template +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(0x0), dst_good[0]); + ASSERT_EQ(static_cast(0x1), dst_good[1]); + ASSERT_EQ(static_cast(0x2), dst_good[2]); + ASSERT_EQ(static_cast(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 @@ ASSERT_EQ(0x1, r); } +TEST(AddressSanitizer, asm_rep_movs) { + TestAsmRepMovs("READ of size 1", "WRITE of size 1"); + TestAsmRepMovs("READ of size 2", "WRITE of size 2"); + TestAsmRepMovs("READ of size 4", "WRITE of size 4"); +#if defined(__x86_64__) + TestAsmRepMovs("READ of size 8", "WRITE of size 8"); +#endif // defined(__x86_64__) +} + #endif // defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) #endif // defined(__linux__)