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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "sanitizer_common/sanitizer_common.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"
|
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-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;
|
|
|
|
pthread_create(&t, 0, WorkerThread, 0);
|
|
|
|
pthread_join(t, 0);
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:24:43 +08:00
|
|
|
bool UptrLess(uptr a, uptr b) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SanitizerCommon, InternalBinarySearch) {
|
|
|
|
const uptr sz = 5;
|
|
|
|
uptr arr[sz];
|
|
|
|
for (uptr i = 0; i < sz; i++) arr[i] = i * i;
|
|
|
|
|
|
|
|
for (uptr i = 0; i < sz; i++)
|
|
|
|
ASSERT_EQ(InternalBinarySearch(arr, 0, sz, i * i, UptrLess), i);
|
|
|
|
|
|
|
|
ASSERT_EQ(InternalBinarySearch(arr, 0, sz, 7, UptrLess), sz + 1);
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:45:58 +08:00
|
|
|
} // namespace __sanitizer
|