1 /* $OpenBSD: subr_poison.c,v 1.6 2014/01/13 09:27:39 mpi Exp $ */ 2 /* 3 * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/param.h> 20 #include <sys/malloc.h> 21 #include <uvm/uvm_extern.h> 22 23 /* 24 * The POISON is used as known text to copy into free objects so 25 * that modifications after frees can be detected. 26 */ 27 #ifdef DEADBEEF0 28 #define POISON0 ((unsigned) DEADBEEF0) 29 #else 30 #define POISON0 ((unsigned) 0xdeadbeef) 31 #endif 32 #ifdef DEADBEEF1 33 #define POISON1 ((unsigned) DEADBEEF1) 34 #else 35 #define POISON1 ((unsigned) 0xdeafbead) 36 #endif 37 #define POISON_SIZE 64 38 39 int32_t 40 poison_value(void *v) 41 { 42 ulong l = (u_long)v; 43 44 l = l >> PAGE_SHIFT; 45 46 return (l & 1) ? POISON0 : POISON1; 47 } 48 49 void 50 poison_mem(void *v, size_t len) 51 { 52 uint32_t *ip = v; 53 size_t i; 54 int32_t poison; 55 56 poison = poison_value(v); 57 58 if (len > POISON_SIZE) 59 len = POISON_SIZE; 60 len = len / sizeof(*ip); 61 for (i = 0; i < len; i++) 62 ip[i] = poison; 63 } 64 65 int 66 poison_check(void *v, size_t len, size_t *pidx, int *pval) 67 { 68 uint32_t *ip = v; 69 size_t i; 70 int32_t poison; 71 72 poison = poison_value(v); 73 74 if (len > POISON_SIZE) 75 len = POISON_SIZE; 76 len = len / sizeof(*ip); 77 for (i = 0; i < len; i++) { 78 if (ip[i] != poison) { 79 *pidx = i; 80 *pval = poison; 81 return 1; 82 } 83 } 84 return 0; 85 } 86 87