aboutsummaryrefslogtreecommitdiff
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.txt11
-rw-r--r--lib/sanitizer_common/tests/sanitizer_allocator_test.cc4
-rw-r--r--lib/sanitizer_common/tests/sanitizer_common_test.cc33
-rw-r--r--lib/sanitizer_common/tests/sanitizer_libc_test.cc108
-rw-r--r--lib/sanitizer_common/tests/sanitizer_linux_test.cc2
-rw-r--r--lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc2
-rw-r--r--lib/sanitizer_common/tests/sanitizer_suppressions_test.cc5
-rw-r--r--lib/sanitizer_common/tests/sanitizer_test_main.cc2
8 files changed, 146 insertions, 21 deletions
diff --git a/lib/sanitizer_common/tests/CMakeLists.txt b/lib/sanitizer_common/tests/CMakeLists.txt
index b0165eac25f0..18b76369a675 100644
--- a/lib/sanitizer_common/tests/CMakeLists.txt
+++ b/lib/sanitizer_common/tests/CMakeLists.txt
@@ -4,6 +4,9 @@ clang_compiler_add_cxx_check()
# FIXME: use SANITIZER_COMMON_SUPPORTED_ARCH here
filter_available_targets(SANITIZER_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el)
+if(APPLE)
+ darwin_filter_host_archs(SANITIZER_UNITTEST_SUPPORTED_ARCH SANITIZER_COMMON_SUPPORTED_ARCH)
+endif()
set(SANITIZER_UNITTESTS
sanitizer_allocator_test.cc
@@ -72,7 +75,7 @@ if(ANDROID)
endif()
set(SANITIZER_TEST_LINK_LIBS)
-append_list_if(ANDROID log SANITIZER_TEST_LINK_LIBS)
+append_list_if(COMPILER_RT_HAS_LIBLOG log SANITIZER_TEST_LINK_LIBS)
# NDK r10 requires -latomic almost always.
append_list_if(ANDROID atomic SANITIZER_TEST_LINK_LIBS)
@@ -168,11 +171,13 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID)
# be sure that produced binaries would work.
if(APPLE)
add_sanitizer_common_lib("RTSanitizerCommon.test.osx"
- $<TARGET_OBJECTS:RTSanitizerCommon.osx>)
+ $<TARGET_OBJECTS:RTSanitizerCommon.osx>
+ $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>)
else()
if(CAN_TARGET_x86_64)
add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64"
- $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>)
+ $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>
+ $<TARGET_OBJECTS:RTSanitizerCommonNoLibc.x86_64>)
endif()
foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH})
add_sanitizer_common_lib("RTSanitizerCommon.test.${arch}"
diff --git a/lib/sanitizer_common/tests/sanitizer_allocator_test.cc b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
index be8fc91aa861..7ba334568146 100644
--- a/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
@@ -40,6 +40,8 @@ typedef SizeClassAllocator64<
kAllocatorSpace, kAllocatorSize, 16, CompactSizeClassMap> Allocator64Compact;
#elif defined(__mips64)
static const u64 kAddressSpaceSize = 1ULL << 40;
+#elif defined(__aarch64__)
+static const u64 kAddressSpaceSize = 1ULL << 39;
#else
static const u64 kAddressSpaceSize = 1ULL << 32;
#endif
@@ -94,7 +96,7 @@ void TestSizeClassAllocator() {
uptr size = sizes[s];
if (!a->CanAllocate(size, 1)) continue;
// printf("s = %ld\n", size);
- uptr n_iter = std::max((uptr)6, 8000000 / size);
+ uptr n_iter = std::max((uptr)6, 4000000 / size);
// fprintf(stderr, "size: %ld iter: %ld\n", size, n_iter);
for (uptr i = 0; i < n_iter; i++) {
uptr class_id0 = Allocator::SizeClassMapT::ClassID(size);
diff --git a/lib/sanitizer_common/tests/sanitizer_common_test.cc b/lib/sanitizer_common/tests/sanitizer_common_test.cc
index e08a38c82450..6fc308ad14d4 100644
--- a/lib/sanitizer_common/tests/sanitizer_common_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_common_test.cc
@@ -188,6 +188,15 @@ TEST(SanitizerCommon, FindPathToBinary) {
InternalFree(true_path);
EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj"));
}
+#elif SANITIZER_WINDOWS
+TEST(SanitizerCommon, FindPathToBinary) {
+ // ntdll.dll should be on PATH in all supported test environments on all
+ // supported Windows versions.
+ char *ntdll_path = FindPathToBinary("ntdll.dll");
+ EXPECT_NE((char*)0, internal_strstr(ntdll_path, "ntdll.dll"));
+ InternalFree(ntdll_path);
+ EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj"));
+}
#endif
TEST(SanitizerCommon, StripPathPrefix) {
@@ -199,6 +208,30 @@ TEST(SanitizerCommon, StripPathPrefix) {
EXPECT_STREQ("file.h", StripPathPrefix("/usr/lib/./file.h", "/usr/lib/"));
}
+TEST(SanitizerCommon, RemoveANSIEscapeSequencesFromString) {
+ RemoveANSIEscapeSequencesFromString(nullptr);
+ const char *buffs[22] = {
+ "Default", "Default",
+ "\033[95mLight magenta", "Light magenta",
+ "\033[30mBlack\033[32mGreen\033[90mGray", "BlackGreenGray",
+ "\033[106mLight cyan \033[107mWhite ", "Light cyan White ",
+ "\033[31mHello\033[0m World", "Hello World",
+ "\033[38;5;82mHello \033[38;5;198mWorld", "Hello World",
+ "123[653456789012", "123[653456789012",
+ "Normal \033[5mBlink \033[25mNormal", "Normal Blink Normal",
+ "\033[106m\033[107m", "",
+ "", "",
+ " ", " ",
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(buffs); i+=2) {
+ char *buffer_copy = internal_strdup(buffs[i]);
+ RemoveANSIEscapeSequencesFromString(buffer_copy);
+ EXPECT_STREQ(buffer_copy, buffs[i+1]);
+ InternalFree(buffer_copy);
+ }
+}
+
TEST(SanitizerCommon, InternalScopedString) {
InternalScopedString str(10);
EXPECT_EQ(0U, str.length());
diff --git a/lib/sanitizer_common/tests/sanitizer_libc_test.cc b/lib/sanitizer_common/tests/sanitizer_libc_test.cc
index 3252db77653d..015e32a09e37 100644
--- a/lib/sanitizer_common/tests/sanitizer_libc_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_libc_test.cc
@@ -8,18 +8,21 @@
//===----------------------------------------------------------------------===//
// Tests for sanitizer_libc.h.
//===----------------------------------------------------------------------===//
+#include <algorithm>
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_platform.h"
#include "gtest/gtest.h"
-#if SANITIZER_LINUX || SANITIZER_MAC
-# define SANITIZER_TEST_HAS_STAT_H 1
+#if SANITIZER_WINDOWS
+#define NOMINMAX
+#include <windows.h>
+#undef NOMINMAX
+#endif
+#if SANITIZER_POSIX
# include <sys/stat.h>
# include "sanitizer_common/sanitizer_posix.h"
-#else
-# define SANITIZER_TEST_HAS_STAT_H 0
#endif
// A regression test for internal_memmove() implementation.
@@ -57,6 +60,17 @@ struct stat_and_more {
};
static void temp_file_name(char *buf, size_t bufsize, const char *prefix) {
+#if SANITIZER_WINDOWS
+ buf[0] = '\0';
+ char tmp_dir[MAX_PATH];
+ if (!::GetTempPathA(MAX_PATH, tmp_dir))
+ return;
+ // GetTempFileNameA needs a MAX_PATH buffer.
+ char tmp_path[MAX_PATH];
+ if (!::GetTempFileNameA(tmp_dir, prefix, 0, tmp_path))
+ return;
+ internal_strncpy(buf, tmp_path, bufsize);
+#else
const char *tmpdir = "/tmp";
#if SANITIZER_ANDROID
// I don't know a way to query temp directory location on Android without
@@ -67,10 +81,9 @@ static void temp_file_name(char *buf, size_t bufsize, const char *prefix) {
#endif
u32 uid = GetUid();
internal_snprintf(buf, bufsize, "%s/%s%d", tmpdir, prefix, uid);
+#endif
}
-// FIXME: File manipulations are not yet supported on Windows
-#if !defined(_WIN32)
TEST(SanitizerCommon, FileOps) {
const char *str1 = "qwerty";
uptr len1 = internal_strlen(str1);
@@ -81,16 +94,23 @@ TEST(SanitizerCommon, FileOps) {
temp_file_name(tmpfile, sizeof(tmpfile), "sanitizer_common.fileops.tmp.");
fd_t fd = OpenFile(tmpfile, WrOnly);
ASSERT_NE(fd, kInvalidFd);
- EXPECT_EQ(len1, internal_write(fd, str1, len1));
- EXPECT_EQ(len2, internal_write(fd, str2, len2));
+ uptr bytes_written = 0;
+ EXPECT_TRUE(WriteToFile(fd, str1, len1, &bytes_written));
+ EXPECT_EQ(len1, bytes_written);
+ EXPECT_TRUE(WriteToFile(fd, str2, len2, &bytes_written));
+ EXPECT_EQ(len2, bytes_written);
CloseFile(fd);
+ EXPECT_TRUE(FileExists(tmpfile));
+
fd = OpenFile(tmpfile, RdOnly);
ASSERT_NE(fd, kInvalidFd);
+
+#if SANITIZER_POSIX
+ // The stat wrappers are posix-only.
uptr fsize = internal_filesize(fd);
EXPECT_EQ(len1 + len2, fsize);
-#if SANITIZER_TEST_HAS_STAT_H
struct stat st1, st2, st3;
EXPECT_EQ(0u, internal_stat(tmpfile, &st1));
EXPECT_EQ(0u, internal_lstat(tmpfile, &st2));
@@ -108,16 +128,43 @@ TEST(SanitizerCommon, FileOps) {
#endif
char buf[64] = {};
- EXPECT_EQ(len1, internal_read(fd, buf, len1));
+ uptr bytes_read = 0;
+ EXPECT_TRUE(ReadFromFile(fd, buf, len1, &bytes_read));
+ EXPECT_EQ(len1, bytes_read);
EXPECT_EQ(0, internal_memcmp(buf, str1, len1));
EXPECT_EQ((char)0, buf[len1 + 1]);
internal_memset(buf, 0, len1);
- EXPECT_EQ(len2, internal_read(fd, buf, len2));
+ EXPECT_TRUE(ReadFromFile(fd, buf, len2, &bytes_read));
+ EXPECT_EQ(len2, bytes_read);
EXPECT_EQ(0, internal_memcmp(buf, str2, len2));
CloseFile(fd);
+
+#if SANITIZER_WINDOWS
+ // No sanitizer needs to delete a file on Windows yet. If we ever do, we can
+ // add a portable wrapper and test it from here.
+ ::DeleteFileA(&tmpfile[0]);
+#else
internal_unlink(tmpfile);
-}
#endif
+}
+
+static const size_t kStrlcpyBufSize = 8;
+void test_internal_strlcpy(char *dbuf, const char *sbuf) {
+ uptr retval = 0;
+ retval = internal_strlcpy(dbuf, sbuf, kStrlcpyBufSize);
+ EXPECT_EQ(internal_strncmp(dbuf, sbuf, kStrlcpyBufSize - 1), 0);
+ EXPECT_EQ(internal_strlen(dbuf),
+ std::min(internal_strlen(sbuf), (uptr)(kStrlcpyBufSize - 1)));
+ EXPECT_EQ(retval, internal_strlen(sbuf));
+
+ // Test with shorter maxlen.
+ uptr maxlen = 2;
+ if (internal_strlen(sbuf) > maxlen) {
+ retval = internal_strlcpy(dbuf, sbuf, maxlen);
+ EXPECT_EQ(internal_strncmp(dbuf, sbuf, maxlen - 1), 0);
+ EXPECT_EQ(internal_strlen(dbuf), maxlen - 1);
+ }
+}
TEST(SanitizerCommon, InternalStrFunctions) {
const char *haystack = "haystack";
@@ -125,10 +172,45 @@ TEST(SanitizerCommon, InternalStrFunctions) {
EXPECT_EQ(haystack + 2, internal_strchrnul(haystack, 'y'));
EXPECT_EQ(0, internal_strchr(haystack, 'z'));
EXPECT_EQ(haystack + 8, internal_strchrnul(haystack, 'z'));
+
+ char dbuf[kStrlcpyBufSize] = {};
+ const char *samesizestr = "1234567";
+ const char *shortstr = "123";
+ const char *longerstr = "123456789";
+
+ // Test internal_strlcpy.
+ internal_strlcpy(dbuf, shortstr, 0);
+ EXPECT_EQ(dbuf[0], 0);
+ EXPECT_EQ(dbuf[0], 0);
+ test_internal_strlcpy(dbuf, samesizestr);
+ test_internal_strlcpy(dbuf, shortstr);
+ test_internal_strlcpy(dbuf, longerstr);
+
+ // Test internal_strlcat.
+ char dcatbuf[kStrlcpyBufSize] = {};
+ uptr retval = 0;
+ retval = internal_strlcat(dcatbuf, "aaa", 0);
+ EXPECT_EQ(internal_strlen(dcatbuf), (uptr)0);
+ EXPECT_EQ(retval, (uptr)3);
+
+ retval = internal_strlcat(dcatbuf, "123", kStrlcpyBufSize);
+ EXPECT_EQ(internal_strcmp(dcatbuf, "123"), 0);
+ EXPECT_EQ(internal_strlen(dcatbuf), (uptr)3);
+ EXPECT_EQ(retval, (uptr)3);
+
+ retval = internal_strlcat(dcatbuf, "123", kStrlcpyBufSize);
+ EXPECT_EQ(internal_strcmp(dcatbuf, "123123"), 0);
+ EXPECT_EQ(internal_strlen(dcatbuf), (uptr)6);
+ EXPECT_EQ(retval, (uptr)6);
+
+ retval = internal_strlcat(dcatbuf, "123", kStrlcpyBufSize);
+ EXPECT_EQ(internal_strcmp(dcatbuf, "1231231"), 0);
+ EXPECT_EQ(internal_strlen(dcatbuf), (uptr)7);
+ EXPECT_EQ(retval, (uptr)9);
}
// FIXME: File manipulations are not yet supported on Windows
-#if !defined(_WIN32) && !SANITIZER_MAC
+#if SANITIZER_POSIX && !SANITIZER_MAC
TEST(SanitizerCommon, InternalMmapWithOffset) {
char tmpfile[128];
temp_file_name(tmpfile, sizeof(tmpfile),
diff --git a/lib/sanitizer_common/tests/sanitizer_linux_test.cc b/lib/sanitizer_common/tests/sanitizer_linux_test.cc
index 11342b775cc7..eef71010afe6 100644
--- a/lib/sanitizer_common/tests/sanitizer_linux_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_linux_test.cc
@@ -195,7 +195,7 @@ TEST(SanitizerCommon, SetEnvTest) {
EXPECT_EQ(0, getenv(kEnvName));
}
-#if defined(__x86_64__) || defined(__i386__)
+#if (defined(__x86_64__) || defined(__i386__)) && !SANITIZER_ANDROID
void *thread_self_offset_test_func(void *arg) {
bool result =
*(uptr *)((char *)ThreadSelf() + ThreadSelfOffset()) == ThreadSelf();
diff --git a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
index 654ea1db82fb..3d57eded948f 100644
--- a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
@@ -82,7 +82,7 @@ TEST_F(FastUnwindTest, Basic) {
}
}
-// From: http://code.google.com/p/address-sanitizer/issues/detail?id=162
+// From: https://github.com/google/sanitizers/issues/162
TEST_F(FastUnwindTest, FramePointerLoop) {
// Make one fp point to itself.
fake_stack[4] = (uhwptr)&fake_stack[4];
diff --git a/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc b/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
index e8c30d07e78c..224ab0538377 100644
--- a/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
@@ -105,9 +105,10 @@ TEST_F(SuppressionContextTest, Parse3) {
ctx_.Parse(
"# last suppression w/o line-feed\n"
"race:foo\n"
- "race:bar"
+ "race:bar\r\n"
+ "race:baz"
); // NOLINT
- CheckSuppressions(2, {"race", "race"}, {"foo", "bar"});
+ CheckSuppressions(3, {"race", "race", "race"}, {"foo", "bar", "baz"});
}
TEST_F(SuppressionContextTest, ParseType) {
diff --git a/lib/sanitizer_common/tests/sanitizer_test_main.cc b/lib/sanitizer_common/tests/sanitizer_test_main.cc
index b7fd3dafab26..20f8f53975d0 100644
--- a/lib/sanitizer_common/tests/sanitizer_test_main.cc
+++ b/lib/sanitizer_common/tests/sanitizer_test_main.cc
@@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
+#include "sanitizer_common/sanitizer_flags.h"
const char *argv0;
@@ -18,5 +19,6 @@ int main(int argc, char **argv) {
argv0 = argv[0];
testing::GTEST_FLAG(death_test_style) = "threadsafe";
testing::InitGoogleTest(&argc, argv);
+ SetCommonFlagsDefaults();
return RUN_ALL_TESTS();
}