2012-07-16 19:27:17 +08:00
|
|
|
//===-- sanitizer_common_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 ThreadSanitizer/AddressSanitizer runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-09-03 21:20:48 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_internal.h"
|
2012-07-16 19:27:17 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2013-11-14 17:41:24 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2012-12-07 19:27:24 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2013-05-14 21:19:33 +08:00
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2014-05-13 20:02:53 +08:00
|
|
|
|
|
|
|
#include "sanitizer_pthread_wrappers.h"
|
|
|
|
|
2012-07-16 19:27:17 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
static bool IsSorted(const uptr *array, uptr n) {
|
|
|
|
for (uptr i = 1; i < n; i++) {
|
|
|
|
if (array[i] < array[i - 1]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SanitizerCommon, SortTest) {
|
|
|
|
uptr array[100];
|
|
|
|
uptr n = 100;
|
|
|
|
// Already sorted.
|
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
array[i] = i;
|
|
|
|
}
|
|
|
|
SortArray(array, n);
|
|
|
|
EXPECT_TRUE(IsSorted(array, n));
|
|
|
|
// Reverse order.
|
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
array[i] = n - 1 - i;
|
|
|
|
}
|
|
|
|
SortArray(array, n);
|
|
|
|
EXPECT_TRUE(IsSorted(array, n));
|
|
|
|
// Mixed order.
|
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
array[i] = (i % 2 == 0) ? i : n - 1 - i;
|
|
|
|
}
|
|
|
|
SortArray(array, n);
|
|
|
|
EXPECT_TRUE(IsSorted(array, n));
|
|
|
|
// All equal.
|
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
array[i] = 42;
|
|
|
|
}
|
|
|
|
SortArray(array, n);
|
|
|
|
EXPECT_TRUE(IsSorted(array, n));
|
|
|
|
// All but one sorted.
|
|
|
|
for (uptr i = 0; i < n - 1; i++) {
|
|
|
|
array[i] = i;
|
|
|
|
}
|
|
|
|
array[n - 1] = 42;
|
|
|
|
SortArray(array, n);
|
|
|
|
EXPECT_TRUE(IsSorted(array, n));
|
|
|
|
// Minimal case - sort three elements.
|
|
|
|
array[0] = 1;
|
|
|
|
array[1] = 0;
|
|
|
|
SortArray(array, 2);
|
|
|
|
EXPECT_TRUE(IsSorted(array, 2));
|
|
|
|
}
|
|
|
|
|
2012-12-06 14:10:31 +08:00
|
|
|
TEST(SanitizerCommon, MmapAlignedOrDie) {
|
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
for (uptr size = 1; size <= 32; size *= 2) {
|
|
|
|
for (uptr alignment = 1; alignment <= 32; alignment *= 2) {
|
|
|
|
for (int iter = 0; iter < 100; iter++) {
|
|
|
|
uptr res = (uptr)MmapAlignedOrDie(
|
|
|
|
size * PageSize, alignment * PageSize, "MmapAlignedOrDieTest");
|
|
|
|
EXPECT_EQ(0U, res % (alignment * PageSize));
|
2012-12-25 17:20:58 +08:00
|
|
|
internal_memset((void*)res, 1, size * PageSize);
|
2012-12-06 14:10:31 +08:00
|
|
|
UnmapOrDie((void*)res, size * PageSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:19:33 +08:00
|
|
|
#if SANITIZER_LINUX
|
2012-12-07 19:27:24 +08:00
|
|
|
TEST(SanitizerCommon, SanitizerSetThreadName) {
|
|
|
|
const char *names[] = {
|
|
|
|
"0123456789012",
|
|
|
|
"01234567890123",
|
|
|
|
"012345678901234", // Larger names will be truncated on linux.
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
|
|
|
|
EXPECT_TRUE(SanitizerSetThreadName(names[i]));
|
|
|
|
char buff[100];
|
|
|
|
EXPECT_TRUE(SanitizerGetThreadName(buff, sizeof(buff) - 1));
|
|
|
|
EXPECT_EQ(0, internal_strcmp(buff, names[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-14 17:59:40 +08:00
|
|
|
TEST(SanitizerCommon, InternalMmapVector) {
|
|
|
|
InternalMmapVector<uptr> vector(1);
|
2013-02-26 21:30:27 +08:00
|
|
|
for (uptr i = 0; i < 100; i++) {
|
2013-03-05 20:23:07 +08:00
|
|
|
EXPECT_EQ(i, vector.size());
|
2013-02-26 21:30:27 +08:00
|
|
|
vector.push_back(i);
|
|
|
|
}
|
|
|
|
for (uptr i = 0; i < 100; i++) {
|
2013-03-05 20:23:07 +08:00
|
|
|
EXPECT_EQ(i, vector[i]);
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2013-03-05 19:58:25 +08:00
|
|
|
for (int i = 99; i >= 0; i--) {
|
2013-03-05 20:23:07 +08:00
|
|
|
EXPECT_EQ((uptr)i, vector.back());
|
2013-03-05 19:58:25 +08:00
|
|
|
vector.pop_back();
|
2013-03-05 20:23:07 +08:00
|
|
|
EXPECT_EQ((uptr)i, vector.size());
|
2013-03-05 19:58:25 +08:00
|
|
|
}
|
2013-12-03 17:21:08 +08:00
|
|
|
InternalMmapVector<uptr> empty_vector(0);
|
|
|
|
CHECK_GT(empty_vector.capacity(), 0U);
|
|
|
|
CHECK_EQ(0U, empty_vector.size());
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 22:41:43 +08:00
|
|
|
void TestThreadInfo(bool main) {
|
|
|
|
uptr stk_addr = 0;
|
|
|
|
uptr stk_size = 0;
|
|
|
|
uptr tls_addr = 0;
|
|
|
|
uptr tls_size = 0;
|
|
|
|
GetThreadStackAndTls(main, &stk_addr, &stk_size, &tls_addr, &tls_size);
|
|
|
|
|
|
|
|
int stack_var;
|
|
|
|
EXPECT_NE(stk_addr, (uptr)0);
|
|
|
|
EXPECT_NE(stk_size, (uptr)0);
|
|
|
|
EXPECT_GT((uptr)&stack_var, stk_addr);
|
|
|
|
EXPECT_LT((uptr)&stack_var, stk_addr + stk_size);
|
|
|
|
|
2013-05-14 21:19:33 +08:00
|
|
|
#if SANITIZER_LINUX && defined(__x86_64__)
|
2013-05-07 22:41:43 +08:00
|
|
|
static __thread int thread_var;
|
|
|
|
EXPECT_NE(tls_addr, (uptr)0);
|
|
|
|
EXPECT_NE(tls_size, (uptr)0);
|
|
|
|
EXPECT_GT((uptr)&thread_var, tls_addr);
|
|
|
|
EXPECT_LT((uptr)&thread_var, tls_addr + tls_size);
|
|
|
|
|
|
|
|
// Ensure that tls and stack do not intersect.
|
|
|
|
uptr tls_end = tls_addr + tls_size;
|
|
|
|
EXPECT_TRUE(tls_addr < stk_addr || tls_addr >= stk_addr + stk_size);
|
|
|
|
EXPECT_TRUE(tls_end < stk_addr || tls_end >= stk_addr + stk_size);
|
|
|
|
EXPECT_TRUE((tls_addr < stk_addr) == (tls_end < stk_addr));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *WorkerThread(void *arg) {
|
|
|
|
TestThreadInfo(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SanitizerCommon, ThreadStackTlsMain) {
|
|
|
|
InitTlsSize();
|
|
|
|
TestThreadInfo(true);
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:19:33 +08:00
|
|
|
TEST(SanitizerCommon, ThreadStackTlsWorker) {
|
2013-05-07 22:41:43 +08:00
|
|
|
InitTlsSize();
|
|
|
|
pthread_t t;
|
2014-05-13 20:02:53 +08:00
|
|
|
PTHREAD_CREATE(&t, 0, WorkerThread, 0);
|
|
|
|
PTHREAD_JOIN(t, 0);
|
2013-05-07 22:41:43 +08:00
|
|
|
}
|
|
|
|
|
2013-08-26 21:24:43 +08:00
|
|
|
bool UptrLess(uptr a, uptr b) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SanitizerCommon, InternalBinarySearch) {
|
2013-08-28 19:26:09 +08:00
|
|
|
static const uptr kSize = 5;
|
|
|
|
uptr arr[kSize];
|
|
|
|
for (uptr i = 0; i < kSize; i++) arr[i] = i * i;
|
2013-08-26 21:24:43 +08:00
|
|
|
|
2013-08-28 19:26:09 +08:00
|
|
|
for (uptr i = 0; i < kSize; i++)
|
|
|
|
ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, i * i, UptrLess), i);
|
2013-08-26 21:24:43 +08:00
|
|
|
|
2013-08-28 19:26:09 +08:00
|
|
|
ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, 7, UptrLess), kSize + 1);
|
2013-08-26 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-04 14:40:48 +08:00
|
|
|
#if SANITIZER_LINUX && !SANITIZER_ANDROID
|
2013-09-03 21:20:48 +08:00
|
|
|
TEST(SanitizerCommon, FindPathToBinary) {
|
|
|
|
char *true_path = FindPathToBinary("true");
|
|
|
|
EXPECT_NE((char*)0, internal_strstr(true_path, "/bin/true"));
|
|
|
|
InternalFree(true_path);
|
|
|
|
EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj"));
|
|
|
|
}
|
2015-08-11 07:40:27 +08:00
|
|
|
#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"));
|
|
|
|
}
|
2013-09-03 21:20:48 +08:00
|
|
|
#endif
|
|
|
|
|
2013-10-04 16:55:03 +08:00
|
|
|
TEST(SanitizerCommon, StripPathPrefix) {
|
|
|
|
EXPECT_EQ(0, StripPathPrefix(0, "prefix"));
|
|
|
|
EXPECT_STREQ("foo", StripPathPrefix("foo", 0));
|
|
|
|
EXPECT_STREQ("dir/file.cc",
|
|
|
|
StripPathPrefix("/usr/lib/dir/file.cc", "/usr/lib/"));
|
|
|
|
EXPECT_STREQ("/file.cc", StripPathPrefix("/usr/myroot/file.cc", "/myroot"));
|
|
|
|
EXPECT_STREQ("file.h", StripPathPrefix("/usr/lib/./file.h", "/usr/lib/"));
|
|
|
|
}
|
|
|
|
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 17:41:24 +08:00
|
|
|
TEST(SanitizerCommon, InternalScopedString) {
|
|
|
|
InternalScopedString str(10);
|
|
|
|
EXPECT_EQ(0U, str.length());
|
|
|
|
EXPECT_STREQ("", str.data());
|
|
|
|
|
|
|
|
str.append("foo");
|
|
|
|
EXPECT_EQ(3U, str.length());
|
|
|
|
EXPECT_STREQ("foo", str.data());
|
|
|
|
|
|
|
|
int x = 1234;
|
|
|
|
str.append("%d", x);
|
|
|
|
EXPECT_EQ(7U, str.length());
|
|
|
|
EXPECT_STREQ("foo1234", str.data());
|
|
|
|
|
|
|
|
str.append("%d", x);
|
|
|
|
EXPECT_EQ(9U, str.length());
|
|
|
|
EXPECT_STREQ("foo123412", str.data());
|
|
|
|
|
|
|
|
str.clear();
|
|
|
|
EXPECT_EQ(0U, str.length());
|
|
|
|
EXPECT_STREQ("", str.data());
|
|
|
|
|
|
|
|
str.append("0123456789");
|
|
|
|
EXPECT_EQ(9U, str.length());
|
|
|
|
EXPECT_STREQ("012345678", str.data());
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:45:58 +08:00
|
|
|
} // namespace __sanitizer
|