xref: /freebsd/tools/regression/redzone9/redzone.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/linker.h>
31 #include <sys/module.h>
32 #include <sys/malloc.h>
33 #include <sys/sysctl.h>
34 
35 
36 SYSCTL_NODE(_debug, OID_AUTO, redzone, CTLFLAG_RW, 0, "");
37 
38 #define	REDZONE_MALLOC_UNDERFLOW		0
39 #define	REDZONE_MALLOC_OVERFLOW			1
40 #define	REDZONE_REALLOC_SMALLER_UNDERFLOW	2
41 #define	REDZONE_REALLOC_SMALLER_OVERFLOW	3
42 #define	REDZONE_REALLOC_BIGGER_UNDERFLOW	4
43 #define	REDZONE_REALLOC_BIGGER_OVERFLOW		5
44 
45 static int
46 redzone_sysctl(SYSCTL_HANDLER_ARGS)
47 {
48 	u_char *p = NULL;
49 	int error, val = 0;
50 
51 	error = sysctl_handle_int(oidp, &val, sizeof(val), req);
52 	if (error != 0 || req->newptr == NULL)
53 		return (0);
54 	switch (arg2) {
55 	case REDZONE_MALLOC_UNDERFLOW:
56 		p = malloc(10, M_TEMP, M_NOWAIT);
57 		if (p == NULL)
58 			return (ENOMEM);
59 		p[-1] = '\0';
60 		break;
61 	case REDZONE_MALLOC_OVERFLOW:
62 		p = malloc(10, M_TEMP, M_NOWAIT);
63 		if (p == NULL)
64 			return (ENOMEM);
65 		p[10] = '\0';
66 		break;
67 	case REDZONE_REALLOC_SMALLER_UNDERFLOW:
68 		p = malloc(8192, M_TEMP, M_NOWAIT);
69 		if (p == NULL)
70 			return (ENOMEM);
71 		p[-1] = '\0';
72 		p = realloc(p, 10, M_TEMP, M_NOWAIT);
73 		if (p == NULL)
74 			return (ENOMEM);
75 		break;
76 	case REDZONE_REALLOC_SMALLER_OVERFLOW:
77 		p = malloc(8192, M_TEMP, M_NOWAIT);
78 		if (p == NULL)
79 			return (ENOMEM);
80 		p[8192] = '\0';
81 		p = realloc(p, 10, M_TEMP, M_NOWAIT);
82 		if (p == NULL)
83 			return (ENOMEM);
84 		break;
85 	case REDZONE_REALLOC_BIGGER_UNDERFLOW:
86 		p = malloc(10, M_TEMP, M_NOWAIT);
87 		if (p == NULL)
88 			return (ENOMEM);
89 		p[-1] = '\0';
90 		p = realloc(p, 8192, M_TEMP, M_NOWAIT);
91 		if (p == NULL)
92 			return (ENOMEM);
93 		break;
94 	case REDZONE_REALLOC_BIGGER_OVERFLOW:
95 		p = malloc(10, M_TEMP, M_NOWAIT);
96 		if (p == NULL)
97 			return (ENOMEM);
98 		p[10] = '\0';
99 		p = realloc(p, 8192, M_TEMP, M_NOWAIT);
100 		if (p == NULL)
101 			return (ENOMEM);
102 		break;
103 	}
104 	free(p, M_TEMP);
105         return (0);
106 }
107 SYSCTL_PROC(_debug_redzone, OID_AUTO, malloc_underflow, CTLTYPE_INT | CTLFLAG_RW,
108     NULL, REDZONE_MALLOC_UNDERFLOW, redzone_sysctl, "I", "");
109 SYSCTL_PROC(_debug_redzone, OID_AUTO, malloc_overflow, CTLTYPE_INT | CTLFLAG_RW,
110     NULL, REDZONE_MALLOC_OVERFLOW, redzone_sysctl, "I", "");
111 SYSCTL_PROC(_debug_redzone, OID_AUTO, realloc_smaller_underflow, CTLTYPE_INT | CTLFLAG_RW,
112     NULL, REDZONE_REALLOC_SMALLER_UNDERFLOW, redzone_sysctl, "I", "");
113 SYSCTL_PROC(_debug_redzone, OID_AUTO, realloc_smaller_overflow, CTLTYPE_INT | CTLFLAG_RW,
114     NULL, REDZONE_REALLOC_SMALLER_OVERFLOW, redzone_sysctl, "I", "");
115 SYSCTL_PROC(_debug_redzone, OID_AUTO, realloc_bigger_underflow, CTLTYPE_INT | CTLFLAG_RW,
116     NULL, REDZONE_REALLOC_BIGGER_UNDERFLOW, redzone_sysctl, "I", "");
117 SYSCTL_PROC(_debug_redzone, OID_AUTO, realloc_bigger_overflow, CTLTYPE_INT | CTLFLAG_RW,
118     NULL, REDZONE_REALLOC_BIGGER_OVERFLOW, redzone_sysctl, "I", "");
119 
120 static int
121 redzone_modevent(module_t mod, int type, void *data)
122 {
123 
124 	switch (type) {
125 	case MOD_LOAD:
126 	case MOD_UNLOAD:
127 		break;
128 	default:
129 		return (EOPNOTSUPP);
130 	}
131 	return (0);
132 }
133 static moduledata_t redzone_module = {
134 	"redzone",
135 	redzone_modevent,
136 	NULL
137 };
138 DECLARE_MODULE(redzone, redzone_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
139