2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/sysctl.h>
39 SYSCTL_NODE(_debug
, OID_AUTO
, redzone
, CTLFLAG_RW
, 0, "");
41 #define REDZONE_MALLOC_UNDERFLOW 0
42 #define REDZONE_MALLOC_OVERFLOW 1
43 #define REDZONE_REALLOC_SMALLER_UNDERFLOW 2
44 #define REDZONE_REALLOC_SMALLER_OVERFLOW 3
45 #define REDZONE_REALLOC_BIGGER_UNDERFLOW 4
46 #define REDZONE_REALLOC_BIGGER_OVERFLOW 5
49 redzone_sysctl(SYSCTL_HANDLER_ARGS
)
54 error
= sysctl_handle_int(oidp
, &val
, sizeof(val
), req
);
55 if (error
!= 0 || req
->newptr
== NULL
)
58 case REDZONE_MALLOC_UNDERFLOW
:
59 p
= malloc(10, M_TEMP
, M_NOWAIT
);
64 case REDZONE_MALLOC_OVERFLOW
:
65 p
= malloc(10, M_TEMP
, M_NOWAIT
);
70 case REDZONE_REALLOC_SMALLER_UNDERFLOW
:
71 p
= malloc(8192, M_TEMP
, M_NOWAIT
);
75 p
= realloc(p
, 10, M_TEMP
, M_NOWAIT
);
79 case REDZONE_REALLOC_SMALLER_OVERFLOW
:
80 p
= malloc(8192, M_TEMP
, M_NOWAIT
);
84 p
= realloc(p
, 10, M_TEMP
, M_NOWAIT
);
88 case REDZONE_REALLOC_BIGGER_UNDERFLOW
:
89 p
= malloc(10, M_TEMP
, M_NOWAIT
);
93 p
= realloc(p
, 8192, M_TEMP
, M_NOWAIT
);
97 case REDZONE_REALLOC_BIGGER_OVERFLOW
:
98 p
= malloc(10, M_TEMP
, M_NOWAIT
);
102 p
= realloc(p
, 8192, M_TEMP
, M_NOWAIT
);
110 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, malloc_underflow
, CTLTYPE_INT
| CTLFLAG_RW
,
111 NULL
, REDZONE_MALLOC_UNDERFLOW
, redzone_sysctl
, "I", "");
112 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, malloc_overflow
, CTLTYPE_INT
| CTLFLAG_RW
,
113 NULL
, REDZONE_MALLOC_OVERFLOW
, redzone_sysctl
, "I", "");
114 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, realloc_smaller_underflow
, CTLTYPE_INT
| CTLFLAG_RW
,
115 NULL
, REDZONE_REALLOC_SMALLER_UNDERFLOW
, redzone_sysctl
, "I", "");
116 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, realloc_smaller_overflow
, CTLTYPE_INT
| CTLFLAG_RW
,
117 NULL
, REDZONE_REALLOC_SMALLER_OVERFLOW
, redzone_sysctl
, "I", "");
118 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, realloc_bigger_underflow
, CTLTYPE_INT
| CTLFLAG_RW
,
119 NULL
, REDZONE_REALLOC_BIGGER_UNDERFLOW
, redzone_sysctl
, "I", "");
120 SYSCTL_PROC(_debug_redzone
, OID_AUTO
, realloc_bigger_overflow
, CTLTYPE_INT
| CTLFLAG_RW
,
121 NULL
, REDZONE_REALLOC_BIGGER_OVERFLOW
, redzone_sysctl
, "I", "");
124 redzone_modevent(module_t mod
, int type
, void *data
)
136 static moduledata_t redzone_module
= {
141 DECLARE_MODULE(redzone
, redzone_module
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
);