]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/redzone.c
This commit was generated by cvs2svn to compensate for changes in r179404,
[FreeBSD/FreeBSD.git] / sys / vm / redzone.c
1 /*-
2  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/stack.h>
34 #include <sys/sysctl.h>
35
36 #include <vm/redzone.h>
37
38
39 SYSCTL_NODE(_vm, OID_AUTO, redzone, CTLFLAG_RW, NULL, "RedZone data");
40 static u_long redzone_extra_mem = 0;
41 SYSCTL_ULONG(_vm_redzone, OID_AUTO, extra_mem, CTLFLAG_RD, &redzone_extra_mem,
42     0, "Extra memory allocated by redzone");     
43 static int redzone_panic = 0;
44 TUNABLE_INT("vm.redzone.panic", &redzone_panic);
45 SYSCTL_INT(_vm_redzone, OID_AUTO, panic, CTLFLAG_RW, &redzone_panic, 0,
46     "Panic when buffer corruption is detected");     
47
48 #define REDZONE_CHSIZE  (16)
49 #define REDZONE_CFSIZE  (16)
50 #define REDZONE_HSIZE   (sizeof(struct stack) + sizeof(u_long) + REDZONE_CHSIZE)
51 #define REDZONE_FSIZE   (REDZONE_CFSIZE)
52
53 static u_long
54 redzone_roundup(u_long n)
55 {
56
57         if (n <= 128)
58                 return (128);
59         else if (n <= 256)
60                 return (256);
61         else if (n <= 512)
62                 return (512);
63         else if (n <= 1024)
64                 return (1024);
65         else if (n <= 2048)
66                 return (2048);
67         return (PAGE_SIZE);
68 }
69
70 u_long
71 redzone_get_size(caddr_t naddr)
72 {
73         u_long nsize;
74
75         bcopy(naddr - REDZONE_CHSIZE - sizeof(u_long), &nsize, sizeof(nsize));
76         return (nsize);
77 }
78
79 u_long
80 redzone_size_ntor(u_long nsize)
81 {
82
83         return (nsize + redzone_roundup(nsize) + REDZONE_FSIZE);
84 }
85
86 void *
87 redzone_addr_ntor(caddr_t naddr)
88 {
89
90         return (naddr - redzone_roundup(redzone_get_size(naddr)));
91 }
92
93 /*
94  * Set redzones and remember allocation backtrace.
95  */
96 void *
97 redzone_setup(caddr_t raddr, u_long nsize)
98 {
99         struct stack st;
100         caddr_t haddr, faddr;
101
102         atomic_add_long(&redzone_extra_mem, redzone_size_ntor(nsize) - nsize);
103
104         haddr = raddr + redzone_roundup(nsize) - REDZONE_HSIZE;
105         faddr = haddr + REDZONE_HSIZE + nsize;
106
107         /* Redzone header. */
108         stack_save(&st);
109         bcopy(&st, haddr, sizeof(st));
110         haddr += sizeof(st);
111         bcopy(&nsize, haddr, sizeof(nsize));
112         haddr += sizeof(nsize);
113         memset(haddr, 0x42, REDZONE_CHSIZE);
114         haddr += REDZONE_CHSIZE;
115
116         /* Redzone footer. */
117         memset(faddr, 0x42, REDZONE_CFSIZE);
118
119         return (haddr);
120 }
121
122 /*
123  * Verify redzones.
124  * This function is called on free() and realloc().
125  */
126 void
127 redzone_check(caddr_t naddr)
128 {
129         struct stack ast, fst;
130         caddr_t haddr, faddr;
131         u_int ncorruptions;
132         u_long nsize;
133         int i;
134
135         haddr = naddr - REDZONE_HSIZE;
136         bcopy(haddr, &ast, sizeof(ast));
137         haddr += sizeof(ast);
138         bcopy(haddr, &nsize, sizeof(nsize));
139         haddr += sizeof(nsize);
140
141         atomic_subtract_long(&redzone_extra_mem,
142             redzone_size_ntor(nsize) - nsize);
143
144         /* Look for buffer underflow. */
145         ncorruptions = 0;
146         for (i = 0; i < REDZONE_CHSIZE; i++, haddr++) {
147                 if (*(u_char *)haddr != 0x42)
148                         ncorruptions++;
149         }
150         if (ncorruptions > 0) {
151                 printf("REDZONE: Buffer underflow detected. %u byte%s "
152                     "corrupted before %p (%lu bytes allocated).\n",
153                     ncorruptions, ncorruptions == 1 ? "" : "s", naddr, nsize);
154                 printf("Allocation backtrace:\n");
155                 stack_print_ddb(&ast);
156                 printf("Free backtrace:\n");
157                 stack_save(&fst);
158                 stack_print_ddb(&fst);
159                 if (redzone_panic)
160                         panic("Stopping here.");
161         }
162         faddr = naddr + nsize;
163         /* Look for buffer overflow. */
164         ncorruptions = 0;
165         for (i = 0; i < REDZONE_CFSIZE; i++, faddr++) {
166                 if (*(u_char *)faddr != 0x42)
167                         ncorruptions++;
168         }
169         if (ncorruptions > 0) {
170                 printf("REDZONE: Buffer overflow detected. %u byte%s corrupted "
171                     "after %p (%lu bytes allocated).\n", ncorruptions,
172                     ncorruptions == 1 ? "" : "s", naddr + nsize, nsize);
173                 printf("Allocation backtrace:\n");
174                 stack_print_ddb(&ast);
175                 printf("Free backtrace:\n");
176                 stack_save(&fst);
177                 stack_print_ddb(&fst);
178                 if (redzone_panic)
179                         panic("Stopping here.");
180         }
181 }