xref: /xnu-10002.41.9/tests/test_conclave_spawn.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions  *
4*699cd480SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions  *
6*699cd480SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions  *
15*699cd480SApple OSS Distributions  * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions  *
18*699cd480SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions  * limitations under the License.
25*699cd480SApple OSS Distributions  *
26*699cd480SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions  */
28*699cd480SApple OSS Distributions 
29*699cd480SApple OSS Distributions #include <sys/sysctl.h>
30*699cd480SApple OSS Distributions #include <mach/mach.h>
31*699cd480SApple OSS Distributions #include <mach/exclaves.h>
32*699cd480SApple OSS Distributions #include <mach/exclaves_l4.h>
33*699cd480SApple OSS Distributions #include <libgen.h>
34*699cd480SApple OSS Distributions #include <darwintest.h>
35*699cd480SApple OSS Distributions #include <darwintest_utils.h>
36*699cd480SApple OSS Distributions #include <removefile.h>
37*699cd480SApple OSS Distributions #include <spawn.h>
38*699cd480SApple OSS Distributions #include <spawn_private.h>
39*699cd480SApple OSS Distributions 
40*699cd480SApple OSS Distributions T_GLOBAL_META(
41*699cd480SApple OSS Distributions 	T_META_NAMESPACE("xnu.exclaves"),
42*699cd480SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
43*699cd480SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("spawn"));
44*699cd480SApple OSS Distributions 
45*699cd480SApple OSS Distributions static int64_t
run_sysctl_test(const char * t,int64_t value)46*699cd480SApple OSS Distributions run_sysctl_test(const char *t, int64_t value)
47*699cd480SApple OSS Distributions {
48*699cd480SApple OSS Distributions 	char name[1024];
49*699cd480SApple OSS Distributions 	int64_t result = 0;
50*699cd480SApple OSS Distributions 	size_t s = sizeof(value);
51*699cd480SApple OSS Distributions 	int rc;
52*699cd480SApple OSS Distributions 
53*699cd480SApple OSS Distributions 	snprintf(name, sizeof(name), "debug.test.%s", t);
54*699cd480SApple OSS Distributions 	rc = sysctlbyname(name, &result, &s, &value, s);
55*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", name);
56*699cd480SApple OSS Distributions 	return result;
57*699cd480SApple OSS Distributions }
58*699cd480SApple OSS Distributions 
59*699cd480SApple OSS Distributions T_DECL(conclave_spawn, "Test the spawn api for conclave")
60*699cd480SApple OSS Distributions {
61*699cd480SApple OSS Distributions 	posix_spawnattr_t attrs;
62*699cd480SApple OSS Distributions 	char *test_prog_name = "conclave_process";
63*699cd480SApple OSS Distributions 	char *test_prog_path = "./conclave_process";
64*699cd480SApple OSS Distributions 	char *child_args[3];
65*699cd480SApple OSS Distributions 	char *conclave_name = "com.apple.conclave.test1";
66*699cd480SApple OSS Distributions 	pid_t child_pid;
67*699cd480SApple OSS Distributions 	int64_t r = run_sysctl_test("exclaves_hello_exclave_test", 0);
68*699cd480SApple OSS Distributions 
69*699cd480SApple OSS Distributions 	if (r == -1) {
70*699cd480SApple OSS Distributions 		T_SKIP("Exclave not available");
71*699cd480SApple OSS Distributions 		T_END;
72*699cd480SApple OSS Distributions 	}
73*699cd480SApple OSS Distributions 
74*699cd480SApple OSS Distributions 	/* Initialize posix_spawn attributes */
75*699cd480SApple OSS Distributions 	posix_spawnattr_init(&attrs);
76*699cd480SApple OSS Distributions 
77*699cd480SApple OSS Distributions 	int err = posix_spawnattr_set_conclave_id_np(&attrs, conclave_name);
78*699cd480SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_set_conclave_id_np");
79*699cd480SApple OSS Distributions 
80*699cd480SApple OSS Distributions 	child_args[0] = test_prog_name;
81*699cd480SApple OSS Distributions 	child_args[1] = conclave_name;
82*699cd480SApple OSS Distributions 	child_args[2] = NULL;
83*699cd480SApple OSS Distributions 
84*699cd480SApple OSS Distributions 	err = posix_spawn(&child_pid, test_prog_path, NULL, &attrs, &child_args[0], NULL);
85*699cd480SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawn");
86*699cd480SApple OSS Distributions 
87*699cd480SApple OSS Distributions 	T_LOG("Child pid is %d\n", child_pid);
88*699cd480SApple OSS Distributions 
89*699cd480SApple OSS Distributions 	int child_status;
90*699cd480SApple OSS Distributions 	/* Wait for child and check for return value */
91*699cd480SApple OSS Distributions 	if (-1 == waitpid(child_pid, &child_status, 0)) {
92*699cd480SApple OSS Distributions 		T_FAIL("wait4: child mia with errno %d", errno);
93*699cd480SApple OSS Distributions 	}
94*699cd480SApple OSS Distributions 
95*699cd480SApple OSS Distributions 	if (WIFEXITED(child_status)) {
96*699cd480SApple OSS Distributions 		T_EXPECT_EQ_INT(WEXITSTATUS(child_status), 0, "Check if child returned zero on exit");
97*699cd480SApple OSS Distributions 	} else {
98*699cd480SApple OSS Distributions 		T_FAIL("Child %d did not exit normally\n", child_pid);
99*699cd480SApple OSS Distributions 	}
100*699cd480SApple OSS Distributions 	T_END;
101*699cd480SApple OSS Distributions }
102