1*43a90889SApple OSS Distributions #include <unistd.h> 2*43a90889SApple OSS Distributions #include <pthread.h> 3*43a90889SApple OSS Distributions #include <errno.h> 4*43a90889SApple OSS Distributions 5*43a90889SApple OSS Distributions #include <sys/event.h> 6*43a90889SApple OSS Distributions #include <mach/mach.h> 7*43a90889SApple OSS Distributions #include <mach/mach_port.h> 8*43a90889SApple OSS Distributions 9*43a90889SApple OSS Distributions #include <Block.h> 10*43a90889SApple OSS Distributions #include <darwintest.h> 11*43a90889SApple OSS Distributions 12*43a90889SApple OSS Distributions T_DECL(kqueue_nesting_level, "rdar://100277117 (Reduce kqueue nesting level so that we don't overflow kernel stack)") 13*43a90889SApple OSS Distributions { 14*43a90889SApple OSS Distributions // Create a port and register a knote for it on a kqueue 15*43a90889SApple OSS Distributions mach_port_t port = MACH_PORT_NULL; 16*43a90889SApple OSS Distributions kern_return_t kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); 17*43a90889SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "allocating a port with a receive right"); 18*43a90889SApple OSS Distributions 19*43a90889SApple OSS Distributions int port_kq = kqueue(); 20*43a90889SApple OSS Distributions struct kevent_qos_s event = { 21*43a90889SApple OSS Distributions .ident = port, 22*43a90889SApple OSS Distributions .filter = EVFILT_MACHPORT, 23*43a90889SApple OSS Distributions .flags = EV_ADD | EV_ENABLE, 24*43a90889SApple OSS Distributions .qos = 0x00, 25*43a90889SApple OSS Distributions .udata = 0x66666666, 26*43a90889SApple OSS Distributions .fflags = MACH_RCV_MSG, 27*43a90889SApple OSS Distributions .xflags = 0, 28*43a90889SApple OSS Distributions .data = 0, 29*43a90889SApple OSS Distributions .ext = {}, 30*43a90889SApple OSS Distributions }; 31*43a90889SApple OSS Distributions int nevents = kevent_qos(port_kq, &event, 1, NULL, 0, NULL, NULL, 0); 32*43a90889SApple OSS Distributions T_EXPECT_EQ(nevents, 0, NULL); 33*43a90889SApple OSS Distributions 34*43a90889SApple OSS Distributions // Register the other kqueues 35*43a90889SApple OSS Distributions int child_kq = port_kq; 36*43a90889SApple OSS Distributions 37*43a90889SApple OSS Distributions for (size_t i = 0; i < 1000; i++) { 38*43a90889SApple OSS Distributions int kq = kqueue(); 39*43a90889SApple OSS Distributions struct kevent_qos_s kq_read_event = { 40*43a90889SApple OSS Distributions .ident = child_kq, 41*43a90889SApple OSS Distributions .filter = EVFILT_READ, 42*43a90889SApple OSS Distributions .flags = EV_ADD | EV_ENABLE, 43*43a90889SApple OSS Distributions .qos = 0x00, 44*43a90889SApple OSS Distributions .udata = 0x66666666, 45*43a90889SApple OSS Distributions .fflags = 0x00, 46*43a90889SApple OSS Distributions .xflags = 0x00, 47*43a90889SApple OSS Distributions .data = 0, 48*43a90889SApple OSS Distributions .ext = {}, 49*43a90889SApple OSS Distributions }; 50*43a90889SApple OSS Distributions 51*43a90889SApple OSS Distributions nevents = kevent_qos(kq, &kq_read_event, 1, NULL, 0, NULL, NULL, 0); 52*43a90889SApple OSS Distributions // This kevent may sometimes fail after we exceed the limit enforced by the 53*43a90889SApple OSS Distributions // kernel in which case, we'd just have created kqueues but not set up any 54*43a90889SApple OSS Distributions // knotes on them. 55*43a90889SApple OSS Distributions // 56*43a90889SApple OSS Distributions // On old-OSes prior to rdar://100277117, this would always succeed and then 57*43a90889SApple OSS Distributions // we'd panic when we send a message 58*43a90889SApple OSS Distributions child_kq = kq; 59*43a90889SApple OSS Distributions } 60*43a90889SApple OSS Distributions 61*43a90889SApple OSS Distributions // Send a message to the port and activate the first kqueue 62*43a90889SApple OSS Distributions struct { 63*43a90889SApple OSS Distributions mach_msg_header_t header; 64*43a90889SApple OSS Distributions uint64_t data; 65*43a90889SApple OSS Distributions } message = { 66*43a90889SApple OSS Distributions .header = { 67*43a90889SApple OSS Distributions .msgh_remote_port = port, 68*43a90889SApple OSS Distributions .msgh_local_port = MACH_PORT_NULL, 69*43a90889SApple OSS Distributions .msgh_voucher_port = MACH_PORT_NULL, 70*43a90889SApple OSS Distributions .msgh_size = sizeof(message), 71*43a90889SApple OSS Distributions .msgh_id = 0x88888888, 72*43a90889SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MAKE_SEND_ONCE, 0, 0, 0), 73*43a90889SApple OSS Distributions }, 74*43a90889SApple OSS Distributions .data = 0x8888888888888, 75*43a90889SApple OSS Distributions }; 76*43a90889SApple OSS Distributions 77*43a90889SApple OSS Distributions kr = mach_msg(&message.header, MACH_SEND_MSG, sizeof(message), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); 78*43a90889SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_msg(SEND)"); 79*43a90889SApple OSS Distributions } 80