xref: /xnu-12377.81.4/tests/vm/vm_mte_reclamation.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2025 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 <darwintest.h>
30 #include <darwintest_utils.h>
31 
32 #include <sys/sysctl.h>
33 
34 #include "test_utils.h"
35 
36 T_GLOBAL_META(
37 	T_META_NAMESPACE("xnu.vm.mteinfo"),
38 	T_META_RADAR_COMPONENT_NAME("xnu"),
39 	T_META_RADAR_COMPONENT_VERSION("VM"),
40 	T_META_RUN_CONCURRENTLY(true),
41 	T_META_TAG_VM_PREFERRED);
42 
43 static int64_t
run_sysctl_test(const char * t,int64_t value,bool may_fail)44 run_sysctl_test(const char *t, int64_t value, bool may_fail)
45 {
46 	char name[1024];
47 	int64_t result = 0;
48 	size_t s = sizeof(value);
49 	int rc;
50 
51 	snprintf(name, sizeof(name), "debug.test.%s", t);
52 	rc = sysctlbyname(name, &result, &s, &value, s);
53 	if (may_fail) {
54 		T_MAYFAIL;
55 	}
56 	T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", t);
57 	return result;
58 }
59 
60 /* HSC TODO: enable MTE */
61 T_DECL(vm_mte_ts_for_compressor_bootarg_on,
62     "Use tag storage pages for the compressor pool",
63     T_META_BOOTARGS_SET("mte_ts_compressor=1"),
64     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
65     T_META_ASROOT(true),
66     XNU_T_META_SOC_SPECIFIC)
67 {
68 	T_EXPECT_EQ_LLONG(1ll, run_sysctl_test("vm_mte_tag_storage_for_compressor",
69 	    1, true),
70 	    "didn't get tag storage pages for compressor pool");
71 }
72 
73 T_DECL(vm_mte_ts_for_compressor_bootarg_off,
74     "Use normal pages for the compressor pool",
75     T_META_BOOTARGS_SET("mte_ts_compressor=0"),
76     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
77     T_META_ASROOT(true),
78     XNU_T_META_SOC_SPECIFIC)
79 {
80 	T_EXPECT_EQ_LLONG(1ll, run_sysctl_test("vm_mte_tag_storage_for_compressor",
81 	    0, true),
82 	    "didn't get normal pages for compressor pool");
83 }
84 
85 T_DECL(vm_mte_ts_for_stack_bootarg_on,
86     "Use tag storage for thread stacks",
87     T_META_BOOTARGS_SET("mte_ts_vm_tag=30"),
88     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
89     T_META_ASROOT(true),
90     XNU_T_META_SOC_SPECIFIC)
91 {
92 	struct {
93 		uint32_t vm_tag;
94 		bool mte;
95 		bool expect_ts;
96 	} args = {30, false, true};
97 	T_EXPECT_EQ_INT(1,
98 	    (int)run_sysctl_test("vm_mte_tag_storage_for_vm_tag", (int64_t)(&args),
99 	    false),
100 	    "didn't get tag storage page for allocation");
101 }
102 
103 T_DECL(vm_mte_ts_for_stack_bootarg_off,
104     "Use normal page for thread stacks",
105     T_META_BOOTARGS_SET("mte_ts_vm_tag=1"),
106     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
107     T_META_ASROOT(true),
108     XNU_T_META_SOC_SPECIFIC)
109 {
110 	struct {
111 		uint32_t vm_tag;
112 		bool mte;
113 		bool expect_ts;
114 	} args = {30, false, false};
115 	T_EXPECT_EQ_INT(1,
116 	    (int)run_sysctl_test("vm_mte_tag_storage_for_vm_tag", (int64_t)(&args),
117 	    false),
118 	    "didn't get normal page for allocation");
119 }
120 
121 T_DECL(vm_mte_ts_for_untagged_malloc,
122     "Use tag storage page for untagged malloc heap",
123     T_META_BOOTARGS_SET("mte_ts_vm_tag=1_4,7_9,11_12"),
124     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
125     T_META_ASROOT(true),
126     XNU_T_META_SOC_SPECIFIC)
127 {
128 	struct {
129 		uint32_t vm_tag;
130 		bool mte;
131 		bool expect_ts;
132 	} args = {2, false, true};
133 	T_EXPECT_EQ_INT(1,
134 	    (int)run_sysctl_test("vm_mte_tag_storage_for_vm_tag", (int64_t)(&args),
135 	    false),
136 	    "didn't get tag storage page for allocation");
137 }
138