1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org> 5 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * uma_dbg.c Debugging features for UMA users 32 * 33 */ 34 35 #include <sys/cdefs.h> 36 #include "opt_vm.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bitset.h> 41 #include <sys/kernel.h> 42 #include <sys/types.h> 43 #include <sys/queue.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/malloc.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_object.h> 50 #include <vm/vm_page.h> 51 #include <vm/uma.h> 52 #include <vm/uma_int.h> 53 #include <vm/uma_dbg.h> 54 #include <vm/memguard.h> 55 56 static const uint32_t uma_junk = 0xdeadc0de; 57 58 /* 59 * Checks an item to make sure it hasn't been overwritten since it was freed, 60 * prior to subsequent reallocation. 61 * 62 * Complies with standard ctor arg/return 63 */ 64 int 65 trash_ctor(void *mem, int size, void *arg, int flags) 66 { 67 int cnt; 68 uint32_t *p; 69 70 #ifdef DEBUG_MEMGUARD 71 if (is_memguard_addr(mem)) 72 return (0); 73 #endif 74 75 cnt = size / sizeof(uma_junk); 76 77 for (p = mem; cnt > 0; cnt--, p++) 78 if (*p != uma_junk) { 79 #ifdef INVARIANTS 80 panic("Memory modified after free %p(%d) val=%x @ %p\n", 81 mem, size, *p, p); 82 #else 83 printf("Memory modified after free %p(%d) val=%x @ %p\n", 84 mem, size, *p, p); 85 #endif 86 return (0); 87 } 88 return (0); 89 } 90 91 /* 92 * Fills an item with predictable garbage 93 * 94 * Complies with standard dtor arg/return 95 * 96 */ 97 void 98 trash_dtor(void *mem, int size, void *arg) 99 { 100 int cnt; 101 uint32_t *p; 102 103 #ifdef DEBUG_MEMGUARD 104 if (is_memguard_addr(mem)) 105 return; 106 #endif 107 108 cnt = size / sizeof(uma_junk); 109 110 for (p = mem; cnt > 0; cnt--, p++) 111 *p = uma_junk; 112 } 113 114 /* 115 * Fills an item with predictable garbage 116 * 117 * Complies with standard init arg/return 118 * 119 */ 120 int 121 trash_init(void *mem, int size, int flags) 122 { 123 trash_dtor(mem, size, NULL); 124 return (0); 125 } 126 127 /* 128 * Checks an item to make sure it hasn't been overwritten since it was freed. 129 * 130 * Complies with standard fini arg/return 131 * 132 */ 133 void 134 trash_fini(void *mem, int size) 135 { 136 (void)trash_ctor(mem, size, NULL, 0); 137 } 138 139 int 140 mtrash_ctor(void *mem, int size, void *arg, int flags) 141 { 142 struct malloc_type **ksp; 143 uint32_t *p = mem; 144 int cnt; 145 146 #ifdef DEBUG_MEMGUARD 147 if (is_memguard_addr(mem)) 148 return (0); 149 #endif 150 151 size -= sizeof(struct malloc_type *); 152 ksp = (struct malloc_type **)mem; 153 ksp += size / sizeof(struct malloc_type *); 154 cnt = size / sizeof(uma_junk); 155 156 for (p = mem; cnt > 0; cnt--, p++) 157 if (*p != uma_junk) { 158 printf("Memory modified after free %p(%d) val=%x @ %p\n", 159 mem, size, *p, p); 160 panic("Most recently used by %s\n", (*ksp == NULL)? 161 "none" : (*ksp)->ks_shortdesc); 162 } 163 return (0); 164 } 165 166 /* 167 * Fills an item with predictable garbage 168 * 169 * Complies with standard dtor arg/return 170 * 171 */ 172 void 173 mtrash_dtor(void *mem, int size, void *arg) 174 { 175 int cnt; 176 uint32_t *p; 177 178 #ifdef DEBUG_MEMGUARD 179 if (is_memguard_addr(mem)) 180 return; 181 #endif 182 183 size -= sizeof(struct malloc_type *); 184 cnt = size / sizeof(uma_junk); 185 186 for (p = mem; cnt > 0; cnt--, p++) 187 *p = uma_junk; 188 } 189 190 /* 191 * Fills an item with predictable garbage 192 * 193 * Complies with standard init arg/return 194 * 195 */ 196 int 197 mtrash_init(void *mem, int size, int flags) 198 { 199 struct malloc_type **ksp; 200 201 #ifdef DEBUG_MEMGUARD 202 if (is_memguard_addr(mem)) 203 return (0); 204 #endif 205 206 mtrash_dtor(mem, size, NULL); 207 208 ksp = (struct malloc_type **)mem; 209 ksp += (size / sizeof(struct malloc_type *)) - 1; 210 *ksp = NULL; 211 return (0); 212 } 213 214 /* 215 * Checks an item to make sure it hasn't been overwritten since it was freed, 216 * prior to freeing it back to available memory. 217 * 218 * Complies with standard fini arg/return 219 * 220 */ 221 void 222 mtrash_fini(void *mem, int size) 223 { 224 (void)mtrash_ctor(mem, size, NULL, 0); 225 } 226