Skip to content

Commit 041c910

Browse files
mhightower83hasenradball
authored andcommitted
Fix, calloc now fails on extra-large request. (esp8266#8482)
Added code to handle multiply overflow in calloc. Added code to handle add overflow in umm_poison_*
1 parent 5df6933 commit 041c910

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

cores/esp8266/heap.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stdlib.h>
77
#include "umm_malloc/umm_malloc.h"
8+
extern "C" size_t umm_umul_sat(const size_t a, const size_t b);;
89

910
// Need FORCE_ALWAYS_INLINE to put HeapSelect class constructor/deconstructor in IRAM
1011
#define FORCE_ALWAYS_INLINE_HEAP_SELECT
@@ -153,7 +154,7 @@ void* _calloc_r(struct _reent* unused, size_t count, size_t size)
153154
{
154155
(void) unused;
155156
void *ret = calloc(count, size);
156-
PTR_CHECK__LOG_LAST_FAIL(ret, count * size);
157+
PTR_CHECK__LOG_LAST_FAIL(ret, umm_umul_sat(count, size));
157158
return ret;
158159
}
159160

@@ -247,8 +248,11 @@ void* IRAM_ATTR calloc(size_t count, size_t size)
247248
INTEGRITY_CHECK__ABORT();
248249
POISON_CHECK__ABORT();
249250
void* ret = UMM_CALLOC(count, size);
250-
PTR_CHECK__LOG_LAST_FAIL(ret, count * size);
251-
OOM_CHECK__PRINT_OOM(ret, size);
251+
#if defined(DEBUG_ESP_OOM)
252+
size_t total_size = umm_umul_sat(count, size);// For logging purposes
253+
#endif
254+
PTR_CHECK__LOG_LAST_FAIL(ret, total_size);
255+
OOM_CHECK__PRINT_OOM(ret, total_size);
252256
return ret;
253257
}
254258

@@ -287,8 +291,11 @@ void* IRAM_ATTR heap_pvPortCalloc(size_t count, size_t size, const char* file, i
287291
INTEGRITY_CHECK__PANIC_FL(file, line);
288292
POISON_CHECK__PANIC_FL(file, line);
289293
void* ret = UMM_CALLOC(count, size);
290-
PTR_CHECK__LOG_LAST_FAIL_FL(ret, count * size, file, line);
291-
OOM_CHECK__PRINT_LOC(ret, size, file, line);
294+
#if defined(DEBUG_ESP_OOM)
295+
size_t total_size = umm_umul_sat(count, size);
296+
#endif
297+
PTR_CHECK__LOG_LAST_FAIL_FL(ret, total_size, file, line);
298+
OOM_CHECK__PRINT_LOC(ret, total_size, file, line);
292299
return ret;
293300
}
294301

cores/esp8266/umm_malloc/umm_local.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void *umm_poison_realloc_fl(void *ptr, size_t size, const char *file, int line)
137137

138138
ptr = get_unpoisoned_check_neighbors(ptr, file, line);
139139

140-
size += poison_size(size);
140+
add_poison_size(&size);
141141
ret = umm_realloc(ptr, size);
142142

143143
ret = get_poisoned(ret, size);
@@ -296,4 +296,32 @@ size_t ICACHE_FLASH_ATTR umm_get_free_null_count(void) {
296296
}
297297
#endif // UMM_STATS_FULL
298298

299+
#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
300+
/*
301+
* Saturated unsigned add
302+
* Poison added to allocation size requires overflow protection.
303+
*/
304+
static size_t umm_uadd_sat(const size_t a, const size_t b) {
305+
size_t r = a + b;
306+
if (r < a) {
307+
return SIZE_MAX;
308+
}
309+
return r;
310+
}
311+
#endif
312+
313+
/*
314+
* Use platform-specific functions to protect against unsigned overflow/wrap by
315+
* implementing saturated unsigned multiply.
316+
* The function umm_calloc requires a saturated multiply function.
317+
*/
318+
size_t umm_umul_sat(const size_t a, const size_t b) {
319+
size_t r;
320+
if (__builtin_mul_overflow(a, b, &r)) {
321+
return SIZE_MAX;
322+
}
323+
return r;
324+
}
325+
326+
299327
#endif // BUILD_UMM_MALLOC_C

cores/esp8266/umm_malloc/umm_local.h

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
#define memset ets_memset
1616

1717

18+
/*
19+
* Saturated unsigned add and unsigned multiply
20+
*/
21+
size_t umm_umul_sat(const size_t a, const size_t b); // share with heap.cpp
22+
#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
23+
static size_t umm_uadd_sat(const size_t a, const size_t b);
24+
#endif
25+
1826
/*
1927
* This redefines DBGLOG_FORCE defined in dbglog/dbglog.h
2028
* Just for printing from umm_info() which is assumed to always be called from

cores/esp8266/umm_malloc/umm_malloc.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1214,10 +1214,14 @@ void *umm_realloc(void *ptr, size_t size) {
12141214
void *umm_calloc(size_t num, size_t item_size) {
12151215
void *ret;
12161216

1217-
ret = umm_malloc((size_t)(item_size * num));
1217+
// Use saturated multiply.
1218+
// Rely on umm_malloc to supply the fail response as needed.
1219+
size_t size = umm_umul_sat(num, item_size);
1220+
1221+
ret = umm_malloc(size);
12181222

12191223
if (ret) {
1220-
memset(ret, 0x00, (size_t)(item_size * num));
1224+
memset(ret, 0x00, size);
12211225
}
12221226

12231227
return ret;

cores/esp8266/umm_malloc/umm_poison.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
#include <stddef.h>
99
#include <stdbool.h>
1010

11+
#define UMM_POISON_BLOCK_SIZE (UMM_POISON_SIZE_BEFORE + sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_AFTER)
12+
1113
/*
12-
* Yields a size of the poison for the block of size `s`.
14+
* Yields the total size of a poison block of size `s`.
1315
* If `s` is 0, returns 0.
16+
* If result overflows/wraps, return saturation value.
1417
*/
15-
static size_t poison_size(size_t s) {
16-
return s ? (UMM_POISON_SIZE_BEFORE +
17-
sizeof(UMM_POISONED_BLOCK_LEN_TYPE) +
18-
UMM_POISON_SIZE_AFTER)
19-
: 0;
18+
static void add_poison_size(size_t* s) {
19+
if (*s == 0) {
20+
return;
21+
}
22+
23+
*s = umm_uadd_sat(*s, UMM_POISON_BLOCK_SIZE);
2024
}
2125

2226
/*
@@ -158,7 +162,7 @@ static void *get_unpoisoned(void *vptr) {
158162
void *umm_poison_malloc(size_t size) {
159163
void *ret;
160164

161-
size += poison_size(size);
165+
add_poison_size(&size);
162166

163167
ret = umm_malloc(size);
164168

@@ -171,9 +175,12 @@ void *umm_poison_malloc(size_t size) {
171175

172176
void *umm_poison_calloc(size_t num, size_t item_size) {
173177
void *ret;
174-
size_t size = item_size * num;
175178

176-
size += poison_size(size);
179+
// Use saturated multiply.
180+
// Rely on umm_malloc to supply the fail response as needed.
181+
size_t size = umm_umul_sat(num, item_size);
182+
183+
add_poison_size(&size);
177184

178185
ret = umm_malloc(size);
179186

@@ -193,7 +200,7 @@ void *umm_poison_realloc(void *ptr, size_t size) {
193200

194201
ptr = get_unpoisoned(ptr);
195202

196-
size += poison_size(size);
203+
add_poison_size(&size);
197204
ret = umm_realloc(ptr, size);
198205

199206
ret = get_poisoned(ret, size);

0 commit comments

Comments
 (0)