-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathheap_glue.py
175 lines (140 loc) · 4.22 KB
/
heap_glue.py
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
#
# Heap/memory allocation glue
#
# Copyright (c) 2020, Arm Limited. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
from .. import glue
GCC_HOOKS = """
#if defined(__GNUC__)
// state of brk
static uint8_t *__heap_brk = NULL;
// assigned by linker
extern uint8_t __heap_start;
extern uint8_t __heap_end;
// GCC's _sbrk uses sp for bounds checking, this
// does not work if our stack is located before the heap
void *_sbrk(ptrdiff_t diff) {
if (!__heap_brk) {
__heap_brk = &__heap_start;
}
uint8_t *pbrk = __heap_brk;
if (pbrk + diff > &__heap_end) {
return (void*)-1;
}
__heap_brk = pbrk+diff;
return pbrk;
}
#endif
"""
# quick heap implementation when unavailable in stdlib
# this is needed for wasm, not because there is no heap, but
# because the heap forces >1 pages (64 KiB each).
#
# note this does not free
#
# TODO replace this
REPLACE_ME_HEAP = """
ssize_t *__heap_start = NULL;
ssize_t *__heap_end;
void *__wrap_malloc(size_t size) {
if (!__heap_start) {
__heap_end = (ssize_t*)(__builtin_wasm_memory_size(0)*64*1024);
__heap_start = __heap_end - %(heap_size)d/4;
__heap_start[0] = %(heap_size)d/4 - 2;
__heap_end[-1] = %(heap_size)d/4 - 2;
}
size = (size+3) / 4;
ssize_t *ptr = __heap_start;
while (ptr < __heap_end) {
// make sure heap isn't corrupted
ssize_t psize = ptr[0];
if (ptr[0] != ptr[(psize & 0x7fffffff)+2-1]) {
__box_abort(-EFAULT);
}
// fits?
if (psize >= (ssize_t)size) {
// should split?
if (psize >= size+2) {
ptr[size+2] = psize - (size+2);
ptr[psize+2-1] = psize - (size+2);
ptr[0] = size;
ptr[size+2-1] = size;
psize = size;
}
// mark as used
ptr[0] = psize | 0x80000000;
ptr[size+2-1] = psize | 0x80000000;
return ptr+1;
}
ptr += (psize & 0x7fffffff) + 2;
}
return NULL;
}
void __wrap_free(void *pptr) {
if (!pptr) {
return;
}
ssize_t *ptr = (ssize_t*)pptr - 1;
ssize_t psize = ptr[0];
// make sure heap isn't corrupted
if (ptr[0] != ptr[(psize & 0x7fffffff)+2-1]) {
__box_abort(-EFAULT);
}
psize = psize & 0x7fffffff;
// coalesce blocks?
while (true) {
if (&ptr[-1] > __heap_start && ptr[-1] >= 0) {
psize += ptr[-1] + 2;
ptr -= ptr[-1] + 2;
} else if (&ptr[psize+2] < __heap_end && ptr[psize+2] >= 0) {
psize += ptr[psize+2] + 2;
} else {
break;
}
}
// mark as free
ptr[0] = psize;
ptr[psize+2-1] = psize;
}
void *__wrap_calloc(size_t size) {
uint8_t *ptr = __wrap_malloc(size);
memset(ptr, 0, size);
return ptr;
}
void *__wrap_realloc(void *pptr, size_t size) {
ssize_t psize = pptr ? ((ssize_t*)pptr)[-1] : 0;
void *nptr = __wrap_malloc(size);
if (nptr) {
// yes this copies extra, we don't really care
memmove(nptr, pptr, psize);
__wrap_free(pptr);
}
return nptr;
}
"""
class HeapGlue(glue.Glue):
"""
Helper layer for handling heap related functions.
"""
__name = 'heap_glue'
def build_c(self, output, box):
super().build_c(output, box)
if not output.no_stdlib_hooks:
output.decls.append(GCC_HOOKS)
def build_wasm_c(self, output, box):
super().build_wasm_c(output, box)
if not output.no_stdlib_hooks:
output.decls.append(REPLACE_ME_HEAP,
heap_size=box.heap.size)
def build_mk(self, output, box):
super().build_mk(output, box)
if (not output.no_wasm and
'wasm_c' in box.outputs and
not box.outputs[box.outputs.index('wasm_c')].no_stdlib_hooks):
out = output.decls.append()
out.printf('### replace-me-heap glue ###')
out.printf('override WASMLDFLAGS += -Wl,--wrap,malloc')
out.printf('override WASMLDFLAGS += -Wl,--wrap,free')
out.printf('override WASMLDFLAGS += -Wl,--wrap,calloc')
out.printf('override WASMLDFLAGS += -Wl,--wrap,realloc')