blob: 3adf70940d688cc3771215fa5e780a42171eb7ca [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()) \
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000077 EXPECT_EQ(origin, __msan_get_umr_origin()); \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000078 } 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); \
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000085 u32 id = __msan_get_umr_origin(); \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000086 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
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000170// Check that malloc poisons memory.
171// A lot of tests below depend on this.
172TEST(MemorySanitizerSanity, PoisonInMalloc) {
173 int *x = (int*)malloc(sizeof(int));
174 EXPECT_POISONED(*x);
175 free(x);
176}
177
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000178TEST(MemorySanitizer, NegativeTest1) {
179 S4 *x = GetPoisoned<S4>();
180 if (g_one)
181 *x = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000182 EXPECT_NOT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000183}
184
185TEST(MemorySanitizer, PositiveTest1) {
186 // Load to store.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000187 EXPECT_POISONED(*GetPoisoned<S1>());
188 EXPECT_POISONED(*GetPoisoned<S2>());
189 EXPECT_POISONED(*GetPoisoned<S4>());
190 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000191
192 // S->S conversions.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000193 EXPECT_POISONED(*GetPoisoned<S1>());
194 EXPECT_POISONED(*GetPoisoned<S1>());
195 EXPECT_POISONED(*GetPoisoned<S1>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000196
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000197 EXPECT_POISONED(*GetPoisoned<S2>());
198 EXPECT_POISONED(*GetPoisoned<S2>());
199 EXPECT_POISONED(*GetPoisoned<S2>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000200
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000201 EXPECT_POISONED(*GetPoisoned<S4>());
202 EXPECT_POISONED(*GetPoisoned<S4>());
203 EXPECT_POISONED(*GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000204
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000205 EXPECT_POISONED(*GetPoisoned<S8>());
206 EXPECT_POISONED(*GetPoisoned<S8>());
207 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000208
209 // ZExt
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000210 EXPECT_POISONED(*GetPoisoned<U1>());
211 EXPECT_POISONED(*GetPoisoned<U1>());
212 EXPECT_POISONED(*GetPoisoned<U1>());
213 EXPECT_POISONED(*GetPoisoned<U2>());
214 EXPECT_POISONED(*GetPoisoned<U2>());
215 EXPECT_POISONED(*GetPoisoned<U4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000216
217 // Unary ops.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000218 EXPECT_POISONED(- *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000219
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000220 EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000221
222
223 a_s4[g_zero] = 1 - *GetPoisoned<S4>();
224 a_s4[g_zero] = 1 + *GetPoisoned<S4>();
225}
226
227TEST(MemorySanitizer, Phi1) {
228 S4 c;
229 if (g_one) {
230 c = *GetPoisoned<S4>();
231 } else {
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000232 break_optimization(0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000233 c = 0;
234 }
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000235 EXPECT_POISONED(c);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000236}
237
238TEST(MemorySanitizer, Phi2) {
239 S4 i = *GetPoisoned<S4>();
240 S4 n = g_one;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000241 EXPECT_UMR(for (; i < g_one; i++););
242 EXPECT_POISONED(i);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000243}
244
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000245NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); }
246NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); }
247NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); }
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000248
249TEST(MemorySanitizer, ArgTest) {
250 Arg1ExpectUMR(*GetPoisoned<S4>());
251 Arg2ExpectUMR(0, *GetPoisoned<S4>());
252 Arg3ExpectUMR(0, 1, *GetPoisoned<S8>());
253}
254
255
256TEST(MemorySanitizer, CallAndRet) {
257 if (!__msan_has_dynamic_component()) return;
258 ReturnPoisoned<S1>();
259 ReturnPoisoned<S2>();
260 ReturnPoisoned<S4>();
261 ReturnPoisoned<S8>();
262
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000263 EXPECT_POISONED(ReturnPoisoned<S1>());
264 EXPECT_POISONED(ReturnPoisoned<S2>());
265 EXPECT_POISONED(ReturnPoisoned<S4>());
266 EXPECT_POISONED(ReturnPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000267}
268
269// malloc() in the following test may be optimized to produce a compile-time
270// undef value. Check that we trap on the volatile assignment anyway.
271TEST(MemorySanitizer, DISABLED_MallocNoIdent) {
272 S4 *x = (int*)malloc(sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000273 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000274 free(x);
275}
276
277TEST(MemorySanitizer, Malloc) {
278 S4 *x = (int*)Ident(malloc(sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000279 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000280 free(x);
281}
282
283TEST(MemorySanitizer, Realloc) {
284 S4 *x = (int*)Ident(realloc(0, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000285 EXPECT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000286 x[0] = 1;
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_NOT_POISONED(x[0]); // Ok, was inited before.
292 EXPECT_POISONED(x[2]);
293 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000294 x[2] = 1; // Init this here. Check that after realloc it is poisoned again.
295 x = (int*)Ident(realloc(x, 2 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000296 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
297 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000298 x = (int*)Ident(realloc(x, 3 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000299 EXPECT_POISONED(x[1]);
300 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000301 free(x);
302}
303
304TEST(MemorySanitizer, Calloc) {
305 S4 *x = (int*)Ident(calloc(1, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000306 EXPECT_NOT_POISONED(*x); // Should not be poisoned.
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000307 // EXPECT_EQ(0, *x);
308 free(x);
309}
310
311TEST(MemorySanitizer, AndOr) {
312 U4 *p = GetPoisoned<U4>();
313 // We poison two bytes in the midle of a 4-byte word to make the test
314 // correct regardless of endianness.
315 ((U1*)p)[1] = 0;
316 ((U1*)p)[2] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000317 EXPECT_NOT_POISONED(*p & 0x00ffff00);
318 EXPECT_NOT_POISONED(*p & 0x00ff0000);
319 EXPECT_NOT_POISONED(*p & 0x0000ff00);
320 EXPECT_POISONED(*p & 0xff000000);
321 EXPECT_POISONED(*p & 0x000000ff);
322 EXPECT_POISONED(*p & 0x0000ffff);
323 EXPECT_POISONED(*p & 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000324
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000325 EXPECT_NOT_POISONED(*p | 0xff0000ff);
326 EXPECT_NOT_POISONED(*p | 0xff00ffff);
327 EXPECT_NOT_POISONED(*p | 0xffff00ff);
328 EXPECT_POISONED(*p | 0xff000000);
329 EXPECT_POISONED(*p | 0x000000ff);
330 EXPECT_POISONED(*p | 0x0000ffff);
331 EXPECT_POISONED(*p | 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000332
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000333 EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000334}
335
336template<class T>
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000337static bool applyNot(T value, T shadow) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000338 __msan_partial_poison(&value, &shadow, sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000339 return !value;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000340}
341
342TEST(MemorySanitizer, Not) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000343 EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0));
344 EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0));
345 EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF));
346 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF));
347 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF));
348 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF));
349 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000));
350 EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000));
351 EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000));
352 EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000353
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000354 EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0));
355 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE));
356 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0));
357 EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000358
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000359 EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1)));
360 EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2)));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000361}
362
363TEST(MemorySanitizer, Shift) {
364 U4 *up = GetPoisoned<U4>();
365 ((U1*)up)[0] = 0;
366 ((U1*)up)[3] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000367 EXPECT_NOT_POISONED(*up >> 30);
368 EXPECT_NOT_POISONED(*up >> 24);
369 EXPECT_POISONED(*up >> 23);
370 EXPECT_POISONED(*up >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000371
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000372 EXPECT_NOT_POISONED(*up << 30);
373 EXPECT_NOT_POISONED(*up << 24);
374 EXPECT_POISONED(*up << 23);
375 EXPECT_POISONED(*up << 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000376
377 S4 *sp = (S4*)up;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000378 EXPECT_NOT_POISONED(*sp >> 30);
379 EXPECT_NOT_POISONED(*sp >> 24);
380 EXPECT_POISONED(*sp >> 23);
381 EXPECT_POISONED(*sp >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000382
383 sp = GetPoisoned<S4>();
384 ((S1*)sp)[1] = 0;
385 ((S1*)sp)[2] = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000386 EXPECT_POISONED(*sp >> 31);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000387
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000388 EXPECT_POISONED(100 >> *GetPoisoned<S4>());
389 EXPECT_POISONED(100U >> *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000390}
391
392NOINLINE static int GetPoisonedZero() {
393 int *zero = new int;
394 *zero = 0;
395 __msan_poison(zero, sizeof(*zero));
396 int res = *zero;
397 delete zero;
398 return res;
399}
400
401TEST(MemorySanitizer, LoadFromDirtyAddress) {
402 int *a = new int;
403 *a = 0;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000404 EXPECT_UMR(break_optimization((void*)(U8)a[GetPoisonedZero()]));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000405 delete a;
406}
407
408TEST(MemorySanitizer, StoreToDirtyAddress) {
409 int *a = new int;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000410 EXPECT_UMR(a[GetPoisonedZero()] = 0);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000411 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000412 delete a;
413}
414
415
416NOINLINE void StackTestFunc() {
417 S4 p4;
418 S4 ok4 = 1;
419 S2 p2;
420 S2 ok2 = 1;
421 S1 p1;
422 S1 ok1 = 1;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000423 break_optimization(&p4);
424 break_optimization(&ok4);
425 break_optimization(&p2);
426 break_optimization(&ok2);
427 break_optimization(&p1);
428 break_optimization(&ok1);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000429
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000430 EXPECT_POISONED(p4);
431 EXPECT_POISONED(p2);
432 EXPECT_POISONED(p1);
433 EXPECT_NOT_POISONED(ok1);
434 EXPECT_NOT_POISONED(ok2);
435 EXPECT_NOT_POISONED(ok4);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000436}
437
438TEST(MemorySanitizer, StackTest) {
439 StackTestFunc();
440}
441
442NOINLINE void StackStressFunc() {
443 int foo[10000];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000444 break_optimization(foo);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000445}
446
447TEST(MemorySanitizer, DISABLED_StackStressTest) {
448 for (int i = 0; i < 1000000; i++)
449 StackStressFunc();
450}
451
452template<class T>
453void TestFloatingPoint() {
454 static volatile T v;
455 static T g[100];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000456 break_optimization(&g);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000457 T *x = GetPoisoned<T>();
458 T *y = GetPoisoned<T>(1);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000459 EXPECT_POISONED(*x);
460 EXPECT_POISONED((long long)*x);
461 EXPECT_POISONED((int)*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000462 g[0] = *x;
463 g[1] = *x + *y;
464 g[2] = *x - *y;
465 g[3] = *x * *y;
466}
467
468TEST(MemorySanitizer, FloatingPointTest) {
469 TestFloatingPoint<float>();
470 TestFloatingPoint<double>();
471}
472
473TEST(MemorySanitizer, DynMem) {
474 S4 x = 0;
475 S4 *y = GetPoisoned<S4>();
476 memcpy(y, &x, g_one * sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000477 EXPECT_NOT_POISONED(*y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000478}
479
480static char *DynRetTestStr;
481
482TEST(MemorySanitizer, DynRet) {
483 if (!__msan_has_dynamic_component()) return;
484 ReturnPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000485 EXPECT_NOT_POISONED(clearenv());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000486}
487
488
489TEST(MemorySanitizer, DynRet1) {
490 if (!__msan_has_dynamic_component()) return;
491 ReturnPoisoned<S8>();
492}
493
494struct LargeStruct {
495 S4 x[10];
496};
497
498NOINLINE
499LargeStruct LargeRetTest() {
500 LargeStruct res;
501 res.x[0] = *GetPoisoned<S4>();
502 res.x[1] = *GetPoisoned<S4>();
503 res.x[2] = *GetPoisoned<S4>();
504 res.x[3] = *GetPoisoned<S4>();
505 res.x[4] = *GetPoisoned<S4>();
506 res.x[5] = *GetPoisoned<S4>();
507 res.x[6] = *GetPoisoned<S4>();
508 res.x[7] = *GetPoisoned<S4>();
509 res.x[8] = *GetPoisoned<S4>();
510 res.x[9] = *GetPoisoned<S4>();
511 return res;
512}
513
514TEST(MemorySanitizer, LargeRet) {
515 LargeStruct a = LargeRetTest();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000516 EXPECT_POISONED(a.x[0]);
517 EXPECT_POISONED(a.x[9]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000518}
519
520TEST(MemorySanitizer, fread) {
521 char *x = new char[32];
522 FILE *f = fopen("/proc/self/stat", "r");
523 assert(f);
524 fread(x, 1, 32, f);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000525 EXPECT_NOT_POISONED(x[0]);
526 EXPECT_NOT_POISONED(x[16]);
527 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000528 fclose(f);
529 delete x;
530}
531
532TEST(MemorySanitizer, read) {
533 char *x = new char[32];
534 int fd = open("/proc/self/stat", O_RDONLY);
535 assert(fd > 0);
536 int sz = read(fd, x, 32);
537 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000538 EXPECT_NOT_POISONED(x[0]);
539 EXPECT_NOT_POISONED(x[16]);
540 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000541 close(fd);
542 delete x;
543}
544
545TEST(MemorySanitizer, pread) {
546 char *x = new char[32];
547 int fd = open("/proc/self/stat", O_RDONLY);
548 assert(fd > 0);
549 int sz = pread(fd, x, 32, 0);
550 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000551 EXPECT_NOT_POISONED(x[0]);
552 EXPECT_NOT_POISONED(x[16]);
553 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000554 close(fd);
555 delete x;
556}
557
558// FIXME: fails now.
559TEST(MemorySanitizer, DISABLED_ioctl) {
560 struct winsize ws;
561 EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000562 EXPECT_NOT_POISONED(ws.ws_col);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000563}
564
565TEST(MemorySanitizer, readlink) {
566 char *x = new char[1000];
567 readlink("/proc/self/exe", x, 1000);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000568 EXPECT_NOT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000569 delete [] x;
570}
571
572
573TEST(MemorySanitizer, stat) {
574 struct stat* st = new struct stat;
575 int res = stat("/proc/self/stat", st);
576 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000577 EXPECT_NOT_POISONED(st->st_dev);
578 EXPECT_NOT_POISONED(st->st_mode);
579 EXPECT_NOT_POISONED(st->st_size);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000580}
581
582TEST(MemorySanitizer, statfs) {
583 struct statfs* st = new struct statfs;
584 int res = statfs("/", st);
585 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000586 EXPECT_NOT_POISONED(st->f_type);
587 EXPECT_NOT_POISONED(st->f_bfree);
588 EXPECT_NOT_POISONED(st->f_namelen);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000589}
590
591TEST(MemorySanitizer, pipe) {
592 int* pipefd = new int[2];
593 int res = pipe(pipefd);
594 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000595 EXPECT_NOT_POISONED(pipefd[0]);
596 EXPECT_NOT_POISONED(pipefd[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000597 close(pipefd[0]);
598 close(pipefd[1]);
599}
600
601TEST(MemorySanitizer, getcwd) {
602 char path[PATH_MAX + 1];
603 char* res = getcwd(path, sizeof(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, realpath) {
609 const char* relpath = ".";
610 char path[PATH_MAX + 1];
611 char* res = realpath(relpath, path);
612 assert(res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000613 EXPECT_NOT_POISONED(path[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000614}
615
616TEST(MemorySanitizer, memcpy) {
617 char* x = new char[2];
618 char* y = new char[2];
619 x[0] = 1;
620 x[1] = *GetPoisoned<char>();
621 memcpy(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000622 EXPECT_NOT_POISONED(y[0]);
623 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000624}
625
626TEST(MemorySanitizer, memmove) {
627 char* x = new char[2];
628 char* y = new char[2];
629 x[0] = 1;
630 x[1] = *GetPoisoned<char>();
631 memmove(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000632 EXPECT_NOT_POISONED(y[0]);
633 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000634}
635
636TEST(MemorySanitizer, strdup) {
637 char *x = strdup("zzz");
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000638 EXPECT_NOT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000639 free(x);
640}
641
642template<class T, int size>
643void TestOverlapMemmove() {
644 T *x = new T[size];
645 assert(size >= 3);
646 x[2] = 0;
647 memmove(x, x + 1, (size - 1) * sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000648 EXPECT_NOT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000649 if (!__msan_has_dynamic_component()) {
650 // FIXME: under DR we will lose this information
651 // because accesses in memmove will unpoisin the shadow.
652 // We need to use our own memove implementation instead of libc's.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000653 EXPECT_POISONED(x[0]);
654 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000655 }
656 delete [] x;
657}
658
659TEST(MemorySanitizer, overlap_memmove) {
660 TestOverlapMemmove<U1, 10>();
661 TestOverlapMemmove<U1, 1000>();
662 TestOverlapMemmove<U8, 4>();
663 TestOverlapMemmove<U8, 1000>();
664}
665
666TEST(MemorySanitizer, strcpy) { // NOLINT
667 char* x = new char[3];
668 char* y = new char[3];
669 x[0] = 'a';
670 x[1] = *GetPoisoned<char>(1, 1);
671 x[2] = 0;
672 strcpy(y, x); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000673 EXPECT_NOT_POISONED(y[0]);
674 EXPECT_POISONED(y[1]);
675 EXPECT_NOT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000676}
677
678TEST(MemorySanitizer, strncpy) { // NOLINT
679 char* x = new char[3];
680 char* y = new char[3];
681 x[0] = 'a';
682 x[1] = *GetPoisoned<char>(1, 1);
683 x[2] = 0;
684 strncpy(y, x, 2); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000685 EXPECT_NOT_POISONED(y[0]);
686 EXPECT_POISONED(y[1]);
687 EXPECT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000688}
689
690TEST(MemorySanitizer, strtol) {
691 char *e;
692 assert(1 == strtol("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000693 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000694}
695
696TEST(MemorySanitizer, strtoll) {
697 char *e;
698 assert(1 == strtoll("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000699 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000700}
701
702TEST(MemorySanitizer, strtoul) {
703 char *e;
704 assert(1 == strtoul("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000705 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000706}
707
708TEST(MemorySanitizer, strtoull) {
709 char *e;
710 assert(1 == strtoull("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000711 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000712}
713
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000714TEST(MemorySanitizer, strtod) {
715 char *e;
716 assert(0 != strtod("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000717 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000718}
719
720TEST(MemorySanitizer, strtof) {
721 char *e;
722 assert(0 != strtof("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000723 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000724}
725
726TEST(MemorySanitizer, strtold) {
727 char *e;
728 assert(0 != strtold("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000729 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000730}
731
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000732TEST(MemorySanitizer, sprintf) { // NOLINT
733 char buff[10];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000734 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000735 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000736 int res = sprintf(buff, "%d", 1234567); // NOLINT
737 assert(res == 7);
738 assert(buff[0] == '1');
739 assert(buff[1] == '2');
740 assert(buff[2] == '3');
741 assert(buff[6] == '7');
742 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000743 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000744}
745
746TEST(MemorySanitizer, snprintf) {
747 char buff[10];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000748 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000749 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000750 int res = snprintf(buff, sizeof(buff), "%d", 1234567);
751 assert(res == 7);
752 assert(buff[0] == '1');
753 assert(buff[1] == '2');
754 assert(buff[2] == '3');
755 assert(buff[6] == '7');
756 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000757 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000758}
759
760TEST(MemorySanitizer, swprintf) {
761 wchar_t buff[10];
762 assert(sizeof(wchar_t) == 4);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000763 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000764 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000765 int res = swprintf(buff, 9, L"%d", 1234567);
766 assert(res == 7);
767 assert(buff[0] == '1');
768 assert(buff[1] == '2');
769 assert(buff[2] == '3');
770 assert(buff[6] == '7');
771 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000772 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000773}
774
775TEST(MemorySanitizer, wcstombs) {
776 const wchar_t *x = L"abc";
777 char buff[10];
778 int res = wcstombs(buff, x, 4);
779 EXPECT_EQ(res, 3);
780 EXPECT_EQ(buff[0], 'a');
781 EXPECT_EQ(buff[1], 'b');
782 EXPECT_EQ(buff[2], 'c');
783}
784
785TEST(MemorySanitizer, gettimeofday) {
786 struct timeval tv;
787 struct timezone tz;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000788 break_optimization(&tv);
789 break_optimization(&tz);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000790 assert(sizeof(tv) == 16);
791 assert(sizeof(tz) == 8);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000792 EXPECT_POISONED(tv.tv_sec);
793 EXPECT_POISONED(tv.tv_usec);
794 EXPECT_POISONED(tz.tz_minuteswest);
795 EXPECT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000796 assert(0 == gettimeofday(&tv, &tz));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000797 EXPECT_NOT_POISONED(tv.tv_sec);
798 EXPECT_NOT_POISONED(tv.tv_usec);
799 EXPECT_NOT_POISONED(tz.tz_minuteswest);
800 EXPECT_NOT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000801}
802
803TEST(MemorySanitizer, mmap) {
804 const int size = 4096;
805 void *p1, *p2;
806 p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
807 __msan_poison(p1, size);
808 munmap(p1, size);
809 for (int i = 0; i < 1000; i++) {
810 p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
811 if (p2 == p1)
812 break;
813 else
814 munmap(p2, size);
815 }
816 if (p1 == p2) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000817 EXPECT_NOT_POISONED(*(char*)p2);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000818 munmap(p2, size);
819 }
820}
821
822// FIXME: enable and add ecvt.
823// FIXME: check why msandr does nt handle fcvt.
824TEST(MemorySanitizer, fcvt) {
825 int a, b;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000826 break_optimization(&a);
827 break_optimization(&b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000828 EXPECT_POISONED(a);
829 EXPECT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000830 char *str = fcvt(12345.6789, 10, &a, &b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000831 EXPECT_NOT_POISONED(a);
832 EXPECT_NOT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000833}
834
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000835struct StructWithDtor {
836 ~StructWithDtor();
837};
838
839NOINLINE StructWithDtor::~StructWithDtor() {
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000840 break_optimization(0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000841}
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();
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001109 break_optimization(&a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001110}
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);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001521 break_optimization(x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001522 __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;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001530 break_optimization(y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001531 __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;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001648 break_optimization(&x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001649 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];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001658 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;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001680 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001681 __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__;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001700 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001701 __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;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001714 break_optimization(&alignment);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001715 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;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001748 break_optimization(x1);
1749 break_optimization(x2);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001750 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) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001771 testing::InitGoogleTest(&argc, argv);
1772 int res = RUN_ALL_TESTS();
1773 return res;
1774}