Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1 | //===-- msan_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 MemorySanitizer. |
| 11 | // |
| 12 | // MemorySanitizer unit tests. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "sanitizer/msan_interface.h" |
| 16 | #include "msandr_test_so.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdarg.h> |
| 21 | #include <stdio.h> |
| 22 | #include <assert.h> |
| 23 | #include <wchar.h> |
| 24 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 25 | #include <dlfcn.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <limits.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <sys/resource.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <sys/utsname.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/vfs.h> |
| 37 | |
| 38 | #if defined(__i386__) || defined(__x86_64__) |
| 39 | # include <emmintrin.h> |
| 40 | # define MSAN_HAS_M128 1 |
| 41 | #else |
| 42 | # define MSAN_HAS_M128 0 |
| 43 | #endif |
| 44 | |
| 45 | typedef unsigned char U1; |
| 46 | typedef unsigned short U2; // NOLINT |
| 47 | typedef unsigned int U4; |
| 48 | typedef unsigned long long U8; // NOLINT |
| 49 | typedef signed char S1; |
| 50 | typedef signed short S2; // NOLINT |
| 51 | typedef signed int S4; |
| 52 | typedef signed long long S8; // NOLINT |
| 53 | #define NOINLINE __attribute__((noinline)) |
| 54 | #define INLINE __attribute__((always_inline)) |
| 55 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 56 | static bool TrackingOrigins() { |
| 57 | S8 x; |
| 58 | __msan_set_origin(&x, sizeof(x), 0x1234); |
| 59 | u32 origin = __msan_get_origin(&x); |
| 60 | __msan_set_origin(&x, sizeof(x), 0); |
| 61 | return origin == 0x1234; |
| 62 | } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 63 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 64 | #define EXPECT_UMR(action) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 65 | do { \ |
| 66 | __msan_set_expect_umr(1); \ |
| 67 | action; \ |
| 68 | __msan_set_expect_umr(0); \ |
| 69 | } while (0) |
| 70 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 71 | #define EXPECT_UMR_O(action, origin) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 72 | do { \ |
| 73 | __msan_set_expect_umr(1); \ |
| 74 | action; \ |
| 75 | __msan_set_expect_umr(0); \ |
| 76 | if (TrackingOrigins()) \ |
| 77 | EXPECT_EQ(origin, __msan_get_origin_tls()); \ |
| 78 | } while (0) |
| 79 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 80 | #define EXPECT_UMR_S(action, stack_origin) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 81 | do { \ |
| 82 | __msan_set_expect_umr(1); \ |
| 83 | action; \ |
| 84 | __msan_set_expect_umr(0); \ |
| 85 | u32 id = __msan_get_origin_tls(); \ |
| 86 | const char *str = __msan_get_origin_descr_if_stack(id); \ |
| 87 | if (!str || strcmp(str, stack_origin)) { \ |
| 88 | fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", \ |
| 89 | id, stack_origin, str); \ |
| 90 | EXPECT_EQ(1, 0); \ |
| 91 | } \ |
| 92 | } while (0) |
| 93 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 94 | #define EXPECT_POISONED(x) ExpectPoisoned(x) |
| 95 | |
| 96 | template<typename T> |
| 97 | void ExpectPoisoned(const T& t) { |
| 98 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 99 | } |
| 100 | |
| 101 | #define EXPECT_POISONED_O(x, origin) \ |
| 102 | ExpectPoisonedWithOrigin(x, origin) |
| 103 | |
| 104 | template<typename T> |
| 105 | void ExpectPoisonedWithOrigin(const T& t, unsigned origin) { |
| 106 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 107 | if (TrackingOrigins()) |
| 108 | EXPECT_EQ(origin, __msan_get_origin((void*)&t)); |
| 109 | } |
| 110 | |
| 111 | #define EXPECT_POISONED_S(x, stack_origin) \ |
| 112 | ExpectPoisonedWithStackOrigin(x, stack_origin) |
| 113 | |
| 114 | template<typename T> |
| 115 | void ExpectPoisonedWithStackOrigin(const T& t, const char *stack_origin) { |
| 116 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 117 | u32 id = __msan_get_origin((void*)&t); |
| 118 | const char *str = __msan_get_origin_descr_if_stack(id); |
| 119 | if (!str || strcmp(str, stack_origin)) { |
| 120 | fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", |
| 121 | id, stack_origin, str); |
| 122 | EXPECT_EQ(1, 0); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #define EXPECT_NOT_POISONED(x) ExpectNotPoisoned(x) |
| 127 | |
| 128 | template<typename T> |
| 129 | void ExpectNotPoisoned(const T& t) { |
| 130 | EXPECT_EQ(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 131 | } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 132 | |
| 133 | static U8 poisoned_array[100]; |
| 134 | template<class T> |
| 135 | T *GetPoisoned(int i = 0, T val = 0) { |
| 136 | T *res = (T*)&poisoned_array[i]; |
| 137 | *res = val; |
| 138 | __msan_poison(&poisoned_array[i], sizeof(T)); |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | template<class T> |
| 143 | T *GetPoisonedO(int i, u32 origin, T val = 0) { |
| 144 | T *res = (T*)&poisoned_array[i]; |
| 145 | *res = val; |
| 146 | __msan_poison(&poisoned_array[i], sizeof(T)); |
| 147 | __msan_set_origin(&poisoned_array[i], sizeof(T), origin); |
| 148 | return res; |
| 149 | } |
| 150 | |
| 151 | // This function returns its parameter but in such a way that compiler |
| 152 | // can not prove it. |
| 153 | template<class T> |
| 154 | NOINLINE |
| 155 | static T Ident(T t) { |
| 156 | volatile T ret = t; |
| 157 | return ret; |
| 158 | } |
| 159 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 160 | template<class T> NOINLINE T ReturnPoisoned() { return *GetPoisoned<T>(); } |
| 161 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 162 | static volatile int g_one = 1; |
| 163 | static volatile int g_zero = 0; |
| 164 | static volatile int g_0 = 0; |
| 165 | static volatile int g_1 = 1; |
| 166 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 167 | S4 a_s4[100]; |
| 168 | S8 a_s8[100]; |
| 169 | |
| 170 | TEST(MemorySanitizer, NegativeTest1) { |
| 171 | S4 *x = GetPoisoned<S4>(); |
| 172 | if (g_one) |
| 173 | *x = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 174 | EXPECT_NOT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST(MemorySanitizer, PositiveTest1) { |
| 178 | // Load to store. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 179 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 180 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 181 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 182 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 183 | |
| 184 | // S->S conversions. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 185 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 186 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 187 | EXPECT_POISONED(*GetPoisoned<S1>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 188 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 189 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 190 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 191 | EXPECT_POISONED(*GetPoisoned<S2>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 192 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 193 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 194 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 195 | EXPECT_POISONED(*GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 196 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 197 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 198 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 199 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 200 | |
| 201 | // ZExt |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 202 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 203 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 204 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 205 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 206 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 207 | EXPECT_POISONED(*GetPoisoned<U4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 208 | |
| 209 | // Unary ops. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 210 | EXPECT_POISONED(- *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 211 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 212 | EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | a_s4[g_zero] = 1 - *GetPoisoned<S4>(); |
| 216 | a_s4[g_zero] = 1 + *GetPoisoned<S4>(); |
| 217 | } |
| 218 | |
| 219 | TEST(MemorySanitizer, Phi1) { |
| 220 | S4 c; |
| 221 | if (g_one) { |
| 222 | c = *GetPoisoned<S4>(); |
| 223 | } else { |
| 224 | __msan_break_optimization(0); |
| 225 | c = 0; |
| 226 | } |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 227 | EXPECT_POISONED(c); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | TEST(MemorySanitizer, Phi2) { |
| 231 | S4 i = *GetPoisoned<S4>(); |
| 232 | S4 n = g_one; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 233 | EXPECT_UMR(for (; i < g_one; i++);); |
| 234 | EXPECT_POISONED(i); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 237 | NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); } |
| 238 | NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); } |
| 239 | NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 240 | |
| 241 | TEST(MemorySanitizer, ArgTest) { |
| 242 | Arg1ExpectUMR(*GetPoisoned<S4>()); |
| 243 | Arg2ExpectUMR(0, *GetPoisoned<S4>()); |
| 244 | Arg3ExpectUMR(0, 1, *GetPoisoned<S8>()); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | TEST(MemorySanitizer, CallAndRet) { |
| 249 | if (!__msan_has_dynamic_component()) return; |
| 250 | ReturnPoisoned<S1>(); |
| 251 | ReturnPoisoned<S2>(); |
| 252 | ReturnPoisoned<S4>(); |
| 253 | ReturnPoisoned<S8>(); |
| 254 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 255 | EXPECT_POISONED(ReturnPoisoned<S1>()); |
| 256 | EXPECT_POISONED(ReturnPoisoned<S2>()); |
| 257 | EXPECT_POISONED(ReturnPoisoned<S4>()); |
| 258 | EXPECT_POISONED(ReturnPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // malloc() in the following test may be optimized to produce a compile-time |
| 262 | // undef value. Check that we trap on the volatile assignment anyway. |
| 263 | TEST(MemorySanitizer, DISABLED_MallocNoIdent) { |
| 264 | S4 *x = (int*)malloc(sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 265 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 266 | free(x); |
| 267 | } |
| 268 | |
| 269 | TEST(MemorySanitizer, Malloc) { |
| 270 | S4 *x = (int*)Ident(malloc(sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 271 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 272 | free(x); |
| 273 | } |
| 274 | |
| 275 | TEST(MemorySanitizer, Realloc) { |
| 276 | S4 *x = (int*)Ident(realloc(0, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 277 | EXPECT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 278 | x[0] = 1; |
| 279 | x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 280 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 281 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 282 | x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 283 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 284 | EXPECT_POISONED(x[2]); |
| 285 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 286 | x[2] = 1; // Init this here. Check that after realloc it is poisoned again. |
| 287 | x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 288 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 289 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 290 | x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 291 | EXPECT_POISONED(x[1]); |
| 292 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 293 | free(x); |
| 294 | } |
| 295 | |
| 296 | TEST(MemorySanitizer, Calloc) { |
| 297 | S4 *x = (int*)Ident(calloc(1, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 298 | EXPECT_NOT_POISONED(*x); // Should not be poisoned. |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 299 | // EXPECT_EQ(0, *x); |
| 300 | free(x); |
| 301 | } |
| 302 | |
| 303 | TEST(MemorySanitizer, AndOr) { |
| 304 | U4 *p = GetPoisoned<U4>(); |
| 305 | // We poison two bytes in the midle of a 4-byte word to make the test |
| 306 | // correct regardless of endianness. |
| 307 | ((U1*)p)[1] = 0; |
| 308 | ((U1*)p)[2] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 309 | EXPECT_NOT_POISONED(*p & 0x00ffff00); |
| 310 | EXPECT_NOT_POISONED(*p & 0x00ff0000); |
| 311 | EXPECT_NOT_POISONED(*p & 0x0000ff00); |
| 312 | EXPECT_POISONED(*p & 0xff000000); |
| 313 | EXPECT_POISONED(*p & 0x000000ff); |
| 314 | EXPECT_POISONED(*p & 0x0000ffff); |
| 315 | EXPECT_POISONED(*p & 0xffff0000); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 316 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 317 | EXPECT_NOT_POISONED(*p | 0xff0000ff); |
| 318 | EXPECT_NOT_POISONED(*p | 0xff00ffff); |
| 319 | EXPECT_NOT_POISONED(*p | 0xffff00ff); |
| 320 | EXPECT_POISONED(*p | 0xff000000); |
| 321 | EXPECT_POISONED(*p | 0x000000ff); |
| 322 | EXPECT_POISONED(*p | 0x0000ffff); |
| 323 | EXPECT_POISONED(*p | 0xffff0000); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 324 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 325 | EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | template<class T> |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 329 | static bool applyNot(T value, T shadow) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 330 | __msan_partial_poison(&value, &shadow, sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 331 | return !value; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | TEST(MemorySanitizer, Not) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 335 | EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0)); |
| 336 | EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0)); |
| 337 | EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF)); |
| 338 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF)); |
| 339 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF)); |
| 340 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF)); |
| 341 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000)); |
| 342 | EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000)); |
| 343 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000)); |
| 344 | EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 345 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 346 | EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0)); |
| 347 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE)); |
| 348 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0)); |
| 349 | EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 350 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 351 | EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1))); |
| 352 | EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2))); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST(MemorySanitizer, Shift) { |
| 356 | U4 *up = GetPoisoned<U4>(); |
| 357 | ((U1*)up)[0] = 0; |
| 358 | ((U1*)up)[3] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 359 | EXPECT_NOT_POISONED(*up >> 30); |
| 360 | EXPECT_NOT_POISONED(*up >> 24); |
| 361 | EXPECT_POISONED(*up >> 23); |
| 362 | EXPECT_POISONED(*up >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 363 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 364 | EXPECT_NOT_POISONED(*up << 30); |
| 365 | EXPECT_NOT_POISONED(*up << 24); |
| 366 | EXPECT_POISONED(*up << 23); |
| 367 | EXPECT_POISONED(*up << 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 368 | |
| 369 | S4 *sp = (S4*)up; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 370 | EXPECT_NOT_POISONED(*sp >> 30); |
| 371 | EXPECT_NOT_POISONED(*sp >> 24); |
| 372 | EXPECT_POISONED(*sp >> 23); |
| 373 | EXPECT_POISONED(*sp >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 374 | |
| 375 | sp = GetPoisoned<S4>(); |
| 376 | ((S1*)sp)[1] = 0; |
| 377 | ((S1*)sp)[2] = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 378 | EXPECT_POISONED(*sp >> 31); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 379 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 380 | EXPECT_POISONED(100 >> *GetPoisoned<S4>()); |
| 381 | EXPECT_POISONED(100U >> *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | NOINLINE static int GetPoisonedZero() { |
| 385 | int *zero = new int; |
| 386 | *zero = 0; |
| 387 | __msan_poison(zero, sizeof(*zero)); |
| 388 | int res = *zero; |
| 389 | delete zero; |
| 390 | return res; |
| 391 | } |
| 392 | |
| 393 | TEST(MemorySanitizer, LoadFromDirtyAddress) { |
| 394 | int *a = new int; |
| 395 | *a = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 396 | EXPECT_UMR(__msan_break_optimization((void*)(U8)a[GetPoisonedZero()])); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 397 | delete a; |
| 398 | } |
| 399 | |
| 400 | TEST(MemorySanitizer, StoreToDirtyAddress) { |
| 401 | int *a = new int; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 402 | EXPECT_UMR(a[GetPoisonedZero()] = 0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 403 | __msan_break_optimization(a); |
| 404 | delete a; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | NOINLINE void StackTestFunc() { |
| 409 | S4 p4; |
| 410 | S4 ok4 = 1; |
| 411 | S2 p2; |
| 412 | S2 ok2 = 1; |
| 413 | S1 p1; |
| 414 | S1 ok1 = 1; |
| 415 | __msan_break_optimization(&p4); |
| 416 | __msan_break_optimization(&ok4); |
| 417 | __msan_break_optimization(&p2); |
| 418 | __msan_break_optimization(&ok2); |
| 419 | __msan_break_optimization(&p1); |
| 420 | __msan_break_optimization(&ok1); |
| 421 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 422 | EXPECT_POISONED(p4); |
| 423 | EXPECT_POISONED(p2); |
| 424 | EXPECT_POISONED(p1); |
| 425 | EXPECT_NOT_POISONED(ok1); |
| 426 | EXPECT_NOT_POISONED(ok2); |
| 427 | EXPECT_NOT_POISONED(ok4); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | TEST(MemorySanitizer, StackTest) { |
| 431 | StackTestFunc(); |
| 432 | } |
| 433 | |
| 434 | NOINLINE void StackStressFunc() { |
| 435 | int foo[10000]; |
| 436 | __msan_break_optimization(foo); |
| 437 | } |
| 438 | |
| 439 | TEST(MemorySanitizer, DISABLED_StackStressTest) { |
| 440 | for (int i = 0; i < 1000000; i++) |
| 441 | StackStressFunc(); |
| 442 | } |
| 443 | |
| 444 | template<class T> |
| 445 | void TestFloatingPoint() { |
| 446 | static volatile T v; |
| 447 | static T g[100]; |
| 448 | __msan_break_optimization(&g); |
| 449 | T *x = GetPoisoned<T>(); |
| 450 | T *y = GetPoisoned<T>(1); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 451 | EXPECT_POISONED(*x); |
| 452 | EXPECT_POISONED((long long)*x); |
| 453 | EXPECT_POISONED((int)*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 454 | g[0] = *x; |
| 455 | g[1] = *x + *y; |
| 456 | g[2] = *x - *y; |
| 457 | g[3] = *x * *y; |
| 458 | } |
| 459 | |
| 460 | TEST(MemorySanitizer, FloatingPointTest) { |
| 461 | TestFloatingPoint<float>(); |
| 462 | TestFloatingPoint<double>(); |
| 463 | } |
| 464 | |
| 465 | TEST(MemorySanitizer, DynMem) { |
| 466 | S4 x = 0; |
| 467 | S4 *y = GetPoisoned<S4>(); |
| 468 | memcpy(y, &x, g_one * sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 469 | EXPECT_NOT_POISONED(*y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static char *DynRetTestStr; |
| 473 | |
| 474 | TEST(MemorySanitizer, DynRet) { |
| 475 | if (!__msan_has_dynamic_component()) return; |
| 476 | ReturnPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 477 | EXPECT_NOT_POISONED(clearenv()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | TEST(MemorySanitizer, DynRet1) { |
| 482 | if (!__msan_has_dynamic_component()) return; |
| 483 | ReturnPoisoned<S8>(); |
| 484 | } |
| 485 | |
| 486 | struct LargeStruct { |
| 487 | S4 x[10]; |
| 488 | }; |
| 489 | |
| 490 | NOINLINE |
| 491 | LargeStruct LargeRetTest() { |
| 492 | LargeStruct res; |
| 493 | res.x[0] = *GetPoisoned<S4>(); |
| 494 | res.x[1] = *GetPoisoned<S4>(); |
| 495 | res.x[2] = *GetPoisoned<S4>(); |
| 496 | res.x[3] = *GetPoisoned<S4>(); |
| 497 | res.x[4] = *GetPoisoned<S4>(); |
| 498 | res.x[5] = *GetPoisoned<S4>(); |
| 499 | res.x[6] = *GetPoisoned<S4>(); |
| 500 | res.x[7] = *GetPoisoned<S4>(); |
| 501 | res.x[8] = *GetPoisoned<S4>(); |
| 502 | res.x[9] = *GetPoisoned<S4>(); |
| 503 | return res; |
| 504 | } |
| 505 | |
| 506 | TEST(MemorySanitizer, LargeRet) { |
| 507 | LargeStruct a = LargeRetTest(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 508 | EXPECT_POISONED(a.x[0]); |
| 509 | EXPECT_POISONED(a.x[9]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | TEST(MemorySanitizer, fread) { |
| 513 | char *x = new char[32]; |
| 514 | FILE *f = fopen("/proc/self/stat", "r"); |
| 515 | assert(f); |
| 516 | fread(x, 1, 32, f); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 517 | EXPECT_NOT_POISONED(x[0]); |
| 518 | EXPECT_NOT_POISONED(x[16]); |
| 519 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 520 | fclose(f); |
| 521 | delete x; |
| 522 | } |
| 523 | |
| 524 | TEST(MemorySanitizer, read) { |
| 525 | char *x = new char[32]; |
| 526 | int fd = open("/proc/self/stat", O_RDONLY); |
| 527 | assert(fd > 0); |
| 528 | int sz = read(fd, x, 32); |
| 529 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 530 | EXPECT_NOT_POISONED(x[0]); |
| 531 | EXPECT_NOT_POISONED(x[16]); |
| 532 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 533 | close(fd); |
| 534 | delete x; |
| 535 | } |
| 536 | |
| 537 | TEST(MemorySanitizer, pread) { |
| 538 | char *x = new char[32]; |
| 539 | int fd = open("/proc/self/stat", O_RDONLY); |
| 540 | assert(fd > 0); |
| 541 | int sz = pread(fd, x, 32, 0); |
| 542 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 543 | EXPECT_NOT_POISONED(x[0]); |
| 544 | EXPECT_NOT_POISONED(x[16]); |
| 545 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 546 | close(fd); |
| 547 | delete x; |
| 548 | } |
| 549 | |
| 550 | // FIXME: fails now. |
| 551 | TEST(MemorySanitizer, DISABLED_ioctl) { |
| 552 | struct winsize ws; |
| 553 | EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 554 | EXPECT_NOT_POISONED(ws.ws_col); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | TEST(MemorySanitizer, readlink) { |
| 558 | char *x = new char[1000]; |
| 559 | readlink("/proc/self/exe", x, 1000); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 560 | EXPECT_NOT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 561 | delete [] x; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | TEST(MemorySanitizer, stat) { |
| 566 | struct stat* st = new struct stat; |
| 567 | int res = stat("/proc/self/stat", st); |
| 568 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 569 | EXPECT_NOT_POISONED(st->st_dev); |
| 570 | EXPECT_NOT_POISONED(st->st_mode); |
| 571 | EXPECT_NOT_POISONED(st->st_size); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | TEST(MemorySanitizer, statfs) { |
| 575 | struct statfs* st = new struct statfs; |
| 576 | int res = statfs("/", st); |
| 577 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 578 | EXPECT_NOT_POISONED(st->f_type); |
| 579 | EXPECT_NOT_POISONED(st->f_bfree); |
| 580 | EXPECT_NOT_POISONED(st->f_namelen); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | TEST(MemorySanitizer, pipe) { |
| 584 | int* pipefd = new int[2]; |
| 585 | int res = pipe(pipefd); |
| 586 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 587 | EXPECT_NOT_POISONED(pipefd[0]); |
| 588 | EXPECT_NOT_POISONED(pipefd[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 589 | close(pipefd[0]); |
| 590 | close(pipefd[1]); |
| 591 | } |
| 592 | |
| 593 | TEST(MemorySanitizer, getcwd) { |
| 594 | char path[PATH_MAX + 1]; |
| 595 | char* res = getcwd(path, sizeof(path)); |
| 596 | assert(res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 597 | EXPECT_NOT_POISONED(path[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | TEST(MemorySanitizer, realpath) { |
| 601 | const char* relpath = "."; |
| 602 | char path[PATH_MAX + 1]; |
| 603 | char* res = realpath(relpath, path); |
| 604 | assert(res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 605 | EXPECT_NOT_POISONED(path[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | TEST(MemorySanitizer, memcpy) { |
| 609 | char* x = new char[2]; |
| 610 | char* y = new char[2]; |
| 611 | x[0] = 1; |
| 612 | x[1] = *GetPoisoned<char>(); |
| 613 | memcpy(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 614 | EXPECT_NOT_POISONED(y[0]); |
| 615 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | TEST(MemorySanitizer, memmove) { |
| 619 | char* x = new char[2]; |
| 620 | char* y = new char[2]; |
| 621 | x[0] = 1; |
| 622 | x[1] = *GetPoisoned<char>(); |
| 623 | memmove(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 624 | EXPECT_NOT_POISONED(y[0]); |
| 625 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | TEST(MemorySanitizer, strdup) { |
| 629 | char *x = strdup("zzz"); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 630 | EXPECT_NOT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 631 | free(x); |
| 632 | } |
| 633 | |
| 634 | template<class T, int size> |
| 635 | void TestOverlapMemmove() { |
| 636 | T *x = new T[size]; |
| 637 | assert(size >= 3); |
| 638 | x[2] = 0; |
| 639 | memmove(x, x + 1, (size - 1) * sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 640 | EXPECT_NOT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 641 | if (!__msan_has_dynamic_component()) { |
| 642 | // FIXME: under DR we will lose this information |
| 643 | // because accesses in memmove will unpoisin the shadow. |
| 644 | // We need to use our own memove implementation instead of libc's. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 645 | EXPECT_POISONED(x[0]); |
| 646 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 647 | } |
| 648 | delete [] x; |
| 649 | } |
| 650 | |
| 651 | TEST(MemorySanitizer, overlap_memmove) { |
| 652 | TestOverlapMemmove<U1, 10>(); |
| 653 | TestOverlapMemmove<U1, 1000>(); |
| 654 | TestOverlapMemmove<U8, 4>(); |
| 655 | TestOverlapMemmove<U8, 1000>(); |
| 656 | } |
| 657 | |
| 658 | TEST(MemorySanitizer, strcpy) { // NOLINT |
| 659 | char* x = new char[3]; |
| 660 | char* y = new char[3]; |
| 661 | x[0] = 'a'; |
| 662 | x[1] = *GetPoisoned<char>(1, 1); |
| 663 | x[2] = 0; |
| 664 | strcpy(y, x); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 665 | EXPECT_NOT_POISONED(y[0]); |
| 666 | EXPECT_POISONED(y[1]); |
| 667 | EXPECT_NOT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | TEST(MemorySanitizer, strncpy) { // NOLINT |
| 671 | char* x = new char[3]; |
| 672 | char* y = new char[3]; |
| 673 | x[0] = 'a'; |
| 674 | x[1] = *GetPoisoned<char>(1, 1); |
| 675 | x[2] = 0; |
| 676 | strncpy(y, x, 2); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 677 | EXPECT_NOT_POISONED(y[0]); |
| 678 | EXPECT_POISONED(y[1]); |
| 679 | EXPECT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | TEST(MemorySanitizer, strtol) { |
| 683 | char *e; |
| 684 | assert(1 == strtol("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 685 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | TEST(MemorySanitizer, strtoll) { |
| 689 | char *e; |
| 690 | assert(1 == strtoll("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 691 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | TEST(MemorySanitizer, strtoul) { |
| 695 | char *e; |
| 696 | assert(1 == strtoul("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 697 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | TEST(MemorySanitizer, strtoull) { |
| 701 | char *e; |
| 702 | assert(1 == strtoull("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 703 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 706 | TEST(MemorySanitizer, strtod) { |
| 707 | char *e; |
| 708 | assert(0 != strtod("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 709 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | TEST(MemorySanitizer, strtof) { |
| 713 | char *e; |
| 714 | assert(0 != strtof("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 715 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | TEST(MemorySanitizer, strtold) { |
| 719 | char *e; |
| 720 | assert(0 != strtold("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 721 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 724 | TEST(MemorySanitizer, sprintf) { // NOLINT |
| 725 | char buff[10]; |
| 726 | __msan_break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 727 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 728 | int res = sprintf(buff, "%d", 1234567); // NOLINT |
| 729 | assert(res == 7); |
| 730 | assert(buff[0] == '1'); |
| 731 | assert(buff[1] == '2'); |
| 732 | assert(buff[2] == '3'); |
| 733 | assert(buff[6] == '7'); |
| 734 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 735 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | TEST(MemorySanitizer, snprintf) { |
| 739 | char buff[10]; |
| 740 | __msan_break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 741 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 742 | int res = snprintf(buff, sizeof(buff), "%d", 1234567); |
| 743 | assert(res == 7); |
| 744 | assert(buff[0] == '1'); |
| 745 | assert(buff[1] == '2'); |
| 746 | assert(buff[2] == '3'); |
| 747 | assert(buff[6] == '7'); |
| 748 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 749 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST(MemorySanitizer, swprintf) { |
| 753 | wchar_t buff[10]; |
| 754 | assert(sizeof(wchar_t) == 4); |
| 755 | __msan_break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 756 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 757 | int res = swprintf(buff, 9, L"%d", 1234567); |
| 758 | assert(res == 7); |
| 759 | assert(buff[0] == '1'); |
| 760 | assert(buff[1] == '2'); |
| 761 | assert(buff[2] == '3'); |
| 762 | assert(buff[6] == '7'); |
| 763 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 764 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | TEST(MemorySanitizer, wcstombs) { |
| 768 | const wchar_t *x = L"abc"; |
| 769 | char buff[10]; |
| 770 | int res = wcstombs(buff, x, 4); |
| 771 | EXPECT_EQ(res, 3); |
| 772 | EXPECT_EQ(buff[0], 'a'); |
| 773 | EXPECT_EQ(buff[1], 'b'); |
| 774 | EXPECT_EQ(buff[2], 'c'); |
| 775 | } |
| 776 | |
| 777 | TEST(MemorySanitizer, gettimeofday) { |
| 778 | struct timeval tv; |
| 779 | struct timezone tz; |
| 780 | __msan_break_optimization(&tv); |
| 781 | __msan_break_optimization(&tz); |
| 782 | assert(sizeof(tv) == 16); |
| 783 | assert(sizeof(tz) == 8); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 784 | EXPECT_POISONED(tv.tv_sec); |
| 785 | EXPECT_POISONED(tv.tv_usec); |
| 786 | EXPECT_POISONED(tz.tz_minuteswest); |
| 787 | EXPECT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 788 | assert(0 == gettimeofday(&tv, &tz)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 789 | EXPECT_NOT_POISONED(tv.tv_sec); |
| 790 | EXPECT_NOT_POISONED(tv.tv_usec); |
| 791 | EXPECT_NOT_POISONED(tz.tz_minuteswest); |
| 792 | EXPECT_NOT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | TEST(MemorySanitizer, mmap) { |
| 796 | const int size = 4096; |
| 797 | void *p1, *p2; |
| 798 | p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 799 | __msan_poison(p1, size); |
| 800 | munmap(p1, size); |
| 801 | for (int i = 0; i < 1000; i++) { |
| 802 | p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 803 | if (p2 == p1) |
| 804 | break; |
| 805 | else |
| 806 | munmap(p2, size); |
| 807 | } |
| 808 | if (p1 == p2) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 809 | EXPECT_NOT_POISONED(*(char*)p2); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 810 | munmap(p2, size); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // FIXME: enable and add ecvt. |
| 815 | // FIXME: check why msandr does nt handle fcvt. |
| 816 | TEST(MemorySanitizer, fcvt) { |
| 817 | int a, b; |
| 818 | __msan_break_optimization(&a); |
| 819 | __msan_break_optimization(&b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 820 | EXPECT_POISONED(a); |
| 821 | EXPECT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 822 | char *str = fcvt(12345.6789, 10, &a, &b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 823 | EXPECT_NOT_POISONED(a); |
| 824 | EXPECT_NOT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | TEST(MemorySanitizer, LoadUnpoisoned) { |
| 828 | S8 s = *GetPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 829 | EXPECT_POISONED(s); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 830 | S8 safe = *GetPoisoned<S8>(); |
| 831 | __msan_load_unpoisoned(&s, sizeof(s), &safe); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 832 | EXPECT_NOT_POISONED(safe); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | struct StructWithDtor { |
| 836 | ~StructWithDtor(); |
| 837 | }; |
| 838 | |
| 839 | NOINLINE StructWithDtor::~StructWithDtor() { |
| 840 | __msan_break_optimization(0); |
| 841 | } |
| 842 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 843 | TEST(MemorySanitizer, Invoke) { |
| 844 | StructWithDtor s; // Will cause the calls to become invokes. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 845 | EXPECT_NOT_POISONED(0); |
| 846 | EXPECT_POISONED(*GetPoisoned<int>()); |
| 847 | EXPECT_NOT_POISONED(0); |
| 848 | EXPECT_POISONED(*GetPoisoned<int>()); |
| 849 | EXPECT_POISONED(ReturnPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | TEST(MemorySanitizer, ptrtoint) { |
| 853 | // Test that shadow is propagated through pointer-to-integer conversion. |
| 854 | void* p = (void*)0xABCD; |
| 855 | __msan_poison(((char*)&p) + 1, sizeof(p)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 856 | EXPECT_NOT_POISONED((((uptr)p) & 0xFF) == 0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 857 | |
| 858 | void* q = (void*)0xABCD; |
| 859 | __msan_poison(&q, sizeof(q) - 1); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 860 | EXPECT_POISONED((((uptr)q) & 0xFF) == 0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void vaargsfn2(int guard, ...) { |
| 864 | va_list vl; |
| 865 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 866 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 867 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 868 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 869 | EXPECT_POISONED(va_arg(vl, double)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 870 | va_end(vl); |
| 871 | } |
| 872 | |
| 873 | static void vaargsfn(int guard, ...) { |
| 874 | va_list vl; |
| 875 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 876 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 877 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 878 | // The following call will overwrite __msan_param_tls. |
| 879 | // Checks after it test that arg shadow was somehow saved across the call. |
| 880 | vaargsfn2(1, 2, 3, 4, *GetPoisoned<double>()); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 881 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 882 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 883 | va_end(vl); |
| 884 | } |
| 885 | |
| 886 | TEST(MemorySanitizer, VAArgTest) { |
| 887 | int* x = GetPoisoned<int>(); |
| 888 | int* y = GetPoisoned<int>(4); |
| 889 | vaargsfn(1, 13, *x, 42, *y); |
| 890 | } |
| 891 | |
| 892 | static void vaargsfn_many(int guard, ...) { |
| 893 | va_list vl; |
| 894 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 895 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 896 | EXPECT_POISONED(va_arg(vl, int)); |
| 897 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 898 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 899 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 900 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 901 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 902 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 903 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 904 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 905 | va_end(vl); |
| 906 | } |
| 907 | |
| 908 | TEST(MemorySanitizer, VAArgManyTest) { |
| 909 | int* x = GetPoisoned<int>(); |
| 910 | int* y = GetPoisoned<int>(4); |
| 911 | vaargsfn_many(1, 2, *x, 3, 4, 5, 6, 7, 8, 9, *y); |
| 912 | } |
| 913 | |
| 914 | static void vaargsfn_pass2(va_list vl) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 915 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 916 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 917 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static void vaargsfn_pass(int guard, ...) { |
| 921 | va_list vl; |
| 922 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 923 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 924 | vaargsfn_pass2(vl); |
| 925 | va_end(vl); |
| 926 | } |
| 927 | |
| 928 | TEST(MemorySanitizer, VAArgPass) { |
| 929 | int* x = GetPoisoned<int>(); |
| 930 | int* y = GetPoisoned<int>(4); |
| 931 | vaargsfn_pass(1, *x, 2, 3, *y); |
| 932 | } |
| 933 | |
| 934 | static void vaargsfn_copy2(va_list vl) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 935 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 936 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static void vaargsfn_copy(int guard, ...) { |
| 940 | va_list vl; |
| 941 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 942 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 943 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 944 | va_list vl2; |
| 945 | va_copy(vl2, vl); |
| 946 | vaargsfn_copy2(vl2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 947 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 948 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 949 | va_end(vl); |
| 950 | } |
| 951 | |
| 952 | TEST(MemorySanitizer, VAArgCopy) { |
| 953 | int* x = GetPoisoned<int>(); |
| 954 | int* y = GetPoisoned<int>(4); |
| 955 | vaargsfn_copy(1, 2, *x, 3, *y); |
| 956 | } |
| 957 | |
| 958 | static void vaargsfn_ptr(int guard, ...) { |
| 959 | va_list vl; |
| 960 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 961 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| 962 | EXPECT_POISONED(va_arg(vl, int*)); |
| 963 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| 964 | EXPECT_POISONED(va_arg(vl, double*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 965 | va_end(vl); |
| 966 | } |
| 967 | |
| 968 | TEST(MemorySanitizer, VAArgPtr) { |
| 969 | int** x = GetPoisoned<int*>(); |
| 970 | double** y = GetPoisoned<double*>(8); |
| 971 | int z; |
| 972 | vaargsfn_ptr(1, &z, *x, &z, *y); |
| 973 | } |
| 974 | |
| 975 | static void vaargsfn_overflow(int guard, ...) { |
| 976 | va_list vl; |
| 977 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 978 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 979 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 980 | EXPECT_POISONED(va_arg(vl, int)); |
| 981 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 982 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 983 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 984 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 985 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 986 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 987 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 988 | EXPECT_POISONED(va_arg(vl, double)); |
| 989 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 990 | EXPECT_POISONED(va_arg(vl, int*)); |
| 991 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 992 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 993 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 994 | EXPECT_POISONED(va_arg(vl, int)); |
| 995 | EXPECT_POISONED(va_arg(vl, double)); |
| 996 | EXPECT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 997 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 998 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 999 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1000 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1001 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1002 | EXPECT_POISONED(va_arg(vl, int)); |
| 1003 | EXPECT_POISONED(va_arg(vl, double)); |
| 1004 | EXPECT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1005 | |
| 1006 | va_end(vl); |
| 1007 | } |
| 1008 | |
| 1009 | TEST(MemorySanitizer, VAArgOverflow) { |
| 1010 | int* x = GetPoisoned<int>(); |
| 1011 | double* y = GetPoisoned<double>(8); |
| 1012 | int** p = GetPoisoned<int*>(16); |
| 1013 | int z; |
| 1014 | vaargsfn_overflow(1, |
| 1015 | 1, 2, *x, 4, 5, 6, |
| 1016 | 1.1, 2.2, 3.3, *y, 5.5, *p, 7.7, 8.8, |
| 1017 | // the following args will overflow for sure |
| 1018 | *x, *y, *p, |
| 1019 | 7, 9.9, &z, |
| 1020 | *x, *y, *p); |
| 1021 | } |
| 1022 | |
| 1023 | static void vaargsfn_tlsoverwrite2(int guard, ...) { |
| 1024 | va_list vl; |
| 1025 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1026 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1027 | va_end(vl); |
| 1028 | } |
| 1029 | |
| 1030 | static void vaargsfn_tlsoverwrite(int guard, ...) { |
| 1031 | // This call will overwrite TLS contents unless it's backed up somewhere. |
| 1032 | vaargsfn_tlsoverwrite2(2, 42); |
| 1033 | va_list vl; |
| 1034 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1035 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1036 | va_end(vl); |
| 1037 | } |
| 1038 | |
| 1039 | TEST(MemorySanitizer, VAArgTLSOverwrite) { |
| 1040 | int* x = GetPoisoned<int>(); |
| 1041 | vaargsfn_tlsoverwrite(1, *x); |
| 1042 | } |
| 1043 | |
| 1044 | struct StructByVal { |
| 1045 | int a, b, c, d, e, f; |
| 1046 | }; |
| 1047 | |
| 1048 | NOINLINE void StructByValTestFunc(struct StructByVal s) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1049 | EXPECT_NOT_POISONED(s.a); |
| 1050 | EXPECT_POISONED(s.b); |
| 1051 | EXPECT_NOT_POISONED(s.c); |
| 1052 | EXPECT_POISONED(s.d); |
| 1053 | EXPECT_NOT_POISONED(s.e); |
| 1054 | EXPECT_POISONED(s.f); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | NOINLINE void StructByValTestFunc1(struct StructByVal s) { |
| 1058 | StructByValTestFunc(s); |
| 1059 | } |
| 1060 | |
| 1061 | NOINLINE void StructByValTestFunc2(int z, struct StructByVal s) { |
| 1062 | StructByValTestFunc(s); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(MemorySanitizer, StructByVal) { |
| 1066 | // Large aggregates are passed as "byval" pointer argument in LLVM. |
| 1067 | struct StructByVal s; |
| 1068 | s.a = 1; |
| 1069 | s.b = *GetPoisoned<int>(); |
| 1070 | s.c = 2; |
| 1071 | s.d = *GetPoisoned<int>(); |
| 1072 | s.e = 3; |
| 1073 | s.f = *GetPoisoned<int>(); |
| 1074 | StructByValTestFunc(s); |
| 1075 | StructByValTestFunc1(s); |
| 1076 | StructByValTestFunc2(0, s); |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | #if MSAN_HAS_M128 |
| 1081 | NOINLINE __m128i m128Eq(__m128i *a, __m128i *b) { return *a == *b; } |
| 1082 | NOINLINE __m128i m128Lt(__m128i *a, __m128i *b) { return *a < *b; } |
| 1083 | TEST(MemorySanitizer, m128) { |
| 1084 | __m128i a = _mm_set1_epi16(0x1234); |
| 1085 | __m128i b = _mm_set1_epi16(0x7890); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1086 | EXPECT_NOT_POISONED(m128Eq(&a, &b)); |
| 1087 | EXPECT_NOT_POISONED(m128Lt(&a, &b)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1088 | } |
| 1089 | // FIXME: add more tests for __m128i. |
| 1090 | #endif // MSAN_HAS_M128 |
| 1091 | |
| 1092 | // We should not complain when copying this poisoned hole. |
| 1093 | struct StructWithHole { |
| 1094 | U4 a; |
| 1095 | // 4-byte hole. |
| 1096 | U8 b; |
| 1097 | }; |
| 1098 | |
| 1099 | NOINLINE StructWithHole ReturnStructWithHole() { |
| 1100 | StructWithHole res; |
| 1101 | __msan_poison(&res, sizeof(res)); |
| 1102 | res.a = 1; |
| 1103 | res.b = 2; |
| 1104 | return res; |
| 1105 | } |
| 1106 | |
| 1107 | TEST(MemorySanitizer, StructWithHole) { |
| 1108 | StructWithHole a = ReturnStructWithHole(); |
| 1109 | __msan_break_optimization(&a); |
| 1110 | } |
| 1111 | |
| 1112 | template <class T> |
| 1113 | NOINLINE T ReturnStruct() { |
| 1114 | T res; |
| 1115 | __msan_poison(&res, sizeof(res)); |
| 1116 | res.a = 1; |
| 1117 | return res; |
| 1118 | } |
| 1119 | |
| 1120 | template <class T> |
| 1121 | NOINLINE void TestReturnStruct() { |
| 1122 | T s1 = ReturnStruct<T>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1123 | EXPECT_NOT_POISONED(s1.a); |
| 1124 | EXPECT_POISONED(s1.b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | struct SSS1 { |
| 1128 | int a, b, c; |
| 1129 | }; |
| 1130 | struct SSS2 { |
| 1131 | int b, a, c; |
| 1132 | }; |
| 1133 | struct SSS3 { |
| 1134 | int b, c, a; |
| 1135 | }; |
| 1136 | struct SSS4 { |
| 1137 | int c, b, a; |
| 1138 | }; |
| 1139 | |
| 1140 | struct SSS5 { |
| 1141 | int a; |
| 1142 | float b; |
| 1143 | }; |
| 1144 | struct SSS6 { |
| 1145 | int a; |
| 1146 | double b; |
| 1147 | }; |
| 1148 | struct SSS7 { |
| 1149 | S8 b; |
| 1150 | int a; |
| 1151 | }; |
| 1152 | struct SSS8 { |
| 1153 | S2 b; |
| 1154 | S8 a; |
| 1155 | }; |
| 1156 | |
| 1157 | TEST(MemorySanitizer, IntStruct3) { |
| 1158 | TestReturnStruct<SSS1>(); |
| 1159 | TestReturnStruct<SSS2>(); |
| 1160 | TestReturnStruct<SSS3>(); |
| 1161 | TestReturnStruct<SSS4>(); |
| 1162 | TestReturnStruct<SSS5>(); |
| 1163 | TestReturnStruct<SSS6>(); |
| 1164 | TestReturnStruct<SSS7>(); |
| 1165 | TestReturnStruct<SSS8>(); |
| 1166 | } |
| 1167 | |
| 1168 | struct LongStruct { |
| 1169 | U1 a1, b1; |
| 1170 | U2 a2, b2; |
| 1171 | U4 a4, b4; |
| 1172 | U8 a8, b8; |
| 1173 | }; |
| 1174 | |
| 1175 | NOINLINE LongStruct ReturnLongStruct1() { |
| 1176 | LongStruct res; |
| 1177 | __msan_poison(&res, sizeof(res)); |
| 1178 | res.a1 = res.a2 = res.a4 = res.a8 = 111; |
| 1179 | // leaves b1, .., b8 poisoned. |
| 1180 | return res; |
| 1181 | } |
| 1182 | |
| 1183 | NOINLINE LongStruct ReturnLongStruct2() { |
| 1184 | LongStruct res; |
| 1185 | __msan_poison(&res, sizeof(res)); |
| 1186 | res.b1 = res.b2 = res.b4 = res.b8 = 111; |
| 1187 | // leaves a1, .., a8 poisoned. |
| 1188 | return res; |
| 1189 | } |
| 1190 | |
| 1191 | TEST(MemorySanitizer, LongStruct) { |
| 1192 | LongStruct s1 = ReturnLongStruct1(); |
| 1193 | __msan_print_shadow(&s1, sizeof(s1)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1194 | EXPECT_NOT_POISONED(s1.a1); |
| 1195 | EXPECT_NOT_POISONED(s1.a2); |
| 1196 | EXPECT_NOT_POISONED(s1.a4); |
| 1197 | EXPECT_NOT_POISONED(s1.a8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1198 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1199 | EXPECT_POISONED(s1.b1); |
| 1200 | EXPECT_POISONED(s1.b2); |
| 1201 | EXPECT_POISONED(s1.b4); |
| 1202 | EXPECT_POISONED(s1.b8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1203 | |
| 1204 | LongStruct s2 = ReturnLongStruct2(); |
| 1205 | __msan_print_shadow(&s2, sizeof(s2)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1206 | EXPECT_NOT_POISONED(s2.b1); |
| 1207 | EXPECT_NOT_POISONED(s2.b2); |
| 1208 | EXPECT_NOT_POISONED(s2.b4); |
| 1209 | EXPECT_NOT_POISONED(s2.b8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1210 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1211 | EXPECT_POISONED(s2.a1); |
| 1212 | EXPECT_POISONED(s2.a2); |
| 1213 | EXPECT_POISONED(s2.a4); |
| 1214 | EXPECT_POISONED(s2.a8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | TEST(MemorySanitizer, getrlimit) { |
| 1218 | struct rlimit limit; |
| 1219 | __msan_poison(&limit, sizeof(limit)); |
| 1220 | int result = getrlimit(RLIMIT_DATA, &limit); |
| 1221 | assert(result == 0); |
| 1222 | volatile rlim_t t; |
| 1223 | t = limit.rlim_cur; |
| 1224 | t = limit.rlim_max; |
| 1225 | } |
| 1226 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1227 | TEST(MemorySanitizer, getrusage) { |
| 1228 | struct rusage usage; |
| 1229 | __msan_poison(&usage, sizeof(usage)); |
| 1230 | int result = getrusage(RUSAGE_SELF, &usage); |
| 1231 | assert(result == 0); |
| 1232 | volatile struct timeval t; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1233 | EXPECT_NOT_POISONED(usage.ru_utime.tv_sec); |
| 1234 | EXPECT_NOT_POISONED(usage.ru_utime.tv_usec); |
| 1235 | EXPECT_NOT_POISONED(usage.ru_stime.tv_sec); |
| 1236 | EXPECT_NOT_POISONED(usage.ru_stime.tv_usec); |
| 1237 | EXPECT_NOT_POISONED(usage.ru_maxrss); |
| 1238 | EXPECT_NOT_POISONED(usage.ru_minflt); |
| 1239 | EXPECT_NOT_POISONED(usage.ru_majflt); |
| 1240 | EXPECT_NOT_POISONED(usage.ru_inblock); |
| 1241 | EXPECT_NOT_POISONED(usage.ru_oublock); |
| 1242 | EXPECT_NOT_POISONED(usage.ru_nvcsw); |
| 1243 | EXPECT_NOT_POISONED(usage.ru_nivcsw); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static void dladdr_testfn() {} |
| 1247 | |
| 1248 | TEST(MemorySanitizer, dladdr) { |
| 1249 | Dl_info info; |
| 1250 | __msan_poison(&info, sizeof(info)); |
| 1251 | int result = dladdr((const void*)dladdr_testfn, &info); |
| 1252 | assert(result != 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1253 | EXPECT_NOT_POISONED((unsigned long)info.dli_fname); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1254 | if (info.dli_fname) |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1255 | EXPECT_NOT_POISONED(strlen(info.dli_fname)); |
| 1256 | EXPECT_NOT_POISONED((unsigned long)info.dli_fbase); |
| 1257 | EXPECT_NOT_POISONED((unsigned long)info.dli_sname); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1258 | if (info.dli_sname) |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1259 | EXPECT_NOT_POISONED(strlen(info.dli_sname)); |
| 1260 | EXPECT_NOT_POISONED((unsigned long)info.dli_saddr); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 1263 | TEST(MemorySanitizer, scanf) { |
| 1264 | const char *input = "42 hello"; |
| 1265 | int* d = new int; |
| 1266 | char* s = new char[7]; |
| 1267 | int res = sscanf(input, "%d %5s", d, s); |
| 1268 | printf("res %d\n", res); |
| 1269 | assert(res == 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1270 | EXPECT_NOT_POISONED(*d); |
| 1271 | EXPECT_NOT_POISONED(s[0]); |
| 1272 | EXPECT_NOT_POISONED(s[1]); |
| 1273 | EXPECT_NOT_POISONED(s[2]); |
| 1274 | EXPECT_NOT_POISONED(s[3]); |
| 1275 | EXPECT_NOT_POISONED(s[4]); |
| 1276 | EXPECT_NOT_POISONED(s[5]); |
| 1277 | EXPECT_POISONED(s[6]); |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 1278 | delete s; |
| 1279 | delete d; |
| 1280 | } |
| 1281 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1282 | static void* SimpleThread_threadfn(void* data) { |
| 1283 | return new int; |
| 1284 | } |
| 1285 | |
| 1286 | TEST(MemorySanitizer, SimpleThread) { |
| 1287 | pthread_t t; |
| 1288 | void* p; |
| 1289 | int res = pthread_create(&t, NULL, SimpleThread_threadfn, NULL); |
| 1290 | assert(!res); |
| 1291 | res = pthread_join(t, &p); |
| 1292 | assert(!res); |
| 1293 | if (!__msan_has_dynamic_component()) // FIXME: intercept pthread_join (?). |
| 1294 | __msan_unpoison(&p, sizeof(p)); |
| 1295 | delete (int*)p; |
| 1296 | } |
| 1297 | |
| 1298 | TEST(MemorySanitizer, uname) { |
| 1299 | struct utsname u; |
| 1300 | int res = uname(&u); |
| 1301 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1302 | EXPECT_NOT_POISONED(strlen(u.sysname)); |
| 1303 | EXPECT_NOT_POISONED(strlen(u.nodename)); |
| 1304 | EXPECT_NOT_POISONED(strlen(u.release)); |
| 1305 | EXPECT_NOT_POISONED(strlen(u.version)); |
| 1306 | EXPECT_NOT_POISONED(strlen(u.machine)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Evgeniy Stepanov | 95d0588 | 2013-01-23 10:43:38 +0000 | [diff] [blame] | 1309 | TEST(MemorySanitizer, gethostname) { |
| 1310 | char buf[100]; |
| 1311 | int res = gethostname(buf, 100); |
| 1312 | assert(!res); |
| 1313 | EXPECT_NOT_POISONED(strlen(buf)); |
| 1314 | } |
| 1315 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1316 | template<class T> |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1317 | static bool applySlt(T value, T shadow) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1318 | __msan_partial_poison(&value, &shadow, sizeof(T)); |
| 1319 | volatile bool zzz = true; |
| 1320 | // This "|| zzz" trick somehow makes LLVM emit "icmp slt" instead of |
| 1321 | // a shift-and-trunc to get at the highest bit. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1322 | volatile bool v = value < 0 || zzz; |
| 1323 | return v; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | TEST(MemorySanitizer, SignedCompareWithZero) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1327 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xF)); |
| 1328 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFF)); |
| 1329 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFFFFFF)); |
| 1330 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0x7FFFFFF)); |
| 1331 | EXPECT_UMR(applySlt<S4>(0xF, 0x80FFFFFF)); |
| 1332 | EXPECT_UMR(applySlt<S4>(0xF, 0xFFFFFFFF)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 1335 | template <class T, class S> |
| 1336 | static T poisoned(T Va, S Sa) { |
| 1337 | char SIZE_CHECK1[(ssize_t)sizeof(T) - (ssize_t)sizeof(S)]; |
| 1338 | char SIZE_CHECK2[(ssize_t)sizeof(S) - (ssize_t)sizeof(T)]; |
| 1339 | T a; |
| 1340 | a = Va; |
| 1341 | __msan_partial_poison(&a, &Sa, sizeof(T)); |
| 1342 | return a; |
| 1343 | } |
| 1344 | |
| 1345 | TEST(MemorySanitizer, ICmpRelational) { |
| 1346 | EXPECT_NOT_POISONED(poisoned(0, 0) < poisoned(0, 0)); |
| 1347 | EXPECT_NOT_POISONED(poisoned(0U, 0) < poisoned(0U, 0)); |
| 1348 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) < poisoned(0LL, 0LLU)); |
| 1349 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) < poisoned(0LLU, 0LLU)); |
| 1350 | EXPECT_POISONED(poisoned(0xFF, 0xFF) < poisoned(0xFF, 0xFF)); |
| 1351 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) < |
| 1352 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 1353 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) < |
| 1354 | poisoned(-1, 0xFFFFFFFFU)); |
| 1355 | |
| 1356 | EXPECT_NOT_POISONED(poisoned(0, 0) <= poisoned(0, 0)); |
| 1357 | EXPECT_NOT_POISONED(poisoned(0U, 0) <= poisoned(0U, 0)); |
| 1358 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) <= poisoned(0LL, 0LLU)); |
| 1359 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) <= poisoned(0LLU, 0LLU)); |
| 1360 | EXPECT_POISONED(poisoned(0xFF, 0xFF) <= poisoned(0xFF, 0xFF)); |
| 1361 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <= |
| 1362 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 1363 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <= |
| 1364 | poisoned(-1, 0xFFFFFFFFU)); |
| 1365 | |
| 1366 | EXPECT_NOT_POISONED(poisoned(0, 0) > poisoned(0, 0)); |
| 1367 | EXPECT_NOT_POISONED(poisoned(0U, 0) > poisoned(0U, 0)); |
| 1368 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) > poisoned(0LL, 0LLU)); |
| 1369 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) > poisoned(0LLU, 0LLU)); |
| 1370 | EXPECT_POISONED(poisoned(0xFF, 0xFF) > poisoned(0xFF, 0xFF)); |
| 1371 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) > |
| 1372 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 1373 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) > |
| 1374 | poisoned(-1, 0xFFFFFFFFU)); |
| 1375 | |
| 1376 | EXPECT_NOT_POISONED(poisoned(0, 0) >= poisoned(0, 0)); |
| 1377 | EXPECT_NOT_POISONED(poisoned(0U, 0) >= poisoned(0U, 0)); |
| 1378 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) >= poisoned(0LL, 0LLU)); |
| 1379 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) >= poisoned(0LLU, 0LLU)); |
| 1380 | EXPECT_POISONED(poisoned(0xFF, 0xFF) >= poisoned(0xFF, 0xFF)); |
| 1381 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >= |
| 1382 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 1383 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >= |
| 1384 | poisoned(-1, 0xFFFFFFFFU)); |
| 1385 | |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 1386 | EXPECT_POISONED(poisoned(6, 0xF) > poisoned(7, 0)); |
| 1387 | EXPECT_POISONED(poisoned(0xF, 0xF) > poisoned(7, 0)); |
| 1388 | |
| 1389 | EXPECT_NOT_POISONED(poisoned(-1, 0x80000000U) >= poisoned(-1, 0U)); |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | #if MSAN_HAS_M128 |
| 1393 | TEST(MemorySanitizer, ICmpVectorRelational) { |
| 1394 | EXPECT_NOT_POISONED(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)) < |
| 1395 | poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0))); |
| 1396 | EXPECT_NOT_POISONED(poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)) < |
| 1397 | poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0))); |
| 1398 | EXPECT_POISONED(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)) < |
| 1399 | poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF))); |
| 1400 | EXPECT_POISONED(poisoned(_mm_set1_epi16(6), _mm_set1_epi16(0xF)) > |
| 1401 | poisoned(_mm_set1_epi16(7), _mm_set1_epi16(0))); |
| 1402 | } |
| 1403 | #endif |
| 1404 | |
Evgeniy Stepanov | 2efa142 | 2013-01-22 12:31:39 +0000 | [diff] [blame] | 1405 | // Volatile bitfield store is implemented as load-mask-store |
| 1406 | // Test that we don't warn on the store of (uninitialized) padding. |
| 1407 | struct VolatileBitfieldStruct { |
| 1408 | volatile unsigned x : 1; |
| 1409 | unsigned y : 1; |
| 1410 | }; |
| 1411 | |
| 1412 | TEST(MemorySanitizer, VolatileBitfield) { |
| 1413 | VolatileBitfieldStruct *S = new VolatileBitfieldStruct; |
| 1414 | S->x = 1; |
Evgeniy Stepanov | 02f4a94 | 2013-01-22 12:33:11 +0000 | [diff] [blame] | 1415 | EXPECT_NOT_POISONED((unsigned)S->x); |
| 1416 | EXPECT_POISONED((unsigned)S->y); |
Evgeniy Stepanov | 2efa142 | 2013-01-22 12:31:39 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1419 | TEST(MemorySanitizerDr, StoreInDSOTest) { |
| 1420 | if (!__msan_has_dynamic_component()) return; |
| 1421 | char* s = new char[10]; |
| 1422 | dso_memfill(s, 9); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1423 | EXPECT_NOT_POISONED(s[5]); |
| 1424 | EXPECT_POISONED(s[9]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | int return_poisoned_int() { |
| 1428 | return ReturnPoisoned<U8>(); |
| 1429 | } |
| 1430 | |
| 1431 | TEST(MemorySanitizerDr, ReturnFromDSOTest) { |
| 1432 | if (!__msan_has_dynamic_component()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1433 | EXPECT_NOT_POISONED(dso_callfn(return_poisoned_int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | NOINLINE int TrashParamTLS(long long x, long long y, long long z) { //NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1437 | EXPECT_POISONED(x); |
| 1438 | EXPECT_POISONED(y); |
| 1439 | EXPECT_POISONED(z); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | static int CheckParamTLS(long long x, long long y, long long z) { //NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1444 | EXPECT_NOT_POISONED(x); |
| 1445 | EXPECT_NOT_POISONED(y); |
| 1446 | EXPECT_NOT_POISONED(z); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | TEST(MemorySanitizerDr, CallFromDSOTest) { |
| 1451 | if (!__msan_has_dynamic_component()) return; |
| 1452 | S8* x = GetPoisoned<S8>(); |
| 1453 | S8* y = GetPoisoned<S8>(); |
| 1454 | S8* z = GetPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1455 | EXPECT_NOT_POISONED(TrashParamTLS(*x, *y, *z)); |
| 1456 | EXPECT_NOT_POISONED(dso_callfn1(CheckParamTLS)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static void StackStoreInDSOFn(int* x, int* y) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1460 | EXPECT_NOT_POISONED(*x); |
| 1461 | EXPECT_NOT_POISONED(*y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | TEST(MemorySanitizerDr, StackStoreInDSOTest) { |
| 1465 | if (!__msan_has_dynamic_component()) return; |
| 1466 | dso_stack_store(StackStoreInDSOFn, 1); |
| 1467 | } |
| 1468 | |
| 1469 | TEST(MemorySanitizerOrigins, SetGet) { |
| 1470 | EXPECT_EQ(TrackingOrigins(), __msan_get_track_origins()); |
| 1471 | if (!TrackingOrigins()) return; |
| 1472 | int x; |
| 1473 | __msan_set_origin(&x, sizeof(x), 1234); |
| 1474 | EXPECT_EQ(1234, __msan_get_origin(&x)); |
| 1475 | __msan_set_origin(&x, sizeof(x), 5678); |
| 1476 | EXPECT_EQ(5678, __msan_get_origin(&x)); |
| 1477 | __msan_set_origin(&x, sizeof(x), 0); |
| 1478 | EXPECT_EQ(0, __msan_get_origin(&x)); |
| 1479 | } |
| 1480 | |
| 1481 | namespace { |
| 1482 | struct S { |
| 1483 | U4 dummy; |
| 1484 | U2 a; |
| 1485 | U2 b; |
| 1486 | }; |
| 1487 | |
| 1488 | // http://code.google.com/p/memory-sanitizer/issues/detail?id=6 |
| 1489 | TEST(MemorySanitizerOrigins, DISABLED_InitializedStoreDoesNotChangeOrigin) { |
| 1490 | if (!TrackingOrigins()) return; |
| 1491 | |
| 1492 | S s; |
| 1493 | u32 origin = rand(); // NOLINT |
| 1494 | s.a = *GetPoisonedO<U2>(0, origin); |
| 1495 | EXPECT_EQ(origin, __msan_get_origin(&s.a)); |
| 1496 | EXPECT_EQ(origin, __msan_get_origin(&s.b)); |
| 1497 | |
| 1498 | s.b = 42; |
| 1499 | EXPECT_EQ(origin, __msan_get_origin(&s.a)); |
| 1500 | EXPECT_EQ(origin, __msan_get_origin(&s.b)); |
| 1501 | } |
| 1502 | } // namespace |
| 1503 | |
| 1504 | template<class T, class BinaryOp> |
| 1505 | INLINE |
| 1506 | void BinaryOpOriginTest(BinaryOp op) { |
| 1507 | u32 ox = rand(); //NOLINT |
| 1508 | u32 oy = rand(); //NOLINT |
| 1509 | T *x = GetPoisonedO<T>(0, ox, 0); |
| 1510 | T *y = GetPoisonedO<T>(1, oy, 0); |
| 1511 | T *z = GetPoisonedO<T>(2, 0, 0); |
| 1512 | |
| 1513 | *z = op(*x, *y); |
| 1514 | u32 origin = __msan_get_origin(z); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1515 | EXPECT_POISONED_O(*z, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1516 | EXPECT_EQ(true, origin == ox || origin == oy); |
| 1517 | |
| 1518 | // y is poisoned, x is not. |
| 1519 | *x = 10101; |
| 1520 | *y = *GetPoisonedO<T>(1, oy); |
| 1521 | __msan_break_optimization(x); |
| 1522 | __msan_set_origin(z, sizeof(*z), 0); |
| 1523 | *z = op(*x, *y); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1524 | EXPECT_POISONED_O(*z, oy); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1525 | EXPECT_EQ(__msan_get_origin(z), oy); |
| 1526 | |
| 1527 | // x is poisoned, y is not. |
| 1528 | *x = *GetPoisonedO<T>(0, ox); |
| 1529 | *y = 10101010; |
| 1530 | __msan_break_optimization(y); |
| 1531 | __msan_set_origin(z, sizeof(*z), 0); |
| 1532 | *z = op(*x, *y); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1533 | EXPECT_POISONED_O(*z, ox); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1534 | EXPECT_EQ(__msan_get_origin(z), ox); |
| 1535 | } |
| 1536 | |
| 1537 | template<class T> INLINE T XOR(const T &a, const T&b) { return a ^ b; } |
| 1538 | template<class T> INLINE T ADD(const T &a, const T&b) { return a + b; } |
| 1539 | template<class T> INLINE T SUB(const T &a, const T&b) { return a - b; } |
| 1540 | template<class T> INLINE T MUL(const T &a, const T&b) { return a * b; } |
| 1541 | template<class T> INLINE T AND(const T &a, const T&b) { return a & b; } |
| 1542 | template<class T> INLINE T OR (const T &a, const T&b) { return a | b; } |
| 1543 | |
| 1544 | TEST(MemorySanitizerOrigins, BinaryOp) { |
| 1545 | if (!TrackingOrigins()) return; |
| 1546 | BinaryOpOriginTest<S8>(XOR<S8>); |
| 1547 | BinaryOpOriginTest<U8>(ADD<U8>); |
| 1548 | BinaryOpOriginTest<S4>(SUB<S4>); |
| 1549 | BinaryOpOriginTest<S4>(MUL<S4>); |
| 1550 | BinaryOpOriginTest<U4>(OR<U4>); |
| 1551 | BinaryOpOriginTest<U4>(AND<U4>); |
| 1552 | BinaryOpOriginTest<double>(ADD<U4>); |
| 1553 | BinaryOpOriginTest<float>(ADD<S4>); |
| 1554 | BinaryOpOriginTest<double>(ADD<double>); |
| 1555 | BinaryOpOriginTest<float>(ADD<double>); |
| 1556 | } |
| 1557 | |
| 1558 | TEST(MemorySanitizerOrigins, Unary) { |
| 1559 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1560 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 1561 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 1562 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 1563 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1564 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1565 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1566 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1567 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1568 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1569 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1570 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 1571 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 1572 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 1573 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1574 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1575 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1576 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1577 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 1578 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1579 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1580 | EXPECT_POISONED_O((void*)*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 1581 | EXPECT_POISONED_O((U8)*GetPoisonedO<void*>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | TEST(MemorySanitizerOrigins, EQ) { |
| 1585 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1586 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) <= 11, __LINE__); |
| 1587 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) == 11, __LINE__); |
| 1588 | EXPECT_POISONED_O(*GetPoisonedO<float>(0, __LINE__) == 1.1, __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | TEST(MemorySanitizerOrigins, DIV) { |
| 1592 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1593 | EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) / 100, __LINE__); |
| 1594 | unsigned o = __LINE__; |
| 1595 | EXPECT_UMR_O(volatile unsigned y = 100 / *GetPoisonedO<S4>(0, o, 1), o); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | TEST(MemorySanitizerOrigins, SHIFT) { |
| 1599 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1600 | EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) >> 10, __LINE__); |
| 1601 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) >> 10, __LINE__); |
| 1602 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) << 10, __LINE__); |
| 1603 | EXPECT_POISONED_O(10U << *GetPoisonedO<U8>(0, __LINE__), __LINE__); |
| 1604 | EXPECT_POISONED_O(-10 >> *GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 1605 | EXPECT_POISONED_O(-10 << *GetPoisonedO<S8>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | template<class T, int N> |
| 1609 | void MemCpyTest() { |
| 1610 | int ox = __LINE__; |
| 1611 | T *x = new T[N]; |
| 1612 | T *y = new T[N]; |
| 1613 | T *z = new T[N]; |
| 1614 | __msan_poison(x, N * sizeof(T)); |
| 1615 | __msan_set_origin(x, N * sizeof(T), ox); |
| 1616 | __msan_set_origin(y, N * sizeof(T), 777777); |
| 1617 | __msan_set_origin(z, N * sizeof(T), 888888); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1618 | EXPECT_NOT_POISONED(x); |
| 1619 | memcpy(y, x, N * sizeof(T)); |
| 1620 | EXPECT_POISONED_O(y[0], ox); |
| 1621 | EXPECT_POISONED_O(y[N/2], ox); |
| 1622 | EXPECT_POISONED_O(y[N-1], ox); |
| 1623 | EXPECT_NOT_POISONED(x); |
| 1624 | memmove(z, x, N * sizeof(T)); |
| 1625 | EXPECT_POISONED_O(z[0], ox); |
| 1626 | EXPECT_POISONED_O(z[N/2], ox); |
| 1627 | EXPECT_POISONED_O(z[N-1], ox); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | TEST(MemorySanitizerOrigins, LargeMemCpy) { |
| 1631 | if (!TrackingOrigins()) return; |
| 1632 | MemCpyTest<U1, 10000>(); |
| 1633 | MemCpyTest<U8, 10000>(); |
| 1634 | } |
| 1635 | |
| 1636 | TEST(MemorySanitizerOrigins, SmallMemCpy) { |
| 1637 | if (!TrackingOrigins()) return; |
| 1638 | MemCpyTest<U8, 1>(); |
| 1639 | MemCpyTest<U8, 2>(); |
| 1640 | MemCpyTest<U8, 3>(); |
| 1641 | } |
| 1642 | |
| 1643 | TEST(MemorySanitizerOrigins, Select) { |
| 1644 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1645 | EXPECT_NOT_POISONED(g_one ? 1 : *GetPoisonedO<S4>(0, __LINE__)); |
| 1646 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1647 | S4 x; |
| 1648 | __msan_break_optimization(&x); |
| 1649 | x = g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 0; |
| 1650 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1651 | EXPECT_POISONED_O(g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 1, __LINE__); |
| 1652 | EXPECT_POISONED_O(g_0 ? 1 : *GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | extern "C" |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1656 | NOINLINE char AllocaTO() { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1657 | int ar[100]; |
| 1658 | __msan_break_optimization(ar); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1659 | return ar[10]; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1660 | // fprintf(stderr, "Descr: %s\n", |
| 1661 | // __msan_get_origin_descr_if_stack(__msan_get_origin_tls())); |
| 1662 | } |
| 1663 | |
| 1664 | TEST(MemorySanitizerOrigins, Alloca) { |
| 1665 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1666 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 1667 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 1668 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 1669 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | // FIXME: replace with a lit-like test. |
| 1673 | TEST(MemorySanitizerOrigins, DISABLED_AllocaDeath) { |
| 1674 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1675 | EXPECT_DEATH(AllocaTO(), "ORIGIN: stack allocation: ar@AllocaTO"); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | NOINLINE int RetvalOriginTest(u32 origin) { |
| 1679 | int *a = new int; |
| 1680 | __msan_break_optimization(a); |
| 1681 | __msan_set_origin(a, sizeof(*a), origin); |
| 1682 | int res = *a; |
| 1683 | delete a; |
| 1684 | return res; |
| 1685 | } |
| 1686 | |
| 1687 | TEST(MemorySanitizerOrigins, Retval) { |
| 1688 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1689 | EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | NOINLINE void ParamOriginTest(int param, u32 origin) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1693 | EXPECT_POISONED_O(param, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | TEST(MemorySanitizerOrigins, Param) { |
| 1697 | if (!TrackingOrigins()) return; |
| 1698 | int *a = new int; |
| 1699 | u32 origin = __LINE__; |
| 1700 | __msan_break_optimization(a); |
| 1701 | __msan_set_origin(a, sizeof(*a), origin); |
| 1702 | ParamOriginTest(*a, origin); |
| 1703 | delete a; |
| 1704 | } |
| 1705 | |
| 1706 | TEST(MemorySanitizerOrigins, Invoke) { |
| 1707 | if (!TrackingOrigins()) return; |
| 1708 | StructWithDtor s; // Will cause the calls to become invokes. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1709 | EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | TEST(MemorySanitizerOrigins, strlen) { |
| 1713 | S8 alignment; |
| 1714 | __msan_break_optimization(&alignment); |
| 1715 | char x[4] = {'a', 'b', 0, 0}; |
| 1716 | __msan_poison(&x[2], 1); |
| 1717 | u32 origin = __LINE__; |
| 1718 | __msan_set_origin(x, sizeof(x), origin); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1719 | EXPECT_UMR_O(volatile unsigned y = strlen(x), origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | TEST(MemorySanitizerOrigins, wcslen) { |
| 1723 | wchar_t w[3] = {'a', 'b', 0}; |
| 1724 | u32 origin = __LINE__; |
| 1725 | __msan_set_origin(w, sizeof(w), origin); |
| 1726 | __msan_poison(&w[2], sizeof(wchar_t)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1727 | EXPECT_UMR_O(volatile unsigned y = wcslen(w), origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | #if MSAN_HAS_M128 |
| 1731 | TEST(MemorySanitizerOrigins, StoreIntrinsic) { |
| 1732 | __m128 x, y; |
| 1733 | u32 origin = __LINE__; |
| 1734 | __msan_set_origin(&x, sizeof(x), origin); |
| 1735 | __msan_poison(&x, sizeof(x)); |
| 1736 | __builtin_ia32_storeups((float*)&y, x); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1737 | EXPECT_POISONED_O(y, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1738 | } |
| 1739 | #endif |
| 1740 | |
| 1741 | NOINLINE void RecursiveMalloc(int depth) { |
| 1742 | static int count; |
| 1743 | count++; |
| 1744 | if ((count % (1024 * 1024)) == 0) |
| 1745 | printf("RecursiveMalloc: %d\n", count); |
| 1746 | int *x1 = new int; |
| 1747 | int *x2 = new int; |
| 1748 | __msan_break_optimization(x1); |
| 1749 | __msan_break_optimization(x2); |
| 1750 | if (depth > 0) { |
| 1751 | RecursiveMalloc(depth-1); |
| 1752 | RecursiveMalloc(depth-1); |
| 1753 | } |
| 1754 | delete x1; |
| 1755 | delete x2; |
| 1756 | } |
| 1757 | |
Kostya Serebryany | 65199f1 | 2013-01-25 11:46:22 +0000 | [diff] [blame] | 1758 | TEST(MemorySanitizer, CallocOverflow) { |
| 1759 | size_t kArraySize = 4096; |
| 1760 | volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); |
| 1761 | volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 1762 | void *p = calloc(kArraySize, kArraySize2); // Should return 0. |
| 1763 | EXPECT_EQ(0L, Ident(p)); |
| 1764 | } |
| 1765 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1766 | TEST(MemorySanitizerStress, DISABLED_MallocStackTrace) { |
| 1767 | RecursiveMalloc(22); |
| 1768 | } |
| 1769 | |
| 1770 | int main(int argc, char **argv) { |
| 1771 | __msan_set_poison_in_malloc(1); |
| 1772 | testing::InitGoogleTest(&argc, argv); |
| 1773 | int res = RUN_ALL_TESTS(); |
| 1774 | return res; |
| 1775 | } |