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