1 /*
2 * Copyright (c) 2016-2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* <rdar://problem/24849324> os_channel_{rx,tx}_ring() needs to check bounds of the ring index */
30
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <uuid/uuid.h>
37 #include <sys/select.h>
38 #include <poll.h>
39 #include <sys/event.h>
40 #include "skywalk_test_driver.h"
41 #include "skywalk_test_common.h"
42 #include "skywalk_test_utils.h"
43
44 #define NUM_TX_RINGS 4
45 #define NUM_RX_RINGS 4
46
47 static void
skt_ringid_init(void)48 skt_ringid_init(void)
49 {
50 struct sktc_nexus_attr attr = SKTC_NEXUS_ATTR_INIT();
51
52 strncpy((char *)attr.name, "skywalk_test_ringid_upipe",
53 sizeof(nexus_name_t) - 1);
54 attr.type = NEXUS_TYPE_USER_PIPE;
55 attr.ntxrings = NUM_TX_RINGS;
56 attr.nrxrings = NUM_RX_RINGS;
57 attr.anonymous = 1;
58
59 sktc_setup_nexus(&attr);
60 }
61
62 static void
skt_ringid_fini(void)63 skt_ringid_fini(void)
64 {
65 sktc_cleanup_nexus();
66 }
67
68 /****************************************************************/
69
70 static int
skt_ringid_main_common(int argc,char * argv[],uint32_t num,ring_id_type_t first,ring_id_type_t last,channel_ring_t (* get_ring)(const channel_t chd,const ring_id_t rid))71 skt_ringid_main_common(int argc, char *argv[], uint32_t num,
72 ring_id_type_t first, ring_id_type_t last,
73 channel_ring_t (*get_ring)(const channel_t chd, const ring_id_t rid))
74 {
75 int error;
76 channel_t channel;
77 uuid_t channel_uuid;
78 ring_id_t fringid, lringid, ringid;
79 channel_ring_t ring;
80
81 error = uuid_parse(argv[3], channel_uuid);
82 SKTC_ASSERT_ERR(!error);
83
84 channel = sktu_channel_create_extended(channel_uuid, 0,
85 CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL,
86 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
87 assert(channel);
88
89 fringid = os_channel_ring_id(channel, first);
90 lringid = os_channel_ring_id(channel, last);
91
92 assert(lringid - fringid == num - 1);
93
94 assert(fringid == 0); // XXX violates opaque abstraction
95
96 /* Verify that we can get all the expected rings */
97 for (ringid = fringid; ringid <= lringid; ringid++) {
98 ring = (*get_ring)(channel, ringid);
99 assert(ring);
100 }
101
102 /* And not a ring outside of the range */
103 assert(ringid == lringid + 1);
104 ring = (*get_ring)(channel, ringid);
105 assert(!ring);
106
107 os_channel_destroy(channel);
108
109 /* Now reopen each channel with just a single ringid
110 * And verify that we can only get the expected ring id
111 */
112 for (ringid = fringid; ringid <= lringid; ringid++) {
113 ring_id_t ringid2;
114
115 channel = sktu_channel_create_extended(channel_uuid, 0,
116 CHANNEL_DIR_TX_RX, ringid, NULL,
117 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
118 assert(channel);
119
120 ringid2 = os_channel_ring_id(channel, first);
121 assert(ringid2 == ringid);
122
123 ringid2 = os_channel_ring_id(channel, last);
124 assert(ringid2 == ringid);
125
126 for (ringid2 = fringid; ringid2 <= lringid + 1; ringid2++) {
127 ring = (*get_ring)(channel, ringid2);
128 assert(ringid2 != ringid || ring);
129 assert(ringid2 == ringid || !ring); // This verifies rdar://problem/24849324
130 }
131
132 os_channel_destroy(channel);
133 }
134
135 /* Now try to reopen the channel with an invalid ringid */
136 assert(ringid == lringid + 1);
137 channel = sktu_channel_create_extended(channel_uuid, 0,
138 CHANNEL_DIR_TX_RX, ringid, NULL,
139 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
140 assert(!channel);
141
142 return 0;
143 }
144
145 /****************************************************************/
146
147 static int
skt_ringidtx_main(int argc,char * argv[])148 skt_ringidtx_main(int argc, char *argv[])
149 {
150 return skt_ringid_main_common(argc, argv,
151 NUM_TX_RINGS, CHANNEL_FIRST_TX_RING, CHANNEL_LAST_TX_RING,
152 &os_channel_tx_ring);
153 }
154
155 static int
skt_ringidrx_main(int argc,char * argv[])156 skt_ringidrx_main(int argc, char *argv[])
157 {
158 return skt_ringid_main_common(argc, argv,
159 NUM_RX_RINGS, CHANNEL_FIRST_RX_RING, CHANNEL_LAST_RX_RING,
160 &os_channel_rx_ring);
161 }
162
163 struct skywalk_test skt_ringidtx = {
164 "ringidtx", "tests opening tx ringids",
165 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
166 skt_ringidtx_main, SKTC_GENERIC_UPIPE_ARGV,
167 skt_ringid_init, skt_ringid_fini,
168 };
169
170 struct skywalk_test skt_ringidrx = {
171 "ringidrx", "tests opening rx ringids",
172 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
173 skt_ringidrx_main, SKTC_GENERIC_UPIPE_ARGV,
174 skt_ringid_init, skt_ringid_fini,
175 };
176
177 /****************************************************************/
178