xref: /xnu-11417.140.69/tests/libmalloc_apple_array.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include <errno.h>
2*43a90889SApple OSS Distributions #include <stdlib.h>
3*43a90889SApple OSS Distributions #include <libgen.h>
4*43a90889SApple OSS Distributions #include <limits.h>
5*43a90889SApple OSS Distributions #include <mach-o/dyld.h>
6*43a90889SApple OSS Distributions #include <sys/types.h>
7*43a90889SApple OSS Distributions #include <sys/sysctl.h>
8*43a90889SApple OSS Distributions #include <xlocale.h>
9*43a90889SApple OSS Distributions 
10*43a90889SApple OSS Distributions #include <darwintest.h>
11*43a90889SApple OSS Distributions #include <darwintest_utils.h>
12*43a90889SApple OSS Distributions 
13*43a90889SApple OSS Distributions #include "drop_priv.h"
14*43a90889SApple OSS Distributions #include "test_utils.h"
15*43a90889SApple OSS Distributions 
16*43a90889SApple OSS Distributions #if ENTITLED
17*43a90889SApple OSS Distributions #define SET_TREATMENT_ID set_treatment_id_entitled
18*43a90889SApple OSS Distributions #define SET_TREATMENT_ID_DESCR "Can set treatment id with entitlement"
19*43a90889SApple OSS Distributions #else /* ENTITLED */
20*43a90889SApple OSS Distributions #define SET_TREATMENT_ID set_treatment_id_unentitled
21*43a90889SApple OSS Distributions #define SET_TREATMENT_ID_DESCR "Can't set treatment id without entitlement"
22*43a90889SApple OSS Distributions #endif /* ENTITLED */
23*43a90889SApple OSS Distributions 
24*43a90889SApple OSS Distributions T_DECL(SET_TREATMENT_ID, "Verifies that EXPERIMENT sysctls can only be set with the entitlement", T_META_ASROOT(false))
25*43a90889SApple OSS Distributions {
26*43a90889SApple OSS Distributions #define TEST_STR "testing"
27*43a90889SApple OSS Distributions #define IDENTIFIER_LENGTH 36
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions 	int ret;
30*43a90889SApple OSS Distributions 	errno_t err;
31*43a90889SApple OSS Distributions 	char val[IDENTIFIER_LENGTH + 1] = {0};
32*43a90889SApple OSS Distributions 	size_t len = sizeof(val);
33*43a90889SApple OSS Distributions 	char new_val[IDENTIFIER_LENGTH + 1] = {0};
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions 	if (!is_development_kernel()) {
36*43a90889SApple OSS Distributions 		T_SKIP("skipping test on release kernel");
37*43a90889SApple OSS Distributions 	}
38*43a90889SApple OSS Distributions 
39*43a90889SApple OSS Distributions 	strlcpy(new_val, TEST_STR, sizeof(new_val));
40*43a90889SApple OSS Distributions 	if (running_as_root()) {
41*43a90889SApple OSS Distributions 		drop_priv();
42*43a90889SApple OSS Distributions 	}
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, strlen(new_val));
45*43a90889SApple OSS Distributions 	err = errno;
46*43a90889SApple OSS Distributions #if ENTITLED
47*43a90889SApple OSS Distributions 	len = sizeof(val);
48*43a90889SApple OSS Distributions 	memset(new_val, 0, sizeof(new_val));
49*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "set kern.trial_treatment_id");
50*43a90889SApple OSS Distributions 	/* Cleanup. Set it back to the empty string. */
51*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, 1);
52*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "reset kern.trial_treatment_id");
53*43a90889SApple OSS Distributions #else
54*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_FAILURE(ret, EPERM, "set kern.trial_treatment_id");
55*43a90889SApple OSS Distributions #endif /* ENTITLED */
56*43a90889SApple OSS Distributions }
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions #if ENTITLED
59*43a90889SApple OSS Distributions /* Check min and max value limits on numeric factors */
60*43a90889SApple OSS Distributions T_DECL(experiment_factor_numeric_limits,
61*43a90889SApple OSS Distributions     "Can only set factors within the legal range.",
62*43a90889SApple OSS Distributions     T_META_ASROOT(false))
63*43a90889SApple OSS Distributions {
64*43a90889SApple OSS Distributions #define kMinVal 5 /* The min value allowed for the testing factor. */
65*43a90889SApple OSS Distributions #define kMaxVal 10 /* The max value allowed for the testing factor. */
66*43a90889SApple OSS Distributions 	errno_t err;
67*43a90889SApple OSS Distributions 	int ret;
68*43a90889SApple OSS Distributions 	unsigned int current_val;
69*43a90889SApple OSS Distributions 	size_t len = sizeof(current_val);
70*43a90889SApple OSS Distributions 	unsigned int new_val;
71*43a90889SApple OSS Distributions 
72*43a90889SApple OSS Distributions 	if (running_as_root()) {
73*43a90889SApple OSS Distributions 		drop_priv();
74*43a90889SApple OSS Distributions 	}
75*43a90889SApple OSS Distributions 	new_val = kMinVal - 1;
76*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
77*43a90889SApple OSS Distributions 	err = errno;
78*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor below range.");
79*43a90889SApple OSS Distributions 
80*43a90889SApple OSS Distributions 	new_val = kMaxVal + 1;
81*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
82*43a90889SApple OSS Distributions 	err = errno;
83*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor above range.");
84*43a90889SApple OSS Distributions 
85*43a90889SApple OSS Distributions 	new_val = kMaxVal;
86*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
87*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at top of range.");
88*43a90889SApple OSS Distributions 
89*43a90889SApple OSS Distributions 	new_val = kMinVal;
90*43a90889SApple OSS Distributions 	ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
91*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at bottom of range.");
92*43a90889SApple OSS Distributions }
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions static uint64_t original_libmalloc_experiment_value = 0;
95*43a90889SApple OSS Distributions 
96*43a90889SApple OSS Distributions static void
reset_libmalloc_experiment(void)97*43a90889SApple OSS Distributions reset_libmalloc_experiment(void)
98*43a90889SApple OSS Distributions {
99*43a90889SApple OSS Distributions 	int ret = sysctlbyname("kern.libmalloc_experiments", NULL, NULL, &original_libmalloc_experiment_value, sizeof(original_libmalloc_experiment_value));
100*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "reset kern.libmalloc_experiments");
101*43a90889SApple OSS Distributions }
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions static void
set_libmalloc_experiment(uint64_t val)104*43a90889SApple OSS Distributions set_libmalloc_experiment(uint64_t val)
105*43a90889SApple OSS Distributions {
106*43a90889SApple OSS Distributions 	T_LOG("Setting kern.libmalloc_experiments to %llu", val);
107*43a90889SApple OSS Distributions 	size_t len = sizeof(original_libmalloc_experiment_value);
108*43a90889SApple OSS Distributions 	int ret = sysctlbyname("kern.libmalloc_experiments", &original_libmalloc_experiment_value, &len, &val, sizeof(val));
109*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "set kern.libmalloc_experiments");
110*43a90889SApple OSS Distributions 	T_ATEND(reset_libmalloc_experiment);
111*43a90889SApple OSS Distributions }
112*43a90889SApple OSS Distributions 
113*43a90889SApple OSS Distributions #define PRINT_APPLE_ARRAY_TOOL "tools/print_apple_array"
114*43a90889SApple OSS Distributions /*
115*43a90889SApple OSS Distributions  * Spawns a new binary and returns the contents of its apple array
116*43a90889SApple OSS Distributions  * (after libsystem initialization).
117*43a90889SApple OSS Distributions  */
118*43a90889SApple OSS Distributions static char **
get_apple_array(size_t * num_array_entries,const char * filename)119*43a90889SApple OSS Distributions get_apple_array(size_t *num_array_entries, const char * filename)
120*43a90889SApple OSS Distributions {
121*43a90889SApple OSS Distributions 	if (filename == NULL) {
122*43a90889SApple OSS Distributions 		filename = PRINT_APPLE_ARRAY_TOOL;
123*43a90889SApple OSS Distributions 	}
124*43a90889SApple OSS Distributions 	int ret;
125*43a90889SApple OSS Distributions 	char stdout_path[MAXPATHLEN] = "apple_array.txt";
126*43a90889SApple OSS Distributions 	dt_resultfile(stdout_path, MAXPATHLEN);
127*43a90889SApple OSS Distributions 	int exit_status = 0, signum = 0;
128*43a90889SApple OSS Distributions 	char binary_path[MAXPATHLEN], binary_dir[MAXPATHLEN];
129*43a90889SApple OSS Distributions 	char *char_ret;
130*43a90889SApple OSS Distributions 	const static size_t kMaxNumArguments = 256;
131*43a90889SApple OSS Distributions 	size_t linecap = 0;
132*43a90889SApple OSS Distributions 	ssize_t linelen = 0;
133*43a90889SApple OSS Distributions 	char **apple_array;
134*43a90889SApple OSS Distributions 	char **line = NULL;
135*43a90889SApple OSS Distributions 	size_t num_lines = 0;
136*43a90889SApple OSS Distributions 	FILE *stdout_f = NULL;
137*43a90889SApple OSS Distributions 	uint32_t name_size = MAXPATHLEN;
138*43a90889SApple OSS Distributions 
139*43a90889SApple OSS Distributions 	ret = _NSGetExecutablePath(binary_path, &name_size);
140*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "_NSGetExecutablePath");
141*43a90889SApple OSS Distributions 	char_ret = dirname_r(binary_path, binary_dir);
142*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(char_ret != NULL, "dirname_r");
143*43a90889SApple OSS Distributions 	snprintf(binary_path, MAXPATHLEN, "%s/%s", binary_dir, filename);
144*43a90889SApple OSS Distributions 
145*43a90889SApple OSS Distributions 	char *launch_tool_args[] = {
146*43a90889SApple OSS Distributions 		binary_path,
147*43a90889SApple OSS Distributions 		NULL
148*43a90889SApple OSS Distributions 	};
149*43a90889SApple OSS Distributions 	pid_t child_pid;
150*43a90889SApple OSS Distributions 	ret = dt_launch_tool(&child_pid, launch_tool_args, false, stdout_path, NULL);
151*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, "dt_launch_tool: %s", binary_path);
152*43a90889SApple OSS Distributions 
153*43a90889SApple OSS Distributions 	ret = dt_waitpid(child_pid, &exit_status, &signum, 60 * 5);
154*43a90889SApple OSS Distributions 	T_ASSERT_EQ(ret, 1, "dt_waitpid");
155*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
156*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
157*43a90889SApple OSS Distributions 
158*43a90889SApple OSS Distributions 	stdout_f = fopen(stdout_path, "r");
159*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_NOTNULL(stdout_f, "open(%s)", stdout_path);
160*43a90889SApple OSS Distributions 	apple_array = calloc(kMaxNumArguments, sizeof(char *));
161*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(apple_array, "calloc: %lu\n", sizeof(char *) * kMaxNumArguments);
162*43a90889SApple OSS Distributions 	while (num_lines < kMaxNumArguments) {
163*43a90889SApple OSS Distributions 		line = &(apple_array[num_lines++]);
164*43a90889SApple OSS Distributions 		linecap = 0;
165*43a90889SApple OSS Distributions 		linelen = getline(line, &linecap, stdout_f);
166*43a90889SApple OSS Distributions 		if (linelen == -1) {
167*43a90889SApple OSS Distributions 			break;
168*43a90889SApple OSS Distributions 		}
169*43a90889SApple OSS Distributions 	}
170*43a90889SApple OSS Distributions 	*num_array_entries = num_lines - 1;
171*43a90889SApple OSS Distributions 
172*43a90889SApple OSS Distributions 	ret = fclose(stdout_f);
173*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "fclose(%s)", stdout_path);
174*43a90889SApple OSS Distributions 
175*43a90889SApple OSS Distributions 	return apple_array;
176*43a90889SApple OSS Distributions }
177*43a90889SApple OSS Distributions 
178*43a90889SApple OSS Distributions #define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="
179*43a90889SApple OSS Distributions 
180*43a90889SApple OSS Distributions #define HARDENED_RUNTIME_KEY "HardenedRuntime="
181*43a90889SApple OSS Distributions 
182*43a90889SApple OSS Distributions #define SECURITY_CONFIG_KEY "security_config="
183*43a90889SApple OSS Distributions 
184*43a90889SApple OSS Distributions 
185*43a90889SApple OSS Distributions /*
186*43a90889SApple OSS Distributions  * Get the value of the key in the apple array.
187*43a90889SApple OSS Distributions  * Returns true iff the key is present.
188*43a90889SApple OSS Distributions  */
189*43a90889SApple OSS Distributions static bool
get_apple_array_key(char ** apple_array,size_t num_array_entries,uint64_t * factors,const char * key)190*43a90889SApple OSS Distributions get_apple_array_key(char **apple_array, size_t num_array_entries, uint64_t *factors, const char *key)
191*43a90889SApple OSS Distributions {
192*43a90889SApple OSS Distributions 	bool found = false;
193*43a90889SApple OSS Distributions 	for (size_t i = 0; i < num_array_entries; i++) {
194*43a90889SApple OSS Distributions 		char *str = apple_array[i];
195*43a90889SApple OSS Distributions 		if (strstr(str, key)) {
196*43a90889SApple OSS Distributions 			found = true;
197*43a90889SApple OSS Distributions 			if (factors != NULL) {
198*43a90889SApple OSS Distributions 				str = strchr(str, '=');
199*43a90889SApple OSS Distributions 				T_ASSERT_NOTNULL(str, "skip over =");
200*43a90889SApple OSS Distributions 				++str;
201*43a90889SApple OSS Distributions 				*factors = strtoull_l(str, NULL, 16, NULL);
202*43a90889SApple OSS Distributions 			}
203*43a90889SApple OSS Distributions 			break;
204*43a90889SApple OSS Distributions 		}
205*43a90889SApple OSS Distributions 	}
206*43a90889SApple OSS Distributions 	return found;
207*43a90889SApple OSS Distributions }
208*43a90889SApple OSS Distributions 
209*43a90889SApple OSS Distributions /* libmalloc relies on these values not changing. If they change,
210*43a90889SApple OSS Distributions  * you need to update the values in that project as well */
211*43a90889SApple OSS Distributions __options_decl(HR_flags_t, uint32_t, {
212*43a90889SApple OSS Distributions 	BrowserHostEntitlementMask       = 0x01,
213*43a90889SApple OSS Distributions 	BrowserGPUEntitlementMask        = 0x02,
214*43a90889SApple OSS Distributions 	BrowserNetworkEntitlementMask    = 0x04,
215*43a90889SApple OSS Distributions 	BrowserWebContentEntitlementMask = 0x08,
216*43a90889SApple OSS Distributions });
217*43a90889SApple OSS Distributions 
218*43a90889SApple OSS Distributions T_DECL(libmalloc_hardened_binary_present,
219*43a90889SApple OSS Distributions     "hardened binary flags show up in apple array",
220*43a90889SApple OSS Distributions     T_META_ASROOT(false))
221*43a90889SApple OSS Distributions {
222*43a90889SApple OSS Distributions 	uint64_t apple_array_val = 0;
223*43a90889SApple OSS Distributions 	size_t num_array_entries = 0;
224*43a90889SApple OSS Distributions 	char **apple_array;
225*43a90889SApple OSS Distributions 	bool found = false;
226*43a90889SApple OSS Distributions 
227*43a90889SApple OSS Distributions 	/* These are the entitlements on the HR1 binary */
228*43a90889SApple OSS Distributions 	uint32_t mask_val = BrowserHostEntitlementMask | BrowserGPUEntitlementMask | BrowserWebContentEntitlementMask;
229*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, "tools/print_apple_array_HR1");
230*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, HARDENED_RUNTIME_KEY);
231*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(found, "Found " HARDENED_RUNTIME_KEY " in apple array");
232*43a90889SApple OSS Distributions 	T_ASSERT_EQ(apple_array_val, mask_val, "Bitmask value matches");
233*43a90889SApple OSS Distributions 	free(apple_array);
234*43a90889SApple OSS Distributions 
235*43a90889SApple OSS Distributions 	/* These are the entitlements on the HR2 binary */
236*43a90889SApple OSS Distributions 	mask_val = BrowserGPUEntitlementMask | BrowserNetworkEntitlementMask;
237*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, "tools/print_apple_array_HR2");
238*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, HARDENED_RUNTIME_KEY);
239*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(found, "Found " HARDENED_RUNTIME_KEY " in apple array");
240*43a90889SApple OSS Distributions 	T_ASSERT_EQ(apple_array_val, mask_val, "Bitmask value matches");
241*43a90889SApple OSS Distributions 	free(apple_array);
242*43a90889SApple OSS Distributions }
243*43a90889SApple OSS Distributions 
244*43a90889SApple OSS Distributions #define SECURITY_CONFIG_HARDENED_HEAP_ENTRY        (0x01)
245*43a90889SApple OSS Distributions #define SECURITY_CONFIG_TPRO_ENTRY                 (0x02)
246*43a90889SApple OSS Distributions 
247*43a90889SApple OSS Distributions T_DECL(libmalloc_security_config_hardened_heap_entitlements,
248*43a90889SApple OSS Distributions     "parse security_config values to verify security configs hardened_heap enablement/disablement",
249*43a90889SApple OSS Distributions     T_META_ASROOT(false))
250*43a90889SApple OSS Distributions {
251*43a90889SApple OSS Distributions 	uint64_t apple_array_val = 0;
252*43a90889SApple OSS Distributions 	size_t num_array_entries = 0;
253*43a90889SApple OSS Distributions 	char **apple_array;
254*43a90889SApple OSS Distributions 	bool found = false;
255*43a90889SApple OSS Distributions 
256*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, "tools/print_apple_array_hardened_proc");
257*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, SECURITY_CONFIG_KEY);
258*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(found, "Found " SECURITY_CONFIG_KEY " in apple array");
259*43a90889SApple OSS Distributions 
260*43a90889SApple OSS Distributions 	/* Let's start parsing the security config, to see what's enabled. */
261*43a90889SApple OSS Distributions 	T_ASSERT_FALSE(apple_array_val & SECURITY_CONFIG_HARDENED_HEAP_ENTRY, "Hardened-heap is disabled");
262*43a90889SApple OSS Distributions 	free(apple_array);
263*43a90889SApple OSS Distributions 
264*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, "tools/print_apple_array_hardened_heap");
265*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, SECURITY_CONFIG_KEY);
266*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(found, "Found " SECURITY_CONFIG_KEY " in apple array");
267*43a90889SApple OSS Distributions 
268*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(apple_array_val & SECURITY_CONFIG_HARDENED_HEAP_ENTRY, "Hardened-heap is enabled");
269*43a90889SApple OSS Distributions 	free(apple_array);
270*43a90889SApple OSS Distributions }
271*43a90889SApple OSS Distributions 
272*43a90889SApple OSS Distributions 
273*43a90889SApple OSS Distributions T_DECL(libmalloc_hardened_binary_absent,
274*43a90889SApple OSS Distributions     "hardened binary flags do not show up in apple array for normal third party processes",
275*43a90889SApple OSS Distributions     T_META_ASROOT(false))
276*43a90889SApple OSS Distributions {
277*43a90889SApple OSS Distributions 	uint64_t new_val, apple_array_val = 0;
278*43a90889SApple OSS Distributions 	size_t num_array_entries = 0;
279*43a90889SApple OSS Distributions 	char **apple_array;
280*43a90889SApple OSS Distributions 	bool found = false;
281*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, NULL); // todo apple_array_3p?
282*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, HARDENED_RUNTIME_KEY);
283*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(!found, "Did not find " HARDENED_RUNTIME_KEY " in apple array");
284*43a90889SApple OSS Distributions 	free(apple_array);
285*43a90889SApple OSS Distributions }
286*43a90889SApple OSS Distributions 
287*43a90889SApple OSS Distributions T_DECL(libmalloc_experiment,
288*43a90889SApple OSS Distributions     "libmalloc experiment flags show up in apple array if we're doing an experiment",
289*43a90889SApple OSS Distributions     T_META_ASROOT(false))
290*43a90889SApple OSS Distributions {
291*43a90889SApple OSS Distributions 	uint64_t new_val, apple_array_val = 0;
292*43a90889SApple OSS Distributions 	size_t num_array_entries = 0;
293*43a90889SApple OSS Distributions 	char **apple_array;
294*43a90889SApple OSS Distributions 	bool found = false;
295*43a90889SApple OSS Distributions 
296*43a90889SApple OSS Distributions 	if (running_as_root()) {
297*43a90889SApple OSS Distributions 		drop_priv();
298*43a90889SApple OSS Distributions 	}
299*43a90889SApple OSS Distributions 	new_val = (1ULL << 63) - 1;
300*43a90889SApple OSS Distributions 	set_libmalloc_experiment(new_val);
301*43a90889SApple OSS Distributions 
302*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, NULL);
303*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, &apple_array_val, LIBMALLOC_EXPERIMENT_FACTORS_KEY);
304*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(found, "Found " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
305*43a90889SApple OSS Distributions 	T_ASSERT_EQ(apple_array_val, new_val, "Experiment value matches");
306*43a90889SApple OSS Distributions 	free(apple_array);
307*43a90889SApple OSS Distributions }
308*43a90889SApple OSS Distributions 
309*43a90889SApple OSS Distributions T_DECL(libmalloc_experiment_not_in_array,
310*43a90889SApple OSS Distributions     "libmalloc experiment flags do not show up in apple array if we're not doing an experiment",
311*43a90889SApple OSS Distributions     T_META_ASROOT(false))
312*43a90889SApple OSS Distributions {
313*43a90889SApple OSS Distributions 	size_t num_array_entries = 0;
314*43a90889SApple OSS Distributions 	char **apple_array;
315*43a90889SApple OSS Distributions 	bool found = false;
316*43a90889SApple OSS Distributions 
317*43a90889SApple OSS Distributions 	if (running_as_root()) {
318*43a90889SApple OSS Distributions 		drop_priv();
319*43a90889SApple OSS Distributions 	}
320*43a90889SApple OSS Distributions 	set_libmalloc_experiment(0);
321*43a90889SApple OSS Distributions 
322*43a90889SApple OSS Distributions 	apple_array = get_apple_array(&num_array_entries, NULL);
323*43a90889SApple OSS Distributions 	found = get_apple_array_key(apple_array, num_array_entries, NULL, LIBMALLOC_EXPERIMENT_FACTORS_KEY);
324*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(!found, "Did not find " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
325*43a90889SApple OSS Distributions 	free(apple_array);
326*43a90889SApple OSS Distributions }
327*43a90889SApple OSS Distributions #endif /* ENTITLED */
328