aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/sanitizer_ut/sanitizer_ut.cpp
blob: adaa6b8d69a773a390077ae582ba9d6a59aaa2c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <yql/essentials/minikql/mkql_alloc.h>

#include <library/cpp/testing/gtest/gtest.h>

#include <cstdlib>
#include <cstdio>

namespace NKikimr::NMiniKQL {

enum class EAllocatorType {
    DefaultAllocator,
    ArrowAllocator,
    HugeAllocator,
};

class MemoryTest: public testing::Test {
protected:
    MemoryTest()
        : ScopedAlloc_(__LOCATION__) {
    }

    void AccessMemory(volatile void* memory, ssize_t offset) const {
        volatile char* ptr = static_cast<volatile char*>(memory) + offset;
        *ptr = 'A'; // Perform a basic write operation
    }

    void BranchMemory(const volatile void* memory, ssize_t offset) const {
        const volatile char* ptr = static_cast<const volatile char*>(memory) + offset;
        if (*ptr == 'A') {
            Cerr << "Branch access" << Endl;
        }
    }

    TAlignedPagePool& GetPagePool() {
        return ScopedAlloc_.Ref();
    }

private:
    TScopedAlloc ScopedAlloc_;
};

class MemoryTestWithSizeAndAllocator: public MemoryTest, public ::testing::WithParamInterface<std::tuple<int, EAllocatorType>> {
protected:
    size_t AllocSize() const {
        return static_cast<size_t>(std::get<0>(GetParam()));
    }

    EAllocatorType GetAllocatorType() const {
        return std::get<1>(GetParam());
    }

    void* AllocateMemory(size_t size) const {
        EAllocatorType allocatorType = GetAllocatorType();
        switch (allocatorType) {
            case EAllocatorType::DefaultAllocator:
                return TWithDefaultMiniKQLAlloc::AllocWithSize(size);
            case EAllocatorType::ArrowAllocator:
                return MKQLArrowAllocate(size);
            case EAllocatorType::HugeAllocator:
                return TMKQLHugeAllocator<char>::allocate(size);
            default:
                return nullptr; // Should never reach here
        }
    }

    void Free(const void* mem, size_t size) const {
        EAllocatorType allocatorType = GetAllocatorType();
        switch (allocatorType) {
            case EAllocatorType::DefaultAllocator:
                TWithDefaultMiniKQLAlloc::FreeWithSize(mem, size);
                break;
            case EAllocatorType::ArrowAllocator:
                MKQLArrowFree(mem, size);
                break;
            case EAllocatorType::HugeAllocator:
                TMKQLHugeAllocator<char>::deallocate(const_cast<char*>(static_cast<const char*>(mem)), size);
                break;
            default:
                break; // Should never reach here
        }
    }
};

// Test naming function
std::string TestNameGenerator(const ::testing::TestParamInfo<MemoryTestWithSizeAndAllocator::ParamType>& info) {
    int sizeNumber = std::get<0>(info.param);
    EAllocatorType allocatorType = std::get<1>(info.param);

    std::string allocatorName = [&]() {
        switch (allocatorType) {
            case EAllocatorType::DefaultAllocator:
                return "DefaultAllocator";
            case EAllocatorType::ArrowAllocator:
                return "ArrowAllocator";
            case EAllocatorType::HugeAllocator:
                return "HugeAllocator";
        }
    }();

    return "Size" + std::to_string(sizeNumber) + "With" + allocatorName + "Allocator";
}

// Out of bounds access + use after free can be tested only with
// --sanitize=address.
#if defined(_asan_enabled_)
TEST_P(MemoryTestWithSizeAndAllocator, AccessOutOfBounds) {
    size_t allocationSize = AllocSize();

    void* memory = AllocateMemory(allocationSize);
    ASSERT_NE(memory, nullptr) << "Memory allocation failed.";
    // Accessing valid memory.
    ASSERT_NO_THROW({
        AccessMemory(memory, 0);
        AccessMemory(memory, allocationSize - 1);
    });

    // Accessing invalid left memory.
    EXPECT_DEATH({ AccessMemory(memory, -1); }, "");
    EXPECT_DEATH({ AccessMemory(memory, -8); }, "");
    EXPECT_DEATH({ AccessMemory(memory, -16); }, "");

    // Accessing invalid right memory.
    EXPECT_DEATH({ AccessMemory(memory, allocationSize); }, "");
    EXPECT_DEATH({ AccessMemory(memory, allocationSize + 6); }, "");
    EXPECT_DEATH({ AccessMemory(memory, allocationSize + 12); }, "");
    EXPECT_DEATH({ AccessMemory(memory, allocationSize + 15); }, "");

    Free(memory, allocationSize);
}

TEST_P(MemoryTestWithSizeAndAllocator, AccessAfterFree) {
    size_t allocationSize = AllocSize();
    void* memory = AllocateMemory(allocationSize);
    void* memory2 = AllocateMemory(allocationSize);
    ASSERT_NE(memory, nullptr) << "Memory allocation failed.";
    Free(memory, allocationSize);

    // Access after free — should crash
    EXPECT_DEATH({ AccessMemory(memory, 0); }, "");
    EXPECT_DEATH({ AccessMemory(memory, allocationSize / 2); }, "");
    EXPECT_DEATH({ AccessMemory(memory, allocationSize - 1); }, "");

    Free(memory2, allocationSize);
    // Access after free — should crash
    EXPECT_DEATH({ AccessMemory(memory2, 0); }, "");
    EXPECT_DEATH({ AccessMemory(memory2, allocationSize / 2); }, "");
    EXPECT_DEATH({ AccessMemory(memory2, allocationSize - 1); }, "");
}

TEST_F(MemoryTest, TestPageFromPagePool) {
    auto* page = GetPagePool().GetPage();
    // No access allowed.
    EXPECT_DEATH({ AccessMemory(page, 0); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE / 2); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE - 1); }, "");

    // Allow access.
    NYql::NUdf::SanitizerMakeRegionAccessible(page, TAlignedPagePool::POOL_PAGE_SIZE);

    // Access allowed.
    AccessMemory(page, 0);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE / 2);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE - 1);

    // Return page should disable access.
    GetPagePool().ReturnPage(page);

    // Page should be unaddressable after being returned.
    EXPECT_DEATH({ AccessMemory(page, 0); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE / 2); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE - 1); }, "");
}

TEST_F(MemoryTest, TestBlockFromPagePool) {
    auto* page = GetPagePool().GetBlock(TAlignedPagePool::POOL_PAGE_SIZE * 2);
    // No access allowed.
    EXPECT_DEATH({ AccessMemory(page, 0); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1); }, "");

    // Allow access.
    NYql::NUdf::SanitizerMakeRegionAccessible(page, TAlignedPagePool::POOL_PAGE_SIZE * 2);

    // Access allowed.
    AccessMemory(page, 0);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1);
    // Return page.
    GetPagePool().ReturnBlock(page, TAlignedPagePool::POOL_PAGE_SIZE * 2);

    // Page should be unaddressable after being returned.
    EXPECT_DEATH({ AccessMemory(page, 0); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE); }, "");
    EXPECT_DEATH({ AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1); }, "");
}

#endif // defined(_asan_enabled_)

#if defined(_msan_enabled_)

TEST_P(MemoryTestWithSizeAndAllocator, UninitializedAccess) {
    size_t allocationSize = AllocSize();
    void* memory = AllocateMemory(allocationSize);

    // Check unitialized access.
    EXPECT_DEATH({ BranchMemory(memory, 0); }, "");
    EXPECT_DEATH({ BranchMemory(memory, AllocSize() - 1); }, "");

    // Initialize memory.
    memset(memory, 0, allocationSize);

    // Check initialized access.
    BranchMemory(memory, 0);
    BranchMemory(memory, AllocSize() - 1);

    Free(memory, allocationSize);
}

TEST_F(MemoryTest, TestPageFromPagePool) {
    auto* page = GetPagePool().GetPage();

    // No access allowed.
    EXPECT_DEATH({ BranchMemory(page, 0); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE / 2); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE - 1); }, "");

    // Open region.
    NYql::NUdf::SanitizerMakeRegionAccessible(page, TAlignedPagePool::POOL_PAGE_SIZE);

    // Still cannot access memory. Opening region does not make memory initialized.
    EXPECT_DEATH({ BranchMemory(page, 0); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1); }, "");

    // Allow access.
    memset(page, 0, TAlignedPagePool::POOL_PAGE_SIZE * 2);

    // Access should be valid.
    BranchMemory(page, 0);
    BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE);
    BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1);

    // Return page should disable access.
    GetPagePool().ReturnPage(page);
}

TEST_F(MemoryTest, TestScopedInterceptorDisable) {
    // Allocate memory but don't initialize it
    size_t allocationSize = 64;
    char* uninitializedSrc = static_cast<char*>(TWithDefaultMiniKQLAlloc::AllocWithSize(allocationSize));
    ASSERT_NE(uninitializedSrc, nullptr) << "Source memory allocation failed.";
    Y_DEFER {
        TWithDefaultMiniKQLAlloc::FreeWithSize(uninitializedSrc, allocationSize);
    };

    // Without disabling access check, using uninitialized memory should trigger MSAN.
    EXPECT_DEATH({
        BranchMemory(uninitializedSrc, 0);
    }, "");

    // With YQL_MSAN_FREEZE_AND_SCOPED_UNPOISON, the access check should be disabled.
    {
        YQL_MSAN_FREEZE_AND_SCOPED_UNPOISON(uninitializedSrc, allocationSize);

        // This should not trigger MSAN.
        BranchMemory(uninitializedSrc, 0);
    }

    // After the scope ends, access check should be re-enabled.
    EXPECT_DEATH({
        BranchMemory(uninitializedSrc, 0);
    }, "");
}

TEST_F(MemoryTest, TestBlockMsan) {
    auto* page = GetPagePool().GetBlock(TAlignedPagePool::POOL_PAGE_SIZE * 2);

    // No access allowed.
    EXPECT_DEATH({ BranchMemory(page, 0); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1); }, "");

    // Open region.
    NYql::NUdf::SanitizerMakeRegionAccessible(page, TAlignedPagePool::POOL_PAGE_SIZE);

    // Still cannot access memory.
    EXPECT_DEATH({ BranchMemory(page, 0); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE); }, "");
    EXPECT_DEATH({ BranchMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1); }, "");

    // Allow access via memory initialization.
    memset(page, 0, TAlignedPagePool::POOL_PAGE_SIZE * 2);

    // Access allowed.
    AccessMemory(page, 0);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2);
    AccessMemory(page, TAlignedPagePool::POOL_PAGE_SIZE * 2 - 1);
    GetPagePool().ReturnBlock(page, TAlignedPagePool::POOL_PAGE_SIZE * 2);
}

#endif // defined(_msan_enabled_)

// Double free tracked only in DEBUG mode.
#ifndef NDEBUG
TEST_P(MemoryTestWithSizeAndAllocator, DoubleFree) {
    if (GetAllocatorType() == EAllocatorType::ArrowAllocator || GetAllocatorType() == EAllocatorType::HugeAllocator) {
        GTEST_SKIP() << "Arrow and Huge allocators arae not instrumented yet to track double free.";
    }
    size_t allocationSize = AllocSize();

    void* memory = AllocateMemory(allocationSize);
    ASSERT_NE(memory, nullptr) << "Memory allocation failed.";

    Free(memory, allocationSize);

    // Attempting double free — should crash
    EXPECT_DEATH({ Free(memory, allocationSize); }, "");
}
#endif // NDEBUG

// Allow empty tests for MSAN and other sanitizers.
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemoryTestWithSizeAndAllocator);

INSTANTIATE_TEST_SUITE_P(MemoryTestWithSizeAndAllocators, MemoryTestWithSizeAndAllocator,
                         ::testing::Combine(::testing::Values(8, 64, 32 * 1024, 64 * 1024, 128 * 1024, 64 * 1024 * 1024),
                                            ::testing::Values(EAllocatorType::DefaultAllocator, EAllocatorType::ArrowAllocator,
                                                              EAllocatorType::HugeAllocator)),
                         TestNameGenerator);

} // namespace NKikimr::NMiniKQL