From 47cb4d7bd09fab414b782de6875dda87148aa4bf Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Sun, 21 Aug 2016 15:36:18 +0000 Subject: [PATCH] Add a utility macro for converting 64-bit pointers to native pointers. Right now we're casting uint64_t's to native pointers. This isn't causing any problems right now, but if we want to provide a 32-bit compatibility layer that works on 64-bit systems as well, this will cause problems. Casting a uint32_t to a 64-bit pointer throws a compiler error. Introduce a TO_PTR() macro that casts the value to uintptr_t before casting it to a pointer. --- sys/compat/cloudabi64/cloudabi64_fd.c | 9 ++++----- sys/compat/cloudabi64/cloudabi64_poll.c | 19 ++++++++++--------- sys/compat/cloudabi64/cloudabi64_sock.c | 15 +++++++++------ sys/compat/cloudabi64/cloudabi64_util.h | 2 ++ 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/sys/compat/cloudabi64/cloudabi64_fd.c b/sys/compat/cloudabi64/cloudabi64_fd.c index c3085b7320d..fcfcc42025e 100644 --- a/sys/compat/cloudabi64/cloudabi64_fd.c +++ b/sys/compat/cloudabi64/cloudabi64_fd.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include /* Copies in 64-bit iovec structures from userspace. */ static int @@ -70,7 +71,7 @@ cloudabi64_copyinuio(const cloudabi64_iovec_t *iovp, size_t iovcnt, free(uio, M_IOV); return (error); } - iov[i].iov_base = (void *)iovobj.iov_base; + iov[i].iov_base = TO_PTR(iovobj.iov_base); iov[i].iov_len = iovobj.iov_len; if (iov[i].iov_len > INT64_MAX - uio->uio_resid) { free(uio, M_IOV); @@ -105,8 +106,7 @@ cloudabi64_sys_fd_pwrite(struct thread *td, struct uio *uio; int error; - error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov, - uap->iovcnt, &uio); + error = cloudabi64_copyinuio(TO_PTR(uap->iov), uap->iovcnt, &uio); if (error != 0) return (error); error = kern_pwritev(td, uap->fd, uio, uap->offset); @@ -136,8 +136,7 @@ cloudabi64_sys_fd_write(struct thread *td, struct uio *uio; int error; - error = cloudabi64_copyinuio((const cloudabi64_iovec_t *)uap->iov, - uap->iovcnt, &uio); + error = cloudabi64_copyinuio(TO_PTR(uap->iov), uap->iovcnt, &uio); if (error != 0) return (error); error = kern_writev(td, uap->fd, uio); diff --git a/sys/compat/cloudabi64/cloudabi64_poll.c b/sys/compat/cloudabi64/cloudabi64_poll.c index e44d69f2e97..63d8120b63e 100644 --- a/sys/compat/cloudabi64/cloudabi64_poll.c +++ b/sys/compat/cloudabi64/cloudabi64_poll.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include /* Converts a FreeBSD signal number to a CloudABI signal number. */ static cloudabi_signal_t @@ -98,7 +99,7 @@ cloudabi64_kevent_copyin(void *arg, struct kevent *kevp, int count) return (error); memset(kevp, 0, sizeof(*kevp)); - kevp->udata = (void *)sub.userdata; + kevp->udata = TO_PTR(sub.userdata); switch (sub.type) { case CLOUDABI_EVENTTYPE_CLOCK: kevp->filter = EVFILT_TIMER; @@ -264,9 +265,9 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev.condvar.condvar = sub.condvar.condvar; ev.error = cloudabi_convert_errno( cloudabi_futex_condvar_wait( - td, (cloudabi_condvar_t *)sub.condvar.condvar, + td, TO_PTR(sub.condvar.condvar), sub.condvar.condvar_scope, - (cloudabi_lock_t *)sub.condvar.lock, + TO_PTR(sub.condvar.lock), sub.condvar.lock_scope, CLOUDABI_CLOCK_MONOTONIC, UINT64_MAX, 0)); td->td_retval[0] = 1; @@ -276,7 +277,7 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev.lock.lock = sub.lock.lock; ev.error = cloudabi_convert_errno( cloudabi_futex_lock_rdlock( - td, (cloudabi_lock_t *)sub.lock.lock, + td, TO_PTR(sub.lock.lock), sub.lock.lock_scope, CLOUDABI_CLOCK_MONOTONIC, UINT64_MAX, 0)); td->td_retval[0] = 1; @@ -286,7 +287,7 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev.lock.lock = sub.lock.lock; ev.error = cloudabi_convert_errno( cloudabi_futex_lock_wrlock( - td, (cloudabi_lock_t *)sub.lock.lock, + td, TO_PTR(sub.lock.lock), sub.lock.lock_scope, CLOUDABI_CLOCK_MONOTONIC, UINT64_MAX, 0)); td->td_retval[0] = 1; @@ -311,9 +312,9 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev[0].condvar.condvar = sub[0].condvar.condvar; ev[1].clock.identifier = sub[1].clock.identifier; error = cloudabi_futex_condvar_wait( - td, (cloudabi_condvar_t *)sub[0].condvar.condvar, + td, TO_PTR(sub[0].condvar.condvar), sub[0].condvar.condvar_scope, - (cloudabi_lock_t *)sub[0].condvar.lock, + TO_PTR(sub[0].condvar.lock), sub[0].condvar.lock_scope, sub[1].clock.clock_id, sub[1].clock.timeout, sub[1].clock.precision); if (error == ETIMEDOUT) { @@ -332,7 +333,7 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev[0].lock.lock = sub[0].lock.lock; ev[1].clock.identifier = sub[1].clock.identifier; error = cloudabi_futex_lock_rdlock( - td, (cloudabi_lock_t *)sub[0].lock.lock, + td, TO_PTR(sub[0].lock.lock), sub[0].lock.lock_scope, sub[1].clock.clock_id, sub[1].clock.timeout, sub[1].clock.precision); if (error == ETIMEDOUT) { @@ -351,7 +352,7 @@ cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) ev[0].lock.lock = sub[0].lock.lock; ev[1].clock.identifier = sub[1].clock.identifier; error = cloudabi_futex_lock_wrlock( - td, (cloudabi_lock_t *)sub[0].lock.lock, + td, TO_PTR(sub[0].lock.lock), sub[0].lock.lock_scope, sub[1].clock.clock_id, sub[1].clock.timeout, sub[1].clock.precision); if (error == ETIMEDOUT) { diff --git a/sys/compat/cloudabi64/cloudabi64_sock.c b/sys/compat/cloudabi64/cloudabi64_sock.c index e6b9c941625..3915ddf0c21 100644 --- a/sys/compat/cloudabi64/cloudabi64_sock.c +++ b/sys/compat/cloudabi64/cloudabi64_sock.c @@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket"); @@ -52,6 +53,7 @@ cloudabi64_sys_sock_recv(struct thread *td, cloudabi64_recv_out_t ro = {}; cloudabi64_iovec_t iovobj; struct msghdr msghdr = {}; + const cloudabi64_iovec_t *user_iov; size_t i; int error; @@ -65,14 +67,14 @@ cloudabi64_sys_sock_recv(struct thread *td, msghdr.msg_iovlen = ri.ri_datalen; msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec), M_SOCKET, M_WAITOK); + user_iov = TO_PTR(ri.ri_data); for (i = 0; i < msghdr.msg_iovlen; i++) { - error = copyin(&((cloudabi64_iovec_t *)ri.ri_data)[i], &iovobj, - sizeof(iovobj)); + error = copyin(&user_iov[i], &iovobj, sizeof(iovobj)); if (error != 0) { free(msghdr.msg_iov, M_SOCKET); return (error); } - msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base; + msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.iov_base); msghdr.msg_iov[i].iov_len = iovobj.iov_len; } msghdr.msg_name = &ss; @@ -104,6 +106,7 @@ cloudabi64_sys_sock_send(struct thread *td, cloudabi64_send_out_t so = {}; cloudabi64_ciovec_t iovobj; struct msghdr msghdr = {}; + const cloudabi64_ciovec_t *user_iov; size_t i; int error, flags; @@ -117,14 +120,14 @@ cloudabi64_sys_sock_send(struct thread *td, msghdr.msg_iovlen = si.si_datalen; msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec), M_SOCKET, M_WAITOK); + user_iov = TO_PTR(si.si_data); for (i = 0; i < msghdr.msg_iovlen; i++) { - error = copyin(&((cloudabi64_ciovec_t *)si.si_data)[i], &iovobj, - sizeof(iovobj)); + error = copyin(&user_iov[i], &iovobj, sizeof(iovobj)); if (error != 0) { free(msghdr.msg_iov, M_SOCKET); return (error); } - msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base; + msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.iov_base); msghdr.msg_iov[i].iov_len = iovobj.iov_len; } diff --git a/sys/compat/cloudabi64/cloudabi64_util.h b/sys/compat/cloudabi64/cloudabi64_util.h index 29a90d77982..6fd15771a50 100644 --- a/sys/compat/cloudabi64/cloudabi64_util.h +++ b/sys/compat/cloudabi64/cloudabi64_util.h @@ -38,6 +38,8 @@ struct thread; extern Elf64_Brandinfo cloudabi64_brand; +#define TO_PTR(x) ((void *)(uintptr_t)(x)) + /* Stack initialization during process execution. */ register_t *cloudabi64_copyout_strings(struct image_params *); int cloudabi64_fixup(register_t **, struct image_params *);