Changeset View
Changeset View
Standalone View
Standalone View
compiler-rt/trunk/lib/asan/tests/asan_interface_test.cc
Show First 20 Lines • Show All 147 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
TEST(AddressSanitizerInterface, DeathCallbackTest) { | TEST(AddressSanitizerInterface, DeathCallbackTest) { | ||||
__asan_set_death_callback(MyDeathCallback); | __asan_set_death_callback(MyDeathCallback); | ||||
EXPECT_DEATH(DoDoubleFree(), "MyDeathCallback"); | EXPECT_DEATH(DoDoubleFree(), "MyDeathCallback"); | ||||
__asan_set_death_callback(NULL); | __asan_set_death_callback(NULL); | ||||
} | } | ||||
static const char* kUseAfterPoisonErrorMessage = "use-after-poison"; | |||||
#define GOOD_ACCESS(ptr, offset) \ | #define GOOD_ACCESS(ptr, offset) \ | ||||
EXPECT_FALSE(__asan_address_is_poisoned(ptr + offset)) | EXPECT_FALSE(__asan_address_is_poisoned(ptr + offset)) | ||||
#define BAD_ACCESS(ptr, offset) \ | #define BAD_ACCESS(ptr, offset) \ | ||||
EXPECT_TRUE(__asan_address_is_poisoned(ptr + offset)) | EXPECT_TRUE(__asan_address_is_poisoned(ptr + offset)) | ||||
#if !defined(ASAN_SHADOW_SCALE) || ASAN_SHADOW_SCALE == 3 | |||||
static const char* kUseAfterPoisonErrorMessage = "use-after-poison"; | |||||
TEST(AddressSanitizerInterface, SimplePoisonMemoryRegionTest) { | TEST(AddressSanitizerInterface, SimplePoisonMemoryRegionTest) { | ||||
char *array = Ident((char*)malloc(120)); | char *array = Ident((char*)malloc(120)); | ||||
// poison array[40..80) | // poison array[40..80) | ||||
__asan_poison_memory_region(array + 40, 40); | __asan_poison_memory_region(array + 40, 40); | ||||
GOOD_ACCESS(array, 39); | GOOD_ACCESS(array, 39); | ||||
GOOD_ACCESS(array, 80); | GOOD_ACCESS(array, 80); | ||||
BAD_ACCESS(array, 40); | BAD_ACCESS(array, 40); | ||||
BAD_ACCESS(array, 60); | BAD_ACCESS(array, 60); | ||||
Show All 22 Lines | TEST(AddressSanitizerInterface, OverlappingPoisonMemoryRegionTest) { | ||||
__asan_unpoison_memory_region(array + 24, 72); | __asan_unpoison_memory_region(array + 24, 72); | ||||
BAD_ACCESS(array, 23); | BAD_ACCESS(array, 23); | ||||
GOOD_ACCESS(array, 24); | GOOD_ACCESS(array, 24); | ||||
GOOD_ACCESS(array, 60); | GOOD_ACCESS(array, 60); | ||||
GOOD_ACCESS(array, 95); | GOOD_ACCESS(array, 95); | ||||
BAD_ACCESS(array, 96); | BAD_ACCESS(array, 96); | ||||
free(array); | free(array); | ||||
} | } | ||||
#endif // !defined(ASAN_SHADOW_SCALE) || ASAN_SHADOW_SCALE == 3 | |||||
TEST(AddressSanitizerInterface, PushAndPopWithPoisoningTest) { | TEST(AddressSanitizerInterface, PushAndPopWithPoisoningTest) { | ||||
// Vector of capacity 20 | // Vector of capacity 20 | ||||
char *vec = Ident((char*)malloc(20)); | char *vec = Ident((char*)malloc(20)); | ||||
__asan_poison_memory_region(vec, 20); | __asan_poison_memory_region(vec, 20); | ||||
for (size_t i = 0; i < 7; i++) { | for (size_t i = 0; i < 7; i++) { | ||||
// Simulate push_back. | // Simulate push_back. | ||||
__asan_unpoison_memory_region(vec + i, 1); | __asan_unpoison_memory_region(vec + i, 1); | ||||
GOOD_ACCESS(vec, i); | GOOD_ACCESS(vec, i); | ||||
BAD_ACCESS(vec, i + 1); | BAD_ACCESS(vec, i + 1); | ||||
} | } | ||||
for (size_t i = 7; i > 0; i--) { | for (size_t i = 7; i > 0; i--) { | ||||
// Simulate pop_back. | // Simulate pop_back. | ||||
__asan_poison_memory_region(vec + i - 1, 1); | __asan_poison_memory_region(vec + i - 1, 1); | ||||
BAD_ACCESS(vec, i - 1); | BAD_ACCESS(vec, i - 1); | ||||
if (i > 1) GOOD_ACCESS(vec, i - 2); | if (i > 1) GOOD_ACCESS(vec, i - 2); | ||||
} | } | ||||
free(vec); | free(vec); | ||||
} | } | ||||
#if !defined(ASAN_SHADOW_SCALE) || ASAN_SHADOW_SCALE == 3 | |||||
// Make sure that each aligned block of size "2^granularity" doesn't have | // Make sure that each aligned block of size "2^granularity" doesn't have | ||||
// "true" value before "false" value. | // "true" value before "false" value. | ||||
static void MakeShadowValid(bool *shadow, int length, int granularity) { | static void MakeShadowValid(bool *shadow, int length, int granularity) { | ||||
bool can_be_poisoned = true; | bool can_be_poisoned = true; | ||||
for (int i = length - 1; i >= 0; i--) { | for (int i = length - 1; i >= 0; i--) { | ||||
if (!shadow[i]) | if (!shadow[i]) | ||||
can_be_poisoned = false; | can_be_poisoned = false; | ||||
if (!can_be_poisoned) | if (!can_be_poisoned) | ||||
Show All 37 Lines | for (size_t s1 = 1; l1 + s1 <= kSize; s1++) { | ||||
ASSERT_EQ(expected[i], __asan_address_is_poisoned(arr + i)); | ASSERT_EQ(expected[i], __asan_address_is_poisoned(arr + i)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
free(arr); | free(arr); | ||||
} | } | ||||
#endif // !defined(ASAN_SHADOW_SCALE) || ASAN_SHADOW_SCALE == 3 | |||||
TEST(AddressSanitizerInterface, GlobalRedzones) { | TEST(AddressSanitizerInterface, GlobalRedzones) { | ||||
GOOD_ACCESS(glob1, 1 - 1); | GOOD_ACCESS(glob1, 1 - 1); | ||||
GOOD_ACCESS(glob2, 2 - 1); | GOOD_ACCESS(glob2, 2 - 1); | ||||
GOOD_ACCESS(glob3, 3 - 1); | GOOD_ACCESS(glob3, 3 - 1); | ||||
GOOD_ACCESS(glob4, 4 - 1); | GOOD_ACCESS(glob4, 4 - 1); | ||||
GOOD_ACCESS(glob5, 5 - 1); | GOOD_ACCESS(glob5, 5 - 1); | ||||
GOOD_ACCESS(glob6, 6 - 1); | GOOD_ACCESS(glob6, 6 - 1); | ||||
▲ Show 20 Lines • Show All 134 Lines • Show Last 20 Lines |