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