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