Commit 5dd2059a authored by Vlad Tsyrklevich's avatar Vlad Tsyrklevich Committed by Commit Bot

GWP-ASan: Fix broken unit test

After a refactor I noticed that this unit test was broken.
_aligned_malloc() does not go through the allocator shims on windows,
but because we used EXPECT_TRUE() we didn't catch this failure. Delete
the _aligned_malloc() test and refactor allocationCheck() to return
failure count so that we can track failures across calls and return the
proper exit code.

Bug: 896019
Change-Id: Ibef719eb14980575cc52796167735f5a0ba177fc
Reviewed-on: https://chromium-review.googlesource.com/c/1339319
Commit-Queue: Vlad Tsyrklevich <vtsyrklevich@chromium.org>
Reviewed-by: default avatarVitaly Buka <vitalybuka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608908}
parent ee29a5b8
...@@ -54,16 +54,20 @@ class SamplingAllocatorShimsTest : public base::MultiProcessTest { ...@@ -54,16 +54,20 @@ class SamplingAllocatorShimsTest : public base::MultiProcessTest {
}; };
// Return whether some of the allocations returned by the calling the allocate // Return whether some of the allocations returned by the calling the allocate
// parameter are sampled to the guarded allocator. // parameter are sampled to the guarded allocator. Keep count of failures
// encountered.
bool allocationCheck(std::function<void*(void)> allocate, bool allocationCheck(std::function<void*(void)> allocate,
std::function<void(void*)> free) { std::function<void(void*)> free,
int* failures) {
size_t guarded = 0; size_t guarded = 0;
size_t unguarded = 0; size_t unguarded = 0;
for (size_t i = 0; i < kLoopIterations; i++) { for (size_t i = 0; i < kLoopIterations; i++) {
std::unique_ptr<void, decltype(free)> alloc(allocate(), free); std::unique_ptr<void, decltype(free)> alloc(allocate(), free);
EXPECT_NE(alloc.get(), nullptr); EXPECT_NE(alloc.get(), nullptr);
if (!alloc.get()) if (!alloc.get()) {
*failures += 1;
return false; return false;
}
if (GetGpaForTesting().PointerIsMine(alloc.get())) if (GetGpaForTesting().PointerIsMine(alloc.get()))
guarded++; guarded++;
...@@ -71,46 +75,57 @@ bool allocationCheck(std::function<void*(void)> allocate, ...@@ -71,46 +75,57 @@ bool allocationCheck(std::function<void*(void)> allocate,
unguarded++; unguarded++;
} }
return guarded > 0 && unguarded > 0; if (guarded > 0 && unguarded > 0)
return true;
*failures += 1;
return false;
} }
MULTIPROCESS_TEST_MAIN(BasicFunctionality) { MULTIPROCESS_TEST_MAIN(BasicFunctionality) {
InstallAllocatorHooks(GuardedPageAllocator::kGpaMaxPages, kSamplingFrequency); InstallAllocatorHooks(GuardedPageAllocator::kGpaMaxPages, kSamplingFrequency);
const size_t page_size = base::GetPageSize(); const size_t page_size = base::GetPageSize();
int failures = 0;
EXPECT_TRUE(allocationCheck([&] { return malloc(page_size); }, &free));
EXPECT_TRUE(allocationCheck([&] { return calloc(1, page_size); }, &free));
EXPECT_TRUE( EXPECT_TRUE(
allocationCheck([&] { return realloc(nullptr, page_size); }, &free)); allocationCheck([&] { return malloc(page_size); }, &free, &failures));
EXPECT_TRUE(
allocationCheck([&] { return calloc(1, page_size); }, &free, &failures));
EXPECT_TRUE(allocationCheck([&] { return realloc(nullptr, page_size); },
&free, &failures));
#if defined(OS_WIN) #if !defined(OS_WIN)
EXPECT_TRUE(allocationCheck(
[&] { return _aligned_malloc(page_size, page_size); }, &_aligned_free));
EXPECT_TRUE(allocationCheck([&] { return _aligned_malloc(page_size, 1); },
&_aligned_free));
#else
EXPECT_TRUE(allocationCheck( EXPECT_TRUE(allocationCheck(
[&] { return aligned_alloc(page_size, page_size); }, &free)); [&] { return aligned_alloc(page_size, page_size); }, &free, &failures));
EXPECT_TRUE( EXPECT_TRUE(allocationCheck([&] { return aligned_alloc(1, page_size); },
allocationCheck([&] { return aligned_alloc(1, page_size); }, &free)); &free, &failures));
#endif #endif
EXPECT_TRUE( EXPECT_TRUE(allocationCheck([&] { return std::malloc(page_size); },
allocationCheck([&] { return std::malloc(page_size); }, &std::free)); &std::free, &failures));
EXPECT_TRUE( EXPECT_TRUE(allocationCheck([&] { return std::calloc(1, page_size); },
allocationCheck([&] { return std::calloc(1, page_size); }, &std::free)); &std::free, &failures));
EXPECT_TRUE(allocationCheck([&] { return std::realloc(nullptr, page_size); }, EXPECT_TRUE(allocationCheck([&] { return std::realloc(nullptr, page_size); },
&std::free)); &std::free, &failures));
EXPECT_TRUE(allocationCheck([] { return new int; }, EXPECT_TRUE(allocationCheck([] { return new int; },
[](void* ptr) { delete (int*)ptr; })); [](void* ptr) { delete (int*)ptr; }, &failures));
EXPECT_TRUE(allocationCheck([] { return new int[4]; }, EXPECT_TRUE(allocationCheck([] { return new int[4]; },
[](void* ptr) { delete[](int*) ptr; })); [](void* ptr) { delete[](int*) ptr; },
&failures));
if (failures)
return kFailure;
EXPECT_FALSE(allocationCheck([&] { return malloc(page_size + 1); }, &free)); EXPECT_FALSE(
allocationCheck([&] { return malloc(page_size + 1); }, &free, &failures));
// Make sure exactly 1 negative test case was hit.
if (failures == 1)
return kSuccess; return kSuccess;
return kFailure;
} }
TEST_F(SamplingAllocatorShimsTest, BasicFunctionality) { TEST_F(SamplingAllocatorShimsTest, BasicFunctionality) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment