1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* Copyright (c) 2008-2011 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#include <dispatch/dispatch.h>
#include <dispatch/private.h>
#ifdef __APPLE__
#include <mach/mach.h>
#endif
#include <stdio.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <bsdtests.h>
#include "dispatch_test.h"
#if TEST_MACHPORT_DEBUG
#define test_mach_assume_zero(x) ({kern_return_t _kr = (x); \
if (_kr) fprintf(stderr, "mach error 0x%x \"%s\": %s\n", \
_kr, mach_error_string(_kr), #x); (void)kr; })
void
test_mach_debug_port(mach_port_t name, const char *str, unsigned int line)
{
mach_port_type_t type;
mach_msg_bits_t ns = 0, nr = 0, nso = 0, nd = 0;
unsigned int dnreqs = 0, dnrsiz;
kern_return_t kr = mach_port_type(mach_task_self(), name, &type);
if (kr) {
fprintf(stderr, "machport[0x%08x] = { error(0x%x) \"%s\" }: %s %u\n",
name, kr, mach_error_string(kr), str, line);
return;
}
if (type & MACH_PORT_TYPE_SEND) {
test_mach_assume_zero(mach_port_get_refs(mach_task_self(), name,
MACH_PORT_RIGHT_SEND, &ns));
}
if (type & MACH_PORT_TYPE_SEND_ONCE) {
test_mach_assume_zero(mach_port_get_refs(mach_task_self(), name,
MACH_PORT_RIGHT_SEND_ONCE, &nso));
}
if (type & MACH_PORT_TYPE_DEAD_NAME) {
test_mach_assume_zero(mach_port_get_refs(mach_task_self(), name,
MACH_PORT_RIGHT_DEAD_NAME, &nd));
}
if (type & (MACH_PORT_TYPE_RECEIVE|MACH_PORT_TYPE_SEND)) {
test_mach_assume_zero(mach_port_dnrequest_info(mach_task_self(), name,
&dnrsiz, &dnreqs));
}
if (type & MACH_PORT_TYPE_RECEIVE) {
mach_port_status_t status = { .mps_pset = 0, };
mach_msg_type_number_t cnt = MACH_PORT_RECEIVE_STATUS_COUNT;
test_mach_assume_zero(mach_port_get_refs(mach_task_self(), name,
MACH_PORT_RIGHT_RECEIVE, &nr));
test_mach_assume_zero(mach_port_get_attributes(mach_task_self(), name,
MACH_PORT_RECEIVE_STATUS, (void*)&status, &cnt));
fprintf(stderr, "machport[0x%08x] = { R(%03u) S(%03u) SO(%03u) D(%03u) "
"dnreqs(%03u) spreq(%s) nsreq(%s) pdreq(%s) srights(%s) "
"sorights(%03u) qlim(%03u) msgcount(%03u) mkscount(%03u) "
"seqno(%03u) }: %s %u\n", name, nr, ns, nso, nd, dnreqs,
type & MACH_PORT_TYPE_SPREQUEST ? "Y":"N",
status.mps_nsrequest ? "Y":"N", status.mps_pdrequest ? "Y":"N",
status.mps_srights ? "Y":"N", status.mps_sorights,
status.mps_qlimit, status.mps_msgcount, status.mps_mscount,
status.mps_seqno, str, line);
} else if (type & (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_SEND_ONCE|
MACH_PORT_TYPE_DEAD_NAME)) {
fprintf(stderr, "machport[0x%08x] = { R(%03u) S(%03u) SO(%03u) D(%03u) "
"dnreqs(%03u) spreq(%s) }: %s %u\n", name, nr, ns, nso, nd,
dnreqs, type & MACH_PORT_TYPE_SPREQUEST ? "Y":"N", str, line);
} else {
fprintf(stderr, "machport[0x%08x] = { type(0x%08x) }: %s %u\n", name,
type, str, line);
}
fflush(stderr);
}
#define test_mach_debug_port(x) test_mach_debug_port(x, __func__, __LINE__)
#else
#define test_mach_debug_port(x) (void)(x)
#endif
static dispatch_group_t g;
static volatile long sent, received;
void
test_dead_name(void)
{
dispatch_group_enter(g);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_source_t ds0;
kern_return_t kr;
mach_port_t mp = pthread_mach_thread_np(pthread_self());
assert(mp);
kr = mach_port_mod_refs(mach_task_self(), mp, MACH_PORT_RIGHT_SEND, 1);
test_mach_error("mach_port_mod_refs", kr, KERN_SUCCESS);
ds0 = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, mp,
DISPATCH_MACH_SEND_DEAD, dispatch_get_main_queue());
test_ptr_notnull("DISPATCH_SOURCE_TYPE_MACH_SEND", ds0);
dispatch_source_set_event_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD",
dispatch_source_get_handle(ds0), mp);
dispatch_source_cancel(ds0);
});
dispatch_source_set_cancel_handler(ds0, ^{
kern_return_t kr = mach_port_deallocate(mach_task_self(), mp);
test_mach_error("mach_port_deallocate", kr, KERN_SUCCESS);
dispatch_release(ds0);
dispatch_group_leave(g);
});
dispatch_resume(ds0);
// give the mgr queue time to start, otherwise the mgr queue will run
// on this thread, thus defeating the test which assumes that this
// thread will die.
usleep(100000);
});
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
}
void
test_register_already_dead_name(void)
{
dispatch_source_t ds0;
kern_return_t kr;
mach_port_t mp;
dispatch_group_enter(g);
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);
kr = mach_port_insert_right(mach_task_self(), mp, mp,
MACH_MSG_TYPE_MAKE_SEND);
test_mach_error("mach_port_insert_right", kr, KERN_SUCCESS);
kr = mach_port_mod_refs(mach_task_self(), mp,
MACH_PORT_RIGHT_RECEIVE, -1); // turn send right into dead name
test_mach_error("mach_port_mod_refs", kr, KERN_SUCCESS);
ds0 = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, mp,
DISPATCH_MACH_SEND_DEAD, dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD",
dispatch_source_get_handle(ds0), mp);
dispatch_source_cancel(ds0);
dispatch_release(ds0);
});
dispatch_source_set_cancel_handler(ds0, ^{
kern_return_t kr = mach_port_deallocate(mach_task_self(), mp);
test_mach_error("mach_port_deallocate", kr, KERN_SUCCESS);
dispatch_group_leave(g);
});
dispatch_resume(ds0);
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
}
void
test_receive_and_dead_name(void)
{
dispatch_source_t ds0, ds;
kern_return_t kr;
mach_port_t mp;
received = 0;
dispatch_group_enter(g);
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);
kr = mach_port_insert_right(mach_task_self(), mp, mp,
MACH_MSG_TYPE_MAKE_SEND);
test_mach_error("mach_port_insert_right", kr, KERN_SUCCESS);
ds0 = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, mp,
DISPATCH_MACH_SEND_DEAD, dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD",
dispatch_source_get_handle(ds0), mp);
dispatch_source_cancel(ds0);
dispatch_release(ds0);
});
dispatch_source_set_cancel_handler(ds0, ^{
kern_return_t kr = mach_port_deallocate(mach_task_self(), mp);
test_mach_error("mach_port_deallocate", kr, KERN_SUCCESS);
dispatch_group_leave(g);
});
dispatch_resume(ds0);
ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, mp, 0,
dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(ds, ^{
__sync_add_and_fetch(&received, 1);
usleep(100000); // rdar://problem/7676437 race with send source re-arm
mach_msg_empty_rcv_t msg = { .header = {
.msgh_size = sizeof(mach_msg_empty_rcv_t),
.msgh_local_port = mp,
}};
kern_return_t kr = mach_msg_receive(&msg.header);
test_mach_error("mach_msg_receive", kr, KERN_SUCCESS);
});
dispatch_source_set_cancel_handler(ds, ^{
kern_return_t kr = mach_port_mod_refs(mach_task_self(), mp,
MACH_PORT_RIGHT_RECEIVE, -1); // turns send right into dead name
test_mach_error("mach_port_mod_refs", kr, KERN_SUCCESS);
});
dispatch_resume(ds);
mach_msg_empty_send_t msg = { .header = {
.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND),
.msgh_size = sizeof(mach_msg_empty_send_t),
.msgh_remote_port = mp,
}};
kr = mach_msg_send(&msg.header);
test_mach_error("mach_msg_send", kr, KERN_SUCCESS);
usleep(200000);
dispatch_source_cancel(ds);
dispatch_release(ds);
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV", received, 1);
if (received > 1 ) {
test_stop();
}
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
}
#if DISPATCH_API_VERSION >= 20110201 && defined(MACH_NOTIFY_SEND_POSSIBLE) && \
defined(MACH_SEND_NOTIFY)
#define TEST_SP_MSGCOUNT 11 // (2*qlim)+1
static bool
send_until_timeout(mach_port_t mp)
{
kern_return_t kr;
do {
test_mach_debug_port(mp);
mach_msg_empty_send_t msg = { .header = {
.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND),
.msgh_size = sizeof(mach_msg_empty_send_t),
.msgh_remote_port = mp,
}};
kr = mach_msg(&msg.header,
MACH_SEND_MSG|MACH_SEND_NOTIFY|MACH_SEND_TIMEOUT,
msg.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
if (kr == MACH_SEND_TIMED_OUT) {
mach_msg_destroy(&msg.header);
test_mach_error("mach_msg(MACH_SEND_MSG) timed out", kr,
MACH_SEND_TIMED_OUT);
} else {
test_mach_error("mach_msg(MACH_SEND_MSG)", kr, KERN_SUCCESS);
if (kr) test_stop();
}
} while (!kr && __sync_add_and_fetch(&sent, 1) < TEST_SP_MSGCOUNT);
test_mach_debug_port(mp);
return kr;
}
void
test_send_possible(void) // rdar://problem/8758200
{
dispatch_source_t ds0, ds, dsp;
kern_return_t kr;
mach_port_t mp;
sent = 0;
received = 0;
dispatch_group_enter(g);
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);
test_mach_debug_port(mp);
kr = mach_port_insert_right(mach_task_self(), mp, mp,
MACH_MSG_TYPE_MAKE_SEND);
test_mach_error("mach_port_insert_right", kr, KERN_SUCCESS);
test_mach_debug_port(mp);
ds0 = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, mp,
DISPATCH_MACH_SEND_DEAD, dispatch_get_global_queue(0, 0));
dispatch_source_set_registration_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD registered",
dispatch_source_get_handle(ds0), mp);
test_mach_debug_port(mp);
});
dispatch_source_set_event_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD delivered",
dispatch_source_get_handle(ds0), mp);
test_mach_debug_port(mp);
dispatch_source_cancel(ds0);
dispatch_release(ds0);
});
dispatch_source_set_cancel_handler(ds0, ^{
test_long("DISPATCH_MACH_SEND_DEAD canceled",
dispatch_source_get_handle(ds0), mp);
test_mach_debug_port(mp);
kern_return_t kr = mach_port_deallocate(mach_task_self(), mp);
test_mach_error("mach_port_deallocate", kr, KERN_SUCCESS);
test_mach_debug_port(mp);
dispatch_group_leave(g);
});
dispatch_resume(ds0);
ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, mp, 0,
dispatch_get_global_queue(0, 0));
dispatch_source_set_registration_handler(ds, ^{
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV registered",
dispatch_source_get_handle(ds), mp);
test_mach_debug_port(mp);
});
dispatch_source_set_event_handler(ds, ^{
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV delivered",
dispatch_source_get_handle(ds), mp);
kern_return_t kr;
do {
test_mach_debug_port(mp);
usleep(10000); // simulate slow receiver
mach_msg_empty_rcv_t msg = { .header = {
.msgh_size = sizeof(mach_msg_empty_rcv_t),
.msgh_local_port = mp,
}};
kr = mach_msg(&msg.header,
MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, msg.header.msgh_size,
msg.header.msgh_local_port, MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
if (kr == MACH_RCV_TIMED_OUT) {
test_mach_error("mach_msg(MACH_RCV_MSG) timed out", kr,
MACH_RCV_TIMED_OUT);
} else {
test_mach_error("mach_msg(MACH_RCV_MSG)", kr, KERN_SUCCESS);
if (kr) test_stop();
}
} while (!kr && __sync_add_and_fetch(&received, 1));
test_mach_debug_port(mp);
});
dispatch_source_set_cancel_handler(ds, ^{
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV canceled",
dispatch_source_get_handle(ds), mp);
test_mach_debug_port(mp);
kern_return_t kr = mach_port_mod_refs(mach_task_self(), mp,
MACH_PORT_RIGHT_RECEIVE, -1); // trigger dead name notification
test_mach_error("mach_port_mod_refs", kr, KERN_SUCCESS);
test_mach_debug_port(mp);
});
dispatch_resume(ds);
dsp = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_SEND, mp,
DISPATCH_MACH_SEND_POSSIBLE, dispatch_get_global_queue(0, 0));
dispatch_source_set_registration_handler(dsp, ^{
test_long("DISPATCH_MACH_SEND_POSSIBLE registered",
dispatch_source_get_handle(dsp), mp);
if (!send_until_timeout(mp)) {
dispatch_source_cancel(dsp); // stop sending
dispatch_release(dsp);
}
});
dispatch_source_set_event_handler(dsp, ^{
test_long("DISPATCH_MACH_SEND_POSSIBLE delivered",
dispatch_source_get_handle(dsp), mp);
if (!send_until_timeout(mp)) {
dispatch_source_cancel(dsp); // stop sending
dispatch_release(dsp);
}
});
dispatch_source_set_cancel_handler(dsp, ^{
test_long("DISPATCH_MACH_SEND_POSSIBLE canceled",
dispatch_source_get_handle(dsp), mp);
test_mach_debug_port(mp);
dispatch_source_cancel(ds); // stop receving
dispatch_release(ds);
});
dispatch_resume(dsp);
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
test_long("DISPATCH_SOURCE_TYPE_MACH_SEND", sent, TEST_SP_MSGCOUNT);
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV", received, TEST_SP_MSGCOUNT);
}
#else
#define test_send_possible()
#endif
static boolean_t
test_mig_callback(mach_msg_header_t *message __attribute__((unused)),
mach_msg_header_t *reply)
{
__sync_add_and_fetch(&received, 1);
reply->msgh_remote_port = 0;
return false;
}
void
test_mig_server_large_msg(void) // rdar://problem/8422992
{
dispatch_source_t ds;
kern_return_t kr;
mach_port_t mp;
received = 0;
dispatch_group_enter(g);
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);
kr = mach_port_insert_right(mach_task_self(), mp, mp,
MACH_MSG_TYPE_MAKE_SEND);
test_mach_error("mach_port_insert_right", kr, KERN_SUCCESS);
ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, mp, 0,
dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(ds, ^{
mach_msg_return_t r = dispatch_mig_server(ds, sizeof(mach_msg_header_t),
test_mig_callback);
test_mach_error("dispatch_mig_server", r, MACH_RCV_TOO_LARGE);
dispatch_group_leave(g);
});
dispatch_source_set_cancel_handler(ds, ^{
kern_return_t kr = mach_port_mod_refs(mach_task_self(), mp,
MACH_PORT_RIGHT_RECEIVE, -1);
test_mach_error("mach_port_mod_refs", kr, KERN_SUCCESS);
kr = mach_port_deallocate(mach_task_self(), mp);
test_mach_error("mach_port_deallocate", kr, KERN_SUCCESS);
dispatch_group_leave(g);
});
dispatch_resume(ds);
struct { mach_msg_header_t header; char payload[4096]; } msg = {
.header = {
.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND),
.msgh_size = sizeof(mach_msg_header_t) + 4096,
.msgh_remote_port = mp,
.msgh_id = 0xfeedface,
}
};
kr = mach_msg_send(&msg.header);
test_mach_error("mach_msg_send", kr, KERN_SUCCESS);
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
dispatch_group_enter(g);
dispatch_source_cancel(ds);
dispatch_release(ds);
test_long("DISPATCH_SOURCE_TYPE_MACH_RECV", received, 0);
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
}
int
main(void)
{
dispatch_test_start("Dispatch dead-name and send-possible notifications");
dispatch_async(dispatch_get_global_queue(0, 0), ^{
g = dispatch_group_create();
test_dead_name();
test_register_already_dead_name();
test_receive_and_dead_name();
test_send_possible();
test_mig_server_large_msg();
test_stop();
});
dispatch_main();
return 0;
}
|