]> andersk Git - openssh.git/blob - monitor_mm.c
- jmc@cvs.openbsd.org 2006/07/18 07:56:28
[openssh.git] / monitor_mm.c
1 /* $OpenBSD: monitor_mm.c,v 1.12 2006/07/11 20:07:25 stevesk Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #include <errno.h>
30
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34
35 #include "ssh.h"
36 #include "xmalloc.h"
37 #include "log.h"
38 #include "monitor_mm.h"
39
40 static int
41 mm_compare(struct mm_share *a, struct mm_share *b)
42 {
43         long diff = (char *)a->address - (char *)b->address;
44
45         if (diff == 0)
46                 return (0);
47         else if (diff < 0)
48                 return (-1);
49         else
50                 return (1);
51 }
52
53 RB_GENERATE(mmtree, mm_share, next, mm_compare)
54
55 static struct mm_share *
56 mm_make_entry(struct mm_master *mm, struct mmtree *head,
57     void *address, size_t size)
58 {
59         struct mm_share *tmp, *tmp2;
60
61         if (mm->mmalloc == NULL)
62                 tmp = xmalloc(sizeof(struct mm_share));
63         else
64                 tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share));
65         tmp->address = address;
66         tmp->size = size;
67
68         tmp2 = RB_INSERT(mmtree, head, tmp);
69         if (tmp2 != NULL)
70                 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
71                     mm, tmp2, address, (u_long)size);
72
73         return (tmp);
74 }
75
76 /* Creates a shared memory area of a certain size */
77
78 struct mm_master *
79 mm_create(struct mm_master *mmalloc, size_t size)
80 {
81         void *address;
82         struct mm_master *mm;
83
84         if (mmalloc == NULL)
85                 mm = xmalloc(sizeof(struct mm_master));
86         else
87                 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
88
89         /*
90          * If the memory map has a mm_master it can be completely
91          * shared including authentication between the child
92          * and the client.
93          */
94         mm->mmalloc = mmalloc;
95
96         address = xmmap(size);
97         if (address == (void *)MAP_FAILED)
98                 fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
99
100         mm->address = address;
101         mm->size = size;
102
103         RB_INIT(&mm->rb_free);
104         RB_INIT(&mm->rb_allocated);
105
106         mm_make_entry(mm, &mm->rb_free, address, size);
107
108         return (mm);
109 }
110
111 /* Frees either the allocated or the free list */
112
113 static void
114 mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
115 {
116         struct mm_share *mms, *next;
117
118         for (mms = RB_ROOT(head); mms; mms = next) {
119                 next = RB_NEXT(mmtree, head, mms);
120                 RB_REMOVE(mmtree, head, mms);
121                 if (mmalloc == NULL)
122                         xfree(mms);
123                 else
124                         mm_free(mmalloc, mms);
125         }
126 }
127
128 /* Destroys a memory mapped area */
129
130 void
131 mm_destroy(struct mm_master *mm)
132 {
133         mm_freelist(mm->mmalloc, &mm->rb_free);
134         mm_freelist(mm->mmalloc, &mm->rb_allocated);
135
136 #ifdef HAVE_MMAP
137         if (munmap(mm->address, mm->size) == -1)
138                 fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
139                     strerror(errno));
140 #else
141         fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
142             __func__);
143 #endif
144         if (mm->mmalloc == NULL)
145                 xfree(mm);
146         else
147                 mm_free(mm->mmalloc, mm);
148 }
149
150 void *
151 mm_xmalloc(struct mm_master *mm, size_t size)
152 {
153         void *address;
154
155         address = mm_malloc(mm, size);
156         if (address == NULL)
157                 fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
158         return (address);
159 }
160
161
162 /* Allocates data from a memory mapped area */
163
164 void *
165 mm_malloc(struct mm_master *mm, size_t size)
166 {
167         struct mm_share *mms, *tmp;
168
169         if (size == 0)
170                 fatal("mm_malloc: try to allocate 0 space");
171         if (size > SIZE_T_MAX - MM_MINSIZE + 1)
172                 fatal("mm_malloc: size too big");
173
174         size = ((size + (MM_MINSIZE - 1)) / MM_MINSIZE) * MM_MINSIZE;
175
176         RB_FOREACH(mms, mmtree, &mm->rb_free) {
177                 if (mms->size >= size)
178                         break;
179         }
180
181         if (mms == NULL)
182                 return (NULL);
183
184         /* Debug */
185         memset(mms->address, 0xd0, size);
186
187         tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
188
189         /* Does not change order in RB tree */
190         mms->size -= size;
191         mms->address = (u_char *)mms->address + size;
192
193         if (mms->size == 0) {
194                 RB_REMOVE(mmtree, &mm->rb_free, mms);
195                 if (mm->mmalloc == NULL)
196                         xfree(mms);
197                 else
198                         mm_free(mm->mmalloc, mms);
199         }
200
201         return (tmp->address);
202 }
203
204 /* Frees memory in a memory mapped area */
205
206 void
207 mm_free(struct mm_master *mm, void *address)
208 {
209         struct mm_share *mms, *prev, tmp;
210
211         tmp.address = address;
212         mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
213         if (mms == NULL)
214                 fatal("mm_free(%p): can not find %p", mm, address);
215
216         /* Debug */
217         memset(mms->address, 0xd0, mms->size);
218
219         /* Remove from allocated list and insert in free list */
220         RB_REMOVE(mmtree, &mm->rb_allocated, mms);
221         if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
222                 fatal("mm_free(%p): double address %p", mm, address);
223
224         /* Find previous entry */
225         prev = mms;
226         if (RB_LEFT(prev, next)) {
227                 prev = RB_LEFT(prev, next);
228                 while (RB_RIGHT(prev, next))
229                         prev = RB_RIGHT(prev, next);
230         } else {
231                 if (RB_PARENT(prev, next) &&
232                     (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
233                         prev = RB_PARENT(prev, next);
234                 else {
235                         while (RB_PARENT(prev, next) &&
236                             (prev == RB_LEFT(RB_PARENT(prev, next), next)))
237                                 prev = RB_PARENT(prev, next);
238                         prev = RB_PARENT(prev, next);
239                 }
240         }
241
242         /* Check if range does not overlap */
243         if (prev != NULL && MM_ADDRESS_END(prev) > address)
244                 fatal("mm_free: memory corruption: %p(%lu) > %p",
245                     prev->address, (u_long)prev->size, address);
246
247         /* See if we can merge backwards */
248         if (prev != NULL && MM_ADDRESS_END(prev) == address) {
249                 prev->size += mms->size;
250                 RB_REMOVE(mmtree, &mm->rb_free, mms);
251                 if (mm->mmalloc == NULL)
252                         xfree(mms);
253                 else
254                         mm_free(mm->mmalloc, mms);
255         } else
256                 prev = mms;
257
258         if (prev == NULL)
259                 return;
260
261         /* Check if we can merge forwards */
262         mms = RB_NEXT(mmtree, &mm->rb_free, prev);
263         if (mms == NULL)
264                 return;
265
266         if (MM_ADDRESS_END(prev) > mms->address)
267                 fatal("mm_free: memory corruption: %p < %p(%lu)",
268                     mms->address, prev->address, (u_long)prev->size);
269         if (MM_ADDRESS_END(prev) != mms->address)
270                 return;
271
272         prev->size += mms->size;
273         RB_REMOVE(mmtree, &mm->rb_free, mms);
274
275         if (mm->mmalloc == NULL)
276                 xfree(mms);
277         else
278                 mm_free(mm->mmalloc, mms);
279 }
280
281 static void
282 mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
283     struct mm_master *mm, struct mm_master *mmold)
284 {
285         struct mm_master *mmalloc = mm->mmalloc;
286         struct mm_share *mms, *new;
287
288         /* Sync free list */
289         RB_FOREACH(mms, mmtree, oldtree) {
290                 /* Check the values */
291                 mm_memvalid(mmold, mms, sizeof(struct mm_share));
292                 mm_memvalid(mm, mms->address, mms->size);
293
294                 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
295                 memcpy(new, mms, sizeof(struct mm_share));
296                 RB_INSERT(mmtree, newtree, new);
297         }
298 }
299
300 void
301 mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
302 {
303         struct mm_master *mm;
304         struct mm_master *mmalloc;
305         struct mm_master *mmold;
306         struct mmtree rb_free, rb_allocated;
307
308         debug3("%s: Share sync", __func__);
309
310         mm = *pmm;
311         mmold = mm->mmalloc;
312         mm_memvalid(mmold, mm, sizeof(*mm));
313
314         mmalloc = mm_create(NULL, mm->size);
315         mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
316         memcpy(mm, *pmm, sizeof(struct mm_master));
317         mm->mmalloc = mmalloc;
318
319         rb_free = mm->rb_free;
320         rb_allocated = mm->rb_allocated;
321
322         RB_INIT(&mm->rb_free);
323         RB_INIT(&mm->rb_allocated);
324
325         mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
326         mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
327
328         mm_destroy(mmold);
329
330         *pmm = mm;
331         *pmmalloc = mmalloc;
332
333         debug3("%s: Share sync end", __func__);
334 }
335
336 void
337 mm_memvalid(struct mm_master *mm, void *address, size_t size)
338 {
339         void *end = (u_char *)address + size;
340
341         if (address < mm->address)
342                 fatal("mm_memvalid: address too small: %p", address);
343         if (end < address)
344                 fatal("mm_memvalid: end < address: %p < %p", end, address);
345         if (end > (void *)((u_char *)mm->address + mm->size))
346                 fatal("mm_memvalid: address too large: %p", address);
347 }
This page took 0.101756 seconds and 5 git commands to generate.