8000 WIP: upstream version of umm_malloc customized for Arduino ESP8266 Core · esp8266/Arduino@b508642 · GitHub
[go: up one dir, main page]

Skip to content

Commit b508642

Browse files
committed
WIP: upstream version of umm_malloc customized for Arduino ESP8266 Core
See comments at the top of umm_malloc.cpp for a summary of changes so far. Added support for integrity check and poison check to refactored heap.cpp.
1 parent 88a32c8 commit b508642

File tree

12 files changed

+1273
-161
lines changed

12 files changed

+1273
-161
lines changed

cores/esp8266/heap.cpp

Lines changed: 148 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,116 @@
88
#include <c_types.h>
99
#include <sys/reent.h>
1010

11+
1112
extern "C" {
1213

14+
#if defined( UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
15+
#define __umm_malloc(s) umm_poison_malloc(s)
16+
#define __umm_calloc(n,s) umm_poison_calloc(n,s)
17+
#define __umm_realloc_fl(p,s,f,l) umm_poison_realloc_fl(p,s,f,l)
18+
#define __umm_free_fl(p,f,l) umm_poison_free_fl(p,f,l)
19+
20+
#ifdef UMM_POISON_CHECK_LITE
21+
#define POISON_CHECK__ABORT() do {} while(0)
22+
#define POISON_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0)
23+
24+
#else // Full heap poison check at every malloc libary call.
25+
#define POISON_CHECK__ABORT() \
26+
do { \
27+
if ( ! POISON_CHECK() ) \
28+
abort(); \
29+
} while(0)
30+
31+
#define POISON_CHECK__PANIC_FL(file, line) \
32+
do { \
33+
if ( ! POISON_CHECK() ) \
34+
__panic_func(file, line, ""); \
35+
} while(0)
36+
#endif
37+
38+
#undef realloc
39+
#undef free
40+
41+
#else // ! UMM_POISON_CHECK
42+
#define __umm_malloc(s) malloc(s)
43+
#define __umm_calloc(n,s) calloc(n,s)
44+
#define __umm_realloc_fl(p,s,f,l) realloc(p,s)
45+
#define __umm_free_fl(p,f,l) free(p)
46+
47+
#define POISON_CHECK__ABORT() do {} while(0)
48+
#define POISON_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0)
49+
#endif // UMM_POISON_CHECK
50+
1351
// Debugging helper, last allocation which returned NULL
1452
void *umm_last_fail_alloc_addr = NULL;
1553
int umm_last_fail_alloc_size = 0;
1654

55+
56+
#ifdef UMM_INTEGRITY_CHECK
57+
#define INTEGRITY_CHECK__ABORT() \
58+
do { \
59+
if ( ! INTEGRITY_CHECK() ) \
60+
abort(); \
61+
} while(0)
62+
63+
#define INTEGRITY_CHECK__PANIC_FL(file, line) \
64+
do { \
65+
if ( ! INTEGRITY_CHECK() ) \
66+
__panic_func(file, line, ""); \
67+
} while(0)
68+
69+
#else // ! UMM_INTEGRITY_CHECK
70+
#define INTEGRITY_CHECK__ABORT() do {} while(0)
71+
#define INTEGRITY_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0)
72+
73+
#endif // UMM_INTEGRITY_CHECK
74+
75+
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a) \
76+
if(0 != (s) && 0 == p)\
77+
{\
78+
umm_last_fail_alloc_addr = a;\
79+
umm_last_fail_alloc_size = s;\
80+
}
81+
1782
void* _malloc_r(struct _reent* unused, size_t size)
1883
{
1984
(void) unused;
2085
void *ret = malloc(size);
21-
if (0 != size && 0 == ret) {
22-
umm_last_fail_alloc_addr = __builtin_return_address(0);
23-
umm_last_fail_alloc_size = size;
24-
}
86+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0))
2587
return ret;
2688
}
2789

2890
void _free_r(struct _reent* unused, void* ptr)
2991
{
3092
(void) unused;
31-
return free(ptr);
93+
free(ptr);
3294
}
3395

3496
void* _realloc_r(struct _reent* unused, void* ptr, size_t size)
3597
{
3698
(void) unused;
3799
void *ret = realloc(ptr, size);
38-
if (0 != size && 0 == ret) {
39-
umm_last_fail_alloc_addr = __builtin_return_address(0);
40-
umm_last_fail_alloc_size = size;
41-
}
100+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0))
42101
return ret;
43102
}
44103

45104
void* _calloc_r(struct _reent* unused, size_t count, size_t size)
46105
{
47106
(void) unused;
48107
void *ret = calloc(count, size);
49-
if (0 != (count * size) && 0 == ret) {
50-
umm_last_fail_alloc_addr = __builtin_return_address(0);
51-
umm_last_fail_alloc_size = count * size;
52-
}
108+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0))
53109
return ret;
54110
}
55111

56-
void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line)
57-
{
58-
(void) file;
59-
(void) line;
60-
free(ptr);
61-
}
62-
63-
#ifdef DEBUG_ESP_OOM
64-
65-
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
66-
{
67-
return malloc_loc(size, file, line);
68-
}
69-
70-
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
71-
{
72-
return calloc_loc(count, size, file, line);
73-
}
74-
75-
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
76-
{
77-
return realloc_loc(ptr, size, file, line);
78-
}
79112

80-
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
81-
{
82-
return calloc_loc(1, size, file, line);
83-
}
113+
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a) \
114+
if(0 != (s) && 0 == p)\
115+
{\
116+
umm_last_fail_alloc_addr = a;\
117+
umm_last_fail_alloc_size = s;\
118+
}
84119

120+
#ifdef DEBUG_ESP_OOM
85121
#undef malloc
86122
#undef calloc
87123
#undef realloc
@@ -90,96 +126,115 @@ static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
90126
static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@";
91127
static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n";
92128

93-
void* malloc (size_t s)
129+
void ICACHE_RAM_ATTR print_loc(size_t size, const char* file, int line)
94130
{
95-
void* ret = umm_malloc(s);
96-
if (!ret)
97-
os_printf(oom_fmt, (int)s);
98-
return ret;
131+
DBGLOG_FUNCTION_P(oom_fmt_1, (int)size);
132+
DBGLOG_FUNCTION_P(file);
133+
DBGLOG_FUNCTION_P(oom_fmt_2, line);
99134
}
100135

101-
void* calloc (size_t n, size_t s)
102-
{
103-
void* ret = umm_calloc(n, s);
104-
if (!ret)
105-
os_printf(oom_fmt, (int)s);
106-
return ret;
107-
}
136+
#define OOM_CHECK__PRINT_OOM(p, f, s) if (!p) DBGLOG_FUNCTION_P(f, s)
137+
#define OOM_CHECK__PRINT_LOC(p, s, f, l) if (!p) print_loc(s, f, l)
108138

109-
void* realloc (void* p, size_t s)
110-
{
111-
void* ret = umm_realloc(p, s);
112-
if (!ret)
113-
os_printf(oom_fmt, (int)s);
114-
return ret;
115-
}
139+
#else // ! DEBUG_ESP_OOM
140+
141+
#if 1
142+
//C Skip OOM check of pvPort... . It cost 64 more bytes of IRAM to turn on.
143+
//C Was not previously enabled.
144+
#undef PTR_CHECK__LOG_LAST_FAIL
145+
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a)
146+
#endif
116147

117-
void print_loc (size_t s, const char* file, int line)
148+
#define OOM_CHECK__PRINT_OOM(p, f, s)
149+
#define OOM_CHECK__PRINT_LOC(p, s, f, l)
150+
#endif
151+
152+
#if defined(DEBUG_ESP_OOM) || defined(UMM_CHECK_POISON) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
153+
154+
void* ICACHE_RAM_ATTR malloc(size_t size)
118155
{
119-
os_printf(oom_fmt_1, (int)s);
120-
os_printf(file);
121-
os_printf(oom_fmt_2, line);
156+
INTEGRITY_CHECK__ABORT();
157+
POISON_CHECK__ABORT();
158+
void* ret = __umm_malloc(size);
159+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
160+
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
161+
return ret;
122162
}
123163

124-
void* malloc_loc (size_t s, const char* file, int line)
164+
void* ICACHE_RAM_ATTR calloc(size_t count, size_t size)
125165
{
126-
void* ret = umm_malloc(s);
127-
if (!ret)
128-
print_loc(s, file, line);
166+
INTEGRITY_CHECK__ABORT();
167+
POISON_CHECK__ABORT();
168+
void* ret = __umm_calloc(count, size);
169+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0));
170+
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
129171
return ret;
130172
}
131173

132-
void* calloc_loc (size_t n, size_t s, const char* file, int line)
174+
void* ICACHE_RAM_ATTR realloc(void* ptr, size_t size)
133175
{
134-
void* ret = umm_calloc(n, s);
135-
if (!ret)
136-
print_loc(s, file, line);
176+
INTEGRITY_CHECK__ABORT();
177+
void* ret = __umm_realloc_fl(ptr, size, NULL, 0);
178+
POISON_CHECK__ABORT();
179+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
180+
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
137181
return ret;
138182
}
139183

140-
void* realloc_loc (void* p, size_t s, const char* file, int line)
184+
void ICACHE_RAM_ATTR free(void* p)
141185
{
142-
void* ret = umm_realloc(p, s);
143-
if (!ret)
144-
print_loc(s, file, line);
145-
return ret;
186+
INTEGRITY_CHECK__ABORT();
187+
__umm_free_fl(p, NULL, 0);
188+
POISON_CHECK__ABORT();
146189
}
190+
#endif
147191

148-
#else
149192

150193
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
151194
{
152-
(void) file;
153-
(void) line;
154-
return malloc(size);
195+
INTEGRITY_CHECK__PANIC_FL(file, line);
196+
POISON_CHECK__PANIC_FL(file, line);
197+
void* ret = __umm_malloc(size);
198+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
199+
OOM_CHECK__PRINT_LOC(ret, size, file, line);
200+
return ret;
155201
}
156202

157203
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
158204
{
159-
(void) file;
160-
(void) line;
161-
return calloc(count, size);
205+
INTEGRITY_CHECK__PANIC_FL(file, line);
206+
POISON_CHECK__PANIC_FL(file, line);
207+
void* ret = __umm_calloc(count, size);
208+
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0));
209+
OOM_CHECK__PRINT_LOC(ret, size, file, line);
210+
return ret;
162211
}
163212

164213
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
165214
{
166-
(void) file;
167-
(void) line;
168-
return realloc(ptr, size);
215+
INTEGRITY_CHECK__PANIC_FL(file, line);
216+
void* ret = __umm_realloc_fl(ptr, size, file, line);
217+
POISON_CHECK__PANIC_FL(file, line);
218+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
219+
OOM_CHECK__PRINT_LOC(ret, size, file, line);
220+
return ret;
169221
}
170222

171223
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
172224
{
173-
(void) file;
174-
(void) line;
175-
return calloc(1, size);
225+
INTEGRITY_CHECK__PANIC_FL(file, line);
226+
POISON_CHECK__PANIC_FL(file, line);
227+
void* ret = __umm_calloc(1, size);
228+
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
229+
OOM_CHECK__PRINT_LOC(ret, size, file, line);
230+
return ret;
176231
}
177232

178-
#endif // !defined(DEBUG_ESP_OOM)
179-
180-
size_t xPortGetFreeHeapSize(void)
233+
void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line)
181234
{
182-
return umm_free_heap_size();
235+
INTEGRITY_CHECK__PANIC_FL(file, line);
236+
POISON_CHECK__PANIC_FL(file, line);
237+
__umm_free_fl(ptr, file, line);
183238
}
184239

185240
size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size)

0 commit comments

Comments
 (0)
0