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 | |
Evgeniy Stepanov | 6e5ff89 | 2013-03-22 09:01:26 +0000 | [diff] [blame] | 15 | #ifndef MSAN_EXTERNAL_TEST_CONFIG |
| 16 | #include "msan_test_config.h" |
| 17 | #endif // MSAN_EXTERNAL_TEST_CONFIG |
| 18 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 19 | #include "sanitizer/msan_interface.h" |
| 20 | #include "msandr_test_so.h" |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 21 | |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 22 | #include <inttypes.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <assert.h> |
| 27 | #include <wchar.h> |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 28 | #include <math.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 29 | |
Evgeniy Stepanov | 9530eb7 | 2013-04-23 14:05:15 +0000 | [diff] [blame] | 30 | #include <arpa/inet.h> |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 31 | #include <dlfcn.h> |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 32 | #include <grp.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 34 | #include <link.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 35 | #include <limits.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <sys/resource.h> |
| 41 | #include <sys/ioctl.h> |
Evgeniy Stepanov | 359d7fc | 2013-06-24 14:25:33 +0000 | [diff] [blame] | 42 | #include <sys/sysinfo.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 43 | #include <sys/utsname.h> |
| 44 | #include <sys/mman.h> |
| 45 | #include <sys/vfs.h> |
Evgeniy Stepanov | d97a15a | 2013-03-14 12:49:23 +0000 | [diff] [blame] | 46 | #include <dirent.h> |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 47 | #include <pwd.h> |
Evgeniy Stepanov | 134fe8a | 2013-04-08 13:45:12 +0000 | [diff] [blame] | 48 | #include <sys/socket.h> |
Evgeniy Stepanov | 0a2cc37 | 2013-05-23 11:10:23 +0000 | [diff] [blame] | 49 | #include <netdb.h> |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 50 | |
| 51 | #if defined(__i386__) || defined(__x86_64__) |
| 52 | # include <emmintrin.h> |
| 53 | # define MSAN_HAS_M128 1 |
| 54 | #else |
| 55 | # define MSAN_HAS_M128 0 |
| 56 | #endif |
| 57 | |
| 58 | typedef unsigned char U1; |
| 59 | typedef unsigned short U2; // NOLINT |
| 60 | typedef unsigned int U4; |
| 61 | typedef unsigned long long U8; // NOLINT |
| 62 | typedef signed char S1; |
| 63 | typedef signed short S2; // NOLINT |
| 64 | typedef signed int S4; |
| 65 | typedef signed long long S8; // NOLINT |
| 66 | #define NOINLINE __attribute__((noinline)) |
| 67 | #define INLINE __attribute__((always_inline)) |
| 68 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 69 | static bool TrackingOrigins() { |
| 70 | S8 x; |
| 71 | __msan_set_origin(&x, sizeof(x), 0x1234); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 72 | U4 origin = __msan_get_origin(&x); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 73 | __msan_set_origin(&x, sizeof(x), 0); |
| 74 | return origin == 0x1234; |
| 75 | } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 76 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 77 | #define EXPECT_UMR(action) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 78 | do { \ |
| 79 | __msan_set_expect_umr(1); \ |
| 80 | action; \ |
| 81 | __msan_set_expect_umr(0); \ |
| 82 | } while (0) |
| 83 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 84 | #define EXPECT_UMR_O(action, origin) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 85 | do { \ |
| 86 | __msan_set_expect_umr(1); \ |
| 87 | action; \ |
| 88 | __msan_set_expect_umr(0); \ |
| 89 | if (TrackingOrigins()) \ |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 90 | EXPECT_EQ(origin, __msan_get_umr_origin()); \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 91 | } while (0) |
| 92 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 93 | #define EXPECT_UMR_S(action, stack_origin) \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 94 | do { \ |
| 95 | __msan_set_expect_umr(1); \ |
| 96 | action; \ |
| 97 | __msan_set_expect_umr(0); \ |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 98 | U4 id = __msan_get_umr_origin(); \ |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 99 | const char *str = __msan_get_origin_descr_if_stack(id); \ |
| 100 | if (!str || strcmp(str, stack_origin)) { \ |
| 101 | fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", \ |
| 102 | id, stack_origin, str); \ |
| 103 | EXPECT_EQ(1, 0); \ |
| 104 | } \ |
| 105 | } while (0) |
| 106 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 107 | #define EXPECT_POISONED(x) ExpectPoisoned(x) |
| 108 | |
| 109 | template<typename T> |
| 110 | void ExpectPoisoned(const T& t) { |
| 111 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 112 | } |
| 113 | |
| 114 | #define EXPECT_POISONED_O(x, origin) \ |
| 115 | ExpectPoisonedWithOrigin(x, origin) |
| 116 | |
| 117 | template<typename T> |
| 118 | void ExpectPoisonedWithOrigin(const T& t, unsigned origin) { |
| 119 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 120 | if (TrackingOrigins()) |
| 121 | EXPECT_EQ(origin, __msan_get_origin((void*)&t)); |
| 122 | } |
| 123 | |
| 124 | #define EXPECT_POISONED_S(x, stack_origin) \ |
| 125 | ExpectPoisonedWithStackOrigin(x, stack_origin) |
| 126 | |
| 127 | template<typename T> |
| 128 | void ExpectPoisonedWithStackOrigin(const T& t, const char *stack_origin) { |
| 129 | EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 130 | U4 id = __msan_get_origin((void*)&t); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 131 | const char *str = __msan_get_origin_descr_if_stack(id); |
| 132 | if (!str || strcmp(str, stack_origin)) { |
| 133 | fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", |
| 134 | id, stack_origin, str); |
| 135 | EXPECT_EQ(1, 0); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #define EXPECT_NOT_POISONED(x) ExpectNotPoisoned(x) |
| 140 | |
| 141 | template<typename T> |
| 142 | void ExpectNotPoisoned(const T& t) { |
| 143 | EXPECT_EQ(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| 144 | } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 145 | |
| 146 | static U8 poisoned_array[100]; |
| 147 | template<class T> |
| 148 | T *GetPoisoned(int i = 0, T val = 0) { |
| 149 | T *res = (T*)&poisoned_array[i]; |
| 150 | *res = val; |
| 151 | __msan_poison(&poisoned_array[i], sizeof(T)); |
| 152 | return res; |
| 153 | } |
| 154 | |
| 155 | template<class T> |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 156 | T *GetPoisonedO(int i, U4 origin, T val = 0) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 157 | T *res = (T*)&poisoned_array[i]; |
| 158 | *res = val; |
| 159 | __msan_poison(&poisoned_array[i], sizeof(T)); |
| 160 | __msan_set_origin(&poisoned_array[i], sizeof(T), origin); |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | // This function returns its parameter but in such a way that compiler |
| 165 | // can not prove it. |
| 166 | template<class T> |
| 167 | NOINLINE |
| 168 | static T Ident(T t) { |
| 169 | volatile T ret = t; |
| 170 | return ret; |
| 171 | } |
| 172 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 173 | template<class T> NOINLINE T ReturnPoisoned() { return *GetPoisoned<T>(); } |
| 174 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 175 | static volatile int g_one = 1; |
| 176 | static volatile int g_zero = 0; |
| 177 | static volatile int g_0 = 0; |
| 178 | static volatile int g_1 = 1; |
| 179 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 180 | S4 a_s4[100]; |
| 181 | S8 a_s8[100]; |
| 182 | |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 183 | // Check that malloc poisons memory. |
| 184 | // A lot of tests below depend on this. |
| 185 | TEST(MemorySanitizerSanity, PoisonInMalloc) { |
| 186 | int *x = (int*)malloc(sizeof(int)); |
| 187 | EXPECT_POISONED(*x); |
| 188 | free(x); |
| 189 | } |
| 190 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 191 | TEST(MemorySanitizer, NegativeTest1) { |
| 192 | S4 *x = GetPoisoned<S4>(); |
| 193 | if (g_one) |
| 194 | *x = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 195 | EXPECT_NOT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST(MemorySanitizer, PositiveTest1) { |
| 199 | // Load to store. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 200 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 201 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 202 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 203 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 204 | |
| 205 | // S->S conversions. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 206 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 207 | EXPECT_POISONED(*GetPoisoned<S1>()); |
| 208 | EXPECT_POISONED(*GetPoisoned<S1>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 209 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 210 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 211 | EXPECT_POISONED(*GetPoisoned<S2>()); |
| 212 | EXPECT_POISONED(*GetPoisoned<S2>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 213 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 214 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 215 | EXPECT_POISONED(*GetPoisoned<S4>()); |
| 216 | EXPECT_POISONED(*GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 217 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 218 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 219 | EXPECT_POISONED(*GetPoisoned<S8>()); |
| 220 | EXPECT_POISONED(*GetPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 221 | |
| 222 | // ZExt |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 223 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 224 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 225 | EXPECT_POISONED(*GetPoisoned<U1>()); |
| 226 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 227 | EXPECT_POISONED(*GetPoisoned<U2>()); |
| 228 | EXPECT_POISONED(*GetPoisoned<U4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 229 | |
| 230 | // Unary ops. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 231 | EXPECT_POISONED(- *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 232 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 233 | EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | a_s4[g_zero] = 1 - *GetPoisoned<S4>(); |
| 237 | a_s4[g_zero] = 1 + *GetPoisoned<S4>(); |
| 238 | } |
| 239 | |
| 240 | TEST(MemorySanitizer, Phi1) { |
| 241 | S4 c; |
| 242 | if (g_one) { |
| 243 | c = *GetPoisoned<S4>(); |
| 244 | } else { |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 245 | break_optimization(0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 246 | c = 0; |
| 247 | } |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 248 | EXPECT_POISONED(c); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TEST(MemorySanitizer, Phi2) { |
| 252 | S4 i = *GetPoisoned<S4>(); |
| 253 | S4 n = g_one; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 254 | EXPECT_UMR(for (; i < g_one; i++);); |
| 255 | EXPECT_POISONED(i); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 258 | NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); } |
| 259 | NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); } |
| 260 | NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 261 | |
| 262 | TEST(MemorySanitizer, ArgTest) { |
| 263 | Arg1ExpectUMR(*GetPoisoned<S4>()); |
| 264 | Arg2ExpectUMR(0, *GetPoisoned<S4>()); |
| 265 | Arg3ExpectUMR(0, 1, *GetPoisoned<S8>()); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | TEST(MemorySanitizer, CallAndRet) { |
| 270 | if (!__msan_has_dynamic_component()) return; |
| 271 | ReturnPoisoned<S1>(); |
| 272 | ReturnPoisoned<S2>(); |
| 273 | ReturnPoisoned<S4>(); |
| 274 | ReturnPoisoned<S8>(); |
| 275 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 276 | EXPECT_POISONED(ReturnPoisoned<S1>()); |
| 277 | EXPECT_POISONED(ReturnPoisoned<S2>()); |
| 278 | EXPECT_POISONED(ReturnPoisoned<S4>()); |
| 279 | EXPECT_POISONED(ReturnPoisoned<S8>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // malloc() in the following test may be optimized to produce a compile-time |
| 283 | // undef value. Check that we trap on the volatile assignment anyway. |
| 284 | TEST(MemorySanitizer, DISABLED_MallocNoIdent) { |
| 285 | S4 *x = (int*)malloc(sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 286 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 287 | free(x); |
| 288 | } |
| 289 | |
| 290 | TEST(MemorySanitizer, Malloc) { |
| 291 | S4 *x = (int*)Ident(malloc(sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 292 | EXPECT_POISONED(*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 293 | free(x); |
| 294 | } |
| 295 | |
| 296 | TEST(MemorySanitizer, Realloc) { |
| 297 | S4 *x = (int*)Ident(realloc(0, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 298 | EXPECT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 299 | x[0] = 1; |
| 300 | x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 301 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 302 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 303 | x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 304 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 305 | EXPECT_POISONED(x[2]); |
| 306 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 307 | x[2] = 1; // Init this here. Check that after realloc it is poisoned again. |
| 308 | x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 309 | EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| 310 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 311 | x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 312 | EXPECT_POISONED(x[1]); |
| 313 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 314 | free(x); |
| 315 | } |
| 316 | |
| 317 | TEST(MemorySanitizer, Calloc) { |
| 318 | S4 *x = (int*)Ident(calloc(1, sizeof(S4))); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 319 | EXPECT_NOT_POISONED(*x); // Should not be poisoned. |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 320 | // EXPECT_EQ(0, *x); |
| 321 | free(x); |
| 322 | } |
| 323 | |
| 324 | TEST(MemorySanitizer, AndOr) { |
| 325 | U4 *p = GetPoisoned<U4>(); |
| 326 | // We poison two bytes in the midle of a 4-byte word to make the test |
| 327 | // correct regardless of endianness. |
| 328 | ((U1*)p)[1] = 0; |
| 329 | ((U1*)p)[2] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 330 | EXPECT_NOT_POISONED(*p & 0x00ffff00); |
| 331 | EXPECT_NOT_POISONED(*p & 0x00ff0000); |
| 332 | EXPECT_NOT_POISONED(*p & 0x0000ff00); |
| 333 | EXPECT_POISONED(*p & 0xff000000); |
| 334 | EXPECT_POISONED(*p & 0x000000ff); |
| 335 | EXPECT_POISONED(*p & 0x0000ffff); |
| 336 | EXPECT_POISONED(*p & 0xffff0000); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 337 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 338 | EXPECT_NOT_POISONED(*p | 0xff0000ff); |
| 339 | EXPECT_NOT_POISONED(*p | 0xff00ffff); |
| 340 | EXPECT_NOT_POISONED(*p | 0xffff00ff); |
| 341 | EXPECT_POISONED(*p | 0xff000000); |
| 342 | EXPECT_POISONED(*p | 0x000000ff); |
| 343 | EXPECT_POISONED(*p | 0x0000ffff); |
| 344 | EXPECT_POISONED(*p | 0xffff0000); |
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_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | template<class T> |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 350 | static bool applyNot(T value, T shadow) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 351 | __msan_partial_poison(&value, &shadow, sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 352 | return !value; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST(MemorySanitizer, Not) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 356 | EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0)); |
| 357 | EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0)); |
| 358 | EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF)); |
| 359 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF)); |
| 360 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF)); |
| 361 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF)); |
| 362 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000)); |
| 363 | EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000)); |
| 364 | EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000)); |
| 365 | EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 366 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 367 | EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0)); |
| 368 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE)); |
| 369 | EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0)); |
| 370 | EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF)); |
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_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1))); |
| 373 | EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2))); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | TEST(MemorySanitizer, Shift) { |
| 377 | U4 *up = GetPoisoned<U4>(); |
| 378 | ((U1*)up)[0] = 0; |
| 379 | ((U1*)up)[3] = 0xff; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 380 | EXPECT_NOT_POISONED(*up >> 30); |
| 381 | EXPECT_NOT_POISONED(*up >> 24); |
| 382 | EXPECT_POISONED(*up >> 23); |
| 383 | EXPECT_POISONED(*up >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 384 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 385 | EXPECT_NOT_POISONED(*up << 30); |
| 386 | EXPECT_NOT_POISONED(*up << 24); |
| 387 | EXPECT_POISONED(*up << 23); |
| 388 | EXPECT_POISONED(*up << 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 389 | |
| 390 | S4 *sp = (S4*)up; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 391 | EXPECT_NOT_POISONED(*sp >> 30); |
| 392 | EXPECT_NOT_POISONED(*sp >> 24); |
| 393 | EXPECT_POISONED(*sp >> 23); |
| 394 | EXPECT_POISONED(*sp >> 10); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 395 | |
| 396 | sp = GetPoisoned<S4>(); |
| 397 | ((S1*)sp)[1] = 0; |
| 398 | ((S1*)sp)[2] = 0; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 399 | EXPECT_POISONED(*sp >> 31); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 400 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 401 | EXPECT_POISONED(100 >> *GetPoisoned<S4>()); |
| 402 | EXPECT_POISONED(100U >> *GetPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | NOINLINE static int GetPoisonedZero() { |
| 406 | int *zero = new int; |
| 407 | *zero = 0; |
| 408 | __msan_poison(zero, sizeof(*zero)); |
| 409 | int res = *zero; |
| 410 | delete zero; |
| 411 | return res; |
| 412 | } |
| 413 | |
| 414 | TEST(MemorySanitizer, LoadFromDirtyAddress) { |
| 415 | int *a = new int; |
| 416 | *a = 0; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 417 | EXPECT_UMR(break_optimization((void*)(U8)a[GetPoisonedZero()])); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 418 | delete a; |
| 419 | } |
| 420 | |
| 421 | TEST(MemorySanitizer, StoreToDirtyAddress) { |
| 422 | int *a = new int; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 423 | EXPECT_UMR(a[GetPoisonedZero()] = 0); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 424 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 425 | delete a; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | NOINLINE void StackTestFunc() { |
| 430 | S4 p4; |
| 431 | S4 ok4 = 1; |
| 432 | S2 p2; |
| 433 | S2 ok2 = 1; |
| 434 | S1 p1; |
| 435 | S1 ok1 = 1; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 436 | break_optimization(&p4); |
| 437 | break_optimization(&ok4); |
| 438 | break_optimization(&p2); |
| 439 | break_optimization(&ok2); |
| 440 | break_optimization(&p1); |
| 441 | break_optimization(&ok1); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 442 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 443 | EXPECT_POISONED(p4); |
| 444 | EXPECT_POISONED(p2); |
| 445 | EXPECT_POISONED(p1); |
| 446 | EXPECT_NOT_POISONED(ok1); |
| 447 | EXPECT_NOT_POISONED(ok2); |
| 448 | EXPECT_NOT_POISONED(ok4); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | TEST(MemorySanitizer, StackTest) { |
| 452 | StackTestFunc(); |
| 453 | } |
| 454 | |
| 455 | NOINLINE void StackStressFunc() { |
| 456 | int foo[10000]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 457 | break_optimization(foo); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | TEST(MemorySanitizer, DISABLED_StackStressTest) { |
| 461 | for (int i = 0; i < 1000000; i++) |
| 462 | StackStressFunc(); |
| 463 | } |
| 464 | |
| 465 | template<class T> |
| 466 | void TestFloatingPoint() { |
| 467 | static volatile T v; |
| 468 | static T g[100]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 469 | break_optimization(&g); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 470 | T *x = GetPoisoned<T>(); |
| 471 | T *y = GetPoisoned<T>(1); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 472 | EXPECT_POISONED(*x); |
| 473 | EXPECT_POISONED((long long)*x); |
| 474 | EXPECT_POISONED((int)*x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 475 | g[0] = *x; |
| 476 | g[1] = *x + *y; |
| 477 | g[2] = *x - *y; |
| 478 | g[3] = *x * *y; |
| 479 | } |
| 480 | |
| 481 | TEST(MemorySanitizer, FloatingPointTest) { |
| 482 | TestFloatingPoint<float>(); |
| 483 | TestFloatingPoint<double>(); |
| 484 | } |
| 485 | |
| 486 | TEST(MemorySanitizer, DynMem) { |
| 487 | S4 x = 0; |
| 488 | S4 *y = GetPoisoned<S4>(); |
| 489 | memcpy(y, &x, g_one * sizeof(S4)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 490 | EXPECT_NOT_POISONED(*y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static char *DynRetTestStr; |
| 494 | |
| 495 | TEST(MemorySanitizer, DynRet) { |
| 496 | if (!__msan_has_dynamic_component()) return; |
| 497 | ReturnPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 498 | EXPECT_NOT_POISONED(clearenv()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | TEST(MemorySanitizer, DynRet1) { |
| 503 | if (!__msan_has_dynamic_component()) return; |
| 504 | ReturnPoisoned<S8>(); |
| 505 | } |
| 506 | |
| 507 | struct LargeStruct { |
| 508 | S4 x[10]; |
| 509 | }; |
| 510 | |
| 511 | NOINLINE |
| 512 | LargeStruct LargeRetTest() { |
| 513 | LargeStruct res; |
| 514 | res.x[0] = *GetPoisoned<S4>(); |
| 515 | res.x[1] = *GetPoisoned<S4>(); |
| 516 | res.x[2] = *GetPoisoned<S4>(); |
| 517 | res.x[3] = *GetPoisoned<S4>(); |
| 518 | res.x[4] = *GetPoisoned<S4>(); |
| 519 | res.x[5] = *GetPoisoned<S4>(); |
| 520 | res.x[6] = *GetPoisoned<S4>(); |
| 521 | res.x[7] = *GetPoisoned<S4>(); |
| 522 | res.x[8] = *GetPoisoned<S4>(); |
| 523 | res.x[9] = *GetPoisoned<S4>(); |
| 524 | return res; |
| 525 | } |
| 526 | |
Alexey Samsonov | 67505a8 | 2013-07-16 12:51:53 +0000 | [diff] [blame] | 527 | TEST(MemorySanitizer, strcmp) { |
| 528 | char s1[10]; |
| 529 | char s2[10]; |
| 530 | strncpy(s1, "foo", 10); |
| 531 | s2[0] = 'f'; |
| 532 | s2[1] = 'n'; |
| 533 | EXPECT_GT(strcmp(s1, s2), 0); |
| 534 | s2[1] = 'o'; |
| 535 | int res; |
| 536 | EXPECT_UMR(res = strcmp(s1, s2)); |
| 537 | EXPECT_NOT_POISONED(res); |
| 538 | EXPECT_EQ(strncmp(s1, s2, 1), 0); |
| 539 | } |
| 540 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 541 | TEST(MemorySanitizer, LargeRet) { |
| 542 | LargeStruct a = LargeRetTest(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 543 | EXPECT_POISONED(a.x[0]); |
| 544 | EXPECT_POISONED(a.x[9]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | TEST(MemorySanitizer, fread) { |
| 548 | char *x = new char[32]; |
| 549 | FILE *f = fopen("/proc/self/stat", "r"); |
| 550 | assert(f); |
| 551 | fread(x, 1, 32, f); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 552 | EXPECT_NOT_POISONED(x[0]); |
| 553 | EXPECT_NOT_POISONED(x[16]); |
| 554 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 555 | fclose(f); |
| 556 | delete x; |
| 557 | } |
| 558 | |
| 559 | TEST(MemorySanitizer, read) { |
| 560 | char *x = new char[32]; |
| 561 | int fd = open("/proc/self/stat", O_RDONLY); |
| 562 | assert(fd > 0); |
| 563 | int sz = read(fd, x, 32); |
| 564 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 565 | EXPECT_NOT_POISONED(x[0]); |
| 566 | EXPECT_NOT_POISONED(x[16]); |
| 567 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 568 | close(fd); |
| 569 | delete x; |
| 570 | } |
| 571 | |
| 572 | TEST(MemorySanitizer, pread) { |
| 573 | char *x = new char[32]; |
| 574 | int fd = open("/proc/self/stat", O_RDONLY); |
| 575 | assert(fd > 0); |
| 576 | int sz = pread(fd, x, 32, 0); |
| 577 | assert(sz == 32); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 578 | EXPECT_NOT_POISONED(x[0]); |
| 579 | EXPECT_NOT_POISONED(x[16]); |
| 580 | EXPECT_NOT_POISONED(x[31]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 581 | close(fd); |
| 582 | delete x; |
| 583 | } |
| 584 | |
Evgeniy Stepanov | b916e6a | 2013-06-24 10:43:23 +0000 | [diff] [blame] | 585 | TEST(MemorySanitizer, readv) { |
| 586 | char buf[2011]; |
| 587 | struct iovec iov[2]; |
| 588 | iov[0].iov_base = buf + 1; |
| 589 | iov[0].iov_len = 5; |
| 590 | iov[1].iov_base = buf + 10; |
| 591 | iov[1].iov_len = 2000; |
| 592 | int fd = open("/proc/self/stat", O_RDONLY); |
| 593 | assert(fd > 0); |
| 594 | int sz = readv(fd, iov, 2); |
| 595 | ASSERT_LT(sz, 5 + 2000); |
| 596 | ASSERT_GT(sz, iov[0].iov_len); |
| 597 | EXPECT_POISONED(buf[0]); |
| 598 | EXPECT_NOT_POISONED(buf[1]); |
| 599 | EXPECT_NOT_POISONED(buf[5]); |
| 600 | EXPECT_POISONED(buf[6]); |
| 601 | EXPECT_POISONED(buf[9]); |
| 602 | EXPECT_NOT_POISONED(buf[10]); |
| 603 | EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]); |
| 604 | EXPECT_POISONED(buf[11 + (sz - 1) - 5]); |
| 605 | close(fd); |
| 606 | } |
| 607 | |
| 608 | TEST(MemorySanitizer, preadv) { |
| 609 | char buf[2011]; |
| 610 | struct iovec iov[2]; |
| 611 | iov[0].iov_base = buf + 1; |
| 612 | iov[0].iov_len = 5; |
| 613 | iov[1].iov_base = buf + 10; |
| 614 | iov[1].iov_len = 2000; |
| 615 | int fd = open("/proc/self/stat", O_RDONLY); |
| 616 | assert(fd > 0); |
| 617 | int sz = preadv(fd, iov, 2, 3); |
| 618 | ASSERT_LT(sz, 5 + 2000); |
| 619 | ASSERT_GT(sz, iov[0].iov_len); |
| 620 | EXPECT_POISONED(buf[0]); |
| 621 | EXPECT_NOT_POISONED(buf[1]); |
| 622 | EXPECT_NOT_POISONED(buf[5]); |
| 623 | EXPECT_POISONED(buf[6]); |
| 624 | EXPECT_POISONED(buf[9]); |
| 625 | EXPECT_NOT_POISONED(buf[10]); |
| 626 | EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]); |
| 627 | EXPECT_POISONED(buf[11 + (sz - 1) - 5]); |
| 628 | close(fd); |
| 629 | } |
| 630 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 631 | // FIXME: fails now. |
| 632 | TEST(MemorySanitizer, DISABLED_ioctl) { |
| 633 | struct winsize ws; |
| 634 | EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 635 | EXPECT_NOT_POISONED(ws.ws_col); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | TEST(MemorySanitizer, readlink) { |
| 639 | char *x = new char[1000]; |
| 640 | readlink("/proc/self/exe", x, 1000); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 641 | EXPECT_NOT_POISONED(x[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 642 | delete [] x; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | TEST(MemorySanitizer, stat) { |
| 647 | struct stat* st = new struct stat; |
| 648 | int res = stat("/proc/self/stat", st); |
| 649 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 650 | EXPECT_NOT_POISONED(st->st_dev); |
| 651 | EXPECT_NOT_POISONED(st->st_mode); |
| 652 | EXPECT_NOT_POISONED(st->st_size); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | TEST(MemorySanitizer, statfs) { |
| 656 | struct statfs* st = new struct statfs; |
| 657 | int res = statfs("/", st); |
| 658 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 659 | EXPECT_NOT_POISONED(st->f_type); |
| 660 | EXPECT_NOT_POISONED(st->f_bfree); |
| 661 | EXPECT_NOT_POISONED(st->f_namelen); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | TEST(MemorySanitizer, pipe) { |
| 665 | int* pipefd = new int[2]; |
| 666 | int res = pipe(pipefd); |
| 667 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 668 | EXPECT_NOT_POISONED(pipefd[0]); |
| 669 | EXPECT_NOT_POISONED(pipefd[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 670 | close(pipefd[0]); |
| 671 | close(pipefd[1]); |
| 672 | } |
| 673 | |
Evgeniy Stepanov | 134fe8a | 2013-04-08 13:45:12 +0000 | [diff] [blame] | 674 | TEST(MemorySanitizer, pipe2) { |
| 675 | int* pipefd = new int[2]; |
| 676 | int res = pipe2(pipefd, O_NONBLOCK); |
| 677 | assert(!res); |
| 678 | EXPECT_NOT_POISONED(pipefd[0]); |
| 679 | EXPECT_NOT_POISONED(pipefd[1]); |
| 680 | close(pipefd[0]); |
| 681 | close(pipefd[1]); |
| 682 | } |
| 683 | |
| 684 | TEST(MemorySanitizer, socketpair) { |
| 685 | int sv[2]; |
| 686 | int res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| 687 | assert(!res); |
| 688 | EXPECT_NOT_POISONED(sv[0]); |
| 689 | EXPECT_NOT_POISONED(sv[1]); |
| 690 | close(sv[0]); |
| 691 | close(sv[1]); |
| 692 | } |
| 693 | |
Evgeniy Stepanov | 9f58c5c | 2013-05-22 13:46:22 +0000 | [diff] [blame] | 694 | TEST(MemorySanitizer, bind_getsockname) { |
| 695 | int sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 696 | |
| 697 | struct sockaddr_in sai; |
| 698 | memset(&sai, 0, sizeof(sai)); |
| 699 | sai.sin_family = AF_UNIX; |
| 700 | int res = bind(sock, (struct sockaddr *)&sai, sizeof(sai)); |
| 701 | |
| 702 | assert(!res); |
| 703 | char buf[200]; |
| 704 | socklen_t addrlen; |
| 705 | EXPECT_UMR(getsockname(sock, (struct sockaddr *)&buf, &addrlen)); |
| 706 | |
| 707 | addrlen = sizeof(buf); |
| 708 | res = getsockname(sock, (struct sockaddr *)&buf, &addrlen); |
| 709 | EXPECT_NOT_POISONED(addrlen); |
| 710 | EXPECT_NOT_POISONED(buf[0]); |
| 711 | EXPECT_NOT_POISONED(buf[addrlen - 1]); |
| 712 | EXPECT_POISONED(buf[addrlen]); |
| 713 | close(sock); |
| 714 | } |
| 715 | |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 716 | TEST(MemorySanitizer, accept) { |
| 717 | int listen_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 718 | ASSERT_LT(0, listen_socket); |
| 719 | |
| 720 | struct sockaddr_in sai; |
| 721 | sai.sin_family = AF_INET; |
| 722 | sai.sin_port = 0; |
| 723 | sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 724 | int res = bind(listen_socket, (struct sockaddr *)&sai, sizeof(sai)); |
| 725 | ASSERT_EQ(0, res); |
| 726 | |
| 727 | res = listen(listen_socket, 1); |
| 728 | ASSERT_EQ(0, res); |
| 729 | |
| 730 | socklen_t sz = sizeof(sai); |
| 731 | res = getsockname(listen_socket, (struct sockaddr *)&sai, &sz); |
| 732 | ASSERT_EQ(0, res); |
| 733 | ASSERT_EQ(sizeof(sai), sz); |
Evgeniy Stepanov | bc33e13 | 2013-05-29 11:49:25 +0000 | [diff] [blame] | 734 | |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 735 | int connect_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 736 | ASSERT_LT(0, connect_socket); |
| 737 | res = fcntl(connect_socket, F_SETFL, O_NONBLOCK); |
| 738 | ASSERT_EQ(0, res); |
| 739 | res = connect(connect_socket, (struct sockaddr *)&sai, sizeof(sai)); |
| 740 | ASSERT_EQ(-1, res); |
| 741 | ASSERT_EQ(EINPROGRESS, errno); |
| 742 | |
| 743 | __msan_poison(&sai, sizeof(sai)); |
Evgeniy Stepanov | bc33e13 | 2013-05-29 11:49:25 +0000 | [diff] [blame] | 744 | int new_sock = accept(listen_socket, (struct sockaddr *)&sai, &sz); |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 745 | ASSERT_LT(0, new_sock); |
| 746 | ASSERT_EQ(sizeof(sai), sz); |
| 747 | EXPECT_NOT_POISONED(sai); |
| 748 | |
Evgeniy Stepanov | bc33e13 | 2013-05-29 11:49:25 +0000 | [diff] [blame] | 749 | __msan_poison(&sai, sizeof(sai)); |
| 750 | res = getpeername(new_sock, (struct sockaddr *)&sai, &sz); |
| 751 | ASSERT_EQ(0, res); |
| 752 | ASSERT_EQ(sizeof(sai), sz); |
| 753 | EXPECT_NOT_POISONED(sai); |
| 754 | |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 755 | close(new_sock); |
| 756 | close(connect_socket); |
| 757 | close(listen_socket); |
| 758 | } |
| 759 | |
Evgeniy Stepanov | 512c616 | 2013-05-29 12:33:31 +0000 | [diff] [blame] | 760 | TEST(MemorySanitizer, getaddrinfo) { |
| 761 | struct addrinfo *ai; |
| 762 | struct addrinfo hints; |
| 763 | memset(&hints, 0, sizeof(hints)); |
| 764 | hints.ai_family = AF_INET; |
| 765 | int res = getaddrinfo("localhost", NULL, &hints, &ai); |
| 766 | ASSERT_EQ(0, res); |
| 767 | EXPECT_NOT_POISONED(*ai); |
| 768 | ASSERT_EQ(sizeof(sockaddr_in), ai->ai_addrlen); |
| 769 | EXPECT_NOT_POISONED(*(sockaddr_in*)ai->ai_addr); |
| 770 | } |
| 771 | |
Evgeniy Stepanov | 9eedf48 | 2013-07-01 13:51:31 +0000 | [diff] [blame] | 772 | TEST(MemorySanitizer, getnameinfo) { |
| 773 | struct sockaddr_in sai; |
| 774 | sai.sin_family = AF_INET; |
| 775 | sai.sin_port = 80; |
| 776 | sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 777 | char host[500]; |
| 778 | char serv[500]; |
| 779 | int res = getnameinfo((struct sockaddr *)&sai, sizeof(sai), host, |
| 780 | sizeof(host), serv, sizeof(serv), 0); |
| 781 | ASSERT_EQ(0, res); |
| 782 | EXPECT_NOT_POISONED(host[0]); |
| 783 | EXPECT_POISONED(host[sizeof(host) - 1]); |
| 784 | |
| 785 | ASSERT_NE(0, strlen(host)); |
| 786 | EXPECT_NOT_POISONED(serv[0]); |
| 787 | EXPECT_POISONED(serv[sizeof(serv) - 1]); |
| 788 | ASSERT_NE(0, strlen(serv)); |
| 789 | } |
| 790 | |
Evgeniy Stepanov | 0a2cc37 | 2013-05-23 11:10:23 +0000 | [diff] [blame] | 791 | #define EXPECT_HOSTENT_NOT_POISONED(he) \ |
| 792 | do { \ |
| 793 | EXPECT_NOT_POISONED(*(he)); \ |
| 794 | ASSERT_NE((void *) 0, (he)->h_name); \ |
| 795 | ASSERT_NE((void *) 0, (he)->h_aliases); \ |
| 796 | ASSERT_NE((void *) 0, (he)->h_addr_list); \ |
| 797 | EXPECT_NOT_POISONED(strlen((he)->h_name)); \ |
| 798 | char **p = (he)->h_aliases; \ |
| 799 | while (*p) { \ |
| 800 | EXPECT_NOT_POISONED(strlen(*p)); \ |
| 801 | ++p; \ |
| 802 | } \ |
| 803 | char **q = (he)->h_addr_list; \ |
| 804 | while (*q) { \ |
| 805 | EXPECT_NOT_POISONED(*q[0]); \ |
| 806 | ++q; \ |
| 807 | } \ |
| 808 | EXPECT_NOT_POISONED(*q); \ |
| 809 | } while (0) |
| 810 | |
| 811 | TEST(MemorySanitizer, gethostent) { |
| 812 | struct hostent *he = gethostent(); |
| 813 | ASSERT_NE((void *)NULL, he); |
| 814 | EXPECT_HOSTENT_NOT_POISONED(he); |
| 815 | } |
| 816 | |
Evgeniy Stepanov | 263800b | 2013-06-27 13:21:00 +0000 | [diff] [blame] | 817 | #ifndef MSAN_TEST_DISABLE_GETHOSTBYNAME |
| 818 | |
Evgeniy Stepanov | 0a2cc37 | 2013-05-23 11:10:23 +0000 | [diff] [blame] | 819 | TEST(MemorySanitizer, gethostbyname) { |
| 820 | struct hostent *he = gethostbyname("localhost"); |
| 821 | ASSERT_NE((void *)NULL, he); |
| 822 | EXPECT_HOSTENT_NOT_POISONED(he); |
| 823 | } |
| 824 | |
Evgeniy Stepanov | 263800b | 2013-06-27 13:21:00 +0000 | [diff] [blame] | 825 | #endif // MSAN_TEST_DISABLE_GETHOSTBYNAME |
| 826 | |
Evgeniy Stepanov | 0a2cc37 | 2013-05-23 11:10:23 +0000 | [diff] [blame] | 827 | TEST(MemorySanitizer, gethostbyname2) { |
| 828 | struct hostent *he = gethostbyname2("localhost", AF_INET); |
| 829 | ASSERT_NE((void *)NULL, he); |
| 830 | EXPECT_HOSTENT_NOT_POISONED(he); |
| 831 | } |
| 832 | |
| 833 | TEST(MemorySanitizer, gethostbyaddr) { |
| 834 | in_addr_t addr = inet_addr("127.0.0.1"); |
| 835 | EXPECT_NOT_POISONED(addr); |
| 836 | struct hostent *he = gethostbyaddr(&addr, sizeof(addr), AF_INET); |
| 837 | ASSERT_NE((void *)NULL, he); |
| 838 | EXPECT_HOSTENT_NOT_POISONED(he); |
| 839 | } |
| 840 | |
| 841 | TEST(MemorySanitizer, gethostent_r) { |
| 842 | char buf[2000]; |
| 843 | struct hostent he; |
| 844 | struct hostent *result; |
| 845 | int err; |
| 846 | int res = gethostent_r(&he, buf, sizeof(buf), &result, &err); |
| 847 | ASSERT_EQ(0, res); |
| 848 | EXPECT_NOT_POISONED(result); |
| 849 | ASSERT_NE((void *)NULL, result); |
| 850 | EXPECT_HOSTENT_NOT_POISONED(result); |
| 851 | EXPECT_NOT_POISONED(err); |
| 852 | } |
| 853 | |
| 854 | TEST(MemorySanitizer, gethostbyname_r) { |
| 855 | char buf[2000]; |
| 856 | struct hostent he; |
| 857 | struct hostent *result; |
| 858 | int err; |
| 859 | int res = gethostbyname_r("localhost", &he, buf, sizeof(buf), &result, &err); |
| 860 | ASSERT_EQ(0, res); |
| 861 | EXPECT_NOT_POISONED(result); |
| 862 | ASSERT_NE((void *)NULL, result); |
| 863 | EXPECT_HOSTENT_NOT_POISONED(result); |
| 864 | EXPECT_NOT_POISONED(err); |
| 865 | } |
| 866 | |
| 867 | TEST(MemorySanitizer, gethostbyname2_r) { |
| 868 | char buf[2000]; |
| 869 | struct hostent he; |
| 870 | struct hostent *result; |
| 871 | int err; |
| 872 | int res = gethostbyname2_r("localhost", AF_INET, &he, buf, sizeof(buf), |
| 873 | &result, &err); |
| 874 | ASSERT_EQ(0, res); |
| 875 | EXPECT_NOT_POISONED(result); |
| 876 | ASSERT_NE((void *)NULL, result); |
| 877 | EXPECT_HOSTENT_NOT_POISONED(result); |
| 878 | EXPECT_NOT_POISONED(err); |
| 879 | } |
| 880 | |
| 881 | TEST(MemorySanitizer, gethostbyaddr_r) { |
| 882 | char buf[2000]; |
| 883 | struct hostent he; |
| 884 | struct hostent *result; |
| 885 | int err; |
| 886 | in_addr_t addr = inet_addr("127.0.0.1"); |
| 887 | EXPECT_NOT_POISONED(addr); |
| 888 | int res = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &he, buf, sizeof(buf), |
| 889 | &result, &err); |
| 890 | ASSERT_EQ(0, res); |
| 891 | EXPECT_NOT_POISONED(result); |
| 892 | ASSERT_NE((void *)NULL, result); |
| 893 | EXPECT_HOSTENT_NOT_POISONED(result); |
| 894 | EXPECT_NOT_POISONED(err); |
| 895 | } |
| 896 | |
Evgeniy Stepanov | f32be42 | 2013-05-23 11:38:08 +0000 | [diff] [blame] | 897 | TEST(MemorySanitizer, getsockopt) { |
| 898 | int sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 899 | struct linger l[2]; |
| 900 | socklen_t sz = sizeof(l[0]); |
| 901 | int res = getsockopt(sock, SOL_SOCKET, SO_LINGER, &l[0], &sz); |
| 902 | ASSERT_EQ(0, res); |
| 903 | ASSERT_EQ(sizeof(l[0]), sz); |
| 904 | EXPECT_NOT_POISONED(l[0]); |
| 905 | EXPECT_POISONED(*(char *)(l + 1)); |
| 906 | } |
| 907 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 908 | TEST(MemorySanitizer, getcwd) { |
| 909 | char path[PATH_MAX + 1]; |
| 910 | char* res = getcwd(path, sizeof(path)); |
| 911 | assert(res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 912 | EXPECT_NOT_POISONED(path[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Evgeniy Stepanov | 7eed04c | 2013-02-12 14:36:22 +0000 | [diff] [blame] | 915 | TEST(MemorySanitizer, getcwd_gnu) { |
| 916 | char* res = getcwd(NULL, 0); |
| 917 | assert(res); |
| 918 | EXPECT_NOT_POISONED(res[0]); |
| 919 | free(res); |
| 920 | } |
| 921 | |
Evgeniy Stepanov | 8014489 | 2013-07-02 13:34:44 +0000 | [diff] [blame] | 922 | TEST(MemorySanitizer, get_current_dir_name) { |
| 923 | char* res = get_current_dir_name(); |
| 924 | assert(res); |
| 925 | EXPECT_NOT_POISONED(res[0]); |
| 926 | free(res); |
| 927 | } |
| 928 | |
Evgeniy Stepanov | 5ec19bc | 2013-07-30 12:46:59 +0000 | [diff] [blame^] | 929 | TEST(MemorySanitizer, confstr) { |
| 930 | char buf[3]; |
| 931 | size_t res = confstr(_CS_PATH, buf, sizeof(buf)); |
| 932 | ASSERT_GT(res, sizeof(buf)); |
| 933 | EXPECT_NOT_POISONED(buf[0]); |
| 934 | EXPECT_NOT_POISONED(buf[sizeof(buf) - 1]); |
| 935 | |
| 936 | char buf2[1000]; |
| 937 | res = confstr(_CS_PATH, buf2, sizeof(buf2)); |
| 938 | ASSERT_LT(res, sizeof(buf2)); |
| 939 | EXPECT_NOT_POISONED(buf2[0]); |
| 940 | EXPECT_NOT_POISONED(buf2[res - 1]); |
| 941 | EXPECT_POISONED(buf2[res]); |
| 942 | ASSERT_EQ(res, strlen(buf2) + 1); |
| 943 | } |
| 944 | |
Evgeniy Stepanov | d97a15a | 2013-03-14 12:49:23 +0000 | [diff] [blame] | 945 | TEST(MemorySanitizer, readdir) { |
| 946 | DIR *dir = opendir("."); |
| 947 | struct dirent *d = readdir(dir); |
| 948 | assert(d); |
| 949 | EXPECT_NOT_POISONED(d->d_name[0]); |
| 950 | closedir(dir); |
| 951 | } |
| 952 | |
Evgeniy Stepanov | b5cf98f | 2013-06-26 15:00:53 +0000 | [diff] [blame] | 953 | TEST(MemorySanitizer, readdir_r) { |
| 954 | DIR *dir = opendir("."); |
| 955 | struct dirent d; |
| 956 | struct dirent *pd; |
| 957 | int res = readdir_r(dir, &d, &pd); |
| 958 | assert(!res); |
| 959 | EXPECT_NOT_POISONED(pd); |
| 960 | EXPECT_NOT_POISONED(d.d_name[0]); |
| 961 | closedir(dir); |
| 962 | } |
| 963 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 964 | TEST(MemorySanitizer, realpath) { |
| 965 | const char* relpath = "."; |
| 966 | char path[PATH_MAX + 1]; |
| 967 | char* res = realpath(relpath, path); |
| 968 | assert(res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 969 | EXPECT_NOT_POISONED(path[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Evgeniy Stepanov | 12eb79d | 2013-07-09 09:53:37 +0000 | [diff] [blame] | 972 | TEST(MemorySanitizer, realpath_null) { |
| 973 | const char* relpath = "."; |
| 974 | char* res = realpath(relpath, NULL); |
| 975 | printf("%d, %s\n", errno, strerror(errno)); |
| 976 | assert(res); |
| 977 | EXPECT_NOT_POISONED(res[0]); |
| 978 | free(res); |
| 979 | } |
| 980 | |
| 981 | TEST(MemorySanitizer, canonicalize_file_name) { |
| 982 | const char* relpath = "."; |
| 983 | char* res = canonicalize_file_name(relpath); |
| 984 | assert(res); |
| 985 | EXPECT_NOT_POISONED(res[0]); |
| 986 | free(res); |
| 987 | } |
| 988 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 989 | TEST(MemorySanitizer, memcpy) { |
| 990 | char* x = new char[2]; |
| 991 | char* y = new char[2]; |
| 992 | x[0] = 1; |
| 993 | x[1] = *GetPoisoned<char>(); |
| 994 | memcpy(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 995 | EXPECT_NOT_POISONED(y[0]); |
| 996 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | TEST(MemorySanitizer, memmove) { |
| 1000 | char* x = new char[2]; |
| 1001 | char* y = new char[2]; |
| 1002 | x[0] = 1; |
| 1003 | x[1] = *GetPoisoned<char>(); |
| 1004 | memmove(y, x, 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1005 | EXPECT_NOT_POISONED(y[0]); |
| 1006 | EXPECT_POISONED(y[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1009 | TEST(MemorySanitizer, bcopy) { |
| 1010 | char* x = new char[2]; |
| 1011 | char* y = new char[2]; |
| 1012 | x[0] = 1; |
| 1013 | x[1] = *GetPoisoned<char>(); |
| 1014 | bcopy(x, y, 2); |
| 1015 | EXPECT_NOT_POISONED(y[0]); |
| 1016 | EXPECT_POISONED(y[1]); |
| 1017 | } |
| 1018 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1019 | TEST(MemorySanitizer, strdup) { |
Evgeniy Stepanov | 8aa1ae0 | 2013-03-14 11:10:36 +0000 | [diff] [blame] | 1020 | char buf[4] = "abc"; |
| 1021 | __msan_poison(buf + 2, sizeof(*buf)); |
| 1022 | char *x = strdup(buf); |
| 1023 | EXPECT_NOT_POISONED(x[0]); |
| 1024 | EXPECT_NOT_POISONED(x[1]); |
| 1025 | EXPECT_POISONED(x[2]); |
| 1026 | EXPECT_NOT_POISONED(x[3]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1027 | free(x); |
| 1028 | } |
| 1029 | |
Evgeniy Stepanov | 8aa1ae0 | 2013-03-14 11:10:36 +0000 | [diff] [blame] | 1030 | TEST(MemorySanitizer, strndup) { |
| 1031 | char buf[4] = "abc"; |
| 1032 | __msan_poison(buf + 2, sizeof(*buf)); |
| 1033 | char *x = strndup(buf, 3); |
| 1034 | EXPECT_NOT_POISONED(x[0]); |
| 1035 | EXPECT_NOT_POISONED(x[1]); |
| 1036 | EXPECT_POISONED(x[2]); |
| 1037 | EXPECT_NOT_POISONED(x[3]); |
| 1038 | free(x); |
| 1039 | } |
| 1040 | |
| 1041 | TEST(MemorySanitizer, strndup_short) { |
| 1042 | char buf[4] = "abc"; |
| 1043 | __msan_poison(buf + 1, sizeof(*buf)); |
| 1044 | __msan_poison(buf + 2, sizeof(*buf)); |
| 1045 | char *x = strndup(buf, 2); |
| 1046 | EXPECT_NOT_POISONED(x[0]); |
| 1047 | EXPECT_POISONED(x[1]); |
Evgeniy Stepanov | 1d21bd1 | 2013-03-14 11:58:13 +0000 | [diff] [blame] | 1048 | EXPECT_NOT_POISONED(x[2]); |
Evgeniy Stepanov | 8aa1ae0 | 2013-03-14 11:10:36 +0000 | [diff] [blame] | 1049 | free(x); |
| 1050 | } |
| 1051 | |
| 1052 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1053 | template<class T, int size> |
| 1054 | void TestOverlapMemmove() { |
| 1055 | T *x = new T[size]; |
| 1056 | assert(size >= 3); |
| 1057 | x[2] = 0; |
| 1058 | memmove(x, x + 1, (size - 1) * sizeof(T)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1059 | EXPECT_NOT_POISONED(x[1]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1060 | if (!__msan_has_dynamic_component()) { |
| 1061 | // FIXME: under DR we will lose this information |
| 1062 | // because accesses in memmove will unpoisin the shadow. |
| 1063 | // We need to use our own memove implementation instead of libc's. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1064 | EXPECT_POISONED(x[0]); |
| 1065 | EXPECT_POISONED(x[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1066 | } |
| 1067 | delete [] x; |
| 1068 | } |
| 1069 | |
| 1070 | TEST(MemorySanitizer, overlap_memmove) { |
| 1071 | TestOverlapMemmove<U1, 10>(); |
| 1072 | TestOverlapMemmove<U1, 1000>(); |
| 1073 | TestOverlapMemmove<U8, 4>(); |
| 1074 | TestOverlapMemmove<U8, 1000>(); |
| 1075 | } |
| 1076 | |
| 1077 | TEST(MemorySanitizer, strcpy) { // NOLINT |
| 1078 | char* x = new char[3]; |
| 1079 | char* y = new char[3]; |
| 1080 | x[0] = 'a'; |
| 1081 | x[1] = *GetPoisoned<char>(1, 1); |
| 1082 | x[2] = 0; |
| 1083 | strcpy(y, x); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1084 | EXPECT_NOT_POISONED(y[0]); |
| 1085 | EXPECT_POISONED(y[1]); |
| 1086 | EXPECT_NOT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | TEST(MemorySanitizer, strncpy) { // NOLINT |
| 1090 | char* x = new char[3]; |
| 1091 | char* y = new char[3]; |
| 1092 | x[0] = 'a'; |
| 1093 | x[1] = *GetPoisoned<char>(1, 1); |
| 1094 | x[2] = 0; |
| 1095 | strncpy(y, x, 2); // NOLINT |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1096 | EXPECT_NOT_POISONED(y[0]); |
| 1097 | EXPECT_POISONED(y[1]); |
| 1098 | EXPECT_POISONED(y[2]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Evgeniy Stepanov | 4bbbe13 | 2013-07-01 15:19:37 +0000 | [diff] [blame] | 1101 | TEST(MemorySanitizer, stpcpy) { // NOLINT |
| 1102 | char* x = new char[3]; |
| 1103 | char* y = new char[3]; |
| 1104 | x[0] = 'a'; |
| 1105 | x[1] = *GetPoisoned<char>(1, 1); |
| 1106 | x[2] = 0; |
| 1107 | char *res = stpcpy(y, x); // NOLINT |
| 1108 | ASSERT_EQ(res, y + 2); |
| 1109 | EXPECT_NOT_POISONED(y[0]); |
| 1110 | EXPECT_POISONED(y[1]); |
| 1111 | EXPECT_NOT_POISONED(y[2]); |
| 1112 | } |
| 1113 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1114 | TEST(MemorySanitizer, strtol) { |
| 1115 | char *e; |
| 1116 | assert(1 == strtol("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1117 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | TEST(MemorySanitizer, strtoll) { |
| 1121 | char *e; |
| 1122 | assert(1 == strtoll("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1123 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | TEST(MemorySanitizer, strtoul) { |
| 1127 | char *e; |
| 1128 | assert(1 == strtoul("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1129 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | TEST(MemorySanitizer, strtoull) { |
| 1133 | char *e; |
| 1134 | assert(1 == strtoull("1", &e, 10)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1135 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1138 | TEST(MemorySanitizer, strtoimax) { |
| 1139 | char *e; |
| 1140 | assert(1 == strtoimax("1", &e, 10)); |
| 1141 | EXPECT_NOT_POISONED((S8) e); |
| 1142 | } |
| 1143 | |
| 1144 | TEST(MemorySanitizer, strtoumax) { |
| 1145 | char *e; |
| 1146 | assert(1 == strtoumax("1", &e, 10)); |
| 1147 | EXPECT_NOT_POISONED((S8) e); |
| 1148 | } |
| 1149 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1150 | TEST(MemorySanitizer, strtod) { |
| 1151 | char *e; |
| 1152 | assert(0 != strtod("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1153 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | TEST(MemorySanitizer, strtof) { |
| 1157 | char *e; |
| 1158 | assert(0 != strtof("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1159 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | TEST(MemorySanitizer, strtold) { |
| 1163 | char *e; |
| 1164 | assert(0 != strtold("1.5", &e)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1165 | EXPECT_NOT_POISONED((S8) e); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Evgeniy Stepanov | c87088b | 2013-05-29 10:03:11 +0000 | [diff] [blame] | 1168 | TEST(MemorySanitizer, modf) { |
| 1169 | double x, y; |
| 1170 | x = modf(2.1, &y); |
| 1171 | EXPECT_NOT_POISONED(y); |
| 1172 | } |
| 1173 | |
| 1174 | TEST(MemorySanitizer, modff) { |
| 1175 | float x, y; |
| 1176 | x = modff(2.1, &y); |
| 1177 | EXPECT_NOT_POISONED(y); |
| 1178 | } |
| 1179 | |
| 1180 | TEST(MemorySanitizer, modfl) { |
| 1181 | long double x, y; |
| 1182 | x = modfl(2.1, &y); |
| 1183 | EXPECT_NOT_POISONED(y); |
| 1184 | } |
| 1185 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1186 | TEST(MemorySanitizer, sprintf) { // NOLINT |
| 1187 | char buff[10]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1188 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1189 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1190 | int res = sprintf(buff, "%d", 1234567); // NOLINT |
| 1191 | assert(res == 7); |
| 1192 | assert(buff[0] == '1'); |
| 1193 | assert(buff[1] == '2'); |
| 1194 | assert(buff[2] == '3'); |
| 1195 | assert(buff[6] == '7'); |
| 1196 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1197 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | TEST(MemorySanitizer, snprintf) { |
| 1201 | char buff[10]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1202 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1203 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1204 | int res = snprintf(buff, sizeof(buff), "%d", 1234567); |
| 1205 | assert(res == 7); |
| 1206 | assert(buff[0] == '1'); |
| 1207 | assert(buff[1] == '2'); |
| 1208 | assert(buff[2] == '3'); |
| 1209 | assert(buff[6] == '7'); |
| 1210 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1211 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | TEST(MemorySanitizer, swprintf) { |
| 1215 | wchar_t buff[10]; |
| 1216 | assert(sizeof(wchar_t) == 4); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1217 | break_optimization(buff); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1218 | EXPECT_POISONED(buff[0]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1219 | int res = swprintf(buff, 9, L"%d", 1234567); |
| 1220 | assert(res == 7); |
| 1221 | assert(buff[0] == '1'); |
| 1222 | assert(buff[1] == '2'); |
| 1223 | assert(buff[2] == '3'); |
| 1224 | assert(buff[6] == '7'); |
| 1225 | assert(buff[7] == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1226 | EXPECT_POISONED(buff[8]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 1229 | TEST(MemorySanitizer, asprintf) { // NOLINT |
| 1230 | char *pbuf; |
| 1231 | EXPECT_POISONED(pbuf); |
| 1232 | int res = asprintf(&pbuf, "%d", 1234567); // NOLINT |
| 1233 | assert(res == 7); |
| 1234 | EXPECT_NOT_POISONED(pbuf); |
| 1235 | assert(pbuf[0] == '1'); |
| 1236 | assert(pbuf[1] == '2'); |
| 1237 | assert(pbuf[2] == '3'); |
| 1238 | assert(pbuf[6] == '7'); |
| 1239 | assert(pbuf[7] == 0); |
| 1240 | free(pbuf); |
| 1241 | } |
| 1242 | |
Alexey Samsonov | e43d210 | 2013-07-12 11:59:58 +0000 | [diff] [blame] | 1243 | TEST(MemorySanitizer, mbstowcs) { |
| 1244 | const char *x = "abc"; |
| 1245 | wchar_t buff[10]; |
| 1246 | int res = mbstowcs(buff, x, 2); |
| 1247 | EXPECT_EQ(2, res); |
| 1248 | EXPECT_EQ(L'a', buff[0]); |
| 1249 | EXPECT_EQ(L'b', buff[1]); |
| 1250 | EXPECT_POISONED(buff[2]); |
| 1251 | res = mbstowcs(buff, x, 10); |
| 1252 | EXPECT_EQ(3, res); |
| 1253 | EXPECT_NOT_POISONED(buff[3]); |
| 1254 | } |
| 1255 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1256 | TEST(MemorySanitizer, wcstombs) { |
| 1257 | const wchar_t *x = L"abc"; |
| 1258 | char buff[10]; |
| 1259 | int res = wcstombs(buff, x, 4); |
| 1260 | EXPECT_EQ(res, 3); |
| 1261 | EXPECT_EQ(buff[0], 'a'); |
| 1262 | EXPECT_EQ(buff[1], 'b'); |
| 1263 | EXPECT_EQ(buff[2], 'c'); |
| 1264 | } |
| 1265 | |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1266 | TEST(MemorySanitizer, wcsrtombs) { |
| 1267 | const wchar_t *x = L"abc"; |
| 1268 | const wchar_t *p = x; |
| 1269 | char buff[10]; |
| 1270 | mbstate_t mbs; |
Evgeniy Stepanov | 57a1095 | 2013-07-10 14:17:46 +0000 | [diff] [blame] | 1271 | memset(&mbs, 0, sizeof(mbs)); |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1272 | int res = wcsrtombs(buff, &p, 4, &mbs); |
| 1273 | EXPECT_EQ(res, 3); |
| 1274 | EXPECT_EQ(buff[0], 'a'); |
| 1275 | EXPECT_EQ(buff[1], 'b'); |
| 1276 | EXPECT_EQ(buff[2], 'c'); |
Evgeniy Stepanov | 9b21ba6 | 2013-07-10 14:01:51 +0000 | [diff] [blame] | 1277 | EXPECT_EQ(buff[3], '\0'); |
| 1278 | EXPECT_POISONED(buff[4]); |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | TEST(MemorySanitizer, wcsnrtombs) { |
| 1282 | const wchar_t *x = L"abc"; |
| 1283 | const wchar_t *p = x; |
| 1284 | char buff[10]; |
| 1285 | mbstate_t mbs; |
Evgeniy Stepanov | 57a1095 | 2013-07-10 14:17:46 +0000 | [diff] [blame] | 1286 | memset(&mbs, 0, sizeof(mbs)); |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1287 | int res = wcsnrtombs(buff, &p, 2, 4, &mbs); |
| 1288 | EXPECT_EQ(res, 2); |
| 1289 | EXPECT_EQ(buff[0], 'a'); |
| 1290 | EXPECT_EQ(buff[1], 'b'); |
Evgeniy Stepanov | 9b21ba6 | 2013-07-10 14:01:51 +0000 | [diff] [blame] | 1291 | EXPECT_POISONED(buff[2]); |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Evgeniy Stepanov | 8014489 | 2013-07-02 13:34:44 +0000 | [diff] [blame] | 1294 | TEST(MemorySanitizer, mbtowc) { |
| 1295 | const char *x = "abc"; |
| 1296 | wchar_t wx; |
| 1297 | int res = mbtowc(&wx, x, 3); |
| 1298 | EXPECT_GT(res, 0); |
| 1299 | EXPECT_NOT_POISONED(wx); |
| 1300 | } |
| 1301 | |
| 1302 | TEST(MemorySanitizer, mbrtowc) { |
| 1303 | const char *x = "abc"; |
| 1304 | wchar_t wx; |
| 1305 | mbstate_t mbs; |
| 1306 | memset(&mbs, 0, sizeof(mbs)); |
| 1307 | int res = mbrtowc(&wx, x, 3, &mbs); |
| 1308 | EXPECT_GT(res, 0); |
| 1309 | EXPECT_NOT_POISONED(wx); |
| 1310 | } |
| 1311 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1312 | TEST(MemorySanitizer, gettimeofday) { |
| 1313 | struct timeval tv; |
| 1314 | struct timezone tz; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1315 | break_optimization(&tv); |
| 1316 | break_optimization(&tz); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1317 | assert(sizeof(tv) == 16); |
| 1318 | assert(sizeof(tz) == 8); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1319 | EXPECT_POISONED(tv.tv_sec); |
| 1320 | EXPECT_POISONED(tv.tv_usec); |
| 1321 | EXPECT_POISONED(tz.tz_minuteswest); |
| 1322 | EXPECT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1323 | assert(0 == gettimeofday(&tv, &tz)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1324 | EXPECT_NOT_POISONED(tv.tv_sec); |
| 1325 | EXPECT_NOT_POISONED(tv.tv_usec); |
| 1326 | EXPECT_NOT_POISONED(tz.tz_minuteswest); |
| 1327 | EXPECT_NOT_POISONED(tz.tz_dsttime); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 1330 | TEST(MemorySanitizer, clock_gettime) { |
| 1331 | struct timespec tp; |
| 1332 | EXPECT_POISONED(tp.tv_sec); |
| 1333 | EXPECT_POISONED(tp.tv_nsec); |
| 1334 | assert(0 == clock_gettime(CLOCK_REALTIME, &tp)); |
| 1335 | EXPECT_NOT_POISONED(tp.tv_sec); |
| 1336 | EXPECT_NOT_POISONED(tp.tv_nsec); |
| 1337 | } |
| 1338 | |
Evgeniy Stepanov | 7cdae16 | 2013-04-23 11:48:31 +0000 | [diff] [blame] | 1339 | TEST(MemorySanitizer, clock_getres) { |
| 1340 | struct timespec tp; |
| 1341 | EXPECT_POISONED(tp.tv_sec); |
| 1342 | EXPECT_POISONED(tp.tv_nsec); |
| 1343 | assert(0 == clock_getres(CLOCK_REALTIME, 0)); |
| 1344 | EXPECT_POISONED(tp.tv_sec); |
| 1345 | EXPECT_POISONED(tp.tv_nsec); |
| 1346 | assert(0 == clock_getres(CLOCK_REALTIME, &tp)); |
| 1347 | EXPECT_NOT_POISONED(tp.tv_sec); |
| 1348 | EXPECT_NOT_POISONED(tp.tv_nsec); |
| 1349 | } |
| 1350 | |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 1351 | TEST(MemorySanitizer, getitimer) { |
| 1352 | struct itimerval it1, it2; |
| 1353 | int res; |
| 1354 | EXPECT_POISONED(it1.it_interval.tv_sec); |
| 1355 | EXPECT_POISONED(it1.it_interval.tv_usec); |
| 1356 | EXPECT_POISONED(it1.it_value.tv_sec); |
| 1357 | EXPECT_POISONED(it1.it_value.tv_usec); |
| 1358 | res = getitimer(ITIMER_VIRTUAL, &it1); |
| 1359 | assert(!res); |
| 1360 | EXPECT_NOT_POISONED(it1.it_interval.tv_sec); |
| 1361 | EXPECT_NOT_POISONED(it1.it_interval.tv_usec); |
| 1362 | EXPECT_NOT_POISONED(it1.it_value.tv_sec); |
| 1363 | EXPECT_NOT_POISONED(it1.it_value.tv_usec); |
| 1364 | |
| 1365 | it1.it_interval.tv_sec = it1.it_value.tv_sec = 10000; |
| 1366 | it1.it_interval.tv_usec = it1.it_value.tv_usec = 0; |
| 1367 | |
| 1368 | res = setitimer(ITIMER_VIRTUAL, &it1, &it2); |
| 1369 | assert(!res); |
| 1370 | EXPECT_NOT_POISONED(it2.it_interval.tv_sec); |
| 1371 | EXPECT_NOT_POISONED(it2.it_interval.tv_usec); |
| 1372 | EXPECT_NOT_POISONED(it2.it_value.tv_sec); |
| 1373 | EXPECT_NOT_POISONED(it2.it_value.tv_usec); |
| 1374 | |
| 1375 | // Check that old_value can be 0, and disable the timer. |
| 1376 | memset(&it1, 0, sizeof(it1)); |
| 1377 | res = setitimer(ITIMER_VIRTUAL, &it1, 0); |
| 1378 | assert(!res); |
| 1379 | } |
| 1380 | |
Evgeniy Stepanov | fef6605 | 2013-04-08 08:25:22 +0000 | [diff] [blame] | 1381 | TEST(MemorySanitizer, time) { |
| 1382 | time_t t; |
| 1383 | EXPECT_POISONED(t); |
| 1384 | time_t t2 = time(&t); |
| 1385 | assert(t2 != (time_t)-1); |
| 1386 | EXPECT_NOT_POISONED(t); |
| 1387 | } |
| 1388 | |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 1389 | TEST(MemorySanitizer, localtime) { |
| 1390 | time_t t = 123; |
| 1391 | struct tm *time = localtime(&t); |
| 1392 | assert(time != 0); |
| 1393 | EXPECT_NOT_POISONED(time->tm_sec); |
| 1394 | EXPECT_NOT_POISONED(time->tm_hour); |
| 1395 | EXPECT_NOT_POISONED(time->tm_year); |
| 1396 | EXPECT_NOT_POISONED(time->tm_isdst); |
| 1397 | } |
| 1398 | |
| 1399 | TEST(MemorySanitizer, localtime_r) { |
| 1400 | time_t t = 123; |
| 1401 | struct tm time; |
| 1402 | struct tm *res = localtime_r(&t, &time); |
| 1403 | assert(res != 0); |
| 1404 | EXPECT_NOT_POISONED(time.tm_sec); |
| 1405 | EXPECT_NOT_POISONED(time.tm_hour); |
| 1406 | EXPECT_NOT_POISONED(time.tm_year); |
| 1407 | EXPECT_NOT_POISONED(time.tm_isdst); |
| 1408 | } |
| 1409 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1410 | TEST(MemorySanitizer, mmap) { |
| 1411 | const int size = 4096; |
| 1412 | void *p1, *p2; |
| 1413 | p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 1414 | __msan_poison(p1, size); |
| 1415 | munmap(p1, size); |
| 1416 | for (int i = 0; i < 1000; i++) { |
| 1417 | p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 1418 | if (p2 == p1) |
| 1419 | break; |
| 1420 | else |
| 1421 | munmap(p2, size); |
| 1422 | } |
| 1423 | if (p1 == p2) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1424 | EXPECT_NOT_POISONED(*(char*)p2); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1425 | munmap(p2, size); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | // FIXME: enable and add ecvt. |
| 1430 | // FIXME: check why msandr does nt handle fcvt. |
| 1431 | TEST(MemorySanitizer, fcvt) { |
| 1432 | int a, b; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1433 | break_optimization(&a); |
| 1434 | break_optimization(&b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1435 | EXPECT_POISONED(a); |
| 1436 | EXPECT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1437 | char *str = fcvt(12345.6789, 10, &a, &b); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1438 | EXPECT_NOT_POISONED(a); |
| 1439 | EXPECT_NOT_POISONED(b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 1442 | TEST(MemorySanitizer, frexp) { |
| 1443 | int x; |
| 1444 | x = *GetPoisoned<int>(); |
| 1445 | double r = frexp(1.1, &x); |
| 1446 | EXPECT_NOT_POISONED(r); |
| 1447 | EXPECT_NOT_POISONED(x); |
| 1448 | |
| 1449 | x = *GetPoisoned<int>(); |
| 1450 | float rf = frexpf(1.1, &x); |
| 1451 | EXPECT_NOT_POISONED(rf); |
| 1452 | EXPECT_NOT_POISONED(x); |
| 1453 | |
| 1454 | x = *GetPoisoned<int>(); |
| 1455 | double rl = frexpl(1.1, &x); |
| 1456 | EXPECT_NOT_POISONED(rl); |
| 1457 | EXPECT_NOT_POISONED(x); |
| 1458 | } |
| 1459 | |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1460 | namespace { |
| 1461 | |
| 1462 | static int cnt; |
| 1463 | |
| 1464 | void SigactionHandler(int signo, siginfo_t* si, void* uc) { |
| 1465 | assert(signo == SIGPROF); |
| 1466 | assert(si); |
| 1467 | EXPECT_NOT_POISONED(si->si_errno); |
| 1468 | EXPECT_NOT_POISONED(si->si_pid); |
| 1469 | #if __linux__ |
| 1470 | # if defined(__x86_64__) |
| 1471 | EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_RIP]); |
| 1472 | # elif defined(__i386__) |
| 1473 | EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_EIP]); |
| 1474 | # endif |
| 1475 | #endif |
| 1476 | ++cnt; |
| 1477 | } |
| 1478 | |
| 1479 | TEST(MemorySanitizer, sigaction) { |
| 1480 | struct sigaction act = {}; |
Evgeniy Stepanov | cd3049d | 2013-04-05 12:58:07 +0000 | [diff] [blame] | 1481 | struct sigaction oldact = {}; |
Evgeniy Stepanov | 6d0b7f6 | 2013-04-15 13:35:05 +0000 | [diff] [blame] | 1482 | struct sigaction origact = {}; |
| 1483 | |
| 1484 | sigaction(SIGPROF, 0, &origact); |
| 1485 | |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1486 | act.sa_flags |= SA_SIGINFO; |
| 1487 | act.sa_sigaction = &SigactionHandler; |
| 1488 | sigaction(SIGPROF, &act, 0); |
| 1489 | |
| 1490 | kill(getpid(), SIGPROF); |
| 1491 | |
| 1492 | act.sa_flags &= ~SA_SIGINFO; |
| 1493 | act.sa_handler = SIG_DFL; |
| 1494 | sigaction(SIGPROF, &act, 0); |
| 1495 | |
| 1496 | act.sa_flags &= ~SA_SIGINFO; |
| 1497 | act.sa_handler = SIG_IGN; |
Evgeniy Stepanov | cd3049d | 2013-04-05 12:58:07 +0000 | [diff] [blame] | 1498 | sigaction(SIGPROF, &act, &oldact); |
| 1499 | EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO); |
| 1500 | EXPECT_EQ(SIG_DFL, oldact.sa_handler); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1501 | kill(getpid(), SIGPROF); |
| 1502 | |
| 1503 | act.sa_flags |= SA_SIGINFO; |
| 1504 | act.sa_sigaction = &SigactionHandler; |
Evgeniy Stepanov | cd3049d | 2013-04-05 12:58:07 +0000 | [diff] [blame] | 1505 | sigaction(SIGPROF, &act, &oldact); |
| 1506 | EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO); |
| 1507 | EXPECT_EQ(SIG_IGN, oldact.sa_handler); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1508 | kill(getpid(), SIGPROF); |
| 1509 | |
| 1510 | act.sa_flags &= ~SA_SIGINFO; |
| 1511 | act.sa_handler = SIG_DFL; |
Evgeniy Stepanov | cd3049d | 2013-04-05 12:58:07 +0000 | [diff] [blame] | 1512 | sigaction(SIGPROF, &act, &oldact); |
| 1513 | EXPECT_TRUE(oldact.sa_flags & SA_SIGINFO); |
| 1514 | EXPECT_EQ(&SigactionHandler, oldact.sa_sigaction); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1515 | EXPECT_EQ(2, cnt); |
Evgeniy Stepanov | 6d0b7f6 | 2013-04-15 13:35:05 +0000 | [diff] [blame] | 1516 | |
| 1517 | sigaction(SIGPROF, &origact, 0); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | } // namespace |
| 1521 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1522 | struct StructWithDtor { |
| 1523 | ~StructWithDtor(); |
| 1524 | }; |
| 1525 | |
| 1526 | NOINLINE StructWithDtor::~StructWithDtor() { |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1527 | break_optimization(0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1530 | TEST(MemorySanitizer, Invoke) { |
| 1531 | StructWithDtor s; // Will cause the calls to become invokes. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1532 | EXPECT_NOT_POISONED(0); |
| 1533 | EXPECT_POISONED(*GetPoisoned<int>()); |
| 1534 | EXPECT_NOT_POISONED(0); |
| 1535 | EXPECT_POISONED(*GetPoisoned<int>()); |
| 1536 | EXPECT_POISONED(ReturnPoisoned<S4>()); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | TEST(MemorySanitizer, ptrtoint) { |
| 1540 | // Test that shadow is propagated through pointer-to-integer conversion. |
| 1541 | void* p = (void*)0xABCD; |
| 1542 | __msan_poison(((char*)&p) + 1, sizeof(p)); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 1543 | EXPECT_NOT_POISONED((((uintptr_t)p) & 0xFF) == 0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1544 | |
| 1545 | void* q = (void*)0xABCD; |
| 1546 | __msan_poison(&q, sizeof(q) - 1); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 1547 | EXPECT_POISONED((((uintptr_t)q) & 0xFF) == 0); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static void vaargsfn2(int guard, ...) { |
| 1551 | va_list vl; |
| 1552 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1553 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1554 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1555 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1556 | EXPECT_POISONED(va_arg(vl, double)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1557 | va_end(vl); |
| 1558 | } |
| 1559 | |
| 1560 | static void vaargsfn(int guard, ...) { |
| 1561 | va_list vl; |
| 1562 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1563 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1564 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1565 | // The following call will overwrite __msan_param_tls. |
| 1566 | // Checks after it test that arg shadow was somehow saved across the call. |
| 1567 | vaargsfn2(1, 2, 3, 4, *GetPoisoned<double>()); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1568 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1569 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1570 | va_end(vl); |
| 1571 | } |
| 1572 | |
| 1573 | TEST(MemorySanitizer, VAArgTest) { |
| 1574 | int* x = GetPoisoned<int>(); |
| 1575 | int* y = GetPoisoned<int>(4); |
| 1576 | vaargsfn(1, 13, *x, 42, *y); |
| 1577 | } |
| 1578 | |
| 1579 | static void vaargsfn_many(int guard, ...) { |
| 1580 | va_list vl; |
| 1581 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1582 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1583 | EXPECT_POISONED(va_arg(vl, int)); |
| 1584 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1585 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1586 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1587 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1588 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1589 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1590 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1591 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1592 | va_end(vl); |
| 1593 | } |
| 1594 | |
| 1595 | TEST(MemorySanitizer, VAArgManyTest) { |
| 1596 | int* x = GetPoisoned<int>(); |
| 1597 | int* y = GetPoisoned<int>(4); |
| 1598 | vaargsfn_many(1, 2, *x, 3, 4, 5, 6, 7, 8, 9, *y); |
| 1599 | } |
| 1600 | |
| 1601 | static void vaargsfn_pass2(va_list vl) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1602 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1603 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1604 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | static void vaargsfn_pass(int guard, ...) { |
| 1608 | va_list vl; |
| 1609 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1610 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1611 | vaargsfn_pass2(vl); |
| 1612 | va_end(vl); |
| 1613 | } |
| 1614 | |
| 1615 | TEST(MemorySanitizer, VAArgPass) { |
| 1616 | int* x = GetPoisoned<int>(); |
| 1617 | int* y = GetPoisoned<int>(4); |
| 1618 | vaargsfn_pass(1, *x, 2, 3, *y); |
| 1619 | } |
| 1620 | |
| 1621 | static void vaargsfn_copy2(va_list vl) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1622 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1623 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | static void vaargsfn_copy(int guard, ...) { |
| 1627 | va_list vl; |
| 1628 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1629 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1630 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1631 | va_list vl2; |
| 1632 | va_copy(vl2, vl); |
| 1633 | vaargsfn_copy2(vl2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1634 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1635 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1636 | va_end(vl); |
| 1637 | } |
| 1638 | |
| 1639 | TEST(MemorySanitizer, VAArgCopy) { |
| 1640 | int* x = GetPoisoned<int>(); |
| 1641 | int* y = GetPoisoned<int>(4); |
| 1642 | vaargsfn_copy(1, 2, *x, 3, *y); |
| 1643 | } |
| 1644 | |
| 1645 | static void vaargsfn_ptr(int guard, ...) { |
| 1646 | va_list vl; |
| 1647 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1648 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| 1649 | EXPECT_POISONED(va_arg(vl, int*)); |
| 1650 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| 1651 | EXPECT_POISONED(va_arg(vl, double*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1652 | va_end(vl); |
| 1653 | } |
| 1654 | |
| 1655 | TEST(MemorySanitizer, VAArgPtr) { |
| 1656 | int** x = GetPoisoned<int*>(); |
| 1657 | double** y = GetPoisoned<double*>(8); |
| 1658 | int z; |
| 1659 | vaargsfn_ptr(1, &z, *x, &z, *y); |
| 1660 | } |
| 1661 | |
| 1662 | static void vaargsfn_overflow(int guard, ...) { |
| 1663 | va_list vl; |
| 1664 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1665 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1666 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1667 | EXPECT_POISONED(va_arg(vl, int)); |
| 1668 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1669 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1670 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1671 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1672 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1673 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1674 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1675 | EXPECT_POISONED(va_arg(vl, double)); |
| 1676 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1677 | EXPECT_POISONED(va_arg(vl, int*)); |
| 1678 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1679 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1680 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1681 | EXPECT_POISONED(va_arg(vl, int)); |
| 1682 | EXPECT_POISONED(va_arg(vl, double)); |
| 1683 | EXPECT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1684 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1685 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
| 1686 | EXPECT_NOT_POISONED(va_arg(vl, double)); |
| 1687 | EXPECT_NOT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1688 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1689 | EXPECT_POISONED(va_arg(vl, int)); |
| 1690 | EXPECT_POISONED(va_arg(vl, double)); |
| 1691 | EXPECT_POISONED(va_arg(vl, int*)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1692 | |
| 1693 | va_end(vl); |
| 1694 | } |
| 1695 | |
| 1696 | TEST(MemorySanitizer, VAArgOverflow) { |
| 1697 | int* x = GetPoisoned<int>(); |
| 1698 | double* y = GetPoisoned<double>(8); |
| 1699 | int** p = GetPoisoned<int*>(16); |
| 1700 | int z; |
| 1701 | vaargsfn_overflow(1, |
| 1702 | 1, 2, *x, 4, 5, 6, |
| 1703 | 1.1, 2.2, 3.3, *y, 5.5, *p, 7.7, 8.8, |
| 1704 | // the following args will overflow for sure |
| 1705 | *x, *y, *p, |
| 1706 | 7, 9.9, &z, |
| 1707 | *x, *y, *p); |
| 1708 | } |
| 1709 | |
| 1710 | static void vaargsfn_tlsoverwrite2(int guard, ...) { |
| 1711 | va_list vl; |
| 1712 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1713 | EXPECT_NOT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1714 | va_end(vl); |
| 1715 | } |
| 1716 | |
| 1717 | static void vaargsfn_tlsoverwrite(int guard, ...) { |
| 1718 | // This call will overwrite TLS contents unless it's backed up somewhere. |
| 1719 | vaargsfn_tlsoverwrite2(2, 42); |
| 1720 | va_list vl; |
| 1721 | va_start(vl, guard); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1722 | EXPECT_POISONED(va_arg(vl, int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1723 | va_end(vl); |
| 1724 | } |
| 1725 | |
| 1726 | TEST(MemorySanitizer, VAArgTLSOverwrite) { |
| 1727 | int* x = GetPoisoned<int>(); |
| 1728 | vaargsfn_tlsoverwrite(1, *x); |
| 1729 | } |
| 1730 | |
| 1731 | struct StructByVal { |
| 1732 | int a, b, c, d, e, f; |
| 1733 | }; |
| 1734 | |
| 1735 | NOINLINE void StructByValTestFunc(struct StructByVal s) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1736 | EXPECT_NOT_POISONED(s.a); |
| 1737 | EXPECT_POISONED(s.b); |
| 1738 | EXPECT_NOT_POISONED(s.c); |
| 1739 | EXPECT_POISONED(s.d); |
| 1740 | EXPECT_NOT_POISONED(s.e); |
| 1741 | EXPECT_POISONED(s.f); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | NOINLINE void StructByValTestFunc1(struct StructByVal s) { |
| 1745 | StructByValTestFunc(s); |
| 1746 | } |
| 1747 | |
| 1748 | NOINLINE void StructByValTestFunc2(int z, struct StructByVal s) { |
| 1749 | StructByValTestFunc(s); |
| 1750 | } |
| 1751 | |
| 1752 | TEST(MemorySanitizer, StructByVal) { |
| 1753 | // Large aggregates are passed as "byval" pointer argument in LLVM. |
| 1754 | struct StructByVal s; |
| 1755 | s.a = 1; |
| 1756 | s.b = *GetPoisoned<int>(); |
| 1757 | s.c = 2; |
| 1758 | s.d = *GetPoisoned<int>(); |
| 1759 | s.e = 3; |
| 1760 | s.f = *GetPoisoned<int>(); |
| 1761 | StructByValTestFunc(s); |
| 1762 | StructByValTestFunc1(s); |
| 1763 | StructByValTestFunc2(0, s); |
| 1764 | } |
| 1765 | |
| 1766 | |
| 1767 | #if MSAN_HAS_M128 |
Evgeniy Stepanov | b921bf2 | 2013-04-16 14:09:47 +0000 | [diff] [blame] | 1768 | NOINLINE __m128i m128Eq(__m128i *a, __m128i *b) { return _mm_cmpeq_epi16(*a, *b); } |
| 1769 | NOINLINE __m128i m128Lt(__m128i *a, __m128i *b) { return _mm_cmplt_epi16(*a, *b); } |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1770 | TEST(MemorySanitizer, m128) { |
| 1771 | __m128i a = _mm_set1_epi16(0x1234); |
| 1772 | __m128i b = _mm_set1_epi16(0x7890); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1773 | EXPECT_NOT_POISONED(m128Eq(&a, &b)); |
| 1774 | EXPECT_NOT_POISONED(m128Lt(&a, &b)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1775 | } |
| 1776 | // FIXME: add more tests for __m128i. |
| 1777 | #endif // MSAN_HAS_M128 |
| 1778 | |
| 1779 | // We should not complain when copying this poisoned hole. |
| 1780 | struct StructWithHole { |
| 1781 | U4 a; |
| 1782 | // 4-byte hole. |
| 1783 | U8 b; |
| 1784 | }; |
| 1785 | |
| 1786 | NOINLINE StructWithHole ReturnStructWithHole() { |
| 1787 | StructWithHole res; |
| 1788 | __msan_poison(&res, sizeof(res)); |
| 1789 | res.a = 1; |
| 1790 | res.b = 2; |
| 1791 | return res; |
| 1792 | } |
| 1793 | |
| 1794 | TEST(MemorySanitizer, StructWithHole) { |
| 1795 | StructWithHole a = ReturnStructWithHole(); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 1796 | break_optimization(&a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | template <class T> |
| 1800 | NOINLINE T ReturnStruct() { |
| 1801 | T res; |
| 1802 | __msan_poison(&res, sizeof(res)); |
| 1803 | res.a = 1; |
| 1804 | return res; |
| 1805 | } |
| 1806 | |
| 1807 | template <class T> |
| 1808 | NOINLINE void TestReturnStruct() { |
| 1809 | T s1 = ReturnStruct<T>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1810 | EXPECT_NOT_POISONED(s1.a); |
| 1811 | EXPECT_POISONED(s1.b); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | struct SSS1 { |
| 1815 | int a, b, c; |
| 1816 | }; |
| 1817 | struct SSS2 { |
| 1818 | int b, a, c; |
| 1819 | }; |
| 1820 | struct SSS3 { |
| 1821 | int b, c, a; |
| 1822 | }; |
| 1823 | struct SSS4 { |
| 1824 | int c, b, a; |
| 1825 | }; |
| 1826 | |
| 1827 | struct SSS5 { |
| 1828 | int a; |
| 1829 | float b; |
| 1830 | }; |
| 1831 | struct SSS6 { |
| 1832 | int a; |
| 1833 | double b; |
| 1834 | }; |
| 1835 | struct SSS7 { |
| 1836 | S8 b; |
| 1837 | int a; |
| 1838 | }; |
| 1839 | struct SSS8 { |
| 1840 | S2 b; |
| 1841 | S8 a; |
| 1842 | }; |
| 1843 | |
| 1844 | TEST(MemorySanitizer, IntStruct3) { |
| 1845 | TestReturnStruct<SSS1>(); |
| 1846 | TestReturnStruct<SSS2>(); |
| 1847 | TestReturnStruct<SSS3>(); |
| 1848 | TestReturnStruct<SSS4>(); |
| 1849 | TestReturnStruct<SSS5>(); |
| 1850 | TestReturnStruct<SSS6>(); |
| 1851 | TestReturnStruct<SSS7>(); |
| 1852 | TestReturnStruct<SSS8>(); |
| 1853 | } |
| 1854 | |
| 1855 | struct LongStruct { |
| 1856 | U1 a1, b1; |
| 1857 | U2 a2, b2; |
| 1858 | U4 a4, b4; |
| 1859 | U8 a8, b8; |
| 1860 | }; |
| 1861 | |
| 1862 | NOINLINE LongStruct ReturnLongStruct1() { |
| 1863 | LongStruct res; |
| 1864 | __msan_poison(&res, sizeof(res)); |
| 1865 | res.a1 = res.a2 = res.a4 = res.a8 = 111; |
| 1866 | // leaves b1, .., b8 poisoned. |
| 1867 | return res; |
| 1868 | } |
| 1869 | |
| 1870 | NOINLINE LongStruct ReturnLongStruct2() { |
| 1871 | LongStruct res; |
| 1872 | __msan_poison(&res, sizeof(res)); |
| 1873 | res.b1 = res.b2 = res.b4 = res.b8 = 111; |
| 1874 | // leaves a1, .., a8 poisoned. |
| 1875 | return res; |
| 1876 | } |
| 1877 | |
| 1878 | TEST(MemorySanitizer, LongStruct) { |
| 1879 | LongStruct s1 = ReturnLongStruct1(); |
| 1880 | __msan_print_shadow(&s1, sizeof(s1)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1881 | EXPECT_NOT_POISONED(s1.a1); |
| 1882 | EXPECT_NOT_POISONED(s1.a2); |
| 1883 | EXPECT_NOT_POISONED(s1.a4); |
| 1884 | EXPECT_NOT_POISONED(s1.a8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1885 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1886 | EXPECT_POISONED(s1.b1); |
| 1887 | EXPECT_POISONED(s1.b2); |
| 1888 | EXPECT_POISONED(s1.b4); |
| 1889 | EXPECT_POISONED(s1.b8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1890 | |
| 1891 | LongStruct s2 = ReturnLongStruct2(); |
| 1892 | __msan_print_shadow(&s2, sizeof(s2)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1893 | EXPECT_NOT_POISONED(s2.b1); |
| 1894 | EXPECT_NOT_POISONED(s2.b2); |
| 1895 | EXPECT_NOT_POISONED(s2.b4); |
| 1896 | EXPECT_NOT_POISONED(s2.b8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1897 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1898 | EXPECT_POISONED(s2.a1); |
| 1899 | EXPECT_POISONED(s2.a2); |
| 1900 | EXPECT_POISONED(s2.a4); |
| 1901 | EXPECT_POISONED(s2.a8); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | TEST(MemorySanitizer, getrlimit) { |
| 1905 | struct rlimit limit; |
| 1906 | __msan_poison(&limit, sizeof(limit)); |
| 1907 | int result = getrlimit(RLIMIT_DATA, &limit); |
| 1908 | assert(result == 0); |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1909 | EXPECT_NOT_POISONED(limit.rlim_cur); |
| 1910 | EXPECT_NOT_POISONED(limit.rlim_max); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1913 | TEST(MemorySanitizer, getrusage) { |
| 1914 | struct rusage usage; |
| 1915 | __msan_poison(&usage, sizeof(usage)); |
| 1916 | int result = getrusage(RUSAGE_SELF, &usage); |
| 1917 | assert(result == 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1918 | EXPECT_NOT_POISONED(usage.ru_utime.tv_sec); |
| 1919 | EXPECT_NOT_POISONED(usage.ru_utime.tv_usec); |
| 1920 | EXPECT_NOT_POISONED(usage.ru_stime.tv_sec); |
| 1921 | EXPECT_NOT_POISONED(usage.ru_stime.tv_usec); |
| 1922 | EXPECT_NOT_POISONED(usage.ru_maxrss); |
| 1923 | EXPECT_NOT_POISONED(usage.ru_minflt); |
| 1924 | EXPECT_NOT_POISONED(usage.ru_majflt); |
| 1925 | EXPECT_NOT_POISONED(usage.ru_inblock); |
| 1926 | EXPECT_NOT_POISONED(usage.ru_oublock); |
| 1927 | EXPECT_NOT_POISONED(usage.ru_nvcsw); |
| 1928 | EXPECT_NOT_POISONED(usage.ru_nivcsw); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 1931 | #ifdef __GLIBC__ |
Evgeniy Stepanov | e6c62f2 | 2013-05-21 08:56:22 +0000 | [diff] [blame] | 1932 | extern char *program_invocation_name; |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 1933 | #else // __GLIBC__ |
| 1934 | # error "TODO: port this" |
| 1935 | #endif |
| 1936 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1937 | static void dladdr_testfn() {} |
| 1938 | |
| 1939 | TEST(MemorySanitizer, dladdr) { |
| 1940 | Dl_info info; |
| 1941 | __msan_poison(&info, sizeof(info)); |
| 1942 | int result = dladdr((const void*)dladdr_testfn, &info); |
| 1943 | assert(result != 0); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1944 | EXPECT_NOT_POISONED((unsigned long)info.dli_fname); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1945 | if (info.dli_fname) |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1946 | EXPECT_NOT_POISONED(strlen(info.dli_fname)); |
| 1947 | EXPECT_NOT_POISONED((unsigned long)info.dli_fbase); |
| 1948 | EXPECT_NOT_POISONED((unsigned long)info.dli_sname); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1949 | if (info.dli_sname) |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 1950 | EXPECT_NOT_POISONED(strlen(info.dli_sname)); |
| 1951 | EXPECT_NOT_POISONED((unsigned long)info.dli_saddr); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1954 | static int dl_phdr_callback(struct dl_phdr_info *info, size_t size, void *data) { |
| 1955 | (*(int *)data)++; |
| 1956 | EXPECT_NOT_POISONED(info->dlpi_addr); |
| 1957 | EXPECT_NOT_POISONED(strlen(info->dlpi_name)); |
| 1958 | EXPECT_NOT_POISONED(info->dlpi_phnum); |
| 1959 | for (int i = 0; i < info->dlpi_phnum; ++i) |
| 1960 | EXPECT_NOT_POISONED(info->dlpi_phdr[i]); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
Evgeniy Stepanov | 263800b | 2013-06-27 13:21:00 +0000 | [diff] [blame] | 1964 | #ifndef MSAN_TEST_DISABLE_DLOPEN |
| 1965 | |
| 1966 | // Compute the path to our loadable DSO. We assume it's in the same |
| 1967 | // directory. Only use string routines that we intercept so far to do this. |
| 1968 | static int PathToLoadable(char *buf, size_t sz) { |
| 1969 | const char *basename = "libmsan_loadable.x86_64.so"; |
| 1970 | char *argv0 = program_invocation_name; |
| 1971 | char *last_slash = strrchr(argv0, '/'); |
| 1972 | assert(last_slash); |
| 1973 | int res = |
| 1974 | snprintf(buf, sz, "%.*s/%s", int(last_slash - argv0), argv0, basename); |
| 1975 | return res < sz ? 0 : res; |
| 1976 | } |
| 1977 | |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1978 | TEST(MemorySanitizer, dl_iterate_phdr) { |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 1979 | char path[4096]; |
| 1980 | int res = PathToLoadable(path, sizeof(path)); |
| 1981 | assert(!res); |
| 1982 | |
| 1983 | // Having at least one dlopen'ed library in the process makes this more |
| 1984 | // entertaining. |
| 1985 | void *lib = dlopen(path, RTLD_LAZY); |
| 1986 | ASSERT_NE((void*)0, lib); |
| 1987 | |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1988 | int count = 0; |
| 1989 | int result = dl_iterate_phdr(dl_phdr_callback, &count); |
| 1990 | assert(count > 0); |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 1991 | |
| 1992 | dlclose(lib); |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 1995 | |
| 1996 | TEST(MemorySanitizer, dlopen) { |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 1997 | char path[4096]; |
| 1998 | int res = PathToLoadable(path, sizeof(path)); |
| 1999 | assert(!res); |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 2000 | |
| 2001 | // We need to clear shadow for globals when doing dlopen. In order to test |
| 2002 | // this, we have to poison the shadow for the DSO before we load it. In |
| 2003 | // general this is difficult, but the loader tends to reload things in the |
| 2004 | // same place, so we open, close, and then reopen. The global should always |
| 2005 | // start out clean after dlopen. |
| 2006 | for (int i = 0; i < 2; i++) { |
| 2007 | void *lib = dlopen(path, RTLD_LAZY); |
| 2008 | if (lib == NULL) { |
| 2009 | printf("dlerror: %s\n", dlerror()); |
| 2010 | assert(lib != NULL); |
| 2011 | } |
| 2012 | void **(*get_dso_global)() = (void **(*)())dlsym(lib, "get_dso_global"); |
| 2013 | assert(get_dso_global); |
| 2014 | void **dso_global = get_dso_global(); |
| 2015 | EXPECT_NOT_POISONED(*dso_global); |
| 2016 | __msan_poison(dso_global, sizeof(*dso_global)); |
| 2017 | EXPECT_POISONED(*dso_global); |
| 2018 | dlclose(lib); |
| 2019 | } |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 2020 | } |
Evgeniy Stepanov | 6c503b9 | 2013-03-22 11:59:49 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Regression test for a crash in dlopen() interceptor. |
| 2023 | TEST(MemorySanitizer, dlopenFailed) { |
| 2024 | const char *path = "/libmsan_loadable_does_not_exist.x86_64.so"; |
| 2025 | void *lib = dlopen(path, RTLD_LAZY); |
| 2026 | ASSERT_EQ(0, lib); |
| 2027 | } |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 2028 | |
Evgeniy Stepanov | 263800b | 2013-06-27 13:21:00 +0000 | [diff] [blame] | 2029 | #endif // MSAN_TEST_DISABLE_DLOPEN |
| 2030 | |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 2031 | TEST(MemorySanitizer, scanf) { |
| 2032 | const char *input = "42 hello"; |
| 2033 | int* d = new int; |
| 2034 | char* s = new char[7]; |
| 2035 | int res = sscanf(input, "%d %5s", d, s); |
| 2036 | printf("res %d\n", res); |
| 2037 | assert(res == 2); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2038 | EXPECT_NOT_POISONED(*d); |
| 2039 | EXPECT_NOT_POISONED(s[0]); |
| 2040 | EXPECT_NOT_POISONED(s[1]); |
| 2041 | EXPECT_NOT_POISONED(s[2]); |
| 2042 | EXPECT_NOT_POISONED(s[3]); |
| 2043 | EXPECT_NOT_POISONED(s[4]); |
| 2044 | EXPECT_NOT_POISONED(s[5]); |
| 2045 | EXPECT_POISONED(s[6]); |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 2046 | delete s; |
| 2047 | delete d; |
| 2048 | } |
| 2049 | |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2050 | static void *SimpleThread_threadfn(void* data) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2051 | return new int; |
| 2052 | } |
| 2053 | |
| 2054 | TEST(MemorySanitizer, SimpleThread) { |
| 2055 | pthread_t t; |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2056 | void *p; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2057 | int res = pthread_create(&t, NULL, SimpleThread_threadfn, NULL); |
| 2058 | assert(!res); |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 2059 | EXPECT_NOT_POISONED(t); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2060 | res = pthread_join(t, &p); |
| 2061 | assert(!res); |
| 2062 | if (!__msan_has_dynamic_component()) // FIXME: intercept pthread_join (?). |
| 2063 | __msan_unpoison(&p, sizeof(p)); |
| 2064 | delete (int*)p; |
| 2065 | } |
| 2066 | |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2067 | static void *SmallStackThread_threadfn(void* data) { |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | TEST(MemorySanitizer, SmallStackThread) { |
| 2072 | pthread_attr_t attr; |
| 2073 | pthread_t t; |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2074 | void *p; |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 2075 | int res; |
| 2076 | res = pthread_attr_init(&attr); |
| 2077 | ASSERT_EQ(0, res); |
| 2078 | res = pthread_attr_setstacksize(&attr, 64 * 1024); |
| 2079 | ASSERT_EQ(0, res); |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2080 | res = pthread_create(&t, &attr, SmallStackThread_threadfn, NULL); |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 2081 | ASSERT_EQ(0, res); |
| 2082 | res = pthread_join(t, &p); |
| 2083 | ASSERT_EQ(0, res); |
| 2084 | res = pthread_attr_destroy(&attr); |
| 2085 | ASSERT_EQ(0, res); |
| 2086 | } |
| 2087 | |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 2088 | TEST(MemorySanitizer, PreAllocatedStackThread) { |
| 2089 | pthread_attr_t attr; |
| 2090 | pthread_t t; |
| 2091 | int res; |
| 2092 | res = pthread_attr_init(&attr); |
| 2093 | ASSERT_EQ(0, res); |
| 2094 | void *stack; |
| 2095 | const size_t kStackSize = 64 * 1024; |
| 2096 | res = posix_memalign(&stack, 4096, kStackSize); |
| 2097 | ASSERT_EQ(0, res); |
| 2098 | res = pthread_attr_setstack(&attr, stack, kStackSize); |
| 2099 | ASSERT_EQ(0, res); |
| 2100 | // A small self-allocated stack can not be extended by the tool. |
| 2101 | // In this case pthread_create is expected to fail. |
| 2102 | res = pthread_create(&t, &attr, SmallStackThread_threadfn, NULL); |
| 2103 | EXPECT_NE(0, res); |
| 2104 | res = pthread_attr_destroy(&attr); |
| 2105 | ASSERT_EQ(0, res); |
| 2106 | } |
| 2107 | |
Evgeniy Stepanov | 56d3472 | 2013-05-21 08:12:08 +0000 | [diff] [blame] | 2108 | TEST(MemorySanitizer, pthread_getschedparam) { |
| 2109 | int policy; |
| 2110 | struct sched_param param; |
| 2111 | int res = pthread_getschedparam(pthread_self(), &policy, ¶m); |
| 2112 | ASSERT_EQ(0, res); |
| 2113 | EXPECT_NOT_POISONED(policy); |
| 2114 | EXPECT_NOT_POISONED(param.sched_priority); |
| 2115 | } |
| 2116 | |
Evgeniy Stepanov | 737da2f | 2013-07-05 12:31:07 +0000 | [diff] [blame] | 2117 | TEST(MemorySanitizer, pthread_key_create) { |
| 2118 | pthread_key_t key; |
| 2119 | int res = pthread_key_create(&key, NULL); |
| 2120 | assert(!res); |
| 2121 | EXPECT_NOT_POISONED(key); |
| 2122 | res = pthread_key_delete(key); |
| 2123 | assert(!res); |
| 2124 | } |
| 2125 | |
Evgeniy Stepanov | b6c8e47 | 2013-04-23 13:27:36 +0000 | [diff] [blame] | 2126 | TEST(MemorySanitizer, posix_memalign) { |
| 2127 | void *p; |
| 2128 | EXPECT_POISONED(p); |
| 2129 | int res = posix_memalign(&p, 4096, 13); |
| 2130 | ASSERT_EQ(0, res); |
| 2131 | EXPECT_NOT_POISONED(p); |
| 2132 | free(p); |
| 2133 | } |
| 2134 | |
Evgeniy Stepanov | 9530eb7 | 2013-04-23 14:05:15 +0000 | [diff] [blame] | 2135 | TEST(MemorySanitizer, inet_pton) { |
| 2136 | const char *s = "1:0:0:0:0:0:0:8"; |
| 2137 | unsigned char buf[sizeof(struct in6_addr)]; |
| 2138 | int res = inet_pton(AF_INET6, s, buf); |
| 2139 | ASSERT_EQ(1, res); |
| 2140 | EXPECT_NOT_POISONED(buf[0]); |
| 2141 | EXPECT_NOT_POISONED(buf[sizeof(struct in6_addr) - 1]); |
| 2142 | |
| 2143 | char s_out[INET6_ADDRSTRLEN]; |
| 2144 | EXPECT_POISONED(s_out[3]); |
| 2145 | const char *q = inet_ntop(AF_INET6, buf, s_out, INET6_ADDRSTRLEN); |
| 2146 | ASSERT_NE((void*)0, q); |
| 2147 | EXPECT_NOT_POISONED(s_out[3]); |
| 2148 | } |
| 2149 | |
Evgeniy Stepanov | 9d60087 | 2013-06-24 13:56:14 +0000 | [diff] [blame] | 2150 | TEST(MemorySanitizer, inet_aton) { |
| 2151 | const char *s = "127.0.0.1"; |
| 2152 | struct in_addr in[2]; |
| 2153 | int res = inet_aton(s, in); |
| 2154 | ASSERT_NE(0, res); |
| 2155 | EXPECT_NOT_POISONED(in[0]); |
| 2156 | EXPECT_POISONED(*(char *)(in + 1)); |
| 2157 | } |
| 2158 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2159 | TEST(MemorySanitizer, uname) { |
| 2160 | struct utsname u; |
| 2161 | int res = uname(&u); |
| 2162 | assert(!res); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2163 | EXPECT_NOT_POISONED(strlen(u.sysname)); |
| 2164 | EXPECT_NOT_POISONED(strlen(u.nodename)); |
| 2165 | EXPECT_NOT_POISONED(strlen(u.release)); |
| 2166 | EXPECT_NOT_POISONED(strlen(u.version)); |
| 2167 | EXPECT_NOT_POISONED(strlen(u.machine)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Evgeniy Stepanov | 95d0588 | 2013-01-23 10:43:38 +0000 | [diff] [blame] | 2170 | TEST(MemorySanitizer, gethostname) { |
| 2171 | char buf[100]; |
| 2172 | int res = gethostname(buf, 100); |
| 2173 | assert(!res); |
| 2174 | EXPECT_NOT_POISONED(strlen(buf)); |
| 2175 | } |
| 2176 | |
Evgeniy Stepanov | 359d7fc | 2013-06-24 14:25:33 +0000 | [diff] [blame] | 2177 | TEST(MemorySanitizer, sysinfo) { |
| 2178 | struct sysinfo info; |
| 2179 | int res = sysinfo(&info); |
| 2180 | assert(!res); |
| 2181 | EXPECT_NOT_POISONED(info); |
| 2182 | } |
| 2183 | |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 2184 | TEST(MemorySanitizer, getpwuid) { |
| 2185 | struct passwd *p = getpwuid(0); // root |
| 2186 | assert(p); |
| 2187 | EXPECT_NOT_POISONED(p->pw_name); |
| 2188 | assert(p->pw_name); |
| 2189 | EXPECT_NOT_POISONED(p->pw_name[0]); |
| 2190 | EXPECT_NOT_POISONED(p->pw_uid); |
| 2191 | assert(p->pw_uid == 0); |
| 2192 | } |
| 2193 | |
| 2194 | TEST(MemorySanitizer, getpwnam_r) { |
| 2195 | struct passwd pwd; |
| 2196 | struct passwd *pwdres; |
| 2197 | char buf[10000]; |
| 2198 | int res = getpwnam_r("root", &pwd, buf, sizeof(buf), &pwdres); |
| 2199 | assert(!res); |
| 2200 | EXPECT_NOT_POISONED(pwd.pw_name); |
| 2201 | assert(pwd.pw_name); |
| 2202 | EXPECT_NOT_POISONED(pwd.pw_name[0]); |
| 2203 | EXPECT_NOT_POISONED(pwd.pw_uid); |
| 2204 | assert(pwd.pw_uid == 0); |
| 2205 | } |
| 2206 | |
| 2207 | TEST(MemorySanitizer, getpwnam_r_positive) { |
| 2208 | struct passwd pwd; |
| 2209 | struct passwd *pwdres; |
| 2210 | char s[5]; |
| 2211 | strncpy(s, "abcd", 5); |
| 2212 | __msan_poison(s, 5); |
| 2213 | char buf[10000]; |
| 2214 | int res; |
| 2215 | EXPECT_UMR(res = getpwnam_r(s, &pwd, buf, sizeof(buf), &pwdres)); |
| 2216 | } |
| 2217 | |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 2218 | TEST(MemorySanitizer, getgrnam_r) { |
| 2219 | struct group grp; |
| 2220 | struct group *grpres; |
| 2221 | char buf[10000]; |
| 2222 | int res = getgrnam_r("root", &grp, buf, sizeof(buf), &grpres); |
| 2223 | assert(!res); |
| 2224 | EXPECT_NOT_POISONED(grp.gr_name); |
| 2225 | assert(grp.gr_name); |
| 2226 | EXPECT_NOT_POISONED(grp.gr_name[0]); |
| 2227 | EXPECT_NOT_POISONED(grp.gr_gid); |
| 2228 | } |
| 2229 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2230 | template<class T> |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2231 | static bool applySlt(T value, T shadow) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2232 | __msan_partial_poison(&value, &shadow, sizeof(T)); |
| 2233 | volatile bool zzz = true; |
| 2234 | // This "|| zzz" trick somehow makes LLVM emit "icmp slt" instead of |
| 2235 | // a shift-and-trunc to get at the highest bit. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2236 | volatile bool v = value < 0 || zzz; |
| 2237 | return v; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | TEST(MemorySanitizer, SignedCompareWithZero) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2241 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xF)); |
| 2242 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFF)); |
| 2243 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFFFFFF)); |
| 2244 | EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0x7FFFFFF)); |
| 2245 | EXPECT_UMR(applySlt<S4>(0xF, 0x80FFFFFF)); |
| 2246 | EXPECT_UMR(applySlt<S4>(0xF, 0xFFFFFFFF)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 2249 | template <class T, class S> |
| 2250 | static T poisoned(T Va, S Sa) { |
| 2251 | char SIZE_CHECK1[(ssize_t)sizeof(T) - (ssize_t)sizeof(S)]; |
| 2252 | char SIZE_CHECK2[(ssize_t)sizeof(S) - (ssize_t)sizeof(T)]; |
| 2253 | T a; |
| 2254 | a = Va; |
| 2255 | __msan_partial_poison(&a, &Sa, sizeof(T)); |
| 2256 | return a; |
| 2257 | } |
| 2258 | |
| 2259 | TEST(MemorySanitizer, ICmpRelational) { |
| 2260 | EXPECT_NOT_POISONED(poisoned(0, 0) < poisoned(0, 0)); |
| 2261 | EXPECT_NOT_POISONED(poisoned(0U, 0) < poisoned(0U, 0)); |
| 2262 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) < poisoned(0LL, 0LLU)); |
| 2263 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) < poisoned(0LLU, 0LLU)); |
| 2264 | EXPECT_POISONED(poisoned(0xFF, 0xFF) < poisoned(0xFF, 0xFF)); |
| 2265 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) < |
| 2266 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 2267 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) < |
| 2268 | poisoned(-1, 0xFFFFFFFFU)); |
| 2269 | |
| 2270 | EXPECT_NOT_POISONED(poisoned(0, 0) <= poisoned(0, 0)); |
| 2271 | EXPECT_NOT_POISONED(poisoned(0U, 0) <= poisoned(0U, 0)); |
| 2272 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) <= poisoned(0LL, 0LLU)); |
| 2273 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) <= poisoned(0LLU, 0LLU)); |
| 2274 | EXPECT_POISONED(poisoned(0xFF, 0xFF) <= poisoned(0xFF, 0xFF)); |
| 2275 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <= |
| 2276 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 2277 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <= |
| 2278 | poisoned(-1, 0xFFFFFFFFU)); |
| 2279 | |
| 2280 | EXPECT_NOT_POISONED(poisoned(0, 0) > poisoned(0, 0)); |
| 2281 | EXPECT_NOT_POISONED(poisoned(0U, 0) > poisoned(0U, 0)); |
| 2282 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) > poisoned(0LL, 0LLU)); |
| 2283 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) > poisoned(0LLU, 0LLU)); |
| 2284 | EXPECT_POISONED(poisoned(0xFF, 0xFF) > poisoned(0xFF, 0xFF)); |
| 2285 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) > |
| 2286 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 2287 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) > |
| 2288 | poisoned(-1, 0xFFFFFFFFU)); |
| 2289 | |
| 2290 | EXPECT_NOT_POISONED(poisoned(0, 0) >= poisoned(0, 0)); |
| 2291 | EXPECT_NOT_POISONED(poisoned(0U, 0) >= poisoned(0U, 0)); |
| 2292 | EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) >= poisoned(0LL, 0LLU)); |
| 2293 | EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) >= poisoned(0LLU, 0LLU)); |
| 2294 | EXPECT_POISONED(poisoned(0xFF, 0xFF) >= poisoned(0xFF, 0xFF)); |
| 2295 | EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >= |
| 2296 | poisoned(0xFFFFFFFFU, 0xFFFFFFFFU)); |
| 2297 | EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >= |
| 2298 | poisoned(-1, 0xFFFFFFFFU)); |
| 2299 | |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 2300 | EXPECT_POISONED(poisoned(6, 0xF) > poisoned(7, 0)); |
| 2301 | EXPECT_POISONED(poisoned(0xF, 0xF) > poisoned(7, 0)); |
| 2302 | |
| 2303 | EXPECT_NOT_POISONED(poisoned(-1, 0x80000000U) >= poisoned(-1, 0U)); |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | #if MSAN_HAS_M128 |
| 2307 | TEST(MemorySanitizer, ICmpVectorRelational) { |
Evgeniy Stepanov | b921bf2 | 2013-04-16 14:09:47 +0000 | [diff] [blame] | 2308 | EXPECT_NOT_POISONED( |
| 2309 | _mm_cmplt_epi16(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)), |
| 2310 | poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)))); |
| 2311 | EXPECT_NOT_POISONED( |
| 2312 | _mm_cmplt_epi16(poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)), |
| 2313 | poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)))); |
| 2314 | EXPECT_POISONED( |
| 2315 | _mm_cmplt_epi16(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)), |
| 2316 | poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)))); |
| 2317 | EXPECT_POISONED(_mm_cmpgt_epi16(poisoned(_mm_set1_epi16(6), _mm_set1_epi16(0xF)), |
| 2318 | poisoned(_mm_set1_epi16(7), _mm_set1_epi16(0)))); |
Evgeniy Stepanov | 9a22a3d | 2013-01-25 15:39:11 +0000 | [diff] [blame] | 2319 | } |
| 2320 | #endif |
| 2321 | |
Evgeniy Stepanov | 2efa142 | 2013-01-22 12:31:39 +0000 | [diff] [blame] | 2322 | // Volatile bitfield store is implemented as load-mask-store |
| 2323 | // Test that we don't warn on the store of (uninitialized) padding. |
| 2324 | struct VolatileBitfieldStruct { |
| 2325 | volatile unsigned x : 1; |
| 2326 | unsigned y : 1; |
| 2327 | }; |
| 2328 | |
| 2329 | TEST(MemorySanitizer, VolatileBitfield) { |
| 2330 | VolatileBitfieldStruct *S = new VolatileBitfieldStruct; |
| 2331 | S->x = 1; |
Evgeniy Stepanov | 02f4a94 | 2013-01-22 12:33:11 +0000 | [diff] [blame] | 2332 | EXPECT_NOT_POISONED((unsigned)S->x); |
| 2333 | EXPECT_POISONED((unsigned)S->y); |
Evgeniy Stepanov | 2efa142 | 2013-01-22 12:31:39 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Evgeniy Stepanov | f43f602 | 2013-06-04 13:08:36 +0000 | [diff] [blame] | 2336 | TEST(MemorySanitizer, UnalignedLoad) { |
| 2337 | char x[32]; |
| 2338 | memset(x + 8, 0, 16); |
| 2339 | EXPECT_POISONED(__sanitizer_unaligned_load16(x+6)); |
| 2340 | EXPECT_POISONED(__sanitizer_unaligned_load16(x+7)); |
| 2341 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+8)); |
| 2342 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+9)); |
| 2343 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+22)); |
| 2344 | EXPECT_POISONED(__sanitizer_unaligned_load16(x+23)); |
| 2345 | EXPECT_POISONED(__sanitizer_unaligned_load16(x+24)); |
| 2346 | |
| 2347 | EXPECT_POISONED(__sanitizer_unaligned_load32(x+4)); |
| 2348 | EXPECT_POISONED(__sanitizer_unaligned_load32(x+7)); |
| 2349 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+8)); |
| 2350 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+9)); |
| 2351 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+20)); |
| 2352 | EXPECT_POISONED(__sanitizer_unaligned_load32(x+21)); |
| 2353 | EXPECT_POISONED(__sanitizer_unaligned_load32(x+24)); |
| 2354 | |
| 2355 | EXPECT_POISONED(__sanitizer_unaligned_load64(x)); |
| 2356 | EXPECT_POISONED(__sanitizer_unaligned_load64(x+1)); |
| 2357 | EXPECT_POISONED(__sanitizer_unaligned_load64(x+7)); |
| 2358 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+8)); |
| 2359 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+9)); |
| 2360 | EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+16)); |
| 2361 | EXPECT_POISONED(__sanitizer_unaligned_load64(x+17)); |
| 2362 | EXPECT_POISONED(__sanitizer_unaligned_load64(x+21)); |
| 2363 | EXPECT_POISONED(__sanitizer_unaligned_load64(x+24)); |
| 2364 | } |
| 2365 | |
| 2366 | TEST(MemorySanitizer, UnalignedStore16) { |
| 2367 | char x[5]; |
| 2368 | U2 y = 0; |
| 2369 | __msan_poison(&y, 1); |
| 2370 | __sanitizer_unaligned_store16(x + 1, y); |
| 2371 | EXPECT_POISONED(x[0]); |
| 2372 | EXPECT_POISONED(x[1]); |
| 2373 | EXPECT_NOT_POISONED(x[2]); |
| 2374 | EXPECT_POISONED(x[3]); |
| 2375 | EXPECT_POISONED(x[4]); |
| 2376 | } |
| 2377 | |
| 2378 | TEST(MemorySanitizer, UnalignedStore32) { |
| 2379 | char x[8]; |
| 2380 | U4 y4 = 0; |
| 2381 | __msan_poison(&y4, 2); |
| 2382 | __sanitizer_unaligned_store32(x+3, y4); |
| 2383 | EXPECT_POISONED(x[0]); |
| 2384 | EXPECT_POISONED(x[1]); |
| 2385 | EXPECT_POISONED(x[2]); |
| 2386 | EXPECT_POISONED(x[3]); |
| 2387 | EXPECT_POISONED(x[4]); |
| 2388 | EXPECT_NOT_POISONED(x[5]); |
| 2389 | EXPECT_NOT_POISONED(x[6]); |
| 2390 | EXPECT_POISONED(x[7]); |
| 2391 | } |
| 2392 | |
| 2393 | TEST(MemorySanitizer, UnalignedStore64) { |
| 2394 | char x[16]; |
| 2395 | U8 y = 0; |
| 2396 | __msan_poison(&y, 3); |
| 2397 | __msan_poison(((char *)&y) + sizeof(y) - 2, 1); |
| 2398 | __sanitizer_unaligned_store64(x+3, y); |
| 2399 | EXPECT_POISONED(x[0]); |
| 2400 | EXPECT_POISONED(x[1]); |
| 2401 | EXPECT_POISONED(x[2]); |
| 2402 | EXPECT_POISONED(x[3]); |
| 2403 | EXPECT_POISONED(x[4]); |
| 2404 | EXPECT_POISONED(x[5]); |
| 2405 | EXPECT_NOT_POISONED(x[6]); |
| 2406 | EXPECT_NOT_POISONED(x[7]); |
| 2407 | EXPECT_NOT_POISONED(x[8]); |
| 2408 | EXPECT_POISONED(x[9]); |
| 2409 | EXPECT_NOT_POISONED(x[10]); |
| 2410 | EXPECT_POISONED(x[11]); |
| 2411 | } |
| 2412 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2413 | TEST(MemorySanitizerDr, StoreInDSOTest) { |
| 2414 | if (!__msan_has_dynamic_component()) return; |
| 2415 | char* s = new char[10]; |
| 2416 | dso_memfill(s, 9); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2417 | EXPECT_NOT_POISONED(s[5]); |
| 2418 | EXPECT_POISONED(s[9]); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | int return_poisoned_int() { |
| 2422 | return ReturnPoisoned<U8>(); |
| 2423 | } |
| 2424 | |
| 2425 | TEST(MemorySanitizerDr, ReturnFromDSOTest) { |
| 2426 | if (!__msan_has_dynamic_component()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2427 | EXPECT_NOT_POISONED(dso_callfn(return_poisoned_int)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | 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] | 2431 | EXPECT_POISONED(x); |
| 2432 | EXPECT_POISONED(y); |
| 2433 | EXPECT_POISONED(z); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | 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] | 2438 | EXPECT_NOT_POISONED(x); |
| 2439 | EXPECT_NOT_POISONED(y); |
| 2440 | EXPECT_NOT_POISONED(z); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | TEST(MemorySanitizerDr, CallFromDSOTest) { |
| 2445 | if (!__msan_has_dynamic_component()) return; |
| 2446 | S8* x = GetPoisoned<S8>(); |
| 2447 | S8* y = GetPoisoned<S8>(); |
| 2448 | S8* z = GetPoisoned<S8>(); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2449 | EXPECT_NOT_POISONED(TrashParamTLS(*x, *y, *z)); |
| 2450 | EXPECT_NOT_POISONED(dso_callfn1(CheckParamTLS)); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | static void StackStoreInDSOFn(int* x, int* y) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2454 | EXPECT_NOT_POISONED(*x); |
| 2455 | EXPECT_NOT_POISONED(*y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | TEST(MemorySanitizerDr, StackStoreInDSOTest) { |
| 2459 | if (!__msan_has_dynamic_component()) return; |
| 2460 | dso_stack_store(StackStoreInDSOFn, 1); |
| 2461 | } |
| 2462 | |
| 2463 | TEST(MemorySanitizerOrigins, SetGet) { |
| 2464 | EXPECT_EQ(TrackingOrigins(), __msan_get_track_origins()); |
| 2465 | if (!TrackingOrigins()) return; |
| 2466 | int x; |
| 2467 | __msan_set_origin(&x, sizeof(x), 1234); |
| 2468 | EXPECT_EQ(1234, __msan_get_origin(&x)); |
| 2469 | __msan_set_origin(&x, sizeof(x), 5678); |
| 2470 | EXPECT_EQ(5678, __msan_get_origin(&x)); |
| 2471 | __msan_set_origin(&x, sizeof(x), 0); |
| 2472 | EXPECT_EQ(0, __msan_get_origin(&x)); |
| 2473 | } |
| 2474 | |
| 2475 | namespace { |
| 2476 | struct S { |
| 2477 | U4 dummy; |
| 2478 | U2 a; |
| 2479 | U2 b; |
| 2480 | }; |
| 2481 | |
| 2482 | // http://code.google.com/p/memory-sanitizer/issues/detail?id=6 |
| 2483 | TEST(MemorySanitizerOrigins, DISABLED_InitializedStoreDoesNotChangeOrigin) { |
| 2484 | if (!TrackingOrigins()) return; |
| 2485 | |
| 2486 | S s; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2487 | U4 origin = rand(); // NOLINT |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2488 | s.a = *GetPoisonedO<U2>(0, origin); |
| 2489 | EXPECT_EQ(origin, __msan_get_origin(&s.a)); |
| 2490 | EXPECT_EQ(origin, __msan_get_origin(&s.b)); |
| 2491 | |
| 2492 | s.b = 42; |
| 2493 | EXPECT_EQ(origin, __msan_get_origin(&s.a)); |
| 2494 | EXPECT_EQ(origin, __msan_get_origin(&s.b)); |
| 2495 | } |
| 2496 | } // namespace |
| 2497 | |
| 2498 | template<class T, class BinaryOp> |
| 2499 | INLINE |
| 2500 | void BinaryOpOriginTest(BinaryOp op) { |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2501 | U4 ox = rand(); //NOLINT |
| 2502 | U4 oy = rand(); //NOLINT |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2503 | T *x = GetPoisonedO<T>(0, ox, 0); |
| 2504 | T *y = GetPoisonedO<T>(1, oy, 0); |
| 2505 | T *z = GetPoisonedO<T>(2, 0, 0); |
| 2506 | |
| 2507 | *z = op(*x, *y); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2508 | U4 origin = __msan_get_origin(z); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2509 | EXPECT_POISONED_O(*z, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2510 | EXPECT_EQ(true, origin == ox || origin == oy); |
| 2511 | |
| 2512 | // y is poisoned, x is not. |
| 2513 | *x = 10101; |
| 2514 | *y = *GetPoisonedO<T>(1, oy); |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2515 | break_optimization(x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2516 | __msan_set_origin(z, sizeof(*z), 0); |
| 2517 | *z = op(*x, *y); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2518 | EXPECT_POISONED_O(*z, oy); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2519 | EXPECT_EQ(__msan_get_origin(z), oy); |
| 2520 | |
| 2521 | // x is poisoned, y is not. |
| 2522 | *x = *GetPoisonedO<T>(0, ox); |
| 2523 | *y = 10101010; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2524 | break_optimization(y); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2525 | __msan_set_origin(z, sizeof(*z), 0); |
| 2526 | *z = op(*x, *y); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2527 | EXPECT_POISONED_O(*z, ox); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2528 | EXPECT_EQ(__msan_get_origin(z), ox); |
| 2529 | } |
| 2530 | |
| 2531 | template<class T> INLINE T XOR(const T &a, const T&b) { return a ^ b; } |
| 2532 | template<class T> INLINE T ADD(const T &a, const T&b) { return a + b; } |
| 2533 | template<class T> INLINE T SUB(const T &a, const T&b) { return a - b; } |
| 2534 | template<class T> INLINE T MUL(const T &a, const T&b) { return a * b; } |
| 2535 | template<class T> INLINE T AND(const T &a, const T&b) { return a & b; } |
| 2536 | template<class T> INLINE T OR (const T &a, const T&b) { return a | b; } |
| 2537 | |
| 2538 | TEST(MemorySanitizerOrigins, BinaryOp) { |
| 2539 | if (!TrackingOrigins()) return; |
| 2540 | BinaryOpOriginTest<S8>(XOR<S8>); |
| 2541 | BinaryOpOriginTest<U8>(ADD<U8>); |
| 2542 | BinaryOpOriginTest<S4>(SUB<S4>); |
| 2543 | BinaryOpOriginTest<S4>(MUL<S4>); |
| 2544 | BinaryOpOriginTest<U4>(OR<U4>); |
| 2545 | BinaryOpOriginTest<U4>(AND<U4>); |
| 2546 | BinaryOpOriginTest<double>(ADD<U4>); |
| 2547 | BinaryOpOriginTest<float>(ADD<S4>); |
| 2548 | BinaryOpOriginTest<double>(ADD<double>); |
| 2549 | BinaryOpOriginTest<float>(ADD<double>); |
| 2550 | } |
| 2551 | |
| 2552 | TEST(MemorySanitizerOrigins, Unary) { |
| 2553 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2554 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 2555 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 2556 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 2557 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2558 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2559 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2560 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2561 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2562 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2563 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2564 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 2565 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 2566 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
| 2567 | EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2568 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2569 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2570 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2571 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
| 2572 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2573 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2574 | EXPECT_POISONED_O((void*)*GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 2575 | EXPECT_POISONED_O((U8)*GetPoisonedO<void*>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | TEST(MemorySanitizerOrigins, EQ) { |
| 2579 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2580 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) <= 11, __LINE__); |
| 2581 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) == 11, __LINE__); |
| 2582 | EXPECT_POISONED_O(*GetPoisonedO<float>(0, __LINE__) == 1.1, __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | TEST(MemorySanitizerOrigins, DIV) { |
| 2586 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2587 | EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) / 100, __LINE__); |
| 2588 | unsigned o = __LINE__; |
| 2589 | 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] | 2590 | } |
| 2591 | |
| 2592 | TEST(MemorySanitizerOrigins, SHIFT) { |
| 2593 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2594 | EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) >> 10, __LINE__); |
| 2595 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) >> 10, __LINE__); |
| 2596 | EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) << 10, __LINE__); |
| 2597 | EXPECT_POISONED_O(10U << *GetPoisonedO<U8>(0, __LINE__), __LINE__); |
| 2598 | EXPECT_POISONED_O(-10 >> *GetPoisonedO<S8>(0, __LINE__), __LINE__); |
| 2599 | EXPECT_POISONED_O(-10 << *GetPoisonedO<S8>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | template<class T, int N> |
| 2603 | void MemCpyTest() { |
| 2604 | int ox = __LINE__; |
| 2605 | T *x = new T[N]; |
| 2606 | T *y = new T[N]; |
| 2607 | T *z = new T[N]; |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 2608 | T *q = new T[N]; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2609 | __msan_poison(x, N * sizeof(T)); |
| 2610 | __msan_set_origin(x, N * sizeof(T), ox); |
| 2611 | __msan_set_origin(y, N * sizeof(T), 777777); |
| 2612 | __msan_set_origin(z, N * sizeof(T), 888888); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2613 | EXPECT_NOT_POISONED(x); |
| 2614 | memcpy(y, x, N * sizeof(T)); |
| 2615 | EXPECT_POISONED_O(y[0], ox); |
| 2616 | EXPECT_POISONED_O(y[N/2], ox); |
| 2617 | EXPECT_POISONED_O(y[N-1], ox); |
| 2618 | EXPECT_NOT_POISONED(x); |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 2619 | void *res = mempcpy(q, x, N * sizeof(T)); |
| 2620 | ASSERT_EQ(q + N, res); |
| 2621 | EXPECT_POISONED_O(q[0], ox); |
| 2622 | EXPECT_POISONED_O(q[N/2], ox); |
| 2623 | EXPECT_POISONED_O(q[N-1], ox); |
| 2624 | EXPECT_NOT_POISONED(x); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2625 | memmove(z, x, N * sizeof(T)); |
| 2626 | EXPECT_POISONED_O(z[0], ox); |
| 2627 | EXPECT_POISONED_O(z[N/2], ox); |
| 2628 | EXPECT_POISONED_O(z[N-1], ox); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | TEST(MemorySanitizerOrigins, LargeMemCpy) { |
| 2632 | if (!TrackingOrigins()) return; |
| 2633 | MemCpyTest<U1, 10000>(); |
| 2634 | MemCpyTest<U8, 10000>(); |
| 2635 | } |
| 2636 | |
| 2637 | TEST(MemorySanitizerOrigins, SmallMemCpy) { |
| 2638 | if (!TrackingOrigins()) return; |
| 2639 | MemCpyTest<U8, 1>(); |
| 2640 | MemCpyTest<U8, 2>(); |
| 2641 | MemCpyTest<U8, 3>(); |
| 2642 | } |
| 2643 | |
| 2644 | TEST(MemorySanitizerOrigins, Select) { |
| 2645 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2646 | EXPECT_NOT_POISONED(g_one ? 1 : *GetPoisonedO<S4>(0, __LINE__)); |
| 2647 | EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2648 | S4 x; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2649 | break_optimization(&x); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2650 | x = g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 0; |
| 2651 | |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2652 | EXPECT_POISONED_O(g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 1, __LINE__); |
| 2653 | EXPECT_POISONED_O(g_0 ? 1 : *GetPoisonedO<S4>(0, __LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | extern "C" |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2657 | NOINLINE char AllocaTO() { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2658 | int ar[100]; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2659 | break_optimization(ar); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2660 | return ar[10]; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2661 | // fprintf(stderr, "Descr: %s\n", |
| 2662 | // __msan_get_origin_descr_if_stack(__msan_get_origin_tls())); |
| 2663 | } |
| 2664 | |
| 2665 | TEST(MemorySanitizerOrigins, Alloca) { |
| 2666 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2667 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 2668 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 2669 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
| 2670 | EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO"); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | // FIXME: replace with a lit-like test. |
| 2674 | TEST(MemorySanitizerOrigins, DISABLED_AllocaDeath) { |
| 2675 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2676 | EXPECT_DEATH(AllocaTO(), "ORIGIN: stack allocation: ar@AllocaTO"); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2679 | NOINLINE int RetvalOriginTest(U4 origin) { |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2680 | int *a = new int; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2681 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2682 | __msan_set_origin(a, sizeof(*a), origin); |
| 2683 | int res = *a; |
| 2684 | delete a; |
| 2685 | return res; |
| 2686 | } |
| 2687 | |
| 2688 | TEST(MemorySanitizerOrigins, Retval) { |
| 2689 | if (!TrackingOrigins()) return; |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2690 | EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2693 | NOINLINE void ParamOriginTest(int param, U4 origin) { |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2694 | EXPECT_POISONED_O(param, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | TEST(MemorySanitizerOrigins, Param) { |
| 2698 | if (!TrackingOrigins()) return; |
| 2699 | int *a = new int; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2700 | U4 origin = __LINE__; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2701 | break_optimization(a); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2702 | __msan_set_origin(a, sizeof(*a), origin); |
| 2703 | ParamOriginTest(*a, origin); |
| 2704 | delete a; |
| 2705 | } |
| 2706 | |
| 2707 | TEST(MemorySanitizerOrigins, Invoke) { |
| 2708 | if (!TrackingOrigins()) return; |
| 2709 | StructWithDtor s; // Will cause the calls to become invokes. |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2710 | EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | TEST(MemorySanitizerOrigins, strlen) { |
| 2714 | S8 alignment; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2715 | break_optimization(&alignment); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2716 | char x[4] = {'a', 'b', 0, 0}; |
| 2717 | __msan_poison(&x[2], 1); |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2718 | U4 origin = __LINE__; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2719 | __msan_set_origin(x, sizeof(x), origin); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2720 | EXPECT_UMR_O(volatile unsigned y = strlen(x), origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | TEST(MemorySanitizerOrigins, wcslen) { |
| 2724 | wchar_t w[3] = {'a', 'b', 0}; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2725 | U4 origin = __LINE__; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2726 | __msan_set_origin(w, sizeof(w), origin); |
| 2727 | __msan_poison(&w[2], sizeof(wchar_t)); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2728 | EXPECT_UMR_O(volatile unsigned y = wcslen(w), origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | #if MSAN_HAS_M128 |
| 2732 | TEST(MemorySanitizerOrigins, StoreIntrinsic) { |
| 2733 | __m128 x, y; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 2734 | U4 origin = __LINE__; |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2735 | __msan_set_origin(&x, sizeof(x), origin); |
| 2736 | __msan_poison(&x, sizeof(x)); |
| 2737 | __builtin_ia32_storeups((float*)&y, x); |
Evgeniy Stepanov | 1192900 | 2013-01-22 12:29:00 +0000 | [diff] [blame] | 2738 | EXPECT_POISONED_O(y, origin); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2739 | } |
| 2740 | #endif |
| 2741 | |
| 2742 | NOINLINE void RecursiveMalloc(int depth) { |
| 2743 | static int count; |
| 2744 | count++; |
| 2745 | if ((count % (1024 * 1024)) == 0) |
| 2746 | printf("RecursiveMalloc: %d\n", count); |
| 2747 | int *x1 = new int; |
| 2748 | int *x2 = new int; |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 2749 | break_optimization(x1); |
| 2750 | break_optimization(x2); |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2751 | if (depth > 0) { |
| 2752 | RecursiveMalloc(depth-1); |
| 2753 | RecursiveMalloc(depth-1); |
| 2754 | } |
| 2755 | delete x1; |
| 2756 | delete x2; |
| 2757 | } |
| 2758 | |
Kostya Serebryany | 65199f1 | 2013-01-25 11:46:22 +0000 | [diff] [blame] | 2759 | TEST(MemorySanitizer, CallocOverflow) { |
| 2760 | size_t kArraySize = 4096; |
| 2761 | volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); |
| 2762 | volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 2763 | void *p = calloc(kArraySize, kArraySize2); // Should return 0. |
| 2764 | EXPECT_EQ(0L, Ident(p)); |
| 2765 | } |
| 2766 | |
Evgeniy Stepanov | 0231c50 | 2012-12-25 12:39:56 +0000 | [diff] [blame] | 2767 | TEST(MemorySanitizerStress, DISABLED_MallocStackTrace) { |
| 2768 | RecursiveMalloc(22); |
| 2769 | } |