Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit f11a677

Browse files
committed
Fix OOM-related regression in arena_tcache_fill_small().
Fix an OOM-related regression in arena_tcache_fill_small() that caused cache corruption that would almost certainly expose the application to undefined behavior, usually in the form of an allocation request returning an already-allocated region, or somewhat less likely, a freed region that had already been returned to the arena, thus making it available to the arena for any purpose. This regression was introduced by 9c43c13 (Reverse tcache fill order.), and was present in all releases from 2.2.0 through 3.6.0. This resolves jemalloc#98.
1 parent e9a3fa2 commit f11a677

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/arena.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,19 @@ arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin, size_t binind,
13301330
ptr = arena_run_reg_alloc(run, &arena_bin_info[binind]);
13311331
else
13321332
ptr = arena_bin_malloc_hard(arena, bin);
1333-
if (ptr == NULL)
1333+
if (ptr == NULL) {
1334+
/*
1335+
* OOM. tbin->avail isn't yet filled down to its first
1336+
* element, so the successful allocations (if any) must
1337+
* be moved to the base of tbin->avail before bailing
1338+
* out.
1339+
*/
1340+
if (i > 0) {
1341+
memmove(tbin->avail, &tbin->avail[nfill - i],
1342+
i * sizeof(void *));
1343+
}
13341344
break;
1345+
}
13351346
if (config_fill && unlikely(opt_junk)) {
13361347
arena_alloc_junk_small(ptr, &arena_bin_info[binind],
13371348
true);

0 commit comments

Comments
 (0)