1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2024 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions *
4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions *
6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions *
15*043036a2SApple OSS Distributions * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions *
18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions * limitations under the License.
25*043036a2SApple OSS Distributions *
26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions */
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions #include <arm_acle.h>
30*043036a2SApple OSS Distributions #include <darwintest.h>
31*043036a2SApple OSS Distributions #include <darwintest_multiprocess.h>
32*043036a2SApple OSS Distributions #include <mach-o/dyld.h>
33*043036a2SApple OSS Distributions #include <pthread.h>
34*043036a2SApple OSS Distributions #include <time.h>
35*043036a2SApple OSS Distributions #include <spawn.h>
36*043036a2SApple OSS Distributions
37*043036a2SApple OSS Distributions #include "arm_mte_utilities.h"
38*043036a2SApple OSS Distributions #include "test_utils.h"
39*043036a2SApple OSS Distributions
40*043036a2SApple OSS Distributions T_GLOBAL_META(
41*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.arm.mte"),
42*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
43*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("arm"),
44*043036a2SApple OSS Distributions T_META_RUN_CONCURRENTLY(true),
45*043036a2SApple OSS Distributions T_META_OWNER("n_sabo"),
46*043036a2SApple OSS Distributions T_META_IGNORECRASHES(".*arm_mte.*")
47*043036a2SApple OSS Distributions );
48*043036a2SApple OSS Distributions
49*043036a2SApple OSS Distributions static int n_threads = 20;
50*043036a2SApple OSS Distributions static int n_procs = 30;
51*043036a2SApple OSS Distributions /* When run with full_test=true, the device needs to be opened
52*043036a2SApple OSS Distributions * and connected to the internet. This doesn't fare well in BATS,
53*043036a2SApple OSS Distributions * but is useful for when running this test on a properly set up
54*043036a2SApple OSS Distributions * device at desk. */
55*043036a2SApple OSS Distributions bool full_test = false;
56*043036a2SApple OSS Distributions
57*043036a2SApple OSS Distributions #if TARGET_OS_IOS
58*043036a2SApple OSS Distributions const char *terminate_safari = "killall -9 MobileSafari";
59*043036a2SApple OSS Distributions const char *safari_identifier = "com.apple.mobilesafari";
60*043036a2SApple OSS Distributions #elif TARGET_OS_OSX
61*043036a2SApple OSS Distributions const char *safari_path = "/Applications/Safari.app/Contents/MacOS/Safari";
62*043036a2SApple OSS Distributions const char *terminate_safari = "killall -9 Safari";
63*043036a2SApple OSS Distributions const char *safari_identifier = "com.apple.Safari";
64*043036a2SApple OSS Distributions #endif
65*043036a2SApple OSS Distributions
66*043036a2SApple OSS Distributions typedef struct compressor_stats {
67*043036a2SApple OSS Distributions uint64_t tag_compressions;
68*043036a2SApple OSS Distributions uint64_t tag_decompressions;
69*043036a2SApple OSS Distributions } compressor_stats;
70*043036a2SApple OSS Distributions
71*043036a2SApple OSS Distributions static void*
allocate_memory_and_wait(void * arg)72*043036a2SApple OSS Distributions allocate_memory_and_wait(void *arg)
73*043036a2SApple OSS Distributions {
74*043036a2SApple OSS Distributions T_SETUPBEGIN;
75*043036a2SApple OSS Distributions static const size_t ALLOC_SIZE = KERNEL_BUFFER_COPY_THRESHOLD;
76*043036a2SApple OSS Distributions long thread_num_for_proc = (long)arg;
77*043036a2SApple OSS Distributions vm_address_t address = (vm_address_t)NULL;
78*043036a2SApple OSS Distributions
79*043036a2SApple OSS Distributions boolean_t is_tagged = thread_num_for_proc % 2;
80*043036a2SApple OSS Distributions
81*043036a2SApple OSS Distributions int flags = VM_FLAGS_ANYWHERE;
82*043036a2SApple OSS Distributions if (is_tagged) {
83*043036a2SApple OSS Distributions flags |= VM_FLAGS_MTE;
84*043036a2SApple OSS Distributions }
85*043036a2SApple OSS Distributions
86*043036a2SApple OSS Distributions /* We want to allocate the max amount of memory we'll need for the test */
87*043036a2SApple OSS Distributions kern_return_t kr = vm_allocate(mach_task_self(), &address, ALLOC_SIZE, flags);
88*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory");
89*043036a2SApple OSS Distributions char *untagged_ptr = (char *)address;
90*043036a2SApple OSS Distributions T_SETUPEND;
91*043036a2SApple OSS Distributions
92*043036a2SApple OSS Distributions char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
93*043036a2SApple OSS Distributions unsigned int orig_tag = extract_mte_tag(orig_tagged_ptr);
94*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(orig_tag, 0U, "originally assigned tag is zero");
95*043036a2SApple OSS Distributions
96*043036a2SApple OSS Distributions if (is_tagged) {
97*043036a2SApple OSS Distributions char *random_tagged_ptr = NULL;
98*043036a2SApple OSS Distributions /*
99*043036a2SApple OSS Distributions * Generate the random tag. xnu automatically excludes 0 as a tag
100*043036a2SApple OSS Distributions * for userspace: ensure that it never shows up in the loop below.
101*043036a2SApple OSS Distributions */
102*043036a2SApple OSS Distributions for (unsigned int i = 0; i < NUM_MTE_TAGS * 4; i++) {
103*043036a2SApple OSS Distributions random_tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, 0);
104*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_NE_PTR(orig_tagged_ptr, random_tagged_ptr,
105*043036a2SApple OSS Distributions "random tag was not taken from excluded tag set");
106*043036a2SApple OSS Distributions
107*043036a2SApple OSS Distributions ptrdiff_t diff = __arm_mte_ptrdiff(untagged_ptr, random_tagged_ptr);
108*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG(diff, (ptrdiff_t)0, "untagged %p and tagged %p have identical address bits",
109*043036a2SApple OSS Distributions untagged_ptr, random_tagged_ptr);
110*043036a2SApple OSS Distributions }
111*043036a2SApple OSS Distributions
112*043036a2SApple OSS Distributions /* Ensure that basic set/read/access operations work */
113*043036a2SApple OSS Distributions
114*043036a2SApple OSS Distributions /* Store the last generated random tag */
115*043036a2SApple OSS Distributions __arm_mte_set_tag((void *)random_tagged_ptr);
116*043036a2SApple OSS Distributions /* Read it back and ensure it matches */
117*043036a2SApple OSS Distributions char *newly_tagged_ptr = __arm_mte_get_tag((void *)random_tagged_ptr);
118*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ_PTR(newly_tagged_ptr, random_tagged_ptr, "tag was committed to memory correctly");
119*043036a2SApple OSS Distributions /* Ensure we can access */
120*043036a2SApple OSS Distributions newly_tagged_ptr[0] = 'a';
121*043036a2SApple OSS Distributions /* Reset the initial zero tag */
122*043036a2SApple OSS Distributions __arm_mte_set_tag((void *)address);
123*043036a2SApple OSS Distributions } else {
124*043036a2SApple OSS Distributions for (uint64_t i = 0; i < ALLOC_SIZE; ++i) {
125*043036a2SApple OSS Distributions orig_tagged_ptr[i] = 'a';
126*043036a2SApple OSS Distributions }
127*043036a2SApple OSS Distributions }
128*043036a2SApple OSS Distributions
129*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(vm_deallocate(mach_task_self(), address, ALLOC_SIZE), "Deallocated memory");
130*043036a2SApple OSS Distributions return (void *)NULL;
131*043036a2SApple OSS Distributions }
132*043036a2SApple OSS Distributions
133*043036a2SApple OSS Distributions T_HELPER_DECL(create_many_threads_helper, "A helper that creates n_threads threads and assert they exit successfully") {
134*043036a2SApple OSS Distributions pthread_t thread[n_threads];
135*043036a2SApple OSS Distributions void *status = NULL;
136*043036a2SApple OSS Distributions
137*043036a2SApple OSS Distributions /* the process should be mte enabled */
138*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(validate_proc_pidinfo_mte_status(getpid(), true), "process is running with MTE");
139*043036a2SApple OSS Distributions
140*043036a2SApple OSS Distributions /* Create multiple threads */
141*043036a2SApple OSS Distributions for (long thread_num = 0; thread_num < n_threads; thread_num++) {
142*043036a2SApple OSS Distributions int return_code = pthread_create(&thread[thread_num], NULL, allocate_memory_and_wait, (void*) thread_num);
143*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(return_code, "Created thread %li", thread_num);
144*043036a2SApple OSS Distributions }
145*043036a2SApple OSS Distributions
146*043036a2SApple OSS Distributions /* Wait for all threads to finish */
147*043036a2SApple OSS Distributions for (int thread_num = 0; thread_num < n_threads; thread_num++) {
148*043036a2SApple OSS Distributions int return_code = pthread_join(thread[thread_num], &status);
149*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(return_code, "Thread %d joined successfully", thread_num);
150*043036a2SApple OSS Distributions }
151*043036a2SApple OSS Distributions T_PASS("Process with pid %d exiting\n", getpid());
152*043036a2SApple OSS Distributions }
153*043036a2SApple OSS Distributions
154*043036a2SApple OSS Distributions T_HELPER_DECL(app_helper, "A helper that launches and stimulates Safari and Notes") {
155*043036a2SApple OSS Distributions #if TARGET_OS_IOS
156*043036a2SApple OSS Distributions int buffer_size = 256;
157*043036a2SApple OSS Distributions char launch_safari[buffer_size] = {};
158*043036a2SApple OSS Distributions snprintf(launch_safari, buffer_size, "xctitool launch %s", safari_identifier);
159*043036a2SApple OSS Distributions /* For now, the running Safari process will not have MTE.
160*043036a2SApple OSS Distributions * Eventually, MTE will be enabled on Safari by default from the system's launchd plist. */
161*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system(launch_safari), "launchd Safari");
162*043036a2SApple OSS Distributions
163*043036a2SApple OSS Distributions /* Move past home screen to launch app in foreground */
164*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("LaunchApp -unlock com.apple.springboard"), "open homescreen");
165*043036a2SApple OSS Distributions
166*043036a2SApple OSS Distributions /* Process 1: Safari, enabled with MTE, launched and we open a new tab */
167*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool interact com.apple.mobilesafari -action \"tap\" -element \"NewTabButton\""), "new Safari tab");
168*043036a2SApple OSS Distributions
169*043036a2SApple OSS Distributions if (full_test) {
170*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool interact com.apple.mobilesafari --element \"favoritesItemIdentifierContent\" --action tap"), "Safari internet search");
171*043036a2SApple OSS Distributions }
172*043036a2SApple OSS Distributions
173*043036a2SApple OSS Distributions /* Process 2: Notes app (spawned without MTE), brought to foreground */
174*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool launch com.apple.mobilenotes"), "launch notes app");
175*043036a2SApple OSS Distributions
176*043036a2SApple OSS Distributions #elif TARGET_OS_OSX
177*043036a2SApple OSS Distributions int buffer_size = 256;
178*043036a2SApple OSS Distributions char launch_safari[buffer_size] = {};
179*043036a2SApple OSS Distributions snprintf(launch_safari, buffer_size, "xctitool launch %s", safari_identifier);
180*043036a2SApple OSS Distributions if (full_test) {
181*043036a2SApple OSS Distributions /* Although these commands pass at desk, weird things happen in BATS */
182*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system(launch_safari), "launchd Safari");
183*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool interact com.apple.Safari -action \"click\" -element \"NewTabButton\""), "new Safari tab");
184*043036a2SApple OSS Distributions /* Since J7XX hardware in BATS can connect to WiFi, make a search.
185*043036a2SApple OSS Distributions * This action opens one of the recommended websites on the Safari homepage .*/
186*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool interact com.apple.Safari --element \"linkRecommendationCollectionViewItem\" --action click"), "Safari internet search");
187*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(system("xctitool launch com.apple.Notes"), "launch notes app");
188*043036a2SApple OSS Distributions }
189*043036a2SApple OSS Distributions #endif
190*043036a2SApple OSS Distributions }
191*043036a2SApple OSS Distributions
192*043036a2SApple OSS Distributions T_HELPER_DECL(arm_mte_stress_helper, "forks many multi-threaded processes that allocated tagged and untagged memory") {
193*043036a2SApple OSS Distributions dt_helper_t helpers[n_procs + 1];
194*043036a2SApple OSS Distributions /* Start the helper that spawns Safari with MTE and excercises it in interesting ways */
195*043036a2SApple OSS Distributions helpers[0] = dt_fork_helper("app_helper");
196*043036a2SApple OSS Distributions /* Start the helpers that allocate tagged memory from multiple threads, for multiple processes */
197*043036a2SApple OSS Distributions for (int i = 1; i <= n_procs; ++i) {
198*043036a2SApple OSS Distributions helpers[i] = dt_fork_helper("create_many_threads_helper");
199*043036a2SApple OSS Distributions }
200*043036a2SApple OSS Distributions dt_run_helpers(helpers, (unsigned long)n_procs + 1, 600);
201*043036a2SApple OSS Distributions }
202*043036a2SApple OSS Distributions
203*043036a2SApple OSS Distributions void
run_munch(bool with_lim_resident)204*043036a2SApple OSS Distributions run_munch(bool with_lim_resident)
205*043036a2SApple OSS Distributions {
206*043036a2SApple OSS Distributions /* Use munch to wire down as much memory as possible. We want the memory to stay
207*043036a2SApple OSS Distributions * wired throughout the test, to make it easier to invoke the compressor. This is why
208*043036a2SApple OSS Distributions * we wire it at priority 98. However, we want the test process to proceed after wiring
209*043036a2SApple OSS Distributions * the memory, so wire it in the background, otherwise, the test blocks at this step. */
210*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(system("munch --lim-jetsam 98 --type=wired --cfg-background")
211*043036a2SApple OSS Distributions , "wired memory with munch");
212*043036a2SApple OSS Distributions
213*043036a2SApple OSS Distributions /*
214*043036a2SApple OSS Distributions * Start munch to increase memory pressure by creating as much page demand as possible,
215*043036a2SApple OSS Distributions * filling new pages with zeros, and creating the need for memory to be compressed or swapped.
216*043036a2SApple OSS Distributions * Spawn this with MTE, as malloc, in some cases, allocates tagged memory
217*043036a2SApple OSS Distributions */
218*043036a2SApple OSS Distributions if (with_lim_resident) {
219*043036a2SApple OSS Distributions char *munch_args[] = {"/usr/local/bin/munch", "--type=malloc", "--lim-resident", "--fill-zero", "--demand-pattern=exponential", "--demand-increment=unlimited", "--cfg-background", NULL};
220*043036a2SApple OSS Distributions posix_spawn_then_perform_action_from_process(munch_args, MTE_SPAWN_USE_LEGACY_API, 0);
221*043036a2SApple OSS Distributions }
222*043036a2SApple OSS Distributions }
223*043036a2SApple OSS Distributions
224*043036a2SApple OSS Distributions static void
tear_down(void)225*043036a2SApple OSS Distributions tear_down(void)
226*043036a2SApple OSS Distributions {
227*043036a2SApple OSS Distributions /* Terminate munch */
228*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(system("killall -9 munch"), "terminated munch");
229*043036a2SApple OSS Distributions }
230*043036a2SApple OSS Distributions
231*043036a2SApple OSS Distributions bool
should_run_munch_lim_resident(int argc,char * const * argv)232*043036a2SApple OSS Distributions should_run_munch_lim_resident(int argc, char *const *argv)
233*043036a2SApple OSS Distributions {
234*043036a2SApple OSS Distributions if (argc == 2) {
235*043036a2SApple OSS Distributions if (atoi(argv[1]) == 1) {
236*043036a2SApple OSS Distributions T_LOG("Will run with munch lim-resident");
237*043036a2SApple OSS Distributions return true;
238*043036a2SApple OSS Distributions }
239*043036a2SApple OSS Distributions }
240*043036a2SApple OSS Distributions return false;
241*043036a2SApple OSS Distributions }
242*043036a2SApple OSS Distributions
243*043036a2SApple OSS Distributions int
parse_num_cycles(int argc,char * const * argv)244*043036a2SApple OSS Distributions parse_num_cycles(int argc, char *const *argv)
245*043036a2SApple OSS Distributions {
246*043036a2SApple OSS Distributions if (argc >= 1) {
247*043036a2SApple OSS Distributions if (atoi(argv[0]) > 0) {
248*043036a2SApple OSS Distributions T_LOG("Will run %d cycles", n_procs);
249*043036a2SApple OSS Distributions return atoi(argv[0]);
250*043036a2SApple OSS Distributions }
251*043036a2SApple OSS Distributions }
252*043036a2SApple OSS Distributions return 3;
253*043036a2SApple OSS Distributions }
254*043036a2SApple OSS Distributions
255*043036a2SApple OSS Distributions void
set_test_mode(int argc,char * const * argv)256*043036a2SApple OSS Distributions set_test_mode(int argc, char *const *argv)
257*043036a2SApple OSS Distributions {
258*043036a2SApple OSS Distributions if (argc >= 3) {
259*043036a2SApple OSS Distributions if (atoi(argv[2]) == 1) {
260*043036a2SApple OSS Distributions T_LOG("Will run the full test version. Requires internet and unlocked device.");
261*043036a2SApple OSS Distributions full_test = true;
262*043036a2SApple OSS Distributions }
263*043036a2SApple OSS Distributions }
264*043036a2SApple OSS Distributions }
265*043036a2SApple OSS Distributions
266*043036a2SApple OSS Distributions void
launch_helper(char * helper_name)267*043036a2SApple OSS Distributions launch_helper(char *helper_name)
268*043036a2SApple OSS Distributions {
269*043036a2SApple OSS Distributions char path[PATH_MAX] = {};
270*043036a2SApple OSS Distributions uint32_t path_size = sizeof(path);
271*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
272*043036a2SApple OSS Distributions char *helper_args[] = { path, "-n", helper_name, NULL};
273*043036a2SApple OSS Distributions int status = -1;
274*043036a2SApple OSS Distributions pid_t child_pid = 0;
275*043036a2SApple OSS Distributions
276*043036a2SApple OSS Distributions /* Now, continuously allocate tagged memory on behalf of multiple, multi-threaded processes
277*043036a2SApple OSS Distributions * by spawning arm_mte_stress_helper repeatedly and launching Safari with MTE and Notes without MTE
278*043036a2SApple OSS Distributions * to provide some end-to-end system testing. */
279*043036a2SApple OSS Distributions int ret = posix_spawn(&child_pid, helper_args[0], NULL, NULL, helper_args, NULL);
280*043036a2SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
281*043036a2SApple OSS Distributions T_ASSERT_NE(child_pid, 0, "posix_spawn");
282*043036a2SApple OSS Distributions
283*043036a2SApple OSS Distributions /* Ensure the process from which tagged memory was allocated succeeded. */
284*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(child_pid, &status, 0), "waitpid");
285*043036a2SApple OSS Distributions T_EXPECT_TRUE(WIFEXITED(status), "exited successfully");
286*043036a2SApple OSS Distributions T_EXPECT_TRUE(WEXITSTATUS(status) == 0, "exited with status %d", WEXITSTATUS(status));
287*043036a2SApple OSS Distributions }
288*043036a2SApple OSS Distributions
289*043036a2SApple OSS Distributions /*
290*043036a2SApple OSS Distributions * One can change the level of memory pressure applied and number of iterations
291*043036a2SApple OSS Distributions * via the cli as follows:
292*043036a2SApple OSS Distributions *
293*043036a2SApple OSS Distributions * ./arm_mte_stress arm_mte_stress_cycler -- <num_cycles> <with_lim_resident> <test_mode>
294*043036a2SApple OSS Distributions *
295*043036a2SApple OSS Distributions * <num_cycles>: number of cycles to repeat the test. Default is 3.
296*043036a2SApple OSS Distributions * <with_lim_resident>: should be 1 to specify running the test with extra pressure.
297*043036a2SApple OSS Distributions * <test_mode>: should be 1 specify running the test with Safari internet searches.
298*043036a2SApple OSS Distributions */
299*043036a2SApple OSS Distributions T_DECL(arm_mte_stress_cycler,
300*043036a2SApple OSS Distributions "Wires down as much memory as permitted using munch and allocates tagged memory "
301*043036a2SApple OSS Distributions "from multiple multi-threaded processes to create memory pressure. Launches Safari "
302*043036a2SApple OSS Distributions "with MTE and opens a new tab. Then launches Notes, which is not MTE enabled, to "
303*043036a2SApple OSS Distributions "exercise the system in a more interesting way. This is repeated three times and then "
304*043036a2SApple OSS Distributions "sysctls are used to ensure that the compressor is compressing and decompressing tag "
305*043036a2SApple OSS Distributions "storage pages. Test can be enhanced to run more cycles, or add additional memory "
306*043036a2SApple OSS Distributions "pressure when run at desk. ",
307*043036a2SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
308*043036a2SApple OSS Distributions /* For now, J8XX form-factor devices with WiFi are not available in BATS */
309*043036a2SApple OSS Distributions #if TARGET_OS_OSX
310*043036a2SApple OSS Distributions T_META_REQUIRES_NETWORK(true),
311*043036a2SApple OSS Distributions #endif
312*043036a2SApple OSS Distributions XNU_T_META_SOC_SPECIFIC,
313*043036a2SApple OSS Distributions T_META_ENABLED(false) /* rdar://147337971 */) {
314*043036a2SApple OSS Distributions T_ATEND(tear_down);
315*043036a2SApple OSS Distributions
316*043036a2SApple OSS Distributions /* User override to add extra memory pressure by running munch --lim-resident.
317*043036a2SApple OSS Distributions * Default is without. */
318*043036a2SApple OSS Distributions bool with_lim_resident = should_run_munch_lim_resident(argc, argv);
319*043036a2SApple OSS Distributions /* User override of number of cycles to repeat the test. Default is 3. */
320*043036a2SApple OSS Distributions int num_cycles = parse_num_cycles(argc, argv);
321*043036a2SApple OSS Distributions /* User override to determine which mode to run the test in. A value of 1
322*043036a2SApple OSS Distributions * means making Safari searches and requires internet connectivity. */
323*043036a2SApple OSS Distributions set_test_mode(argc, argv);
324*043036a2SApple OSS Distributions
325*043036a2SApple OSS Distributions /* Create memory pressure using munch. */
326*043036a2SApple OSS Distributions run_munch(with_lim_resident);
327*043036a2SApple OSS Distributions
328*043036a2SApple OSS Distributions struct compressor_stats *compressor_data = malloc(num_cycles * sizeof(struct compressor_stats));
329*043036a2SApple OSS Distributions
330*043036a2SApple OSS Distributions for (int i = 0; i < num_cycles; ++i) {
331*043036a2SApple OSS Distributions /* Verify that MTE compression is not disabled on the device */
332*043036a2SApple OSS Distributions uint64_t no_compressor_pager_for_mte_count = sysctl_get_Q("vm.mte.no_compressor_pager_for_mte");
333*043036a2SApple OSS Distributions if (no_compressor_pager_for_mte_count > 0) {
334*043036a2SApple OSS Distributions T_SKIP("MTE compression is disabled on this device.");
335*043036a2SApple OSS Distributions }
336*043036a2SApple OSS Distributions
337*043036a2SApple OSS Distributions compressor_data[i].tag_compressions = sysctl_get_Q("vm.mte.compress_pages_compressed");
338*043036a2SApple OSS Distributions T_LOG("Compressed tags: %llu compressed tags", compressor_data[i].tag_compressions);
339*043036a2SApple OSS Distributions compressor_data[i].tag_decompressions = sysctl_get_Q("vm.mte.compress_pages_decompressed");
340*043036a2SApple OSS Distributions T_LOG("Decompressed tags: %llu decompressed tags", compressor_data[i].tag_decompressions);
341*043036a2SApple OSS Distributions
342*043036a2SApple OSS Distributions /* Now, continuously allocate tagged memory on behalf of multiple, multi-threaded processes
343*043036a2SApple OSS Distributions * by spawning arm_mte_stress_helper repeatedly and launching Safari with MTE and Notes without MTE
344*043036a2SApple OSS Distributions * to provide some end-to-end system testing. */
345*043036a2SApple OSS Distributions launch_helper("arm_mte_stress_helper");
346*043036a2SApple OSS Distributions
347*043036a2SApple OSS Distributions /* When invoked with a larger number of cycles, ensure tag pages are compressed and
348*043036a2SApple OSS Distributions * decompressed throughout the test */
349*043036a2SApple OSS Distributions if (i >= 40 && i >= (num_cycles / 3)) {
350*043036a2SApple OSS Distributions /* Ensure the compressor is compressing and decompressing tag pages. */
351*043036a2SApple OSS Distributions /* If after (num_cycles / 3) rounds, compressions and decompressions have not */
352*043036a2SApple OSS Distributions /* increased, something is blocked */
353*043036a2SApple OSS Distributions T_EXPECT_GT_(compressor_data[i].tag_compressions, compressor_data[i - (num_cycles / 3)].tag_compressions, "MTE tag pages are being compressed as expected");
354*043036a2SApple OSS Distributions T_EXPECT_GT_(compressor_data[i].tag_decompressions, compressor_data[i - (num_cycles / 3)].tag_decompressions, "MTE tag pages are being decompressed as expected");
355*043036a2SApple OSS Distributions }
356*043036a2SApple OSS Distributions }
357*043036a2SApple OSS Distributions
358*043036a2SApple OSS Distributions /* Assert tag pages were compressed or decompressed since the beginning of the test. */
359*043036a2SApple OSS Distributions T_EXPECT_TRUE((compressor_data[num_cycles - 1].tag_compressions > compressor_data[0].tag_compressions) ||
360*043036a2SApple OSS Distributions (compressor_data[num_cycles - 1].tag_decompressions > compressor_data[0].tag_decompressions),
361*043036a2SApple OSS Distributions "MTE tag pages are being compressed and/or decompressed as expected");
362*043036a2SApple OSS Distributions
363*043036a2SApple OSS Distributions /* Summarize compression / decompression growth over the duration of the test */
364*043036a2SApple OSS Distributions T_LOG("Tag page compressions:");
365*043036a2SApple OSS Distributions for (int i = 0; i < num_cycles; ++i) {
366*043036a2SApple OSS Distributions /* T_LOG inserts a newline after each metric, after printing a timestamp.
367*043036a2SApple OSS Distributions * That makes these statistics difficult to transfer over to say, excel,
368*043036a2SApple OSS Distributions * for further analysis. Print the values in a single, comma delineated line.
369*043036a2SApple OSS Distributions */
370*043036a2SApple OSS Distributions fprintf(stderr, "%llu, ", compressor_data[i].tag_compressions);
371*043036a2SApple OSS Distributions }
372*043036a2SApple OSS Distributions T_LOG("Tag page decompressions:");
373*043036a2SApple OSS Distributions for (int i = 0; i < num_cycles; ++i) {
374*043036a2SApple OSS Distributions fprintf(stderr, "%llu, ", compressor_data[i].tag_decompressions);
375*043036a2SApple OSS Distributions }
376*043036a2SApple OSS Distributions
377*043036a2SApple OSS Distributions free(compressor_data);
378*043036a2SApple OSS Distributions }
379