os-wrappers-test.c 8.24 KB
Newer Older
1 2
/*
 * Copyright © 2012 Collabora, Ltd.
3
 * Copyright © 2012 Intel Corporation
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
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
30
#include <sys/stat.h>
31 32 33
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
Pekka Paalanen's avatar
Pekka Paalanen committed
34 35
#include <stdarg.h>
#include <fcntl.h>
36
#include <stdio.h>
Pekka Paalanen's avatar
Pekka Paalanen committed
37
#include <sys/epoll.h>
38

39
#include "wayland-private.h"
40
#include "test-runner.h"
41
#include "wayland-os.h"
42 43 44 45

static int fall_back;

static int (*real_socket)(int, int, int);
Pekka Paalanen's avatar
Pekka Paalanen committed
46 47 48 49
static int wrapped_calls_socket;

static int (*real_fcntl)(int, int, ...);
static int wrapped_calls_fcntl;
50

51 52 53
static ssize_t (*real_recvmsg)(int, struct msghdr *, int);
static int wrapped_calls_recvmsg;

Pekka Paalanen's avatar
Pekka Paalanen committed
54 55 56
static int (*real_epoll_create1)(int);
static int wrapped_calls_epoll_create1;

57 58 59 60 61
static void
init_fallbacks(int do_fallbacks)
{
	fall_back = do_fallbacks;
	real_socket = dlsym(RTLD_NEXT, "socket");
Pekka Paalanen's avatar
Pekka Paalanen committed
62
	real_fcntl = dlsym(RTLD_NEXT, "fcntl");
63
	real_recvmsg = dlsym(RTLD_NEXT, "recvmsg");
Pekka Paalanen's avatar
Pekka Paalanen committed
64
	real_epoll_create1 = dlsym(RTLD_NEXT, "epoll_create1");
65 66 67 68 69
}

__attribute__ ((visibility("default"))) int
socket(int domain, int type, int protocol)
{
Pekka Paalanen's avatar
Pekka Paalanen committed
70
	wrapped_calls_socket++;
71 72 73 74 75 76 77 78 79

	if (fall_back && (type & SOCK_CLOEXEC)) {
		errno = EINVAL;
		return -1;
	}

	return real_socket(domain, type, protocol);
}

Pekka Paalanen's avatar
Pekka Paalanen committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
__attribute__ ((visibility("default"))) int
fcntl(int fd, int cmd, ...)
{
	va_list ap;
	void *arg;

	wrapped_calls_fcntl++;

	if (fall_back && (cmd == F_DUPFD_CLOEXEC)) {
		errno = EINVAL;
		return -1;
	}

	va_start(ap, cmd);
	arg = va_arg(ap, void*);
	va_end(ap);

	return real_fcntl(fd, cmd, arg);
}

100 101 102 103 104 105 106 107 108 109 110 111 112
__attribute__ ((visibility("default"))) ssize_t
recvmsg(int sockfd, struct msghdr *msg, int flags)
{
	wrapped_calls_recvmsg++;

	if (fall_back && (flags & MSG_CMSG_CLOEXEC)) {
		errno = EINVAL;
		return -1;
	}

	return real_recvmsg(sockfd, msg, flags);
}

Pekka Paalanen's avatar
Pekka Paalanen committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126
__attribute__ ((visibility("default"))) int
epoll_create1(int flags)
{
	wrapped_calls_epoll_create1++;

	if (fall_back) {
		wrapped_calls_epoll_create1++; /* epoll_create() not wrapped */
		errno = EINVAL;
		return -1;
	}

	return real_epoll_create1(flags);
}

127 128 129 130 131 132 133 134 135 136
static void
do_os_wrappers_socket_cloexec(int n)
{
	int fd;
	int nr_fds;

	nr_fds = count_open_fds();

	/* simply create a socket that closes on exec */
	fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
137
	assert(fd >= 0);
138 139 140 141 142

	/*
	 * Must have 2 calls if falling back, but must also allow
	 * falling back without a forced fallback.
	 */
Pekka Paalanen's avatar
Pekka Paalanen committed
143
	assert(wrapped_calls_socket > n);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	exec_fd_leak_check(nr_fds);
}

TEST(os_wrappers_socket_cloexec)
{
	/* normal case */
	init_fallbacks(0);
	do_os_wrappers_socket_cloexec(0);
}

TEST(os_wrappers_socket_cloexec_fallback)
{
	/* forced fallback */
	init_fallbacks(1);
	do_os_wrappers_socket_cloexec(1);
}
Pekka Paalanen's avatar
Pekka Paalanen committed
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

static void
do_os_wrappers_dupfd_cloexec(int n)
{
	int base_fd;
	int fd;
	int nr_fds;

	nr_fds = count_open_fds();

	base_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
	assert(base_fd >= 0);

	fd = wl_os_dupfd_cloexec(base_fd, 13);
	assert(fd >= 13);

	close(base_fd);

	/*
	 * Must have 4 calls if falling back, but must also allow
	 * falling back without a forced fallback.
	 */
	assert(wrapped_calls_fcntl > n);

	exec_fd_leak_check(nr_fds);
}

TEST(os_wrappers_dupfd_cloexec)
{
	init_fallbacks(0);
	do_os_wrappers_dupfd_cloexec(0);
}

TEST(os_wrappers_dupfd_cloexec_fallback)
{
	init_fallbacks(1);
	do_os_wrappers_dupfd_cloexec(3);
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

struct marshal_data {
	struct wl_connection *read_connection;
	struct wl_connection *write_connection;
	int s[2];
	uint32_t read_mask;
	uint32_t write_mask;
	union {
		int h[3];
	} value;
	int nr_fds_begin;
	int nr_fds_conn;
	int wrapped_calls;
};

static void
setup_marshal_data(struct marshal_data *data)
{
	assert(socketpair(AF_UNIX,
			  SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);

220
	data->read_connection = wl_connection_create(data->s[0]);
221 222
	assert(data->read_connection);

223
	data->write_connection = wl_connection_create(data->s[1]);
224 225 226 227
	assert(data->write_connection);
}

static void
Michael Vetter's avatar
Michael Vetter committed
228
marshal_demarshal(struct marshal_data *data,
229 230
		  void (*func)(void), int size, const char *format, ...)
{
231
	struct wl_closure *closure;
232 233 234 235
	static const int opcode = 4444;
	static struct wl_object sender = { NULL, NULL, 1234 };
	struct wl_message message = { "test", format, NULL };
	struct wl_map objects;
236
	struct wl_object object = { NULL, &func, 1234 };
237 238 239 240
	va_list ap;
	uint32_t msg[1] = { 1234 };

	va_start(ap, format);
241
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
242 243
	va_end(ap);

244 245 246
	assert(closure);
	assert(wl_closure_send(closure, data->write_connection) == 0);
	wl_closure_destroy(closure);
247 248 249
	assert(wl_connection_flush(data->write_connection) == size);

	assert(wl_connection_read(data->read_connection) == size);
250

251
	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
252
	object.id = msg[0];
253 254
	closure = wl_connection_demarshal(data->read_connection,
					  size, &objects, &message);
255
	assert(closure);
256
	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
257
	wl_closure_destroy(closure);
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
}

static void
validate_recvmsg_h(struct marshal_data *data,
		   struct wl_object *object, int fd1, int fd2, int fd3)
{
	struct stat buf1, buf2;

	assert(fd1 >= 0);
	assert(fd2 >= 0);
	assert(fd3 >= 0);

	assert(fd1 != data->value.h[0]);
	assert(fd2 != data->value.h[1]);
	assert(fd3 != data->value.h[2]);

	assert(fstat(fd3, &buf1) == 0);
	assert(fstat(data->value.h[2], &buf2) == 0);
	assert(buf1.st_dev == buf2.st_dev);
	assert(buf1.st_ino == buf2.st_ino);

	/* close the original file descriptors */
	close(data->value.h[0]);
	close(data->value.h[1]);
	close(data->value.h[2]);

	/* the dup'd (received) fds should still be open */
	assert(count_open_fds() == data->nr_fds_conn + 3);

	/*
	 * Must have 2 calls if falling back, but must also allow
	 * falling back without a forced fallback.
	 */
	assert(wrapped_calls_recvmsg > data->wrapped_calls);

	if (data->wrapped_calls == 0 && wrapped_calls_recvmsg > 1)
		printf("recvmsg fell back unforced.\n");

	/* all fds opened during the test in any way should be gone on exec */
	exec_fd_leak_check(data->nr_fds_begin);
}

static void
do_os_wrappers_recvmsg_cloexec(int n)
{
	struct marshal_data data;

	data.nr_fds_begin = count_open_fds();
	data.wrapped_calls = n;

	setup_marshal_data(&data);
	data.nr_fds_conn = count_open_fds();

	assert(pipe(data.value.h) >= 0);

	data.value.h[2] = open("/dev/zero", O_RDONLY);
	assert(data.value.h[2] >= 0);

	marshal_demarshal(&data, (void *) validate_recvmsg_h,
			  8, "hhh", data.value.h[0], data.value.h[1],
			  data.value.h[2]);
}

TEST(os_wrappers_recvmsg_cloexec)
{
	init_fallbacks(0);
	do_os_wrappers_recvmsg_cloexec(0);
}

TEST(os_wrappers_recvmsg_cloexec_fallback)
{
	init_fallbacks(1);
	do_os_wrappers_recvmsg_cloexec(1);
}
Pekka Paalanen's avatar
Pekka Paalanen committed
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

static void
do_os_wrappers_epoll_create_cloexec(int n)
{
	int fd;
	int nr_fds;

	nr_fds = count_open_fds();

	fd = wl_os_epoll_create_cloexec();
	assert(fd >= 0);

#ifdef EPOLL_CLOEXEC
	assert(wrapped_calls_epoll_create1 == n);
#else
	printf("No epoll_create1.\n");
#endif

	exec_fd_leak_check(nr_fds);
}

TEST(os_wrappers_epoll_create_cloexec)
{
	init_fallbacks(0);
	do_os_wrappers_epoll_create_cloexec(1);
}

TEST(os_wrappers_epoll_create_cloexec_fallback)
{
	init_fallbacks(1);
	do_os_wrappers_epoll_create_cloexec(2);
}
364 365

/* FIXME: add tests for wl_os_accept_cloexec() */