aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/poison.cpp
blob: 2d05246df8f898ab560f8fa8348443fb04fab1a1 (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
#include "poison.h"

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace {

template <char Byte0, char Byte1, char Byte2, char Byte3>
void ClobberMemory(char* __restrict__ ptr, size_t size)
{
    while (size >= 4) {
        *ptr++ = Byte0;
        *ptr++ = Byte1;
        *ptr++ = Byte2;
        *ptr++ = Byte3;
        size -= 4;
    }

    switch (size) {
        case 3:
            *ptr++ = Byte0;
            [[fallthrough]];
        case 2:
            *ptr++ = Byte1;
            [[fallthrough]];
        case 1:
            *ptr++ = Byte2;
    }
}

} // namespace

#if !defined(NDEBUG) && !defined(_asan_enabled_) && !defined(_msan_enabled_)

void PoisonUninitializedMemory(TMutableRef ref)
{
    // BAADBOBA
    ClobberMemory<'\xba', '\xad', '\xb0', '\xba'>(ref.data(), ref.size());
}

void PoisonFreedMemory(TMutableRef ref)
{
    // DEADBEEF
    ClobberMemory<'\xde', '\xad', '\xbe', '\xef'>(ref.data(), ref.size());
}

void RecycleFreedMemory(TMutableRef ref)
{
    // COOLBIBA
    ClobberMemory<'\xc0', '\x01', '\xb1', '\xba'>(ref.data(), ref.size());
}

#endif

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT