| //===-- msan_test.cc ------------------------------------------------------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file is a part of MemorySanitizer. |
| // |
| // MemorySanitizer unit tests. |
| //===----------------------------------------------------------------------===// |
| |
| #ifndef MSAN_EXTERNAL_TEST_CONFIG |
| #include "msan_test_config.h" |
| #endif // MSAN_EXTERNAL_TEST_CONFIG |
| |
| #include "sanitizer_common/tests/sanitizer_test_utils.h" |
| |
| #include "sanitizer/allocator_interface.h" |
| #include "sanitizer/msan_interface.h" |
| |
| #if defined(__FreeBSD__) |
| # define _KERNEL // To declare 'shminfo' structure. |
| # include <sys/shm.h> |
| # undef _KERNEL |
| extern "C" { |
| // <sys/shm.h> doesn't declare these functions in _KERNEL mode. |
| void *shmat(int, const void *, int); |
| int shmget(key_t, size_t, int); |
| int shmctl(int, int, struct shmid_ds *); |
| int shmdt(const void *); |
| } |
| #endif |
| |
| #include <inttypes.h> |
| #include <stdlib.h> |
| #include <stdarg.h> |
| #include <stdio.h> |
| #include <wchar.h> |
| #include <math.h> |
| |
| #include <arpa/inet.h> |
| #include <dlfcn.h> |
| #include <grp.h> |
| #include <unistd.h> |
| #include <link.h> |
| #include <limits.h> |
| #include <sys/time.h> |
| #include <poll.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <fcntl.h> |
| #include <sys/resource.h> |
| #include <sys/ioctl.h> |
| #include <sys/statvfs.h> |
| #include <sys/utsname.h> |
| #include <sys/mman.h> |
| #include <dirent.h> |
| #include <pwd.h> |
| #include <sys/socket.h> |
| #include <netdb.h> |
| #include <wordexp.h> |
| #include <sys/ipc.h> |
| #include <sys/shm.h> |
| |
| #if !defined(__FreeBSD__) |
| # include <malloc.h> |
| # include <sys/sysinfo.h> |
| # include <sys/vfs.h> |
| # include <mntent.h> |
| # include <netinet/ether.h> |
| #else |
| # include <signal.h> |
| # include <netinet/in.h> |
| # include <pthread_np.h> |
| # include <sys/uio.h> |
| # include <sys/mount.h> |
| # include <sys/sysctl.h> |
| # include <net/ethernet.h> |
| # define f_namelen f_namemax // FreeBSD names this statfs field so. |
| # define cpu_set_t cpuset_t |
| extern "C" { |
| // FreeBSD's <ssp/string.h> defines mempcpy() to be a macro expanding into |
| // a __builtin___mempcpy_chk() call, but since Msan RTL defines it as an |
| // ordinary function, we can declare it here to complete the tests. |
| void *mempcpy(void *dest, const void *src, size_t n); |
| } |
| #endif |
| |
| #if defined(__i386__) || defined(__x86_64__) |
| # include <emmintrin.h> |
| # define MSAN_HAS_M128 1 |
| #else |
| # define MSAN_HAS_M128 0 |
| #endif |
| |
| #ifdef __AVX2__ |
| # include <immintrin.h> |
| #endif |
| |
| // On FreeBSD procfs is not enabled by default. |
| #if defined(__FreeBSD__) |
| # define FILE_TO_READ "/bin/cat" |
| # define DIR_TO_READ "/bin" |
| # define SUBFILE_TO_READ "cat" |
| # define SYMLINK_TO_READ "/usr/bin/tar" |
| # define SUPERUSER_GROUP "wheel" |
| #else |
| # define FILE_TO_READ "/proc/self/stat" |
| # define DIR_TO_READ "/proc/self" |
| # define SUBFILE_TO_READ "stat" |
| # define SYMLINK_TO_READ "/proc/self/exe" |
| # define SUPERUSER_GROUP "root" |
| #endif |
| |
| static uintptr_t GetPageSize() { |
| return sysconf(_SC_PAGESIZE); |
| } |
| |
| const size_t kMaxPathLength = 4096; |
| |
| typedef unsigned char U1; |
| typedef unsigned short U2; // NOLINT |
| typedef unsigned int U4; |
| typedef unsigned long long U8; // NOLINT |
| typedef signed char S1; |
| typedef signed short S2; // NOLINT |
| typedef signed int S4; |
| typedef signed long long S8; // NOLINT |
| #define NOINLINE __attribute__((noinline)) |
| #define INLINE __attribute__((always_inline)) |
| |
| static bool TrackingOrigins() { |
| S8 x; |
| __msan_set_origin(&x, sizeof(x), 0x1234); |
| U4 origin = __msan_get_origin(&x); |
| __msan_set_origin(&x, sizeof(x), 0); |
| return __msan_origin_is_descendant_or_same(origin, 0x1234); |
| } |
| |
| #define EXPECT_ORIGIN(expected, origin) \ |
| EXPECT_TRUE(__msan_origin_is_descendant_or_same((origin), (expected))) |
| |
| #define EXPECT_UMR(action) \ |
| do { \ |
| __msan_set_expect_umr(1); \ |
| action; \ |
| __msan_set_expect_umr(0); \ |
| } while (0) |
| |
| #define EXPECT_UMR_O(action, origin) \ |
| do { \ |
| __msan_set_expect_umr(1); \ |
| action; \ |
| __msan_set_expect_umr(0); \ |
| if (TrackingOrigins()) EXPECT_ORIGIN(origin, __msan_get_umr_origin()); \ |
| } while (0) |
| |
| #define EXPECT_POISONED(x) ExpectPoisoned(x) |
| |
| template<typename T> |
| void ExpectPoisoned(const T& t) { |
| EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| } |
| |
| #define EXPECT_POISONED_O(x, origin) \ |
| ExpectPoisonedWithOrigin(x, origin) |
| |
| template<typename T> |
| void ExpectPoisonedWithOrigin(const T& t, unsigned origin) { |
| EXPECT_NE(-1, __msan_test_shadow((void*)&t, sizeof(t))); |
| if (TrackingOrigins()) EXPECT_ORIGIN(origin, __msan_get_origin((void *)&t)); |
| } |
| |
| #define EXPECT_NOT_POISONED(x) EXPECT_EQ(true, TestForNotPoisoned((x))) |
| |
| template<typename T> |
| bool TestForNotPoisoned(const T& t) { |
| return __msan_test_shadow((void*)&t, sizeof(t)) == -1; |
| } |
| |
| static U8 poisoned_array[100]; |
| template<class T> |
| T *GetPoisoned(int i = 0, T val = 0) { |
| T *res = (T*)&poisoned_array[i]; |
| *res = val; |
| __msan_poison(&poisoned_array[i], sizeof(T)); |
| return res; |
| } |
| |
| template<class T> |
| T *GetPoisonedO(int i, U4 origin, T val = 0) { |
| T *res = (T*)&poisoned_array[i]; |
| *res = val; |
| __msan_poison(&poisoned_array[i], sizeof(T)); |
| __msan_set_origin(&poisoned_array[i], sizeof(T), origin); |
| return res; |
| } |
| |
| template<typename T> |
| T Poisoned(T v = 0, T s = (T)(-1)) { |
| __msan_partial_poison(&v, &s, sizeof(T)); |
| return v; |
| } |
| |
| template<class T> NOINLINE T ReturnPoisoned() { return *GetPoisoned<T>(); } |
| |
| static volatile int g_one = 1; |
| static volatile int g_zero = 0; |
| static volatile int g_0 = 0; |
| static volatile int g_1 = 1; |
| |
| S4 a_s4[100]; |
| S8 a_s8[100]; |
| |
| // Check that malloc poisons memory. |
| // A lot of tests below depend on this. |
| TEST(MemorySanitizerSanity, PoisonInMalloc) { |
| int *x = (int*)malloc(sizeof(int)); |
| EXPECT_POISONED(*x); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, NegativeTest1) { |
| S4 *x = GetPoisoned<S4>(); |
| if (g_one) |
| *x = 0; |
| EXPECT_NOT_POISONED(*x); |
| } |
| |
| TEST(MemorySanitizer, PositiveTest1) { |
| // Load to store. |
| EXPECT_POISONED(*GetPoisoned<S1>()); |
| EXPECT_POISONED(*GetPoisoned<S2>()); |
| EXPECT_POISONED(*GetPoisoned<S4>()); |
| EXPECT_POISONED(*GetPoisoned<S8>()); |
| |
| // S->S conversions. |
| EXPECT_POISONED(*GetPoisoned<S1>()); |
| EXPECT_POISONED(*GetPoisoned<S1>()); |
| EXPECT_POISONED(*GetPoisoned<S1>()); |
| |
| EXPECT_POISONED(*GetPoisoned<S2>()); |
| EXPECT_POISONED(*GetPoisoned<S2>()); |
| EXPECT_POISONED(*GetPoisoned<S2>()); |
| |
| EXPECT_POISONED(*GetPoisoned<S4>()); |
| EXPECT_POISONED(*GetPoisoned<S4>()); |
| EXPECT_POISONED(*GetPoisoned<S4>()); |
| |
| EXPECT_POISONED(*GetPoisoned<S8>()); |
| EXPECT_POISONED(*GetPoisoned<S8>()); |
| EXPECT_POISONED(*GetPoisoned<S8>()); |
| |
| // ZExt |
| EXPECT_POISONED(*GetPoisoned<U1>()); |
| EXPECT_POISONED(*GetPoisoned<U1>()); |
| EXPECT_POISONED(*GetPoisoned<U1>()); |
| EXPECT_POISONED(*GetPoisoned<U2>()); |
| EXPECT_POISONED(*GetPoisoned<U2>()); |
| EXPECT_POISONED(*GetPoisoned<U4>()); |
| |
| // Unary ops. |
| EXPECT_POISONED(- *GetPoisoned<S4>()); |
| |
| EXPECT_UMR(a_s4[g_zero] = 100 / *GetPoisoned<S4>(0, 1)); |
| |
| |
| a_s4[g_zero] = 1 - *GetPoisoned<S4>(); |
| a_s4[g_zero] = 1 + *GetPoisoned<S4>(); |
| } |
| |
| TEST(MemorySanitizer, Phi1) { |
| S4 c; |
| if (g_one) { |
| c = *GetPoisoned<S4>(); |
| } else { |
| break_optimization(0); |
| c = 0; |
| } |
| EXPECT_POISONED(c); |
| } |
| |
| TEST(MemorySanitizer, Phi2) { |
| S4 i = *GetPoisoned<S4>(); |
| S4 n = g_one; |
| EXPECT_UMR(for (; i < g_one; i++);); |
| EXPECT_POISONED(i); |
| } |
| |
| NOINLINE void Arg1ExpectUMR(S4 a1) { EXPECT_POISONED(a1); } |
| NOINLINE void Arg2ExpectUMR(S4 a1, S4 a2) { EXPECT_POISONED(a2); } |
| NOINLINE void Arg3ExpectUMR(S1 a1, S4 a2, S8 a3) { EXPECT_POISONED(a3); } |
| |
| TEST(MemorySanitizer, ArgTest) { |
| Arg1ExpectUMR(*GetPoisoned<S4>()); |
| Arg2ExpectUMR(0, *GetPoisoned<S4>()); |
| Arg3ExpectUMR(0, 1, *GetPoisoned<S8>()); |
| } |
| |
| |
| TEST(MemorySanitizer, CallAndRet) { |
| ReturnPoisoned<S1>(); |
| ReturnPoisoned<S2>(); |
| ReturnPoisoned<S4>(); |
| ReturnPoisoned<S8>(); |
| |
| EXPECT_POISONED(ReturnPoisoned<S1>()); |
| EXPECT_POISONED(ReturnPoisoned<S2>()); |
| EXPECT_POISONED(ReturnPoisoned<S4>()); |
| EXPECT_POISONED(ReturnPoisoned<S8>()); |
| } |
| |
| // malloc() in the following test may be optimized to produce a compile-time |
| // undef value. Check that we trap on the volatile assignment anyway. |
| TEST(MemorySanitizer, DISABLED_MallocNoIdent) { |
| S4 *x = (int*)malloc(sizeof(S4)); |
| EXPECT_POISONED(*x); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, Malloc) { |
| S4 *x = (int*)Ident(malloc(sizeof(S4))); |
| EXPECT_POISONED(*x); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, Realloc) { |
| S4 *x = (int*)Ident(realloc(0, sizeof(S4))); |
| EXPECT_POISONED(x[0]); |
| x[0] = 1; |
| x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
| EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| EXPECT_POISONED(x[1]); |
| x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
| EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| EXPECT_POISONED(x[2]); |
| EXPECT_POISONED(x[1]); |
| x[2] = 1; // Init this here. Check that after realloc it is poisoned again. |
| x = (int*)Ident(realloc(x, 2 * sizeof(S4))); |
| EXPECT_NOT_POISONED(x[0]); // Ok, was inited before. |
| EXPECT_POISONED(x[1]); |
| x = (int*)Ident(realloc(x, 3 * sizeof(S4))); |
| EXPECT_POISONED(x[1]); |
| EXPECT_POISONED(x[2]); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, Calloc) { |
| S4 *x = (int*)Ident(calloc(1, sizeof(S4))); |
| EXPECT_NOT_POISONED(*x); // Should not be poisoned. |
| EXPECT_EQ(0, *x); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, CallocReturnsZeroMem) { |
| size_t sizes[] = {16, 1000, 10000, 100000, 2100000}; |
| for (size_t s = 0; s < sizeof(sizes)/sizeof(sizes[0]); s++) { |
| size_t size = sizes[s]; |
| for (size_t iter = 0; iter < 5; iter++) { |
| char *x = Ident((char*)calloc(1, size)); |
| EXPECT_EQ(x[0], 0); |
| EXPECT_EQ(x[size - 1], 0); |
| EXPECT_EQ(x[size / 2], 0); |
| EXPECT_EQ(x[size / 3], 0); |
| EXPECT_EQ(x[size / 4], 0); |
| memset(x, 0x42, size); |
| free(Ident(x)); |
| } |
| } |
| } |
| |
| TEST(MemorySanitizer, AndOr) { |
| U4 *p = GetPoisoned<U4>(); |
| // We poison two bytes in the midle of a 4-byte word to make the test |
| // correct regardless of endianness. |
| ((U1*)p)[1] = 0; |
| ((U1*)p)[2] = 0xff; |
| EXPECT_NOT_POISONED(*p & 0x00ffff00); |
| EXPECT_NOT_POISONED(*p & 0x00ff0000); |
| EXPECT_NOT_POISONED(*p & 0x0000ff00); |
| EXPECT_POISONED(*p & 0xff000000); |
| EXPECT_POISONED(*p & 0x000000ff); |
| EXPECT_POISONED(*p & 0x0000ffff); |
| EXPECT_POISONED(*p & 0xffff0000); |
| |
| EXPECT_NOT_POISONED(*p | 0xff0000ff); |
| EXPECT_NOT_POISONED(*p | 0xff00ffff); |
| EXPECT_NOT_POISONED(*p | 0xffff00ff); |
| EXPECT_POISONED(*p | 0xff000000); |
| EXPECT_POISONED(*p | 0x000000ff); |
| EXPECT_POISONED(*p | 0x0000ffff); |
| EXPECT_POISONED(*p | 0xffff0000); |
| |
| EXPECT_POISONED(*GetPoisoned<bool>() & *GetPoisoned<bool>()); |
| } |
| |
| template<class T> |
| static bool applyNot(T value, T shadow) { |
| __msan_partial_poison(&value, &shadow, sizeof(T)); |
| return !value; |
| } |
| |
| TEST(MemorySanitizer, Not) { |
| EXPECT_NOT_POISONED(applyNot<U4>(0x0, 0x0)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFFFFFFFF, 0x0)); |
| EXPECT_POISONED(applyNot<U4>(0xFFFFFFFF, 0xFFFFFFFF)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0FFFFFFF)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00FFFFFF)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x0000FFFF)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFF000000, 0x00000000)); |
| EXPECT_POISONED(applyNot<U4>(0xFF000000, 0xFF000000)); |
| EXPECT_NOT_POISONED(applyNot<U4>(0xFF800000, 0xFF000000)); |
| EXPECT_POISONED(applyNot<U4>(0x00008000, 0x00008000)); |
| |
| EXPECT_NOT_POISONED(applyNot<U1>(0x0, 0x0)); |
| EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0xFE)); |
| EXPECT_NOT_POISONED(applyNot<U1>(0xFF, 0x0)); |
| EXPECT_POISONED(applyNot<U1>(0xFF, 0xFF)); |
| |
| EXPECT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-1))); |
| EXPECT_NOT_POISONED(applyNot<void*>((void*)0xFFFFFF, (void*)(-2))); |
| } |
| |
| TEST(MemorySanitizer, Shift) { |
| U4 *up = GetPoisoned<U4>(); |
| ((U1*)up)[0] = 0; |
| ((U1*)up)[3] = 0xff; |
| EXPECT_NOT_POISONED(*up >> 30); |
| EXPECT_NOT_POISONED(*up >> 24); |
| EXPECT_POISONED(*up >> 23); |
| EXPECT_POISONED(*up >> 10); |
| |
| EXPECT_NOT_POISONED(*up << 30); |
| EXPECT_NOT_POISONED(*up << 24); |
| EXPECT_POISONED(*up << 23); |
| EXPECT_POISONED(*up << 10); |
| |
| S4 *sp = (S4*)up; |
| EXPECT_NOT_POISONED(*sp >> 30); |
| EXPECT_NOT_POISONED(*sp >> 24); |
| EXPECT_POISONED(*sp >> 23); |
| EXPECT_POISONED(*sp >> 10); |
| |
| sp = GetPoisoned<S4>(); |
| ((S1*)sp)[1] = 0; |
| ((S1*)sp)[2] = 0; |
| EXPECT_POISONED(*sp >> 31); |
| |
| EXPECT_POISONED(100 >> *GetPoisoned<S4>()); |
| EXPECT_POISONED(100U >> *GetPoisoned<S4>()); |
| } |
| |
| NOINLINE static int GetPoisonedZero() { |
| int *zero = new int; |
| *zero = 0; |
| __msan_poison(zero, sizeof(*zero)); |
| int res = *zero; |
| delete zero; |
| return res; |
| } |
| |
| TEST(MemorySanitizer, LoadFromDirtyAddress) { |
| int *a = new int; |
| *a = 0; |
| EXPECT_UMR(break_optimization((void*)(U8)a[GetPoisonedZero()])); |
| delete a; |
| } |
| |
| TEST(MemorySanitizer, StoreToDirtyAddress) { |
| int *a = new int; |
| EXPECT_UMR(a[GetPoisonedZero()] = 0); |
| break_optimization(a); |
| delete a; |
| } |
| |
| |
| NOINLINE void StackTestFunc() { |
| S4 p4; |
| S4 ok4 = 1; |
| S2 p2; |
| S2 ok2 = 1; |
| S1 p1; |
| S1 ok1 = 1; |
| break_optimization(&p4); |
| break_optimization(&ok4); |
| break_optimization(&p2); |
| break_optimization(&ok2); |
| break_optimization(&p1); |
| break_optimization(&ok1); |
| |
| EXPECT_POISONED(p4); |
| EXPECT_POISONED(p2); |
| EXPECT_POISONED(p1); |
| EXPECT_NOT_POISONED(ok1); |
| EXPECT_NOT_POISONED(ok2); |
| EXPECT_NOT_POISONED(ok4); |
| } |
| |
| TEST(MemorySanitizer, StackTest) { |
| StackTestFunc(); |
| } |
| |
| NOINLINE void StackStressFunc() { |
| int foo[10000]; |
| break_optimization(foo); |
| } |
| |
| TEST(MemorySanitizer, DISABLED_StackStressTest) { |
| for (int i = 0; i < 1000000; i++) |
| StackStressFunc(); |
| } |
| |
| template<class T> |
| void TestFloatingPoint() { |
| static volatile T v; |
| static T g[100]; |
| break_optimization(&g); |
| T *x = GetPoisoned<T>(); |
| T *y = GetPoisoned<T>(1); |
| EXPECT_POISONED(*x); |
| EXPECT_POISONED((long long)*x); |
| EXPECT_POISONED((int)*x); |
| g[0] = *x; |
| g[1] = *x + *y; |
| g[2] = *x - *y; |
| g[3] = *x * *y; |
| } |
| |
| TEST(MemorySanitizer, FloatingPointTest) { |
| TestFloatingPoint<float>(); |
| TestFloatingPoint<double>(); |
| } |
| |
| TEST(MemorySanitizer, DynMem) { |
| S4 x = 0; |
| S4 *y = GetPoisoned<S4>(); |
| memcpy(y, &x, g_one * sizeof(S4)); |
| EXPECT_NOT_POISONED(*y); |
| } |
| |
| static char *DynRetTestStr; |
| |
| TEST(MemorySanitizer, DynRet) { |
| ReturnPoisoned<S8>(); |
| EXPECT_NOT_POISONED(atoi("0")); |
| } |
| |
| TEST(MemorySanitizer, DynRet1) { |
| ReturnPoisoned<S8>(); |
| } |
| |
| struct LargeStruct { |
| S4 x[10]; |
| }; |
| |
| NOINLINE |
| LargeStruct LargeRetTest() { |
| LargeStruct res; |
| res.x[0] = *GetPoisoned<S4>(); |
| res.x[1] = *GetPoisoned<S4>(); |
| res.x[2] = *GetPoisoned<S4>(); |
| res.x[3] = *GetPoisoned<S4>(); |
| res.x[4] = *GetPoisoned<S4>(); |
| res.x[5] = *GetPoisoned<S4>(); |
| res.x[6] = *GetPoisoned<S4>(); |
| res.x[7] = *GetPoisoned<S4>(); |
| res.x[8] = *GetPoisoned<S4>(); |
| res.x[9] = *GetPoisoned<S4>(); |
| return res; |
| } |
| |
| TEST(MemorySanitizer, strcmp) { |
| char s1[10]; |
| char s2[10]; |
| strncpy(s1, "foo", 10); |
| s2[0] = 'f'; |
| s2[1] = 'n'; |
| EXPECT_GT(strcmp(s1, s2), 0); |
| s2[1] = 'o'; |
| int res; |
| EXPECT_UMR(res = strcmp(s1, s2)); |
| EXPECT_NOT_POISONED(res); |
| EXPECT_EQ(strncmp(s1, s2, 1), 0); |
| } |
| |
| TEST(MemorySanitizer, LargeRet) { |
| LargeStruct a = LargeRetTest(); |
| EXPECT_POISONED(a.x[0]); |
| EXPECT_POISONED(a.x[9]); |
| } |
| |
| TEST(MemorySanitizer, strerror) { |
| char *buf = strerror(EINVAL); |
| EXPECT_NOT_POISONED(strlen(buf)); |
| buf = strerror(123456); |
| EXPECT_NOT_POISONED(strlen(buf)); |
| } |
| |
| TEST(MemorySanitizer, strerror_r) { |
| errno = 0; |
| char buf[1000]; |
| char *res = (char*) (size_t) strerror_r(EINVAL, buf, sizeof(buf)); |
| ASSERT_EQ(0, errno); |
| if (!res) res = buf; // POSIX version success. |
| EXPECT_NOT_POISONED(strlen(res)); |
| } |
| |
| TEST(MemorySanitizer, fread) { |
| char *x = new char[32]; |
| FILE *f = fopen(FILE_TO_READ, "r"); |
| ASSERT_TRUE(f != NULL); |
| fread(x, 1, 32, f); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_NOT_POISONED(x[16]); |
| EXPECT_NOT_POISONED(x[31]); |
| fclose(f); |
| delete[] x; |
| } |
| |
| TEST(MemorySanitizer, read) { |
| char *x = new char[32]; |
| int fd = open(FILE_TO_READ, O_RDONLY); |
| ASSERT_GT(fd, 0); |
| int sz = read(fd, x, 32); |
| ASSERT_EQ(sz, 32); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_NOT_POISONED(x[16]); |
| EXPECT_NOT_POISONED(x[31]); |
| close(fd); |
| delete[] x; |
| } |
| |
| TEST(MemorySanitizer, pread) { |
| char *x = new char[32]; |
| int fd = open(FILE_TO_READ, O_RDONLY); |
| ASSERT_GT(fd, 0); |
| int sz = pread(fd, x, 32, 0); |
| ASSERT_EQ(sz, 32); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_NOT_POISONED(x[16]); |
| EXPECT_NOT_POISONED(x[31]); |
| close(fd); |
| delete[] x; |
| } |
| |
| TEST(MemorySanitizer, readv) { |
| char buf[2011]; |
| struct iovec iov[2]; |
| iov[0].iov_base = buf + 1; |
| iov[0].iov_len = 5; |
| iov[1].iov_base = buf + 10; |
| iov[1].iov_len = 2000; |
| int fd = open(FILE_TO_READ, O_RDONLY); |
| ASSERT_GT(fd, 0); |
| int sz = readv(fd, iov, 2); |
| ASSERT_GE(sz, 0); |
| ASSERT_LE(sz, 5 + 2000); |
| ASSERT_GT((size_t)sz, iov[0].iov_len); |
| EXPECT_POISONED(buf[0]); |
| EXPECT_NOT_POISONED(buf[1]); |
| EXPECT_NOT_POISONED(buf[5]); |
| EXPECT_POISONED(buf[6]); |
| EXPECT_POISONED(buf[9]); |
| EXPECT_NOT_POISONED(buf[10]); |
| EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]); |
| EXPECT_POISONED(buf[11 + (sz - 1) - 5]); |
| close(fd); |
| } |
| |
| TEST(MemorySanitizer, preadv) { |
| char buf[2011]; |
| struct iovec iov[2]; |
| iov[0].iov_base = buf + 1; |
| iov[0].iov_len = 5; |
| iov[1].iov_base = buf + 10; |
| iov[1].iov_len = 2000; |
| int fd = open(FILE_TO_READ, O_RDONLY); |
| ASSERT_GT(fd, 0); |
| int sz = preadv(fd, iov, 2, 3); |
| ASSERT_GE(sz, 0); |
| ASSERT_LE(sz, 5 + 2000); |
| ASSERT_GT((size_t)sz, iov[0].iov_len); |
| EXPECT_POISONED(buf[0]); |
| EXPECT_NOT_POISONED(buf[1]); |
| EXPECT_NOT_POISONED(buf[5]); |
| EXPECT_POISONED(buf[6]); |
| EXPECT_POISONED(buf[9]); |
| EXPECT_NOT_POISONED(buf[10]); |
| EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]); |
| EXPECT_POISONED(buf[11 + (sz - 1) - 5]); |
| close(fd); |
| } |
| |
| // FIXME: fails now. |
| TEST(MemorySanitizer, DISABLED_ioctl) { |
| struct winsize ws; |
| EXPECT_EQ(ioctl(2, TIOCGWINSZ, &ws), 0); |
| EXPECT_NOT_POISONED(ws.ws_col); |
| } |
| |
| TEST(MemorySanitizer, readlink) { |
| char *x = new char[1000]; |
| readlink(SYMLINK_TO_READ, x, 1000); |
| EXPECT_NOT_POISONED(x[0]); |
| delete [] x; |
| } |
| |
| TEST(MemorySanitizer, stat) { |
| struct stat* st = new struct stat; |
| int res = stat(FILE_TO_READ, st); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(st->st_dev); |
| EXPECT_NOT_POISONED(st->st_mode); |
| EXPECT_NOT_POISONED(st->st_size); |
| } |
| |
| TEST(MemorySanitizer, fstatat) { |
| struct stat* st = new struct stat; |
| int dirfd = open(DIR_TO_READ, O_RDONLY); |
| ASSERT_GT(dirfd, 0); |
| int res = fstatat(dirfd, SUBFILE_TO_READ, st, 0); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(st->st_dev); |
| EXPECT_NOT_POISONED(st->st_mode); |
| EXPECT_NOT_POISONED(st->st_size); |
| close(dirfd); |
| } |
| |
| TEST(MemorySanitizer, statfs) { |
| struct statfs st; |
| int res = statfs("/", &st); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(st.f_type); |
| EXPECT_NOT_POISONED(st.f_bfree); |
| EXPECT_NOT_POISONED(st.f_namelen); |
| } |
| |
| TEST(MemorySanitizer, statvfs) { |
| struct statvfs st; |
| int res = statvfs("/", &st); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(st.f_bsize); |
| EXPECT_NOT_POISONED(st.f_blocks); |
| EXPECT_NOT_POISONED(st.f_bfree); |
| EXPECT_NOT_POISONED(st.f_namemax); |
| } |
| |
| TEST(MemorySanitizer, fstatvfs) { |
| struct statvfs st; |
| int fd = open("/", O_RDONLY | O_DIRECTORY); |
| int res = fstatvfs(fd, &st); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(st.f_bsize); |
| EXPECT_NOT_POISONED(st.f_blocks); |
| EXPECT_NOT_POISONED(st.f_bfree); |
| EXPECT_NOT_POISONED(st.f_namemax); |
| close(fd); |
| } |
| |
| TEST(MemorySanitizer, pipe) { |
| int* pipefd = new int[2]; |
| int res = pipe(pipefd); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(pipefd[0]); |
| EXPECT_NOT_POISONED(pipefd[1]); |
| close(pipefd[0]); |
| close(pipefd[1]); |
| } |
| |
| TEST(MemorySanitizer, pipe2) { |
| int* pipefd = new int[2]; |
| int res = pipe2(pipefd, O_NONBLOCK); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(pipefd[0]); |
| EXPECT_NOT_POISONED(pipefd[1]); |
| close(pipefd[0]); |
| close(pipefd[1]); |
| } |
| |
| TEST(MemorySanitizer, socketpair) { |
| int sv[2]; |
| int res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(sv[0]); |
| EXPECT_NOT_POISONED(sv[1]); |
| close(sv[0]); |
| close(sv[1]); |
| } |
| |
| TEST(MemorySanitizer, poll) { |
| int* pipefd = new int[2]; |
| int res = pipe(pipefd); |
| ASSERT_EQ(0, res); |
| |
| char data = 42; |
| res = write(pipefd[1], &data, 1); |
| ASSERT_EQ(1, res); |
| |
| pollfd fds[2]; |
| fds[0].fd = pipefd[0]; |
| fds[0].events = POLLIN; |
| fds[1].fd = pipefd[1]; |
| fds[1].events = POLLIN; |
| res = poll(fds, 2, 500); |
| ASSERT_EQ(1, res); |
| EXPECT_NOT_POISONED(fds[0].revents); |
| EXPECT_NOT_POISONED(fds[1].revents); |
| |
| close(pipefd[0]); |
| close(pipefd[1]); |
| } |
| |
| // There is no ppoll() on FreeBSD. |
| #if !defined (__FreeBSD__) |
| TEST(MemorySanitizer, ppoll) { |
| int* pipefd = new int[2]; |
| int res = pipe(pipefd); |
| ASSERT_EQ(0, res); |
| |
| char data = 42; |
| res = write(pipefd[1], &data, 1); |
| ASSERT_EQ(1, res); |
| |
| pollfd fds[2]; |
| fds[0].fd = pipefd[0]; |
| fds[0].events = POLLIN; |
| fds[1].fd = pipefd[1]; |
| fds[1].events = POLLIN; |
| sigset_t ss; |
| sigemptyset(&ss); |
| res = ppoll(fds, 2, NULL, &ss); |
| ASSERT_EQ(1, res); |
| EXPECT_NOT_POISONED(fds[0].revents); |
| EXPECT_NOT_POISONED(fds[1].revents); |
| |
| close(pipefd[0]); |
| close(pipefd[1]); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, poll_positive) { |
| int* pipefd = new int[2]; |
| int res = pipe(pipefd); |
| ASSERT_EQ(0, res); |
| |
| pollfd fds[2]; |
| fds[0].fd = pipefd[0]; |
| fds[0].events = POLLIN; |
| // fds[1].fd uninitialized |
| fds[1].events = POLLIN; |
| EXPECT_UMR(poll(fds, 2, 0)); |
| |
| close(pipefd[0]); |
| close(pipefd[1]); |
| } |
| |
| TEST(MemorySanitizer, bind_getsockname) { |
| int sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| |
| struct sockaddr_in sai; |
| memset(&sai, 0, sizeof(sai)); |
| sai.sin_family = AF_UNIX; |
| int res = bind(sock, (struct sockaddr *)&sai, sizeof(sai)); |
| |
| ASSERT_EQ(0, res); |
| char buf[200]; |
| socklen_t addrlen; |
| EXPECT_UMR(getsockname(sock, (struct sockaddr *)&buf, &addrlen)); |
| |
| addrlen = sizeof(buf); |
| res = getsockname(sock, (struct sockaddr *)&buf, &addrlen); |
| EXPECT_NOT_POISONED(addrlen); |
| EXPECT_NOT_POISONED(buf[0]); |
| EXPECT_NOT_POISONED(buf[addrlen - 1]); |
| EXPECT_POISONED(buf[addrlen]); |
| close(sock); |
| } |
| |
| TEST(MemorySanitizer, accept) { |
| int listen_socket = socket(AF_INET, SOCK_STREAM, 0); |
| ASSERT_LT(0, listen_socket); |
| |
| struct sockaddr_in sai; |
| memset(&sai, 0, sizeof(sai)); |
| sai.sin_family = AF_INET; |
| sai.sin_port = 0; |
| sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| int res = bind(listen_socket, (struct sockaddr *)&sai, sizeof(sai)); |
| ASSERT_EQ(0, res); |
| |
| res = listen(listen_socket, 1); |
| ASSERT_EQ(0, res); |
| |
| socklen_t sz = sizeof(sai); |
| res = getsockname(listen_socket, (struct sockaddr *)&sai, &sz); |
| ASSERT_EQ(0, res); |
| ASSERT_EQ(sizeof(sai), sz); |
| |
| int connect_socket = socket(AF_INET, SOCK_STREAM, 0); |
| ASSERT_LT(0, connect_socket); |
| res = fcntl(connect_socket, F_SETFL, O_NONBLOCK); |
| ASSERT_EQ(0, res); |
| res = connect(connect_socket, (struct sockaddr *)&sai, sizeof(sai)); |
| // On FreeBSD this connection completes immediately. |
| if (res != 0) { |
| ASSERT_EQ(-1, res); |
| ASSERT_EQ(EINPROGRESS, errno); |
| } |
| |
| __msan_poison(&sai, sizeof(sai)); |
| int new_sock = accept(listen_socket, (struct sockaddr *)&sai, &sz); |
| ASSERT_LT(0, new_sock); |
| ASSERT_EQ(sizeof(sai), sz); |
| EXPECT_NOT_POISONED(sai); |
| |
| __msan_poison(&sai, sizeof(sai)); |
| res = getpeername(new_sock, (struct sockaddr *)&sai, &sz); |
| ASSERT_EQ(0, res); |
| ASSERT_EQ(sizeof(sai), sz); |
| EXPECT_NOT_POISONED(sai); |
| |
| close(new_sock); |
| close(connect_socket); |
| close(listen_socket); |
| } |
| |
| TEST(MemorySanitizer, getaddrinfo) { |
| struct addrinfo *ai; |
| struct addrinfo hints; |
| memset(&hints, 0, sizeof(hints)); |
| hints.ai_family = AF_INET; |
| int res = getaddrinfo("localhost", NULL, &hints, &ai); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(*ai); |
| ASSERT_EQ(sizeof(sockaddr_in), ai->ai_addrlen); |
| EXPECT_NOT_POISONED(*(sockaddr_in*)ai->ai_addr); |
| } |
| |
| TEST(MemorySanitizer, getnameinfo) { |
| struct sockaddr_in sai; |
| memset(&sai, 0, sizeof(sai)); |
| sai.sin_family = AF_INET; |
| sai.sin_port = 80; |
| sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| char host[500]; |
| char serv[500]; |
| int res = getnameinfo((struct sockaddr *)&sai, sizeof(sai), host, |
| sizeof(host), serv, sizeof(serv), 0); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(host[0]); |
| EXPECT_POISONED(host[sizeof(host) - 1]); |
| |
| ASSERT_NE(0U, strlen(host)); |
| EXPECT_NOT_POISONED(serv[0]); |
| EXPECT_POISONED(serv[sizeof(serv) - 1]); |
| ASSERT_NE(0U, strlen(serv)); |
| } |
| |
| #define EXPECT_HOSTENT_NOT_POISONED(he) \ |
| do { \ |
| EXPECT_NOT_POISONED(*(he)); \ |
| ASSERT_NE((void *) 0, (he)->h_name); \ |
| ASSERT_NE((void *) 0, (he)->h_aliases); \ |
| ASSERT_NE((void *) 0, (he)->h_addr_list); \ |
| EXPECT_NOT_POISONED(strlen((he)->h_name)); \ |
| char **p = (he)->h_aliases; \ |
| while (*p) { \ |
| EXPECT_NOT_POISONED(strlen(*p)); \ |
| ++p; \ |
| } \ |
| char **q = (he)->h_addr_list; \ |
| while (*q) { \ |
| EXPECT_NOT_POISONED(*q[0]); \ |
| ++q; \ |
| } \ |
| EXPECT_NOT_POISONED(*q); \ |
| } while (0) |
| |
| TEST(MemorySanitizer, gethostent) { |
| struct hostent *he = gethostent(); |
| ASSERT_NE((void *)NULL, he); |
| EXPECT_HOSTENT_NOT_POISONED(he); |
| } |
| |
| #ifndef MSAN_TEST_DISABLE_GETHOSTBYNAME |
| |
| TEST(MemorySanitizer, gethostbyname) { |
| struct hostent *he = gethostbyname("localhost"); |
| ASSERT_NE((void *)NULL, he); |
| EXPECT_HOSTENT_NOT_POISONED(he); |
| } |
| |
| #endif // MSAN_TEST_DISABLE_GETHOSTBYNAME |
| |
| TEST(MemorySanitizer, recvmsg) { |
| int server_socket = socket(AF_INET, SOCK_DGRAM, 0); |
| ASSERT_LT(0, server_socket); |
| |
| struct sockaddr_in sai; |
| memset(&sai, 0, sizeof(sai)); |
| sai.sin_family = AF_INET; |
| sai.sin_port = 0; |
| sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| int res = bind(server_socket, (struct sockaddr *)&sai, sizeof(sai)); |
| ASSERT_EQ(0, res); |
| |
| socklen_t sz = sizeof(sai); |
| res = getsockname(server_socket, (struct sockaddr *)&sai, &sz); |
| ASSERT_EQ(0, res); |
| ASSERT_EQ(sizeof(sai), sz); |
| |
| |
| int client_socket = socket(AF_INET, SOCK_DGRAM, 0); |
| ASSERT_LT(0, client_socket); |
| |
| struct sockaddr_in client_sai; |
| memset(&client_sai, 0, sizeof(client_sai)); |
| client_sai.sin_family = AF_INET; |
| client_sai.sin_port = 0; |
| client_sai.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| res = bind(client_socket, (struct sockaddr *)&client_sai, sizeof(client_sai)); |
| ASSERT_EQ(0, res); |
| |
| sz = sizeof(client_sai); |
| res = getsockname(client_socket, (struct sockaddr *)&client_sai, &sz); |
| ASSERT_EQ(0, res); |
| ASSERT_EQ(sizeof(client_sai), sz); |
| |
| const char *s = "message text"; |
| struct iovec iov; |
| iov.iov_base = (void *)s; |
| iov.iov_len = strlen(s) + 1; |
| struct msghdr msg; |
| memset(&msg, 0, sizeof(msg)); |
| msg.msg_name = &sai; |
| msg.msg_namelen = sizeof(sai); |
| msg.msg_iov = &iov; |
| msg.msg_iovlen = 1; |
| res = sendmsg(client_socket, &msg, 0); |
| ASSERT_LT(0, res); |
| |
| |
| char buf[1000]; |
| struct iovec recv_iov; |
| recv_iov.iov_base = (void *)&buf; |
| recv_iov.iov_len = sizeof(buf); |
| struct sockaddr_in recv_sai; |
| struct msghdr recv_msg; |
| memset(&recv_msg, 0, sizeof(recv_msg)); |
| recv_msg.msg_name = &recv_sai; |
| recv_msg.msg_namelen = sizeof(recv_sai); |
| recv_msg.msg_iov = &recv_iov; |
| recv_msg.msg_iovlen = 1; |
| res = recvmsg(server_socket, &recv_msg, 0); |
| ASSERT_LT(0, res); |
| |
| ASSERT_EQ(sizeof(recv_sai), recv_msg.msg_namelen); |
| EXPECT_NOT_POISONED(*(struct sockaddr_in *)recv_msg.msg_name); |
| EXPECT_STREQ(s, buf); |
| |
| close(server_socket); |
| close(client_socket); |
| } |
| |
| TEST(MemorySanitizer, gethostbyname2) { |
| struct hostent *he = gethostbyname2("localhost", AF_INET); |
| ASSERT_NE((void *)NULL, he); |
| EXPECT_HOSTENT_NOT_POISONED(he); |
| } |
| |
| TEST(MemorySanitizer, gethostbyaddr) { |
| in_addr_t addr = inet_addr("127.0.0.1"); |
| EXPECT_NOT_POISONED(addr); |
| struct hostent *he = gethostbyaddr(&addr, sizeof(addr), AF_INET); |
| ASSERT_NE((void *)NULL, he); |
| EXPECT_HOSTENT_NOT_POISONED(he); |
| } |
| |
| TEST(MemorySanitizer, gethostent_r) { |
| char buf[2000]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| int res = gethostent_r(&he, buf, sizeof(buf), &result, &err); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(result); |
| ASSERT_NE((void *)NULL, result); |
| EXPECT_HOSTENT_NOT_POISONED(result); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, gethostbyname_r) { |
| char buf[2000]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| int res = gethostbyname_r("localhost", &he, buf, sizeof(buf), &result, &err); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(result); |
| ASSERT_NE((void *)NULL, result); |
| EXPECT_HOSTENT_NOT_POISONED(result); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, gethostbyname_r_bad_host_name) { |
| char buf[2000]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| int res = gethostbyname_r("bad-host-name", &he, buf, sizeof(buf), &result, &err); |
| ASSERT_EQ((struct hostent *)0, result); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, gethostbyname_r_erange) { |
| char buf[5]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| gethostbyname_r("localhost", &he, buf, sizeof(buf), &result, &err); |
| ASSERT_EQ(ERANGE, errno); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, gethostbyname2_r) { |
| char buf[2000]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| int res = gethostbyname2_r("localhost", AF_INET, &he, buf, sizeof(buf), |
| &result, &err); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(result); |
| ASSERT_NE((void *)NULL, result); |
| EXPECT_HOSTENT_NOT_POISONED(result); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, gethostbyaddr_r) { |
| char buf[2000]; |
| struct hostent he; |
| struct hostent *result; |
| int err; |
| in_addr_t addr = inet_addr("127.0.0.1"); |
| EXPECT_NOT_POISONED(addr); |
| int res = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &he, buf, sizeof(buf), |
| &result, &err); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(result); |
| ASSERT_NE((void *)NULL, result); |
| EXPECT_HOSTENT_NOT_POISONED(result); |
| EXPECT_NOT_POISONED(err); |
| } |
| |
| TEST(MemorySanitizer, getsockopt) { |
| int sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| struct linger l[2]; |
| socklen_t sz = sizeof(l[0]); |
| int res = getsockopt(sock, SOL_SOCKET, SO_LINGER, &l[0], &sz); |
| ASSERT_EQ(0, res); |
| ASSERT_EQ(sizeof(l[0]), sz); |
| EXPECT_NOT_POISONED(l[0]); |
| EXPECT_POISONED(*(char *)(l + 1)); |
| } |
| |
| TEST(MemorySanitizer, getcwd) { |
| char path[PATH_MAX + 1]; |
| char* res = getcwd(path, sizeof(path)); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(path[0]); |
| } |
| |
| TEST(MemorySanitizer, getcwd_gnu) { |
| char* res = getcwd(NULL, 0); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(res[0]); |
| free(res); |
| } |
| |
| // There's no get_current_dir_name() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, get_current_dir_name) { |
| char* res = get_current_dir_name(); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(res[0]); |
| free(res); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, shmctl) { |
| int id = shmget(IPC_PRIVATE, 4096, 0644 | IPC_CREAT); |
| ASSERT_GT(id, -1); |
| |
| struct shmid_ds ds; |
| int res = shmctl(id, IPC_STAT, &ds); |
| ASSERT_GT(res, -1); |
| EXPECT_NOT_POISONED(ds); |
| |
| // FreeBSD does not support shmctl(IPC_INFO) and shmctl(SHM_INFO). |
| #if !defined(__FreeBSD__) |
| struct shminfo si; |
| res = shmctl(id, IPC_INFO, (struct shmid_ds *)&si); |
| ASSERT_GT(res, -1); |
| EXPECT_NOT_POISONED(si); |
| |
| struct shm_info s_i; |
| res = shmctl(id, SHM_INFO, (struct shmid_ds *)&s_i); |
| ASSERT_GT(res, -1); |
| EXPECT_NOT_POISONED(s_i); |
| #endif |
| |
| res = shmctl(id, IPC_RMID, 0); |
| ASSERT_GT(res, -1); |
| } |
| |
| TEST(MemorySanitizer, shmat) { |
| const int kShmSize = 4096; |
| void *mapping_start = mmap(NULL, kShmSize + SHMLBA, PROT_READ | PROT_WRITE, |
| MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| ASSERT_NE(MAP_FAILED, mapping_start); |
| |
| void *p = (void *)(((unsigned long)mapping_start + SHMLBA - 1) / SHMLBA * SHMLBA); |
| // p is now SHMLBA-aligned; |
| |
| ((char *)p)[10] = *GetPoisoned<U1>(); |
| ((char *)p)[kShmSize - 1] = *GetPoisoned<U1>(); |
| |
| int res = munmap(mapping_start, kShmSize + SHMLBA); |
| ASSERT_EQ(0, res); |
| |
| int id = shmget(IPC_PRIVATE, kShmSize, 0644 | IPC_CREAT); |
| ASSERT_GT(id, -1); |
| |
| void *q = shmat(id, p, 0); |
| ASSERT_EQ(p, q); |
| |
| EXPECT_NOT_POISONED(((char *)q)[0]); |
| EXPECT_NOT_POISONED(((char *)q)[10]); |
| EXPECT_NOT_POISONED(((char *)q)[kShmSize - 1]); |
| |
| res = shmdt(q); |
| ASSERT_EQ(0, res); |
| |
| res = shmctl(id, IPC_RMID, 0); |
| ASSERT_GT(res, -1); |
| } |
| |
| // There's no random_r() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, random_r) { |
| int32_t x; |
| char z[64]; |
| memset(z, 0, sizeof(z)); |
| |
| struct random_data buf; |
| memset(&buf, 0, sizeof(buf)); |
| |
| int res = initstate_r(0, z, sizeof(z), &buf); |
| ASSERT_EQ(0, res); |
| |
| res = random_r(&buf, &x); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(x); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, confstr) { |
| char buf[3]; |
| size_t res = confstr(_CS_PATH, buf, sizeof(buf)); |
| ASSERT_GT(res, sizeof(buf)); |
| EXPECT_NOT_POISONED(buf[0]); |
| EXPECT_NOT_POISONED(buf[sizeof(buf) - 1]); |
| |
| char buf2[1000]; |
| res = confstr(_CS_PATH, buf2, sizeof(buf2)); |
| ASSERT_LT(res, sizeof(buf2)); |
| EXPECT_NOT_POISONED(buf2[0]); |
| EXPECT_NOT_POISONED(buf2[res - 1]); |
| EXPECT_POISONED(buf2[res]); |
| ASSERT_EQ(res, strlen(buf2) + 1); |
| } |
| |
| TEST(MemorySanitizer, opendir) { |
| DIR *dir = opendir("."); |
| closedir(dir); |
| |
| char name[10] = "."; |
| __msan_poison(name, sizeof(name)); |
| EXPECT_UMR(dir = opendir(name)); |
| closedir(dir); |
| } |
| |
| TEST(MemorySanitizer, readdir) { |
| DIR *dir = opendir("."); |
| struct dirent *d = readdir(dir); |
| ASSERT_TRUE(d != NULL); |
| EXPECT_NOT_POISONED(d->d_name[0]); |
| closedir(dir); |
| } |
| |
| TEST(MemorySanitizer, readdir_r) { |
| DIR *dir = opendir("."); |
| struct dirent d; |
| struct dirent *pd; |
| int res = readdir_r(dir, &d, &pd); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(pd); |
| EXPECT_NOT_POISONED(d.d_name[0]); |
| closedir(dir); |
| } |
| |
| TEST(MemorySanitizer, realpath) { |
| const char* relpath = "."; |
| char path[PATH_MAX + 1]; |
| char* res = realpath(relpath, path); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(path[0]); |
| } |
| |
| TEST(MemorySanitizer, realpath_null) { |
| const char* relpath = "."; |
| char* res = realpath(relpath, NULL); |
| printf("%d, %s\n", errno, strerror(errno)); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(res[0]); |
| free(res); |
| } |
| |
| // There's no canonicalize_file_name() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, canonicalize_file_name) { |
| const char* relpath = "."; |
| char* res = canonicalize_file_name(relpath); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(res[0]); |
| free(res); |
| } |
| #endif |
| |
| extern char **environ; |
| |
| TEST(MemorySanitizer, setenv) { |
| setenv("AAA", "BBB", 1); |
| for (char **envp = environ; *envp; ++envp) { |
| EXPECT_NOT_POISONED(*envp); |
| EXPECT_NOT_POISONED(*envp[0]); |
| } |
| } |
| |
| TEST(MemorySanitizer, putenv) { |
| char s[] = "AAA=BBB"; |
| putenv(s); |
| for (char **envp = environ; *envp; ++envp) { |
| EXPECT_NOT_POISONED(*envp); |
| EXPECT_NOT_POISONED(*envp[0]); |
| } |
| } |
| |
| TEST(MemorySanitizer, memcpy) { |
| char* x = new char[2]; |
| char* y = new char[2]; |
| x[0] = 1; |
| x[1] = *GetPoisoned<char>(); |
| memcpy(y, x, 2); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| } |
| |
| void TestUnalignedMemcpy(unsigned left, unsigned right, bool src_is_aligned, |
| bool src_is_poisoned, bool dst_is_poisoned) { |
| fprintf(stderr, "%s(%d, %d, %d, %d, %d)\n", __func__, left, right, |
| src_is_aligned, src_is_poisoned, dst_is_poisoned); |
| |
| const unsigned sz = 20; |
| U4 dst_origin, src_origin; |
| char *dst = (char *)malloc(sz); |
| if (dst_is_poisoned) |
| dst_origin = __msan_get_origin(dst); |
| else |
| memset(dst, 0, sz); |
| |
| char *src = (char *)malloc(sz); |
| if (src_is_poisoned) |
| src_origin = __msan_get_origin(src); |
| else |
| memset(src, 0, sz); |
| |
| memcpy(dst + left, src_is_aligned ? src + left : src, sz - left - right); |
| |
| for (unsigned i = 0; i < (left & (~3U)); ++i) |
| if (dst_is_poisoned) |
| EXPECT_POISONED_O(dst[i], dst_origin); |
| else |
| EXPECT_NOT_POISONED(dst[i]); |
| |
| for (unsigned i = 0; i < (right & (~3U)); ++i) |
| if (dst_is_poisoned) |
| EXPECT_POISONED_O(dst[sz - i - 1], dst_origin); |
| else |
| EXPECT_NOT_POISONED(dst[sz - i - 1]); |
| |
| for (unsigned i = left; i < sz - right; ++i) |
| if (src_is_poisoned) |
| EXPECT_POISONED_O(dst[i], src_origin); |
| else |
| EXPECT_NOT_POISONED(dst[i]); |
| |
| free(dst); |
| free(src); |
| } |
| |
| TEST(MemorySanitizer, memcpy_unaligned) { |
| for (int i = 0; i < 10; ++i) |
| for (int j = 0; j < 10; ++j) |
| for (int aligned = 0; aligned < 2; ++aligned) |
| for (int srcp = 0; srcp < 2; ++srcp) |
| for (int dstp = 0; dstp < 2; ++dstp) |
| TestUnalignedMemcpy(i, j, aligned, srcp, dstp); |
| } |
| |
| TEST(MemorySanitizer, memmove) { |
| char* x = new char[2]; |
| char* y = new char[2]; |
| x[0] = 1; |
| x[1] = *GetPoisoned<char>(); |
| memmove(y, x, 2); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| } |
| |
| TEST(MemorySanitizer, memccpy_nomatch) { |
| char* x = new char[5]; |
| char* y = new char[5]; |
| strcpy(x, "abc"); |
| memccpy(y, x, 'd', 4); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_NOT_POISONED(y[1]); |
| EXPECT_NOT_POISONED(y[2]); |
| EXPECT_NOT_POISONED(y[3]); |
| EXPECT_POISONED(y[4]); |
| delete[] x; |
| delete[] y; |
| } |
| |
| TEST(MemorySanitizer, memccpy_match) { |
| char* x = new char[5]; |
| char* y = new char[5]; |
| strcpy(x, "abc"); |
| memccpy(y, x, 'b', 4); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_NOT_POISONED(y[1]); |
| EXPECT_POISONED(y[2]); |
| EXPECT_POISONED(y[3]); |
| EXPECT_POISONED(y[4]); |
| delete[] x; |
| delete[] y; |
| } |
| |
| TEST(MemorySanitizer, memccpy_nomatch_positive) { |
| char* x = new char[5]; |
| char* y = new char[5]; |
| strcpy(x, "abc"); |
| EXPECT_UMR(memccpy(y, x, 'd', 5)); |
| delete[] x; |
| delete[] y; |
| } |
| |
| TEST(MemorySanitizer, memccpy_match_positive) { |
| char* x = new char[5]; |
| char* y = new char[5]; |
| x[0] = 'a'; |
| x[2] = 'b'; |
| EXPECT_UMR(memccpy(y, x, 'b', 5)); |
| delete[] x; |
| delete[] y; |
| } |
| |
| TEST(MemorySanitizer, bcopy) { |
| char* x = new char[2]; |
| char* y = new char[2]; |
| x[0] = 1; |
| x[1] = *GetPoisoned<char>(); |
| bcopy(x, y, 2); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| } |
| |
| TEST(MemorySanitizer, strdup) { |
| char buf[4] = "abc"; |
| __msan_poison(buf + 2, sizeof(*buf)); |
| char *x = strdup(buf); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_NOT_POISONED(x[1]); |
| EXPECT_POISONED(x[2]); |
| EXPECT_NOT_POISONED(x[3]); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, strndup) { |
| char buf[4] = "abc"; |
| __msan_poison(buf + 2, sizeof(*buf)); |
| char *x = strndup(buf, 3); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_NOT_POISONED(x[1]); |
| EXPECT_POISONED(x[2]); |
| EXPECT_NOT_POISONED(x[3]); |
| free(x); |
| } |
| |
| TEST(MemorySanitizer, strndup_short) { |
| char buf[4] = "abc"; |
| __msan_poison(buf + 1, sizeof(*buf)); |
| __msan_poison(buf + 2, sizeof(*buf)); |
| char *x = strndup(buf, 2); |
| EXPECT_NOT_POISONED(x[0]); |
| EXPECT_POISONED(x[1]); |
| EXPECT_NOT_POISONED(x[2]); |
| free(x); |
| } |
| |
| |
| template<class T, int size> |
| void TestOverlapMemmove() { |
| T *x = new T[size]; |
| ASSERT_GE(size, 3); |
| x[2] = 0; |
| memmove(x, x + 1, (size - 1) * sizeof(T)); |
| EXPECT_NOT_POISONED(x[1]); |
| EXPECT_POISONED(x[0]); |
| EXPECT_POISONED(x[2]); |
| delete [] x; |
| } |
| |
| TEST(MemorySanitizer, overlap_memmove) { |
| TestOverlapMemmove<U1, 10>(); |
| TestOverlapMemmove<U1, 1000>(); |
| TestOverlapMemmove<U8, 4>(); |
| TestOverlapMemmove<U8, 1000>(); |
| } |
| |
| TEST(MemorySanitizer, strcpy) { // NOLINT |
| char* x = new char[3]; |
| char* y = new char[3]; |
| x[0] = 'a'; |
| x[1] = *GetPoisoned<char>(1, 1); |
| x[2] = 0; |
| strcpy(y, x); // NOLINT |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| EXPECT_NOT_POISONED(y[2]); |
| } |
| |
| TEST(MemorySanitizer, strncpy) { // NOLINT |
| char* x = new char[3]; |
| char* y = new char[5]; |
| x[0] = 'a'; |
| x[1] = *GetPoisoned<char>(1, 1); |
| x[2] = '\0'; |
| strncpy(y, x, 4); // NOLINT |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| EXPECT_NOT_POISONED(y[2]); |
| EXPECT_NOT_POISONED(y[3]); |
| EXPECT_POISONED(y[4]); |
| } |
| |
| TEST(MemorySanitizer, stpcpy) { // NOLINT |
| char* x = new char[3]; |
| char* y = new char[3]; |
| x[0] = 'a'; |
| x[1] = *GetPoisoned<char>(1, 1); |
| x[2] = 0; |
| char *res = stpcpy(y, x); // NOLINT |
| ASSERT_EQ(res, y + 2); |
| EXPECT_NOT_POISONED(y[0]); |
| EXPECT_POISONED(y[1]); |
| EXPECT_NOT_POISONED(y[2]); |
| } |
| |
| TEST(MemorySanitizer, strcat) { // NOLINT |
| char a[10]; |
| char b[] = "def"; |
| strcpy(a, "abc"); |
| __msan_poison(b + 1, 1); |
| strcat(a, b); |
| EXPECT_NOT_POISONED(a[3]); |
| EXPECT_POISONED(a[4]); |
| EXPECT_NOT_POISONED(a[5]); |
| EXPECT_NOT_POISONED(a[6]); |
| EXPECT_POISONED(a[7]); |
| } |
| |
| TEST(MemorySanitizer, strncat) { // NOLINT |
| char a[10]; |
| char b[] = "def"; |
| strcpy(a, "abc"); |
| __msan_poison(b + 1, 1); |
| strncat(a, b, 5); |
| EXPECT_NOT_POISONED(a[3]); |
| EXPECT_POISONED(a[4]); |
| EXPECT_NOT_POISONED(a[5]); |
| EXPECT_NOT_POISONED(a[6]); |
| EXPECT_POISONED(a[7]); |
| } |
| |
| TEST(MemorySanitizer, strncat_overflow) { // NOLINT |
| char a[10]; |
| char b[] = "def"; |
| strcpy(a, "abc"); |
| __msan_poison(b + 1, 1); |
| strncat(a, b, 2); |
| EXPECT_NOT_POISONED(a[3]); |
| EXPECT_POISONED(a[4]); |
| EXPECT_NOT_POISONED(a[5]); |
| EXPECT_POISONED(a[6]); |
| EXPECT_POISONED(a[7]); |
| } |
| |
| #define TEST_STRTO_INT(func_name, char_type, str_prefix) \ |
| TEST(MemorySanitizer, func_name) { \ |
| char_type *e; \ |
| EXPECT_EQ(1U, func_name(str_prefix##"1", &e, 10)); \ |
| EXPECT_NOT_POISONED((S8)e); \ |
| } |
| |
| #define TEST_STRTO_FLOAT(func_name, char_type, str_prefix) \ |
| TEST(MemorySanitizer, func_name) { \ |
| char_type *e; \ |
| EXPECT_NE(0, func_name(str_prefix##"1.5", &e)); \ |
| EXPECT_NOT_POISONED((S8)e); \ |
| } |
| |
| #define TEST_STRTO_FLOAT_LOC(func_name, char_type, str_prefix) \ |
| TEST(MemorySanitizer, func_name) { \ |
| locale_t loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0); \ |
| char_type *e; \ |
| EXPECT_NE(0, func_name(str_prefix##"1.5", &e, loc)); \ |
| EXPECT_NOT_POISONED((S8)e); \ |
| freelocale(loc); \ |
| } |
| |
| #define TEST_STRTO_INT_LOC(func_name, char_type, str_prefix) \ |
| TEST(MemorySanitizer, func_name) { \ |
| locale_t loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0); \ |
| char_type *e; \ |
| ASSERT_EQ(1U, func_name(str_prefix##"1", &e, 10, loc)); \ |
| EXPECT_NOT_POISONED((S8)e); \ |
| freelocale(loc); \ |
| } |
| |
| TEST_STRTO_INT(strtol, char, ) |
| TEST_STRTO_INT(strtoll, char, ) |
| TEST_STRTO_INT(strtoul, char, ) |
| TEST_STRTO_INT(strtoull, char, ) |
| |
| TEST_STRTO_FLOAT(strtof, char, ) |
| TEST_STRTO_FLOAT(strtod, char, ) |
| TEST_STRTO_FLOAT(strtold, char, ) |
| |
| TEST_STRTO_FLOAT_LOC(strtof_l, char, ) |
| TEST_STRTO_FLOAT_LOC(strtod_l, char, ) |
| TEST_STRTO_FLOAT_LOC(strtold_l, char, ) |
| |
| TEST_STRTO_INT_LOC(strtol_l, char, ) |
| TEST_STRTO_INT_LOC(strtoll_l, char, ) |
| TEST_STRTO_INT_LOC(strtoul_l, char, ) |
| TEST_STRTO_INT_LOC(strtoull_l, char, ) |
| |
| TEST_STRTO_INT(wcstol, wchar_t, L) |
| TEST_STRTO_INT(wcstoll, wchar_t, L) |
| TEST_STRTO_INT(wcstoul, wchar_t, L) |
| TEST_STRTO_INT(wcstoull, wchar_t, L) |
| |
| TEST_STRTO_FLOAT(wcstof, wchar_t, L) |
| TEST_STRTO_FLOAT(wcstod, wchar_t, L) |
| TEST_STRTO_FLOAT(wcstold, wchar_t, L) |
| |
| TEST_STRTO_FLOAT_LOC(wcstof_l, wchar_t, L) |
| TEST_STRTO_FLOAT_LOC(wcstod_l, wchar_t, L) |
| TEST_STRTO_FLOAT_LOC(wcstold_l, wchar_t, L) |
| |
| TEST_STRTO_INT_LOC(wcstol_l, wchar_t, L) |
| TEST_STRTO_INT_LOC(wcstoll_l, wchar_t, L) |
| TEST_STRTO_INT_LOC(wcstoul_l, wchar_t, L) |
| TEST_STRTO_INT_LOC(wcstoull_l, wchar_t, L) |
| |
| |
| TEST(MemorySanitizer, strtoimax) { |
| char *e; |
| ASSERT_EQ(1, strtoimax("1", &e, 10)); |
| EXPECT_NOT_POISONED((S8) e); |
| } |
| |
| TEST(MemorySanitizer, strtoumax) { |
| char *e; |
| ASSERT_EQ(1U, strtoumax("1", &e, 10)); |
| EXPECT_NOT_POISONED((S8) e); |
| } |
| |
| #ifdef __GLIBC__ |
| extern "C" float __strtof_l(const char *nptr, char **endptr, locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__strtof_l, char, ) |
| extern "C" double __strtod_l(const char *nptr, char **endptr, locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__strtod_l, char, ) |
| extern "C" long double __strtold_l(const char *nptr, char **endptr, |
| locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__strtold_l, char, ) |
| |
| extern "C" float __wcstof_l(const wchar_t *nptr, wchar_t **endptr, locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__wcstof_l, wchar_t, L) |
| extern "C" double __wcstod_l(const wchar_t *nptr, wchar_t **endptr, locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__wcstod_l, wchar_t, L) |
| extern "C" long double __wcstold_l(const wchar_t *nptr, wchar_t **endptr, |
| locale_t loc); |
| TEST_STRTO_FLOAT_LOC(__wcstold_l, wchar_t, L) |
| #endif // __GLIBC__ |
| |
| TEST(MemorySanitizer, modf) { |
| double x, y; |
| x = modf(2.1, &y); |
| EXPECT_NOT_POISONED(y); |
| } |
| |
| TEST(MemorySanitizer, modff) { |
| float x, y; |
| x = modff(2.1, &y); |
| EXPECT_NOT_POISONED(y); |
| } |
| |
| TEST(MemorySanitizer, modfl) { |
| long double x, y; |
| x = modfl(2.1, &y); |
| EXPECT_NOT_POISONED(y); |
| } |
| |
| // There's no sincos() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, sincos) { |
| double s, c; |
| sincos(0.2, &s, &c); |
| EXPECT_NOT_POISONED(s); |
| EXPECT_NOT_POISONED(c); |
| } |
| #endif |
| |
| // There's no sincosf() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, sincosf) { |
| float s, c; |
| sincosf(0.2, &s, &c); |
| EXPECT_NOT_POISONED(s); |
| EXPECT_NOT_POISONED(c); |
| } |
| #endif |
| |
| // There's no sincosl() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, sincosl) { |
| long double s, c; |
| sincosl(0.2, &s, &c); |
| EXPECT_NOT_POISONED(s); |
| EXPECT_NOT_POISONED(c); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, remquo) { |
| int quo; |
| double res = remquo(29.0, 3.0, &quo); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(quo); |
| } |
| |
| TEST(MemorySanitizer, remquof) { |
| int quo; |
| float res = remquof(29.0, 3.0, &quo); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(quo); |
| } |
| |
| TEST(MemorySanitizer, remquol) { |
| int quo; |
| long double res = remquof(29.0, 3.0, &quo); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(quo); |
| } |
| |
| TEST(MemorySanitizer, lgamma) { |
| double res = lgamma(1.1); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(signgam); |
| } |
| |
| TEST(MemorySanitizer, lgammaf) { |
| float res = lgammaf(1.1); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(signgam); |
| } |
| |
| TEST(MemorySanitizer, lgammal) { |
| long double res = lgammal(1.1); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(signgam); |
| } |
| |
| TEST(MemorySanitizer, lgamma_r) { |
| int sgn; |
| double res = lgamma_r(1.1, &sgn); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(sgn); |
| } |
| |
| TEST(MemorySanitizer, lgammaf_r) { |
| int sgn; |
| float res = lgammaf_r(1.1, &sgn); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(sgn); |
| } |
| |
| // There's no lgammal_r() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, lgammal_r) { |
| int sgn; |
| long double res = lgammal_r(1.1, &sgn); |
| ASSERT_NE(0.0, res); |
| EXPECT_NOT_POISONED(sgn); |
| } |
| #endif |
| |
| // There's no drand48_r() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, drand48_r) { |
| struct drand48_data buf; |
| srand48_r(0, &buf); |
| double d; |
| drand48_r(&buf, &d); |
| EXPECT_NOT_POISONED(d); |
| } |
| #endif |
| |
| // There's no lrand48_r() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, lrand48_r) { |
| struct drand48_data buf; |
| srand48_r(0, &buf); |
| long d; |
| lrand48_r(&buf, &d); |
| EXPECT_NOT_POISONED(d); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, sprintf) { // NOLINT |
| char buff[10]; |
| break_optimization(buff); |
| EXPECT_POISONED(buff[0]); |
| int res = sprintf(buff, "%d", 1234567); // NOLINT |
| ASSERT_EQ(res, 7); |
| ASSERT_EQ(buff[0], '1'); |
| ASSERT_EQ(buff[1], '2'); |
| ASSERT_EQ(buff[2], '3'); |
| ASSERT_EQ(buff[6], '7'); |
| ASSERT_EQ(buff[7], 0); |
| EXPECT_POISONED(buff[8]); |
| } |
| |
| TEST(MemorySanitizer, snprintf) { |
| char buff[10]; |
| break_optimization(buff); |
| EXPECT_POISONED(buff[0]); |
| int res = snprintf(buff, sizeof(buff), "%d", 1234567); |
| ASSERT_EQ(res, 7); |
| ASSERT_EQ(buff[0], '1'); |
| ASSERT_EQ(buff[1], '2'); |
| ASSERT_EQ(buff[2], '3'); |
| ASSERT_EQ(buff[6], '7'); |
| ASSERT_EQ(buff[7], 0); |
| EXPECT_POISONED(buff[8]); |
| } |
| |
| TEST(MemorySanitizer, swprintf) { |
| wchar_t buff[10]; |
| ASSERT_EQ(4U, sizeof(wchar_t)); |
| break_optimization(buff); |
| EXPECT_POISONED(buff[0]); |
| int res = swprintf(buff, 9, L"%d", 1234567); |
| ASSERT_EQ(res, 7); |
| ASSERT_EQ(buff[0], '1'); |
| ASSERT_EQ(buff[1], '2'); |
| ASSERT_EQ(buff[2], '3'); |
| ASSERT_EQ(buff[6], '7'); |
| ASSERT_EQ(buff[7], L'\0'); |
| EXPECT_POISONED(buff[8]); |
| } |
| |
| TEST(MemorySanitizer, asprintf) { // NOLINT |
| char *pbuf; |
| EXPECT_POISONED(pbuf); |
| int res = asprintf(&pbuf, "%d", 1234567); // NOLINT |
| ASSERT_EQ(res, 7); |
| EXPECT_NOT_POISONED(pbuf); |
| ASSERT_EQ(pbuf[0], '1'); |
| ASSERT_EQ(pbuf[1], '2'); |
| ASSERT_EQ(pbuf[2], '3'); |
| ASSERT_EQ(pbuf[6], '7'); |
| ASSERT_EQ(pbuf[7], 0); |
| free(pbuf); |
| } |
| |
| TEST(MemorySanitizer, mbstowcs) { |
| const char *x = "abc"; |
| wchar_t buff[10]; |
| int res = mbstowcs(buff, x, 2); |
| EXPECT_EQ(2, res); |
| EXPECT_EQ(L'a', buff[0]); |
| EXPECT_EQ(L'b', buff[1]); |
| EXPECT_POISONED(buff[2]); |
| res = mbstowcs(buff, x, 10); |
| EXPECT_EQ(3, res); |
| EXPECT_NOT_POISONED(buff[3]); |
| } |
| |
| TEST(MemorySanitizer, wcstombs) { |
| const wchar_t *x = L"abc"; |
| char buff[10]; |
| int res = wcstombs(buff, x, 4); |
| EXPECT_EQ(res, 3); |
| EXPECT_EQ(buff[0], 'a'); |
| EXPECT_EQ(buff[1], 'b'); |
| EXPECT_EQ(buff[2], 'c'); |
| } |
| |
| TEST(MemorySanitizer, wcsrtombs) { |
| const wchar_t *x = L"abc"; |
| const wchar_t *p = x; |
| char buff[10]; |
| mbstate_t mbs; |
| memset(&mbs, 0, sizeof(mbs)); |
| int res = wcsrtombs(buff, &p, 4, &mbs); |
| EXPECT_EQ(res, 3); |
| EXPECT_EQ(buff[0], 'a'); |
| EXPECT_EQ(buff[1], 'b'); |
| EXPECT_EQ(buff[2], 'c'); |
| EXPECT_EQ(buff[3], '\0'); |
| EXPECT_POISONED(buff[4]); |
| } |
| |
| TEST(MemorySanitizer, wcsnrtombs) { |
| const wchar_t *x = L"abc"; |
| const wchar_t *p = x; |
| char buff[10]; |
| mbstate_t mbs; |
| memset(&mbs, 0, sizeof(mbs)); |
| int res = wcsnrtombs(buff, &p, 2, 4, &mbs); |
| EXPECT_EQ(res, 2); |
| EXPECT_EQ(buff[0], 'a'); |
| EXPECT_EQ(buff[1], 'b'); |
| EXPECT_POISONED(buff[2]); |
| } |
| |
| TEST(MemorySanitizer, wcrtomb) { |
| wchar_t x = L'a'; |
| char buff[10]; |
| mbstate_t mbs; |
| memset(&mbs, 0, sizeof(mbs)); |
| size_t res = wcrtomb(buff, x, &mbs); |
| EXPECT_EQ(res, (size_t)1); |
| EXPECT_EQ(buff[0], 'a'); |
| } |
| |
| TEST(MemorySanitizer, wmemset) { |
| wchar_t x[25]; |
| break_optimization(x); |
| EXPECT_POISONED(x[0]); |
| wmemset(x, L'A', 10); |
| EXPECT_EQ(x[0], L'A'); |
| EXPECT_EQ(x[9], L'A'); |
| EXPECT_POISONED(x[10]); |
| } |
| |
| TEST(MemorySanitizer, mbtowc) { |
| const char *x = "abc"; |
| wchar_t wx; |
| int res = mbtowc(&wx, x, 3); |
| EXPECT_GT(res, 0); |
| EXPECT_NOT_POISONED(wx); |
| } |
| |
| TEST(MemorySanitizer, mbrtowc) { |
| const char *x = "abc"; |
| wchar_t wx; |
| mbstate_t mbs; |
| memset(&mbs, 0, sizeof(mbs)); |
| int res = mbrtowc(&wx, x, 3, &mbs); |
| EXPECT_GT(res, 0); |
| EXPECT_NOT_POISONED(wx); |
| } |
| |
| TEST(MemorySanitizer, wcsftime) { |
| wchar_t x[100]; |
| time_t t = time(NULL); |
| struct tm tms; |
| struct tm *tmres = localtime_r(&t, &tms); |
| ASSERT_NE((void *)0, tmres); |
| size_t res = wcsftime(x, sizeof(x) / sizeof(x[0]), L"%Y-%m-%d", tmres); |
| EXPECT_GT(res, 0UL); |
| EXPECT_EQ(res, wcslen(x)); |
| } |
| |
| TEST(MemorySanitizer, gettimeofday) { |
| struct timeval tv; |
| struct timezone tz; |
| break_optimization(&tv); |
| break_optimization(&tz); |
| ASSERT_EQ(16U, sizeof(tv)); |
| ASSERT_EQ(8U, sizeof(tz)); |
| EXPECT_POISONED(tv.tv_sec); |
| EXPECT_POISONED(tv.tv_usec); |
| EXPECT_POISONED(tz.tz_minuteswest); |
| EXPECT_POISONED(tz.tz_dsttime); |
| ASSERT_EQ(0, gettimeofday(&tv, &tz)); |
| EXPECT_NOT_POISONED(tv.tv_sec); |
| EXPECT_NOT_POISONED(tv.tv_usec); |
| EXPECT_NOT_POISONED(tz.tz_minuteswest); |
| EXPECT_NOT_POISONED(tz.tz_dsttime); |
| } |
| |
| TEST(MemorySanitizer, clock_gettime) { |
| struct timespec tp; |
| EXPECT_POISONED(tp.tv_sec); |
| EXPECT_POISONED(tp.tv_nsec); |
| ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &tp)); |
| EXPECT_NOT_POISONED(tp.tv_sec); |
| EXPECT_NOT_POISONED(tp.tv_nsec); |
| } |
| |
| TEST(MemorySanitizer, clock_getres) { |
| struct timespec tp; |
| EXPECT_POISONED(tp.tv_sec); |
| EXPECT_POISONED(tp.tv_nsec); |
| ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, 0)); |
| EXPECT_POISONED(tp.tv_sec); |
| EXPECT_POISONED(tp.tv_nsec); |
| ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &tp)); |
| EXPECT_NOT_POISONED(tp.tv_sec); |
| EXPECT_NOT_POISONED(tp.tv_nsec); |
| } |
| |
| TEST(MemorySanitizer, getitimer) { |
| struct itimerval it1, it2; |
| int res; |
| EXPECT_POISONED(it1.it_interval.tv_sec); |
| EXPECT_POISONED(it1.it_interval.tv_usec); |
| EXPECT_POISONED(it1.it_value.tv_sec); |
| EXPECT_POISONED(it1.it_value.tv_usec); |
| res = getitimer(ITIMER_VIRTUAL, &it1); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(it1.it_interval.tv_sec); |
| EXPECT_NOT_POISONED(it1.it_interval.tv_usec); |
| EXPECT_NOT_POISONED(it1.it_value.tv_sec); |
| EXPECT_NOT_POISONED(it1.it_value.tv_usec); |
| |
| it1.it_interval.tv_sec = it1.it_value.tv_sec = 10000; |
| it1.it_interval.tv_usec = it1.it_value.tv_usec = 0; |
| |
| res = setitimer(ITIMER_VIRTUAL, &it1, &it2); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(it2.it_interval.tv_sec); |
| EXPECT_NOT_POISONED(it2.it_interval.tv_usec); |
| EXPECT_NOT_POISONED(it2.it_value.tv_sec); |
| EXPECT_NOT_POISONED(it2.it_value.tv_usec); |
| |
| // Check that old_value can be 0, and disable the timer. |
| memset(&it1, 0, sizeof(it1)); |
| res = setitimer(ITIMER_VIRTUAL, &it1, 0); |
| ASSERT_EQ(0, res); |
| } |
| |
| TEST(MemorySanitizer, setitimer_null) { |
| setitimer(ITIMER_VIRTUAL, 0, 0); |
| // Not testing the return value, since it the behaviour seems to differ |
| // between libc implementations and POSIX. |
| // Should never crash, though. |
| } |
| |
| TEST(MemorySanitizer, time) { |
| time_t t; |
| EXPECT_POISONED(t); |
| time_t t2 = time(&t); |
| ASSERT_NE(t2, (time_t)-1); |
| EXPECT_NOT_POISONED(t); |
| } |
| |
| TEST(MemorySanitizer, strptime) { |
| struct tm time; |
| char *p = strptime("11/1/2013-05:39", "%m/%d/%Y-%H:%M", &time); |
| ASSERT_TRUE(p != NULL); |
| EXPECT_NOT_POISONED(time.tm_sec); |
| EXPECT_NOT_POISONED(time.tm_hour); |
| EXPECT_NOT_POISONED(time.tm_year); |
| } |
| |
| TEST(MemorySanitizer, localtime) { |
| time_t t = 123; |
| struct tm *time = localtime(&t); |
| ASSERT_TRUE(time != NULL); |
| EXPECT_NOT_POISONED(time->tm_sec); |
| EXPECT_NOT_POISONED(time->tm_hour); |
| EXPECT_NOT_POISONED(time->tm_year); |
| EXPECT_NOT_POISONED(time->tm_isdst); |
| EXPECT_NE(0U, strlen(time->tm_zone)); |
| } |
| |
| TEST(MemorySanitizer, localtime_r) { |
| time_t t = 123; |
| struct tm time; |
| struct tm *res = localtime_r(&t, &time); |
| ASSERT_TRUE(res != NULL); |
| EXPECT_NOT_POISONED(time.tm_sec); |
| EXPECT_NOT_POISONED(time.tm_hour); |
| EXPECT_NOT_POISONED(time.tm_year); |
| EXPECT_NOT_POISONED(time.tm_isdst); |
| EXPECT_NE(0U, strlen(time.tm_zone)); |
| } |
| |
| // There's no getmntent() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, getmntent) { |
| FILE *fp = setmntent("/etc/fstab", "r"); |
| struct mntent *mnt = getmntent(fp); |
| ASSERT_TRUE(mnt != NULL); |
| ASSERT_NE(0U, strlen(mnt->mnt_fsname)); |
| ASSERT_NE(0U, strlen(mnt->mnt_dir)); |
| ASSERT_NE(0U, strlen(mnt->mnt_type)); |
| ASSERT_NE(0U, strlen(mnt->mnt_opts)); |
| EXPECT_NOT_POISONED(mnt->mnt_freq); |
| EXPECT_NOT_POISONED(mnt->mnt_passno); |
| fclose(fp); |
| } |
| #endif |
| |
| // There's no getmntent_r() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, getmntent_r) { |
| FILE *fp = setmntent("/etc/fstab", "r"); |
| struct mntent mntbuf; |
| char buf[1000]; |
| struct mntent *mnt = getmntent_r(fp, &mntbuf, buf, sizeof(buf)); |
| ASSERT_TRUE(mnt != NULL); |
| ASSERT_NE(0U, strlen(mnt->mnt_fsname)); |
| ASSERT_NE(0U, strlen(mnt->mnt_dir)); |
| ASSERT_NE(0U, strlen(mnt->mnt_type)); |
| ASSERT_NE(0U, strlen(mnt->mnt_opts)); |
| EXPECT_NOT_POISONED(mnt->mnt_freq); |
| EXPECT_NOT_POISONED(mnt->mnt_passno); |
| fclose(fp); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, ether) { |
| const char *asc = "11:22:33:44:55:66"; |
| struct ether_addr *paddr = ether_aton(asc); |
| EXPECT_NOT_POISONED(*paddr); |
| |
| struct ether_addr addr; |
| paddr = ether_aton_r(asc, &addr); |
| ASSERT_EQ(paddr, &addr); |
| EXPECT_NOT_POISONED(addr); |
| |
| char *s = ether_ntoa(&addr); |
| ASSERT_NE(0U, strlen(s)); |
| |
| char buf[100]; |
| s = ether_ntoa_r(&addr, buf); |
| ASSERT_EQ(s, buf); |
| ASSERT_NE(0U, strlen(buf)); |
| } |
| |
| TEST(MemorySanitizer, mmap) { |
| const int size = 4096; |
| void *p1, *p2; |
| p1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| __msan_poison(p1, size); |
| munmap(p1, size); |
| for (int i = 0; i < 1000; i++) { |
| p2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| if (p2 == p1) |
| break; |
| else |
| munmap(p2, size); |
| } |
| if (p1 == p2) { |
| EXPECT_NOT_POISONED(*(char*)p2); |
| munmap(p2, size); |
| } |
| } |
| |
| // There's no fcvt() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| // FIXME: enable and add ecvt. |
| // FIXME: check why msandr does nt handle fcvt. |
| TEST(MemorySanitizer, fcvt) { |
| int a, b; |
| break_optimization(&a); |
| break_optimization(&b); |
| EXPECT_POISONED(a); |
| EXPECT_POISONED(b); |
| char *str = fcvt(12345.6789, 10, &a, &b); |
| EXPECT_NOT_POISONED(a); |
| EXPECT_NOT_POISONED(b); |
| ASSERT_NE(nullptr, str); |
| EXPECT_NOT_POISONED(str[0]); |
| ASSERT_NE(0U, strlen(str)); |
| } |
| #endif |
| |
| // There's no fcvt_long() on FreeBSD. |
| #if !defined(__FreeBSD__) |
| TEST(MemorySanitizer, fcvt_long) { |
| int a, b; |
| break_optimization(&a); |
| break_optimization(&b); |
| EXPECT_POISONED(a); |
| EXPECT_POISONED(b); |
| char *str = fcvt(111111112345.6789, 10, &a, &b); |
| EXPECT_NOT_POISONED(a); |
| EXPECT_NOT_POISONED(b); |
| ASSERT_NE(nullptr, str); |
| EXPECT_NOT_POISONED(str[0]); |
| ASSERT_NE(0U, strlen(str)); |
| } |
| #endif |
| |
| TEST(MemorySanitizer, memchr) { |
| char x[10]; |
| break_optimization(x); |
| EXPECT_POISONED(x[0]); |
| x[2] = '2'; |
| void *res; |
| EXPECT_UMR(res = memchr(x, '2', 10)); |
| EXPECT_NOT_POISONED(res); |
| x[0] = '0'; |
| x[1] = '1'; |
| res = memchr(x, '2', 10); |
| EXPECT_EQ(&x[2], res); |
| EXPECT_UMR(res = memchr(x, '3', 10)); |
| EXPECT_NOT_POISONED(res); |
| } |
| |
| TEST(MemorySanitizer, memrchr) { |
| char x[10]; |
| break_optimization(x); |
| EXPECT_POISONED(x[0]); |
| x[9] = '9'; |
| void *res; |
| EXPECT_UMR(res = memrchr(x, '9', 10)); |
| EXPECT_NOT_POISONED(res); |
| x[0] = '0'; |
| x[1] = '1'; |
| res = memrchr(x, '0', 2); |
| EXPECT_EQ(&x[0], res); |
| EXPECT_UMR(res = memrchr(x, '7', 10)); |
| EXPECT_NOT_POISONED(res); |
| } |
| |
| TEST(MemorySanitizer, frexp) { |
| int x; |
| x = *GetPoisoned<int>(); |
| double r = frexp(1.1, &x); |
| EXPECT_NOT_POISONED(r); |
| EXPECT_NOT_POISONED(x); |
| |
| x = *GetPoisoned<int>(); |
| float rf = frexpf(1.1, &x); |
| EXPECT_NOT_POISONED(rf); |
| EXPECT_NOT_POISONED(x); |
| |
| x = *GetPoisoned<int>(); |
| double rl = frexpl(1.1, &x); |
| EXPECT_NOT_POISONED(rl); |
| EXPECT_NOT_POISONED(x); |
| } |
| |
| namespace { |
| |
| static int cnt; |
| |
| void SigactionHandler(int signo, siginfo_t* si, void* uc) { |
| ASSERT_EQ(signo, SIGPROF); |
| ASSERT_TRUE(si != NULL); |
| EXPECT_NOT_POISONED(si->si_errno); |
| EXPECT_NOT_POISONED(si->si_pid); |
| #if __linux__ |
| # if defined(__x86_64__) |
| EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_RIP]); |
| # elif defined(__i386__) |
| EXPECT_NOT_POISONED(((ucontext_t*)uc)->uc_mcontext.gregs[REG_EIP]); |
| # endif |
| #endif |
| ++cnt; |
| } |
| |
| TEST(MemorySanitizer, sigaction) { |
| struct sigaction act = {}; |
| struct sigaction oldact = {}; |
| struct sigaction origact = {}; |
| |
| sigaction(SIGPROF, 0, &origact); |
| |
| act.sa_flags |= SA_SIGINFO; |
| act.sa_sigaction = &SigactionHandler; |
| sigaction(SIGPROF, &act, 0); |
| |
| kill(getpid(), SIGPROF); |
| |
| act.sa_flags &= ~SA_SIGINFO; |
| act.sa_handler = SIG_DFL; |
| sigaction(SIGPROF, &act, 0); |
| |
| act.sa_flags &= ~SA_SIGINFO; |
| act.sa_handler = SIG_IGN; |
| sigaction(SIGPROF, &act, &oldact); |
| EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO); |
| EXPECT_EQ(SIG_DFL, oldact.sa_handler); |
| kill(getpid(), SIGPROF); |
| |
| act.sa_flags |= SA_SIGINFO; |
| act.sa_sigaction = &SigactionHandler; |
| sigaction(SIGPROF, &act, &oldact); |
| EXPECT_FALSE(oldact.sa_flags & SA_SIGINFO); |
| EXPECT_EQ(SIG_IGN, oldact.sa_handler); |
| kill(getpid(), SIGPROF); |
| |
| act.sa_flags &= ~SA_SIGINFO; |
| act.sa_handler = SIG_DFL; |
| sigaction(SIGPROF, &act, &oldact); |
| EXPECT_TRUE(oldact.sa_flags & SA_SIGINFO); |
| EXPECT_EQ(&SigactionHandler, oldact.sa_sigaction); |
| EXPECT_EQ(2, cnt); |
| |
| sigaction(SIGPROF, &origact, 0); |
| } |
| |
| } // namespace |
| |
| |
| TEST(MemorySanitizer, sigemptyset) { |
| sigset_t s; |
| EXPECT_POISONED(s); |
| int res = sigemptyset(&s); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(s); |
| } |
| |
| TEST(MemorySanitizer, sigfillset) { |
| sigset_t s; |
| EXPECT_POISONED(s); |
| int res = sigfillset(&s); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(s); |
| } |
| |
| TEST(MemorySanitizer, sigpending) { |
| sigset_t s; |
| EXPECT_POISONED(s); |
| int res = sigpending(&s); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(s); |
| } |
| |
| TEST(MemorySanitizer, sigprocmask) { |
| sigset_t s; |
| EXPECT_POISONED(s); |
| int res = sigprocmask(SIG_BLOCK, 0, &s); |
| ASSERT_EQ(0, res); |
| EXPECT_NOT_POISONED(s); |
| } |
| |
| struct StructWithDtor { |
| ~StructWithDtor(); |
| }; |
| |
| NOINLINE StructWithDtor::~StructWithDtor() { |
| break_optimization(0); |
| } |
| |
| TEST(MemorySanitizer, Invoke) { |
| StructWithDtor s; // Will cause the calls to become invokes. |
| EXPECT_NOT_POISONED(0); |
| EXPECT_POISONED(*GetPoisoned<int>()); |
| EXPECT_NOT_POISONED(0); |
| EXPECT_POISONED(*GetPoisoned<int>()); |
| EXPECT_POISONED(ReturnPoisoned<S4>()); |
| } |
| |
| TEST(MemorySanitizer, ptrtoint) { |
| // Test that shadow is propagated through pointer-to-integer conversion. |
| unsigned char c = 0; |
| __msan_poison(&c, 1); |
| uintptr_t u = (uintptr_t)c << 8; |
| EXPECT_NOT_POISONED(u & 0xFF00FF); |
| EXPECT_POISONED(u & 0xFF00); |
| |
| break_optimization(&u); |
| void* p = (void*)u; |
| |
| break_optimization(&p); |
| EXPECT_POISONED(p); |
| EXPECT_NOT_POISONED(((uintptr_t)p) & 0xFF00FF); |
| EXPECT_POISONED(((uintptr_t)p) & 0xFF00); |
| } |
| |
| static void vaargsfn2(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, double)); |
| va_end(vl); |
| } |
| |
| static void vaargsfn(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| // The following call will overwrite __msan_param_tls. |
| // Checks after it test that arg shadow was somehow saved across the call. |
| vaargsfn2(1, 2, 3, 4, *GetPoisoned<double>()); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgTest) { |
| int* x = GetPoisoned<int>(); |
| int* y = GetPoisoned<int>(4); |
| vaargsfn(1, 13, *x, 42, *y); |
| } |
| |
| static void vaargsfn_many(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgManyTest) { |
| int* x = GetPoisoned<int>(); |
| int* y = GetPoisoned<int>(4); |
| vaargsfn_many(1, 2, *x, 3, 4, 5, 6, 7, 8, 9, *y); |
| } |
| |
| static void vaargsfn_manyfix(int g1, int g2, int g3, int g4, int g5, int g6, int g7, int g8, int g9, ...) { |
| va_list vl; |
| va_start(vl, g9); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgManyFixTest) { |
| int* x = GetPoisoned<int>(); |
| int* y = GetPoisoned<int>(); |
| vaargsfn_manyfix(1, *x, 3, 4, 5, 6, 7, 8, 9, 10, *y); |
| } |
| |
| static void vaargsfn_pass2(va_list vl) { |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| } |
| |
| static void vaargsfn_pass(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_POISONED(va_arg(vl, int)); |
| vaargsfn_pass2(vl); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgPass) { |
| int* x = GetPoisoned<int>(); |
| int* y = GetPoisoned<int>(4); |
| vaargsfn_pass(1, *x, 2, 3, *y); |
| } |
| |
| static void vaargsfn_copy2(va_list vl) { |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| } |
| |
| static void vaargsfn_copy(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_list vl2; |
| va_copy(vl2, vl); |
| vaargsfn_copy2(vl2); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgCopy) { |
| int* x = GetPoisoned<int>(); |
| int* y = GetPoisoned<int>(4); |
| vaargsfn_copy(1, 2, *x, 3, *y); |
| } |
| |
| static void vaargsfn_ptr(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| EXPECT_POISONED(va_arg(vl, int*)); |
| EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| EXPECT_POISONED(va_arg(vl, double*)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgPtr) { |
| int** x = GetPoisoned<int*>(); |
| double** y = GetPoisoned<double*>(8); |
| int z; |
| vaargsfn_ptr(1, &z, *x, &z, *y); |
| } |
| |
| static void vaargsfn_overflow(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_POISONED(va_arg(vl, double)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_POISONED(va_arg(vl, int*)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| |
| EXPECT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, double)); |
| EXPECT_POISONED(va_arg(vl, int*)); |
| |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| EXPECT_NOT_POISONED(va_arg(vl, double)); |
| EXPECT_NOT_POISONED(va_arg(vl, int*)); |
| |
| EXPECT_POISONED(va_arg(vl, int)); |
| EXPECT_POISONED(va_arg(vl, double)); |
| EXPECT_POISONED(va_arg(vl, int*)); |
| |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgOverflow) { |
| int* x = GetPoisoned<int>(); |
| double* y = GetPoisoned<double>(8); |
| int** p = GetPoisoned<int*>(16); |
| int z; |
| vaargsfn_overflow(1, |
| 1, 2, *x, 4, 5, 6, |
| 1.1, 2.2, 3.3, *y, 5.5, *p, 7.7, 8.8, |
| // the following args will overflow for sure |
| *x, *y, *p, |
| 7, 9.9, &z, |
| *x, *y, *p); |
| } |
| |
| static void vaargsfn_tlsoverwrite2(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| for (int i = 0; i < 20; ++i) |
| EXPECT_NOT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| static void vaargsfn_tlsoverwrite(int guard, ...) { |
| // This call will overwrite TLS contents unless it's backed up somewhere. |
| vaargsfn_tlsoverwrite2(2, |
| 42, 42, 42, 42, 42, |
| 42, 42, 42, 42, 42, |
| 42, 42, 42, 42, 42, |
| 42, 42, 42, 42, 42); // 20x |
| va_list vl; |
| va_start(vl, guard); |
| for (int i = 0; i < 20; ++i) |
| EXPECT_POISONED(va_arg(vl, int)); |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgTLSOverwrite) { |
| int* x = GetPoisoned<int>(); |
| vaargsfn_tlsoverwrite(1, |
| *x, *x, *x, *x, *x, |
| *x, *x, *x, *x, *x, |
| *x, *x, *x, *x, *x, |
| *x, *x, *x, *x, *x); // 20x |
| |
| } |
| |
| struct StructByVal { |
| int a, b, c, d, e, f; |
| }; |
| |
| static void vaargsfn_structbyval(int guard, ...) { |
| va_list vl; |
| va_start(vl, guard); |
| { |
| StructByVal s = va_arg(vl, StructByVal); |
| EXPECT_NOT_POISONED(s.a); |
| EXPECT_POISONED(s.b); |
| EXPECT_NOT_POISONED(s.c); |
| EXPECT_POISONED(s.d); |
| EXPECT_NOT_POISONED(s.e); |
| EXPECT_POISONED(s.f); |
| } |
| { |
| StructByVal s = va_arg(vl, StructByVal); |
| EXPECT_NOT_POISONED(s.a); |
| EXPECT_POISONED(s.b); |
| EXPECT_NOT_POISONED(s.c); |
| EXPECT_POISONED(s.d); |
| EXPECT_NOT_POISONED(s.e); |
| EXPECT_POISONED(s.f); |
| } |
| va_end(vl); |
| } |
| |
| TEST(MemorySanitizer, VAArgStructByVal) { |
| StructByVal s; |
| s.a = 1; |
| s.b = *GetPoisoned<int>(); |
| s.c = 2; |
| s.d = *GetPoisoned<int>(); |
| s.e = 3; |
| s.f = *GetPoisoned<int>(); |
| vaargsfn_structbyval(0, s, s); |
| } |
| |
| NOINLINE void StructByValTestFunc(struct StructByVal s) { |
| EXPECT_NOT_POISONED(s.a); |
| EXPECT_POISONED(s.b); |
| EXPECT_NOT_POISONED(s.c); |
| EXPECT_POISONED(s.d); |
| EXPECT_NOT_POISONED(s.e); |
| EXPECT_POISONED(s.f); |
| } |
| |
| NOINLINE void StructByValTestFunc1(struct StructByVal s) { |
| StructByValTestFunc(s); |
| } |
| |
| NOINLINE void StructByValTestFunc2(int z, struct StructByVal s) { |
| StructByValTestFunc(s); |
| } |
| |
| TEST(MemorySanitizer, StructByVal) { |
| // Large aggregates are passed as "byval" pointer argument in LLVM. |
| struct StructByVal s; |
| s.a = 1; |
| s.b = *GetPoisoned<int>(); |
| s.c = 2; |
| s.d = *GetPoisoned<int>(); |
| s.e = 3; |
| s.f = *GetPoisoned<int>(); |
| StructByValTestFunc(s); |
| StructByValTestFunc1(s); |
| StructByValTestFunc2(0, s); |
| } |
| |
| |
| #if MSAN_HAS_M128 |
| NOINLINE __m128i m128Eq(__m128i *a, __m128i *b) { return _mm_cmpeq_epi16(*a, *b); } |
| NOINLINE __m128i m128Lt(__m128i *a, __m128i *b) { return _mm_cmplt_epi16(*a, *b); } |
| TEST(MemorySanitizer, m128) { |
| __m128i a = _mm_set1_epi16(0x1234); |
| __m128i b = _mm_set1_epi16(0x7890); |
| EXPECT_NOT_POISONED(m128Eq(&a, &b)); |
| EXPECT_NOT_POISONED(m128Lt(&a, &b)); |
| } |
| // FIXME: add more tests for __m128i. |
| #endif // MSAN_HAS_M128 |
| |
| // We should not complain when copying this poisoned hole. |
| struct StructWithHole { |
| U4 a; |
| // 4-byte hole. |
| U8 b; |
| }; |
| |
| NOINLINE StructWithHole ReturnStructWithHole() { |
| StructWithHole res; |
| __msan_poison(&res, sizeof(res)); |
| res.a = 1; |
| res.b = 2; |
| return res; |
| } |
| |
| TEST(MemorySanitizer, StructWithHole) { |
| StructWithHole a = ReturnStructWithHole(); |
| break_optimization(&a); |
| } |
| |
| template <class T> |
| NOINLINE T ReturnStruct() { |
| T res; |
| __msan_poison(&res, sizeof(res)); |
| res.a = 1; |
| return res; |
| } |
| |
| template <class T> |
| NOINLINE void TestReturnStruct() { |
| T s1 = ReturnStruct<T>(); |
| EXPECT_NOT_POISONED(s1.a); |
| EXPECT_POISONED(s1.b); |
| } |
| |
| struct SSS1 { |
| int a, b, c; |
| }; |
| struct SSS2 { |
| int b, a, c; |
| }; |
| struct SSS3 { |
| int b, c, a; |
| }; |
| struct SSS4 { |
| int c, b, a; |
| }; |
| |
| struct SSS5 { |
| int a; |
| float b; |
| }; |
| struct SSS6 { |
| int a; |
| double b; |
| }; |
| struct SSS7 { |
| S8 b; |
| int a; |
| }; |
| struct SSS8 { |
| S2 b; |
| S8 a; |
| }; |
| |
| TEST(MemorySanitizer, IntStruct3) { |
| TestReturnStruct<SSS1>(); |
| TestReturnStruct<SSS2>(); |
| TestReturnStruct<SSS3>(); |
| TestReturnStruct<SSS4>(); |
| TestReturnStruct<SSS5>(); |
| TestReturnStruct<SSS6>(); |
| TestReturnStruct<SSS7>(); |
| TestReturnStruct<SSS8>(); |
| } |
| |
| struct LongStruct { |
| U1 a1, b1; |
| U2 a2, b2; |
| U4 a4, b4; |
| U8 a8, b8; |
| }; |
| |
| NOINLINE LongStruct ReturnLongStruct1() { |
| LongStruct res; |
| __msan_poison(&res, sizeof(res)); |
| res.a1 = res.a2 = res.a4 = res.a8 = 111; |
| // leaves b1, .., b8 poisoned. |
| return res; |
| } |
| |
| NOINLINE LongStruct ReturnLongStruct2() { |
| LongStruct res; |
| __msan_poison(&res, sizeof(res)); |
| res.b1 = res.b2 = res.b4 = res.b8 = 111; |
| // leaves a1, .., a8 poisoned. |
| return res; |
| } |
| |
| TEST(MemorySanitizer, LongStruct) { |
| LongStruct s1 = ReturnLongStruct1(); |
| __msan_print_shadow(&s1, sizeof(s1)); |
| EXPECT_NOT_POISONED(s1.a1); |
| EXPECT_NOT_POISONED(s1.a2); |
| EXPECT_NOT_POISONED(s1.a4); |
| EXPECT_NOT_POISONED(s1.a8); |
| |
| EXPECT_POISONED(s1.b1); |
| EXPECT_POISONED(s1.b2); |
| EXPECT_POISONED(s1.b4); |
| EXPECT_POISONED(s1.b8); |
| |
| LongStruct s2 = ReturnLongStruct2(); |
| __msan_print_shadow(&s2, sizeof(s2)); |
| EXPECT_NOT_POISONED(s2.b1); |
| EXPECT_NOT_POISONED(s2.b2); |
| EXPECT_NOT_POISONED(s2.b4); |
| EXPECT_NOT_POISONED(s2.b8); |
| |
| EXPECT_POISONED(s2.a1); |
| EXPECT_POISONED(s2.a2); |
| EXPECT_POISONED(s2.a4); |
| EXPECT_POISONED(s2.a8); |
| } |
| |
| TEST(MemorySanitizer, getrlimit) { |
| struct rlimit limit; |
| __msan_poison(&limit, sizeof(limit)); |
| int result = getrlimit(RLIMIT_DATA, &limit); |
| ASSERT_EQ(result, 0); |
| EXPECT_NOT_POISONED(limit.rlim_cur); |
| EXPECT_NOT_POISONED(limit.rlim_max); |
| |
| struct rlimit limit2; |
| __msan_poison(&limit2, sizeof(limit2)); |
| result = prlimit(getpid(), |