blob: 1890db2b9918d2b26a12fb72a88cf482cbdcd85f [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
Evgeniy Stepanov6e5ff892013-03-22 09:01:26 +000015#ifndef MSAN_EXTERNAL_TEST_CONFIG
16#include "msan_test_config.h"
17#endif // MSAN_EXTERNAL_TEST_CONFIG
18
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000019#include "sanitizer/msan_interface.h"
20#include "msandr_test_so.h"
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000021
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +000022#include <inttypes.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000023#include <stdlib.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <assert.h>
27#include <wchar.h>
Evgeniy Stepanov7cbbb292013-03-14 11:34:39 +000028#include <math.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000029
Evgeniy Stepanov9530eb72013-04-23 14:05:15 +000030#include <arpa/inet.h>
Evgeniy Stepanove03345b2013-01-17 13:42:17 +000031#include <dlfcn.h>
Evgeniy Stepanov103a63e2013-04-23 12:01:20 +000032#include <grp.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000033#include <unistd.h>
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +000034#include <link.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000035#include <limits.h>
36#include <sys/time.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <sys/resource.h>
41#include <sys/ioctl.h>
Evgeniy Stepanov359d7fc2013-06-24 14:25:33 +000042#include <sys/sysinfo.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000043#include <sys/utsname.h>
44#include <sys/mman.h>
45#include <sys/vfs.h>
Evgeniy Stepanovd97a15a2013-03-14 12:49:23 +000046#include <dirent.h>
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +000047#include <pwd.h>
Evgeniy Stepanov134fe8a2013-04-08 13:45:12 +000048#include <sys/socket.h>
Evgeniy Stepanov0a2cc372013-05-23 11:10:23 +000049#include <netdb.h>
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000050
51#if defined(__i386__) || defined(__x86_64__)
52# include <emmintrin.h>
53# define MSAN_HAS_M128 1
54#else
55# define MSAN_HAS_M128 0
56#endif
57
58typedef unsigned char U1;
59typedef unsigned short U2; // NOLINT
60typedef unsigned int U4;
61typedef unsigned long long U8; // NOLINT
62typedef signed char S1;
63typedef signed short S2; // NOLINT
64typedef signed int S4;
65typedef signed long long S8; // NOLINT
66#define NOINLINE __attribute__((noinline))
67#define INLINE __attribute__((always_inline))
68
Evgeniy Stepanov11929002013-01-22 12:29:00 +000069static bool TrackingOrigins() {
70 S8 x;
71 __msan_set_origin(&x, sizeof(x), 0x1234);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000072 U4 origin = __msan_get_origin(&x);
Evgeniy Stepanov11929002013-01-22 12:29:00 +000073 __msan_set_origin(&x, sizeof(x), 0);
74 return origin == 0x1234;
75}
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000076
Evgeniy Stepanov11929002013-01-22 12:29:00 +000077#define EXPECT_UMR(action) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000078 do { \
79 __msan_set_expect_umr(1); \
80 action; \
81 __msan_set_expect_umr(0); \
82 } while (0)
83
Evgeniy Stepanov11929002013-01-22 12:29:00 +000084#define EXPECT_UMR_O(action, origin) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000085 do { \
86 __msan_set_expect_umr(1); \
87 action; \
88 __msan_set_expect_umr(0); \
89 if (TrackingOrigins()) \
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000090 EXPECT_EQ(origin, __msan_get_umr_origin()); \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000091 } while (0)
92
Evgeniy Stepanov11929002013-01-22 12:29:00 +000093#define EXPECT_UMR_S(action, stack_origin) \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000094 do { \
95 __msan_set_expect_umr(1); \
96 action; \
97 __msan_set_expect_umr(0); \
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000098 U4 id = __msan_get_umr_origin(); \
Evgeniy Stepanov0231c502012-12-25 12:39:56 +000099 const char *str = __msan_get_origin_descr_if_stack(id); \
100 if (!str || strcmp(str, stack_origin)) { \
101 fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s", \
102 id, stack_origin, str); \
103 EXPECT_EQ(1, 0); \
104 } \
105 } while (0)
106
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000107#define EXPECT_POISONED(x) ExpectPoisoned(x)
108
109template<typename T>
110void ExpectPoisoned(const T& t) {
111 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
112}
113
114#define EXPECT_POISONED_O(x, origin) \
115 ExpectPoisonedWithOrigin(x, origin)
116
117template<typename T>
118void ExpectPoisonedWithOrigin(const T& t, unsigned origin) {
119 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
120 if (TrackingOrigins())
121 EXPECT_EQ(origin, __msan_get_origin((void*)&t));
122}
123
124#define EXPECT_POISONED_S(x, stack_origin) \
125 ExpectPoisonedWithStackOrigin(x, stack_origin)
126
127template<typename T>
128void ExpectPoisonedWithStackOrigin(const T& t, const char *stack_origin) {
129 EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t)));
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000130 U4 id = __msan_get_origin((void*)&t);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000131 const char *str = __msan_get_origin_descr_if_stack(id);
132 if (!str || strcmp(str, stack_origin)) {
133 fprintf(stderr, "EXPECT_POISONED_S: id=%u %s, %s",
134 id, stack_origin, str);
135 EXPECT_EQ(1, 0);
136 }
137}
138
139#define EXPECT_NOT_POISONED(x) ExpectNotPoisoned(x)
140
141template<typename T>
142void ExpectNotPoisoned(const T& t) {
143 EXPECT_EQ(-1, __msan_test_shadow((void*)&t, sizeof(t)));
144}
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000145
146static U8 poisoned_array[100];
147template<class T>
148T *GetPoisoned(int i = 0, T val = 0) {
149 T *res = (T*)&poisoned_array[i];
150 *res = val;
151 __msan_poison(&poisoned_array[i], sizeof(T));
152 return res;
153}
154
155template<class T>
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000156T *GetPoisonedO(int i, U4 origin, T val = 0) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000157 T *res = (T*)&poisoned_array[i];
158 *res = val;
159 __msan_poison(&poisoned_array[i], sizeof(T));
160 __msan_set_origin(&poisoned_array[i], sizeof(T), origin);
161 return res;
162}
163
164// This function returns its parameter but in such a way that compiler
165// can not prove it.
166template<class T>
167NOINLINE
168static T Ident(T t) {
169 volatile T ret = t;
170 return ret;
171}
172
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000173template<class T> NOINLINE T ReturnPoisoned() { return *GetPoisoned<T>(); }
174
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000175static volatile int g_one = 1;
176static volatile int g_zero = 0;
177static volatile int g_0 = 0;
178static volatile int g_1 = 1;
179
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000180S4 a_s4[100];
181S8 a_s8[100];
182
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000183// Check that malloc poisons memory.
184// A lot of tests below depend on this.
185TEST(MemorySanitizerSanity, PoisonInMalloc) {
186 int *x = (int*)malloc(sizeof(int));
187 EXPECT_POISONED(*x);
188 free(x);
189}
190
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000191TEST(MemorySanitizer, NegativeTest1) {
192 S4 *x = GetPoisoned<S4>();
193 if (g_one)
194 *x = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000195 EXPECT_NOT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000196}
197
198TEST(MemorySanitizer, PositiveTest1) {
199 // Load to store.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000200 EXPECT_POISONED(*GetPoisoned<S1>());
201 EXPECT_POISONED(*GetPoisoned<S2>());
202 EXPECT_POISONED(*GetPoisoned<S4>());
203 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000204
205 // S->S conversions.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000206 EXPECT_POISONED(*GetPoisoned<S1>());
207 EXPECT_POISONED(*GetPoisoned<S1>());
208 EXPECT_POISONED(*GetPoisoned<S1>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000209
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000210 EXPECT_POISONED(*GetPoisoned<S2>());
211 EXPECT_POISONED(*GetPoisoned<S2>());
212 EXPECT_POISONED(*GetPoisoned<S2>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000213
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000214 EXPECT_POISONED(*GetPoisoned<S4>());
215 EXPECT_POISONED(*GetPoisoned<S4>());
216 EXPECT_POISONED(*GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000217
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000218 EXPECT_POISONED(*GetPoisoned<S8>());
219 EXPECT_POISONED(*GetPoisoned<S8>());
220 EXPECT_POISONED(*GetPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000221
222 // ZExt
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000223 EXPECT_POISONED(*GetPoisoned<U1>());
224 EXPECT_POISONED(*GetPoisoned<U1>());
225 EXPECT_POISONED(*GetPoisoned<U1>());
226 EXPECT_POISONED(*GetPoisoned<U2>());
227 EXPECT_POISONED(*GetPoisoned<U2>());
228 EXPECT_POISONED(*GetPoisoned<U4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000229
230 // Unary ops.
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000231 EXPECT_POISONED(- *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000232
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000233 EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000234
235
236 a_s4[g_zero] = 1 - *GetPoisoned<S4>();
237 a_s4[g_zero] = 1 + *GetPoisoned<S4>();
238}
239
240TEST(MemorySanitizer, Phi1) {
241 S4 c;
242 if (g_one) {
243 c = *GetPoisoned<S4>();
244 } else {
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000245 break_optimization(0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000246 c = 0;
247 }
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000248 EXPECT_POISONED(c);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000249}
250
251TEST(MemorySanitizer, Phi2) {
252 S4 i = *GetPoisoned<S4>();
253 S4 n = g_one;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000254 EXPECT_UMR(for (; i < g_one; i++););
255 EXPECT_POISONED(i);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000256}
257
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000258NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); }
259NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); }
260NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); }
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000261
262TEST(MemorySanitizer, ArgTest) {
263 Arg1ExpectUMR(*GetPoisoned<S4>());
264 Arg2ExpectUMR(0, *GetPoisoned<S4>());
265 Arg3ExpectUMR(0, 1, *GetPoisoned<S8>());
266}
267
268
269TEST(MemorySanitizer, CallAndRet) {
270 if (!__msan_has_dynamic_component()) return;
271 ReturnPoisoned<S1>();
272 ReturnPoisoned<S2>();
273 ReturnPoisoned<S4>();
274 ReturnPoisoned<S8>();
275
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000276 EXPECT_POISONED(ReturnPoisoned<S1>());
277 EXPECT_POISONED(ReturnPoisoned<S2>());
278 EXPECT_POISONED(ReturnPoisoned<S4>());
279 EXPECT_POISONED(ReturnPoisoned<S8>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000280}
281
282// malloc() in the following test may be optimized to produce a compile-time
283// undef value. Check that we trap on the volatile assignment anyway.
284TEST(MemorySanitizer, DISABLED_MallocNoIdent) {
285 S4 *x = (int*)malloc(sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000286 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000287 free(x);
288}
289
290TEST(MemorySanitizer, Malloc) {
291 S4 *x = (int*)Ident(malloc(sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000292 EXPECT_POISONED(*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000293 free(x);
294}
295
296TEST(MemorySanitizer, Realloc) {
297 S4 *x = (int*)Ident(realloc(0, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000298 EXPECT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000299 x[0] = 1;
300 x = (int*)Ident(realloc(x, 2 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000301 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
302 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000303 x = (int*)Ident(realloc(x, 3 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000304 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
305 EXPECT_POISONED(x[2]);
306 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000307 x[2] = 1; // Init this here. Check that after realloc it is poisoned again.
308 x = (int*)Ident(realloc(x, 2 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000309 EXPECT_NOT_POISONED(x[0]); // Ok, was inited before.
310 EXPECT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000311 x = (int*)Ident(realloc(x, 3 * sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000312 EXPECT_POISONED(x[1]);
313 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000314 free(x);
315}
316
317TEST(MemorySanitizer, Calloc) {
318 S4 *x = (int*)Ident(calloc(1, sizeof(S4)));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000319 EXPECT_NOT_POISONED(*x); // Should not be poisoned.
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000320 // EXPECT_EQ(0, *x);
321 free(x);
322}
323
324TEST(MemorySanitizer, AndOr) {
325 U4 *p = GetPoisoned<U4>();
326 // We poison two bytes in the midle of a 4-byte word to make the test
327 // correct regardless of endianness.
328 ((U1*)p)[1] = 0;
329 ((U1*)p)[2] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000330 EXPECT_NOT_POISONED(*p & 0x00ffff00);
331 EXPECT_NOT_POISONED(*p & 0x00ff0000);
332 EXPECT_NOT_POISONED(*p & 0x0000ff00);
333 EXPECT_POISONED(*p & 0xff000000);
334 EXPECT_POISONED(*p & 0x000000ff);
335 EXPECT_POISONED(*p & 0x0000ffff);
336 EXPECT_POISONED(*p & 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000337
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000338 EXPECT_NOT_POISONED(*p | 0xff0000ff);
339 EXPECT_NOT_POISONED(*p | 0xff00ffff);
340 EXPECT_NOT_POISONED(*p | 0xffff00ff);
341 EXPECT_POISONED(*p | 0xff000000);
342 EXPECT_POISONED(*p | 0x000000ff);
343 EXPECT_POISONED(*p | 0x0000ffff);
344 EXPECT_POISONED(*p | 0xffff0000);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000345
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000346 EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000347}
348
349template<class T>
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000350static bool applyNot(T value, T shadow) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000351 __msan_partial_poison(&value, &shadow, sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000352 return !value;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000353}
354
355TEST(MemorySanitizer, Not) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000356 EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0));
357 EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0));
358 EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF));
359 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF));
360 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF));
361 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF));
362 EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000));
363 EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000));
364 EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000));
365 EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000366
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000367 EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0));
368 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE));
369 EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0));
370 EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000371
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000372 EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1)));
373 EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2)));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000374}
375
376TEST(MemorySanitizer, Shift) {
377 U4 *up = GetPoisoned<U4>();
378 ((U1*)up)[0] = 0;
379 ((U1*)up)[3] = 0xff;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000380 EXPECT_NOT_POISONED(*up >> 30);
381 EXPECT_NOT_POISONED(*up >> 24);
382 EXPECT_POISONED(*up >> 23);
383 EXPECT_POISONED(*up >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000384
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000385 EXPECT_NOT_POISONED(*up << 30);
386 EXPECT_NOT_POISONED(*up << 24);
387 EXPECT_POISONED(*up << 23);
388 EXPECT_POISONED(*up << 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000389
390 S4 *sp = (S4*)up;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000391 EXPECT_NOT_POISONED(*sp >> 30);
392 EXPECT_NOT_POISONED(*sp >> 24);
393 EXPECT_POISONED(*sp >> 23);
394 EXPECT_POISONED(*sp >> 10);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000395
396 sp = GetPoisoned<S4>();
397 ((S1*)sp)[1] = 0;
398 ((S1*)sp)[2] = 0;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000399 EXPECT_POISONED(*sp >> 31);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000400
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000401 EXPECT_POISONED(100 >> *GetPoisoned<S4>());
402 EXPECT_POISONED(100U >> *GetPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000403}
404
405NOINLINE static int GetPoisonedZero() {
406 int *zero = new int;
407 *zero = 0;
408 __msan_poison(zero, sizeof(*zero));
409 int res = *zero;
410 delete zero;
411 return res;
412}
413
414TEST(MemorySanitizer, LoadFromDirtyAddress) {
415 int *a = new int;
416 *a = 0;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000417 EXPECT_UMR(break_optimization((void*)(U8)a[GetPoisonedZero()]));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000418 delete a;
419}
420
421TEST(MemorySanitizer, StoreToDirtyAddress) {
422 int *a = new int;
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000423 EXPECT_UMR(a[GetPoisonedZero()] = 0);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000424 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000425 delete a;
426}
427
428
429NOINLINE void StackTestFunc() {
430 S4 p4;
431 S4 ok4 = 1;
432 S2 p2;
433 S2 ok2 = 1;
434 S1 p1;
435 S1 ok1 = 1;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000436 break_optimization(&p4);
437 break_optimization(&ok4);
438 break_optimization(&p2);
439 break_optimization(&ok2);
440 break_optimization(&p1);
441 break_optimization(&ok1);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000442
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000443 EXPECT_POISONED(p4);
444 EXPECT_POISONED(p2);
445 EXPECT_POISONED(p1);
446 EXPECT_NOT_POISONED(ok1);
447 EXPECT_NOT_POISONED(ok2);
448 EXPECT_NOT_POISONED(ok4);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000449}
450
451TEST(MemorySanitizer, StackTest) {
452 StackTestFunc();
453}
454
455NOINLINE void StackStressFunc() {
456 int foo[10000];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000457 break_optimization(foo);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000458}
459
460TEST(MemorySanitizer, DISABLED_StackStressTest) {
461 for (int i = 0; i < 1000000; i++)
462 StackStressFunc();
463}
464
465template<class T>
466void TestFloatingPoint() {
467 static volatile T v;
468 static T g[100];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000469 break_optimization(&g);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000470 T *x = GetPoisoned<T>();
471 T *y = GetPoisoned<T>(1);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000472 EXPECT_POISONED(*x);
473 EXPECT_POISONED((long long)*x);
474 EXPECT_POISONED((int)*x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000475 g[0] = *x;
476 g[1] = *x + *y;
477 g[2] = *x - *y;
478 g[3] = *x * *y;
479}
480
481TEST(MemorySanitizer, FloatingPointTest) {
482 TestFloatingPoint<float>();
483 TestFloatingPoint<double>();
484}
485
486TEST(MemorySanitizer, DynMem) {
487 S4 x = 0;
488 S4 *y = GetPoisoned<S4>();
489 memcpy(y, &x, g_one * sizeof(S4));
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000490 EXPECT_NOT_POISONED(*y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000491}
492
493static char *DynRetTestStr;
494
495TEST(MemorySanitizer, DynRet) {
496 if (!__msan_has_dynamic_component()) return;
497 ReturnPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000498 EXPECT_NOT_POISONED(clearenv());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000499}
500
501
502TEST(MemorySanitizer, DynRet1) {
503 if (!__msan_has_dynamic_component()) return;
504 ReturnPoisoned<S8>();
505}
506
507struct LargeStruct {
508 S4 x[10];
509};
510
511NOINLINE
512LargeStruct LargeRetTest() {
513 LargeStruct res;
514 res.x[0] = *GetPoisoned<S4>();
515 res.x[1] = *GetPoisoned<S4>();
516 res.x[2] = *GetPoisoned<S4>();
517 res.x[3] = *GetPoisoned<S4>();
518 res.x[4] = *GetPoisoned<S4>();
519 res.x[5] = *GetPoisoned<S4>();
520 res.x[6] = *GetPoisoned<S4>();
521 res.x[7] = *GetPoisoned<S4>();
522 res.x[8] = *GetPoisoned<S4>();
523 res.x[9] = *GetPoisoned<S4>();
524 return res;
525}
526
Alexey Samsonov67505a82013-07-16 12:51:53 +0000527TEST(MemorySanitizer, strcmp) {
528 char s1[10];
529 char s2[10];
530 strncpy(s1, "foo", 10);
531 s2[0] = 'f';
532 s2[1] = 'n';
533 EXPECT_GT(strcmp(s1, s2), 0);
534 s2[1] = 'o';
535 int res;
536 EXPECT_UMR(res = strcmp(s1, s2));
537 EXPECT_NOT_POISONED(res);
538 EXPECT_EQ(strncmp(s1, s2, 1), 0);
539}
540
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000541TEST(MemorySanitizer, LargeRet) {
542 LargeStruct a = LargeRetTest();
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000543 EXPECT_POISONED(a.x[0]);
544 EXPECT_POISONED(a.x[9]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000545}
546
547TEST(MemorySanitizer, fread) {
548 char *x = new char[32];
549 FILE *f = fopen("/proc/self/stat", "r");
550 assert(f);
551 fread(x, 1, 32, f);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000552 EXPECT_NOT_POISONED(x[0]);
553 EXPECT_NOT_POISONED(x[16]);
554 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000555 fclose(f);
556 delete x;
557}
558
559TEST(MemorySanitizer, read) {
560 char *x = new char[32];
561 int fd = open("/proc/self/stat", O_RDONLY);
562 assert(fd > 0);
563 int sz = read(fd, x, 32);
564 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000565 EXPECT_NOT_POISONED(x[0]);
566 EXPECT_NOT_POISONED(x[16]);
567 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000568 close(fd);
569 delete x;
570}
571
572TEST(MemorySanitizer, pread) {
573 char *x = new char[32];
574 int fd = open("/proc/self/stat", O_RDONLY);
575 assert(fd > 0);
576 int sz = pread(fd, x, 32, 0);
577 assert(sz == 32);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000578 EXPECT_NOT_POISONED(x[0]);
579 EXPECT_NOT_POISONED(x[16]);
580 EXPECT_NOT_POISONED(x[31]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000581 close(fd);
582 delete x;
583}
584
Evgeniy Stepanovb916e6a2013-06-24 10:43:23 +0000585TEST(MemorySanitizer, readv) {
586 char buf[2011];
587 struct iovec iov[2];
588 iov[0].iov_base = buf + 1;
589 iov[0].iov_len = 5;
590 iov[1].iov_base = buf + 10;
591 iov[1].iov_len = 2000;
592 int fd = open("/proc/self/stat", O_RDONLY);
593 assert(fd > 0);
594 int sz = readv(fd, iov, 2);
595 ASSERT_LT(sz, 5 + 2000);
596 ASSERT_GT(sz, iov[0].iov_len);
597 EXPECT_POISONED(buf[0]);
598 EXPECT_NOT_POISONED(buf[1]);
599 EXPECT_NOT_POISONED(buf[5]);
600 EXPECT_POISONED(buf[6]);
601 EXPECT_POISONED(buf[9]);
602 EXPECT_NOT_POISONED(buf[10]);
603 EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]);
604 EXPECT_POISONED(buf[11 + (sz - 1) - 5]);
605 close(fd);
606}
607
608TEST(MemorySanitizer, preadv) {
609 char buf[2011];
610 struct iovec iov[2];
611 iov[0].iov_base = buf + 1;
612 iov[0].iov_len = 5;
613 iov[1].iov_base = buf + 10;
614 iov[1].iov_len = 2000;
615 int fd = open("/proc/self/stat", O_RDONLY);
616 assert(fd > 0);
617 int sz = preadv(fd, iov, 2, 3);
618 ASSERT_LT(sz, 5 + 2000);
619 ASSERT_GT(sz, iov[0].iov_len);
620 EXPECT_POISONED(buf[0]);
621 EXPECT_NOT_POISONED(buf[1]);
622 EXPECT_NOT_POISONED(buf[5]);
623 EXPECT_POISONED(buf[6]);
624 EXPECT_POISONED(buf[9]);
625 EXPECT_NOT_POISONED(buf[10]);
626 EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]);
627 EXPECT_POISONED(buf[11 + (sz - 1) - 5]);
628 close(fd);
629}
630
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000631// FIXME: fails now.
632TEST(MemorySanitizer, DISABLED_ioctl) {
633 struct winsize ws;
634 EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000635 EXPECT_NOT_POISONED(ws.ws_col);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000636}
637
638TEST(MemorySanitizer, readlink) {
639 char *x = new char[1000];
640 readlink("/proc/self/exe", x, 1000);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000641 EXPECT_NOT_POISONED(x[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000642 delete [] x;
643}
644
645
646TEST(MemorySanitizer, stat) {
647 struct stat* st = new struct stat;
648 int res = stat("/proc/self/stat", st);
649 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000650 EXPECT_NOT_POISONED(st->st_dev);
651 EXPECT_NOT_POISONED(st->st_mode);
652 EXPECT_NOT_POISONED(st->st_size);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000653}
654
655TEST(MemorySanitizer, statfs) {
656 struct statfs* st = new struct statfs;
657 int res = statfs("/", st);
658 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000659 EXPECT_NOT_POISONED(st->f_type);
660 EXPECT_NOT_POISONED(st->f_bfree);
661 EXPECT_NOT_POISONED(st->f_namelen);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000662}
663
664TEST(MemorySanitizer, pipe) {
665 int* pipefd = new int[2];
666 int res = pipe(pipefd);
667 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000668 EXPECT_NOT_POISONED(pipefd[0]);
669 EXPECT_NOT_POISONED(pipefd[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000670 close(pipefd[0]);
671 close(pipefd[1]);
672}
673
Evgeniy Stepanov134fe8a2013-04-08 13:45:12 +0000674TEST(MemorySanitizer, pipe2) {
675 int* pipefd = new int[2];
676 int res = pipe2(pipefd, O_NONBLOCK);
677 assert(!res);
678 EXPECT_NOT_POISONED(pipefd[0]);
679 EXPECT_NOT_POISONED(pipefd[1]);
680 close(pipefd[0]);
681 close(pipefd[1]);
682}
683
684TEST(MemorySanitizer, socketpair) {
685 int sv[2];
686 int res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
687 assert(!res);
688 EXPECT_NOT_POISONED(sv[0]);
689 EXPECT_NOT_POISONED(sv[1]);
690 close(sv[0]);
691 close(sv[1]);
692}
693
Evgeniy Stepanov9f58c5c2013-05-22 13:46:22 +0000694TEST(MemorySanitizer, bind_getsockname) {
695 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
696
697 struct sockaddr_in sai;
698 memset(&sai, 0, sizeof(sai));
699 sai.sin_family = AF_UNIX;
700 int res = bind(sock, (struct sockaddr *)&sai, sizeof(sai));
701
702 assert(!res);
703 char buf[200];
704 socklen_t addrlen;
705 EXPECT_UMR(getsockname(sock, (struct sockaddr *)&buf, &addrlen));
706
707 addrlen = sizeof(buf);
708 res = getsockname(sock, (struct sockaddr *)&buf, &addrlen);
709 EXPECT_NOT_POISONED(addrlen);
710 EXPECT_NOT_POISONED(buf[0]);
711 EXPECT_NOT_POISONED(buf[addrlen - 1]);
712 EXPECT_POISONED(buf[addrlen]);
713 close(sock);
714}
715
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000716TEST(MemorySanitizer, accept) {
717 int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
718 ASSERT_LT(0, listen_socket);
719
720 struct sockaddr_in sai;
721 sai.sin_family = AF_INET;
722 sai.sin_port = 0;
723 sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
724 int res = bind(listen_socket, (struct sockaddr *)&sai, sizeof(sai));
725 ASSERT_EQ(0, res);
726
727 res = listen(listen_socket, 1);
728 ASSERT_EQ(0, res);
729
730 socklen_t sz = sizeof(sai);
731 res = getsockname(listen_socket, (struct sockaddr *)&sai, &sz);
732 ASSERT_EQ(0, res);
733 ASSERT_EQ(sizeof(sai), sz);
Evgeniy Stepanovbc33e132013-05-29 11:49:25 +0000734
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000735 int connect_socket = socket(AF_INET, SOCK_STREAM, 0);
736 ASSERT_LT(0, connect_socket);
737 res = fcntl(connect_socket, F_SETFL, O_NONBLOCK);
738 ASSERT_EQ(0, res);
739 res = connect(connect_socket, (struct sockaddr *)&sai, sizeof(sai));
740 ASSERT_EQ(-1, res);
741 ASSERT_EQ(EINPROGRESS, errno);
742
743 __msan_poison(&sai, sizeof(sai));
Evgeniy Stepanovbc33e132013-05-29 11:49:25 +0000744 int new_sock = accept(listen_socket, (struct sockaddr *)&sai, &sz);
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000745 ASSERT_LT(0, new_sock);
746 ASSERT_EQ(sizeof(sai), sz);
747 EXPECT_NOT_POISONED(sai);
748
Evgeniy Stepanovbc33e132013-05-29 11:49:25 +0000749 __msan_poison(&sai, sizeof(sai));
750 res = getpeername(new_sock, (struct sockaddr *)&sai, &sz);
751 ASSERT_EQ(0, res);
752 ASSERT_EQ(sizeof(sai), sz);
753 EXPECT_NOT_POISONED(sai);
754
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +0000755 close(new_sock);
756 close(connect_socket);
757 close(listen_socket);
758}
759
Evgeniy Stepanov512c6162013-05-29 12:33:31 +0000760TEST(MemorySanitizer, getaddrinfo) {
761 struct addrinfo *ai;
762 struct addrinfo hints;
763 memset(&hints, 0, sizeof(hints));
764 hints.ai_family = AF_INET;
765 int res = getaddrinfo("localhost", NULL, &hints, &ai);
766 ASSERT_EQ(0, res);
767 EXPECT_NOT_POISONED(*ai);
768 ASSERT_EQ(sizeof(sockaddr_in), ai->ai_addrlen);
769 EXPECT_NOT_POISONED(*(sockaddr_in*)ai->ai_addr);
770}
771
Evgeniy Stepanov9eedf482013-07-01 13:51:31 +0000772TEST(MemorySanitizer, getnameinfo) {
773 struct sockaddr_in sai;
774 sai.sin_family = AF_INET;
775 sai.sin_port = 80;
776 sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
777 char host[500];
778 char serv[500];
779 int res = getnameinfo((struct sockaddr *)&sai, sizeof(sai), host,
780 sizeof(host), serv, sizeof(serv), 0);
781 ASSERT_EQ(0, res);
782 EXPECT_NOT_POISONED(host[0]);
783 EXPECT_POISONED(host[sizeof(host) - 1]);
784
785 ASSERT_NE(0, strlen(host));
786 EXPECT_NOT_POISONED(serv[0]);
787 EXPECT_POISONED(serv[sizeof(serv) - 1]);
788 ASSERT_NE(0, strlen(serv));
789}
790
Evgeniy Stepanov0a2cc372013-05-23 11:10:23 +0000791#define EXPECT_HOSTENT_NOT_POISONED(he) \
792 do { \
793 EXPECT_NOT_POISONED(*(he)); \
794 ASSERT_NE((void *) 0, (he)->h_name); \
795 ASSERT_NE((void *) 0, (he)->h_aliases); \
796 ASSERT_NE((void *) 0, (he)->h_addr_list); \
797 EXPECT_NOT_POISONED(strlen((he)->h_name)); \
798 char **p = (he)->h_aliases; \
799 while (*p) { \
800 EXPECT_NOT_POISONED(strlen(*p)); \
801 ++p; \
802 } \
803 char **q = (he)->h_addr_list; \
804 while (*q) { \
805 EXPECT_NOT_POISONED(*q[0]); \
806 ++q; \
807 } \
808 EXPECT_NOT_POISONED(*q); \
809 } while (0)
810
811TEST(MemorySanitizer, gethostent) {
812 struct hostent *he = gethostent();
813 ASSERT_NE((void *)NULL, he);
814 EXPECT_HOSTENT_NOT_POISONED(he);
815}
816
Evgeniy Stepanov263800b2013-06-27 13:21:00 +0000817#ifndef MSAN_TEST_DISABLE_GETHOSTBYNAME
818
Evgeniy Stepanov0a2cc372013-05-23 11:10:23 +0000819TEST(MemorySanitizer, gethostbyname) {
820 struct hostent *he = gethostbyname("localhost");
821 ASSERT_NE((void *)NULL, he);
822 EXPECT_HOSTENT_NOT_POISONED(he);
823}
824
Evgeniy Stepanov263800b2013-06-27 13:21:00 +0000825#endif // MSAN_TEST_DISABLE_GETHOSTBYNAME
826
Evgeniy Stepanov0a2cc372013-05-23 11:10:23 +0000827TEST(MemorySanitizer, gethostbyname2) {
828 struct hostent *he = gethostbyname2("localhost", AF_INET);
829 ASSERT_NE((void *)NULL, he);
830 EXPECT_HOSTENT_NOT_POISONED(he);
831}
832
833TEST(MemorySanitizer, gethostbyaddr) {
834 in_addr_t addr = inet_addr("127.0.0.1");
835 EXPECT_NOT_POISONED(addr);
836 struct hostent *he = gethostbyaddr(&addr, sizeof(addr), AF_INET);
837 ASSERT_NE((void *)NULL, he);
838 EXPECT_HOSTENT_NOT_POISONED(he);
839}
840
841TEST(MemorySanitizer, gethostent_r) {
842 char buf[2000];
843 struct hostent he;
844 struct hostent *result;
845 int err;
846 int res = gethostent_r(&he, buf, sizeof(buf), &result, &err);
847 ASSERT_EQ(0, res);
848 EXPECT_NOT_POISONED(result);
849 ASSERT_NE((void *)NULL, result);
850 EXPECT_HOSTENT_NOT_POISONED(result);
851 EXPECT_NOT_POISONED(err);
852}
853
854TEST(MemorySanitizer, gethostbyname_r) {
855 char buf[2000];
856 struct hostent he;
857 struct hostent *result;
858 int err;
859 int res = gethostbyname_r("localhost", &he, buf, sizeof(buf), &result, &err);
860 ASSERT_EQ(0, res);
861 EXPECT_NOT_POISONED(result);
862 ASSERT_NE((void *)NULL, result);
863 EXPECT_HOSTENT_NOT_POISONED(result);
864 EXPECT_NOT_POISONED(err);
865}
866
867TEST(MemorySanitizer, gethostbyname2_r) {
868 char buf[2000];
869 struct hostent he;
870 struct hostent *result;
871 int err;
872 int res = gethostbyname2_r("localhost", AF_INET, &he, buf, sizeof(buf),
873 &result, &err);
874 ASSERT_EQ(0, res);
875 EXPECT_NOT_POISONED(result);
876 ASSERT_NE((void *)NULL, result);
877 EXPECT_HOSTENT_NOT_POISONED(result);
878 EXPECT_NOT_POISONED(err);
879}
880
881TEST(MemorySanitizer, gethostbyaddr_r) {
882 char buf[2000];
883 struct hostent he;
884 struct hostent *result;
885 int err;
886 in_addr_t addr = inet_addr("127.0.0.1");
887 EXPECT_NOT_POISONED(addr);
888 int res = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &he, buf, sizeof(buf),
889 &result, &err);
890 ASSERT_EQ(0, res);
891 EXPECT_NOT_POISONED(result);
892 ASSERT_NE((void *)NULL, result);
893 EXPECT_HOSTENT_NOT_POISONED(result);
894 EXPECT_NOT_POISONED(err);
895}
896
Evgeniy Stepanovf32be422013-05-23 11:38:08 +0000897TEST(MemorySanitizer, getsockopt) {
898 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
899 struct linger l[2];
900 socklen_t sz = sizeof(l[0]);
901 int res = getsockopt(sock, SOL_SOCKET, SO_LINGER, &l[0], &sz);
902 ASSERT_EQ(0, res);
903 ASSERT_EQ(sizeof(l[0]), sz);
904 EXPECT_NOT_POISONED(l[0]);
905 EXPECT_POISONED(*(char *)(l + 1));
906}
907
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000908TEST(MemorySanitizer, getcwd) {
909 char path[PATH_MAX + 1];
910 char* res = getcwd(path, sizeof(path));
911 assert(res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000912 EXPECT_NOT_POISONED(path[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000913}
914
Evgeniy Stepanov7eed04c2013-02-12 14:36:22 +0000915TEST(MemorySanitizer, getcwd_gnu) {
916 char* res = getcwd(NULL, 0);
917 assert(res);
918 EXPECT_NOT_POISONED(res[0]);
919 free(res);
920}
921
Evgeniy Stepanov80144892013-07-02 13:34:44 +0000922TEST(MemorySanitizer, get_current_dir_name) {
923 char* res = get_current_dir_name();
924 assert(res);
925 EXPECT_NOT_POISONED(res[0]);
926 free(res);
927}
928
Evgeniy Stepanov5ec19bc2013-07-30 12:46:59 +0000929TEST(MemorySanitizer, confstr) {
930 char buf[3];
931 size_t res = confstr(_CS_PATH, buf, sizeof(buf));
932 ASSERT_GT(res, sizeof(buf));
933 EXPECT_NOT_POISONED(buf[0]);
934 EXPECT_NOT_POISONED(buf[sizeof(buf) - 1]);
935
936 char buf2[1000];
937 res = confstr(_CS_PATH, buf2, sizeof(buf2));
938 ASSERT_LT(res, sizeof(buf2));
939 EXPECT_NOT_POISONED(buf2[0]);
940 EXPECT_NOT_POISONED(buf2[res - 1]);
941 EXPECT_POISONED(buf2[res]);
942 ASSERT_EQ(res, strlen(buf2) + 1);
943}
944
Evgeniy Stepanovd97a15a2013-03-14 12:49:23 +0000945TEST(MemorySanitizer, readdir) {
946 DIR *dir = opendir(".");
947 struct dirent *d = readdir(dir);
948 assert(d);
949 EXPECT_NOT_POISONED(d->d_name[0]);
950 closedir(dir);
951}
952
Evgeniy Stepanovb5cf98f2013-06-26 15:00:53 +0000953TEST(MemorySanitizer, readdir_r) {
954 DIR *dir = opendir(".");
955 struct dirent d;
956 struct dirent *pd;
957 int res = readdir_r(dir, &d, &pd);
958 assert(!res);
959 EXPECT_NOT_POISONED(pd);
960 EXPECT_NOT_POISONED(d.d_name[0]);
961 closedir(dir);
962}
963
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000964TEST(MemorySanitizer, realpath) {
965 const char* relpath = ".";
966 char path[PATH_MAX + 1];
967 char* res = realpath(relpath, path);
968 assert(res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000969 EXPECT_NOT_POISONED(path[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000970}
971
Evgeniy Stepanov12eb79d2013-07-09 09:53:37 +0000972TEST(MemorySanitizer, realpath_null) {
973 const char* relpath = ".";
974 char* res = realpath(relpath, NULL);
975 printf("%d, %s\n", errno, strerror(errno));
976 assert(res);
977 EXPECT_NOT_POISONED(res[0]);
978 free(res);
979}
980
981TEST(MemorySanitizer, canonicalize_file_name) {
982 const char* relpath = ".";
983 char* res = canonicalize_file_name(relpath);
984 assert(res);
985 EXPECT_NOT_POISONED(res[0]);
986 free(res);
987}
988
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000989TEST(MemorySanitizer, memcpy) {
990 char* x = new char[2];
991 char* y = new char[2];
992 x[0] = 1;
993 x[1] = *GetPoisoned<char>();
994 memcpy(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +0000995 EXPECT_NOT_POISONED(y[0]);
996 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +0000997}
998
999TEST(MemorySanitizer, memmove) {
1000 char* x = new char[2];
1001 char* y = new char[2];
1002 x[0] = 1;
1003 x[1] = *GetPoisoned<char>();
1004 memmove(y, x, 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001005 EXPECT_NOT_POISONED(y[0]);
1006 EXPECT_POISONED(y[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001007}
1008
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001009TEST(MemorySanitizer, bcopy) {
1010 char* x = new char[2];
1011 char* y = new char[2];
1012 x[0] = 1;
1013 x[1] = *GetPoisoned<char>();
1014 bcopy(x, y, 2);
1015 EXPECT_NOT_POISONED(y[0]);
1016 EXPECT_POISONED(y[1]);
1017}
1018
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001019TEST(MemorySanitizer, strdup) {
Evgeniy Stepanov8aa1ae02013-03-14 11:10:36 +00001020 char buf[4] = "abc";
1021 __msan_poison(buf + 2, sizeof(*buf));
1022 char *x = strdup(buf);
1023 EXPECT_NOT_POISONED(x[0]);
1024 EXPECT_NOT_POISONED(x[1]);
1025 EXPECT_POISONED(x[2]);
1026 EXPECT_NOT_POISONED(x[3]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001027 free(x);
1028}
1029
Evgeniy Stepanov8aa1ae02013-03-14 11:10:36 +00001030TEST(MemorySanitizer, strndup) {
1031 char buf[4] = "abc";
1032 __msan_poison(buf + 2, sizeof(*buf));
1033 char *x = strndup(buf, 3);
1034 EXPECT_NOT_POISONED(x[0]);
1035 EXPECT_NOT_POISONED(x[1]);
1036 EXPECT_POISONED(x[2]);
1037 EXPECT_NOT_POISONED(x[3]);
1038 free(x);
1039}
1040
1041TEST(MemorySanitizer, strndup_short) {
1042 char buf[4] = "abc";
1043 __msan_poison(buf + 1, sizeof(*buf));
1044 __msan_poison(buf + 2, sizeof(*buf));
1045 char *x = strndup(buf, 2);
1046 EXPECT_NOT_POISONED(x[0]);
1047 EXPECT_POISONED(x[1]);
Evgeniy Stepanov1d21bd12013-03-14 11:58:13 +00001048 EXPECT_NOT_POISONED(x[2]);
Evgeniy Stepanov8aa1ae02013-03-14 11:10:36 +00001049 free(x);
1050}
1051
1052
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001053template<class T, int size>
1054void TestOverlapMemmove() {
1055 T *x = new T[size];
1056 assert(size >= 3);
1057 x[2] = 0;
1058 memmove(x, x + 1, (size - 1) * sizeof(T));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001059 EXPECT_NOT_POISONED(x[1]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001060 if (!__msan_has_dynamic_component()) {
1061 // FIXME: under DR we will lose this information
1062 // because accesses in memmove will unpoisin the shadow.
1063 // We need to use our own memove implementation instead of libc's.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001064 EXPECT_POISONED(x[0]);
1065 EXPECT_POISONED(x[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001066 }
1067 delete [] x;
1068}
1069
1070TEST(MemorySanitizer, overlap_memmove) {
1071 TestOverlapMemmove<U1, 10>();
1072 TestOverlapMemmove<U1, 1000>();
1073 TestOverlapMemmove<U8, 4>();
1074 TestOverlapMemmove<U8, 1000>();
1075}
1076
1077TEST(MemorySanitizer, strcpy) { // NOLINT
1078 char* x = new char[3];
1079 char* y = new char[3];
1080 x[0] = 'a';
1081 x[1] = *GetPoisoned<char>(1, 1);
1082 x[2] = 0;
1083 strcpy(y, x); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001084 EXPECT_NOT_POISONED(y[0]);
1085 EXPECT_POISONED(y[1]);
1086 EXPECT_NOT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001087}
1088
1089TEST(MemorySanitizer, strncpy) { // NOLINT
1090 char* x = new char[3];
1091 char* y = new char[3];
1092 x[0] = 'a';
1093 x[1] = *GetPoisoned<char>(1, 1);
1094 x[2] = 0;
1095 strncpy(y, x, 2); // NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001096 EXPECT_NOT_POISONED(y[0]);
1097 EXPECT_POISONED(y[1]);
1098 EXPECT_POISONED(y[2]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001099}
1100
Evgeniy Stepanov4bbbe132013-07-01 15:19:37 +00001101TEST(MemorySanitizer, stpcpy) { // NOLINT
1102 char* x = new char[3];
1103 char* y = new char[3];
1104 x[0] = 'a';
1105 x[1] = *GetPoisoned<char>(1, 1);
1106 x[2] = 0;
1107 char *res = stpcpy(y, x); // NOLINT
1108 ASSERT_EQ(res, y + 2);
1109 EXPECT_NOT_POISONED(y[0]);
1110 EXPECT_POISONED(y[1]);
1111 EXPECT_NOT_POISONED(y[2]);
1112}
1113
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001114TEST(MemorySanitizer, strtol) {
1115 char *e;
1116 assert(1 == strtol("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001117 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001118}
1119
1120TEST(MemorySanitizer, strtoll) {
1121 char *e;
1122 assert(1 == strtoll("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001123 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001124}
1125
1126TEST(MemorySanitizer, strtoul) {
1127 char *e;
1128 assert(1 == strtoul("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001129 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001130}
1131
1132TEST(MemorySanitizer, strtoull) {
1133 char *e;
1134 assert(1 == strtoull("1", &e, 10));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001135 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001136}
1137
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001138TEST(MemorySanitizer, strtoimax) {
1139 char *e;
1140 assert(1 == strtoimax("1", &e, 10));
1141 EXPECT_NOT_POISONED((S8) e);
1142}
1143
1144TEST(MemorySanitizer, strtoumax) {
1145 char *e;
1146 assert(1 == strtoumax("1", &e, 10));
1147 EXPECT_NOT_POISONED((S8) e);
1148}
1149
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001150TEST(MemorySanitizer, strtod) {
1151 char *e;
1152 assert(0 != strtod("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001153 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001154}
1155
1156TEST(MemorySanitizer, strtof) {
1157 char *e;
1158 assert(0 != strtof("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001159 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001160}
1161
1162TEST(MemorySanitizer, strtold) {
1163 char *e;
1164 assert(0 != strtold("1.5", &e));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001165 EXPECT_NOT_POISONED((S8) e);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001166}
1167
Evgeniy Stepanovc87088b2013-05-29 10:03:11 +00001168TEST(MemorySanitizer, modf) {
1169 double x, y;
1170 x = modf(2.1, &y);
1171 EXPECT_NOT_POISONED(y);
1172}
1173
1174TEST(MemorySanitizer, modff) {
1175 float x, y;
1176 x = modff(2.1, &y);
1177 EXPECT_NOT_POISONED(y);
1178}
1179
1180TEST(MemorySanitizer, modfl) {
1181 long double x, y;
1182 x = modfl(2.1, &y);
1183 EXPECT_NOT_POISONED(y);
1184}
1185
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001186TEST(MemorySanitizer, sprintf) { // NOLINT
1187 char buff[10];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001188 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001189 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001190 int res = sprintf(buff, "%d", 1234567); // NOLINT
1191 assert(res == 7);
1192 assert(buff[0] == '1');
1193 assert(buff[1] == '2');
1194 assert(buff[2] == '3');
1195 assert(buff[6] == '7');
1196 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001197 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001198}
1199
1200TEST(MemorySanitizer, snprintf) {
1201 char buff[10];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001202 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001203 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001204 int res = snprintf(buff, sizeof(buff), "%d", 1234567);
1205 assert(res == 7);
1206 assert(buff[0] == '1');
1207 assert(buff[1] == '2');
1208 assert(buff[2] == '3');
1209 assert(buff[6] == '7');
1210 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001211 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001212}
1213
1214TEST(MemorySanitizer, swprintf) {
1215 wchar_t buff[10];
1216 assert(sizeof(wchar_t) == 4);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001217 break_optimization(buff);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001218 EXPECT_POISONED(buff[0]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001219 int res = swprintf(buff, 9, L"%d", 1234567);
1220 assert(res == 7);
1221 assert(buff[0] == '1');
1222 assert(buff[1] == '2');
1223 assert(buff[2] == '3');
1224 assert(buff[6] == '7');
1225 assert(buff[7] == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001226 EXPECT_POISONED(buff[8]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001227}
1228
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +00001229TEST(MemorySanitizer, asprintf) { // NOLINT
1230 char *pbuf;
1231 EXPECT_POISONED(pbuf);
1232 int res = asprintf(&pbuf, "%d", 1234567); // NOLINT
1233 assert(res == 7);
1234 EXPECT_NOT_POISONED(pbuf);
1235 assert(pbuf[0] == '1');
1236 assert(pbuf[1] == '2');
1237 assert(pbuf[2] == '3');
1238 assert(pbuf[6] == '7');
1239 assert(pbuf[7] == 0);
1240 free(pbuf);
1241}
1242
Alexey Samsonove43d2102013-07-12 11:59:58 +00001243TEST(MemorySanitizer, mbstowcs) {
1244 const char *x = "abc";
1245 wchar_t buff[10];
1246 int res = mbstowcs(buff, x, 2);
1247 EXPECT_EQ(2, res);
1248 EXPECT_EQ(L'a', buff[0]);
1249 EXPECT_EQ(L'b', buff[1]);
1250 EXPECT_POISONED(buff[2]);
1251 res = mbstowcs(buff, x, 10);
1252 EXPECT_EQ(3, res);
1253 EXPECT_NOT_POISONED(buff[3]);
1254}
1255
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001256TEST(MemorySanitizer, wcstombs) {
1257 const wchar_t *x = L"abc";
1258 char buff[10];
1259 int res = wcstombs(buff, x, 4);
1260 EXPECT_EQ(res, 3);
1261 EXPECT_EQ(buff[0], 'a');
1262 EXPECT_EQ(buff[1], 'b');
1263 EXPECT_EQ(buff[2], 'c');
1264}
1265
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001266TEST(MemorySanitizer, wcsrtombs) {
1267 const wchar_t *x = L"abc";
1268 const wchar_t *p = x;
1269 char buff[10];
1270 mbstate_t mbs;
Evgeniy Stepanov57a10952013-07-10 14:17:46 +00001271 memset(&mbs, 0, sizeof(mbs));
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001272 int res = wcsrtombs(buff, &p, 4, &mbs);
1273 EXPECT_EQ(res, 3);
1274 EXPECT_EQ(buff[0], 'a');
1275 EXPECT_EQ(buff[1], 'b');
1276 EXPECT_EQ(buff[2], 'c');
Evgeniy Stepanov9b21ba62013-07-10 14:01:51 +00001277 EXPECT_EQ(buff[3], '\0');
1278 EXPECT_POISONED(buff[4]);
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001279}
1280
1281TEST(MemorySanitizer, wcsnrtombs) {
1282 const wchar_t *x = L"abc";
1283 const wchar_t *p = x;
1284 char buff[10];
1285 mbstate_t mbs;
Evgeniy Stepanov57a10952013-07-10 14:17:46 +00001286 memset(&mbs, 0, sizeof(mbs));
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001287 int res = wcsnrtombs(buff, &p, 2, 4, &mbs);
1288 EXPECT_EQ(res, 2);
1289 EXPECT_EQ(buff[0], 'a');
1290 EXPECT_EQ(buff[1], 'b');
Evgeniy Stepanov9b21ba62013-07-10 14:01:51 +00001291 EXPECT_POISONED(buff[2]);
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001292}
1293
Evgeniy Stepanov80144892013-07-02 13:34:44 +00001294TEST(MemorySanitizer, mbtowc) {
1295 const char *x = "abc";
1296 wchar_t wx;
1297 int res = mbtowc(&wx, x, 3);
1298 EXPECT_GT(res, 0);
1299 EXPECT_NOT_POISONED(wx);
1300}
1301
1302TEST(MemorySanitizer, mbrtowc) {
1303 const char *x = "abc";
1304 wchar_t wx;
1305 mbstate_t mbs;
1306 memset(&mbs, 0, sizeof(mbs));
1307 int res = mbrtowc(&wx, x, 3, &mbs);
1308 EXPECT_GT(res, 0);
1309 EXPECT_NOT_POISONED(wx);
1310}
1311
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001312TEST(MemorySanitizer, gettimeofday) {
1313 struct timeval tv;
1314 struct timezone tz;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001315 break_optimization(&tv);
1316 break_optimization(&tz);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001317 assert(sizeof(tv) == 16);
1318 assert(sizeof(tz) == 8);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001319 EXPECT_POISONED(tv.tv_sec);
1320 EXPECT_POISONED(tv.tv_usec);
1321 EXPECT_POISONED(tz.tz_minuteswest);
1322 EXPECT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001323 assert(0 == gettimeofday(&tv, &tz));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001324 EXPECT_NOT_POISONED(tv.tv_sec);
1325 EXPECT_NOT_POISONED(tv.tv_usec);
1326 EXPECT_NOT_POISONED(tz.tz_minuteswest);
1327 EXPECT_NOT_POISONED(tz.tz_dsttime);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001328}
1329
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00001330TEST(MemorySanitizer, clock_gettime) {
1331 struct timespec tp;
1332 EXPECT_POISONED(tp.tv_sec);
1333 EXPECT_POISONED(tp.tv_nsec);
1334 assert(0 == clock_gettime(CLOCK_REALTIME, &tp));
1335 EXPECT_NOT_POISONED(tp.tv_sec);
1336 EXPECT_NOT_POISONED(tp.tv_nsec);
1337}
1338
Evgeniy Stepanov7cdae162013-04-23 11:48:31 +00001339TEST(MemorySanitizer, clock_getres) {
1340 struct timespec tp;
1341 EXPECT_POISONED(tp.tv_sec);
1342 EXPECT_POISONED(tp.tv_nsec);
1343 assert(0 == clock_getres(CLOCK_REALTIME, 0));
1344 EXPECT_POISONED(tp.tv_sec);
1345 EXPECT_POISONED(tp.tv_nsec);
1346 assert(0 == clock_getres(CLOCK_REALTIME, &tp));
1347 EXPECT_NOT_POISONED(tp.tv_sec);
1348 EXPECT_NOT_POISONED(tp.tv_nsec);
1349}
1350
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00001351TEST(MemorySanitizer, getitimer) {
1352 struct itimerval it1, it2;
1353 int res;
1354 EXPECT_POISONED(it1.it_interval.tv_sec);
1355 EXPECT_POISONED(it1.it_interval.tv_usec);
1356 EXPECT_POISONED(it1.it_value.tv_sec);
1357 EXPECT_POISONED(it1.it_value.tv_usec);
1358 res = getitimer(ITIMER_VIRTUAL, &it1);
1359 assert(!res);
1360 EXPECT_NOT_POISONED(it1.it_interval.tv_sec);
1361 EXPECT_NOT_POISONED(it1.it_interval.tv_usec);
1362 EXPECT_NOT_POISONED(it1.it_value.tv_sec);
1363 EXPECT_NOT_POISONED(it1.it_value.tv_usec);
1364
1365 it1.it_interval.tv_sec = it1.it_value.tv_sec = 10000;
1366 it1.it_interval.tv_usec = it1.it_value.tv_usec = 0;
1367
1368 res = setitimer(ITIMER_VIRTUAL, &it1, &it2);
1369 assert(!res);
1370 EXPECT_NOT_POISONED(it2.it_interval.tv_sec);
1371 EXPECT_NOT_POISONED(it2.it_interval.tv_usec);
1372 EXPECT_NOT_POISONED(it2.it_value.tv_sec);
1373 EXPECT_NOT_POISONED(it2.it_value.tv_usec);
1374
1375 // Check that old_value can be 0, and disable the timer.
1376 memset(&it1, 0, sizeof(it1));
1377 res = setitimer(ITIMER_VIRTUAL, &it1, 0);
1378 assert(!res);
1379}
1380
Evgeniy Stepanovfef66052013-04-08 08:25:22 +00001381TEST(MemorySanitizer, time) {
1382 time_t t;
1383 EXPECT_POISONED(t);
1384 time_t t2 = time(&t);
1385 assert(t2 != (time_t)-1);
1386 EXPECT_NOT_POISONED(t);
1387}
1388
Evgeniy Stepanov9358c582013-02-19 09:19:16 +00001389TEST(MemorySanitizer, localtime) {
1390 time_t t = 123;
1391 struct tm *time = localtime(&t);
1392 assert(time != 0);
1393 EXPECT_NOT_POISONED(time->tm_sec);
1394 EXPECT_NOT_POISONED(time->tm_hour);
1395 EXPECT_NOT_POISONED(time->tm_year);
1396 EXPECT_NOT_POISONED(time->tm_isdst);
1397}
1398
1399TEST(MemorySanitizer, localtime_r) {
1400 time_t t = 123;
1401 struct tm time;
1402 struct tm *res = localtime_r(&t, &time);
1403 assert(res != 0);
1404 EXPECT_NOT_POISONED(time.tm_sec);
1405 EXPECT_NOT_POISONED(time.tm_hour);
1406 EXPECT_NOT_POISONED(time.tm_year);
1407 EXPECT_NOT_POISONED(time.tm_isdst);
1408}
1409
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001410TEST(MemorySanitizer, mmap) {
1411 const int size = 4096;
1412 void *p1, *p2;
1413 p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1414 __msan_poison(p1, size);
1415 munmap(p1, size);
1416 for (int i = 0; i < 1000; i++) {
1417 p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1418 if (p2 == p1)
1419 break;
1420 else
1421 munmap(p2, size);
1422 }
1423 if (p1 == p2) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001424 EXPECT_NOT_POISONED(*(char*)p2);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001425 munmap(p2, size);
1426 }
1427}
1428
1429// FIXME: enable and add ecvt.
1430// FIXME: check why msandr does nt handle fcvt.
1431TEST(MemorySanitizer, fcvt) {
1432 int a, b;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001433 break_optimization(&a);
1434 break_optimization(&b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001435 EXPECT_POISONED(a);
1436 EXPECT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001437 char *str = fcvt(12345.6789, 10, &a, &b);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001438 EXPECT_NOT_POISONED(a);
1439 EXPECT_NOT_POISONED(b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001440}
1441
Evgeniy Stepanov7cbbb292013-03-14 11:34:39 +00001442TEST(MemorySanitizer, frexp) {
1443 int x;
1444 x = *GetPoisoned<int>();
1445 double r = frexp(1.1, &x);
1446 EXPECT_NOT_POISONED(r);
1447 EXPECT_NOT_POISONED(x);
1448
1449 x = *GetPoisoned<int>();
1450 float rf = frexpf(1.1, &x);
1451 EXPECT_NOT_POISONED(rf);
1452 EXPECT_NOT_POISONED(x);
1453
1454 x = *GetPoisoned<int>();
1455 double rl = frexpl(1.1, &x);
1456 EXPECT_NOT_POISONED(rl);
1457 EXPECT_NOT_POISONED(x);
1458}
1459
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001460namespace {
1461
1462static int cnt;
1463
1464void SigactionHandler(int signo, siginfo_t* si, void* uc) {
1465 assert(signo == SIGPROF);
1466 assert(si);
1467 EXPECT_NOT_POISONED(si->si_errno);
1468 EXPECT_NOT_POISONED(si->si_pid);
1469#if __linux__
1470# if defined(__x86_64__)
1471 EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_RIP]);
1472# elif defined(__i386__)
1473 EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_EIP]);
1474# endif
1475#endif
1476 ++cnt;
1477}
1478
1479TEST(MemorySanitizer, sigaction) {
1480 struct sigaction act = {};
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001481 struct sigaction oldact = {};
Evgeniy Stepanov6d0b7f62013-04-15 13:35:05 +00001482 struct sigaction origact = {};
1483
1484 sigaction(SIGPROF, 0, &origact);
1485
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001486 act.sa_flags |= SA_SIGINFO;
1487 act.sa_sigaction = &SigactionHandler;
1488 sigaction(SIGPROF, &act, 0);
1489
1490 kill(getpid(), SIGPROF);
1491
1492 act.sa_flags &= ~SA_SIGINFO;
1493 act.sa_handler = SIG_DFL;
1494 sigaction(SIGPROF, &act, 0);
1495
1496 act.sa_flags &= ~SA_SIGINFO;
1497 act.sa_handler = SIG_IGN;
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001498 sigaction(SIGPROF, &act, &oldact);
1499 EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO);
1500 EXPECT_EQ(SIG_DFL, oldact.sa_handler);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001501 kill(getpid(), SIGPROF);
1502
1503 act.sa_flags |= SA_SIGINFO;
1504 act.sa_sigaction = &SigactionHandler;
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001505 sigaction(SIGPROF, &act, &oldact);
1506 EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO);
1507 EXPECT_EQ(SIG_IGN, oldact.sa_handler);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001508 kill(getpid(), SIGPROF);
1509
1510 act.sa_flags &= ~SA_SIGINFO;
1511 act.sa_handler = SIG_DFL;
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001512 sigaction(SIGPROF, &act, &oldact);
1513 EXPECT_TRUE(oldact.sa_flags & SA_SIGINFO);
1514 EXPECT_EQ(&SigactionHandler, oldact.sa_sigaction);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001515 EXPECT_EQ(2, cnt);
Evgeniy Stepanov6d0b7f62013-04-15 13:35:05 +00001516
1517 sigaction(SIGPROF, &origact, 0);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001518}
1519
1520} // namespace
1521
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001522struct StructWithDtor {
1523 ~StructWithDtor();
1524};
1525
1526NOINLINE StructWithDtor::~StructWithDtor() {
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001527 break_optimization(0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001528}
1529
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001530TEST(MemorySanitizer, Invoke) {
1531 StructWithDtor s; // Will cause the calls to become invokes.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001532 EXPECT_NOT_POISONED(0);
1533 EXPECT_POISONED(*GetPoisoned<int>());
1534 EXPECT_NOT_POISONED(0);
1535 EXPECT_POISONED(*GetPoisoned<int>());
1536 EXPECT_POISONED(ReturnPoisoned<S4>());
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001537}
1538
1539TEST(MemorySanitizer, ptrtoint) {
1540 // Test that shadow is propagated through pointer-to-integer conversion.
1541 void* p = (void*)0xABCD;
1542 __msan_poison(((char*)&p) + 1, sizeof(p));
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00001543 EXPECT_NOT_POISONED((((uintptr_t)p) & 0xFF) == 0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001544
1545 void* q = (void*)0xABCD;
1546 __msan_poison(&q, sizeof(q) - 1);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00001547 EXPECT_POISONED((((uintptr_t)q) & 0xFF) == 0);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001548}
1549
1550static void vaargsfn2(int guard, ...) {
1551 va_list vl;
1552 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001553 EXPECT_NOT_POISONED(va_arg(vl, int));
1554 EXPECT_NOT_POISONED(va_arg(vl, int));
1555 EXPECT_NOT_POISONED(va_arg(vl, int));
1556 EXPECT_POISONED(va_arg(vl, double));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001557 va_end(vl);
1558}
1559
1560static void vaargsfn(int guard, ...) {
1561 va_list vl;
1562 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001563 EXPECT_NOT_POISONED(va_arg(vl, int));
1564 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001565 // The following call will overwrite __msan_param_tls.
1566 // Checks after it test that arg shadow was somehow saved across the call.
1567 vaargsfn2(1, 2, 3, 4, *GetPoisoned<double>());
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001568 EXPECT_NOT_POISONED(va_arg(vl, int));
1569 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001570 va_end(vl);
1571}
1572
1573TEST(MemorySanitizer, VAArgTest) {
1574 int* x = GetPoisoned<int>();
1575 int* y = GetPoisoned<int>(4);
1576 vaargsfn(1, 13, *x, 42, *y);
1577}
1578
1579static void vaargsfn_many(int guard, ...) {
1580 va_list vl;
1581 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001582 EXPECT_NOT_POISONED(va_arg(vl, int));
1583 EXPECT_POISONED(va_arg(vl, int));
1584 EXPECT_NOT_POISONED(va_arg(vl, int));
1585 EXPECT_NOT_POISONED(va_arg(vl, int));
1586 EXPECT_NOT_POISONED(va_arg(vl, int));
1587 EXPECT_NOT_POISONED(va_arg(vl, int));
1588 EXPECT_NOT_POISONED(va_arg(vl, int));
1589 EXPECT_NOT_POISONED(va_arg(vl, int));
1590 EXPECT_NOT_POISONED(va_arg(vl, int));
1591 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001592 va_end(vl);
1593}
1594
1595TEST(MemorySanitizer, VAArgManyTest) {
1596 int* x = GetPoisoned<int>();
1597 int* y = GetPoisoned<int>(4);
1598 vaargsfn_many(1, 2, *x, 3, 4, 5, 6, 7, 8, 9, *y);
1599}
1600
1601static void vaargsfn_pass2(va_list vl) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001602 EXPECT_NOT_POISONED(va_arg(vl, int));
1603 EXPECT_NOT_POISONED(va_arg(vl, int));
1604 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001605}
1606
1607static void vaargsfn_pass(int guard, ...) {
1608 va_list vl;
1609 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001610 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001611 vaargsfn_pass2(vl);
1612 va_end(vl);
1613}
1614
1615TEST(MemorySanitizer, VAArgPass) {
1616 int* x = GetPoisoned<int>();
1617 int* y = GetPoisoned<int>(4);
1618 vaargsfn_pass(1, *x, 2, 3, *y);
1619}
1620
1621static void vaargsfn_copy2(va_list vl) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001622 EXPECT_NOT_POISONED(va_arg(vl, int));
1623 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001624}
1625
1626static void vaargsfn_copy(int guard, ...) {
1627 va_list vl;
1628 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001629 EXPECT_NOT_POISONED(va_arg(vl, int));
1630 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001631 va_list vl2;
1632 va_copy(vl2, vl);
1633 vaargsfn_copy2(vl2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001634 EXPECT_NOT_POISONED(va_arg(vl, int));
1635 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001636 va_end(vl);
1637}
1638
1639TEST(MemorySanitizer, VAArgCopy) {
1640 int* x = GetPoisoned<int>();
1641 int* y = GetPoisoned<int>(4);
1642 vaargsfn_copy(1, 2, *x, 3, *y);
1643}
1644
1645static void vaargsfn_ptr(int guard, ...) {
1646 va_list vl;
1647 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001648 EXPECT_NOT_POISONED(va_arg(vl, int*));
1649 EXPECT_POISONED(va_arg(vl, int*));
1650 EXPECT_NOT_POISONED(va_arg(vl, int*));
1651 EXPECT_POISONED(va_arg(vl, double*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001652 va_end(vl);
1653}
1654
1655TEST(MemorySanitizer, VAArgPtr) {
1656 int** x = GetPoisoned<int*>();
1657 double** y = GetPoisoned<double*>(8);
1658 int z;
1659 vaargsfn_ptr(1, &z, *x, &z, *y);
1660}
1661
1662static void vaargsfn_overflow(int guard, ...) {
1663 va_list vl;
1664 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001665 EXPECT_NOT_POISONED(va_arg(vl, int));
1666 EXPECT_NOT_POISONED(va_arg(vl, int));
1667 EXPECT_POISONED(va_arg(vl, int));
1668 EXPECT_NOT_POISONED(va_arg(vl, int));
1669 EXPECT_NOT_POISONED(va_arg(vl, int));
1670 EXPECT_NOT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001671
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001672 EXPECT_NOT_POISONED(va_arg(vl, double));
1673 EXPECT_NOT_POISONED(va_arg(vl, double));
1674 EXPECT_NOT_POISONED(va_arg(vl, double));
1675 EXPECT_POISONED(va_arg(vl, double));
1676 EXPECT_NOT_POISONED(va_arg(vl, double));
1677 EXPECT_POISONED(va_arg(vl, int*));
1678 EXPECT_NOT_POISONED(va_arg(vl, double));
1679 EXPECT_NOT_POISONED(va_arg(vl, double));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001680
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001681 EXPECT_POISONED(va_arg(vl, int));
1682 EXPECT_POISONED(va_arg(vl, double));
1683 EXPECT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001684
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001685 EXPECT_NOT_POISONED(va_arg(vl, int));
1686 EXPECT_NOT_POISONED(va_arg(vl, double));
1687 EXPECT_NOT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001688
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001689 EXPECT_POISONED(va_arg(vl, int));
1690 EXPECT_POISONED(va_arg(vl, double));
1691 EXPECT_POISONED(va_arg(vl, int*));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001692
1693 va_end(vl);
1694}
1695
1696TEST(MemorySanitizer, VAArgOverflow) {
1697 int* x = GetPoisoned<int>();
1698 double* y = GetPoisoned<double>(8);
1699 int** p = GetPoisoned<int*>(16);
1700 int z;
1701 vaargsfn_overflow(1,
1702 1, 2, *x, 4, 5, 6,
1703 1.1, 2.2, 3.3, *y, 5.5, *p, 7.7, 8.8,
1704 // the following args will overflow for sure
1705 *x, *y, *p,
1706 7, 9.9, &z,
1707 *x, *y, *p);
1708}
1709
1710static void vaargsfn_tlsoverwrite2(int guard, ...) {
1711 va_list vl;
1712 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001713 EXPECT_NOT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001714 va_end(vl);
1715}
1716
1717static void vaargsfn_tlsoverwrite(int guard, ...) {
1718 // This call will overwrite TLS contents unless it's backed up somewhere.
1719 vaargsfn_tlsoverwrite2(2, 42);
1720 va_list vl;
1721 va_start(vl, guard);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001722 EXPECT_POISONED(va_arg(vl, int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001723 va_end(vl);
1724}
1725
1726TEST(MemorySanitizer, VAArgTLSOverwrite) {
1727 int* x = GetPoisoned<int>();
1728 vaargsfn_tlsoverwrite(1, *x);
1729}
1730
1731struct StructByVal {
1732 int a, b, c, d, e, f;
1733};
1734
1735NOINLINE void StructByValTestFunc(struct StructByVal s) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001736 EXPECT_NOT_POISONED(s.a);
1737 EXPECT_POISONED(s.b);
1738 EXPECT_NOT_POISONED(s.c);
1739 EXPECT_POISONED(s.d);
1740 EXPECT_NOT_POISONED(s.e);
1741 EXPECT_POISONED(s.f);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001742}
1743
1744NOINLINE void StructByValTestFunc1(struct StructByVal s) {
1745 StructByValTestFunc(s);
1746}
1747
1748NOINLINE void StructByValTestFunc2(int z, struct StructByVal s) {
1749 StructByValTestFunc(s);
1750}
1751
1752TEST(MemorySanitizer, StructByVal) {
1753 // Large aggregates are passed as "byval" pointer argument in LLVM.
1754 struct StructByVal s;
1755 s.a = 1;
1756 s.b = *GetPoisoned<int>();
1757 s.c = 2;
1758 s.d = *GetPoisoned<int>();
1759 s.e = 3;
1760 s.f = *GetPoisoned<int>();
1761 StructByValTestFunc(s);
1762 StructByValTestFunc1(s);
1763 StructByValTestFunc2(0, s);
1764}
1765
1766
1767#if MSAN_HAS_M128
Evgeniy Stepanovb921bf22013-04-16 14:09:47 +00001768NOINLINE __m128i m128Eq(__m128i *a, __m128i *b) { return _mm_cmpeq_epi16(*a, *b); }
1769NOINLINE __m128i m128Lt(__m128i *a, __m128i *b) { return _mm_cmplt_epi16(*a, *b); }
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001770TEST(MemorySanitizer, m128) {
1771 __m128i a = _mm_set1_epi16(0x1234);
1772 __m128i b = _mm_set1_epi16(0x7890);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001773 EXPECT_NOT_POISONED(m128Eq(&a, &b));
1774 EXPECT_NOT_POISONED(m128Lt(&a, &b));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001775}
1776// FIXME: add more tests for __m128i.
1777#endif // MSAN_HAS_M128
1778
1779// We should not complain when copying this poisoned hole.
1780struct StructWithHole {
1781 U4 a;
1782 // 4-byte hole.
1783 U8 b;
1784};
1785
1786NOINLINE StructWithHole ReturnStructWithHole() {
1787 StructWithHole res;
1788 __msan_poison(&res, sizeof(res));
1789 res.a = 1;
1790 res.b = 2;
1791 return res;
1792}
1793
1794TEST(MemorySanitizer, StructWithHole) {
1795 StructWithHole a = ReturnStructWithHole();
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00001796 break_optimization(&a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001797}
1798
1799template <class T>
1800NOINLINE T ReturnStruct() {
1801 T res;
1802 __msan_poison(&res, sizeof(res));
1803 res.a = 1;
1804 return res;
1805}
1806
1807template <class T>
1808NOINLINE void TestReturnStruct() {
1809 T s1 = ReturnStruct<T>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001810 EXPECT_NOT_POISONED(s1.a);
1811 EXPECT_POISONED(s1.b);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001812}
1813
1814struct SSS1 {
1815 int a, b, c;
1816};
1817struct SSS2 {
1818 int b, a, c;
1819};
1820struct SSS3 {
1821 int b, c, a;
1822};
1823struct SSS4 {
1824 int c, b, a;
1825};
1826
1827struct SSS5 {
1828 int a;
1829 float b;
1830};
1831struct SSS6 {
1832 int a;
1833 double b;
1834};
1835struct SSS7 {
1836 S8 b;
1837 int a;
1838};
1839struct SSS8 {
1840 S2 b;
1841 S8 a;
1842};
1843
1844TEST(MemorySanitizer, IntStruct3) {
1845 TestReturnStruct<SSS1>();
1846 TestReturnStruct<SSS2>();
1847 TestReturnStruct<SSS3>();
1848 TestReturnStruct<SSS4>();
1849 TestReturnStruct<SSS5>();
1850 TestReturnStruct<SSS6>();
1851 TestReturnStruct<SSS7>();
1852 TestReturnStruct<SSS8>();
1853}
1854
1855struct LongStruct {
1856 U1 a1, b1;
1857 U2 a2, b2;
1858 U4 a4, b4;
1859 U8 a8, b8;
1860};
1861
1862NOINLINE LongStruct ReturnLongStruct1() {
1863 LongStruct res;
1864 __msan_poison(&res, sizeof(res));
1865 res.a1 = res.a2 = res.a4 = res.a8 = 111;
1866 // leaves b1, .., b8 poisoned.
1867 return res;
1868}
1869
1870NOINLINE LongStruct ReturnLongStruct2() {
1871 LongStruct res;
1872 __msan_poison(&res, sizeof(res));
1873 res.b1 = res.b2 = res.b4 = res.b8 = 111;
1874 // leaves a1, .., a8 poisoned.
1875 return res;
1876}
1877
1878TEST(MemorySanitizer, LongStruct) {
1879 LongStruct s1 = ReturnLongStruct1();
1880 __msan_print_shadow(&s1, sizeof(s1));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001881 EXPECT_NOT_POISONED(s1.a1);
1882 EXPECT_NOT_POISONED(s1.a2);
1883 EXPECT_NOT_POISONED(s1.a4);
1884 EXPECT_NOT_POISONED(s1.a8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001885
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001886 EXPECT_POISONED(s1.b1);
1887 EXPECT_POISONED(s1.b2);
1888 EXPECT_POISONED(s1.b4);
1889 EXPECT_POISONED(s1.b8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001890
1891 LongStruct s2 = ReturnLongStruct2();
1892 __msan_print_shadow(&s2, sizeof(s2));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001893 EXPECT_NOT_POISONED(s2.b1);
1894 EXPECT_NOT_POISONED(s2.b2);
1895 EXPECT_NOT_POISONED(s2.b4);
1896 EXPECT_NOT_POISONED(s2.b8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001897
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001898 EXPECT_POISONED(s2.a1);
1899 EXPECT_POISONED(s2.a2);
1900 EXPECT_POISONED(s2.a4);
1901 EXPECT_POISONED(s2.a8);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001902}
1903
1904TEST(MemorySanitizer, getrlimit) {
1905 struct rlimit limit;
1906 __msan_poison(&limit, sizeof(limit));
1907 int result = getrlimit(RLIMIT_DATA, &limit);
1908 assert(result == 0);
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001909 EXPECT_NOT_POISONED(limit.rlim_cur);
1910 EXPECT_NOT_POISONED(limit.rlim_max);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00001911}
1912
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001913TEST(MemorySanitizer, getrusage) {
1914 struct rusage usage;
1915 __msan_poison(&usage, sizeof(usage));
1916 int result = getrusage(RUSAGE_SELF, &usage);
1917 assert(result == 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001918 EXPECT_NOT_POISONED(usage.ru_utime.tv_sec);
1919 EXPECT_NOT_POISONED(usage.ru_utime.tv_usec);
1920 EXPECT_NOT_POISONED(usage.ru_stime.tv_sec);
1921 EXPECT_NOT_POISONED(usage.ru_stime.tv_usec);
1922 EXPECT_NOT_POISONED(usage.ru_maxrss);
1923 EXPECT_NOT_POISONED(usage.ru_minflt);
1924 EXPECT_NOT_POISONED(usage.ru_majflt);
1925 EXPECT_NOT_POISONED(usage.ru_inblock);
1926 EXPECT_NOT_POISONED(usage.ru_oublock);
1927 EXPECT_NOT_POISONED(usage.ru_nvcsw);
1928 EXPECT_NOT_POISONED(usage.ru_nivcsw);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001929}
1930
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +00001931#ifdef __GLIBC__
Evgeniy Stepanove6c62f22013-05-21 08:56:22 +00001932extern char *program_invocation_name;
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +00001933#else // __GLIBC__
1934# error "TODO: port this"
1935#endif
1936
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001937static void dladdr_testfn() {}
1938
1939TEST(MemorySanitizer, dladdr) {
1940 Dl_info info;
1941 __msan_poison(&info, sizeof(info));
1942 int result = dladdr((const void*)dladdr_testfn, &info);
1943 assert(result != 0);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001944 EXPECT_NOT_POISONED((unsigned long)info.dli_fname);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001945 if (info.dli_fname)
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001946 EXPECT_NOT_POISONED(strlen(info.dli_fname));
1947 EXPECT_NOT_POISONED((unsigned long)info.dli_fbase);
1948 EXPECT_NOT_POISONED((unsigned long)info.dli_sname);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001949 if (info.dli_sname)
Evgeniy Stepanov11929002013-01-22 12:29:00 +00001950 EXPECT_NOT_POISONED(strlen(info.dli_sname));
1951 EXPECT_NOT_POISONED((unsigned long)info.dli_saddr);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001952}
1953
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001954static int dl_phdr_callback(struct dl_phdr_info *info, size_t size, void *data) {
1955 (*(int *)data)++;
1956 EXPECT_NOT_POISONED(info->dlpi_addr);
1957 EXPECT_NOT_POISONED(strlen(info->dlpi_name));
1958 EXPECT_NOT_POISONED(info->dlpi_phnum);
1959 for (int i = 0; i < info->dlpi_phnum; ++i)
1960 EXPECT_NOT_POISONED(info->dlpi_phdr[i]);
1961 return 0;
1962}
1963
Evgeniy Stepanov263800b2013-06-27 13:21:00 +00001964#ifndef MSAN_TEST_DISABLE_DLOPEN
1965
1966// Compute the path to our loadable DSO. We assume it's in the same
1967// directory. Only use string routines that we intercept so far to do this.
1968static int PathToLoadable(char *buf, size_t sz) {
1969 const char *basename = "libmsan_loadable.x86_64.so";
1970 char *argv0 = program_invocation_name;
1971 char *last_slash = strrchr(argv0, '/');
1972 assert(last_slash);
1973 int res =
1974 snprintf(buf, sz, "%.*s/%s", int(last_slash - argv0), argv0, basename);
1975 return res < sz ? 0 : res;
1976}
1977
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001978TEST(MemorySanitizer, dl_iterate_phdr) {
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +00001979 char path[4096];
1980 int res = PathToLoadable(path, sizeof(path));
1981 assert(!res);
1982
1983 // Having at least one dlopen'ed library in the process makes this more
1984 // entertaining.
1985 void *lib = dlopen(path, RTLD_LAZY);
1986 ASSERT_NE((void*)0, lib);
1987
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001988 int count = 0;
1989 int result = dl_iterate_phdr(dl_phdr_callback, &count);
1990 assert(count > 0);
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +00001991
1992 dlclose(lib);
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001993}
1994
Reid Kleckner0f92deb2013-03-11 18:07:42 +00001995
1996TEST(MemorySanitizer, dlopen) {
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +00001997 char path[4096];
1998 int res = PathToLoadable(path, sizeof(path));
1999 assert(!res);
Reid Kleckner0f92deb2013-03-11 18:07:42 +00002000
2001 // We need to clear shadow for globals when doing dlopen. In order to test
2002 // this, we have to poison the shadow for the DSO before we load it. In
2003 // general this is difficult, but the loader tends to reload things in the
2004 // same place, so we open, close, and then reopen. The global should always
2005 // start out clean after dlopen.
2006 for (int i = 0; i < 2; i++) {
2007 void *lib = dlopen(path, RTLD_LAZY);
2008 if (lib == NULL) {
2009 printf("dlerror: %s\n", dlerror());
2010 assert(lib != NULL);
2011 }
2012 void **(*get_dso_global)() = (void **(*)())dlsym(lib, "get_dso_global");
2013 assert(get_dso_global);
2014 void **dso_global = get_dso_global();
2015 EXPECT_NOT_POISONED(*dso_global);
2016 __msan_poison(dso_global, sizeof(*dso_global));
2017 EXPECT_POISONED(*dso_global);
2018 dlclose(lib);
2019 }
Reid Kleckner0f92deb2013-03-11 18:07:42 +00002020}
Evgeniy Stepanov6c503b92013-03-22 11:59:49 +00002021
2022// Regression test for a crash in dlopen() interceptor.
2023TEST(MemorySanitizer, dlopenFailed) {
2024 const char *path = "/libmsan_loadable_does_not_exist.x86_64.so";
2025 void *lib = dlopen(path, RTLD_LAZY);
2026 ASSERT_EQ(0, lib);
2027}
Reid Kleckner0f92deb2013-03-11 18:07:42 +00002028
Evgeniy Stepanov263800b2013-06-27 13:21:00 +00002029#endif // MSAN_TEST_DISABLE_DLOPEN
2030
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002031TEST(MemorySanitizer, scanf) {
2032 const char *input = "42 hello";
2033 int* d = new int;
2034 char* s = new char[7];
2035 int res = sscanf(input, "%d %5s", d, s);
2036 printf("res %d\n", res);
2037 assert(res == 2);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002038 EXPECT_NOT_POISONED(*d);
2039 EXPECT_NOT_POISONED(s[0]);
2040 EXPECT_NOT_POISONED(s[1]);
2041 EXPECT_NOT_POISONED(s[2]);
2042 EXPECT_NOT_POISONED(s[3]);
2043 EXPECT_NOT_POISONED(s[4]);
2044 EXPECT_NOT_POISONED(s[5]);
2045 EXPECT_POISONED(s[6]);
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002046 delete s;
2047 delete d;
2048}
2049
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002050static void *SimpleThread_threadfn(void* data) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002051 return new int;
2052}
2053
2054TEST(MemorySanitizer, SimpleThread) {
2055 pthread_t t;
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002056 void *p;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002057 int res = pthread_create(&t, NULL, SimpleThread_threadfn, NULL);
2058 assert(!res);
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00002059 EXPECT_NOT_POISONED(t);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002060 res = pthread_join(t, &p);
2061 assert(!res);
2062 if (!__msan_has_dynamic_component()) // FIXME: intercept pthread_join (?).
2063 __msan_unpoison(&p, sizeof(p));
2064 delete (int*)p;
2065}
2066
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002067static void *SmallStackThread_threadfn(void* data) {
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00002068 return 0;
2069}
2070
2071TEST(MemorySanitizer, SmallStackThread) {
2072 pthread_attr_t attr;
2073 pthread_t t;
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002074 void *p;
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00002075 int res;
2076 res = pthread_attr_init(&attr);
2077 ASSERT_EQ(0, res);
2078 res = pthread_attr_setstacksize(&attr, 64 * 1024);
2079 ASSERT_EQ(0, res);
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002080 res = pthread_create(&t, &attr, SmallStackThread_threadfn, NULL);
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00002081 ASSERT_EQ(0, res);
2082 res = pthread_join(t, &p);
2083 ASSERT_EQ(0, res);
2084 res = pthread_attr_destroy(&attr);
2085 ASSERT_EQ(0, res);
2086}
2087
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00002088TEST(MemorySanitizer, PreAllocatedStackThread) {
2089 pthread_attr_t attr;
2090 pthread_t t;
2091 int res;
2092 res = pthread_attr_init(&attr);
2093 ASSERT_EQ(0, res);
2094 void *stack;
2095 const size_t kStackSize = 64 * 1024;
2096 res = posix_memalign(&stack, 4096, kStackSize);
2097 ASSERT_EQ(0, res);
2098 res = pthread_attr_setstack(&attr, stack, kStackSize);
2099 ASSERT_EQ(0, res);
2100 // A small self-allocated stack can not be extended by the tool.
2101 // In this case pthread_create is expected to fail.
2102 res = pthread_create(&t, &attr, SmallStackThread_threadfn, NULL);
2103 EXPECT_NE(0, res);
2104 res = pthread_attr_destroy(&attr);
2105 ASSERT_EQ(0, res);
2106}
2107
Evgeniy Stepanov56d34722013-05-21 08:12:08 +00002108TEST(MemorySanitizer, pthread_getschedparam) {
2109 int policy;
2110 struct sched_param param;
2111 int res = pthread_getschedparam(pthread_self(), &policy, &param);
2112 ASSERT_EQ(0, res);
2113 EXPECT_NOT_POISONED(policy);
2114 EXPECT_NOT_POISONED(param.sched_priority);
2115}
2116
Evgeniy Stepanov737da2f2013-07-05 12:31:07 +00002117TEST(MemorySanitizer, pthread_key_create) {
2118 pthread_key_t key;
2119 int res = pthread_key_create(&key, NULL);
2120 assert(!res);
2121 EXPECT_NOT_POISONED(key);
2122 res = pthread_key_delete(key);
2123 assert(!res);
2124}
2125
Evgeniy Stepanovb6c8e472013-04-23 13:27:36 +00002126TEST(MemorySanitizer, posix_memalign) {
2127 void *p;
2128 EXPECT_POISONED(p);
2129 int res = posix_memalign(&p, 4096, 13);
2130 ASSERT_EQ(0, res);
2131 EXPECT_NOT_POISONED(p);
2132 free(p);
2133}
2134
Evgeniy Stepanov9530eb72013-04-23 14:05:15 +00002135TEST(MemorySanitizer, inet_pton) {
2136 const char *s = "1:0:0:0:0:0:0:8";
2137 unsigned char buf[sizeof(struct in6_addr)];
2138 int res = inet_pton(AF_INET6, s, buf);
2139 ASSERT_EQ(1, res);
2140 EXPECT_NOT_POISONED(buf[0]);
2141 EXPECT_NOT_POISONED(buf[sizeof(struct in6_addr) - 1]);
2142
2143 char s_out[INET6_ADDRSTRLEN];
2144 EXPECT_POISONED(s_out[3]);
2145 const char *q = inet_ntop(AF_INET6, buf, s_out, INET6_ADDRSTRLEN);
2146 ASSERT_NE((void*)0, q);
2147 EXPECT_NOT_POISONED(s_out[3]);
2148}
2149
Evgeniy Stepanov9d600872013-06-24 13:56:14 +00002150TEST(MemorySanitizer, inet_aton) {
2151 const char *s = "127.0.0.1";
2152 struct in_addr in[2];
2153 int res = inet_aton(s, in);
2154 ASSERT_NE(0, res);
2155 EXPECT_NOT_POISONED(in[0]);
2156 EXPECT_POISONED(*(char *)(in + 1));
2157}
2158
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002159TEST(MemorySanitizer, uname) {
2160 struct utsname u;
2161 int res = uname(&u);
2162 assert(!res);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002163 EXPECT_NOT_POISONED(strlen(u.sysname));
2164 EXPECT_NOT_POISONED(strlen(u.nodename));
2165 EXPECT_NOT_POISONED(strlen(u.release));
2166 EXPECT_NOT_POISONED(strlen(u.version));
2167 EXPECT_NOT_POISONED(strlen(u.machine));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002168}
2169
Evgeniy Stepanov95d05882013-01-23 10:43:38 +00002170TEST(MemorySanitizer, gethostname) {
2171 char buf[100];
2172 int res = gethostname(buf, 100);
2173 assert(!res);
2174 EXPECT_NOT_POISONED(strlen(buf));
2175}
2176
Evgeniy Stepanov359d7fc2013-06-24 14:25:33 +00002177TEST(MemorySanitizer, sysinfo) {
2178 struct sysinfo info;
2179 int res = sysinfo(&info);
2180 assert(!res);
2181 EXPECT_NOT_POISONED(info);
2182}
2183
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00002184TEST(MemorySanitizer, getpwuid) {
2185 struct passwd *p = getpwuid(0); // root
2186 assert(p);
2187 EXPECT_NOT_POISONED(p->pw_name);
2188 assert(p->pw_name);
2189 EXPECT_NOT_POISONED(p->pw_name[0]);
2190 EXPECT_NOT_POISONED(p->pw_uid);
2191 assert(p->pw_uid == 0);
2192}
2193
2194TEST(MemorySanitizer, getpwnam_r) {
2195 struct passwd pwd;
2196 struct passwd *pwdres;
2197 char buf[10000];
2198 int res = getpwnam_r("root", &pwd, buf, sizeof(buf), &pwdres);
2199 assert(!res);
2200 EXPECT_NOT_POISONED(pwd.pw_name);
2201 assert(pwd.pw_name);
2202 EXPECT_NOT_POISONED(pwd.pw_name[0]);
2203 EXPECT_NOT_POISONED(pwd.pw_uid);
2204 assert(pwd.pw_uid == 0);
2205}
2206
2207TEST(MemorySanitizer, getpwnam_r_positive) {
2208 struct passwd pwd;
2209 struct passwd *pwdres;
2210 char s[5];
2211 strncpy(s, "abcd", 5);
2212 __msan_poison(s, 5);
2213 char buf[10000];
2214 int res;
2215 EXPECT_UMR(res = getpwnam_r(s, &pwd, buf, sizeof(buf), &pwdres));
2216}
2217
Evgeniy Stepanov103a63e2013-04-23 12:01:20 +00002218TEST(MemorySanitizer, getgrnam_r) {
2219 struct group grp;
2220 struct group *grpres;
2221 char buf[10000];
2222 int res = getgrnam_r("root", &grp, buf, sizeof(buf), &grpres);
2223 assert(!res);
2224 EXPECT_NOT_POISONED(grp.gr_name);
2225 assert(grp.gr_name);
2226 EXPECT_NOT_POISONED(grp.gr_name[0]);
2227 EXPECT_NOT_POISONED(grp.gr_gid);
2228}
2229
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002230template<class T>
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002231static bool applySlt(T value, T shadow) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002232 __msan_partial_poison(&value, &shadow, sizeof(T));
2233 volatile bool zzz = true;
2234 // This "|| zzz" trick somehow makes LLVM emit "icmp slt" instead of
2235 // a shift-and-trunc to get at the highest bit.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002236 volatile bool v = value < 0 || zzz;
2237 return v;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002238}
2239
2240TEST(MemorySanitizer, SignedCompareWithZero) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002241 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xF));
2242 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFF));
2243 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0xFFFFFF));
2244 EXPECT_NOT_POISONED(applySlt<S4>(0xF, 0x7FFFFFF));
2245 EXPECT_UMR(applySlt<S4>(0xF, 0x80FFFFFF));
2246 EXPECT_UMR(applySlt<S4>(0xF, 0xFFFFFFFF));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002247}
2248
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00002249template <class T, class S>
2250static T poisoned(T Va, S Sa) {
2251 char SIZE_CHECK1[(ssize_t)sizeof(T) - (ssize_t)sizeof(S)];
2252 char SIZE_CHECK2[(ssize_t)sizeof(S) - (ssize_t)sizeof(T)];
2253 T a;
2254 a = Va;
2255 __msan_partial_poison(&a, &Sa, sizeof(T));
2256 return a;
2257}
2258
2259TEST(MemorySanitizer, ICmpRelational) {
2260 EXPECT_NOT_POISONED(poisoned(0, 0) < poisoned(0, 0));
2261 EXPECT_NOT_POISONED(poisoned(0U, 0) < poisoned(0U, 0));
2262 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) < poisoned(0LL, 0LLU));
2263 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) < poisoned(0LLU, 0LLU));
2264 EXPECT_POISONED(poisoned(0xFF, 0xFF) < poisoned(0xFF, 0xFF));
2265 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <
2266 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
2267 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <
2268 poisoned(-1, 0xFFFFFFFFU));
2269
2270 EXPECT_NOT_POISONED(poisoned(0, 0) <= poisoned(0, 0));
2271 EXPECT_NOT_POISONED(poisoned(0U, 0) <= poisoned(0U, 0));
2272 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) <= poisoned(0LL, 0LLU));
2273 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) <= poisoned(0LLU, 0LLU));
2274 EXPECT_POISONED(poisoned(0xFF, 0xFF) <= poisoned(0xFF, 0xFF));
2275 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) <=
2276 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
2277 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) <=
2278 poisoned(-1, 0xFFFFFFFFU));
2279
2280 EXPECT_NOT_POISONED(poisoned(0, 0) > poisoned(0, 0));
2281 EXPECT_NOT_POISONED(poisoned(0U, 0) > poisoned(0U, 0));
2282 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) > poisoned(0LL, 0LLU));
2283 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) > poisoned(0LLU, 0LLU));
2284 EXPECT_POISONED(poisoned(0xFF, 0xFF) > poisoned(0xFF, 0xFF));
2285 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >
2286 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
2287 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >
2288 poisoned(-1, 0xFFFFFFFFU));
2289
2290 EXPECT_NOT_POISONED(poisoned(0, 0) >= poisoned(0, 0));
2291 EXPECT_NOT_POISONED(poisoned(0U, 0) >= poisoned(0U, 0));
2292 EXPECT_NOT_POISONED(poisoned(0LL, 0LLU) >= poisoned(0LL, 0LLU));
2293 EXPECT_NOT_POISONED(poisoned(0LLU, 0LLU) >= poisoned(0LLU, 0LLU));
2294 EXPECT_POISONED(poisoned(0xFF, 0xFF) >= poisoned(0xFF, 0xFF));
2295 EXPECT_POISONED(poisoned(0xFFFFFFFFU, 0xFFFFFFFFU) >=
2296 poisoned(0xFFFFFFFFU, 0xFFFFFFFFU));
2297 EXPECT_POISONED(poisoned(-1, 0xFFFFFFFFU) >=
2298 poisoned(-1, 0xFFFFFFFFU));
2299
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00002300 EXPECT_POISONED(poisoned(6, 0xF) > poisoned(7, 0));
2301 EXPECT_POISONED(poisoned(0xF, 0xF) > poisoned(7, 0));
2302
2303 EXPECT_NOT_POISONED(poisoned(-1, 0x80000000U) >= poisoned(-1, 0U));
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00002304}
2305
2306#if MSAN_HAS_M128
2307TEST(MemorySanitizer, ICmpVectorRelational) {
Evgeniy Stepanovb921bf22013-04-16 14:09:47 +00002308 EXPECT_NOT_POISONED(
2309 _mm_cmplt_epi16(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0)),
2310 poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0))));
2311 EXPECT_NOT_POISONED(
2312 _mm_cmplt_epi16(poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0)),
2313 poisoned(_mm_set1_epi32(0), _mm_set1_epi32(0))));
2314 EXPECT_POISONED(
2315 _mm_cmplt_epi16(poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF)),
2316 poisoned(_mm_set1_epi16(0), _mm_set1_epi16(0xFFFF))));
2317 EXPECT_POISONED(_mm_cmpgt_epi16(poisoned(_mm_set1_epi16(6), _mm_set1_epi16(0xF)),
2318 poisoned(_mm_set1_epi16(7), _mm_set1_epi16(0))));
Evgeniy Stepanov9a22a3d2013-01-25 15:39:11 +00002319}
2320#endif
2321
Evgeniy Stepanov2efa1422013-01-22 12:31:39 +00002322// Volatile bitfield store is implemented as load-mask-store
2323// Test that we don't warn on the store of (uninitialized) padding.
2324struct VolatileBitfieldStruct {
2325 volatile unsigned x : 1;
2326 unsigned y : 1;
2327};
2328
2329TEST(MemorySanitizer, VolatileBitfield) {
2330 VolatileBitfieldStruct *S = new VolatileBitfieldStruct;
2331 S->x = 1;
Evgeniy Stepanov02f4a942013-01-22 12:33:11 +00002332 EXPECT_NOT_POISONED((unsigned)S->x);
2333 EXPECT_POISONED((unsigned)S->y);
Evgeniy Stepanov2efa1422013-01-22 12:31:39 +00002334}
2335
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +00002336TEST(MemorySanitizer, UnalignedLoad) {
2337 char x[32];
2338 memset(x + 8, 0, 16);
2339 EXPECT_POISONED(__sanitizer_unaligned_load16(x+6));
2340 EXPECT_POISONED(__sanitizer_unaligned_load16(x+7));
2341 EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+8));
2342 EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+9));
2343 EXPECT_NOT_POISONED(__sanitizer_unaligned_load16(x+22));
2344 EXPECT_POISONED(__sanitizer_unaligned_load16(x+23));
2345 EXPECT_POISONED(__sanitizer_unaligned_load16(x+24));
2346
2347 EXPECT_POISONED(__sanitizer_unaligned_load32(x+4));
2348 EXPECT_POISONED(__sanitizer_unaligned_load32(x+7));
2349 EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+8));
2350 EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+9));
2351 EXPECT_NOT_POISONED(__sanitizer_unaligned_load32(x+20));
2352 EXPECT_POISONED(__sanitizer_unaligned_load32(x+21));
2353 EXPECT_POISONED(__sanitizer_unaligned_load32(x+24));
2354
2355 EXPECT_POISONED(__sanitizer_unaligned_load64(x));
2356 EXPECT_POISONED(__sanitizer_unaligned_load64(x+1));
2357 EXPECT_POISONED(__sanitizer_unaligned_load64(x+7));
2358 EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+8));
2359 EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+9));
2360 EXPECT_NOT_POISONED(__sanitizer_unaligned_load64(x+16));
2361 EXPECT_POISONED(__sanitizer_unaligned_load64(x+17));
2362 EXPECT_POISONED(__sanitizer_unaligned_load64(x+21));
2363 EXPECT_POISONED(__sanitizer_unaligned_load64(x+24));
2364}
2365
2366TEST(MemorySanitizer, UnalignedStore16) {
2367 char x[5];
2368 U2 y = 0;
2369 __msan_poison(&y, 1);
2370 __sanitizer_unaligned_store16(x + 1, y);
2371 EXPECT_POISONED(x[0]);
2372 EXPECT_POISONED(x[1]);
2373 EXPECT_NOT_POISONED(x[2]);
2374 EXPECT_POISONED(x[3]);
2375 EXPECT_POISONED(x[4]);
2376}
2377
2378TEST(MemorySanitizer, UnalignedStore32) {
2379 char x[8];
2380 U4 y4 = 0;
2381 __msan_poison(&y4, 2);
2382 __sanitizer_unaligned_store32(x+3, y4);
2383 EXPECT_POISONED(x[0]);
2384 EXPECT_POISONED(x[1]);
2385 EXPECT_POISONED(x[2]);
2386 EXPECT_POISONED(x[3]);
2387 EXPECT_POISONED(x[4]);
2388 EXPECT_NOT_POISONED(x[5]);
2389 EXPECT_NOT_POISONED(x[6]);
2390 EXPECT_POISONED(x[7]);
2391}
2392
2393TEST(MemorySanitizer, UnalignedStore64) {
2394 char x[16];
2395 U8 y = 0;
2396 __msan_poison(&y, 3);
2397 __msan_poison(((char *)&y) + sizeof(y) - 2, 1);
2398 __sanitizer_unaligned_store64(x+3, y);
2399 EXPECT_POISONED(x[0]);
2400 EXPECT_POISONED(x[1]);
2401 EXPECT_POISONED(x[2]);
2402 EXPECT_POISONED(x[3]);
2403 EXPECT_POISONED(x[4]);
2404 EXPECT_POISONED(x[5]);
2405 EXPECT_NOT_POISONED(x[6]);
2406 EXPECT_NOT_POISONED(x[7]);
2407 EXPECT_NOT_POISONED(x[8]);
2408 EXPECT_POISONED(x[9]);
2409 EXPECT_NOT_POISONED(x[10]);
2410 EXPECT_POISONED(x[11]);
2411}
2412
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002413TEST(MemorySanitizerDr, StoreInDSOTest) {
2414 if (!__msan_has_dynamic_component()) return;
2415 char* s = new char[10];
2416 dso_memfill(s, 9);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002417 EXPECT_NOT_POISONED(s[5]);
2418 EXPECT_POISONED(s[9]);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002419}
2420
2421int return_poisoned_int() {
2422 return ReturnPoisoned<U8>();
2423}
2424
2425TEST(MemorySanitizerDr, ReturnFromDSOTest) {
2426 if (!__msan_has_dynamic_component()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002427 EXPECT_NOT_POISONED(dso_callfn(return_poisoned_int));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002428}
2429
2430NOINLINE int TrashParamTLS(long long x, long long y, long long z) { //NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002431 EXPECT_POISONED(x);
2432 EXPECT_POISONED(y);
2433 EXPECT_POISONED(z);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002434 return 0;
2435}
2436
2437static int CheckParamTLS(long long x, long long y, long long z) { //NOLINT
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002438 EXPECT_NOT_POISONED(x);
2439 EXPECT_NOT_POISONED(y);
2440 EXPECT_NOT_POISONED(z);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002441 return 0;
2442}
2443
2444TEST(MemorySanitizerDr, CallFromDSOTest) {
2445 if (!__msan_has_dynamic_component()) return;
2446 S8* x = GetPoisoned<S8>();
2447 S8* y = GetPoisoned<S8>();
2448 S8* z = GetPoisoned<S8>();
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002449 EXPECT_NOT_POISONED(TrashParamTLS(*x, *y, *z));
2450 EXPECT_NOT_POISONED(dso_callfn1(CheckParamTLS));
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002451}
2452
2453static void StackStoreInDSOFn(int* x, int* y) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002454 EXPECT_NOT_POISONED(*x);
2455 EXPECT_NOT_POISONED(*y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002456}
2457
2458TEST(MemorySanitizerDr, StackStoreInDSOTest) {
2459 if (!__msan_has_dynamic_component()) return;
2460 dso_stack_store(StackStoreInDSOFn, 1);
2461}
2462
2463TEST(MemorySanitizerOrigins, SetGet) {
2464 EXPECT_EQ(TrackingOrigins(), __msan_get_track_origins());
2465 if (!TrackingOrigins()) return;
2466 int x;
2467 __msan_set_origin(&x, sizeof(x), 1234);
2468 EXPECT_EQ(1234, __msan_get_origin(&x));
2469 __msan_set_origin(&x, sizeof(x), 5678);
2470 EXPECT_EQ(5678, __msan_get_origin(&x));
2471 __msan_set_origin(&x, sizeof(x), 0);
2472 EXPECT_EQ(0, __msan_get_origin(&x));
2473}
2474
2475namespace {
2476struct S {
2477 U4 dummy;
2478 U2 a;
2479 U2 b;
2480};
2481
2482// http://code.google.com/p/memory-sanitizer/issues/detail?id=6
2483TEST(MemorySanitizerOrigins, DISABLED_InitializedStoreDoesNotChangeOrigin) {
2484 if (!TrackingOrigins()) return;
2485
2486 S s;
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002487 U4 origin = rand(); // NOLINT
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002488 s.a = *GetPoisonedO<U2>(0, origin);
2489 EXPECT_EQ(origin, __msan_get_origin(&s.a));
2490 EXPECT_EQ(origin, __msan_get_origin(&s.b));
2491
2492 s.b = 42;
2493 EXPECT_EQ(origin, __msan_get_origin(&s.a));
2494 EXPECT_EQ(origin, __msan_get_origin(&s.b));
2495}
2496} // namespace
2497
2498template<class T, class BinaryOp>
2499INLINE
2500void BinaryOpOriginTest(BinaryOp op) {
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002501 U4 ox = rand(); //NOLINT
2502 U4 oy = rand(); //NOLINT
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002503 T *x = GetPoisonedO<T>(0, ox, 0);
2504 T *y = GetPoisonedO<T>(1, oy, 0);
2505 T *z = GetPoisonedO<T>(2, 0, 0);
2506
2507 *z = op(*x, *y);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002508 U4 origin = __msan_get_origin(z);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002509 EXPECT_POISONED_O(*z, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002510 EXPECT_EQ(true, origin == ox || origin == oy);
2511
2512 // y is poisoned, x is not.
2513 *x = 10101;
2514 *y = *GetPoisonedO<T>(1, oy);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002515 break_optimization(x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002516 __msan_set_origin(z, sizeof(*z), 0);
2517 *z = op(*x, *y);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002518 EXPECT_POISONED_O(*z, oy);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002519 EXPECT_EQ(__msan_get_origin(z), oy);
2520
2521 // x is poisoned, y is not.
2522 *x = *GetPoisonedO<T>(0, ox);
2523 *y = 10101010;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002524 break_optimization(y);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002525 __msan_set_origin(z, sizeof(*z), 0);
2526 *z = op(*x, *y);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002527 EXPECT_POISONED_O(*z, ox);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002528 EXPECT_EQ(__msan_get_origin(z), ox);
2529}
2530
2531template<class T> INLINE T XOR(const T &a, const T&b) { return a ^ b; }
2532template<class T> INLINE T ADD(const T &a, const T&b) { return a + b; }
2533template<class T> INLINE T SUB(const T &a, const T&b) { return a - b; }
2534template<class T> INLINE T MUL(const T &a, const T&b) { return a * b; }
2535template<class T> INLINE T AND(const T &a, const T&b) { return a & b; }
2536template<class T> INLINE T OR (const T &a, const T&b) { return a | b; }
2537
2538TEST(MemorySanitizerOrigins, BinaryOp) {
2539 if (!TrackingOrigins()) return;
2540 BinaryOpOriginTest<S8>(XOR<S8>);
2541 BinaryOpOriginTest<U8>(ADD<U8>);
2542 BinaryOpOriginTest<S4>(SUB<S4>);
2543 BinaryOpOriginTest<S4>(MUL<S4>);
2544 BinaryOpOriginTest<U4>(OR<U4>);
2545 BinaryOpOriginTest<U4>(AND<U4>);
2546 BinaryOpOriginTest<double>(ADD<U4>);
2547 BinaryOpOriginTest<float>(ADD<S4>);
2548 BinaryOpOriginTest<double>(ADD<double>);
2549 BinaryOpOriginTest<float>(ADD<double>);
2550}
2551
2552TEST(MemorySanitizerOrigins, Unary) {
2553 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002554 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
2555 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
2556 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
2557 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002558
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002559 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2560 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2561 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2562 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002563
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002564 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
2565 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
2566 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
2567 EXPECT_POISONED_O(*GetPoisonedO<U4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002568
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002569 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2570 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2571 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
2572 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002573
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002574 EXPECT_POISONED_O((void*)*GetPoisonedO<S8>(0, __LINE__), __LINE__);
2575 EXPECT_POISONED_O((U8)*GetPoisonedO<void*>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002576}
2577
2578TEST(MemorySanitizerOrigins, EQ) {
2579 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002580 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) <= 11, __LINE__);
2581 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__) == 11, __LINE__);
2582 EXPECT_POISONED_O(*GetPoisonedO<float>(0, __LINE__) == 1.1, __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002583}
2584
2585TEST(MemorySanitizerOrigins, DIV) {
2586 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002587 EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) / 100, __LINE__);
2588 unsigned o = __LINE__;
2589 EXPECT_UMR_O(volatile unsigned y = 100 / *GetPoisonedO<S4>(0, o, 1), o);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002590}
2591
2592TEST(MemorySanitizerOrigins, SHIFT) {
2593 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002594 EXPECT_POISONED_O(*GetPoisonedO<U8>(0, __LINE__) >> 10, __LINE__);
2595 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) >> 10, __LINE__);
2596 EXPECT_POISONED_O(*GetPoisonedO<S8>(0, __LINE__) << 10, __LINE__);
2597 EXPECT_POISONED_O(10U << *GetPoisonedO<U8>(0, __LINE__), __LINE__);
2598 EXPECT_POISONED_O(-10 >> *GetPoisonedO<S8>(0, __LINE__), __LINE__);
2599 EXPECT_POISONED_O(-10 << *GetPoisonedO<S8>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002600}
2601
2602template<class T, int N>
2603void MemCpyTest() {
2604 int ox = __LINE__;
2605 T *x = new T[N];
2606 T *y = new T[N];
2607 T *z = new T[N];
Evgeniy Stepanov353c9962013-07-02 14:49:24 +00002608 T *q = new T[N];
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002609 __msan_poison(x, N * sizeof(T));
2610 __msan_set_origin(x, N * sizeof(T), ox);
2611 __msan_set_origin(y, N * sizeof(T), 777777);
2612 __msan_set_origin(z, N * sizeof(T), 888888);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002613 EXPECT_NOT_POISONED(x);
2614 memcpy(y, x, N * sizeof(T));
2615 EXPECT_POISONED_O(y[0], ox);
2616 EXPECT_POISONED_O(y[N/2], ox);
2617 EXPECT_POISONED_O(y[N-1], ox);
2618 EXPECT_NOT_POISONED(x);
Evgeniy Stepanov353c9962013-07-02 14:49:24 +00002619 void *res = mempcpy(q, x, N * sizeof(T));
2620 ASSERT_EQ(q + N, res);
2621 EXPECT_POISONED_O(q[0], ox);
2622 EXPECT_POISONED_O(q[N/2], ox);
2623 EXPECT_POISONED_O(q[N-1], ox);
2624 EXPECT_NOT_POISONED(x);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002625 memmove(z, x, N * sizeof(T));
2626 EXPECT_POISONED_O(z[0], ox);
2627 EXPECT_POISONED_O(z[N/2], ox);
2628 EXPECT_POISONED_O(z[N-1], ox);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002629}
2630
2631TEST(MemorySanitizerOrigins, LargeMemCpy) {
2632 if (!TrackingOrigins()) return;
2633 MemCpyTest<U1, 10000>();
2634 MemCpyTest<U8, 10000>();
2635}
2636
2637TEST(MemorySanitizerOrigins, SmallMemCpy) {
2638 if (!TrackingOrigins()) return;
2639 MemCpyTest<U8, 1>();
2640 MemCpyTest<U8, 2>();
2641 MemCpyTest<U8, 3>();
2642}
2643
2644TEST(MemorySanitizerOrigins, Select) {
2645 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002646 EXPECT_NOT_POISONED(g_one ? 1 : *GetPoisonedO<S4>(0, __LINE__));
2647 EXPECT_POISONED_O(*GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002648 S4 x;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002649 break_optimization(&x);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002650 x = g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 0;
2651
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002652 EXPECT_POISONED_O(g_1 ? *GetPoisonedO<S4>(0, __LINE__) : 1, __LINE__);
2653 EXPECT_POISONED_O(g_0 ? 1 : *GetPoisonedO<S4>(0, __LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002654}
2655
2656extern "C"
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002657NOINLINE char AllocaTO() {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002658 int ar[100];
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002659 break_optimization(ar);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002660 return ar[10];
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002661 // fprintf(stderr, "Descr: %s\n",
2662 // __msan_get_origin_descr_if_stack(__msan_get_origin_tls()));
2663}
2664
2665TEST(MemorySanitizerOrigins, Alloca) {
2666 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002667 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
2668 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
2669 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
2670 EXPECT_POISONED_S(AllocaTO(), "ar@AllocaTO");
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002671}
2672
2673// FIXME: replace with a lit-like test.
2674TEST(MemorySanitizerOrigins, DISABLED_AllocaDeath) {
2675 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002676 EXPECT_DEATH(AllocaTO(), "ORIGIN: stack allocation: ar@AllocaTO");
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002677}
2678
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002679NOINLINE int RetvalOriginTest(U4 origin) {
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002680 int *a = new int;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002681 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002682 __msan_set_origin(a, sizeof(*a), origin);
2683 int res = *a;
2684 delete a;
2685 return res;
2686}
2687
2688TEST(MemorySanitizerOrigins, Retval) {
2689 if (!TrackingOrigins()) return;
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002690 EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002691}
2692
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002693NOINLINE void ParamOriginTest(int param, U4 origin) {
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002694 EXPECT_POISONED_O(param, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002695}
2696
2697TEST(MemorySanitizerOrigins, Param) {
2698 if (!TrackingOrigins()) return;
2699 int *a = new int;
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002700 U4 origin = __LINE__;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002701 break_optimization(a);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002702 __msan_set_origin(a, sizeof(*a), origin);
2703 ParamOriginTest(*a, origin);
2704 delete a;
2705}
2706
2707TEST(MemorySanitizerOrigins, Invoke) {
2708 if (!TrackingOrigins()) return;
2709 StructWithDtor s; // Will cause the calls to become invokes.
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002710 EXPECT_POISONED_O(RetvalOriginTest(__LINE__), __LINE__);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002711}
2712
2713TEST(MemorySanitizerOrigins, strlen) {
2714 S8 alignment;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002715 break_optimization(&alignment);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002716 char x[4] = {'a', 'b', 0, 0};
2717 __msan_poison(&x[2], 1);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002718 U4 origin = __LINE__;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002719 __msan_set_origin(x, sizeof(x), origin);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002720 EXPECT_UMR_O(volatile unsigned y = strlen(x), origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002721}
2722
2723TEST(MemorySanitizerOrigins, wcslen) {
2724 wchar_t w[3] = {'a', 'b', 0};
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002725 U4 origin = __LINE__;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002726 __msan_set_origin(w, sizeof(w), origin);
2727 __msan_poison(&w[2], sizeof(wchar_t));
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002728 EXPECT_UMR_O(volatile unsigned y = wcslen(w), origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002729}
2730
2731#if MSAN_HAS_M128
2732TEST(MemorySanitizerOrigins, StoreIntrinsic) {
2733 __m128 x, y;
Evgeniy Stepanov250f2212013-01-30 13:12:08 +00002734 U4 origin = __LINE__;
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002735 __msan_set_origin(&x, sizeof(x), origin);
2736 __msan_poison(&x, sizeof(x));
2737 __builtin_ia32_storeups((float*)&y, x);
Evgeniy Stepanov11929002013-01-22 12:29:00 +00002738 EXPECT_POISONED_O(y, origin);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002739}
2740#endif
2741
2742NOINLINE void RecursiveMalloc(int depth) {
2743 static int count;
2744 count++;
2745 if ((count % (1024 * 1024)) == 0)
2746 printf("RecursiveMalloc: %d\n", count);
2747 int *x1 = new int;
2748 int *x2 = new int;
Evgeniy Stepanov12c46932013-01-29 14:33:29 +00002749 break_optimization(x1);
2750 break_optimization(x2);
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002751 if (depth > 0) {
2752 RecursiveMalloc(depth-1);
2753 RecursiveMalloc(depth-1);
2754 }
2755 delete x1;
2756 delete x2;
2757}
2758
Kostya Serebryany65199f12013-01-25 11:46:22 +00002759TEST(MemorySanitizer, CallocOverflow) {
2760 size_t kArraySize = 4096;
2761 volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max();
2762 volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
2763 void *p = calloc(kArraySize, kArraySize2); // Should return 0.
2764 EXPECT_EQ(0L, Ident(p));
2765}
2766
Evgeniy Stepanov0231c502012-12-25 12:39:56 +00002767TEST(MemorySanitizerStress, DISABLED_MallocStackTrace) {
2768 RecursiveMalloc(22);
2769}