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()) \ |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 77 | EXPECT_EQ(origin, __msan_get_umr_origin()); \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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); \ |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 85 | u32 id = __msan_get_umr_origin(); \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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 | |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 170 | // Check that malloc poisons memory. |
| 171 | // A lot of tests below depend on this. |
| 172 | TEST(MemorySanitizerSanity, PoisonInMalloc) { |
| 173 | int *x = (int*)malloc(sizeof(int)); |
| 174 | EXPECT_POISONED(*x); |
| 175 | free(x); |
| 176 | } |
| 177 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 178 | TEST(MemorySanitizer, NegativeTest1) { |
| 179 | S4 *x = GetPoisoned<S4>(); |
| 180 | if (g_one) |
| 181 | *x = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 182 | EXPECT_NOT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST(MemorySanitizer, PositiveTest1) { |
| 186 | // Load to store. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 187 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 188 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 189 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 190 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 191 | |
| 192 | // S->S conversions. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 193 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 194 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 195 | EXPECT_POISONED(*GetPoisoned<S1>()); |
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<S2>()); |
| 198 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 199 | EXPECT_POISONED(*GetPoisoned<S2>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 200 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 201 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 202 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 203 | EXPECT_POISONED(*GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 204 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 205 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 206 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 207 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 208 | |
| 209 | // ZExt |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 210 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 211 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 212 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 213 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 214 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 215 | EXPECT_POISONED(*GetPoisoned<U4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 216 | |
| 217 | // Unary ops. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 218 | EXPECT_POISONED(- *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 219 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 220 | EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | a_s4[g_zero] = 1 - *GetPoisoned<S4>(); |
| 224 | a_s4[g_zero] = 1 + *GetPoisoned<S4>(); |
| 225 | } |
| 226 | |
| 227 | TEST(MemorySanitizer, Phi1) { |
| 228 | S4 c; |
| 229 | if (g_one) { |
| 230 | c = *GetPoisoned<S4>(); |
| 231 | } else { |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 232 | break_optimization(0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 233 | c = 0; |
| 234 | } |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 235 | EXPECT_POISONED(c); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST(MemorySanitizer, Phi2) { |
| 239 | S4 i = *GetPoisoned<S4>(); |
| 240 | S4 n = g_one; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 241 | EXPECT_UMR(for (; i < g_one; i++);); |
| 242 | EXPECT_POISONED(i); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 245 | NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); } |
| 246 | NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); } |
| 247 | NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 248 | |
| 249 | TEST(MemorySanitizer, ArgTest) { |
| 250 | Arg1ExpectUMR(*GetPoisoned<S4>()); |
| 251 | Arg2ExpectUMR(0, *GetPoisoned<S4>()); |
| 252 | Arg3ExpectUMR(0, 1, *GetPoisoned<S8>()); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | TEST(MemorySanitizer, CallAndRet) { |
| 257 | if (!__msan_has_dynamic_component()) return; |
| 258 | ReturnPoisoned<S1>(); |
| 259 | ReturnPoisoned<S2>(); |
| 260 | ReturnPoisoned<S4>(); |
| 261 | ReturnPoisoned<S8>(); |
| 262 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 263 | EXPECT_POISONED(ReturnPoisoned<S1>()); |
| 264 | EXPECT_POISONED(ReturnPoisoned<S2>()); |
| 265 | EXPECT_POISONED(ReturnPoisoned<S4>()); |
| 266 | EXPECT_POISONED(ReturnPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // malloc() in the following test may be optimized to produce a compile-time |
| 270 | // undef value. Check that we trap on the volatile assignment anyway. |
| 271 | TEST(MemorySanitizer, DISABLED_MallocNoIdent) { |
| 272 | S4 *x = (int*)malloc(sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 273 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 274 | free(x); |
| 275 | } |
| 276 | |
| 277 | TEST(MemorySanitizer, Malloc) { |
| 278 | S4 *x = (int*)Ident(malloc(sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 279 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 280 | free(x); |
| 281 | } |
| 282 | |
| 283 | TEST(MemorySanitizer, Realloc) { |
| 284 | S4 *x = (int*)Ident(realloc(0, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 285 | EXPECT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 286 | x[0] = 1; |
| 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_NOT_POISONED(x[0]); // Ok, was inited before. |
| 292 | EXPECT_POISONED(x[2]); |
| 293 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 294 | x[2] = 1; // Init this here. Check that after realloc it is poisoned again. |
| 295 | x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 296 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 297 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 298 | x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 299 | EXPECT_POISONED(x[1]); |
| 300 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 301 | free(x); |
| 302 | } |
| 303 | |
| 304 | TEST(MemorySanitizer, Calloc) { |
| 305 | S4 *x = (int*)Ident(calloc(1, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 306 | EXPECT_NOT_POISONED(*x); // Should not be poisoned. |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 307 | // EXPECT_EQ(0, *x); |
| 308 | free(x); |
| 309 | } |
| 310 | |
| 311 | TEST(MemorySanitizer, AndOr) { |
| 312 | U4 *p = GetPoisoned<U4>(); |
| 313 | // We poison two bytes in the midle of a 4-byte word to make the test |
| 314 | // correct regardless of endianness. |
| 315 | ((U1*)p)[1] = 0; |
| 316 | ((U1*)p)[2] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 317 | EXPECT_NOT_POISONED(*p & 0x00ffff00); |
| 318 | EXPECT_NOT_POISONED(*p & 0x00ff0000); |
| 319 | EXPECT_NOT_POISONED(*p & 0x0000ff00); |
| 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_NOT_POISONED(*p | 0xff0000ff); |
| 326 | EXPECT_NOT_POISONED(*p | 0xff00ffff); |
| 327 | EXPECT_NOT_POISONED(*p | 0xffff00ff); |
| 328 | EXPECT_POISONED(*p | 0xff000000); |
| 329 | EXPECT_POISONED(*p | 0x000000ff); |
| 330 | EXPECT_POISONED(*p | 0x0000ffff); |
| 331 | EXPECT_POISONED(*p | 0xffff0000); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 332 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 333 | EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | template<class T> |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 337 | static bool applyNot(T value, T shadow) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 338 | __msan_partial_poison(&value, &shadow, sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 339 | return !value; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST(MemorySanitizer, Not) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 343 | EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0)); |
| 344 | EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0)); |
| 345 | EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF)); |
| 346 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF)); |
| 347 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF)); |
| 348 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF)); |
| 349 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000)); |
| 350 | EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000)); |
| 351 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000)); |
| 352 | EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 353 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 354 | EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0)); |
| 355 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE)); |
| 356 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0)); |
| 357 | EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 358 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 359 | EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1))); |
| 360 | EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2))); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST(MemorySanitizer, Shift) { |
| 364 | U4 *up = GetPoisoned<U4>(); |
| 365 | ((U1*)up)[0] = 0; |
| 366 | ((U1*)up)[3] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 367 | EXPECT_NOT_POISONED(*up >> 30); |
| 368 | EXPECT_NOT_POISONED(*up >> 24); |
| 369 | EXPECT_POISONED(*up >> 23); |
| 370 | EXPECT_POISONED(*up >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 371 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 372 | EXPECT_NOT_POISONED(*up << 30); |
| 373 | EXPECT_NOT_POISONED(*up << 24); |
| 374 | EXPECT_POISONED(*up << 23); |
| 375 | EXPECT_POISONED(*up << 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 376 | |
| 377 | S4 *sp = (S4*)up; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 378 | EXPECT_NOT_POISONED(*sp >> 30); |
| 379 | EXPECT_NOT_POISONED(*sp >> 24); |
| 380 | EXPECT_POISONED(*sp >> 23); |
| 381 | EXPECT_POISONED(*sp >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 382 | |
| 383 | sp = GetPoisoned<S4>(); |
| 384 | ((S1*)sp)[1] = 0; |
| 385 | ((S1*)sp)[2] = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 386 | EXPECT_POISONED(*sp >> 31); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 387 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 388 | EXPECT_POISONED(100 >> *GetPoisoned<S4>()); |
| 389 | EXPECT_POISONED(100U >> *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | NOINLINE static int GetPoisonedZero() { |
| 393 | int *zero = new int; |
| 394 | *zero = 0; |
| 395 | __msan_poison(zero, sizeof(*zero)); |
| 396 | int res = *zero; |
| 397 | delete zero; |
| 398 | return res; |
| 399 | } |
| 400 | |
| 401 | TEST(MemorySanitizer, LoadFromDirtyAddress) { |
| 402 | int *a = new int; |
| 403 | *a = 0; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 404 | EXPECT_UMR(break_optimization((void*)(U8)a[GetPoisonedZero()])); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 405 | delete a; |
| 406 | } |
| 407 | |
| 408 | TEST(MemorySanitizer, StoreToDirtyAddress) { |
| 409 | int *a = new int; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 410 | EXPECT_UMR(a[GetPoisonedZero()] = 0); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 411 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 412 | delete a; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | NOINLINE void StackTestFunc() { |
| 417 | S4 p4; |
| 418 | S4 ok4 = 1; |
| 419 | S2 p2; |
| 420 | S2 ok2 = 1; |
| 421 | S1 p1; |
| 422 | S1 ok1 = 1; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 423 | break_optimization(&p4); |
| 424 | break_optimization(&ok4); |
| 425 | break_optimization(&p2); |
| 426 | break_optimization(&ok2); |
| 427 | break_optimization(&p1); |
| 428 | break_optimization(&ok1); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 429 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 430 | EXPECT_POISONED(p4); |
| 431 | EXPECT_POISONED(p2); |
| 432 | EXPECT_POISONED(p1); |
| 433 | EXPECT_NOT_POISONED(ok1); |
| 434 | EXPECT_NOT_POISONED(ok2); |
| 435 | EXPECT_NOT_POISONED(ok4); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | TEST(MemorySanitizer, StackTest) { |
| 439 | StackTestFunc(); |
| 440 | } |
| 441 | |
| 442 | NOINLINE void StackStressFunc() { |
| 443 | int foo[10000]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 444 | break_optimization(foo); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST(MemorySanitizer, DISABLED_StackStressTest) { |
| 448 | for (int i = 0; i < 1000000; i++) |
| 449 | StackStressFunc(); |
| 450 | } |
| 451 | |
| 452 | template<class T> |
| 453 | void TestFloatingPoint() { |
| 454 | static volatile T v; |
| 455 | static T g[100]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 456 | break_optimization(&g); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 457 | T *x = GetPoisoned<T>(); |
| 458 | T *y = GetPoisoned<T>(1); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 459 | EXPECT_POISONED(*x); |
| 460 | EXPECT_POISONED((long long)*x); |
| 461 | EXPECT_POISONED((int)*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 462 | g[0] = *x; |
| 463 | g[1] = *x + *y; |
| 464 | g[2] = *x - *y; |
| 465 | g[3] = *x * *y; |
| 466 | } |
| 467 | |
| 468 | TEST(MemorySanitizer, FloatingPointTest) { |
| 469 | TestFloatingPoint<float>(); |
| 470 | TestFloatingPoint<double>(); |
| 471 | } |
| 472 | |
| 473 | TEST(MemorySanitizer, DynMem) { |
| 474 | S4 x = 0; |
| 475 | S4 *y = GetPoisoned<S4>(); |
| 476 | memcpy(y, &x, g_one * sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 477 | EXPECT_NOT_POISONED(*y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static char *DynRetTestStr; |
| 481 | |
| 482 | TEST(MemorySanitizer, DynRet) { |
| 483 | if (!__msan_has_dynamic_component()) return; |
| 484 | ReturnPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 485 | EXPECT_NOT_POISONED(clearenv()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | TEST(MemorySanitizer, DynRet1) { |
| 490 | if (!__msan_has_dynamic_component()) return; |
| 491 | ReturnPoisoned<S8>(); |
| 492 | } |
| 493 | |
| 494 | struct LargeStruct { |
| 495 | S4 x[10]; |
| 496 | }; |
| 497 | |
| 498 | NOINLINE |
| 499 | LargeStruct LargeRetTest() { |
| 500 | LargeStruct res; |
| 501 | res.x[0] = *GetPoisoned<S4>(); |
| 502 | res.x[1] = *GetPoisoned<S4>(); |
| 503 | res.x[2] = *GetPoisoned<S4>(); |
| 504 | res.x[3] = *GetPoisoned<S4>(); |
| 505 | res.x[4] = *GetPoisoned<S4>(); |
| 506 | res.x[5] = *GetPoisoned<S4>(); |
| 507 | res.x[6] = *GetPoisoned<S4>(); |
| 508 | res.x[7] = *GetPoisoned<S4>(); |
| 509 | res.x[8] = *GetPoisoned<S4>(); |
| 510 | res.x[9] = *GetPoisoned<S4>(); |
| 511 | return res; |
| 512 | } |
| 513 | |
| 514 | TEST(MemorySanitizer, LargeRet) { |
| 515 | LargeStruct a = LargeRetTest(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 516 | EXPECT_POISONED(a.x[0]); |
| 517 | EXPECT_POISONED(a.x[9]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | TEST(MemorySanitizer, fread) { |
| 521 | char *x = new char[32]; |
| 522 | FILE *f = fopen("/proc/self/stat", "r"); |
| 523 | assert(f); |
| 524 | fread(x, 1, 32, f); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 525 | EXPECT_NOT_POISONED(x[0]); |
| 526 | EXPECT_NOT_POISONED(x[16]); |
| 527 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 528 | fclose(f); |
| 529 | delete x; |
| 530 | } |
| 531 | |
| 532 | TEST(MemorySanitizer, read) { |
| 533 | char *x = new char[32]; |
| 534 | int fd = open("/proc/self/stat", O_RDONLY); |
| 535 | assert(fd > 0); |
| 536 | int sz = read(fd, x, 32); |
| 537 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 538 | EXPECT_NOT_POISONED(x[0]); |
| 539 | EXPECT_NOT_POISONED(x[16]); |
| 540 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 541 | close(fd); |
| 542 | delete x; |
| 543 | } |
| 544 | |
| 545 | TEST(MemorySanitizer, pread) { |
| 546 | char *x = new char[32]; |
| 547 | int fd = open("/proc/self/stat", O_RDONLY); |
| 548 | assert(fd > 0); |
| 549 | int sz = pread(fd, x, 32, 0); |
| 550 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 551 | EXPECT_NOT_POISONED(x[0]); |
| 552 | EXPECT_NOT_POISONED(x[16]); |
| 553 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 554 | close(fd); |
| 555 | delete x; |
| 556 | } |
| 557 | |
| 558 | // FIXME: fails now. |
| 559 | TEST(MemorySanitizer, DISABLED_ioctl) { |
| 560 | struct winsize ws; |
| 561 | EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 562 | EXPECT_NOT_POISONED(ws.ws_col); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | TEST(MemorySanitizer, readlink) { |
| 566 | char *x = new char[1000]; |
| 567 | readlink("/proc/self/exe", x, 1000); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 568 | EXPECT_NOT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 569 | delete [] x; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | TEST(MemorySanitizer, stat) { |
| 574 | struct stat* st = new struct stat; |
| 575 | int res = stat("/proc/self/stat", st); |
| 576 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 577 | EXPECT_NOT_POISONED(st->st_dev); |
| 578 | EXPECT_NOT_POISONED(st->st_mode); |
| 579 | EXPECT_NOT_POISONED(st->st_size); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | TEST(MemorySanitizer, statfs) { |
| 583 | struct statfs* st = new struct statfs; |
| 584 | int res = statfs("/", st); |
| 585 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 586 | EXPECT_NOT_POISONED(st->f_type); |
| 587 | EXPECT_NOT_POISONED(st->f_bfree); |
| 588 | EXPECT_NOT_POISONED(st->f_namelen); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | TEST(MemorySanitizer, pipe) { |
| 592 | int* pipefd = new int[2]; |
| 593 | int res = pipe(pipefd); |
| 594 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 595 | EXPECT_NOT_POISONED(pipefd[0]); |
| 596 | EXPECT_NOT_POISONED(pipefd[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 597 | close(pipefd[0]); |
| 598 | close(pipefd[1]); |
| 599 | } |
| 600 | |
| 601 | TEST(MemorySanitizer, getcwd) { |
| 602 | char path[PATH_MAX + 1]; |
| 603 | char* res = getcwd(path, sizeof(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, realpath) { |
| 609 | const char* relpath = "."; |
| 610 | char path[PATH_MAX + 1]; |
| 611 | char* res = realpath(relpath, path); |
| 612 | assert(res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 613 | EXPECT_NOT_POISONED(path[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | TEST(MemorySanitizer, memcpy) { |
| 617 | char* x = new char[2]; |
| 618 | char* y = new char[2]; |
| 619 | x[0] = 1; |
| 620 | x[1] = *GetPoisoned<char>(); |
| 621 | memcpy(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 622 | EXPECT_NOT_POISONED(y[0]); |
| 623 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | TEST(MemorySanitizer, memmove) { |
| 627 | char* x = new char[2]; |
| 628 | char* y = new char[2]; |
| 629 | x[0] = 1; |
| 630 | x[1] = *GetPoisoned<char>(); |
| 631 | memmove(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 632 | EXPECT_NOT_POISONED(y[0]); |
| 633 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | TEST(MemorySanitizer, strdup) { |
| 637 | char *x = strdup("zzz"); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 638 | EXPECT_NOT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 639 | free(x); |
| 640 | } |
| 641 | |
| 642 | template<class T, int size> |
| 643 | void TestOverlapMemmove() { |
| 644 | T *x = new T[size]; |
| 645 | assert(size >= 3); |
| 646 | x[2] = 0; |
| 647 | memmove(x, x + 1, (size - 1) * sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 648 | EXPECT_NOT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 649 | if (!__msan_has_dynamic_component()) { |
| 650 | // FIXME: under DR we will lose this information |
| 651 | // because accesses in memmove will unpoisin the shadow. |
| 652 | // We need to use our own memove implementation instead of libc's. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 653 | EXPECT_POISONED(x[0]); |
| 654 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 655 | } |
| 656 | delete [] x; |
| 657 | } |
| 658 | |
| 659 | TEST(MemorySanitizer, overlap_memmove) { |
| 660 | TestOverlapMemmove<U1, 10>(); |
| 661 | TestOverlapMemmove<U1, 1000>(); |
| 662 | TestOverlapMemmove<U8, 4>(); |
| 663 | TestOverlapMemmove<U8, 1000>(); |
| 664 | } |
| 665 | |
| 666 | TEST(MemorySanitizer, strcpy) { // NOLINT |
| 667 | char* x = new char[3]; |
| 668 | char* y = new char[3]; |
| 669 | x[0] = 'a'; |
| 670 | x[1] = *GetPoisoned<char>(1, 1); |
| 671 | x[2] = 0; |
| 672 | strcpy(y, x); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 673 | EXPECT_NOT_POISONED(y[0]); |
| 674 | EXPECT_POISONED(y[1]); |
| 675 | EXPECT_NOT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | TEST(MemorySanitizer, strncpy) { // NOLINT |
| 679 | char* x = new char[3]; |
| 680 | char* y = new char[3]; |
| 681 | x[0] = 'a'; |
| 682 | x[1] = *GetPoisoned<char>(1, 1); |
| 683 | x[2] = 0; |
| 684 | strncpy(y, x, 2); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 685 | EXPECT_NOT_POISONED(y[0]); |
| 686 | EXPECT_POISONED(y[1]); |
| 687 | EXPECT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | TEST(MemorySanitizer, strtol) { |
| 691 | char *e; |
| 692 | assert(1 == strtol("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 693 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | TEST(MemorySanitizer, strtoll) { |
| 697 | char *e; |
| 698 | assert(1 == strtoll("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 699 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | TEST(MemorySanitizer, strtoul) { |
| 703 | char *e; |
| 704 | assert(1 == strtoul("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 705 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | TEST(MemorySanitizer, strtoull) { |
| 709 | char *e; |
| 710 | assert(1 == strtoull("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 711 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 714 | TEST(MemorySanitizer, strtod) { |
| 715 | char *e; |
| 716 | assert(0 != strtod("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 717 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | TEST(MemorySanitizer, strtof) { |
| 721 | char *e; |
| 722 | assert(0 != strtof("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 723 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | TEST(MemorySanitizer, strtold) { |
| 727 | char *e; |
| 728 | assert(0 != strtold("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 729 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 732 | TEST(MemorySanitizer, sprintf) { // NOLINT |
| 733 | char buff[10]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 734 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 735 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 736 | int res = sprintf(buff, "%d", 1234567); // NOLINT |
| 737 | assert(res == 7); |
| 738 | assert(buff[0] == '1'); |
| 739 | assert(buff[1] == '2'); |
| 740 | assert(buff[2] == '3'); |
| 741 | assert(buff[6] == '7'); |
| 742 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 743 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | TEST(MemorySanitizer, snprintf) { |
| 747 | char buff[10]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 748 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 749 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 750 | int res = snprintf(buff, sizeof(buff), "%d", 1234567); |
| 751 | assert(res == 7); |
| 752 | assert(buff[0] == '1'); |
| 753 | assert(buff[1] == '2'); |
| 754 | assert(buff[2] == '3'); |
| 755 | assert(buff[6] == '7'); |
| 756 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 757 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | TEST(MemorySanitizer, swprintf) { |
| 761 | wchar_t buff[10]; |
| 762 | assert(sizeof(wchar_t) == 4); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 763 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 764 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 765 | int res = swprintf(buff, 9, L"%d", 1234567); |
| 766 | assert(res == 7); |
| 767 | assert(buff[0] == '1'); |
| 768 | assert(buff[1] == '2'); |
| 769 | assert(buff[2] == '3'); |
| 770 | assert(buff[6] == '7'); |
| 771 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 772 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | TEST(MemorySanitizer, wcstombs) { |
| 776 | const wchar_t *x = L"abc"; |
| 777 | char buff[10]; |
| 778 | int res = wcstombs(buff, x, 4); |
| 779 | EXPECT_EQ(res, 3); |
| 780 | EXPECT_EQ(buff[0], 'a'); |
| 781 | EXPECT_EQ(buff[1], 'b'); |
| 782 | EXPECT_EQ(buff[2], 'c'); |
| 783 | } |
| 784 | |
| 785 | TEST(MemorySanitizer, gettimeofday) { |
| 786 | struct timeval tv; |
| 787 | struct timezone tz; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 788 | break_optimization(&tv); |
| 789 | break_optimization(&tz); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 790 | assert(sizeof(tv) == 16); |
| 791 | assert(sizeof(tz) == 8); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 792 | EXPECT_POISONED(tv.tv_sec); |
| 793 | EXPECT_POISONED(tv.tv_usec); |
| 794 | EXPECT_POISONED(tz.tz_minuteswest); |
| 795 | EXPECT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 796 | assert(0 == gettimeofday(&tv, &tz)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 797 | EXPECT_NOT_POISONED(tv.tv_sec); |
| 798 | EXPECT_NOT_POISONED(tv.tv_usec); |
| 799 | EXPECT_NOT_POISONED(tz.tz_minuteswest); |
| 800 | EXPECT_NOT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | TEST(MemorySanitizer, mmap) { |
| 804 | const int size = 4096; |
| 805 | void *p1, *p2; |
| 806 | p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 807 | __msan_poison(p1, size); |
| 808 | munmap(p1, size); |
| 809 | for (int i = 0; i < 1000; i++) { |
| 810 | p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 811 | if (p2 == p1) |
| 812 | break; |
| 813 | else |
| 814 | munmap(p2, size); |
| 815 | } |
| 816 | if (p1 == p2) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 817 | EXPECT_NOT_POISONED(*(char*)p2); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 818 | munmap(p2, size); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // FIXME: enable and add ecvt. |
| 823 | // FIXME: check why msandr does nt handle fcvt. |
| 824 | TEST(MemorySanitizer, fcvt) { |
| 825 | int a, b; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 826 | break_optimization(&a); |
| 827 | break_optimization(&b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 828 | EXPECT_POISONED(a); |
| 829 | EXPECT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 830 | char *str = fcvt(12345.6789, 10, &a, &b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 831 | EXPECT_NOT_POISONED(a); |
| 832 | EXPECT_NOT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 835 | struct StructWithDtor { |
| 836 | ~StructWithDtor(); |
| 837 | }; |
| 838 | |
| 839 | NOINLINE StructWithDtor::~StructWithDtor() { |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 840 | break_optimization(0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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(); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1109 | break_optimization(&a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1521 | break_optimization(x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1530 | break_optimization(y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1648 | break_optimization(&x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1658 | 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; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1680 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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__; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1700 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1714 | break_optimization(&alignment); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame^] | 1748 | break_optimization(x1); |
| 1749 | break_optimization(x2); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 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) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1771 | testing::InitGoogleTest(&argc, argv); |
| 1772 | int res = RUN_ALL_TESTS(); |
| 1773 | return res; |
| 1774 | } |