xref: /xnu-12377.61.12/tests/mach_exception_reply.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1*4d495c6eSApple OSS Distributions #include <darwintest.h>
2*4d495c6eSApple OSS Distributions 
3*4d495c6eSApple OSS Distributions #include <pthread.h>
4*4d495c6eSApple OSS Distributions #include <setjmp.h>
5*4d495c6eSApple OSS Distributions #include <signal.h>
6*4d495c6eSApple OSS Distributions #include <stdlib.h>
7*4d495c6eSApple OSS Distributions #include <unistd.h>
8*4d495c6eSApple OSS Distributions #include <mach/mach.h>
9*4d495c6eSApple OSS Distributions #include <pthread/qos_private.h>
10*4d495c6eSApple OSS Distributions #include <mach/mach_voucher.h>
11*4d495c6eSApple OSS Distributions #include <bank/bank_types.h>
12*4d495c6eSApple OSS Distributions 
13*4d495c6eSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
14*4d495c6eSApple OSS Distributions     T_META_NAMESPACE("xnu.ipc"),
15*4d495c6eSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
16*4d495c6eSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("IPC"));
17*4d495c6eSApple OSS Distributions 
18*4d495c6eSApple OSS Distributions #define MSG      1024
19*4d495c6eSApple OSS Distributions #define PG_ALLOC 4096
20*4d495c6eSApple OSS Distributions 
21*4d495c6eSApple OSS Distributions typedef enum {
22*4d495c6eSApple OSS Distributions 	ReplyWithNoError,
23*4d495c6eSApple OSS Distributions 	ReplyWithReplyPort,
24*4d495c6eSApple OSS Distributions 	ReplyWithReplyPortMove,
25*4d495c6eSApple OSS Distributions 	ReplyWithReplyPortCplxBit,
26*4d495c6eSApple OSS Distributions 	ReplyWithReplyPortMoveCplxBit,
27*4d495c6eSApple OSS Distributions 	ReplyWithPortDesc,
28*4d495c6eSApple OSS Distributions 	ReplyWithOOLDesc,
29*4d495c6eSApple OSS Distributions 	ReplyWithVoucher,
30*4d495c6eSApple OSS Distributions 	ReplyWithVoucherGarbage
31*4d495c6eSApple OSS Distributions } ReplyType;
32*4d495c6eSApple OSS Distributions 
33*4d495c6eSApple OSS Distributions struct exc_thread_arg {
34*4d495c6eSApple OSS Distributions 	ReplyType    rt;
35*4d495c6eSApple OSS Distributions 	mach_port_t  port;
36*4d495c6eSApple OSS Distributions };
37*4d495c6eSApple OSS Distributions 
38*4d495c6eSApple OSS Distributions static const char *
reply_type_str(ReplyType rt)39*4d495c6eSApple OSS Distributions reply_type_str(ReplyType rt)
40*4d495c6eSApple OSS Distributions {
41*4d495c6eSApple OSS Distributions 	switch (rt) {
42*4d495c6eSApple OSS Distributions 	case ReplyWithNoError:
43*4d495c6eSApple OSS Distributions 		return "ReplyWithNoError";
44*4d495c6eSApple OSS Distributions 	case ReplyWithReplyPort:
45*4d495c6eSApple OSS Distributions 		return "ReplyWithReplyPort";
46*4d495c6eSApple OSS Distributions 	case ReplyWithReplyPortMove:
47*4d495c6eSApple OSS Distributions 		return "ReplyWithReplyPortMove";
48*4d495c6eSApple OSS Distributions 	case ReplyWithReplyPortCplxBit:
49*4d495c6eSApple OSS Distributions 		return "ReplyWithReplyPortCplxBit";
50*4d495c6eSApple OSS Distributions 	case ReplyWithReplyPortMoveCplxBit:
51*4d495c6eSApple OSS Distributions 		return "ReplyWithReplyPortMoveCplxBit";
52*4d495c6eSApple OSS Distributions 	case ReplyWithPortDesc:
53*4d495c6eSApple OSS Distributions 		return "ReplyWithPortDesc";
54*4d495c6eSApple OSS Distributions 	case ReplyWithOOLDesc:
55*4d495c6eSApple OSS Distributions 		return "ReplyWithOOLDesc";
56*4d495c6eSApple OSS Distributions 	case ReplyWithVoucher:
57*4d495c6eSApple OSS Distributions 		return "ReplyWithVoucher";
58*4d495c6eSApple OSS Distributions 	case ReplyWithVoucherGarbage:
59*4d495c6eSApple OSS Distributions 		return "ReplyWithVoucherGarbage";
60*4d495c6eSApple OSS Distributions 	}
61*4d495c6eSApple OSS Distributions }
62*4d495c6eSApple OSS Distributions 
63*4d495c6eSApple OSS Distributions static mach_voucher_t
create_task_voucher(void)64*4d495c6eSApple OSS Distributions create_task_voucher(void)
65*4d495c6eSApple OSS Distributions {
66*4d495c6eSApple OSS Distributions 	static mach_voucher_attr_recipe_data_t task_create_recipe = {
67*4d495c6eSApple OSS Distributions 		.key = MACH_VOUCHER_ATTR_KEY_BANK,
68*4d495c6eSApple OSS Distributions 		.command = MACH_VOUCHER_ATTR_BANK_CREATE,
69*4d495c6eSApple OSS Distributions 	};
70*4d495c6eSApple OSS Distributions 	mach_voucher_t voucher = MACH_PORT_NULL;
71*4d495c6eSApple OSS Distributions 	kern_return_t kr;
72*4d495c6eSApple OSS Distributions 
73*4d495c6eSApple OSS Distributions 	kr = host_create_mach_voucher(mach_host_self(),
74*4d495c6eSApple OSS Distributions 	    (mach_voucher_attr_raw_recipe_array_t)&task_create_recipe,
75*4d495c6eSApple OSS Distributions 	    sizeof(task_create_recipe),
76*4d495c6eSApple OSS Distributions 	    &voucher);
77*4d495c6eSApple OSS Distributions 
78*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_create_mach_voucher");
79*4d495c6eSApple OSS Distributions 	return voucher;
80*4d495c6eSApple OSS Distributions }
81*4d495c6eSApple OSS Distributions 
82*4d495c6eSApple OSS Distributions static void *
handle_exceptions(void * arg)83*4d495c6eSApple OSS Distributions handle_exceptions(void *arg)
84*4d495c6eSApple OSS Distributions {
85*4d495c6eSApple OSS Distributions 	struct exc_thread_arg *ta = (struct exc_thread_arg *)arg;
86*4d495c6eSApple OSS Distributions 	mach_port_t ePort = ta->port;
87*4d495c6eSApple OSS Distributions 	ReplyType reply_type = ta->rt;
88*4d495c6eSApple OSS Distributions 
89*4d495c6eSApple OSS Distributions 	char msg_store[MSG + MAX_TRAILER_SIZE];
90*4d495c6eSApple OSS Distributions 	char reply_store[MSG];
91*4d495c6eSApple OSS Distributions 	mach_msg_header_t *msg = (mach_msg_header_t *)msg_store;
92*4d495c6eSApple OSS Distributions 	vm_address_t page;
93*4d495c6eSApple OSS Distributions 	kern_return_t kr;
94*4d495c6eSApple OSS Distributions 
95*4d495c6eSApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &page, PG_ALLOC, VM_FLAGS_ANYWHERE);
96*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "ool page allocation of %d bytes", PG_ALLOC);
97*4d495c6eSApple OSS Distributions 
98*4d495c6eSApple OSS Distributions 	mach_voucher_t voucher = create_task_voucher();
99*4d495c6eSApple OSS Distributions 
100*4d495c6eSApple OSS Distributions 	while (1) {
101*4d495c6eSApple OSS Distributions 		bzero(msg, sizeof(msg_store));
102*4d495c6eSApple OSS Distributions 
103*4d495c6eSApple OSS Distributions 		msg->msgh_local_port = ePort;
104*4d495c6eSApple OSS Distributions 		msg->msgh_size = MSG;
105*4d495c6eSApple OSS Distributions 		kr = mach_msg_receive(msg);
106*4d495c6eSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "exception msg recv");
107*4d495c6eSApple OSS Distributions 
108*4d495c6eSApple OSS Distributions 		bzero(reply_store, sizeof(reply_store));
109*4d495c6eSApple OSS Distributions 
110*4d495c6eSApple OSS Distributions 		switch (reply_type) {
111*4d495c6eSApple OSS Distributions 		case ReplyWithNoError: {
112*4d495c6eSApple OSS Distributions #pragma pack(4)
113*4d495c6eSApple OSS Distributions 			typedef struct {
114*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
115*4d495c6eSApple OSS Distributions 				NDR_record_t ndr;
116*4d495c6eSApple OSS Distributions 				kern_return_t kr;
117*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
118*4d495c6eSApple OSS Distributions #pragma pack()
119*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
120*4d495c6eSApple OSS Distributions 
121*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, 0);
122*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
123*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = MACH_PORT_NULL;
124*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
125*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
126*4d495c6eSApple OSS Distributions 			break;
127*4d495c6eSApple OSS Distributions 		}
128*4d495c6eSApple OSS Distributions 
129*4d495c6eSApple OSS Distributions 		case ReplyWithReplyPort: {
130*4d495c6eSApple OSS Distributions #pragma pack(4)
131*4d495c6eSApple OSS Distributions 			typedef struct {
132*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
133*4d495c6eSApple OSS Distributions 				NDR_record_t ndr;
134*4d495c6eSApple OSS Distributions 				kern_return_t kr;
135*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
136*4d495c6eSApple OSS Distributions #pragma pack()
137*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
138*4d495c6eSApple OSS Distributions 
139*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, MACH_MSG_TYPE_COPY_SEND, 0, 0);
140*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
141*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = ePort; /* Bogus */
142*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
143*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
144*4d495c6eSApple OSS Distributions 			break;
145*4d495c6eSApple OSS Distributions 		}
146*4d495c6eSApple OSS Distributions 
147*4d495c6eSApple OSS Distributions 		case ReplyWithReplyPortMove: {
148*4d495c6eSApple OSS Distributions #pragma pack(4)
149*4d495c6eSApple OSS Distributions 			typedef struct {
150*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
151*4d495c6eSApple OSS Distributions 				NDR_record_t ndr;
152*4d495c6eSApple OSS Distributions 				kern_return_t kr;
153*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
154*4d495c6eSApple OSS Distributions #pragma pack()
155*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
156*4d495c6eSApple OSS Distributions 
157*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, MACH_MSG_TYPE_MOVE_SEND, 0, 0);
158*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
159*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = ePort; /* Bogus */
160*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
161*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
162*4d495c6eSApple OSS Distributions 			break;
163*4d495c6eSApple OSS Distributions 		}
164*4d495c6eSApple OSS Distributions 
165*4d495c6eSApple OSS Distributions 		case ReplyWithReplyPortCplxBit: {
166*4d495c6eSApple OSS Distributions #pragma pack(4)
167*4d495c6eSApple OSS Distributions 			typedef struct {
168*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
169*4d495c6eSApple OSS Distributions 				mach_msg_body_t body;
170*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
171*4d495c6eSApple OSS Distributions #pragma pack()
172*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
173*4d495c6eSApple OSS Distributions 
174*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, MACH_MSG_TYPE_COPY_SEND, 0, MACH_MSGH_BITS_COMPLEX);
175*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
176*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = ePort; /* Bogus */
177*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
178*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
179*4d495c6eSApple OSS Distributions 			reply->body.msgh_descriptor_count = 0;
180*4d495c6eSApple OSS Distributions 			break;
181*4d495c6eSApple OSS Distributions 		}
182*4d495c6eSApple OSS Distributions 
183*4d495c6eSApple OSS Distributions 		case ReplyWithReplyPortMoveCplxBit: {
184*4d495c6eSApple OSS Distributions #pragma pack(4)
185*4d495c6eSApple OSS Distributions 			typedef struct {
186*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
187*4d495c6eSApple OSS Distributions 				mach_msg_body_t body;
188*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
189*4d495c6eSApple OSS Distributions #pragma pack()
190*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
191*4d495c6eSApple OSS Distributions 
192*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, MACH_MSG_TYPE_MOVE_SEND, 0, MACH_MSGH_BITS_COMPLEX);
193*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
194*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = ePort; /* Bogus */
195*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
196*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
197*4d495c6eSApple OSS Distributions 			reply->body.msgh_descriptor_count = 0;
198*4d495c6eSApple OSS Distributions 			break;
199*4d495c6eSApple OSS Distributions 		}
200*4d495c6eSApple OSS Distributions 
201*4d495c6eSApple OSS Distributions 		case ReplyWithPortDesc: {
202*4d495c6eSApple OSS Distributions #pragma pack(4)
203*4d495c6eSApple OSS Distributions 			typedef struct {
204*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
205*4d495c6eSApple OSS Distributions 				mach_msg_body_t body;
206*4d495c6eSApple OSS Distributions 				mach_msg_port_descriptor_t port;
207*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
208*4d495c6eSApple OSS Distributions #pragma pack()
209*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
210*4d495c6eSApple OSS Distributions 
211*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, MACH_MSGH_BITS_COMPLEX);
212*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
213*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = MACH_PORT_NULL;
214*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
215*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
216*4d495c6eSApple OSS Distributions 			reply->body.msgh_descriptor_count = 1;
217*4d495c6eSApple OSS Distributions 			reply->port.type = MACH_MSG_PORT_DESCRIPTOR;
218*4d495c6eSApple OSS Distributions 			reply->port.name = ePort;
219*4d495c6eSApple OSS Distributions 			reply->port.disposition = MACH_MSG_TYPE_COPY_SEND;
220*4d495c6eSApple OSS Distributions 			break;
221*4d495c6eSApple OSS Distributions 		}
222*4d495c6eSApple OSS Distributions 
223*4d495c6eSApple OSS Distributions 		case ReplyWithOOLDesc: {
224*4d495c6eSApple OSS Distributions #pragma pack(4)
225*4d495c6eSApple OSS Distributions 			typedef struct {
226*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
227*4d495c6eSApple OSS Distributions 				mach_msg_body_t body;
228*4d495c6eSApple OSS Distributions 				mach_msg_ool_descriptor_t ool;
229*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
230*4d495c6eSApple OSS Distributions #pragma pack()
231*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
232*4d495c6eSApple OSS Distributions 
233*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, MACH_MSGH_BITS_COMPLEX);
234*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
235*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = MACH_PORT_NULL;
236*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
237*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
238*4d495c6eSApple OSS Distributions 			reply->body.msgh_descriptor_count = 1;
239*4d495c6eSApple OSS Distributions 			reply->ool.type = MACH_MSG_OOL_DESCRIPTOR;
240*4d495c6eSApple OSS Distributions 			reply->ool.address = (void *)page;
241*4d495c6eSApple OSS Distributions 			reply->ool.size = PG_ALLOC;
242*4d495c6eSApple OSS Distributions 			reply->ool.deallocate = 0;
243*4d495c6eSApple OSS Distributions 			reply->ool.copy = MACH_MSG_VIRTUAL_COPY;
244*4d495c6eSApple OSS Distributions 			break;
245*4d495c6eSApple OSS Distributions 		}
246*4d495c6eSApple OSS Distributions 
247*4d495c6eSApple OSS Distributions 		case ReplyWithVoucher: {
248*4d495c6eSApple OSS Distributions #pragma pack(4)
249*4d495c6eSApple OSS Distributions 			typedef struct {
250*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
251*4d495c6eSApple OSS Distributions 				NDR_record_t ndr;
252*4d495c6eSApple OSS Distributions 				kern_return_t kr;
253*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
254*4d495c6eSApple OSS Distributions #pragma pack()
255*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
256*4d495c6eSApple OSS Distributions 
257*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
258*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = MACH_PORT_NULL;
259*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
260*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
261*4d495c6eSApple OSS Distributions 			reply->kr = KERN_SUCCESS;
262*4d495c6eSApple OSS Distributions 
263*4d495c6eSApple OSS Distributions 			/* try to send a voucher */
264*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE,
265*4d495c6eSApple OSS Distributions 			    0,
266*4d495c6eSApple OSS Distributions 			    MACH_MSG_TYPE_MOVE_SEND,
267*4d495c6eSApple OSS Distributions 			    0);
268*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_voucher_port = voucher;
269*4d495c6eSApple OSS Distributions 			voucher = MACH_VOUCHER_NULL;
270*4d495c6eSApple OSS Distributions 			break;
271*4d495c6eSApple OSS Distributions 		}
272*4d495c6eSApple OSS Distributions 
273*4d495c6eSApple OSS Distributions 		case ReplyWithVoucherGarbage: {
274*4d495c6eSApple OSS Distributions #pragma pack(4)
275*4d495c6eSApple OSS Distributions 			typedef struct {
276*4d495c6eSApple OSS Distributions 				mach_msg_header_t hdr;
277*4d495c6eSApple OSS Distributions 				NDR_record_t ndr;
278*4d495c6eSApple OSS Distributions 				kern_return_t kr;
279*4d495c6eSApple OSS Distributions 			} reply_fmt_t;
280*4d495c6eSApple OSS Distributions #pragma pack()
281*4d495c6eSApple OSS Distributions 			reply_fmt_t *reply = (reply_fmt_t *)reply_store;
282*4d495c6eSApple OSS Distributions 
283*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_remote_port = msg->msgh_remote_port;
284*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_local_port = MACH_PORT_NULL;
285*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_size = sizeof(*reply);
286*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_id = msg->msgh_id + 100;
287*4d495c6eSApple OSS Distributions 			reply->kr = KERN_SUCCESS;
288*4d495c6eSApple OSS Distributions 
289*4d495c6eSApple OSS Distributions 			/* don't claim to send a voucher */
290*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE,
291*4d495c6eSApple OSS Distributions 			    0, 0, 0);
292*4d495c6eSApple OSS Distributions 			/* but put some bits in the field */
293*4d495c6eSApple OSS Distributions 			reply->hdr.msgh_voucher_port = (mach_voucher_t)0xdead;
294*4d495c6eSApple OSS Distributions 			break;
295*4d495c6eSApple OSS Distributions 		}
296*4d495c6eSApple OSS Distributions 
297*4d495c6eSApple OSS Distributions 		default:
298*4d495c6eSApple OSS Distributions 			T_ASSERT_FAIL("Invalid ReplyType: %d", reply_type);
299*4d495c6eSApple OSS Distributions 			T_END;
300*4d495c6eSApple OSS Distributions 		}
301*4d495c6eSApple OSS Distributions 
302*4d495c6eSApple OSS Distributions 		if (voucher) {
303*4d495c6eSApple OSS Distributions 			kr = mach_port_mod_refs(mach_task_self(), voucher,
304*4d495c6eSApple OSS Distributions 			    MACH_PORT_RIGHT_SEND, -1);
305*4d495c6eSApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "destroy voucher");
306*4d495c6eSApple OSS Distributions 		}
307*4d495c6eSApple OSS Distributions 
308*4d495c6eSApple OSS Distributions 		T_LOG("sending exception reply of type (%s)", reply_type_str(reply_type));
309*4d495c6eSApple OSS Distributions 		kr = mach_msg_send((mach_msg_header_t *)reply_store);
310*4d495c6eSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "exception reply msg send");
311*4d495c6eSApple OSS Distributions 
312*4d495c6eSApple OSS Distributions 		T_PASS("Successfully delivered exception reply message of type %s", reply_type_str(reply_type));
313*4d495c6eSApple OSS Distributions 		T_END;
314*4d495c6eSApple OSS Distributions 		return NULL;
315*4d495c6eSApple OSS Distributions 	}
316*4d495c6eSApple OSS Distributions }
317*4d495c6eSApple OSS Distributions 
318*4d495c6eSApple OSS Distributions static sigjmp_buf jb;
319*4d495c6eSApple OSS Distributions static int *bad_pointer = NULL;
320*4d495c6eSApple OSS Distributions static int s_sigmask = 0;
321*4d495c6eSApple OSS Distributions 
322*4d495c6eSApple OSS Distributions static void
signal_handler(int sig,siginfo_t * sip __unused,void * ucontext __unused)323*4d495c6eSApple OSS Distributions signal_handler(int sig, siginfo_t *sip __unused, void *ucontext __unused)
324*4d495c6eSApple OSS Distributions {
325*4d495c6eSApple OSS Distributions 	if (sigmask(sig) & s_sigmask) { /* TODO: check that the fault was generated by us */
326*4d495c6eSApple OSS Distributions 		siglongjmp(jb, sig);
327*4d495c6eSApple OSS Distributions 	} else {
328*4d495c6eSApple OSS Distributions 		siglongjmp(jb, -sig);
329*4d495c6eSApple OSS Distributions 	}
330*4d495c6eSApple OSS Distributions }
331*4d495c6eSApple OSS Distributions 
332*4d495c6eSApple OSS Distributions static int
handle_signals(void)333*4d495c6eSApple OSS Distributions handle_signals(void)
334*4d495c6eSApple OSS Distributions {
335*4d495c6eSApple OSS Distributions 	int mask = 0;
336*4d495c6eSApple OSS Distributions 
337*4d495c6eSApple OSS Distributions 	struct sigaction sa = {
338*4d495c6eSApple OSS Distributions 		.sa_sigaction = signal_handler,
339*4d495c6eSApple OSS Distributions 		.sa_flags = SA_SIGINFO
340*4d495c6eSApple OSS Distributions 	};
341*4d495c6eSApple OSS Distributions 	sigfillset(&sa.sa_mask);
342*4d495c6eSApple OSS Distributions 
343*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(sigaction(SIGTRAP, &sa, NULL), NULL);
344*4d495c6eSApple OSS Distributions 	mask |= sigmask(SIGTRAP);
345*4d495c6eSApple OSS Distributions 
346*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(sigaction(SIGSEGV, &sa, NULL), NULL);
347*4d495c6eSApple OSS Distributions 	mask |= sigmask(SIGSEGV);
348*4d495c6eSApple OSS Distributions 
349*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
350*4d495c6eSApple OSS Distributions 	mask |= sigmask(SIGILL);
351*4d495c6eSApple OSS Distributions 
352*4d495c6eSApple OSS Distributions 	return mask;
353*4d495c6eSApple OSS Distributions }
354*4d495c6eSApple OSS Distributions 
355*4d495c6eSApple OSS Distributions static void
test_exc_reply_type(ReplyType reply_type)356*4d495c6eSApple OSS Distributions test_exc_reply_type(ReplyType reply_type)
357*4d495c6eSApple OSS Distributions {
358*4d495c6eSApple OSS Distributions 	kern_return_t kr;
359*4d495c6eSApple OSS Distributions 	task_t me = mach_task_self();
360*4d495c6eSApple OSS Distributions 	thread_t self = mach_thread_self();
361*4d495c6eSApple OSS Distributions 	pthread_t handler_thread;
362*4d495c6eSApple OSS Distributions 	pthread_attr_t  attr;
363*4d495c6eSApple OSS Distributions 	mach_port_t ePort;
364*4d495c6eSApple OSS Distributions 
365*4d495c6eSApple OSS Distributions 	s_sigmask = handle_signals();
366*4d495c6eSApple OSS Distributions 	T_LOG("task self = 0x%x, thread self = 0x%x\n", me, self);
367*4d495c6eSApple OSS Distributions 
368*4d495c6eSApple OSS Distributions 	kr = mach_port_allocate(me, MACH_PORT_RIGHT_RECEIVE, &ePort);
369*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "allocate receive right");
370*4d495c6eSApple OSS Distributions 
371*4d495c6eSApple OSS Distributions 	kr = mach_port_insert_right(me, ePort, ePort, MACH_MSG_TYPE_MAKE_SEND);
372*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "insert right into port=[%d]", ePort);
373*4d495c6eSApple OSS Distributions 
374*4d495c6eSApple OSS Distributions 	kr = thread_set_exception_ports(self, EXC_MASK_ALL, ePort, EXCEPTION_DEFAULT, THREAD_STATE_NONE);
375*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "set exception ports on self=[%d], handler=[%d]", self, ePort);
376*4d495c6eSApple OSS Distributions 
377*4d495c6eSApple OSS Distributions 	pthread_attr_init(&attr);
378*4d495c6eSApple OSS Distributions 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
379*4d495c6eSApple OSS Distributions 	struct exc_thread_arg *ta = (struct exc_thread_arg *)malloc(sizeof(*ta));
380*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ta, "exception handler thread args allocation");
381*4d495c6eSApple OSS Distributions 	ta->port = ePort;
382*4d495c6eSApple OSS Distributions 	ta->rt = reply_type;
383*4d495c6eSApple OSS Distributions 
384*4d495c6eSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&handler_thread, &attr, handle_exceptions, (void *)ta),
385*4d495c6eSApple OSS Distributions 	    "pthread creation");
386*4d495c6eSApple OSS Distributions 
387*4d495c6eSApple OSS Distributions 	pthread_attr_destroy(&attr);
388*4d495c6eSApple OSS Distributions 
389*4d495c6eSApple OSS Distributions 	/* cause exception! */
390*4d495c6eSApple OSS Distributions 	int x = sigsetjmp(jb, 0); //s_sigmask);
391*4d495c6eSApple OSS Distributions 	if (x == 0) {
392*4d495c6eSApple OSS Distributions 		*bad_pointer = 0;
393*4d495c6eSApple OSS Distributions 	} else if (x < 0) {
394*4d495c6eSApple OSS Distributions 		T_FAIL("Unexpected state on return-from-exception");
395*4d495c6eSApple OSS Distributions 		T_END;
396*4d495c6eSApple OSS Distributions 	} else {
397*4d495c6eSApple OSS Distributions 		T_PASS("Successfully recovered from exception");
398*4d495c6eSApple OSS Distributions 		T_END;
399*4d495c6eSApple OSS Distributions 	}
400*4d495c6eSApple OSS Distributions 	T_FAIL("Unexpected end of test!");
401*4d495c6eSApple OSS Distributions 	T_END;
402*4d495c6eSApple OSS Distributions }
403*4d495c6eSApple OSS Distributions 
404*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyNoError, "exception server reply with no error",
405*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
406*4d495c6eSApple OSS Distributions {
407*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithNoError);
408*4d495c6eSApple OSS Distributions }
409*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithReplyPort, "exception server reply with reply port",
410*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
411*4d495c6eSApple OSS Distributions {
412*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithReplyPort);
413*4d495c6eSApple OSS Distributions }
414*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithReplyPortMove, "exception server reply with reply port as MOVE_SEND",
415*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
416*4d495c6eSApple OSS Distributions {
417*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithReplyPortMove);
418*4d495c6eSApple OSS Distributions }
419*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithReplyPortCplxBit, "exception server reply with reply port and complex bit set",
420*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
421*4d495c6eSApple OSS Distributions {
422*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithReplyPortCplxBit);
423*4d495c6eSApple OSS Distributions }
424*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithReplyPortMoveCplxBit, "exception server reply with reply port as MOVE_SEND and complex bit set",
425*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
426*4d495c6eSApple OSS Distributions {
427*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithReplyPortMoveCplxBit);
428*4d495c6eSApple OSS Distributions }
429*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithOOLPort, "exception server reply with OOL port descriptor",
430*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
431*4d495c6eSApple OSS Distributions {
432*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithPortDesc);
433*4d495c6eSApple OSS Distributions }
434*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithOOLDesc, "exception server reply with OOL memory descriptor",
435*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
436*4d495c6eSApple OSS Distributions {
437*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithOOLDesc);
438*4d495c6eSApple OSS Distributions }
439*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithVoucher, "exception server reply with a voucher",
440*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
441*4d495c6eSApple OSS Distributions {
442*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithVoucher);
443*4d495c6eSApple OSS Distributions }
444*4d495c6eSApple OSS Distributions T_DECL(mach_exc_ReplyWithVoucherGarbage, "exception server reply with bits in msgh_voucher_port",
445*4d495c6eSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*mach_exception_reply.*"), T_META_TAG_VM_PREFERRED)
446*4d495c6eSApple OSS Distributions {
447*4d495c6eSApple OSS Distributions 	test_exc_reply_type(ReplyWithVoucherGarbage);
448*4d495c6eSApple OSS Distributions }
449