1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
3*1b191cb5SApple OSS Distributions #include <mach/mach.h>
4*1b191cb5SApple OSS Distributions #include <mach/task_info.h>
5*1b191cb5SApple OSS Distributions #include <mach/vm_region.h>
6*1b191cb5SApple OSS Distributions #include <mach/mach_vm.h>
7*1b191cb5SApple OSS Distributions #include <sys/kern_sysctl.h>
8*1b191cb5SApple OSS Distributions #include <errno.h>
9*1b191cb5SApple OSS Distributions
10*1b191cb5SApple OSS Distributions T_GLOBAL_META(
11*1b191cb5SApple OSS Distributions T_META_NAMESPACE("xnu.vm"),
12*1b191cb5SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
13*1b191cb5SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"));
14*1b191cb5SApple OSS Distributions
15*1b191cb5SApple OSS Distributions static const char* g_sysctl_name = "vm.get_owned_vmobjects";
16*1b191cb5SApple OSS Distributions
17*1b191cb5SApple OSS Distributions mach_port_t get_corpse(void);
18*1b191cb5SApple OSS Distributions
19*1b191cb5SApple OSS Distributions mach_port_t
get_corpse()20*1b191cb5SApple OSS Distributions get_corpse()
21*1b191cb5SApple OSS Distributions {
22*1b191cb5SApple OSS Distributions kern_return_t kr;
23*1b191cb5SApple OSS Distributions mach_port_t corpse_port;
24*1b191cb5SApple OSS Distributions
25*1b191cb5SApple OSS Distributions kr = task_generate_corpse(mach_task_self(), &corpse_port);
26*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
27*1b191cb5SApple OSS Distributions mach_error("task_generate_corpse failed", kr);
28*1b191cb5SApple OSS Distributions corpse_port = MACH_PORT_NULL;
29*1b191cb5SApple OSS Distributions
30*1b191cb5SApple OSS Distributions switch (kr) {
31*1b191cb5SApple OSS Distributions case KERN_NOT_SUPPORTED:
32*1b191cb5SApple OSS Distributions case KERN_FAILURE:
33*1b191cb5SApple OSS Distributions case KERN_RESOURCE_SHORTAGE:
34*1b191cb5SApple OSS Distributions break;
35*1b191cb5SApple OSS Distributions default:
36*1b191cb5SApple OSS Distributions /* convert to KERN_FAILURE after logging to catch other rc codes */
37*1b191cb5SApple OSS Distributions /* and trigger test failure */
38*1b191cb5SApple OSS Distributions kr = KERN_FAILURE;
39*1b191cb5SApple OSS Distributions break;
40*1b191cb5SApple OSS Distributions }
41*1b191cb5SApple OSS Distributions }
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions /* anything other than KERN_FAILURE is valid */
44*1b191cb5SApple OSS Distributions T_EXPECT_NE(kr, KERN_FAILURE, "corpse creation\n");
45*1b191cb5SApple OSS Distributions
46*1b191cb5SApple OSS Distributions return corpse_port;
47*1b191cb5SApple OSS Distributions }
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributions static void
main_test(void)50*1b191cb5SApple OSS Distributions main_test(void)
51*1b191cb5SApple OSS Distributions {
52*1b191cb5SApple OSS Distributions int ret;
53*1b191cb5SApple OSS Distributions mach_port_name_t task_name;
54*1b191cb5SApple OSS Distributions vmobject_list_output_t out_buffer;
55*1b191cb5SApple OSS Distributions size_t out_size;
56*1b191cb5SApple OSS Distributions size_t output_size;
57*1b191cb5SApple OSS Distributions const vm_size_t tmp_size = 16 * 1024 * 1024; /* arbitrary size */
58*1b191cb5SApple OSS Distributions vm_address_t tmp_buf;
59*1b191cb5SApple OSS Distributions vm_address_t tmp_buf2;
60*1b191cb5SApple OSS Distributions mach_vm_size_t addr_size;
61*1b191cb5SApple OSS Distributions mach_vm_address_t addr;
62*1b191cb5SApple OSS Distributions kern_return_t kr;
63*1b191cb5SApple OSS Distributions mach_port_t __self = mach_task_self();
64*1b191cb5SApple OSS Distributions vm_region_submap_info_data_64_t regionInfo;
65*1b191cb5SApple OSS Distributions uint32_t nestingDepth;
66*1b191cb5SApple OSS Distributions mach_msg_type_number_t count;
67*1b191cb5SApple OSS Distributions
68*1b191cb5SApple OSS Distributions /* allocate a temporary buffer */
69*1b191cb5SApple OSS Distributions kr = vm_allocate(__self, &tmp_buf, tmp_size, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);
70*1b191cb5SApple OSS Distributions T_QUIET;
71*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "vm_allocate(%zu) error 0x%x (%s)",
72*1b191cb5SApple OSS Distributions (size_t) tmp_size, kr, mach_error_string(kr));
73*1b191cb5SApple OSS Distributions T_QUIET;
74*1b191cb5SApple OSS Distributions T_EXPECT_NE(tmp_buf, (vm_address_t) 0, "failed to allocate temporary purgable buffer\n");
75*1b191cb5SApple OSS Distributions
76*1b191cb5SApple OSS Distributions kr = vm_allocate(__self, &tmp_buf2, tmp_size, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);
77*1b191cb5SApple OSS Distributions T_QUIET;
78*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "vm_allocate(%zu) error 0x%x (%s)",
79*1b191cb5SApple OSS Distributions (size_t) tmp_size, kr, mach_error_string(kr));
80*1b191cb5SApple OSS Distributions T_QUIET;
81*1b191cb5SApple OSS Distributions T_EXPECT_NE(tmp_buf2, (vm_address_t) 0, "failed to allocate temporary purgable buffer\n");
82*1b191cb5SApple OSS Distributions
83*1b191cb5SApple OSS Distributions /* expected failures */
84*1b191cb5SApple OSS Distributions out_size = tmp_size;
85*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, NULL, 0, NULL, 0);
86*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, -1, "expected failure with 0 parameters\n");
87*1b191cb5SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "expected EINVAL with 0 parameters\n");
88*1b191cb5SApple OSS Distributions
89*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, (void*) tmp_buf, &out_size, NULL, 0);
90*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, -1, "expected failure with no new parameters\n");
91*1b191cb5SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "expected EINVAL with 0 new parameters\n");
92*1b191cb5SApple OSS Distributions
93*1b191cb5SApple OSS Distributions out_size = tmp_size;
94*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, NULL, 0, (void*) tmp_buf, out_size);
95*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, -1, "expected failure with no old parameters\n");
96*1b191cb5SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "expected EINVAL with 0 old parameters\n");
97*1b191cb5SApple OSS Distributions
98*1b191cb5SApple OSS Distributions task_name = MACH_PORT_NULL;
99*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, (void*) tmp_buf, &out_size, &task_name, sizeof(task_name));
100*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, -1, "expected failure with task_name == MACH_PORT_NULL in new parameters\n");
101*1b191cb5SApple OSS Distributions T_EXPECT_EQ(errno, ESRCH, "expected ESRCH with invalid task port name\n");
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions out_size = 0;
104*1b191cb5SApple OSS Distributions task_name = __self;
105*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, (void*) tmp_buf, &out_size, &task_name, sizeof(task_name));
106*1b191cb5SApple OSS Distributions T_QUIET;
107*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, -1, "expected failure with out_size == 0\n");
108*1b191cb5SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "expected EINVAL with 0 output size and valid pointer\n");
109*1b191cb5SApple OSS Distributions
110*1b191cb5SApple OSS Distributions /* we should get the number of entries we should allocate for */
111*1b191cb5SApple OSS Distributions out_size = 0;
112*1b191cb5SApple OSS Distributions output_size = 0;
113*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, NULL, &out_size, &task_name, sizeof(task_name));
114*1b191cb5SApple OSS Distributions T_QUIET;
115*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "failed getting the number of entries\n");
116*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_size, 2 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "expected one entry\n");
117*1b191cb5SApple OSS Distributions
118*1b191cb5SApple OSS Distributions /* calculcate and allocate the proper sized output buffer */
119*1b191cb5SApple OSS Distributions output_size = out_size;
120*1b191cb5SApple OSS Distributions out_buffer = (vmobject_list_output_t)calloc(output_size, 1);
121*1b191cb5SApple OSS Distributions T_QUIET;
122*1b191cb5SApple OSS Distributions T_EXPECT_NE(out_buffer, NULL, "failed to allocate the output buffer for sysctlbyname\n");
123*1b191cb5SApple OSS Distributions
124*1b191cb5SApple OSS Distributions /* get the truncated list for the current process */
125*1b191cb5SApple OSS Distributions memset(out_buffer, 0, output_size);
126*1b191cb5SApple OSS Distributions out_size = 1 * sizeof(vm_object_query_data_t) + sizeof(int64_t);
127*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, out_buffer, &out_size, &task_name, sizeof(task_name));
128*1b191cb5SApple OSS Distributions
129*1b191cb5SApple OSS Distributions T_QUIET;
130*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "sysctlbyname failed\n");
131*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_size, 1 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "sysctl return size is incorrect\n");
132*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_buffer->entries, 1ULL, "should have 1 vm object\n");
133*1b191cb5SApple OSS Distributions T_EXPECT_NE(out_buffer->data[0].object_id, 0ULL, "vm_object_id should not be 0\n");
134*1b191cb5SApple OSS Distributions
135*1b191cb5SApple OSS Distributions /* get the list for the current process with an overly large size */
136*1b191cb5SApple OSS Distributions out_size = SIZE_MAX;
137*1b191cb5SApple OSS Distributions memset(out_buffer, 0, output_size);
138*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, out_buffer, &out_size, &task_name, sizeof(task_name));
139*1b191cb5SApple OSS Distributions
140*1b191cb5SApple OSS Distributions T_QUIET;
141*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "sysctlbyname failed\n");
142*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_size, 2 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "sysctl return size is incorrect\n");
143*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_buffer->entries, 2ULL, "should have 2 vm objects\n");
144*1b191cb5SApple OSS Distributions T_EXPECT_NE(out_buffer->data[0].object_id, 0ULL, "vm_object_id should not be 0\n");
145*1b191cb5SApple OSS Distributions
146*1b191cb5SApple OSS Distributions /* get the list for the current process with the correct output size */
147*1b191cb5SApple OSS Distributions out_size = output_size;
148*1b191cb5SApple OSS Distributions memset(out_buffer, 0, output_size);
149*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, out_buffer, &out_size, &task_name, sizeof(task_name));
150*1b191cb5SApple OSS Distributions
151*1b191cb5SApple OSS Distributions T_QUIET;
152*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "sysctlbyname failed\n");
153*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_size, 2 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "sysctl return size is incorrect\n");
154*1b191cb5SApple OSS Distributions T_EXPECT_EQ(out_buffer->entries, 2ULL, "should have 2 vm objects\n");
155*1b191cb5SApple OSS Distributions T_EXPECT_NE(out_buffer->data[0].object_id, 0ULL, "vm_object_id should not be 0\n");
156*1b191cb5SApple OSS Distributions
157*1b191cb5SApple OSS Distributions addr = tmp_buf;
158*1b191cb5SApple OSS Distributions addr_size = tmp_size;
159*1b191cb5SApple OSS Distributions nestingDepth = UINT_MAX;
160*1b191cb5SApple OSS Distributions count = VM_REGION_SUBMAP_INFO_V2_COUNT_64;
161*1b191cb5SApple OSS Distributions kr = mach_vm_region_recurse(__self, &addr, &addr_size, &nestingDepth, (vm_region_info_t)®ionInfo, &count);
162*1b191cb5SApple OSS Distributions T_QUIET;
163*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "mach_vm_region_recurse(%zu) error 0x%x (%s)\n",
164*1b191cb5SApple OSS Distributions tmp_size, kr, mach_error_string(kr));
165*1b191cb5SApple OSS Distributions T_EXPECT_EQ(regionInfo.object_id_full, out_buffer->data[0].object_id, "object_id_full does not match out_buffer->object[0]\n");
166*1b191cb5SApple OSS Distributions
167*1b191cb5SApple OSS Distributions addr = tmp_buf2;
168*1b191cb5SApple OSS Distributions addr_size = tmp_size;
169*1b191cb5SApple OSS Distributions nestingDepth = UINT_MAX;
170*1b191cb5SApple OSS Distributions count = VM_REGION_SUBMAP_INFO_V2_COUNT_64;
171*1b191cb5SApple OSS Distributions kr = mach_vm_region_recurse(__self, &addr, &addr_size, &nestingDepth, (vm_region_info_t)®ionInfo, &count);
172*1b191cb5SApple OSS Distributions T_QUIET;
173*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "mach_vm_region_recurse(%zu) error 0x%x (%s)\n",
174*1b191cb5SApple OSS Distributions tmp_size, kr, mach_error_string(kr));
175*1b191cb5SApple OSS Distributions T_EXPECT_EQ(regionInfo.object_id_full, out_buffer->data[1].object_id, "object_id_full does not match out_buffer->object[1]\n");
176*1b191cb5SApple OSS Distributions
177*1b191cb5SApple OSS Distributions /* corpse */
178*1b191cb5SApple OSS Distributions {
179*1b191cb5SApple OSS Distributions mach_port_t corpse_port = get_corpse();
180*1b191cb5SApple OSS Distributions
181*1b191cb5SApple OSS Distributions /* corpse_port can be a valid NULL if out of resources, corpse limit, or corpses disabled */
182*1b191cb5SApple OSS Distributions if (corpse_port != MACH_PORT_NULL) {
183*1b191cb5SApple OSS Distributions vmobject_list_output_t corpse_out_buffer;
184*1b191cb5SApple OSS Distributions size_t corpse_out_size;
185*1b191cb5SApple OSS Distributions size_t corpse_output_size;
186*1b191cb5SApple OSS Distributions
187*1b191cb5SApple OSS Distributions /* we should get the number of entries we should allocate for */
188*1b191cb5SApple OSS Distributions corpse_out_size = 0;
189*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, NULL, &corpse_out_size, &corpse_port, sizeof(corpse_port));
190*1b191cb5SApple OSS Distributions
191*1b191cb5SApple OSS Distributions T_QUIET;
192*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "failed getting the number of entries for corpse\n");
193*1b191cb5SApple OSS Distributions T_EXPECT_EQ(corpse_out_size, out_size, "corpse output size matchrd the parent process\n");
194*1b191cb5SApple OSS Distributions T_EXPECT_EQ(corpse_out_size, 2 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "corpse expected one entry\n");
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions /* calculcate and allocate the proper sized output buffer */
197*1b191cb5SApple OSS Distributions corpse_output_size = corpse_out_size;
198*1b191cb5SApple OSS Distributions corpse_out_buffer = (vmobject_list_output_t)calloc(corpse_output_size, 1);
199*1b191cb5SApple OSS Distributions T_QUIET;
200*1b191cb5SApple OSS Distributions T_EXPECT_NE(corpse_out_buffer, NULL, "failed to allocate the output buffer for sysctlbyname for corpse\n");
201*1b191cb5SApple OSS Distributions
202*1b191cb5SApple OSS Distributions /* get the list for the current process */
203*1b191cb5SApple OSS Distributions corpse_out_size = corpse_output_size;
204*1b191cb5SApple OSS Distributions memset(corpse_out_buffer, 0, corpse_output_size);
205*1b191cb5SApple OSS Distributions ret = sysctlbyname(g_sysctl_name, corpse_out_buffer, &corpse_out_size, &corpse_port, sizeof(corpse_port));
206*1b191cb5SApple OSS Distributions
207*1b191cb5SApple OSS Distributions int rc = memcmp(corpse_out_buffer, out_buffer, corpse_out_size);
208*1b191cb5SApple OSS Distributions
209*1b191cb5SApple OSS Distributions T_QUIET;
210*1b191cb5SApple OSS Distributions T_EXPECT_EQ(ret, 0, "corpse sysctlbyname failed\n");
211*1b191cb5SApple OSS Distributions T_EXPECT_EQ(rc, 0, "corpse vmobjects should match parent vmobjects\n");
212*1b191cb5SApple OSS Distributions T_EXPECT_EQ(corpse_out_size, 2 * sizeof(vm_object_query_data_t) + sizeof(int64_t), "corpse sysctl return size is incorrect\n");
213*1b191cb5SApple OSS Distributions T_EXPECT_EQ(corpse_out_buffer->entries, 2ULL, "corpse should have 2 vm objects\n");
214*1b191cb5SApple OSS Distributions T_EXPECT_NE(corpse_out_buffer->data[0].object_id, 0ULL, "corpse vm_object_id should not be 0\n");
215*1b191cb5SApple OSS Distributions
216*1b191cb5SApple OSS Distributions free(corpse_out_buffer);
217*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), corpse_port);
218*1b191cb5SApple OSS Distributions }
219*1b191cb5SApple OSS Distributions }
220*1b191cb5SApple OSS Distributions
221*1b191cb5SApple OSS Distributions kr = vm_deallocate(__self, tmp_buf, tmp_size);
222*1b191cb5SApple OSS Distributions T_QUIET;
223*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "vm_deallocate(%zu) error 0x%x (%s)\n",
224*1b191cb5SApple OSS Distributions tmp_size, kr, mach_error_string(kr));
225*1b191cb5SApple OSS Distributions
226*1b191cb5SApple OSS Distributions kr = vm_deallocate(__self, tmp_buf2, tmp_size);
227*1b191cb5SApple OSS Distributions T_QUIET;
228*1b191cb5SApple OSS Distributions T_EXPECT_EQ(kr, KERN_SUCCESS, "vm_deallocate(%zu) error 0x%x (%s)\n",
229*1b191cb5SApple OSS Distributions tmp_size, kr, mach_error_string(kr));
230*1b191cb5SApple OSS Distributions
231*1b191cb5SApple OSS Distributions free(out_buffer);
232*1b191cb5SApple OSS Distributions out_buffer = NULL;
233*1b191cb5SApple OSS Distributions }
234*1b191cb5SApple OSS Distributions
235*1b191cb5SApple OSS Distributions T_DECL(test_get_vmobject_list, "Get owned vm_objects for process")
236*1b191cb5SApple OSS Distributions {
237*1b191cb5SApple OSS Distributions main_test();
238*1b191cb5SApple OSS Distributions }
239