summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/asan/tests
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2012-10-17 13:39:09 +0000
committerAlexey Samsonov <samsonov@google.com>2012-10-17 13:39:09 +0000
commit7b52cb9e6dd9e4c56c2ea160c3ea1f0bd1b53ed9 (patch)
treed743209f63a0c3044f50bf7985c68336994bc0a0 /compiler-rt/lib/asan/tests
parent7943eb5cb51d26c0c9ab84f85d94fd08a9b44bdc (diff)
downloadbcm5719-llvm-7b52cb9e6dd9e4c56c2ea160c3ea1f0bd1b53ed9.tar.gz
bcm5719-llvm-7b52cb9e6dd9e4c56c2ea160c3ea1f0bd1b53ed9.zip
[ASan] Fix strchr/index tests for users who have 'char* strchr(char*,int)' instead of 'char* strchr(const char*, int)'
llvm-svn: 166101
Diffstat (limited to 'compiler-rt/lib/asan/tests')
-rw-r--r--compiler-rt/lib/asan/tests/asan_test.cc27
-rw-r--r--compiler-rt/lib/asan/tests/asan_test_utils.h2
2 files changed, 27 insertions, 2 deletions
diff --git a/compiler-rt/lib/asan/tests/asan_test.cc b/compiler-rt/lib/asan/tests/asan_test.cc
index 569564db2ca..7f0c04ca50b 100644
--- a/compiler-rt/lib/asan/tests/asan_test.cc
+++ b/compiler-rt/lib/asan/tests/asan_test.cc
@@ -1078,8 +1078,14 @@ TEST(AddressSanitizer, StrNCpyOOBTest) {
free(from);
}
-typedef char*(*PointerToStrChr)(const char*, int);
-void RunStrChrTest(PointerToStrChr StrChr) {
+// Users may have different definitions of "strchr" and "index", so provide
+// function pointer typedefs and overload RunStrChrTest implementation.
+// We can't use macro for RunStrChrTest body here, as this macro would
+// confuse EXPECT_DEATH gtest macro.
+typedef char*(*PointerToStrChr1)(const char*, int);
+typedef char*(*PointerToStrChr2)(char*, int);
+
+USED static void RunStrChrTest(PointerToStrChr1 StrChr) {
size_t size = Ident(100);
char *str = MallocAndMemsetString(size);
str[10] = 'q';
@@ -1095,6 +1101,23 @@ void RunStrChrTest(PointerToStrChr StrChr) {
EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBErrorMessage(0));
free(str);
}
+USED static void RunStrChrTest(PointerToStrChr2 StrChr) {
+ size_t size = Ident(100);
+ char *str = MallocAndMemsetString(size);
+ str[10] = 'q';
+ str[11] = '\0';
+ EXPECT_EQ(str, StrChr(str, 'z'));
+ EXPECT_EQ(str + 10, StrChr(str, 'q'));
+ EXPECT_EQ(NULL, StrChr(str, 'a'));
+ // StrChr argument points to not allocated memory.
+ EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBErrorMessage(1));
+ EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBErrorMessage(0));
+ // Overwrite the terminator and hit not allocated memory.
+ str[11] = 'z';
+ EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBErrorMessage(0));
+ free(str);
+}
+
TEST(AddressSanitizer, StrChrAndIndexOOBTest) {
RunStrChrTest(&strchr);
RunStrChrTest(&index);
diff --git a/compiler-rt/lib/asan/tests/asan_test_utils.h b/compiler-rt/lib/asan/tests/asan_test_utils.h
index 577a7ebbd3f..ffcaaec4c42 100644
--- a/compiler-rt/lib/asan/tests/asan_test_utils.h
+++ b/compiler-rt/lib/asan/tests/asan_test_utils.h
@@ -30,8 +30,10 @@ typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
# define NOINLINE __declspec(noinline)
+# define USED
#else // defined(_WIN32)
# define NOINLINE __attribute__((noinline))
+# define USED __attribute__((used))
#endif // defined(_WIN32)
#if !defined(__has_feature)
OpenPOWER on IntegriCloud