xref: /xnu-10063.121.3/osfmk/kern/zalloc_bounds.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1 /*
2  * Copyright (c) 2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <kern/turnstile.h>
30 #include <kern/kalloc.h>
31 
32 ZONE_DECLARE_ID(ZONE_ID_TURNSTILE, struct turnstile);
33 
34 #if DEBUG || DEVELOPMENT
35 /*
36  * Make sure the various allocators work with bound checking.
37  */
38 extern void zalloc_bound_checks(void);
39 extern void kalloc_type_bound_checks(void);
40 extern void kalloc_data_bound_checks(void);
41 
42 void
zalloc_bound_checks(void)43 zalloc_bound_checks(void)
44 {
45 	struct turnstile *__single ts = zalloc_id(ZONE_ID_TURNSTILE, Z_WAITOK);
46 
47 	zfree_id(ZONE_ID_TURNSTILE, ts);
48 }
49 
50 void
kalloc_data_bound_checks(void)51 kalloc_data_bound_checks(void)
52 {
53 	int *__single i;
54 	int *__bidi_indexable a;
55 	void *__bidi_indexable d;
56 
57 	d = kalloc_data(10, Z_WAITOK);
58 	kfree_data(d, 10);
59 
60 	i = kalloc_type(int, Z_WAITOK);
61 	kfree_type(int, i);
62 
63 	a = kalloc_type(int, 10, Z_WAITOK);
64 	a = krealloc_type(int, 10, 20, a, Z_WAITOK | Z_REALLOCF);
65 	kfree_type(int, 20, a);
66 
67 	a = kalloc_type(int, int, 10, Z_WAITOK);
68 	a = krealloc_type(int, int, 10, 20, a, Z_WAITOK | Z_REALLOCF);
69 	kfree_type(int, int, 20, a);
70 }
71 
72 void
kalloc_type_bound_checks(void)73 kalloc_type_bound_checks(void)
74 {
75 	struct turnstile *__single ts;
76 	struct turnstile *__bidi_indexable ts_a;
77 
78 	ts = kalloc_type(struct turnstile, Z_WAITOK);
79 
80 	kfree_type(struct turnstile, ts);
81 
82 	ts_a = kalloc_type(struct turnstile, 10, Z_WAITOK);
83 
84 	ts_a = krealloc_type(struct turnstile, 10, 20,
85 	    ts_a, Z_WAITOK | Z_REALLOCF);
86 
87 	kfree_type(struct turnstile, 20, ts_a);
88 
89 	ts_a = kalloc_type(struct turnstile, struct turnstile, 10, Z_WAITOK);
90 
91 	ts_a = krealloc_type(struct turnstile, struct turnstile, 10, 20,
92 	    ts_a, Z_WAITOK | Z_REALLOCF);
93 
94 	kfree_type(struct turnstile, struct turnstile, 20, ts_a);
95 }
96 #endif /* DEBUG || DEVELOPMENT */
97