summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/tests
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sanitizer_common/tests')
-rw-r--r--lib/sanitizer_common/tests/CMakeLists.txt34
-rw-r--r--lib/sanitizer_common/tests/sanitizer_allocator_test.cc16
-rw-r--r--lib/sanitizer_common/tests/sanitizer_allocator_testlib.cc12
-rw-r--r--lib/sanitizer_common/tests/sanitizer_flags_test.cc6
-rw-r--r--lib/sanitizer_common/tests/sanitizer_ioctl_test.cc6
-rw-r--r--lib/sanitizer_common/tests/sanitizer_linux_test.cc35
-rw-r--r--lib/sanitizer_common/tests/sanitizer_posix_test.cc1
-rw-r--r--lib/sanitizer_common/tests/sanitizer_printf_test.cc6
-rw-r--r--lib/sanitizer_common/tests/sanitizer_procmaps_test.cc8
-rw-r--r--lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h4
-rw-r--r--lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc13
-rw-r--r--lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc12
-rw-r--r--lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc4
13 files changed, 131 insertions, 26 deletions
diff --git a/lib/sanitizer_common/tests/CMakeLists.txt b/lib/sanitizer_common/tests/CMakeLists.txt
index 18b76369a675f..0a828dc134862 100644
--- a/lib/sanitizer_common/tests/CMakeLists.txt
+++ b/lib/sanitizer_common/tests/CMakeLists.txt
@@ -44,7 +44,7 @@ foreach(header ${SANITIZER_HEADERS})
endforeach()
set(SANITIZER_TEST_CFLAGS_COMMON
- ${COMPILER_RT_TEST_CFLAGS}
+ ${COMPILER_RT_UNITTEST_CFLAGS}
${COMPILER_RT_GTEST_CFLAGS}
-I${COMPILER_RT_SOURCE_DIR}/include
-I${COMPILER_RT_SOURCE_DIR}/lib
@@ -65,6 +65,10 @@ if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
else()
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -g)
endif()
+if(MSVC)
+ list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
+endif()
+list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)
if(NOT MSVC)
list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
@@ -74,6 +78,13 @@ if(ANDROID)
list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -pie)
endif()
+# MSVC linker is allocating 1M for the stack by default, which is not
+# enough for the unittests. Some unittests require more than 2M.
+# The default stack size for clang is 8M.
+if(MSVC)
+ list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,/STACK:0xC00000)
+endif()
+
set(SANITIZER_TEST_LINK_LIBS)
append_list_if(COMPILER_RT_HAS_LIBLOG log SANITIZER_TEST_LINK_LIBS)
# NDK r10 requires -latomic almost always.
@@ -96,7 +107,8 @@ include_directories(../..)
macro(add_sanitizer_common_lib library)
add_library(${library} STATIC ${ARGN})
set_target_properties(${library} PROPERTIES
- ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+ ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ FOLDER "Compiler-RT Runtime tests")
endmacro()
function(get_sanitizer_common_lib_for_arch arch lib lib_name)
@@ -106,17 +118,21 @@ function(get_sanitizer_common_lib_for_arch arch lib lib_name)
set(tgt_name "RTSanitizerCommon.test.${arch}")
endif()
set(${lib} "${tgt_name}" PARENT_SCOPE)
+ if(CMAKE_CONFIGURATION_TYPES)
+ set(configuration_path "${CMAKE_CFG_INTDIR}/")
+ else()
+ set(configuration_path "")
+ endif()
if(NOT MSVC)
- set(${lib_name} "lib${tgt_name}.a" PARENT_SCOPE)
+ set(${lib_name} "${configuration_path}lib${tgt_name}.a" PARENT_SCOPE)
else()
- set(${lib_name} "${tgt_name}.lib" PARENT_SCOPE)
+ set(${lib_name} "${configuration_path}${tgt_name}.lib" PARENT_SCOPE)
endif()
endfunction()
# Sanitizer_common unit tests testsuite.
add_custom_target(SanitizerUnitTests)
-set_target_properties(SanitizerUnitTests PROPERTIES
- FOLDER "Sanitizer unittests")
+set_target_properties(SanitizerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
# Adds sanitizer tests for architecture.
macro(add_sanitizer_tests_for_arch arch)
@@ -130,7 +146,11 @@ macro(add_sanitizer_tests_for_arch arch)
set(SANITIZER_TEST_OBJECTS)
foreach(source ${SANITIZER_TEST_SOURCES})
get_filename_component(basename ${source} NAME)
- set(output_obj "${basename}.${arch}.o")
+ if(CMAKE_CONFIGURATION_TYPES)
+ set(output_obj "${CMAKE_CFG_INTDIR}/${basename}.${arch}.o")
+ else()
+ set(output_obj "${basename}.${arch}.o")
+ endif()
clang_compile(${output_obj} ${source}
CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
DEPS ${SANITIZER_TEST_COMPILE_DEPS})
diff --git a/lib/sanitizer_common/tests/sanitizer_allocator_test.cc b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
index 7ba334568146a..31eec19c3632e 100644
--- a/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
@@ -29,9 +29,15 @@
#if !SANITIZER_DEBUG
#if SANITIZER_CAN_USE_ALLOCATOR64
+#if SANITIZER_WINDOWS
+static const uptr kAllocatorSpace = 0x10000000000ULL;
+static const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
+static const u64 kAddressSpaceSize = 1ULL << 40;
+#else
static const uptr kAllocatorSpace = 0x700000000000ULL;
static const uptr kAllocatorSize = 0x010000000000ULL; // 1T.
static const u64 kAddressSpaceSize = 1ULL << 47;
+#endif
typedef SizeClassAllocator64<
kAllocatorSpace, kAllocatorSize, 16, DefaultSizeClassMap> Allocator64;
@@ -42,6 +48,10 @@ typedef SizeClassAllocator64<
static const u64 kAddressSpaceSize = 1ULL << 40;
#elif defined(__aarch64__)
static const u64 kAddressSpaceSize = 1ULL << 39;
+#elif defined(__s390x__)
+static const u64 kAddressSpaceSize = 1ULL << 53;
+#elif defined(__s390__)
+static const u64 kAddressSpaceSize = 1ULL << 31;
#else
static const u64 kAddressSpaceSize = 1ULL << 32;
#endif
@@ -331,7 +341,6 @@ TEST(SanitizerCommon, SizeClassAllocator64Overflow) {
}
#endif
-#if !defined(_WIN32) // FIXME: This currently fails on Windows.
TEST(SanitizerCommon, LargeMmapAllocator) {
LargeMmapAllocator<> a;
a.Init(/* may_return_null */ false);
@@ -407,7 +416,6 @@ TEST(SanitizerCommon, LargeMmapAllocator) {
CHECK_NE(p, (char *)a.GetBlockBegin(p + page_size));
a.Deallocate(&stats, p);
}
-#endif
template
<class PrimaryAllocator, class SecondaryAllocator, class AllocatorCache>
@@ -479,13 +487,11 @@ TEST(SanitizerCommon, CombinedAllocator64Compact) {
}
#endif
-#if !defined(_WIN32) // FIXME: This currently fails on Windows.
TEST(SanitizerCommon, CombinedAllocator32Compact) {
TestCombinedAllocator<Allocator32Compact,
LargeMmapAllocator<>,
SizeClassAllocatorLocalCache<Allocator32Compact> > ();
}
-#endif
template <class AllocatorCache>
void TestSizeClassAllocatorLocalCache() {
@@ -601,6 +607,8 @@ TEST(Allocator, AllocatorCacheDeallocNewThread) {
pthread_t t;
PTHREAD_CREATE(&t, 0, DeallocNewThreadWorker, params);
PTHREAD_JOIN(t, 0);
+
+ allocator.TestOnlyUnmap();
}
#endif
diff --git a/lib/sanitizer_common/tests/sanitizer_allocator_testlib.cc b/lib/sanitizer_common/tests/sanitizer_allocator_testlib.cc
index 0cc3b9ba6944c..038d9c543bb7d 100644
--- a/lib/sanitizer_common/tests/sanitizer_allocator_testlib.cc
+++ b/lib/sanitizer_common/tests/sanitizer_allocator_testlib.cc
@@ -11,9 +11,10 @@
// for CombinedAllocator.
//===----------------------------------------------------------------------===//
/* Usage:
-clang++ -fno-exceptions -g -fPIC -I. -I../include -Isanitizer \
+clang++ -std=c++11 -fno-exceptions -g -fPIC -I. -I../include -Isanitizer \
sanitizer_common/tests/sanitizer_allocator_testlib.cc \
- sanitizer_common/sanitizer_*.cc -shared -lpthread -o testmalloc.so
+ $(\ls sanitizer_common/sanitizer_*.cc | grep -v sanitizer_common_nolibc.cc) \
+ -shared -lpthread -o testmalloc.so
LD_PRELOAD=`pwd`/testmalloc.so /your/app
*/
#include "sanitizer_common/sanitizer_allocator.h"
@@ -36,7 +37,8 @@ namespace {
static const uptr kAllocatorSpace = 0x600000000000ULL;
static const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
-typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0,
+// typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0,
+typedef SizeClassAllocator64<~(uptr)0, kAllocatorSize, 0,
CompactSizeClassMap> PrimaryAllocator;
typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
typedef LargeMmapAllocator<> SecondaryAllocator;
@@ -60,12 +62,12 @@ static void thread_dtor(void *v) {
static void NOINLINE thread_init() {
if (!global_inited) {
global_inited = true;
- allocator.Init();
+ allocator.Init(false /*may_return_null*/);
pthread_key_create(&pkey, thread_dtor);
}
thread_inited = true;
pthread_setspecific(pkey, (void*)1);
- cache.Init();
+ cache.Init(nullptr);
}
} // namespace
diff --git a/lib/sanitizer_common/tests/sanitizer_flags_test.cc b/lib/sanitizer_common/tests/sanitizer_flags_test.cc
index 3e5d8381ed3a8..24a3f3d3c7ebe 100644
--- a/lib/sanitizer_common/tests/sanitizer_flags_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_flags_test.cc
@@ -47,6 +47,9 @@ void TestFlag(const char *start_value, const char *env,
parser.ParseString(env);
EXPECT_EQ(0, internal_strcmp(final_value, flag));
+
+ // Reporting unrecognized flags is needed to reset them.
+ ReportUnrecognizedFlags();
}
TEST(SanitizerCommon, BooleanFlags) {
@@ -97,6 +100,9 @@ static void TestTwoFlags(const char *env, bool expected_flag1,
EXPECT_EQ(expected_flag1, flag1);
EXPECT_EQ(0, internal_strcmp(flag2, expected_flag2));
+
+ // Reporting unrecognized flags is needed to reset them.
+ ReportUnrecognizedFlags();
}
TEST(SanitizerCommon, MultipleFlags) {
diff --git a/lib/sanitizer_common/tests/sanitizer_ioctl_test.cc b/lib/sanitizer_common/tests/sanitizer_ioctl_test.cc
index 22fa5228dfbd2..6e2a20b85d6ab 100644
--- a/lib/sanitizer_common/tests/sanitizer_ioctl_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_ioctl_test.cc
@@ -78,7 +78,8 @@ TEST(SanitizerIoctl, Fixup) {
// Test decoding KVM ioctl numbers.
TEST(SanitizerIoctl, KVM_GET_MP_STATE) {
ioctl_desc desc;
- bool res = ioctl_decode(0x8004ae98U, &desc);
+ unsigned int desc_value = SANITIZER_MIPS ? 0x4004ae98U : 0x8004ae98U;
+ bool res = ioctl_decode(desc_value, &desc);
EXPECT_TRUE(res);
EXPECT_EQ(ioctl_desc::WRITE, desc.type);
EXPECT_EQ(4U, desc.size);
@@ -86,7 +87,8 @@ TEST(SanitizerIoctl, KVM_GET_MP_STATE) {
TEST(SanitizerIoctl, KVM_GET_LAPIC) {
ioctl_desc desc;
- bool res = ioctl_decode(0x8400ae8eU, &desc);
+ unsigned int desc_value = SANITIZER_MIPS ? 0x4400ae8eU : 0x8400ae8eU;
+ bool res = ioctl_decode(desc_value, &desc);
EXPECT_TRUE(res);
EXPECT_EQ(ioctl_desc::WRITE, desc.type);
EXPECT_EQ(1024U, desc.size);
diff --git a/lib/sanitizer_common/tests/sanitizer_linux_test.cc b/lib/sanitizer_common/tests/sanitizer_linux_test.cc
index eef71010afe65..fb6b109ee23b7 100644
--- a/lib/sanitizer_common/tests/sanitizer_linux_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_linux_test.cc
@@ -263,6 +263,41 @@ TEST(SanitizerLinux, ThreadSelfTest) {
}
#endif
+TEST(SanitizerCommon, StartSubprocessTest) {
+ int pipe_fds[2];
+ ASSERT_EQ(0, pipe(pipe_fds));
+#if SANITIZER_ANDROID
+ const char *shell = "/system/bin/sh";
+#else
+ const char *shell = "/bin/sh";
+#endif
+ const char *argv[] = {shell, "-c", "echo -n 'hello'", (char *)NULL};
+ int pid = StartSubprocess(shell, argv,
+ /* stdin */ kInvalidFd, /* stdout */ pipe_fds[1]);
+ ASSERT_GT(pid, 0);
+
+ // wait for process to finish.
+ while (IsProcessRunning(pid)) {
+ }
+ ASSERT_FALSE(IsProcessRunning(pid));
+
+ char buffer[256];
+ {
+ char *ptr = buffer;
+ uptr bytes_read;
+ while (ReadFromFile(pipe_fds[0], ptr, 256, &bytes_read)) {
+ if (!bytes_read) {
+ break;
+ }
+ ptr += bytes_read;
+ }
+ ASSERT_EQ(5, ptr - buffer);
+ *ptr = 0;
+ }
+ ASSERT_EQ(0, strcmp(buffer, "hello")) << "Buffer: " << buffer;
+ internal_close(pipe_fds[0]);
+}
+
} // namespace __sanitizer
#endif // SANITIZER_LINUX
diff --git a/lib/sanitizer_common/tests/sanitizer_posix_test.cc b/lib/sanitizer_common/tests/sanitizer_posix_test.cc
index 03ca449d36223..b7cca83622aa7 100644
--- a/lib/sanitizer_common/tests/sanitizer_posix_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_posix_test.cc
@@ -56,6 +56,7 @@ TEST(SanitizerCommon, PthreadDestructorIterations) {
EXPECT_TRUE(destructor_executed);
SpawnThread(GetPthreadDestructorIterations() + 1);
EXPECT_FALSE(destructor_executed);
+ ASSERT_EQ(0, pthread_key_delete(key));
}
TEST(SanitizerCommon, IsAccessibleMemoryRange) {
diff --git a/lib/sanitizer_common/tests/sanitizer_printf_test.cc b/lib/sanitizer_common/tests/sanitizer_printf_test.cc
index 5e39e0a591d6e..5a77b470caf5c 100644
--- a/lib/sanitizer_common/tests/sanitizer_printf_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_printf_test.cc
@@ -23,9 +23,9 @@ TEST(Printf, Basic) {
char buf[1024];
uptr len = internal_snprintf(buf, sizeof(buf),
"a%db%zdc%ue%zuf%xh%zxq%pe%sr",
- (int)-1, (long)-2, // NOLINT
- (unsigned)-4, (unsigned long)5, // NOLINT
- (unsigned)10, (unsigned long)11, // NOLINT
+ (int)-1, (uptr)-2, // NOLINT
+ (unsigned)-4, (uptr)5, // NOLINT
+ (unsigned)10, (uptr)11, // NOLINT
(void*)0x123, "_string_");
EXPECT_EQ(len, strlen(buf));
diff --git a/lib/sanitizer_common/tests/sanitizer_procmaps_test.cc b/lib/sanitizer_common/tests/sanitizer_procmaps_test.cc
index 12bc9e18193a5..ae7c5d531ae75 100644
--- a/lib/sanitizer_common/tests/sanitizer_procmaps_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_procmaps_test.cc
@@ -37,11 +37,11 @@ TEST(MemoryMappingLayout, DumpListOfModules) {
const char *binary_name = last_slash ? last_slash + 1 : argv0;
MemoryMappingLayout memory_mapping(false);
const uptr kMaxModules = 100;
- LoadedModule modules[kMaxModules];
- uptr n_modules = memory_mapping.DumpListOfModules(modules, kMaxModules, 0);
- EXPECT_GT(n_modules, 0U);
+ InternalMmapVector<LoadedModule> modules(kMaxModules);
+ memory_mapping.DumpListOfModules(&modules);
+ EXPECT_GT(modules.size(), 0U);
bool found = false;
- for (uptr i = 0; i < n_modules; ++i) {
+ for (uptr i = 0; i < modules.size(); ++i) {
if (modules[i].containsAddress((uptr)&noop)) {
// Verify that the module name is sane.
if (strstr(modules[i].full_name(), binary_name) != 0)
diff --git a/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h b/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h
index 47b0f97de840d..b7d784c2566df 100644
--- a/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h
+++ b/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h
@@ -48,7 +48,9 @@ inline void PTHREAD_CREATE(pthread_t *thread, void *attr,
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.";
+ DWORD err = GetLastError();
+ ASSERT_NE(nullptr, *thread) << "Failed to create a thread, got error 0x"
+ << std::hex << err;
}
inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) {
diff --git a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
index 3d57eded948ff..ba9f4fd693265 100644
--- a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
@@ -136,6 +136,19 @@ TEST_F(FastUnwindTest, FPBelowPrevFP) {
EXPECT_EQ(PC(1), trace.trace[1]);
}
+TEST_F(FastUnwindTest, CloseToZeroFrame) {
+ // Make one pc a NULL pointer.
+ fake_stack[5] = 0x0;
+ if (!TryFastUnwind(kStackTraceMax))
+ return;
+ // The stack should be truncated at the NULL pointer (and not include it).
+ EXPECT_EQ(3U, trace.size);
+ EXPECT_EQ(start_pc, trace.trace[0]);
+ for (uptr i = 1; i < 3U; i++) {
+ EXPECT_EQ(PC(i*2 - 1), trace.trace[i]);
+ }
+}
+
TEST(SlowUnwindTest, ShortStackTrace) {
if (StackTrace::WillUseFastUnwind(false))
return;
diff --git a/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc b/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
index 429ac591e5023..3d5678ab7fb87 100644
--- a/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
@@ -55,4 +55,16 @@ TEST(Symbolizer, ExtractTokenUpToDelimiter) {
InternalFree(token);
}
+#if !SANITIZER_WINDOWS
+TEST(Symbolizer, DemangleSwiftAndCXX) {
+ // Swift names are not demangled in default llvm build because Swift
+ // runtime is not linked in.
+ EXPECT_STREQ("_TtSd", DemangleSwiftAndCXX("_TtSd"));
+ // Check that the rest demangles properly.
+ EXPECT_STREQ("f1(char*, int)", DemangleSwiftAndCXX("_Z2f1Pci"));
+ EXPECT_STREQ("foo", DemangleSwiftAndCXX("foo"));
+ EXPECT_STREQ("", DemangleSwiftAndCXX(""));
+}
+#endif
+
} // namespace __sanitizer
diff --git a/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc b/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc
index 58c627a704ffc..1132bfdf62e32 100644
--- a/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc
@@ -224,6 +224,10 @@ static void ThreadedTestRegistry(ThreadRegistry *registry) {
}
TEST(SanitizerCommon, ThreadRegistryThreadedTest) {
+ memset(&num_created, 0, sizeof(num_created));
+ memset(&num_started, 0, sizeof(num_created));
+ memset(&num_joined, 0, sizeof(num_created));
+
ThreadRegistry registry(GetThreadContext<TestThreadContext>,
kThreadsPerShard * kNumShards + 1, 10);
ThreadedTestRegistry(&registry);