Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | //===-- asan_asm_test.cc --------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "asan_test_utils.h" |
| 14 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 15 | #if defined(__linux__) |
| 16 | |
| 17 | #if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) |
| 18 | |
| 19 | #include <emmintrin.h> |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | template<typename T> void asm_write(T *ptr, T val); |
| 24 | template<typename T> T asm_read(T *ptr); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 25 | template<typename T> void asm_rep_movs(T *dst, T *src, size_t n); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 26 | |
| 27 | } // End of anonymous namespace |
| 28 | |
| 29 | #endif // defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) |
| 30 | |
| 31 | #if defined(__x86_64__) |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | #define DECLARE_ASM_WRITE(Type, Size, Mov, Reg) \ |
| 36 | template<> void asm_write<Type>(Type *ptr, Type val) { \ |
| 37 | __asm__( \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 38 | Mov " %[val], (%[ptr]) \n\t" \ |
| 39 | : \ |
| 40 | : [ptr] "r" (ptr), [val] Reg (val) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 41 | : "memory" \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 42 | ); \ |
| 43 | } |
| 44 | |
| 45 | #define DECLARE_ASM_READ(Type, Size, Mov, Reg) \ |
| 46 | template<> Type asm_read<Type>(Type *ptr) { \ |
| 47 | Type res; \ |
| 48 | __asm__( \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 49 | Mov " (%[ptr]), %[res] \n\t" \ |
| 50 | : [res] Reg (res) \ |
| 51 | : [ptr] "r" (ptr) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 52 | : "memory" \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 53 | ); \ |
| 54 | return res; \ |
| 55 | } |
| 56 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 57 | #define DECLARE_ASM_REP_MOVS(Type, Movs) \ |
| 58 | template <> void asm_rep_movs<Type>(Type * dst, Type * src, size_t size) { \ |
| 59 | __asm__("rep " Movs " \n\t" \ |
| 60 | : \ |
| 61 | : "D"(dst), "S"(src), "c"(size) \ |
| 62 | : "rsi", "rdi", "rcx", "memory"); \ |
| 63 | } |
| 64 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 65 | DECLARE_ASM_WRITE(U8, "8", "movq", "r"); |
| 66 | DECLARE_ASM_READ(U8, "8", "movq", "=r"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 67 | DECLARE_ASM_REP_MOVS(U8, "movsq"); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 68 | |
| 69 | } // End of anonymous namespace |
| 70 | |
| 71 | #endif // defined(__x86_64__) |
| 72 | |
| 73 | #if defined(__i386__) && defined(__SSE2__) |
| 74 | |
| 75 | namespace { |
| 76 | |
| 77 | #define DECLARE_ASM_WRITE(Type, Size, Mov, Reg) \ |
| 78 | template<> void asm_write<Type>(Type *ptr, Type val) { \ |
| 79 | __asm__( \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 80 | Mov " %[val], (%[ptr]) \n\t" \ |
| 81 | : \ |
| 82 | : [ptr] "r" (ptr), [val] Reg (val) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 83 | : "memory" \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 84 | ); \ |
| 85 | } |
| 86 | |
| 87 | #define DECLARE_ASM_READ(Type, Size, Mov, Reg) \ |
| 88 | template<> Type asm_read<Type>(Type *ptr) { \ |
| 89 | Type res; \ |
| 90 | __asm__( \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 91 | Mov " (%[ptr]), %[res] \n\t" \ |
| 92 | : [res] Reg (res) \ |
| 93 | : [ptr] "r" (ptr) \ |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 94 | : "memory" \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 95 | ); \ |
| 96 | return res; \ |
| 97 | } |
| 98 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 99 | #define DECLARE_ASM_REP_MOVS(Type, Movs) \ |
| 100 | template <> void asm_rep_movs<Type>(Type * dst, Type * src, size_t size) { \ |
| 101 | __asm__("rep " Movs " \n\t" \ |
| 102 | : \ |
| 103 | : "D"(dst), "S"(src), "c"(size) \ |
| 104 | : "esi", "edi", "ecx", "memory"); \ |
| 105 | } |
| 106 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 107 | } // End of anonymous namespace |
| 108 | |
| 109 | #endif // defined(__i386__) && defined(__SSE2__) |
| 110 | |
| 111 | #if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) |
| 112 | |
| 113 | namespace { |
| 114 | |
| 115 | DECLARE_ASM_WRITE(U1, "1", "movb", "r"); |
| 116 | DECLARE_ASM_WRITE(U2, "2", "movw", "r"); |
| 117 | DECLARE_ASM_WRITE(U4, "4", "movl", "r"); |
| 118 | DECLARE_ASM_WRITE(__m128i, "16", "movaps", "x"); |
| 119 | |
| 120 | DECLARE_ASM_READ(U1, "1", "movb", "=r"); |
| 121 | DECLARE_ASM_READ(U2, "2", "movw", "=r"); |
| 122 | DECLARE_ASM_READ(U4, "4", "movl", "=r"); |
| 123 | DECLARE_ASM_READ(__m128i, "16", "movaps", "=x"); |
| 124 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 125 | DECLARE_ASM_REP_MOVS(U1, "movsb"); |
| 126 | DECLARE_ASM_REP_MOVS(U2, "movsw"); |
| 127 | DECLARE_ASM_REP_MOVS(U4, "movsl"); |
| 128 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 129 | template<typename T> void TestAsmWrite(const char *DeathPattern) { |
| 130 | T *buf = new T; |
| 131 | EXPECT_DEATH(asm_write(&buf[1], static_cast<T>(0)), DeathPattern); |
| 132 | T var = 0x12; |
| 133 | asm_write(&var, static_cast<T>(0x21)); |
| 134 | ASSERT_EQ(static_cast<T>(0x21), var); |
| 135 | delete buf; |
| 136 | } |
| 137 | |
| 138 | template<> void TestAsmWrite<__m128i>(const char *DeathPattern) { |
| 139 | char *buf = new char[16]; |
| 140 | char *p = buf + 16; |
| 141 | if (((uintptr_t) p % 16) != 0) |
| 142 | p = buf + 8; |
| 143 | assert(((uintptr_t) p % 16) == 0); |
| 144 | __m128i val = _mm_set1_epi16(0x1234); |
| 145 | EXPECT_DEATH(asm_write<__m128i>((__m128i*) p, val), DeathPattern); |
| 146 | __m128i var = _mm_set1_epi16(0x4321); |
| 147 | asm_write(&var, val); |
| 148 | ASSERT_EQ(0x1234, _mm_extract_epi16(var, 0)); |
| 149 | delete [] buf; |
| 150 | } |
| 151 | |
| 152 | template<typename T> void TestAsmRead(const char *DeathPattern) { |
| 153 | T *buf = new T; |
| 154 | EXPECT_DEATH(asm_read(&buf[1]), DeathPattern); |
| 155 | T var = 0x12; |
| 156 | ASSERT_EQ(static_cast<T>(0x12), asm_read(&var)); |
| 157 | delete buf; |
| 158 | } |
| 159 | |
| 160 | template<> void TestAsmRead<__m128i>(const char *DeathPattern) { |
| 161 | char *buf = new char[16]; |
| 162 | char *p = buf + 16; |
| 163 | if (((uintptr_t) p % 16) != 0) |
| 164 | p = buf + 8; |
| 165 | assert(((uintptr_t) p % 16) == 0); |
| 166 | EXPECT_DEATH(asm_read<__m128i>((__m128i*) p), DeathPattern); |
| 167 | __m128i val = _mm_set1_epi16(0x1234); |
| 168 | ASSERT_EQ(0x1234, _mm_extract_epi16(asm_read(&val), 0)); |
| 169 | delete [] buf; |
| 170 | } |
| 171 | |
| 172 | U4 AsmLoad(U4 *a) { |
| 173 | U4 r; |
| 174 | __asm__("movl (%[a]), %[r] \n\t" : [r] "=r" (r) : [a] "r" (a) : "memory"); |
| 175 | return r; |
| 176 | } |
| 177 | |
| 178 | void AsmStore(U4 r, U4 *a) { |
| 179 | __asm__("movl %[r], (%[a]) \n\t" : : [a] "r" (a), [r] "r" (r) : "memory"); |
| 180 | } |
| 181 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 182 | template <typename T> |
| 183 | void TestAsmRepMovs(const char *DeathPatternRead, |
| 184 | const char *DeathPatternWrite) { |
| 185 | T src_good[4] = { 0x0, 0x1, 0x2, 0x3 }; |
| 186 | T dst_good[4] = {}; |
| 187 | asm_rep_movs(dst_good, src_good, 4); |
| 188 | ASSERT_EQ(static_cast<T>(0x0), dst_good[0]); |
| 189 | ASSERT_EQ(static_cast<T>(0x1), dst_good[1]); |
| 190 | ASSERT_EQ(static_cast<T>(0x2), dst_good[2]); |
| 191 | ASSERT_EQ(static_cast<T>(0x3), dst_good[3]); |
| 192 | |
| 193 | T dst_bad[3]; |
| 194 | EXPECT_DEATH(asm_rep_movs(dst_bad, src_good, 4), DeathPatternWrite); |
| 195 | |
| 196 | T src_bad[3] = { 0x0, 0x1, 0x2 }; |
| 197 | EXPECT_DEATH(asm_rep_movs(dst_good, src_bad, 4), DeathPatternRead); |
| 198 | |
| 199 | T* dp = dst_bad + 4; |
| 200 | T* sp = src_bad + 4; |
| 201 | asm_rep_movs(dp, sp, 0); |
| 202 | } |
| 203 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 204 | } // End of anonymous namespace |
| 205 | |
| 206 | TEST(AddressSanitizer, asm_load_store) { |
| 207 | U4* buf = new U4[2]; |
| 208 | EXPECT_DEATH(AsmLoad(&buf[3]), "READ of size 4"); |
| 209 | EXPECT_DEATH(AsmStore(0x1234, &buf[3]), "WRITE of size 4"); |
| 210 | delete [] buf; |
| 211 | } |
| 212 | |
| 213 | TEST(AddressSanitizer, asm_rw) { |
| 214 | TestAsmWrite<U1>("WRITE of size 1"); |
| 215 | TestAsmWrite<U2>("WRITE of size 2"); |
| 216 | TestAsmWrite<U4>("WRITE of size 4"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 217 | #if defined(__x86_64__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 218 | TestAsmWrite<U8>("WRITE of size 8"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 219 | #endif // defined(__x86_64__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 220 | TestAsmWrite<__m128i>("WRITE of size 16"); |
| 221 | |
| 222 | TestAsmRead<U1>("READ of size 1"); |
| 223 | TestAsmRead<U2>("READ of size 2"); |
| 224 | TestAsmRead<U4>("READ of size 4"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 225 | #if defined(__x86_64__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 226 | TestAsmRead<U8>("READ of size 8"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 227 | #endif // defined(__x86_64__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 228 | TestAsmRead<__m128i>("READ of size 16"); |
| 229 | } |
| 230 | |
| 231 | TEST(AddressSanitizer, asm_flags) { |
| 232 | long magic = 0x1234; |
| 233 | long r = 0x0; |
| 234 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 235 | #if defined(__x86_64__) && !defined(__ILP32__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 236 | __asm__("xorq %%rax, %%rax \n\t" |
| 237 | "movq (%[p]), %%rax \n\t" |
| 238 | "sete %%al \n\t" |
| 239 | "movzbq %%al, %[r] \n\t" |
| 240 | : [r] "=r"(r) |
| 241 | : [p] "r"(&magic) |
| 242 | : "rax", "memory"); |
| 243 | #else |
| 244 | __asm__("xorl %%eax, %%eax \n\t" |
| 245 | "movl (%[p]), %%eax \n\t" |
| 246 | "sete %%al \n\t" |
| 247 | "movzbl %%al, %[r] \n\t" |
| 248 | : [r] "=r"(r) |
| 249 | : [p] "r"(&magic) |
| 250 | : "eax", "memory"); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 251 | #endif // defined(__x86_64__) && !defined(__ILP32__) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 252 | |
| 253 | ASSERT_EQ(0x1, r); |
| 254 | } |
| 255 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 256 | TEST(AddressSanitizer, asm_rep_movs) { |
| 257 | TestAsmRepMovs<U1>("READ of size 1", "WRITE of size 1"); |
| 258 | TestAsmRepMovs<U2>("READ of size 2", "WRITE of size 2"); |
| 259 | TestAsmRepMovs<U4>("READ of size 4", "WRITE of size 4"); |
| 260 | #if defined(__x86_64__) |
| 261 | TestAsmRepMovs<U8>("READ of size 8", "WRITE of size 8"); |
| 262 | #endif // defined(__x86_64__) |
| 263 | } |
| 264 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 265 | #endif // defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) |
| 266 | |
| 267 | #endif // defined(__linux__) |