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