1*c54f35caSApple OSS Distributions #include <errno.h>
2*c54f35caSApple OSS Distributions #include <stdlib.h>
3*c54f35caSApple OSS Distributions #include <libgen.h>
4*c54f35caSApple OSS Distributions #include <limits.h>
5*c54f35caSApple OSS Distributions #include <mach-o/dyld.h>
6*c54f35caSApple OSS Distributions #include <sys/types.h>
7*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
8*c54f35caSApple OSS Distributions #include <xlocale.h>
9*c54f35caSApple OSS Distributions
10*c54f35caSApple OSS Distributions #include <darwintest.h>
11*c54f35caSApple OSS Distributions #include <darwintest_utils.h>
12*c54f35caSApple OSS Distributions
13*c54f35caSApple OSS Distributions #include "drop_priv.h"
14*c54f35caSApple OSS Distributions #include "test_utils.h"
15*c54f35caSApple OSS Distributions
16*c54f35caSApple OSS Distributions #if ENTITLED
17*c54f35caSApple OSS Distributions #define SET_TREATMENT_ID set_treatment_id_entitled
18*c54f35caSApple OSS Distributions #define SET_TREATMENT_ID_DESCR "Can set treatment id with entitlement"
19*c54f35caSApple OSS Distributions #else /* ENTITLED */
20*c54f35caSApple OSS Distributions #define SET_TREATMENT_ID set_treatment_id_unentitled
21*c54f35caSApple OSS Distributions #define SET_TREATMENT_ID_DESCR "Can't set treatment id without entitlement"
22*c54f35caSApple OSS Distributions #endif /* ENTITLED */
23*c54f35caSApple OSS Distributions
24*c54f35caSApple OSS Distributions T_DECL(SET_TREATMENT_ID, "Verifies that EXPERIMENT sysctls can only be set with the entitlement", T_META_ASROOT(false))
25*c54f35caSApple OSS Distributions {
26*c54f35caSApple OSS Distributions #define TEST_STR "testing"
27*c54f35caSApple OSS Distributions #define IDENTIFIER_LENGTH 36
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions int ret;
30*c54f35caSApple OSS Distributions errno_t err;
31*c54f35caSApple OSS Distributions char val[IDENTIFIER_LENGTH + 1] = {0};
32*c54f35caSApple OSS Distributions size_t len = sizeof(val);
33*c54f35caSApple OSS Distributions char new_val[IDENTIFIER_LENGTH + 1] = {0};
34*c54f35caSApple OSS Distributions
35*c54f35caSApple OSS Distributions if (!is_development_kernel()) {
36*c54f35caSApple OSS Distributions T_SKIP("skipping test on release kernel");
37*c54f35caSApple OSS Distributions }
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions strlcpy(new_val, TEST_STR, sizeof(new_val));
40*c54f35caSApple OSS Distributions if (running_as_root()) {
41*c54f35caSApple OSS Distributions drop_priv();
42*c54f35caSApple OSS Distributions }
43*c54f35caSApple OSS Distributions
44*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, strlen(new_val));
45*c54f35caSApple OSS Distributions err = errno;
46*c54f35caSApple OSS Distributions #if ENTITLED
47*c54f35caSApple OSS Distributions len = sizeof(val);
48*c54f35caSApple OSS Distributions memset(new_val, 0, sizeof(new_val));
49*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "set kern.trial_treatment_id");
50*c54f35caSApple OSS Distributions /* Cleanup. Set it back to the empty string. */
51*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, 1);
52*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "reset kern.trial_treatment_id");
53*c54f35caSApple OSS Distributions #else
54*c54f35caSApple OSS Distributions T_ASSERT_POSIX_FAILURE(ret, EPERM, "set kern.trial_treatment_id");
55*c54f35caSApple OSS Distributions #endif /* ENTITLED */
56*c54f35caSApple OSS Distributions }
57*c54f35caSApple OSS Distributions
58*c54f35caSApple OSS Distributions #if ENTITLED
59*c54f35caSApple OSS Distributions /* Check min and max value limits on numeric factors */
60*c54f35caSApple OSS Distributions T_DECL(experiment_factor_numeric_limits,
61*c54f35caSApple OSS Distributions "Can only set factors within the legal range.",
62*c54f35caSApple OSS Distributions T_META_ASROOT(false))
63*c54f35caSApple OSS Distributions {
64*c54f35caSApple OSS Distributions #define kMinVal 5 /* The min value allowed for the testing factor. */
65*c54f35caSApple OSS Distributions #define kMaxVal 10 /* The max value allowed for the testing factor. */
66*c54f35caSApple OSS Distributions errno_t err;
67*c54f35caSApple OSS Distributions int ret;
68*c54f35caSApple OSS Distributions unsigned int current_val;
69*c54f35caSApple OSS Distributions size_t len = sizeof(current_val);
70*c54f35caSApple OSS Distributions unsigned int new_val;
71*c54f35caSApple OSS Distributions
72*c54f35caSApple OSS Distributions if (running_as_root()) {
73*c54f35caSApple OSS Distributions drop_priv();
74*c54f35caSApple OSS Distributions }
75*c54f35caSApple OSS Distributions new_val = kMinVal - 1;
76*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
77*c54f35caSApple OSS Distributions err = errno;
78*c54f35caSApple OSS Distributions T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor below range.");
79*c54f35caSApple OSS Distributions
80*c54f35caSApple OSS Distributions new_val = kMaxVal + 1;
81*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
82*c54f35caSApple OSS Distributions err = errno;
83*c54f35caSApple OSS Distributions T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor above range.");
84*c54f35caSApple OSS Distributions
85*c54f35caSApple OSS Distributions new_val = kMaxVal;
86*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
87*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at top of range.");
88*c54f35caSApple OSS Distributions
89*c54f35caSApple OSS Distributions new_val = kMinVal;
90*c54f35caSApple OSS Distributions ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
91*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at bottom of range.");
92*c54f35caSApple OSS Distributions }
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions static uint64_t original_libmalloc_experiment_value = 0;
95*c54f35caSApple OSS Distributions
96*c54f35caSApple OSS Distributions static void
reset_libmalloc_experiment()97*c54f35caSApple OSS Distributions reset_libmalloc_experiment()
98*c54f35caSApple OSS Distributions {
99*c54f35caSApple OSS Distributions int ret = sysctlbyname("kern.libmalloc_experiments", NULL, NULL, &original_libmalloc_experiment_value, sizeof(original_libmalloc_experiment_value));
100*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "reset kern.libmalloc_experiments");
101*c54f35caSApple OSS Distributions }
102*c54f35caSApple OSS Distributions
103*c54f35caSApple OSS Distributions static void
set_libmalloc_experiment(uint64_t val)104*c54f35caSApple OSS Distributions set_libmalloc_experiment(uint64_t val)
105*c54f35caSApple OSS Distributions {
106*c54f35caSApple OSS Distributions T_LOG("Setting kern.libmalloc_experiments to %llu", val);
107*c54f35caSApple OSS Distributions size_t len = sizeof(original_libmalloc_experiment_value);
108*c54f35caSApple OSS Distributions int ret = sysctlbyname("kern.libmalloc_experiments", &original_libmalloc_experiment_value, &len, &val, sizeof(val));
109*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "set kern.libmalloc_experiments");
110*c54f35caSApple OSS Distributions T_ATEND(reset_libmalloc_experiment);
111*c54f35caSApple OSS Distributions }
112*c54f35caSApple OSS Distributions
113*c54f35caSApple OSS Distributions #define PRINT_APPLE_ARRAY_TOOL "tools/print_apple_array"
114*c54f35caSApple OSS Distributions /*
115*c54f35caSApple OSS Distributions * Spawns a new binary and returns the contents of its apple array
116*c54f35caSApple OSS Distributions * (after libsystem initialization).
117*c54f35caSApple OSS Distributions */
118*c54f35caSApple OSS Distributions static char **
get_apple_array(size_t * num_array_entries)119*c54f35caSApple OSS Distributions get_apple_array(size_t *num_array_entries)
120*c54f35caSApple OSS Distributions {
121*c54f35caSApple OSS Distributions int ret;
122*c54f35caSApple OSS Distributions char stdout_path[MAXPATHLEN] = "apple_array.txt";
123*c54f35caSApple OSS Distributions dt_resultfile(stdout_path, MAXPATHLEN);
124*c54f35caSApple OSS Distributions int exit_status = 0, signum = 0;
125*c54f35caSApple OSS Distributions char binary_path[MAXPATHLEN], binary_dir[MAXPATHLEN];
126*c54f35caSApple OSS Distributions char *char_ret;
127*c54f35caSApple OSS Distributions const static size_t kMaxNumArguments = 256;
128*c54f35caSApple OSS Distributions size_t linecap = 0;
129*c54f35caSApple OSS Distributions ssize_t linelen = 0;
130*c54f35caSApple OSS Distributions char **apple_array;
131*c54f35caSApple OSS Distributions char **line = NULL;
132*c54f35caSApple OSS Distributions size_t num_lines = 0;
133*c54f35caSApple OSS Distributions FILE *stdout_f = NULL;
134*c54f35caSApple OSS Distributions uint32_t name_size = MAXPATHLEN;
135*c54f35caSApple OSS Distributions
136*c54f35caSApple OSS Distributions ret = _NSGetExecutablePath(binary_path, &name_size);
137*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(ret, 0, "_NSGetExecutablePath");
138*c54f35caSApple OSS Distributions char_ret = dirname_r(binary_path, binary_dir);
139*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_TRUE(char_ret != NULL, "dirname_r");
140*c54f35caSApple OSS Distributions snprintf(binary_path, MAXPATHLEN, "%s/%s", binary_dir, PRINT_APPLE_ARRAY_TOOL);
141*c54f35caSApple OSS Distributions
142*c54f35caSApple OSS Distributions char *launch_tool_args[] = {
143*c54f35caSApple OSS Distributions binary_path,
144*c54f35caSApple OSS Distributions NULL
145*c54f35caSApple OSS Distributions };
146*c54f35caSApple OSS Distributions pid_t child_pid;
147*c54f35caSApple OSS Distributions ret = dt_launch_tool(&child_pid, launch_tool_args, false, stdout_path, NULL);
148*c54f35caSApple OSS Distributions T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, "dt_launch_tool: %s", binary_path);
149*c54f35caSApple OSS Distributions
150*c54f35caSApple OSS Distributions ret = dt_waitpid(child_pid, &exit_status, &signum, 60 * 5);
151*c54f35caSApple OSS Distributions T_ASSERT_EQ(ret, 1, "dt_waitpid");
152*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
153*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
154*c54f35caSApple OSS Distributions
155*c54f35caSApple OSS Distributions stdout_f = fopen(stdout_path, "r");
156*c54f35caSApple OSS Distributions T_WITH_ERRNO; T_ASSERT_NOTNULL(stdout_f, "open(%s)", stdout_path);
157*c54f35caSApple OSS Distributions apple_array = calloc(kMaxNumArguments, sizeof(char *));
158*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(apple_array, "calloc: %lu\n", sizeof(char *) * kMaxNumArguments);
159*c54f35caSApple OSS Distributions while (num_lines < kMaxNumArguments) {
160*c54f35caSApple OSS Distributions line = &(apple_array[num_lines++]);
161*c54f35caSApple OSS Distributions linecap = 0;
162*c54f35caSApple OSS Distributions linelen = getline(line, &linecap, stdout_f);
163*c54f35caSApple OSS Distributions if (linelen == -1) {
164*c54f35caSApple OSS Distributions break;
165*c54f35caSApple OSS Distributions }
166*c54f35caSApple OSS Distributions }
167*c54f35caSApple OSS Distributions *num_array_entries = num_lines - 1;
168*c54f35caSApple OSS Distributions
169*c54f35caSApple OSS Distributions ret = fclose(stdout_f);
170*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "fclose(%s)", stdout_path);
171*c54f35caSApple OSS Distributions
172*c54f35caSApple OSS Distributions return apple_array;
173*c54f35caSApple OSS Distributions }
174*c54f35caSApple OSS Distributions
175*c54f35caSApple OSS Distributions #define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="
176*c54f35caSApple OSS Distributions
177*c54f35caSApple OSS Distributions /*
178*c54f35caSApple OSS Distributions * Get the value of the MallocExperiment key in the apple array.
179*c54f35caSApple OSS Distributions * Returns true iff the key is present.
180*c54f35caSApple OSS Distributions */
181*c54f35caSApple OSS Distributions static bool
get_libmalloc_experiment_factors(char ** apple_array,size_t num_array_entries,uint64_t * factors)182*c54f35caSApple OSS Distributions get_libmalloc_experiment_factors(char **apple_array, size_t num_array_entries, uint64_t *factors)
183*c54f35caSApple OSS Distributions {
184*c54f35caSApple OSS Distributions bool found = false;
185*c54f35caSApple OSS Distributions for (size_t i = 0; i < num_array_entries; i++) {
186*c54f35caSApple OSS Distributions char *str = apple_array[i];
187*c54f35caSApple OSS Distributions if (strstr(str, LIBMALLOC_EXPERIMENT_FACTORS_KEY)) {
188*c54f35caSApple OSS Distributions found = true;
189*c54f35caSApple OSS Distributions if (factors != NULL) {
190*c54f35caSApple OSS Distributions str = strchr(str, '=');
191*c54f35caSApple OSS Distributions T_ASSERT_NOTNULL(str, "skip over =");
192*c54f35caSApple OSS Distributions ++str;
193*c54f35caSApple OSS Distributions *factors = strtoull_l(str, NULL, 16, NULL);
194*c54f35caSApple OSS Distributions }
195*c54f35caSApple OSS Distributions break;
196*c54f35caSApple OSS Distributions }
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions return found;
199*c54f35caSApple OSS Distributions }
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions T_DECL(libmalloc_experiment,
202*c54f35caSApple OSS Distributions "libmalloc experiment flags show up in apple array if we're doing an experiment",
203*c54f35caSApple OSS Distributions T_META_ASROOT(false))
204*c54f35caSApple OSS Distributions {
205*c54f35caSApple OSS Distributions uint64_t new_val, apple_array_val = 0;
206*c54f35caSApple OSS Distributions size_t num_array_entries = 0;
207*c54f35caSApple OSS Distributions char **apple_array;
208*c54f35caSApple OSS Distributions bool found = false;
209*c54f35caSApple OSS Distributions
210*c54f35caSApple OSS Distributions if (running_as_root()) {
211*c54f35caSApple OSS Distributions drop_priv();
212*c54f35caSApple OSS Distributions }
213*c54f35caSApple OSS Distributions new_val = (1ULL << 63) - 1;
214*c54f35caSApple OSS Distributions set_libmalloc_experiment(new_val);
215*c54f35caSApple OSS Distributions
216*c54f35caSApple OSS Distributions apple_array = get_apple_array(&num_array_entries);
217*c54f35caSApple OSS Distributions found = get_libmalloc_experiment_factors(apple_array, num_array_entries, &apple_array_val);
218*c54f35caSApple OSS Distributions T_ASSERT_TRUE(found, "Found " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
219*c54f35caSApple OSS Distributions T_ASSERT_EQ(apple_array_val, new_val, "Experiment value matches");
220*c54f35caSApple OSS Distributions free(apple_array);
221*c54f35caSApple OSS Distributions }
222*c54f35caSApple OSS Distributions
223*c54f35caSApple OSS Distributions T_DECL(libmalloc_experiment_not_in_array,
224*c54f35caSApple OSS Distributions "libmalloc experiment flags do not show up in apple array if we're not doing an experiment",
225*c54f35caSApple OSS Distributions T_META_ASROOT(false))
226*c54f35caSApple OSS Distributions {
227*c54f35caSApple OSS Distributions size_t num_array_entries = 0;
228*c54f35caSApple OSS Distributions char **apple_array;
229*c54f35caSApple OSS Distributions bool found = false;
230*c54f35caSApple OSS Distributions
231*c54f35caSApple OSS Distributions if (running_as_root()) {
232*c54f35caSApple OSS Distributions drop_priv();
233*c54f35caSApple OSS Distributions }
234*c54f35caSApple OSS Distributions set_libmalloc_experiment(0);
235*c54f35caSApple OSS Distributions
236*c54f35caSApple OSS Distributions apple_array = get_apple_array(&num_array_entries);
237*c54f35caSApple OSS Distributions found = get_libmalloc_experiment_factors(apple_array, num_array_entries, NULL);
238*c54f35caSApple OSS Distributions T_ASSERT_TRUE(!found, "Did not find " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
239*c54f35caSApple OSS Distributions free(apple_array);
240*c54f35caSApple OSS Distributions }
241*c54f35caSApple OSS Distributions #endif /* ENTITLED */
242