blob: 4dbe291cf9693a87c8c3a7c634f5a3e2b7ffc476 [file] [log] [blame]
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001//===-- msan_test.cc ------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of MemorySanitizer.
11//
12// MemorySanitizer unit tests.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer/msan_interface.h"
16#include "msandr_test_so.h"
17#include "gtest/gtest.h"
18
19#include <stdlib.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <assert.h>
23#include <wchar.h>
24
Evgeniy Stepanove03345b2013-01-17 13:42:17 +000025#include <dlfcn.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000026#include <unistd.h>
27#include <limits.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <sys/resource.h>
33#include <sys/ioctl.h>
34#include <sys/utsname.h>
35#include <sys/mman.h>
36#include <sys/vfs.h>
37
38#if defined(__i386__) || defined(__x86_64__)
39# include <emmintrin.h>
40# define MSAN_HAS_M128 1
41#else
42# define MSAN_HAS_M128 0
43#endif
44
45typedef unsigned char U1;
46typedef unsigned short U2; // NOLINT
47typedef unsigned int U4;
48typedef unsigned long long U8; // NOLINT
49typedef signed char S1;
50typedef signed short S2; // NOLINT
51typedef signed int S4;
52typedef signed long long S8; // NOLINT
53#define NOINLINE __attribute__((noinline))
54#define INLINE __attribute__((always_inline))
55
Evgeniy Stepanov11929002013-01-22 12:29:00 +000056static bool TrackingOrigins() {
57 S8 x;
58 __msan_set_origin(&x, sizeof(x), 0x1234);
59 u32 origin = __msan_get_origin(&x);
60 __msan_set_origin(&x, sizeof(x), 0);
61 return origin == 0x1234;
62}
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000063
Evgeniy Stepanov11929002013-01-22 12:29:00 +000064#define EXPECT_UMR(action) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000065 do { \
66 __msan_set_expect_umr(1); \
67 action; \
68 __msan_set_expect_umr(0); \
69 } while (0)
70
Evgeniy Stepanov11929002013-01-22 12:29:00 +000071#define EXPECT_UMR_O(action, origin) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000072 do { \
73 __msan_set_expect_umr(1); \
74 action; \
75 __msan_set_expect_umr(0); \
76 if (TrackingOrigins()) \
77 EXPECT_EQ(origin, __msan_get_origin_tls()); \
78 } while (0)
79
Evgeniy Stepanov11929002013-01-22 12:29:00 +000080#define EXPECT_UMR_S(action, stack_origin) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000081 do { \
82 __msan_set_expect_umr(1); \
83 action; \
84 __msan_set_expect_umr(0); \
85 u32 id = __msan_get_origin_tls(); \
86 const char *str = __msan_get_origin_descr_if_stack(id); \
87 if (!str || strcmp(str, stack_origin)) { \
88 fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", \
89 id, stack_origin, str); \
90 EXPECT_EQ(1, 0); \
91 } \
92 } while (0)
93
Evgeniy Stepanov11929002013-01-22 12:29:00 +000094#define EXPECT_POISONED(x) ExpectPoisoned(x)
95
96template<typename T>
97void ExpectPoisoned(const T& t) {
98 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
99}
100
101#define EXPECT_POISONED_O(x, origin) \
102 ExpectPoisonedWithOrigin(x, origin)
103
104template<typename T>
105void ExpectPoisonedWithOrigin(const T& t, unsigned origin) {
106 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
107 if (TrackingOrigins())
108 EXPECT_EQ(origin, __msan_get_origin((void*)&t));
109}
110
111#define EXPECT_POISONED_S(x, stack_origin) \
112 ExpectPoisonedWithStackOrigin(x, stack_origin)
113
114template<typename T>
115void ExpectPoisonedWithStackOrigin(const T& t, const char *stack_origin) {
116 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
117 u32 id = __msan_get_origin((void*)&t);
118 const char *str = __msan_get_origin_descr_if_stack(id);
119 if (!str || strcmp(str, stack_origin)) {
120 fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s",
121 id, stack_origin, str);
122 EXPECT_EQ(1, 0);
123 }
124}
125
126#define EXPECT_NOT_POISONED(x) ExpectNotPoisoned(x)
127
128template<typename T>
129void ExpectNotPoisoned(const T& t) {
130 EXPECT_EQ(-1, __msan_test_shadow((void*)&t, sizeof(t)));
131}
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000132
133static U8 poisoned_array[100];
134template<class T>
135T *GetPoisoned(int i = 0, T val = 0) {
136 T *res = (T*)&poisoned_array[i];
137 *res = val;
138 __msan_poison(&poisoned_array[i], sizeof(T));
139 return res;
140}
141
142template<class T>
143T *GetPoisonedO(int i, u32 origin, T val = 0) {
144 T *res = (T*)&poisoned_array[i];
145 *res = val;
146 __msan_poison(&poisoned_array[i], sizeof(T));
147 __msan_set_origin(&poisoned_array[i], sizeof(T), origin);
148 return res;
149}
150
151// This function returns its parameter but in such a way that compiler
152// can not prove it.
153template<class T>
154NOINLINE
155static T Ident(T t) {
156 volatile T ret = t;
157 return ret;
158}
159
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000160template<class T> NOINLINE T ReturnPoisoned() { return *GetPoisoned<T>(); }
161
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000162static volatile int g_one = 1;
163static volatile int g_zero = 0;
164static volatile int g_0 = 0;
165static volatile int g_1 = 1;
166
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000167S4 a_s4[100];
168S8 a_s8[100];
169
170TEST(MemorySanitizer, NegativeTest1) {
171 S4 *x = GetPoisoned<S4>();
172 if (g_one)
173 *x = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000174 EXPECT_NOT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000175}
176
177TEST(MemorySanitizer, PositiveTest1) {
178 // Load to store.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000179 EXPECT_POISONED(*GetPoisoned<S1>());
180 EXPECT_POISONED(*GetPoisoned<S2>());
181 EXPECT_POISONED(*GetPoisoned<S4>());
182 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000183
184 // S->S conversions.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000185 EXPECT_POISONED(*GetPoisoned<S1>());
186 EXPECT_POISONED(*GetPoisoned<S1>());
187 EXPECT_POISONED(*GetPoisoned<S1>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000188
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000189 EXPECT_POISONED(*GetPoisoned<S2>());
190 EXPECT_POISONED(*GetPoisoned<S2>());
191 EXPECT_POISONED(*GetPoisoned<S2>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000192
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000193 EXPECT_POISONED(*GetPoisoned<S4>());
194 EXPECT_POISONED(*GetPoisoned<S4>());
195 EXPECT_POISONED(*GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000196
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000197 EXPECT_POISONED(*GetPoisoned<S8>());
198 EXPECT_POISONED(*GetPoisoned<S8>());
199 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000200
201 // ZExt
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000202 EXPECT_POISONED(*GetPoisoned<U1>());
203 EXPECT_POISONED(*GetPoisoned<U1>());
204 EXPECT_POISONED(*GetPoisoned<U1>());
205 EXPECT_POISONED(*GetPoisoned<U2>());
206 EXPECT_POISONED(*GetPoisoned<U2>());
207 EXPECT_POISONED(*GetPoisoned<U4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000208
209 // Unary ops.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000210 EXPECT_POISONED(- *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000211
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000212 EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000213
214
215 a_s4[g_zero] = 1 - *GetPoisoned<S4>();
216 a_s4[g_zero] = 1 + *GetPoisoned<S4>();
217}
218
219TEST(MemorySanitizer, Phi1) {
220 S4 c;
221 if (g_one) {
222 c = *GetPoisoned<S4>();
223 } else {
224 __msan_break_optimization(0);
225 c = 0;
226 }
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000227 EXPECT_POISONED(c);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000228}
229
230TEST(MemorySanitizer, Phi2) {
231 S4 i = *GetPoisoned<S4>();
232 S4 n = g_one;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000233 EXPECT_UMR(for (; i < g_one; i++););
234 EXPECT_POISONED(i);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000235}
236
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000237NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); }
238NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); }
239NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); }
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000240
241TEST(MemorySanitizer, ArgTest) {
242 Arg1ExpectUMR(*GetPoisoned<S4>());
243 Arg2ExpectUMR(0, *GetPoisoned<S4>());
244 Arg3ExpectUMR(0, 1, *GetPoisoned<S8>());
245}
246
247
248TEST(MemorySanitizer, CallAndRet) {
249 if (!__msan_has_dynamic_component()) return;
250 ReturnPoisoned<S1>();
251 ReturnPoisoned<S2>();
252 ReturnPoisoned<S4>();
253 ReturnPoisoned<S8>();
254
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000255 EXPECT_POISONED(ReturnPoisoned<S1>());
256 EXPECT_POISONED(ReturnPoisoned<S2>());
257 EXPECT_POISONED(ReturnPoisoned<S4>());
258 EXPECT_POISONED(ReturnPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000259}
260
261// malloc() in the following test may be optimized to produce a compile-time
262// undef value. Check that we trap on the volatile assignment anyway.
263TEST(MemorySanitizer, DISABLED_MallocNoIdent) {
264 S4 *x = (int*)malloc(sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000265 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000266 free(x);
267}
268
269TEST(MemorySanitizer, Malloc) {
270 S4 *x = (int*)Ident(malloc(sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000271 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000272 free(x);
273}
274
275TEST(MemorySanitizer, Realloc) {
276 S4 *x = (int*)Ident(realloc(0, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000277 EXPECT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000278 x[0] = 1;
279 x = (int*)Ident(realloc(x, 2 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000280 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
281 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000282 x = (int*)Ident(realloc(x, 3 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000283 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
284 EXPECT_POISONED(x[2]);
285 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000286 x[2] = 1; // Init this here. Check that after realloc it is poisoned again.
287 x = (int*)Ident(realloc(x, 2 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000288 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
289 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000290 x = (int*)Ident(realloc(x, 3 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000291 EXPECT_POISONED(x[1]);
292 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000293 free(x);
294}
295
296TEST(MemorySanitizer, Calloc) {
297 S4 *x = (int*)Ident(calloc(1, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000298 EXPECT_NOT_POISONED(*x); // Should not be poisoned.
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000299 // EXPECT_EQ(0, *x);
300 free(x);
301}
302
303TEST(MemorySanitizer, AndOr) {
304 U4 *p = GetPoisoned<U4>();
305 // We poison two bytes in the midle of a 4-byte word to make the test
306 // correct regardless of endianness.
307 ((U1*)p)[1] = 0;
308 ((U1*)p)[2] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000309 EXPECT_NOT_POISONED(*p & 0x00ffff00);
310 EXPECT_NOT_POISONED(*p & 0x00ff0000);
311 EXPECT_NOT_POISONED(*p & 0x0000ff00);
312 EXPECT_POISONED(*p & 0xff000000);
313 EXPECT_POISONED(*p & 0x000000ff);
314 EXPECT_POISONED(*p & 0x0000ffff);
315 EXPECT_POISONED(*p & 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000316
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000317 EXPECT_NOT_POISONED(*p | 0xff0000ff);
318 EXPECT_NOT_POISONED(*p | 0xff00ffff);
319 EXPECT_NOT_POISONED(*p | 0xffff00ff);
320 EXPECT_POISONED(*p | 0xff000000);
321 EXPECT_POISONED(*p | 0x000000ff);
322 EXPECT_POISONED(*p | 0x0000ffff);
323 EXPECT_POISONED(*p | 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000324
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000325 EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000326}
327
328template<class T>
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000329static bool applyNot(T value, T shadow) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000330 __msan_partial_poison(&value, &shadow, sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000331 return !value;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000332}
333
334TEST(MemorySanitizer, Not) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000335 EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0));
336 EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0));
337 EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF));
338 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF));
339 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF));
340 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF));
341 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000));
342 EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000));
343 EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000));
344 EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000345
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000346 EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0));
347 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE));
348 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0));
349 EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000350
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000351 EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1)));
352 EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2)));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000353}
354
355TEST(MemorySanitizer, Shift) {
356 U4 *up = GetPoisoned<U4>();
357 ((U1*)up)[0] = 0;
358 ((U1*)up)[3] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000359 EXPECT_NOT_POISONED(*up >> 30);
360 EXPECT_NOT_POISONED(*up >> 24);
361 EXPECT_POISONED(*up >> 23);
362 EXPECT_POISONED(*up >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000363
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000364 EXPECT_NOT_POISONED(*up << 30);
365 EXPECT_NOT_POISONED(*up << 24);
366 EXPECT_POISONED(*up << 23);
367 EXPECT_POISONED(*up << 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000368
369 S4 *sp = (S4*)up;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000370 EXPECT_NOT_POISONED(*sp >> 30);
371 EXPECT_NOT_POISONED(*sp >> 24);
372 EXPECT_POISONED(*sp >> 23);
373 EXPECT_POISONED(*sp >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000374
375 sp = GetPoisoned<S4>();
376 ((S1*)sp)[1] = 0;
377 ((S1*)sp)[2] = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000378 EXPECT_POISONED(*sp >> 31);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000379
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000380 EXPECT_POISONED(100 >> *GetPoisoned<S4>());
381 EXPECT_POISONED(100U >> *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000382}
383
384NOINLINE static int GetPoisonedZero() {
385 int *zero = new int;
386 *zero = 0;
387 __msan_poison(zero, sizeof(*zero));
388 int res = *zero;
389 delete zero;
390 return res;
391}
392
393TEST(MemorySanitizer, LoadFromDirtyAddress) {
394 int *a = new int;
395 *a = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000396 EXPECT_UMR(__msan_break_optimization((void*)(U8)a[GetPoisonedZero()]));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000397 delete a;
398}
399
400TEST(MemorySanitizer, StoreToDirtyAddress) {
401 int *a = new int;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000402 EXPECT_UMR(a[GetPoisonedZero()] = 0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000403 __msan_break_optimization(a);
404 delete a;
405}
406
407
408NOINLINE void StackTestFunc() {
409 S4 p4;
410 S4 ok4 = 1;
411 S2 p2;
412 S2 ok2 = 1;
413 S1 p1;
414 S1 ok1 = 1;
415 __msan_break_optimization(&p4);
416 __msan_break_optimization(&ok4);
417 __msan_break_optimization(&p2);
418 __msan_break_optimization(&ok2);
419 __msan_break_optimization(&p1);
420 __msan_break_optimization(&ok1);
421
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000422 EXPECT_POISONED(p4);
423 EXPECT_POISONED(p2);
424 EXPECT_POISONED(p1);
425 EXPECT_NOT_POISONED(ok1);
426 EXPECT_NOT_POISONED(ok2);
427 EXPECT_NOT_POISONED(ok4);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000428}
429
430TEST(MemorySanitizer, StackTest) {
431 StackTestFunc();
432}
433
434NOINLINE void StackStressFunc() {
435 int foo[10000];
436 __msan_break_optimization(foo);
437}
438
439TEST(MemorySanitizer, DISABLED_StackStressTest) {
440 for (int i = 0; i < 1000000; i++)
441 StackStressFunc();
442}
443
444template<class T>
445void TestFloatingPoint() {
446 static volatile T v;
447 static T g[100];
448 __msan_break_optimization(&g);
449 T *x = GetPoisoned<T>();
450 T *y = GetPoisoned<T>(1);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000451 EXPECT_POISONED(*x);
452 EXPECT_POISONED((long long)*x);
453 EXPECT_POISONED((int)*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000454 g[0] = *x;
455 g[1] = *x + *y;
456 g[2] = *x - *y;
457 g[3] = *x * *y;
458}
459
460TEST(MemorySanitizer, FloatingPointTest) {
461 TestFloatingPoint<float>();
462 TestFloatingPoint<double>();
463}
464
465TEST(MemorySanitizer, DynMem) {
466 S4 x = 0;
467 S4 *y = GetPoisoned<S4>();
468 memcpy(y, &x, g_one * sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000469 EXPECT_NOT_POISONED(*y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000470}
471
472static char *DynRetTestStr;
473
474TEST(MemorySanitizer, DynRet) {
475 if (!__msan_has_dynamic_component()) return;
476 ReturnPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000477 EXPECT_NOT_POISONED(clearenv());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000478}
479
480
481TEST(MemorySanitizer, DynRet1) {
482 if (!__msan_has_dynamic_component()) return;
483 ReturnPoisoned<S8>();
484}
485
486struct LargeStruct {
487 S4 x[10];
488};
489
490NOINLINE
491LargeStruct LargeRetTest() {
492 LargeStruct res;
493 res.x[0] = *GetPoisoned<S4>();
494 res.x[1] = *GetPoisoned<S4>();
495 res.x[2] = *GetPoisoned<S4>();
496 res.x[3] = *GetPoisoned<S4>();
497 res.x[4] = *GetPoisoned<S4>();
498 res.x[5] = *GetPoisoned<S4>();
499 res.x[6] = *GetPoisoned<S4>();
500 res.x[7] = *GetPoisoned<S4>();
501 res.x[8] = *GetPoisoned<S4>();
502 res.x[9] = *GetPoisoned<S4>();
503 return res;
504}
505
506TEST(MemorySanitizer, LargeRet) {
507 LargeStruct a = LargeRetTest();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000508 EXPECT_POISONED(a.x[0]);
509 EXPECT_POISONED(a.x[9]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000510}
511
512TEST(MemorySanitizer, fread) {
513 char *x = new char[32];
514 FILE *f = fopen("/proc/self/stat", "r");
515 assert(f);
516 fread(x, 1, 32, f);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000517 EXPECT_NOT_POISONED(x[0]);
518 EXPECT_NOT_POISONED(x[16]);
519 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000520 fclose(f);
521 delete x;
522}
523
524TEST(MemorySanitizer, read) {
525 char *x = new char[32];
526 int fd = open("/proc/self/stat", O_RDONLY);
527 assert(fd > 0);
528 int sz = read(fd, x, 32);
529 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000530 EXPECT_NOT_POISONED(x[0]);
531 EXPECT_NOT_POISONED(x[16]);
532 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000533 close(fd);
534 delete x;
535}
536
537TEST(MemorySanitizer, pread) {
538 char *x = new char[32];
539 int fd = open("/proc/self/stat", O_RDONLY);
540 assert(fd > 0);
541 int sz = pread(fd, x, 32, 0);
542 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000543 EXPECT_NOT_POISONED(x[0]);
544 EXPECT_NOT_POISONED(x[16]);
545 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000546 close(fd);
547 delete x;
548}
549
550// FIXME: fails now.
551TEST(MemorySanitizer, DISABLED_ioctl) {
552 struct winsize ws;
553 EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000554 EXPECT_NOT_POISONED(ws.ws_col);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000555}
556
557TEST(MemorySanitizer, readlink) {
558 char *x = new char[1000];
559 readlink("/proc/self/exe", x, 1000);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000560 EXPECT_NOT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000561 delete [] x;
562}
563
564
565TEST(MemorySanitizer, stat) {
566 struct stat* st = new struct stat;
567 int res = stat("/proc/self/stat", st);
568 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000569 EXPECT_NOT_POISONED(st->st_dev);
570 EXPECT_NOT_POISONED(st->st_mode);
571 EXPECT_NOT_POISONED(st->st_size);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000572}
573
574TEST(MemorySanitizer, statfs) {
575 struct statfs* st = new struct statfs;
576 int res = statfs("/", st);
577 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000578 EXPECT_NOT_POISONED(st->f_type);
579 EXPECT_NOT_POISONED(st->f_bfree);
580 EXPECT_NOT_POISONED(st->f_namelen);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000581}
582
583TEST(MemorySanitizer, pipe) {
584 int* pipefd = new int[2];
585 int res = pipe(pipefd);
586 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000587 EXPECT_NOT_POISONED(pipefd[0]);
588 EXPECT_NOT_POISONED(pipefd[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000589 close(pipefd[0]);
590 close(pipefd[1]);
591}
592
593TEST(MemorySanitizer, getcwd) {
594 char path[PATH_MAX + 1];
595 char* res = getcwd(path, sizeof(path));
596 assert(res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000597 EXPECT_NOT_POISONED(path[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000598}
599
600TEST(MemorySanitizer, realpath) {
601 const char* relpath = ".";
602 char path[PATH_MAX + 1];
603 char* res = realpath(relpath, path);
604 assert(res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000605 EXPECT_NOT_POISONED(path[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000606}
607
608TEST(MemorySanitizer, memcpy) {
609 char* x = new char[2];
610 char* y = new char[2];
611 x[0] = 1;
612 x[1] = *GetPoisoned<char>();
613 memcpy(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000614 EXPECT_NOT_POISONED(y[0]);
615 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000616}
617
618TEST(MemorySanitizer, memmove) {
619 char* x = new char[2];
620 char* y = new char[2];
621 x[0] = 1;
622 x[1] = *GetPoisoned<char>();
623 memmove(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000624 EXPECT_NOT_POISONED(y[0]);
625 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000626}
627
628TEST(MemorySanitizer, strdup) {
629 char *x = strdup("zzz");
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000630 EXPECT_NOT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000631 free(x);
632}
633
634template<class T, int size>
635void TestOverlapMemmove() {
636 T *x = new T[size];
637 assert(size >= 3);
638 x[2] = 0;
639 memmove(x, x + 1, (size - 1) * sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000640 EXPECT_NOT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000641 if (!__msan_has_dynamic_component()) {
642 // FIXME: under DR we will lose this information
643 // because accesses in memmove will unpoisin the shadow.
644 // We need to use our own memove implementation instead of libc's.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000645 EXPECT_POISONED(x[0]);
646 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000647 }
648 delete [] x;
649}
650
651TEST(MemorySanitizer, overlap_memmove) {
652 TestOverlapMemmove<U1, 10>();
653 TestOverlapMemmove<U1, 1000>();
654 TestOverlapMemmove<U8, 4>();
655 TestOverlapMemmove<U8, 1000>();
656}
657
658TEST(MemorySanitizer, strcpy) { // NOLINT
659 char* x = new char[3];
660 char* y = new char[3];
661 x[0] = 'a';
662 x[1] = *GetPoisoned<char>(1, 1);
663 x[2] = 0;
664 strcpy(y, x); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000665 EXPECT_NOT_POISONED(y[0]);
666 EXPECT_POISONED(y[1]);
667 EXPECT_NOT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000668}
669
670TEST(MemorySanitizer, strncpy) { // NOLINT
671 char* x = new char[3];
672 char* y = new char[3];
673 x[0] = 'a';
674 x[1] = *GetPoisoned<char>(1, 1);
675 x[2] = 0;
676 strncpy(y, x, 2); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000677 EXPECT_NOT_POISONED(y[0]);
678 EXPECT_POISONED(y[1]);
679 EXPECT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000680}
681
682TEST(MemorySanitizer, strtol) {
683 char *e;
684 assert(1 == strtol("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000685 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000686}
687
688TEST(MemorySanitizer, strtoll) {
689 char *e;
690 assert(1 == strtoll("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000691 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000692}
693
694TEST(MemorySanitizer, strtoul) {
695 char *e;
696 assert(1 == strtoul("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000697 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000698}
699
700TEST(MemorySanitizer, strtoull) {
701 char *e;
702 assert(1 == strtoull("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000703 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000704}
705
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000706TEST(MemorySanitizer, strtod) {
707 char *e;
708 assert(0 != strtod("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000709 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000710}
711
712TEST(MemorySanitizer, strtof) {
713 char *e;
714 assert(0 != strtof("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000715 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000716}
717
718TEST(MemorySanitizer, strtold) {
719 char *e;
720 assert(0 != strtold("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000721 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000722}
723
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000724TEST(MemorySanitizer, sprintf) { // NOLINT
725 char buff[10];
726 __msan_break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000727 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000728 int res = sprintf(buff, "%d", 1234567); // NOLINT
729 assert(res == 7);
730 assert(buff[0] == '1');
731 assert(buff[1] == '2');
732 assert(buff[2] == '3');
733 assert(buff[6] == '7');
734 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000735 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000736}
737
738TEST(MemorySanitizer, snprintf) {
739 char buff[10];
740 __msan_break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000741 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000742 int res = snprintf(buff, sizeof(buff), "%d", 1234567);
743 assert(res == 7);
744 assert(buff[0] == '1');
745 assert(buff[1] == '2');
746 assert(buff[2] == '3');
747 assert(buff[6] == '7');
748 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000749 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000750}
751
752TEST(MemorySanitizer, swprintf) {
753 wchar_t buff[10];
754 assert(sizeof(wchar_t) == 4);
755 __msan_break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000756 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000757 int res = swprintf(buff, 9, L"%d", 1234567);
758 assert(res == 7);
759 assert(buff[0] == '1');
760 assert(buff[1] == '2');
761 assert(buff[2] == '3');
762 assert(buff[6] == '7');
763 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000764 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000765}
766
767TEST(MemorySanitizer, wcstombs) {
768 const wchar_t *x = L"abc";
769 char buff[10];
770 int res = wcstombs(buff, x, 4);
771 EXPECT_EQ(res, 3);
772 EXPECT_EQ(buff[0], 'a');
773 EXPECT_EQ(buff[1], 'b');
774 EXPECT_EQ(buff[2], 'c');
775}
776
777TEST(MemorySanitizer, gettimeofday) {
778 struct timeval tv;
779 struct timezone tz;
780 __msan_break_optimization(&tv);
781 __msan_break_optimization(&tz);
782 assert(sizeof(tv) == 16);
783 assert(sizeof(tz) == 8);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000784 EXPECT_POISONED(tv.tv_sec);
785 EXPECT_POISONED(tv.tv_usec);
786 EXPECT_POISONED(tz.tz_minuteswest);
787 EXPECT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000788 assert(0 == gettimeofday(&tv, &tz));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000789 EXPECT_NOT_POISONED(tv.tv_sec);
790 EXPECT_NOT_POISONED(tv.tv_usec);
791 EXPECT_NOT_POISONED(tz.tz_minuteswest);
792 EXPECT_NOT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000793}
794
795TEST(MemorySanitizer, mmap) {
796 const int size = 4096;
797 void *p1, *p2;
798 p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
799 __msan_poison(p1, size);
800 munmap(p1, size);
801 for (int i = 0; i < 1000; i++) {
802 p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
803 if (p2 == p1)
804 break;
805 else
806 munmap(p2, size);
807 }
808 if (p1 == p2) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000809 EXPECT_NOT_POISONED(*(char*)p2);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000810 munmap(p2, size);
811 }
812}
813
814// FIXME: enable and add ecvt.
815// FIXME: check why msandr does nt handle fcvt.
816TEST(MemorySanitizer, fcvt) {
817 int a, b;
818 __msan_break_optimization(&a);
819 __msan_break_optimization(&b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000820 EXPECT_POISONED(a);
821 EXPECT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000822 char *str = fcvt(12345.6789, 10, &a, &b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000823 EXPECT_NOT_POISONED(a);
824 EXPECT_NOT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000825}
826
827TEST(MemorySanitizer, LoadUnpoisoned) {
828 S8 s = *GetPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000829 EXPECT_POISONED(s);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000830 S8 safe = *GetPoisoned<S8>();
831 __msan_load_unpoisoned(&s, sizeof(s), &safe);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000832 EXPECT_NOT_POISONED(safe);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000833}
834
835struct StructWithDtor {
836 ~StructWithDtor();
837};
838
839NOINLINE StructWithDtor::~StructWithDtor() {
840 __msan_break_optimization(0);
841}
842
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000843TEST(MemorySanitizer, Invoke) {
844 StructWithDtor s; // Will cause the calls to become invokes.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000845 EXPECT_NOT_POISONED(0);
846 EXPECT_POISONED(*GetPoisoned<int>());
847 EXPECT_NOT_POISONED(0);
848 EXPECT_POISONED(*GetPoisoned<int>());
849 EXPECT_POISONED(ReturnPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000850}
851
852TEST(MemorySanitizer, ptrtoint) {
853 // Test that shadow is propagated through pointer-to-integer conversion.
854 void* p = (void*)0xABCD;
855 __msan_poison(((char*)&p) + 1, sizeof(p));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000856 EXPECT_NOT_POISONED((((uptr)p) & 0xFF) == 0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000857
858 void* q = (void*)0xABCD;
859 __msan_poison(&q, sizeof(q) - 1);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000860 EXPECT_POISONED((((uptr)q) & 0xFF) == 0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000861}
862
863static void vaargsfn2(int guard, ...) {
864 va_list vl;
865 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000866 EXPECT_NOT_POISONED(va_arg(vl, int));
867 EXPECT_NOT_POISONED(va_arg(vl, int));
868 EXPECT_NOT_POISONED(va_arg(vl, int));
869 EXPECT_POISONED(va_arg(vl, double));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000870 va_end(vl);
871}
872
873static void vaargsfn(int guard, ...) {
874 va_list vl;
875 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000876 EXPECT_NOT_POISONED(va_arg(vl, int));
877 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000878 // The following call will overwrite __msan_param_tls.
879 // Checks after it test that arg shadow was somehow saved across the call.
880 vaargsfn2(1, 2, 3, 4, *GetPoisoned<double>());
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000881 EXPECT_NOT_POISONED(va_arg(vl, int));
882 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000883 va_end(vl);
884}
885
886TEST(MemorySanitizer, VAArgTest) {
887 int* x = GetPoisoned<int>();
888 int* y = GetPoisoned<int>(4);
889 vaargsfn(1, 13, *x, 42, *y);
890}
891
892static void vaargsfn_many(int guard, ...) {
893 va_list vl;
894 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000895 EXPECT_NOT_POISONED(va_arg(vl, int));
896 EXPECT_POISONED(va_arg(vl, int));
897 EXPECT_NOT_POISONED(va_arg(vl, int));
898 EXPECT_NOT_POISONED(va_arg(vl, int));
899 EXPECT_NOT_POISONED(va_arg(vl, int));
900 EXPECT_NOT_POISONED(va_arg(vl, int));
901 EXPECT_NOT_POISONED(va_arg(vl, int));
902 EXPECT_NOT_POISONED(va_arg(vl, int));
903 EXPECT_NOT_POISONED(va_arg(vl, int));
904 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000905 va_end(vl);
906}
907
908TEST(MemorySanitizer, VAArgManyTest) {
909 int* x = GetPoisoned<int>();
910 int* y = GetPoisoned<int>(4);
911 vaargsfn_many(1, 2, *x, 3, 4, 5, 6, 7, 8, 9, *y);
912}
913
914static void vaargsfn_pass2(va_list vl) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000915 EXPECT_NOT_POISONED(va_arg(vl, int));
916 EXPECT_NOT_POISONED(va_arg(vl, int));
917 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000918}
919
920static void vaargsfn_pass(int guard, ...) {
921 va_list vl;
922 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000923 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000924 vaargsfn_pass2(vl);
925 va_end(vl);
926}
927
928TEST(MemorySanitizer, VAArgPass) {
929 int* x = GetPoisoned<int>();
930 int* y = GetPoisoned<int>(4);
931 vaargsfn_pass(1, *x, 2, 3, *y);
932}
933
934static void vaargsfn_copy2(va_list vl) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000935 EXPECT_NOT_POISONED(va_arg(vl, int));
936 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000937}
938
939static void vaargsfn_copy(int guard, ...) {
940 va_list vl;
941 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000942 EXPECT_NOT_POISONED(va_arg(vl, int));
943 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000944 va_list vl2;
945 va_copy(vl2, vl);
946 vaargsfn_copy2(vl2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000947 EXPECT_NOT_POISONED(va_arg(vl, int));
948 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000949 va_end(vl);
950}
951
952TEST(MemorySanitizer, VAArgCopy) {
953 int* x = GetPoisoned<int>();
954 int* y = GetPoisoned<int>(4);
955 vaargsfn_copy(1, 2, *x, 3, *y);
956}
957
958static void vaargsfn_ptr(int guard, ...) {
959 va_list vl;
960 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000961 EXPECT_NOT_POISONED(va_arg(vl, int*));
962 EXPECT_POISONED(va_arg(vl, int*));
963 EXPECT_NOT_POISONED(va_arg(vl, int*));
964 EXPECT_POISONED(va_arg(vl, double*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000965 va_end(vl);
966}
967
968TEST(MemorySanitizer, VAArgPtr) {
969 int** x = GetPoisoned<int*>();
970 double** y = GetPoisoned<double*>(8);
971 int z;
972 vaargsfn_ptr(1, &z, *x, &z, *y);
973}
974
975static void vaargsfn_overflow(int guard, ...) {
976 va_list vl;
977 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000978 EXPECT_NOT_POISONED(va_arg(vl, int));
979 EXPECT_NOT_POISONED(va_arg(vl, int));
980 EXPECT_POISONED(va_arg(vl, int));
981 EXPECT_NOT_POISONED(va_arg(vl, int));
982 EXPECT_NOT_POISONED(va_arg(vl, int));
983 EXPECT_NOT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000984
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000985 EXPECT_NOT_POISONED(va_arg(vl, double));
986 EXPECT_NOT_POISONED(va_arg(vl, double));
987 EXPECT_NOT_POISONED(va_arg(vl, double));
988 EXPECT_POISONED(va_arg(vl, double));
989 EXPECT_NOT_POISONED(va_arg(vl, double));
990 EXPECT_POISONED(va_arg(vl, int*));
991 EXPECT_NOT_POISONED(va_arg(vl, double));
992 EXPECT_NOT_POISONED(va_arg(vl, double));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000993
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000994 EXPECT_POISONED(va_arg(vl, int));
995 EXPECT_POISONED(va_arg(vl, double));
996 EXPECT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000997
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000998 EXPECT_NOT_POISONED(va_arg(vl, int));
999 EXPECT_NOT_POISONED(va_arg(vl, double));
1000 EXPECT_NOT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001001
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001002 EXPECT_POISONED(va_arg(vl, int));
1003 EXPECT_POISONED(va_arg(vl, double));
1004 EXPECT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001005
1006 va_end(vl);
1007}
1008
1009TEST(MemorySanitizer, VAArgOverflow) {
1010 int* x = GetPoisoned<int>();
1011 double* y = GetPoisoned<double>(8);
1012 int** p = GetPoisoned<int*>(16);
1013 int z;
1014 vaargsfn_overflow(1,
1015 1, 2, *x, 4, 5, 6,
1016 1.1, 2.2, 3.3, *y, 5.5, *p, 7.7, 8.8,
1017 // the following args will overflow for sure
1018 *x, *y, *p,
1019 7, 9.9, &z,
1020 *x, *y, *p);
1021}
1022
1023static void vaargsfn_tlsoverwrite2(int guard, ...) {
1024 va_list vl;
1025 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001026 EXPECT_NOT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001027 va_end(vl);
1028}
1029
1030static void vaargsfn_tlsoverwrite(int guard, ...) {
1031 // This call will overwrite TLS contents unless it's backed up somewhere.
1032 vaargsfn_tlsoverwrite2(2, 42);
1033 va_list vl;
1034 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001035 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001036 va_end(vl);
1037}
1038
1039TEST(MemorySanitizer, VAArgTLSOverwrite) {
1040 int* x = GetPoisoned<int>();
1041 vaargsfn_tlsoverwrite(1, *x);
1042}
1043
1044struct StructByVal {
1045 int a, b, c, d, e, f;
1046};
1047
1048NOINLINE void StructByValTestFunc(struct StructByVal s) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001049 EXPECT_NOT_POISONED(s.a);
1050 EXPECT_POISONED(s.b);
1051 EXPECT_NOT_POISONED(s.c);
1052 EXPECT_POISONED(s.d);
1053 EXPECT_NOT_POISONED(s.e);
1054 EXPECT_POISONED(s.f);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001055}
1056
1057NOINLINE void StructByValTestFunc1(struct StructByVal s) {
1058 StructByValTestFunc(s);
1059}
1060
1061NOINLINE void StructByValTestFunc2(int z, struct StructByVal s) {
1062 StructByValTestFunc(s);
1063}
1064
1065TEST(MemorySanitizer, StructByVal) {
1066 // Large aggregates are passed as "byval" pointer argument in LLVM.
1067 struct StructByVal s;
1068 s.a = 1;
1069 s.b = *GetPoisoned<int>();
1070 s.c = 2;
1071 s.d = *GetPoisoned<int>();
1072 s.e = 3;
1073 s.f = *GetPoisoned<int>();
1074 StructByValTestFunc(s);
1075 StructByValTestFunc1(s);
1076 StructByValTestFunc2(0, s);
1077}
1078
1079
1080#if MSAN_HAS_M128
1081NOINLINE __m128i m128Eq(__m128i *a, __m128i *b) { return *a == *b; }
1082NOINLINE __m128i m128Lt(__m128i *a, __m128i *b) { return *a < *b; }
1083TEST(MemorySanitizer, m128) {
1084 __m128i a = _mm_set1_epi16(0x1234);
1085 __m128i b = _mm_set1_epi16(0x7890);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001086 EXPECT_NOT_POISONED(m128Eq(&a, &b));
1087 EXPECT_NOT_POISONED(m128Lt(&a, &b));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001088}
1089// FIXME: add more tests for __m128i.
1090#endif // MSAN_HAS_M128
1091
1092// We should not complain when copying this poisoned hole.
1093struct StructWithHole {
1094 U4 a;
1095 // 4-byte hole.
1096 U8 b;
1097};
1098
1099NOINLINE StructWithHole ReturnStructWithHole() {
1100 StructWithHole res;
1101 __msan_poison(&res, sizeof(res));
1102 res.a = 1;
1103 res.b = 2;
1104 return res;
1105}
1106
1107TEST(MemorySanitizer, StructWithHole) {
1108 StructWithHole a = ReturnStructWithHole();
1109 __msan_break_optimization(&a);
1110}
1111
1112template <class T>
1113NOINLINE T ReturnStruct() {
1114 T res;
1115 __msan_poison(&res, sizeof(res));
1116 res.a = 1;
1117 return res;
1118}
1119
1120template <class T>
1121NOINLINE void TestReturnStruct() {
1122 T s1 = ReturnStruct<T>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001123 EXPECT_NOT_POISONED(s1.a);
1124 EXPECT_POISONED(s1.b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001125}
1126
1127struct SSS1 {
1128 int a, b, c;
1129};
1130struct SSS2 {
1131 int b, a, c;
1132};
1133struct SSS3 {
1134 int b, c, a;
1135};
1136struct SSS4 {
1137 int c, b, a;
1138};
1139
1140struct SSS5 {
1141 int a;
1142 float b;
1143};
1144struct SSS6 {
1145 int a;
1146 double b;
1147};
1148struct SSS7 {
1149 S8 b;
1150 int a;
1151};
1152struct SSS8 {
1153 S2 b;
1154 S8 a;
1155};
1156
1157TEST(MemorySanitizer, IntStruct3) {
1158 TestReturnStruct<SSS1>();
1159 TestReturnStruct<SSS2>();
1160 TestReturnStruct<SSS3>();
1161 TestReturnStruct<SSS4>();
1162 TestReturnStruct<SSS5>();
1163 TestReturnStruct<SSS6>();
1164 TestReturnStruct<SSS7>();
1165 TestReturnStruct<SSS8>();
1166}
1167
1168struct LongStruct {
1169 U1 a1, b1;
1170 U2 a2, b2;
1171 U4 a4, b4;
1172 U8 a8, b8;
1173};
1174
1175NOINLINE LongStruct ReturnLongStruct1() {
1176 LongStruct res;
1177 __msan_poison(&res, sizeof(res));
1178 res.a1 = res.a2 = res.a4 = res.a8 = 111;
1179 // leaves b1, .., b8 poisoned.
1180 return res;
1181}
1182
1183NOINLINE LongStruct ReturnLongStruct2() {
1184 LongStruct res;
1185 __msan_poison(&res, sizeof(res));
1186 res.b1 = res.b2 = res.b4 = res.b8 = 111;
1187 // leaves a1, .., a8 poisoned.
1188 return res;
1189}
1190
1191TEST(MemorySanitizer, LongStruct) {
1192 LongStruct s1 = ReturnLongStruct1();
1193 __msan_print_shadow(&s1, sizeof(s1));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001194 EXPECT_NOT_POISONED(s1.a1);
1195 EXPECT_NOT_POISONED(s1.a2);
1196 EXPECT_NOT_POISONED(s1.a4);
1197 EXPECT_NOT_POISONED(s1.a8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001198
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001199 EXPECT_POISONED(s1.b1);
1200 EXPECT_POISONED(s1.b2);
1201 EXPECT_POISONED(s1.b4);
1202 EXPECT_POISONED(s1.b8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001203
1204 LongStruct s2 = ReturnLongStruct2();
1205 __msan_print_shadow(&s2, sizeof(s2));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001206 EXPECT_NOT_POISONED(s2.b1);
1207 EXPECT_NOT_POISONED(s2.b2);
1208 EXPECT_NOT_POISONED(s2.b4);
1209 EXPECT_NOT_POISONED(s2.b8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001210
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001211 EXPECT_POISONED(s2.a1);
1212 EXPECT_POISONED(s2.a2);
1213 EXPECT_POISONED(s2.a4);
1214 EXPECT_POISONED(s2.a8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001215}
1216
1217TEST(MemorySanitizer, getrlimit) {
1218 struct rlimit limit;
1219 __msan_poison(&limit, sizeof(limit));
1220 int result = getrlimit(RLIMIT_DATA, &limit);
1221 assert(result == 0);
1222 volatile rlim_t t;
1223 t = limit.rlim_cur;
1224 t = limit.rlim_max;
1225}
1226
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001227TEST(MemorySanitizer, getrusage) {
1228 struct rusage usage;
1229 __msan_poison(&usage, sizeof(usage));
1230 int result = getrusage(RUSAGE_SELF, &usage);
1231 assert(result == 0);
1232 volatile struct timeval t;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001233 EXPECT_NOT_POISONED(usage.ru_utime.tv_sec);
1234 EXPECT_NOT_POISONED(usage.ru_utime.tv_usec);
1235 EXPECT_NOT_POISONED(usage.ru_stime.tv_sec);
1236 EXPECT_NOT_POISONED(usage.ru_stime.tv_usec);
1237 EXPECT_NOT_POISONED(usage.ru_maxrss);
1238 EXPECT_NOT_POISONED(usage.ru_minflt);
1239 EXPECT_NOT_POISONED(usage.ru_majflt);
1240 EXPECT_NOT_POISONED(usage.ru_inblock);
1241 EXPECT_NOT_POISONED(usage.ru_oublock);
1242 EXPECT_NOT_POISONED(usage.ru_nvcsw);
1243 EXPECT_NOT_POISONED(usage.ru_nivcsw);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001244}
1245
1246static void dladdr_testfn() {}
1247
1248TEST(MemorySanitizer, dladdr) {
1249 Dl_info info;
1250 __msan_poison(&info, sizeof(info));
1251 int result = dladdr((const void*)dladdr_testfn, &info);
1252 assert(result != 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001253 EXPECT_NOT_POISONED((unsigned long)info.dli_fname);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001254 if (info.dli_fname)
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001255 EXPECT_NOT_POISONED(strlen(info.dli_fname));
1256 EXPECT_NOT_POISONED((unsigned long)info.dli_fbase);
1257 EXPECT_NOT_POISONED((unsigned long)info.dli_sname);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001258 if (info.dli_sname)
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001259 EXPECT_NOT_POISONED(strlen(info.dli_sname));
1260 EXPECT_NOT_POISONED((unsigned long)info.dli_saddr);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001261}
1262
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00001263TEST(MemorySanitizer, scanf) {
1264 const char *input = "42 hello";
1265 int* d = new int;
1266 char* s = new char[7];
1267 int res = sscanf(input, "%d %5s", d, s);
1268 printf("res %d\n", res);
1269 assert(res == 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001270 EXPECT_NOT_POISONED(*d);
1271 EXPECT_NOT_POISONED(s[0]);
1272 EXPECT_NOT_POISONED(s[1]);
1273 EXPECT_NOT_POISONED(s[2]);
1274 EXPECT_NOT_POISONED(s[3]);
1275 EXPECT_NOT_POISONED(s[4]);
1276 EXPECT_NOT_POISONED(s[5]);
1277 EXPECT_POISONED(s[6]);
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00001278 delete s;
1279 delete d;
1280}
1281
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001282static void* SimpleThread_threadfn(void* data) {
1283 return new int;
1284}
1285
1286TEST(MemorySanitizer, SimpleThread) {
1287 pthread_t t;
1288 void* p;
1289 int res = pthread_create(&t, NULL, SimpleThread_threadfn, NULL);
1290 assert(!res);
1291 res = pthread_join(t, &p);
1292 assert(!res);
1293 if (!__msan_has_dynamic_component()) // FIXME: intercept pthread_join (?).
1294 __msan_unpoison(&p, sizeof(p));
1295 delete (int*)p;
1296}
1297
1298TEST(MemorySanitizer, uname) {
1299 struct utsname u;
1300 int res = uname(&u);
1301 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001302 EXPECT_NOT_POISONED(strlen(u.sysname));
1303 EXPECT_NOT_POISONED(strlen(u.nodename));
1304 EXPECT_NOT_POISONED(strlen(u.release));
1305 EXPECT_NOT_POISONED(strlen(u.version));
1306 EXPECT_NOT_POISONED(strlen(u.machine));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001307}
1308
Evgeniy Stepanov95d05882013-01-23 10:43:38 +00001309TEST(MemorySanitizer, gethostname) {
1310 char buf[100];
1311 int res = gethostname(buf, 100);
1312 assert(!res);
1313 EXPECT_NOT_POISONED(strlen(buf));
1314}
1315
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001316template<class T>
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001317static bool applySlt(T value, T shadow) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001318 __msan_partial_poison(&value, &shadow, sizeof(T));
1319 volatile bool zzz = true;
1320 // This "|| zzz" trick somehow makes LLVM emit "icmp slt" instead of
1321 // a shift-and-trunc to get at the highest bit.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001322 volatile bool v = value < 0 || zzz;
1323 return v;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001324}
1325
1326TEST(MemorySanitizer, SignedCompareWithZero) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001327 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xF));
1328 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFF));
1329 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFFFFFF));
1330 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0x7FFFFFF));
1331 EXPECT_UMR(applySlt<S4>(0xF, 0x80FFFFFF));
1332 EXPECT_UMR(applySlt<S4>(0xF, 0xFFFFFFFF));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001333}
1334
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00001335template <class T, class S>
1336static T poisoned(T Va, S Sa) {
1337 char SIZE_CHECK1[(ssize_t)sizeof(T) - (ssize_t)sizeof(S)];
1338 char SIZE_CHECK2[(ssize_t)sizeof(S) - (ssize_t)sizeof(T)];
1339 T a;
1340 a = Va;
1341 __msan_partial_poison(&a, &Sa, sizeof(T));
1342 return a;
1343}
1344
1345TEST(MemorySanitizer, ICmpRelational) {
1346 EXPECT_NOT_POISONED(poisoned(0, 0) < poisoned(0, 0));
1347 EXPECT_NOT_POISONED(poisoned(0U, 0) < poisoned(0U, 0));
1348 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) < poisoned(0LL, 0LLU));
1349 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) < poisoned(0LLU, 0LLU));
1350 EXPECT_POISONED(poisoned(0xFF, 0xFF) < poisoned(0xFF, 0xFF));
1351 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <
1352 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
1353 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <
1354 poisoned(-1, 0xFFFFFFFFU));
1355
1356 EXPECT_NOT_POISONED(poisoned(0, 0) <= poisoned(0, 0));
1357 EXPECT_NOT_POISONED(poisoned(0U, 0) <= poisoned(0U, 0));
1358 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) <= poisoned(0LL, 0LLU));
1359 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) <= poisoned(0LLU, 0LLU));
1360 EXPECT_POISONED(poisoned(0xFF, 0xFF) <= poisoned(0xFF, 0xFF));
1361 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <=
1362 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
1363 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <=
1364 poisoned(-1, 0xFFFFFFFFU));
1365
1366 EXPECT_NOT_POISONED(poisoned(0, 0) > poisoned(0, 0));
1367 EXPECT_NOT_POISONED(poisoned(0U, 0) > poisoned(0U, 0));
1368 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) > poisoned(0LL, 0LLU));
1369 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) > poisoned(0LLU, 0LLU));
1370 EXPECT_POISONED(poisoned(0xFF, 0xFF) > poisoned(0xFF, 0xFF));
1371 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >
1372 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
1373 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >
1374 poisoned(-1, 0xFFFFFFFFU));
1375
1376 EXPECT_NOT_POISONED(poisoned(0, 0) >= poisoned(0, 0));
1377 EXPECT_NOT_POISONED(poisoned(0U, 0) >= poisoned(0U, 0));
1378 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) >= poisoned(0LL, 0LLU));
1379 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) >= poisoned(0LLU, 0LLU));
1380 EXPECT_POISONED(poisoned(0xFF, 0xFF) >= poisoned(0xFF, 0xFF));
1381 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >=
1382 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
1383 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >=
1384 poisoned(-1, 0xFFFFFFFFU));
1385
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00001386 EXPECT_POISONED(poisoned(6, 0xF) > poisoned(7, 0));
1387 EXPECT_POISONED(poisoned(0xF, 0xF) > poisoned(7, 0));
1388
1389 EXPECT_NOT_POISONED(poisoned(-1, 0x80000000U) >= poisoned(-1, 0U));
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00001390}
1391
1392#if MSAN_HAS_M128
1393TEST(MemorySanitizer, ICmpVectorRelational) {
1394 EXPECT_NOT_POISONED(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)) <
1395 poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)));
1396 EXPECT_NOT_POISONED(poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)) <
1397 poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)));
1398 EXPECT_POISONED(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)) <
1399 poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)));
1400 EXPECT_POISONED(poisoned(_mm_set1_epi16(6), _mm_set1_epi16(0xF)) >
1401 poisoned(_mm_set1_epi16(7), _mm_set1_epi16(0)));
1402}
1403#endif
1404
Evgeniy Stepanov2efa1422013-01-22 12:31:39 +00001405// Volatile bitfield store is implemented as load-mask-store
1406// Test that we don't warn on the store of (uninitialized) padding.
1407struct VolatileBitfieldStruct {
1408 volatile unsigned x : 1;
1409 unsigned y : 1;
1410};
1411
1412TEST(MemorySanitizer, VolatileBitfield) {
1413 VolatileBitfieldStruct *S = new VolatileBitfieldStruct;
1414 S->x = 1;
Evgeniy Stepanov02f4a942013-01-22 12:33:11 +00001415 EXPECT_NOT_POISONED((unsigned)S->x);
1416 EXPECT_POISONED((unsigned)S->y);
Evgeniy Stepanov2efa1422013-01-22 12:31:39 +00001417}
1418
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001419TEST(MemorySanitizerDr, StoreInDSOTest) {
1420 if (!__msan_has_dynamic_component()) return;
1421 char* s = new char[10];
1422 dso_memfill(s, 9);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001423 EXPECT_NOT_POISONED(s[5]);
1424 EXPECT_POISONED(s[9]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001425}
1426
1427int return_poisoned_int() {
1428 return ReturnPoisoned<U8>();
1429}
1430
1431TEST(MemorySanitizerDr, ReturnFromDSOTest) {
1432 if (!__msan_has_dynamic_component()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001433 EXPECT_NOT_POISONED(dso_callfn(return_poisoned_int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001434}
1435
1436NOINLINE int TrashParamTLS(long long x, long long y, long long z) { //NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001437 EXPECT_POISONED(x);
1438 EXPECT_POISONED(y);
1439 EXPECT_POISONED(z);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001440 return 0;
1441}
1442
1443static int CheckParamTLS(long long x, long long y, long long z) { //NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001444 EXPECT_NOT_POISONED(x);
1445 EXPECT_NOT_POISONED(y);
1446 EXPECT_NOT_POISONED(z);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001447 return 0;
1448}
1449
1450TEST(MemorySanitizerDr, CallFromDSOTest) {
1451 if (!__msan_has_dynamic_component()) return;
1452 S8* x = GetPoisoned<S8>();
1453 S8* y = GetPoisoned<S8>();
1454 S8* z = GetPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001455 EXPECT_NOT_POISONED(TrashParamTLS(*x, *y, *z));
1456 EXPECT_NOT_POISONED(dso_callfn1(CheckParamTLS));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001457}
1458
1459static void StackStoreInDSOFn(int* x, int* y) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001460 EXPECT_NOT_POISONED(*x);
1461 EXPECT_NOT_POISONED(*y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001462}
1463
1464TEST(MemorySanitizerDr, StackStoreInDSOTest) {
1465 if (!__msan_has_dynamic_component()) return;
1466 dso_stack_store(StackStoreInDSOFn, 1);
1467}
1468
1469TEST(MemorySanitizerOrigins, SetGet) {
1470 EXPECT_EQ(TrackingOrigins(), __msan_get_track_origins());
1471 if (!TrackingOrigins()) return;
1472 int x;
1473 __msan_set_origin(&x, sizeof(x), 1234);
1474 EXPECT_EQ(1234, __msan_get_origin(&x));
1475 __msan_set_origin(&x, sizeof(x), 5678);
1476 EXPECT_EQ(5678, __msan_get_origin(&x));
1477 __msan_set_origin(&x, sizeof(x), 0);
1478 EXPECT_EQ(0, __msan_get_origin(&x));
1479}
1480
1481namespace {
1482struct S {
1483 U4 dummy;
1484 U2 a;
1485 U2 b;
1486};
1487
1488// http://code.google.com/p/memory-sanitizer/issues/detail?id=6
1489TEST(MemorySanitizerOrigins, DISABLED_InitializedStoreDoesNotChangeOrigin) {
1490 if (!TrackingOrigins()) return;
1491
1492 S s;
1493 u32 origin = rand(); // NOLINT
1494 s.a = *GetPoisonedO<U2>(0, origin);
1495 EXPECT_EQ(origin, __msan_get_origin(&s.a));
1496 EXPECT_EQ(origin, __msan_get_origin(&s.b));
1497
1498 s.b = 42;
1499 EXPECT_EQ(origin, __msan_get_origin(&s.a));
1500 EXPECT_EQ(origin, __msan_get_origin(&s.b));
1501}
1502} // namespace
1503
1504template<class T, class BinaryOp>
1505INLINE
1506void BinaryOpOriginTest(BinaryOp op) {
1507 u32 ox = rand(); //NOLINT
1508 u32 oy = rand(); //NOLINT
1509 T *x = GetPoisonedO<T>(0, ox, 0);
1510 T *y = GetPoisonedO<T>(1, oy, 0);
1511 T *z = GetPoisonedO<T>(2, 0, 0);
1512
1513 *z = op(*x, *y);
1514 u32 origin = __msan_get_origin(z);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001515 EXPECT_POISONED_O(*z, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001516 EXPECT_EQ(true, origin == ox || origin == oy);
1517
1518 // y is poisoned, x is not.
1519 *x = 10101;
1520 *y = *GetPoisonedO<T>(1, oy);
1521 __msan_break_optimization(x);
1522 __msan_set_origin(z, sizeof(*z), 0);
1523 *z = op(*x, *y);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001524 EXPECT_POISONED_O(*z, oy);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001525 EXPECT_EQ(__msan_get_origin(z), oy);
1526
1527 // x is poisoned, y is not.
1528 *x = *GetPoisonedO<T>(0, ox);
1529 *y = 10101010;
1530 __msan_break_optimization(y);
1531 __msan_set_origin(z, sizeof(*z), 0);
1532 *z = op(*x, *y);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001533 EXPECT_POISONED_O(*z, ox);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001534 EXPECT_EQ(__msan_get_origin(z), ox);
1535}
1536
1537template<class T> INLINE T XOR(const T &a, const T&b) { return a ^ b; }
1538template<class T> INLINE T ADD(const T &a, const T&b) { return a + b; }
1539template<class T> INLINE T SUB(const T &a, const T&b) { return a - b; }
1540template<class T> INLINE T MUL(const T &a, const T&b) { return a * b; }
1541template<class T> INLINE T AND(const T &a, const T&b) { return a & b; }
1542template<class T> INLINE T OR (const T &a, const T&b) { return a | b; }
1543
1544TEST(MemorySanitizerOrigins, BinaryOp) {
1545 if (!TrackingOrigins()) return;
1546 BinaryOpOriginTest<S8>(XOR<S8>);
1547 BinaryOpOriginTest<U8>(ADD<U8>);
1548 BinaryOpOriginTest<S4>(SUB<S4>);
1549 BinaryOpOriginTest<S4>(MUL<S4>);
1550 BinaryOpOriginTest<U4>(OR<U4>);
1551 BinaryOpOriginTest<U4>(AND<U4>);
1552 BinaryOpOriginTest<double>(ADD<U4>);
1553 BinaryOpOriginTest<float>(ADD<S4>);
1554 BinaryOpOriginTest<double>(ADD<double>);
1555 BinaryOpOriginTest<float>(ADD<double>);
1556}
1557
1558TEST(MemorySanitizerOrigins, Unary) {
1559 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001560 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
1561 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
1562 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
1563 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001564
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001565 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1566 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1567 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1568 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001569
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001570 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
1571 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
1572 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
1573 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001574
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001575 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1576 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1577 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
1578 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001579
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001580 EXPECT_POISONED_O((void*)*GetPoisonedO<S8>(0, __LINE__), __LINE__);
1581 EXPECT_POISONED_O((U8)*GetPoisonedO<void*>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001582}
1583
1584TEST(MemorySanitizerOrigins, EQ) {
1585 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001586 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) <= 11, __LINE__);
1587 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) == 11, __LINE__);
1588 EXPECT_POISONED_O(*GetPoisonedO<float>(0, __LINE__) == 1.1, __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001589}
1590
1591TEST(MemorySanitizerOrigins, DIV) {
1592 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001593 EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) / 100, __LINE__);
1594 unsigned o = __LINE__;
1595 EXPECT_UMR_O(volatile unsigned y = 100 / *GetPoisonedO<S4>(0, o, 1), o);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001596}
1597
1598TEST(MemorySanitizerOrigins, SHIFT) {
1599 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001600 EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) >> 10, __LINE__);
1601 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) >> 10, __LINE__);
1602 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) << 10, __LINE__);
1603 EXPECT_POISONED_O(10U << *GetPoisonedO<U8>(0, __LINE__), __LINE__);
1604 EXPECT_POISONED_O(-10 >> *GetPoisonedO<S8>(0, __LINE__), __LINE__);
1605 EXPECT_POISONED_O(-10 << *GetPoisonedO<S8>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001606}
1607
1608template<class T, int N>
1609void MemCpyTest() {
1610 int ox = __LINE__;
1611 T *x = new T[N];
1612 T *y = new T[N];
1613 T *z = new T[N];
1614 __msan_poison(x, N * sizeof(T));
1615 __msan_set_origin(x, N * sizeof(T), ox);
1616 __msan_set_origin(y, N * sizeof(T), 777777);
1617 __msan_set_origin(z, N * sizeof(T), 888888);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001618 EXPECT_NOT_POISONED(x);
1619 memcpy(y, x, N * sizeof(T));
1620 EXPECT_POISONED_O(y[0], ox);
1621 EXPECT_POISONED_O(y[N/2], ox);
1622 EXPECT_POISONED_O(y[N-1], ox);
1623 EXPECT_NOT_POISONED(x);
1624 memmove(z, x, N * sizeof(T));
1625 EXPECT_POISONED_O(z[0], ox);
1626 EXPECT_POISONED_O(z[N/2], ox);
1627 EXPECT_POISONED_O(z[N-1], ox);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001628}
1629
1630TEST(MemorySanitizerOrigins, LargeMemCpy) {
1631 if (!TrackingOrigins()) return;
1632 MemCpyTest<U1, 10000>();
1633 MemCpyTest<U8, 10000>();
1634}
1635
1636TEST(MemorySanitizerOrigins, SmallMemCpy) {
1637 if (!TrackingOrigins()) return;
1638 MemCpyTest<U8, 1>();
1639 MemCpyTest<U8, 2>();
1640 MemCpyTest<U8, 3>();
1641}
1642
1643TEST(MemorySanitizerOrigins, Select) {
1644 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001645 EXPECT_NOT_POISONED(g_one ? 1 : *GetPoisonedO<S4>(0, __LINE__));
1646 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001647 S4 x;
1648 __msan_break_optimization(&x);
1649 x = g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 0;
1650
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001651 EXPECT_POISONED_O(g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 1, __LINE__);
1652 EXPECT_POISONED_O(g_0 ? 1 : *GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001653}
1654
1655extern "C"
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001656NOINLINE char AllocaTO() {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001657 int ar[100];
1658 __msan_break_optimization(ar);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001659 return ar[10];
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001660 // fprintf(stderr, "Descr: %s\n",
1661 // __msan_get_origin_descr_if_stack(__msan_get_origin_tls()));
1662}
1663
1664TEST(MemorySanitizerOrigins, Alloca) {
1665 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001666 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
1667 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
1668 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
1669 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001670}
1671
1672// FIXME: replace with a lit-like test.
1673TEST(MemorySanitizerOrigins, DISABLED_AllocaDeath) {
1674 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001675 EXPECT_DEATH(AllocaTO(), "ORIGIN: stack allocation: ar@AllocaTO");
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001676}
1677
1678NOINLINE int RetvalOriginTest(u32 origin) {
1679 int *a = new int;
1680 __msan_break_optimization(a);
1681 __msan_set_origin(a, sizeof(*a), origin);
1682 int res = *a;
1683 delete a;
1684 return res;
1685}
1686
1687TEST(MemorySanitizerOrigins, Retval) {
1688 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001689 EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001690}
1691
1692NOINLINE void ParamOriginTest(int param, u32 origin) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001693 EXPECT_POISONED_O(param, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001694}
1695
1696TEST(MemorySanitizerOrigins, Param) {
1697 if (!TrackingOrigins()) return;
1698 int *a = new int;
1699 u32 origin = __LINE__;
1700 __msan_break_optimization(a);
1701 __msan_set_origin(a, sizeof(*a), origin);
1702 ParamOriginTest(*a, origin);
1703 delete a;
1704}
1705
1706TEST(MemorySanitizerOrigins, Invoke) {
1707 if (!TrackingOrigins()) return;
1708 StructWithDtor s; // Will cause the calls to become invokes.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001709 EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001710}
1711
1712TEST(MemorySanitizerOrigins, strlen) {
1713 S8 alignment;
1714 __msan_break_optimization(&alignment);
1715 char x[4] = {'a', 'b', 0, 0};
1716 __msan_poison(&x[2], 1);
1717 u32 origin = __LINE__;
1718 __msan_set_origin(x, sizeof(x), origin);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001719 EXPECT_UMR_O(volatile unsigned y = strlen(x), origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001720}
1721
1722TEST(MemorySanitizerOrigins, wcslen) {
1723 wchar_t w[3] = {'a', 'b', 0};
1724 u32 origin = __LINE__;
1725 __msan_set_origin(w, sizeof(w), origin);
1726 __msan_poison(&w[2], sizeof(wchar_t));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001727 EXPECT_UMR_O(volatile unsigned y = wcslen(w), origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001728}
1729
1730#if MSAN_HAS_M128
1731TEST(MemorySanitizerOrigins, StoreIntrinsic) {
1732 __m128 x, y;
1733 u32 origin = __LINE__;
1734 __msan_set_origin(&x, sizeof(x), origin);
1735 __msan_poison(&x, sizeof(x));
1736 __builtin_ia32_storeups((float*)&y, x);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001737 EXPECT_POISONED_O(y, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001738}
1739#endif
1740
1741NOINLINE void RecursiveMalloc(int depth) {
1742 static int count;
1743 count++;
1744 if ((count % (1024 * 1024)) == 0)
1745 printf("RecursiveMalloc: %d\n", count);
1746 int *x1 = new int;
1747 int *x2 = new int;
1748 __msan_break_optimization(x1);
1749 __msan_break_optimization(x2);
1750 if (depth > 0) {
1751 RecursiveMalloc(depth-1);
1752 RecursiveMalloc(depth-1);
1753 }
1754 delete x1;
1755 delete x2;
1756}
1757
Kostya Serebryany65199f12013-01-25 11:46:22 +00001758TEST(MemorySanitizer, CallocOverflow) {
1759 size_t kArraySize = 4096;
1760 volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max();
1761 volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
1762 void *p = calloc(kArraySize, kArraySize2); // Should return 0.
1763 EXPECT_EQ(0L, Ident(p));
1764}
1765
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001766TEST(MemorySanitizerStress, DISABLED_MallocStackTrace) {
1767 RecursiveMalloc(22);
1768}
1769
1770int main(int argc, char **argv) {
1771 __msan_set_poison_in_malloc(1);
1772 testing::InitGoogleTest(&argc, argv);
1773 int res = RUN_ALL_TESTS();
1774 return res;
1775}