[compiler-rt] r208674 - [Sanitizer tests] Make simple pthread tests compile and pass on Windows
Timur Iskhodzhanov
timurrrr at google.com
Tue May 13 01:07:09 PDT 2014
Author: timurrrr
Date: Tue May 13 03:07:09 2014
New Revision: 208674
URL: http://llvm.org/viewvc/llvm-project?rev=208674&view=rev
Log:
[Sanitizer tests] Make simple pthread tests compile and pass on Windows
Reviewed at http://reviews.llvm.org/D3725
Added:
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h (with props)
Modified:
compiler-rt/trunk/lib/asan/tests/asan_test_utils.h
compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_test_utils.h
Modified: compiler-rt/trunk/lib/asan/tests/asan_test_utils.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_test_utils.h?rev=208674&r1=208673&r2=208674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_test_utils.h (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_test_utils.h Tue May 13 03:07:09 2014
@@ -21,6 +21,8 @@
#endif
#include "sanitizer_test_utils.h"
+#include "sanitizer_pthread_wrappers.h"
+
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
@@ -30,10 +32,9 @@
#include <algorithm>
#if !defined(_WIN32)
-#include <strings.h>
-#include <pthread.h>
-#include <sys/mman.h>
-#include <setjmp.h>
+# include <strings.h>
+# include <sys/mman.h>
+# include <setjmp.h>
#endif
#ifdef __linux__
@@ -48,10 +49,6 @@
#include <malloc.h>
#endif
-// Check that pthread_create/pthread_join return success.
-#define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
-#define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
-
#if ASAN_HAS_EXCEPTIONS
# define ASAN_THROW(x) throw (x)
#else
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt?rev=208674&r1=208673&r2=208674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt Tue May 13 03:07:09 2014
@@ -26,6 +26,7 @@ set(SANITIZER_UNITTESTS
sanitizer_thread_registry_test.cc)
set(SANITIZER_TEST_HEADERS
+ sanitizer_pthread_wrappers.h
sanitizer_test_utils.h)
foreach(header ${SANITIZER_HEADERS})
list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
Added: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h?rev=208674&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h (added)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h Tue May 13 03:07:09 2014
@@ -0,0 +1,68 @@
+//===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
+//
+// 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 *Sanitizer runtime.
+// It provides handy wrappers for thread manipulation, that:
+// a) assert on any failure rather than returning an error code
+// b) defines pthread-like interface on platforms where where <pthread.h>
+// is not supplied by default.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SANITIZER_PTHREAD_WRAPPERS_H
+#define SANITIZER_PTHREAD_WRAPPERS_H
+
+#include "sanitizer_test_utils.h"
+
+#include "gtest/gtest.h"
+
+#if !defined(_WIN32)
+# include <pthread.h>
+// Simply forward the arguments and check that the pthread functions succeed.
+# define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
+# define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
+#else
+typedef HANDLE pthread_t;
+
+struct PthreadHelperCreateThreadInfo {
+ void *(*start_routine)(void *);
+ void *arg;
+};
+
+inline DWORD WINAPI PthreadHelperThreadProc(void *arg) {
+ PthreadHelperCreateThreadInfo *start_data =
+ reinterpret_cast<PthreadHelperCreateThreadInfo*>(arg);
+ void *ret = (start_data->start_routine)(start_data->arg);
+ delete start_data;
+ return (DWORD)ret;
+}
+
+inline void PTHREAD_CREATE(pthread_t *thread, void *attr,
+ void *(*start_routine)(void *), void *arg) {
+ ASSERT_EQ(0, attr) << "Thread attributes are not supported yet.";
+ PthreadHelperCreateThreadInfo *data = new PthreadHelperCreateThreadInfo;
+ data->start_routine = start_routine;
+ data->arg = arg;
+ *thread = CreateThread(0, 0, PthreadHelperThreadProc, data, 0, 0);
+ ASSERT_NE(nullptr, *thread) << "Failed to create a thread.";
+}
+
+inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) {
+ ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet.";
+ ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE));
+ ASSERT_NE(0, CloseHandle(thread));
+}
+
+inline void pthread_exit(void *retval) {
+ ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet.";
+ ExitThread((DWORD)retval);
+}
+#endif // _WIN32
+
+#endif // SANITIZER_PTHREAD_WRAPPERS_H
Propchange: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h
------------------------------------------------------------------------------
svn:eol-style = LF
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_test_utils.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_test_utils.h?rev=208674&r1=208673&r2=208674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_test_utils.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_test_utils.h Tue May 13 03:07:09 2014
@@ -15,6 +15,14 @@
#ifndef SANITIZER_TEST_UTILS_H
#define SANITIZER_TEST_UTILS_H
+#if defined(_WIN32)
+// <windows.h> should always be the first include on Windows.
+# include <windows.h>
+// MSVS headers define max/min as macros, so std::max/min gets crazy.
+# undef max
+# undef min
+#endif
+
#include <stdint.h>
#if defined(_MSC_VER)
More information about the llvm-commits
mailing list