xref: /dragonfly/sys/dev/misc/tbridge/safe_mem.c (revision 648f3ded)
1a563ca70SAlex Hornung /*
2a563ca70SAlex Hornung  * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
3a563ca70SAlex Hornung  * All rights reserved.
4a563ca70SAlex Hornung  *
5a563ca70SAlex Hornung  * Redistribution and use in source and binary forms, with or without
6a563ca70SAlex Hornung  * modification, are permitted provided that the following conditions
7a563ca70SAlex Hornung  * are met:
8a563ca70SAlex Hornung  *
9a563ca70SAlex Hornung  * 1. Redistributions of source code must retain the above copyright
10a563ca70SAlex Hornung  *    notice, this list of conditions and the following disclaimer.
11a563ca70SAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
12a563ca70SAlex Hornung  *    notice, this list of conditions and the following disclaimer in
13a563ca70SAlex Hornung  *    the documentation and/or other materials provided with the
14a563ca70SAlex Hornung  *    distribution.
15a563ca70SAlex Hornung  *
16a563ca70SAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a563ca70SAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a563ca70SAlex Hornung  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19a563ca70SAlex Hornung  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20a563ca70SAlex Hornung  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21a563ca70SAlex Hornung  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22a563ca70SAlex Hornung  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23a563ca70SAlex Hornung  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24a563ca70SAlex Hornung  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25a563ca70SAlex Hornung  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26a563ca70SAlex Hornung  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a563ca70SAlex Hornung  * SUCH DAMAGE.
28a563ca70SAlex Hornung  */
29a563ca70SAlex Hornung 
30a563ca70SAlex Hornung #include <sys/types.h>
31a563ca70SAlex Hornung #include <sys/param.h>
32a563ca70SAlex Hornung #include <sys/systm.h>
33a563ca70SAlex Hornung #include <sys/kernel.h>
34a563ca70SAlex Hornung #include <sys/malloc.h>
35a563ca70SAlex Hornung #include <sys/libkern.h>
36a563ca70SAlex Hornung 
37a563ca70SAlex Hornung #include <sys/tbridge.h>
38a563ca70SAlex Hornung 
39a563ca70SAlex Hornung MALLOC_DEFINE(M_SAFEMEM, "safemem", "safemem kernel port");
40a563ca70SAlex Hornung 
41a563ca70SAlex Hornung struct safe_mem_hdr {
42a563ca70SAlex Hornung 	struct safe_mem_hdr	*prev;
43a563ca70SAlex Hornung 	struct safe_mem_hdr	*next;
44a563ca70SAlex Hornung 	struct safe_mem_tail	*tail;
45a563ca70SAlex Hornung 	const char	*file;
46a563ca70SAlex Hornung 	int		line;
47a563ca70SAlex Hornung 	size_t		alloc_sz;
48a563ca70SAlex Hornung 	char		sig[8]; /* SAFEMEM */
49a563ca70SAlex Hornung };
50a563ca70SAlex Hornung 
51a563ca70SAlex Hornung struct safe_mem_tail {
52a563ca70SAlex Hornung 	char sig[8]; /* SAFEMEM */
53a563ca70SAlex Hornung };
54a563ca70SAlex Hornung 
55a563ca70SAlex Hornung static struct safe_mem_hdr *safe_mem_hdr_first = NULL;
56a563ca70SAlex Hornung 
57a563ca70SAlex Hornung static int safemem_error_count = 0;
58a563ca70SAlex Hornung 
59a563ca70SAlex Hornung void
safemem_reset_error_count(void)60a563ca70SAlex Hornung safemem_reset_error_count(void)
61a563ca70SAlex Hornung {
62a563ca70SAlex Hornung 	safemem_error_count = 0;
63a563ca70SAlex Hornung }
64a563ca70SAlex Hornung 
65a563ca70SAlex Hornung int
safemem_get_error_count(void)66a563ca70SAlex Hornung safemem_get_error_count(void)
67a563ca70SAlex Hornung {
68a563ca70SAlex Hornung 	return safemem_error_count;
69a563ca70SAlex Hornung }
70a563ca70SAlex Hornung 
71a563ca70SAlex Hornung void *
_alloc_safe_mem(size_t req_sz,const char * file,int line)72a563ca70SAlex Hornung _alloc_safe_mem(size_t req_sz, const char *file, int line)
73a563ca70SAlex Hornung {
74a563ca70SAlex Hornung 	struct safe_mem_hdr *hdr, *hdrp;
75a563ca70SAlex Hornung 	struct safe_mem_tail *tail;
76a563ca70SAlex Hornung 	size_t alloc_sz;
77a563ca70SAlex Hornung 	char *mem, *user_mem;
78a563ca70SAlex Hornung 
79a563ca70SAlex Hornung 	/* only shift, to make sure things (TM) keep aligned */
80a563ca70SAlex Hornung 	alloc_sz = req_sz;
81*648f3dedSSascha Wildner 	mem = kmalloc(alloc_sz << 2, M_SAFEMEM, M_WAITOK | M_ZERO);
82a563ca70SAlex Hornung 	user_mem = mem + alloc_sz;
83a563ca70SAlex Hornung 	hdr = (struct safe_mem_hdr *)(user_mem - sizeof(*hdr));
84a563ca70SAlex Hornung 	tail = (struct safe_mem_tail *)(user_mem + alloc_sz);
85a563ca70SAlex Hornung 
86a563ca70SAlex Hornung 	strcpy(hdr->sig, "SAFEMEM");
87a563ca70SAlex Hornung 	strcpy(tail->sig, "SAFEMEM");
88a563ca70SAlex Hornung 	hdr->tail = tail;
89a563ca70SAlex Hornung 	hdr->alloc_sz = alloc_sz;
90a563ca70SAlex Hornung 	hdr->file = file;
91a563ca70SAlex Hornung 	hdr->line = line;
92a563ca70SAlex Hornung 	hdr->next = NULL;
93a563ca70SAlex Hornung 
94a563ca70SAlex Hornung 	if (safe_mem_hdr_first == NULL) {
95a563ca70SAlex Hornung 		safe_mem_hdr_first = hdr;
96a563ca70SAlex Hornung 	} else {
97a563ca70SAlex Hornung 		hdrp = safe_mem_hdr_first;
98a563ca70SAlex Hornung 		while (hdrp->next != NULL)
99a563ca70SAlex Hornung 			hdrp = hdrp->next;
100a563ca70SAlex Hornung 		hdr->prev = hdrp;
101a563ca70SAlex Hornung 		hdrp->next = hdr;
102a563ca70SAlex Hornung 	}
103a563ca70SAlex Hornung 
104a563ca70SAlex Hornung 	return user_mem;
105a563ca70SAlex Hornung }
106a563ca70SAlex Hornung 
107a563ca70SAlex Hornung void
_free_safe_mem(void * mem_ptr,const char * file,int line)108a563ca70SAlex Hornung _free_safe_mem(void *mem_ptr, const char *file, int line)
109a563ca70SAlex Hornung {
110a563ca70SAlex Hornung 	struct safe_mem_hdr *hdr;
111a563ca70SAlex Hornung 	struct safe_mem_tail *tail;
112a563ca70SAlex Hornung 	size_t alloc_sz;
113a563ca70SAlex Hornung 	char *mem = mem_ptr;
114a563ca70SAlex Hornung 	char *user_mem = mem_ptr;
115a563ca70SAlex Hornung 
116a563ca70SAlex Hornung 	hdr = (struct safe_mem_hdr *)(user_mem - sizeof(*hdr));
117a563ca70SAlex Hornung 	tail = (struct safe_mem_tail *)(user_mem + hdr->alloc_sz);
118a563ca70SAlex Hornung 	mem -= hdr->alloc_sz;
119a563ca70SAlex Hornung 
120a563ca70SAlex Hornung #ifdef DEBUG
121a563ca70SAlex Hornung 	kprintf("freeing safe_mem (hdr): %#lx (%s:%d)\n",
122a563ca70SAlex Hornung 		    (unsigned long)(void *)hdr, hdr->file, hdr->line);
123a563ca70SAlex Hornung #endif
124a563ca70SAlex Hornung 
125a563ca70SAlex Hornung 	if (hdr->alloc_sz == 0) {
126a563ca70SAlex Hornung 		tbridge_printf("SAFEMEM BUG: double-free at %s:%d !!!\n", file,
127a563ca70SAlex Hornung 		    line);
128a563ca70SAlex Hornung 		return;
129a563ca70SAlex Hornung 	}
130a563ca70SAlex Hornung 
131a563ca70SAlex Hornung 	/* Integrity checks */
132a563ca70SAlex Hornung 	if ((memcmp(hdr->sig, "SAFEMEM\0", 8) != 0) ||
133a563ca70SAlex Hornung 	    (memcmp(tail->sig, "SAFEMEM\0", 8) != 0)) {
134a563ca70SAlex Hornung 		tbridge_printf("SAFEMEM BUG: safe_mem buffer under- or overflow "
135a563ca70SAlex Hornung 		    "at %s:%d !!!\n", file, line);
136a563ca70SAlex Hornung 		return;
137a563ca70SAlex Hornung 	}
138a563ca70SAlex Hornung 
139a563ca70SAlex Hornung 	if (safe_mem_hdr_first == NULL) {
140a563ca70SAlex Hornung 		tbridge_printf("SAFEMEM BUG: safe_mem list should not be empty "
141a563ca70SAlex Hornung 		    "at %s:%d !!!\n", file, line);
142a563ca70SAlex Hornung 		return;
143a563ca70SAlex Hornung 	}
144a563ca70SAlex Hornung 
145a563ca70SAlex Hornung 	if (hdr->prev != NULL)
146a563ca70SAlex Hornung 		hdr->prev->next = hdr->next;
147a563ca70SAlex Hornung 	if (hdr->next != NULL)
148a563ca70SAlex Hornung 		hdr->next->prev = hdr->prev;
149a563ca70SAlex Hornung 	if (safe_mem_hdr_first == hdr)
150a563ca70SAlex Hornung 		safe_mem_hdr_first = hdr->next;
151a563ca70SAlex Hornung 
152a563ca70SAlex Hornung 	alloc_sz = hdr->alloc_sz;
153a563ca70SAlex Hornung 
154a563ca70SAlex Hornung 	bzero(mem, alloc_sz << 2);
155a563ca70SAlex Hornung 	kfree(mem, M_SAFEMEM);
156a563ca70SAlex Hornung }
157a563ca70SAlex Hornung 
158a563ca70SAlex Hornung void
check_and_purge_safe_mem(void)159a563ca70SAlex Hornung check_and_purge_safe_mem(void)
160a563ca70SAlex Hornung {
161a563ca70SAlex Hornung 	struct safe_mem_hdr *hdr;
162a563ca70SAlex Hornung 	char *mem;
163a563ca70SAlex Hornung #ifdef DEBUG
164a563ca70SAlex Hornung 	int ok;
165a563ca70SAlex Hornung #endif
166a563ca70SAlex Hornung 
167a563ca70SAlex Hornung 	if (safe_mem_hdr_first == NULL)
168a563ca70SAlex Hornung 		return;
169a563ca70SAlex Hornung 
170a563ca70SAlex Hornung 	hdr = safe_mem_hdr_first;
171a563ca70SAlex Hornung 	while ((hdr = safe_mem_hdr_first) != NULL) {
172a563ca70SAlex Hornung #ifdef DEBUG
173a563ca70SAlex Hornung 		if ((hdr->alloc_sz > 0) &&
174a563ca70SAlex Hornung 		    (memcmp(hdr->sig, "SAFEMEM\0", 8) == 0) &&
175a563ca70SAlex Hornung 		    (memcmp(hdr->tail->sig, "SAFEMEM\0", 8) == 0))
176a563ca70SAlex Hornung 			ok = 1;
177a563ca70SAlex Hornung 		else
178a563ca70SAlex Hornung 			ok = 0;
179a563ca70SAlex Hornung 
180a563ca70SAlex Hornung 		kprintf("SAFEMEM: un-freed safe_mem: %#lx (%s:%d) [integrity=%s]\n",
181a563ca70SAlex Hornung 		    (unsigned long)(void *)hdr, hdr->file, hdr->line,
182a563ca70SAlex Hornung 		    ok? "ok" : "failed");
183a563ca70SAlex Hornung #endif
184a563ca70SAlex Hornung 		mem = (void *)hdr;
185a563ca70SAlex Hornung 		mem += sizeof(*hdr);
186a563ca70SAlex Hornung 		_free_safe_mem(mem, "check_and_purge_safe_mem", 0);
187a563ca70SAlex Hornung 	}
188a563ca70SAlex Hornung }
189