2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 RCSID("$OpenBSD: monitor_mm.c,v 1.6 2002/06/04 23:05:49 markus Exp $");
29 #ifdef HAVE_SYS_MMAN_H
36 #include "monitor_mm.h"
39 mm_compare(struct mm_share *a, struct mm_share *b)
41 return ((char *)a->address - (char *)b->address);
44 RB_GENERATE(mmtree, mm_share, next, mm_compare)
46 static struct mm_share *
47 mm_make_entry(struct mm_master *mm, struct mmtree *head,
48 void *address, size_t size)
50 struct mm_share *tmp, *tmp2;
52 if (mm->mmalloc == NULL)
53 tmp = xmalloc(sizeof(struct mm_share));
55 tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share));
56 tmp->address = address;
59 tmp2 = RB_INSERT(mmtree, head, tmp);
61 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
62 mm, tmp2, address, (u_long)size);
67 /* Creates a shared memory area of a certain size */
70 mm_create(struct mm_master *mmalloc, size_t size)
76 mm = xmalloc(sizeof(struct mm_master));
78 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
81 * If the memory map has a mm_master it can be completely
82 * shared including authentication between the child
85 mm->mmalloc = mmalloc;
87 #if defined(HAVE_MMAP) && defined(MAP_ANON)
88 address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
90 if (address == MAP_FAILED)
91 fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
93 fatal("%s: UsePrivilegeSeparation=yes not supported",
97 mm->address = address;
100 RB_INIT(&mm->rb_free);
101 RB_INIT(&mm->rb_allocated);
103 mm_make_entry(mm, &mm->rb_free, address, size);
108 /* Frees either the allocated or the free list */
111 mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
113 struct mm_share *mms, *next;
115 for (mms = RB_ROOT(head); mms; mms = next) {
116 next = RB_NEXT(mmtree, head, mms);
117 RB_REMOVE(mmtree, head, mms);
121 mm_free(mmalloc, mms);
125 /* Destroys a memory mapped area */
128 mm_destroy(struct mm_master *mm)
130 mm_freelist(mm->mmalloc, &mm->rb_free);
131 mm_freelist(mm->mmalloc, &mm->rb_allocated);
134 if (munmap(mm->address, mm->size) == -1)
135 fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
138 fatal("%s: UsePrivilegeSeparation=yes not supported",
141 if (mm->mmalloc == NULL)
144 mm_free(mm->mmalloc, mm);
148 mm_xmalloc(struct mm_master *mm, size_t size)
152 address = mm_malloc(mm, size);
154 fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
159 /* Allocates data from a memory mapped area */
162 mm_malloc(struct mm_master *mm, size_t size)
164 struct mm_share *mms, *tmp;
167 fatal("mm_malloc: try to allocate 0 space");
169 size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE;
171 RB_FOREACH(mms, mmtree, &mm->rb_free) {
172 if (mms->size >= size)
180 memset(mms->address, 0xd0, size);
182 tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
184 /* Does not change order in RB tree */
186 mms->address = (u_char *)mms->address + size;
188 if (mms->size == 0) {
189 RB_REMOVE(mmtree, &mm->rb_free, mms);
190 if (mm->mmalloc == NULL)
193 mm_free(mm->mmalloc, mms);
196 return (tmp->address);
199 /* Frees memory in a memory mapped area */
202 mm_free(struct mm_master *mm, void *address)
204 struct mm_share *mms, *prev, tmp;
206 tmp.address = address;
207 mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
209 fatal("mm_free(%p): can not find %p", mm, address);
212 memset(mms->address, 0xd0, mms->size);
214 /* Remove from allocated list and insert in free list */
215 RB_REMOVE(mmtree, &mm->rb_allocated, mms);
216 if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
217 fatal("mm_free(%p): double address %p", mm, address);
219 /* Find previous entry */
221 if (RB_LEFT(prev, next)) {
222 prev = RB_LEFT(prev, next);
223 while (RB_RIGHT(prev, next))
224 prev = RB_RIGHT(prev, next);
226 if (RB_PARENT(prev, next) &&
227 (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
228 prev = RB_PARENT(prev, next);
230 while (RB_PARENT(prev, next) &&
231 (prev == RB_LEFT(RB_PARENT(prev, next), next)))
232 prev = RB_PARENT(prev, next);
233 prev = RB_PARENT(prev, next);
237 /* Check if range does not overlap */
238 if (prev != NULL && MM_ADDRESS_END(prev) > address)
239 fatal("mm_free: memory corruption: %p(%lu) > %p",
240 prev->address, (u_long)prev->size, address);
242 /* See if we can merge backwards */
243 if (prev != NULL && MM_ADDRESS_END(prev) == address) {
244 prev->size += mms->size;
245 RB_REMOVE(mmtree, &mm->rb_free, mms);
246 if (mm->mmalloc == NULL)
249 mm_free(mm->mmalloc, mms);
256 /* Check if we can merge forwards */
257 mms = RB_NEXT(mmtree, &mm->rb_free, prev);
261 if (MM_ADDRESS_END(prev) > mms->address)
262 fatal("mm_free: memory corruption: %p < %p(%lu)",
263 mms->address, prev->address, (u_long)prev->size);
264 if (MM_ADDRESS_END(prev) != mms->address)
267 prev->size += mms->size;
268 RB_REMOVE(mmtree, &mm->rb_free, mms);
270 if (mm->mmalloc == NULL)
273 mm_free(mm->mmalloc, mms);
277 mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
278 struct mm_master *mm, struct mm_master *mmold)
280 struct mm_master *mmalloc = mm->mmalloc;
281 struct mm_share *mms, *new;
284 RB_FOREACH(mms, mmtree, oldtree) {
285 /* Check the values */
286 mm_memvalid(mmold, mms, sizeof(struct mm_share));
287 mm_memvalid(mm, mms->address, mms->size);
289 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
290 memcpy(new, mms, sizeof(struct mm_share));
291 RB_INSERT(mmtree, newtree, new);
296 mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
298 struct mm_master *mm;
299 struct mm_master *mmalloc;
300 struct mm_master *mmold;
301 struct mmtree rb_free, rb_allocated;
303 debug3("%s: Share sync", __func__);
307 mm_memvalid(mmold, mm, sizeof(*mm));
309 mmalloc = mm_create(NULL, mm->size);
310 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
311 memcpy(mm, *pmm, sizeof(struct mm_master));
312 mm->mmalloc = mmalloc;
314 rb_free = mm->rb_free;
315 rb_allocated = mm->rb_allocated;
317 RB_INIT(&mm->rb_free);
318 RB_INIT(&mm->rb_allocated);
320 mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
321 mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
328 debug3("%s: Share sync end", __func__);
332 mm_memvalid(struct mm_master *mm, void *address, size_t size)
334 void *end = (u_char *)address + size;
336 if (address < mm->address)
337 fatal("mm_memvalid: address too small: %p", address);
339 fatal("mm_memvalid: end < address: %p < %p", end, address);
340 if (end > (void *)((u_char *)mm->address + mm->size))
341 fatal("mm_memvalid: address too large: %p", address);