|
| 1 | +/*++ |
| 2 | +
|
| 3 | + Copyright (c) Microsoft Corporation. |
| 4 | + Licensed under the MIT License. |
| 5 | +
|
| 6 | +Abstract: |
| 7 | +
|
| 8 | + Tests to verify that memory allocations are zero-initialized by default. |
| 9 | +
|
| 10 | +--*/ |
| 11 | + |
| 12 | +#include "main.h" |
| 13 | +#include <algorithm> |
| 14 | + |
| 15 | +// |
| 16 | +// Size large enough to avoid small-allocation optimizations that might |
| 17 | +// coincidentally return zeroed memory. |
| 18 | +// |
| 19 | +#define TEST_ALLOC_SIZE 256 |
| 20 | + |
| 21 | +static bool |
| 22 | +IsZeroMemory( |
| 23 | + _In_ const uint8_t* Buffer, |
| 24 | + _In_ size_t Size |
| 25 | + ) |
| 26 | +{ |
| 27 | + return std::all_of(Buffer, Buffer + Size, [](uint8_t b) { return b == 0; }); |
| 28 | +} |
| 29 | + |
| 30 | +// |
| 31 | +// Allocates memory, fills it with non-zero data, frees it, then reallocates |
| 32 | +// the same size and checks whether the new allocation is zero-initialized. |
| 33 | +// |
| 34 | +// If the allocator does NOT zero-initialize, the recycled block will likely |
| 35 | +// still contain the 0xDE pattern, causing the test to fail. |
| 36 | +// |
| 37 | +TEST(AllocTest, NonPagedAllocIsZeroInitialized) |
| 38 | +{ |
| 39 | + // |
| 40 | + // Phase 1: Allocate and poison memory so the heap has a dirty free block. |
| 41 | + // |
| 42 | + uint8_t* First = |
| 43 | + (uint8_t*)CXPLAT_ALLOC_NONPAGED(TEST_ALLOC_SIZE, QUIC_POOL_TEST); |
| 44 | + ASSERT_NE(nullptr, First); |
| 45 | + |
| 46 | + memset(First, 0xDE, TEST_ALLOC_SIZE); |
| 47 | + CXPLAT_FREE(First, QUIC_POOL_TEST); |
| 48 | + |
| 49 | + // |
| 50 | + // Phase 2: Reallocate the same size. A non-zeroing allocator will likely |
| 51 | + // hand back the same (still dirty) block. |
| 52 | + // |
| 53 | + uint8_t* Second = |
| 54 | + (uint8_t*)CXPLAT_ALLOC_NONPAGED(TEST_ALLOC_SIZE, QUIC_POOL_TEST); |
| 55 | + ASSERT_NE(nullptr, Second); |
| 56 | + |
| 57 | + EXPECT_TRUE(IsZeroMemory(Second, TEST_ALLOC_SIZE)) |
| 58 | + << "CXPLAT_ALLOC_NONPAGED returned non-zero-initialized memory. "; |
| 59 | + |
| 60 | + CXPLAT_FREE(Second, QUIC_POOL_TEST); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(AllocTest, PagedAllocIsZeroInitialized) |
| 64 | +{ |
| 65 | + uint8_t* First = |
| 66 | + (uint8_t*)CXPLAT_ALLOC_PAGED(TEST_ALLOC_SIZE, QUIC_POOL_TEST); |
| 67 | + ASSERT_NE(nullptr, First); |
| 68 | + |
| 69 | + memset(First, 0xDE, TEST_ALLOC_SIZE); |
| 70 | + CXPLAT_FREE(First, QUIC_POOL_TEST); |
| 71 | + |
| 72 | + uint8_t* Second = |
| 73 | + (uint8_t*)CXPLAT_ALLOC_PAGED(TEST_ALLOC_SIZE, QUIC_POOL_TEST); |
| 74 | + ASSERT_NE(nullptr, Second); |
| 75 | + |
| 76 | + EXPECT_TRUE(IsZeroMemory(Second, TEST_ALLOC_SIZE)) |
| 77 | + << "CXPLAT_ALLOC_PAGED returned non-zero-initialized memory. "; |
| 78 | + |
| 79 | + CXPLAT_FREE(Second, QUIC_POOL_TEST); |
| 80 | +} |
| 81 | + |
| 82 | +// |
| 83 | +// CxPlatPoolAlloc can return a previously-used |
| 84 | +// object from its free list, and that object will contain stale data unless |
| 85 | +// the allocator explicitly zeroes it. |
| 86 | +// |
| 87 | +TEST(AllocTest, PoolAllocIsZeroInitializedOnReuse) |
| 88 | +{ |
| 89 | + CXPLAT_POOL Pool; |
| 90 | + CxPlatPoolInitialize(FALSE, TEST_ALLOC_SIZE, QUIC_POOL_TEST, &Pool); |
| 91 | + |
| 92 | + // |
| 93 | + // Phase 1: Allocate from pool, poison, return to pool. |
| 94 | + // |
| 95 | + uint8_t* First = (uint8_t*)CxPlatPoolAlloc(&Pool); |
| 96 | + ASSERT_NE(nullptr, First); |
| 97 | + |
| 98 | + memset(First, 0xDE, TEST_ALLOC_SIZE); |
| 99 | + CxPlatPoolFree(First); |
| 100 | + |
| 101 | + // |
| 102 | + // Phase 2: Re-allocate from pool. The pool will return the same object |
| 103 | + // from its free list. If pool alloc doesn't zero-init, it will still |
| 104 | + // contain the 0xDE poison bytes. |
| 105 | + // |
| 106 | + uint8_t* Second = (uint8_t*)CxPlatPoolAlloc(&Pool); |
| 107 | + ASSERT_NE(nullptr, Second); |
| 108 | + |
| 109 | + EXPECT_TRUE(IsZeroMemory(Second, TEST_ALLOC_SIZE)) |
| 110 | + << "CxPlatPoolAlloc returned non-zero-initialized memory on reuse. "; |
| 111 | + |
| 112 | + CxPlatPoolFree(Second); |
| 113 | + CxPlatPoolUninitialize(&Pool); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(AllocTest, PoolAllocIsZeroInitializedFresh) |
| 117 | +{ |
| 118 | + CXPLAT_POOL Pool; |
| 119 | + CxPlatPoolInitialize(FALSE, TEST_ALLOC_SIZE, QUIC_POOL_TEST, &Pool); |
| 120 | + |
| 121 | + uint8_t* Mem = (uint8_t*)CxPlatPoolAlloc(&Pool); |
| 122 | + ASSERT_NE(nullptr, Mem); |
| 123 | + |
| 124 | + EXPECT_TRUE(IsZeroMemory(Mem, TEST_ALLOC_SIZE)) |
| 125 | + << "CxPlatPoolAlloc returned non-zero-initialized memory on fresh alloc. "; |
| 126 | + |
| 127 | + CxPlatPoolFree(Mem); |
| 128 | + CxPlatPoolUninitialize(&Pool); |
| 129 | +} |
0 commit comments