aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/foundation/src/malloc.cpp
blob: c8d4c5d6673717446b33919a0c1cd7ccbcfa354f (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
#include <foundation/types.h>
#include <foundation/malloc.h>
#include <foundation/platform.h>

//------------------------------------------------------------------------------
struct Header
{
    enum : uint32 { Magic = 0xaa55aa55 };
    size_t  size;
    uint32  bias;
    uint32  canary;
    uint8   ptr[];
};
static bool g_add_canaries = false;

//------------------------------------------------------------------------------
#ifdef _WIN32

template <typename T=uint8>
static T* tt_malloc_norm(size_t size, size_t alignment)
{
    alignment = uint32(std::max(alignment, alignof(std::max_align_t)));
    if (!g_add_canaries)
        return (T*)_aligned_malloc(size, alignment);

    uint32 lead_size = uint32(sizeof(Header) + alignment - 1) & ~uint32(alignment - 1);
    size_t alloc_size = lead_size + size + sizeof(uint32);
    auto* alloc_ptr = (uint8*)_aligned_malloc(alloc_size, alignment);

    auto* header = (Header*)(alloc_ptr + lead_size - sizeof(Header));
    header->bias = lead_size;
    header->size = size;
    header->canary = Header::Magic;

    std::memcpy(header->ptr + size, &header->canary, sizeof(uint32));

    return header->ptr;
}

static void tt_free_norm(void* address)
{
    if (address == nullptr)
        return;

    if (!g_add_canaries)
        return _aligned_free(address);

    auto* header = (Header*)address - 1;

    auto is_canary = [] (void* ptr) {
        uint32 canary = Header::Magic;
        return !std::memcmp(ptr, &canary, sizeof(uint32));
    };
    if (!is_canary(&header->canary))            __debugbreak();
    if (!is_canary(header->ptr + header->size)) __debugbreak();

    _aligned_free(header->ptr - header->bias);
}

#else // POSIX

template <typename T=uint8>
static T* tt_malloc_norm(size_t size, size_t alignment)
{
    alignment = std::max(alignment, alignof(std::max_align_t));
    // std::aligned_alloc requires size to be a multiple of alignment
    size_t aligned_size = (size + alignment - 1) & ~(alignment - 1);
    if (aligned_size == 0)
        aligned_size = alignment;

    if (!g_add_canaries)
        return (T*)std::aligned_alloc(alignment, aligned_size);

    uint32 lead_size = uint32(sizeof(Header) + alignment - 1) & ~uint32(alignment - 1);
    size_t alloc_size = lead_size + size + sizeof(uint32);
    size_t alloc_aligned = (alloc_size + alignment - 1) & ~(alignment - 1);
    auto* alloc_ptr = (uint8*)std::aligned_alloc(alignment, alloc_aligned);

    auto* header = (Header*)(alloc_ptr + lead_size - sizeof(Header));
    header->bias = lead_size;
    header->size = size;
    header->canary = Header::Magic;

    std::memcpy(header->ptr + size, &header->canary, sizeof(uint32));

    return header->ptr;
}

static void tt_free_norm(void* address)
{
    if (address == nullptr)
        return;

    if (!g_add_canaries)
        return std::free(address);

    auto* header = (Header*)address - 1;

    auto is_canary = [] (void* ptr) {
        uint32 canary = Header::Magic;
        return !std::memcmp(ptr, &canary, sizeof(uint32));
    };
    if (!is_canary(&header->canary))            __builtin_trap();
    if (!is_canary(header->ptr + header->size)) __builtin_trap();

    std::free(header->ptr - header->bias);
}

#endif

//------------------------------------------------------------------------------
void tt_memory_canaries_on()
{
    g_add_canaries = true;
}


//------------------------------------------------------------------------------
void*    (*tt_malloc_impl)(size_t, size_t)  = tt_malloc_norm;
void     (*tt_free)(void*)                  = tt_free_norm;



//------------------------------------------------------------------------------
#ifdef _WIN32

template <typename T=uint8>
static T* tt_malloc_stomp(size_t size, size_t alignment)
{
    enum { PAGE_SIZE = 64 << 10 };
    size = (size + (alignment - 1)) & ~(alignment - 1);
    size_t alloc_size = (size + (2 * PAGE_SIZE)) & ~(PAGE_SIZE - 1);
    auto* ptr = (uint8*)VirtualAlloc(nullptr, alloc_size, MEM_COMMIT, PAGE_READWRITE);
    ptr += alloc_size - PAGE_SIZE;
    DWORD unused;
    VirtualProtect(ptr, PAGE_SIZE, PAGE_NOACCESS, &unused);

    return (T*)(ptr - size);
}

static void tt_free_stomp(void* address)
{
    if (address == nullptr)
        return;

    *(uint32*)address = 0x30493049;

    MEMORY_BASIC_INFORMATION info;
    VirtualQuery(address, &info, sizeof(info));
    DWORD unused;
    VirtualProtect(info.AllocationBase, info.RegionSize, PAGE_NOACCESS, &unused);
}

#else // POSIX

static size_t tt_get_page_size()
{
    static size_t page_size = size_t(sysconf(_SC_PAGESIZE));
    return page_size;
}

struct StompHeader
{
    void*  base;
    size_t total_size;
};

template <typename T=uint8>
static T* tt_malloc_stomp(size_t size, size_t alignment)
{
    size_t page_size = tt_get_page_size();
    size = (size + (alignment - 1)) & ~(alignment - 1);
    size_t needed = sizeof(StompHeader) + size + page_size;
    size_t alloc_size = (needed + (page_size - 1)) & ~(page_size - 1);
    auto* base = (uint8*)mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE,
                              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (base == MAP_FAILED)
        return nullptr;

    // Guard page at the end
    uint8* guard = base + alloc_size - page_size;
    mprotect(guard, page_size, PROT_NONE);

    // User allocation right before the guard, header right before that
    T* user_ptr = (T*)(guard - size);
    auto* hdr = (StompHeader*)((uint8*)user_ptr - sizeof(StompHeader));
    hdr->base = base;
    hdr->total_size = alloc_size;

    return user_ptr;
}

static void tt_free_stomp(void* address)
{
    if (address == nullptr)
        return;

    *(uint32*)address = 0x30493049;

    auto* hdr = (StompHeader*)((uint8*)address - sizeof(StompHeader));
    mprotect(hdr->base, hdr->total_size, PROT_NONE);
}

#endif

//------------------------------------------------------------------------------
void tt_memory_stomp_on()
{
    tt_malloc_impl = tt_malloc_stomp;
    tt_free = tt_free_stomp;
}