Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1 | /*- |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, 2010, 2011, 2013, 2014, 2016 |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 3 | * mirabilos <m@mirbsd.org> |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 4 | * |
| 5 | * Provided that these terms and disclaimer and all copyright notices |
| 6 | * are retained or reproduced in an accompanying document, permission |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 7 | * is granted to deal in this work without restriction, including un- |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 8 | * limited rights to use, publicly perform, distribute, sell, modify, |
| 9 | * merge, give away, or sublicence. |
| 10 | * |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 11 | * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 12 | * the utmost extent permitted by applicable law, neither express nor |
| 13 | * implied; without malicious intent or gross negligence. In no event |
| 14 | * may a licensor, author or contributor be held liable for indirect, |
| 15 | * direct, other damage, loss, or other issues arising in any way out |
| 16 | * of dealing in the work, even if advised of the possibility of such |
| 17 | * damage or existence of a defect, except proven that it results out |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 18 | * of said person's immediate fault when using the work as intended. |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "sh.h" |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 22 | #ifdef MKSH_ALLOC_CATCH_UNDERRUNS |
| 23 | #include <err.h> |
| 24 | #endif |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 26 | __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $"); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 27 | |
| 28 | /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */ |
| 29 | #if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 30 | #define remalloc(p,n) ((p) == NULL ? malloc_osi(n) : realloc_osi((p), (n))) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 31 | #else |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 32 | #define remalloc(p,n) realloc_osi((p), (n)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 36 | static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *); |
| 37 | |
| 38 | #ifndef MKSH_ALLOC_CATCH_UNDERRUNS |
| 39 | #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common)) |
| 40 | #else |
| 41 | #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095) |
| 42 | #undef remalloc |
| 43 | #undef free_osimalloc |
| 44 | |
| 45 | static void |
| 46 | free_osimalloc(void *ptr) |
| 47 | { |
| 48 | struct lalloc_item *lp = ptr; |
| 49 | |
| 50 | if (munmap(lp, lp->len)) |
| 51 | err(1, "free_osimalloc"); |
| 52 | } |
| 53 | |
| 54 | static void * |
| 55 | remalloc(void *ptr, size_t size) |
| 56 | { |
| 57 | struct lalloc_item *lp, *lold = ptr; |
| 58 | |
| 59 | size = (size + 4095) & ~(size_t)4095; |
| 60 | |
| 61 | if (lold && lold->len >= size) |
| 62 | return (ptr); |
| 63 | |
| 64 | if ((lp = mmap(NULL, size, PROT_READ | PROT_WRITE, |
| 65 | MAP_ANON | MAP_PRIVATE, -1, (off_t)0)) == MAP_FAILED) |
| 66 | err(1, "remalloc: mmap(%zu)", size); |
| 67 | if (ALLOC_ISUNALIGNED(lp)) |
| 68 | errx(1, "remalloc: unaligned(%p)", lp); |
| 69 | if (mprotect(((char *)lp) + 4096, 4096, PROT_NONE)) |
| 70 | err(1, "remalloc: mprotect"); |
| 71 | lp->len = size; |
| 72 | |
| 73 | if (lold) { |
| 74 | memcpy(((char *)lp) + 8192, ((char *)lold) + 8192, |
| 75 | lold->len - 8192); |
| 76 | if (munmap(lold, lold->len)) |
| 77 | err(1, "remalloc: munmap"); |
| 78 | } |
| 79 | |
| 80 | return (lp); |
| 81 | } |
| 82 | #endif |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 83 | |
| 84 | void |
| 85 | ainit(Area *ap) |
| 86 | { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 87 | #ifdef MKSH_ALLOC_CATCH_UNDERRUNS |
| 88 | if (sysconf(_SC_PAGESIZE) != 4096) { |
| 89 | fprintf(stderr, "mksh: fatal: pagesize %lu not 4096!\n", |
| 90 | sysconf(_SC_PAGESIZE)); |
| 91 | fflush(stderr); |
| 92 | abort(); |
| 93 | } |
| 94 | #endif |
| 95 | /* area pointer and items share struct lalloc_common */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 96 | ap->next = NULL; |
| 97 | } |
| 98 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 99 | static struct lalloc_common * |
| 100 | findptr(struct lalloc_common **lpp, char *ptr, Area *ap) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 101 | { |
| 102 | void *lp; |
| 103 | |
| 104 | #ifndef MKSH_SMALL |
| 105 | if (ALLOC_ISUNALIGNED(ptr)) |
| 106 | goto fail; |
| 107 | #endif |
| 108 | /* get address of ALLOC_ITEM from user item */ |
| 109 | /* |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 110 | * note: the alignment of "ptr" to ALLOC_ITEM is checked |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 111 | * above; the "void *" gets us rid of a gcc 2.95 warning |
| 112 | */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 113 | *lpp = (lp = ptr - sizeof(ALLOC_ITEM)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 114 | /* search for allocation item in group list */ |
| 115 | while (ap->next != lp) |
| 116 | if ((ap = ap->next) == NULL) { |
| 117 | #ifndef MKSH_SMALL |
| 118 | fail: |
| 119 | #endif |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 120 | #ifdef DEBUG |
| 121 | internal_warningf("rogue pointer %zX in ap %zX", |
| 122 | (size_t)ptr, (size_t)ap); |
| 123 | /* try to get a coredump */ |
| 124 | abort(); |
| 125 | #else |
| 126 | internal_errorf("rogue pointer %zX", (size_t)ptr); |
| 127 | #endif |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 128 | } |
| 129 | return (ap); |
| 130 | } |
| 131 | |
| 132 | void * |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 133 | aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap) |
| 134 | { |
| 135 | if (notoktomul(fac1, fac2)) |
| 136 | internal_errorf(Tintovfl, fac1, '*', fac2); |
| 137 | return (aresize(ptr, fac1 * fac2, ap)); |
| 138 | } |
| 139 | |
| 140 | void * |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 141 | aresize(void *ptr, size_t numb, Area *ap) |
| 142 | { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 143 | struct lalloc_common *lp = NULL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 144 | |
| 145 | /* resizing (true) or newly allocating? */ |
| 146 | if (ptr != NULL) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 147 | struct lalloc_common *pp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 148 | |
| 149 | pp = findptr(&lp, ptr, ap); |
| 150 | pp->next = lp->next; |
| 151 | } |
| 152 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 153 | if (notoktoadd(numb, sizeof(ALLOC_ITEM)) || |
| 154 | (lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 155 | #ifndef MKSH_SMALL |
| 156 | || ALLOC_ISUNALIGNED(lp) |
| 157 | #endif |
| 158 | ) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 159 | internal_errorf(Toomem, numb); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 160 | /* area pointer and items share struct lalloc_common */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 161 | lp->next = ap->next; |
| 162 | ap->next = lp; |
| 163 | /* return user item address */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 164 | return ((char *)lp + sizeof(ALLOC_ITEM)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | afree(void *ptr, Area *ap) |
| 169 | { |
| 170 | if (ptr != NULL) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 171 | struct lalloc_common *lp, *pp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 172 | |
| 173 | pp = findptr(&lp, ptr, ap); |
| 174 | /* unhook */ |
| 175 | pp->next = lp->next; |
| 176 | /* now free ALLOC_ITEM */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 177 | free_osimalloc(lp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | afreeall(Area *ap) |
| 183 | { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 184 | struct lalloc_common *lp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 185 | |
| 186 | /* traverse group (linked list) */ |
| 187 | while ((lp = ap->next) != NULL) { |
| 188 | /* make next ALLOC_ITEM head of list */ |
| 189 | ap->next = lp->next; |
| 190 | /* free old head */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 191 | free_osimalloc(lp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 192 | } |
| 193 | } |