forked from OSchip/llvm-project
[tests] Resolve old TODOs in ASan unit tests.
llvm-svn: 261713
This commit is contained in:
parent
5ac589171d
commit
0dc3d24d5d
|
@ -20,10 +20,31 @@
|
||||||
static char global_string[] = "global";
|
static char global_string[] = "global";
|
||||||
static size_t global_string_length = 6;
|
static size_t global_string_length = 6;
|
||||||
|
|
||||||
|
const char kStackReadUnderflow[] = "READ.*underflows this variable";
|
||||||
|
const char kStackReadOverflow[] = "READ.*overflows this variable";
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
enum class OOBKind {
|
||||||
|
Heap,
|
||||||
|
Stack,
|
||||||
|
Global,
|
||||||
|
};
|
||||||
|
|
||||||
|
string LeftOOBReadMessage(OOBKind oob_kind, int oob_distance) {
|
||||||
|
return oob_kind == OOBKind::Stack ? kStackReadUnderflow
|
||||||
|
: ::LeftOOBReadMessage(oob_distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
string RightOOBReadMessage(OOBKind oob_kind, int oob_distance) {
|
||||||
|
return oob_kind == OOBKind::Stack ? kStackReadOverflow
|
||||||
|
: ::RightOOBReadMessage(oob_distance);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
// Input to a test is a zero-terminated string str with given length
|
// Input to a test is a zero-terminated string str with given length
|
||||||
// Accesses to the bytes to the left and to the right of str
|
// Accesses to the bytes to the left and to the right of str
|
||||||
// are presumed to produce OOB errors
|
// are presumed to produce OOB errors
|
||||||
void StrLenOOBTestTemplate(char *str, size_t length, bool is_global) {
|
void StrLenOOBTestTemplate(char *str, size_t length, OOBKind oob_kind) {
|
||||||
// Normal strlen calls
|
// Normal strlen calls
|
||||||
EXPECT_EQ(strlen(str), length);
|
EXPECT_EQ(strlen(str), length);
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
|
@ -31,17 +52,18 @@ void StrLenOOBTestTemplate(char *str, size_t length, bool is_global) {
|
||||||
EXPECT_EQ(0U, strlen(str + length));
|
EXPECT_EQ(0U, strlen(str + length));
|
||||||
}
|
}
|
||||||
// Arg of strlen is not malloced, OOB access
|
// Arg of strlen is not malloced, OOB access
|
||||||
if (!is_global) {
|
if (oob_kind != OOBKind::Global) {
|
||||||
// We don't insert RedZones to the left of global variables
|
// We don't insert RedZones to the left of global variables
|
||||||
EXPECT_DEATH(Ident(strlen(str - 1)), LeftOOBReadMessage(1));
|
EXPECT_DEATH(Ident(strlen(str - 1)), LeftOOBReadMessage(oob_kind, 1));
|
||||||
EXPECT_DEATH(Ident(strlen(str - 5)), LeftOOBReadMessage(5));
|
EXPECT_DEATH(Ident(strlen(str - 5)), LeftOOBReadMessage(oob_kind, 5));
|
||||||
}
|
}
|
||||||
EXPECT_DEATH(Ident(strlen(str + length + 1)), RightOOBReadMessage(0));
|
EXPECT_DEATH(Ident(strlen(str + length + 1)),
|
||||||
|
RightOOBReadMessage(oob_kind, 0));
|
||||||
// Overwrite terminator
|
// Overwrite terminator
|
||||||
str[length] = 'a';
|
str[length] = 'a';
|
||||||
// String is not zero-terminated, strlen will lead to OOB access
|
// String is not zero-terminated, strlen will lead to OOB access
|
||||||
EXPECT_DEATH(Ident(strlen(str)), RightOOBReadMessage(0));
|
EXPECT_DEATH(Ident(strlen(str)), RightOOBReadMessage(oob_kind, 0));
|
||||||
EXPECT_DEATH(Ident(strlen(str + length)), RightOOBReadMessage(0));
|
EXPECT_DEATH(Ident(strlen(str + length)), RightOOBReadMessage(oob_kind, 0));
|
||||||
// Restore terminator
|
// Restore terminator
|
||||||
str[length] = 0;
|
str[length] = 0;
|
||||||
}
|
}
|
||||||
|
@ -57,11 +79,9 @@ TEST(AddressSanitizer, StrLenOOBTest) {
|
||||||
}
|
}
|
||||||
heap_string[length] = 0;
|
heap_string[length] = 0;
|
||||||
stack_string[length] = 0;
|
stack_string[length] = 0;
|
||||||
StrLenOOBTestTemplate(heap_string, length, false);
|
StrLenOOBTestTemplate(heap_string, length, OOBKind::Heap);
|
||||||
// TODO(samsonov): Fix expected messages in StrLenOOBTestTemplate to
|
StrLenOOBTestTemplate(stack_string, length, OOBKind::Stack);
|
||||||
// make test for stack_string work. Or move it to output tests.
|
StrLenOOBTestTemplate(global_string, global_string_length, OOBKind::Global);
|
||||||
// StrLenOOBTestTemplate(stack_string, length, false);
|
|
||||||
StrLenOOBTestTemplate(global_string, global_string_length, true);
|
|
||||||
free(heap_string);
|
free(heap_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -809,9 +809,6 @@ TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) {
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(samsonov): Add a test with malloc(0)
|
|
||||||
// TODO(samsonov): Add tests for str* and mem* functions.
|
|
||||||
|
|
||||||
NOINLINE static int LargeFunction(bool do_bad_access) {
|
NOINLINE static int LargeFunction(bool do_bad_access) {
|
||||||
int *x = new int[100];
|
int *x = new int[100];
|
||||||
x[0]++;
|
x[0]++;
|
||||||
|
|
Loading…
Reference in New Issue