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