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