diff options
author | otto <> | 2018-01-26 19:14:51 +0000 |
---|---|---|
committer | otto <> | 2018-01-26 19:14:51 +0000 |
commit | 8df22ba873a3a23abdc070f97ee5a5d08cbbb8db (patch) | |
tree | 54bc6c8fed70f35770856fb2bc23207073a53b40 | |
parent | ffa49cfcc71c84fb877cf150d758ca8420dcb2d2 (diff) | |
download | openbsd-8df22ba873a3a23abdc070f97ee5a5d08cbbb8db.tar.gz openbsd-8df22ba873a3a23abdc070f97ee5a5d08cbbb8db.tar.bz2 openbsd-8df22ba873a3a23abdc070f97ee5a5d08cbbb8db.zip |
- do not junk pages returned by free_bytes(), all freed chunks are already
junked
- freezero(): only clear requested size
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 97d77b0a63..8778bf1410 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.241 2018/01/18 20:06:16 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.242 2018/01/26 19:14:51 otto Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -633,7 +633,7 @@ delete(struct dir_info *d, struct region_info *ri) | |||
633 | * cache are in MALLOC_PAGESIZE units. | 633 | * cache are in MALLOC_PAGESIZE units. |
634 | */ | 634 | */ |
635 | static void | 635 | static void |
636 | unmap(struct dir_info *d, void *p, size_t sz, int clear) | 636 | unmap(struct dir_info *d, void *p, size_t sz, size_t clear, int junk) |
637 | { | 637 | { |
638 | size_t psz = sz >> MALLOC_PAGESHIFT; | 638 | size_t psz = sz >> MALLOC_PAGESHIFT; |
639 | size_t rsz; | 639 | size_t rsz; |
@@ -650,7 +650,7 @@ unmap(struct dir_info *d, void *p, size_t sz, int clear) | |||
650 | * to unmap is larger than the cache size or we're clearing and the | 650 | * to unmap is larger than the cache size or we're clearing and the |
651 | * cache is full, just munmap | 651 | * cache is full, just munmap |
652 | */ | 652 | */ |
653 | if (psz > mopts.malloc_cache || (clear && rsz == 0)) { | 653 | if (psz > mopts.malloc_cache || (clear > 0 && rsz == 0)) { |
654 | i = munmap(p, sz); | 654 | i = munmap(p, sz); |
655 | if (i) | 655 | if (i) |
656 | wrterror(d, "munmap %p", p); | 656 | wrterror(d, "munmap %p", p); |
@@ -686,11 +686,10 @@ unmap(struct dir_info *d, void *p, size_t sz, int clear) | |||
686 | for (i = 0; ; i++) { | 686 | for (i = 0; ; i++) { |
687 | r = &d->free_regions[(i + offset) & mask]; | 687 | r = &d->free_regions[(i + offset) & mask]; |
688 | if (r->p == NULL) { | 688 | if (r->p == NULL) { |
689 | if (clear) | 689 | if (clear > 0) |
690 | memset(p, 0, sz - mopts.malloc_guard); | 690 | memset(p, 0, clear); |
691 | if (mopts.malloc_junk && !mopts.malloc_freeunmap) { | 691 | if (junk && !mopts.malloc_freeunmap) { |
692 | size_t amt = mopts.malloc_junk == 1 ? | 692 | size_t amt = junk == 1 ? MALLOC_MAXCHUNK : sz; |
693 | MALLOC_MAXCHUNK : sz; | ||
694 | memset(p, SOME_FREEJUNK, amt); | 693 | memset(p, SOME_FREEJUNK, amt); |
695 | } | 694 | } |
696 | if (mopts.malloc_freeunmap) | 695 | if (mopts.malloc_freeunmap) |
@@ -888,7 +887,7 @@ omalloc_make_chunks(struct dir_info *d, int bits, int listnum) | |||
888 | return bp; | 887 | return bp; |
889 | 888 | ||
890 | err: | 889 | err: |
891 | unmap(d, pp, MALLOC_PAGESIZE, 0); | 890 | unmap(d, pp, MALLOC_PAGESIZE, 0, mopts.malloc_junk); |
892 | return NULL; | 891 | return NULL; |
893 | } | 892 | } |
894 | 893 | ||
@@ -1087,7 +1086,7 @@ free_bytes(struct dir_info *d, struct region_info *r, void *ptr) | |||
1087 | 1086 | ||
1088 | if (info->size == 0 && !mopts.malloc_freeunmap) | 1087 | if (info->size == 0 && !mopts.malloc_freeunmap) |
1089 | mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE); | 1088 | mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE); |
1090 | unmap(d, info->page, MALLOC_PAGESIZE, 0); | 1089 | unmap(d, info->page, MALLOC_PAGESIZE, 0, 0); |
1091 | 1090 | ||
1092 | delete(d, r); | 1091 | delete(d, r); |
1093 | if (info->size != 0) | 1092 | if (info->size != 0) |
@@ -1118,7 +1117,7 @@ omalloc(struct dir_info *pool, size_t sz, int zero_fill, void *f) | |||
1118 | return NULL; | 1117 | return NULL; |
1119 | } | 1118 | } |
1120 | if (insert(pool, p, sz, f)) { | 1119 | if (insert(pool, p, sz, f)) { |
1121 | unmap(pool, p, psz, 0); | 1120 | unmap(pool, p, psz, 0, 0); |
1122 | errno = ENOMEM; | 1121 | errno = ENOMEM; |
1123 | return NULL; | 1122 | return NULL; |
1124 | } | 1123 | } |
@@ -1309,8 +1308,7 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1309 | uint32_t chunknum = | 1308 | uint32_t chunknum = |
1310 | find_chunknum(pool, info, p, 0); | 1309 | find_chunknum(pool, info, p, 0); |
1311 | 1310 | ||
1312 | if (info->bits[info->offset + chunknum] < | 1311 | if (info->bits[info->offset + chunknum] < argsz) |
1313 | argsz) | ||
1314 | wrterror(pool, "recorded size %hu" | 1312 | wrterror(pool, "recorded size %hu" |
1315 | " < %zu", | 1313 | " < %zu", |
1316 | info->bits[info->offset + chunknum], | 1314 | info->bits[info->offset + chunknum], |
@@ -1350,7 +1348,8 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1350 | } | 1348 | } |
1351 | STATS_SUB(pool->malloc_guarded, mopts.malloc_guard); | 1349 | STATS_SUB(pool->malloc_guarded, mopts.malloc_guard); |
1352 | } | 1350 | } |
1353 | unmap(pool, p, PAGEROUND(sz), clear); | 1351 | unmap(pool, p, PAGEROUND(sz), clear ? argsz : 0, |
1352 | mopts.malloc_junk); | ||
1354 | delete(pool, r); | 1353 | delete(pool, r); |
1355 | } else { | 1354 | } else { |
1356 | /* Validate and optionally canary check */ | 1355 | /* Validate and optionally canary check */ |
@@ -1376,8 +1375,8 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1376 | pool->delayed_chunks[i] = tmp; | 1375 | pool->delayed_chunks[i] = tmp; |
1377 | if (mopts.malloc_junk) | 1376 | if (mopts.malloc_junk) |
1378 | validate_junk(pool, p); | 1377 | validate_junk(pool, p); |
1379 | } else if (sz > 0) | 1378 | } else if (argsz > 0) |
1380 | memset(p, 0, sz); | 1379 | memset(p, 0, argsz); |
1381 | if (p != NULL) { | 1380 | if (p != NULL) { |
1382 | r = find(pool, p); | 1381 | r = find(pool, p); |
1383 | if (r == NULL) | 1382 | if (r == NULL) |
@@ -1575,7 +1574,8 @@ gotit: | |||
1575 | PROT_NONE)) | 1574 | PROT_NONE)) |
1576 | wrterror(pool, "mprotect"); | 1575 | wrterror(pool, "mprotect"); |
1577 | } | 1576 | } |
1578 | unmap(pool, (char *)r->p + rnewsz, roldsz - rnewsz, 0); | 1577 | unmap(pool, (char *)r->p + rnewsz, roldsz - rnewsz, 0, |
1578 | mopts.malloc_junk); | ||
1579 | r->size = gnewsz; | 1579 | r->size = gnewsz; |
1580 | if (MALLOC_MOVE_COND(gnewsz)) { | 1580 | if (MALLOC_MOVE_COND(gnewsz)) { |
1581 | void *pp = MALLOC_MOVE(r->p, gnewsz); | 1581 | void *pp = MALLOC_MOVE(r->p, gnewsz); |
@@ -1791,7 +1791,7 @@ orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | |||
1791 | } else | 1791 | } else |
1792 | memcpy(newptr, p, newsize); | 1792 | memcpy(newptr, p, newsize); |
1793 | 1793 | ||
1794 | ofree(pool, p, 1, 0, 0); | 1794 | ofree(pool, p, 1, 0, oldsize); |
1795 | 1795 | ||
1796 | done: | 1796 | done: |
1797 | if (argpool != pool) { | 1797 | if (argpool != pool) { |
@@ -1984,7 +1984,7 @@ omemalign(struct dir_info *pool, size_t alignment, size_t sz, int zero_fill, | |||
1984 | } | 1984 | } |
1985 | 1985 | ||
1986 | if (insert(pool, p, sz, f)) { | 1986 | if (insert(pool, p, sz, f)) { |
1987 | unmap(pool, p, psz, 0); | 1987 | unmap(pool, p, psz, 0, 0); |
1988 | errno = ENOMEM; | 1988 | errno = ENOMEM; |
1989 | return NULL; | 1989 | return NULL; |
1990 | } | 1990 | } |