xref: /xnu-12377.61.12/tests/ipc/ipc_utils.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 #ifndef IPC_UTILS_H
2 #define IPC_UTILS_H
3 
4 #include <mach/mach.h>
5 #include <mach/message.h>
6 #include <mach/mach_error.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdbool.h>
10 #include <sys/code_signing.h>
11 
12 /*
13  * Common IPC utilities for XNU tests
14  *
15  * This header provides standardized message structures and utilities
16  * for common IPC patterns used across multiple test files.
17  */
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* Message structure for sending a single port */
24 typedef struct {
25 	mach_msg_header_t header;
26 	mach_msg_body_t body;
27 	mach_msg_port_descriptor_t port;
28 } ipc_single_port_msg_t;
29 
30 /* Message structure for sending multiple ports in an array */
31 typedef struct {
32 	mach_msg_header_t header;
33 	mach_msg_body_t body;
34 	mach_msg_ool_ports_descriptor_t ports_descriptor;
35 } ipc_port_array_msg_t;
36 
37 /* Generic message structure with trailer for receiving */
38 typedef struct {
39 	mach_msg_header_t header;
40 	mach_msg_body_t body;
41 	union {
42 		/* Single port descriptor */
43 		struct {
44 			mach_msg_port_descriptor_t port;
45 		} single;
46 
47 		/* Port array descriptor */
48 		struct {
49 			mach_msg_ool_ports_descriptor_t ports_descriptor;
50 		} array;
51 	} data;
52 	mach_msg_max_trailer_t trailer;
53 } ipc_receive_msg_t;
54 
55 /*
56  * Port Management Functions
57  */
58 
59 /* Create a new receive port with send right */
60 mach_port_t ipc_create_receive_port(void);
61 
62 /* Create a receive port with specific options */
63 mach_port_t ipc_create_receive_port_with_options(uint32_t mpo_flags);
64 
65 /* Safely deallocate a port */
66 void ipc_deallocate_port(mach_port_t port);
67 
68 /* Insert a send right to a receive right */
69 kern_return_t ipc_insert_send_right(mach_port_t receive_port);
70 
71 /*
72  * Single Port Messaging Functions
73  */
74 
75 /* Send a single port to destination */
76 kern_return_t ipc_send_port(mach_port_t destination, mach_port_t port,
77     mach_msg_type_name_t disposition);
78 
79 /* Receive a single port from destination */
80 kern_return_t ipc_receive_port(mach_port_t destination, mach_port_t *port);
81 
82 /*
83  * Port Array Messaging Functions
84  */
85 
86 /* Send an array of ports to destination */
87 kern_return_t ipc_send_port_array(mach_port_t destination,
88     mach_port_t *ports, mach_msg_type_number_t count,
89     mach_msg_type_name_t disposition);
90 
91 /* Receive an array of ports from destination */
92 kern_return_t ipc_receive_port_array(mach_port_t destination,
93     mach_port_t **ports, mach_msg_type_number_t *count);
94 
95 /*
96  * Generic Messaging Functions
97  */
98 
99 /* Send a pre-constructed message */
100 kern_return_t ipc_send_message(mach_msg_header_t *msg);
101 
102 /* Receive a message into a pre-allocated buffer */
103 kern_return_t ipc_receive_message(mach_port_t destination, ipc_receive_msg_t *msg,
104     mach_msg_size_t max_size);
105 
106 /*
107  * Security and Code Signing Utilities
108  */
109 
110 /* Check if IPC hardening is disabled (CS_CONFIG_GET_OUT_OF_MY_WAY) */
111 bool ipc_hardening_disabled(void);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* IPC_UTILS_H */
118