libc: split libc and syscall interposing (1/2)

System calls or their wrappers are now interposed by
__libsys_interposing with purely libc entries remaining in
__libc_interposing.

Use __libsys_interposing_slot in libthr to update __libsys_interposing,
but also make __libc_interposing_slot fall back to
__libsys_interposing_slot so an out of date libc has a chance of working
during updates.

Reviewed by:	kib, emaste, imp
Pull Request:	https://github.com/freebsd/freebsd-src/pull/908
This commit is contained in:
Brooks Davis 2024-01-16 20:16:39 +00:00
parent da63ac1b07
commit 9cbd96582c
52 changed files with 85 additions and 101 deletions

View file

@ -43,9 +43,8 @@ int __creat(const char *path, mode_t mode);
int
__creat(const char *path, mode_t mode)
{
return (((int (*)(int, const char *, int, ...))
__libc_interposing[INTERPOS_openat])(AT_FDCWD, path, O_WRONLY |
__libsys_interposing[INTERPOS_openat])(AT_FDCWD, path, O_WRONLY |
O_CREAT | O_TRUNC, mode));
}

View file

@ -63,7 +63,7 @@ lockf(int filedes, int function, off_t size)
case F_TEST:
fl.l_type = F_WRLCK;
if (((int (*)(int, int, ...))
__libc_interposing[INTERPOS_fcntl])(filedes, F_GETLK, &fl)
__libsys_interposing[INTERPOS_fcntl])(filedes, F_GETLK, &fl)
== -1)
return (-1);
if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 &&
@ -79,5 +79,5 @@ lockf(int filedes, int function, off_t size)
}
return (((int (*)(int, int, ...))
__libc_interposing[INTERPOS_fcntl])(filedes, cmd, &fl));
__libsys_interposing[INTERPOS_fcntl])(filedes, cmd, &fl));
}

View file

@ -56,7 +56,7 @@ __sleep(unsigned int seconds)
time_to_sleep.tv_sec = seconds;
time_to_sleep.tv_nsec = 0;
if (((int (*)(const struct timespec *, struct timespec *))
__libc_interposing[INTERPOS_nanosleep])(
__libsys_interposing[INTERPOS_nanosleep])(
&time_to_sleep, &time_remaining) != -1)
return (0);
if (errno != EINTR)

View file

@ -46,7 +46,7 @@ __usleep(useconds_t useconds)
time_to_sleep.tv_nsec = (useconds % 1000000) * 1000;
time_to_sleep.tv_sec = useconds / 1000000;
return (((int (*)(const struct timespec *, struct timespec *))
__libc_interposing[INTERPOS_nanosleep])(&time_to_sleep, NULL));
__libsys_interposing[INTERPOS_nanosleep])(&time_to_sleep, NULL));
}
__weak_reference(__usleep, usleep);

View file

@ -43,9 +43,8 @@ pid_t __wait(int *);
pid_t
__wait(int *istat)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
__libc_interposing[INTERPOS_wait4])(WAIT_ANY, istat, 0, NULL));
__libsys_interposing[INTERPOS_wait4])(WAIT_ANY, istat, 0, NULL));
}
__weak_reference(__wait, wait);

View file

@ -43,9 +43,9 @@ pid_t __wait3(int *, int, struct rusage *);
pid_t
__wait3(int *istat, int options, struct rusage *rup)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
__libc_interposing[INTERPOS_wait4])(WAIT_ANY, istat, options, rup));
__libsys_interposing[INTERPOS_wait4])(WAIT_ANY, istat, options,
rup));
}
__weak_reference(__wait3, wait3);

View file

@ -47,7 +47,7 @@ __waitid(idtype_t idtype, id_t id, siginfo_t *info, int flags)
pid_t ret;
ret = ((pid_t (*)(idtype_t, id_t, int *, int, struct __wrusage *,
siginfo_t *))__libc_interposing[INTERPOS_wait6])(idtype, id,
siginfo_t *))__libsys_interposing[INTERPOS_wait6])(idtype, id,
&status, flags, NULL, info);
/*

View file

@ -43,9 +43,8 @@ pid_t __waitpid(pid_t, int *, int);
pid_t
__waitpid(pid_t pid, int *istat, int options)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
__libc_interposing[INTERPOS_wait4])(pid, istat, options, NULL));
__libsys_interposing[INTERPOS_wait4])(pid, istat, options, NULL));
}
__weak_reference(__waitpid, waitpid);

View file

@ -200,6 +200,8 @@ int _pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex,
typedef int (*interpos_func_t)(void);
interpos_func_t *__libc_interposing_slot(int interposno);
extern interpos_func_t __libc_interposing[] __hidden;
interpos_func_t *__libsys_interposing_slot(int interposno);
extern interpos_func_t __libsys_interposing[] __hidden;
enum {
INTERPOS_accept,

View file

@ -44,6 +44,6 @@ recv(int s, void *buf, size_t len, int flags)
*/
return (((ssize_t (*)(int, void *, size_t, int,
struct sockaddr *, socklen_t *))
__libc_interposing[INTERPOS_recvfrom])(s, buf, len, flags,
__libsys_interposing[INTERPOS_recvfrom])(s, buf, len, flags,
NULL, NULL));
}

View file

@ -44,6 +44,6 @@ send(int s, const void *msg, size_t len, int flags)
*/
return (((ssize_t (*)(int, const void *, size_t, int,
const struct sockaddr *, socklen_t))
__libc_interposing[INTERPOS_sendto])(s, msg, len, flags,
__libsys_interposing[INTERPOS_sendto])(s, msg, len, flags,
NULL, 0));
}

View file

@ -1051,6 +1051,7 @@ FBSDprivate_1.0 {
/* Things that aren't system calls (in sort(1) order) */
__libc_interposing_slot;
__libc_sigwait;
__libsys_interposing_slot;
__set_error_selector;
__sigwait;
gssd_syscall;

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_accept, __accept);
int
accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
return (((int (*)(int, struct sockaddr *, socklen_t *))
__libc_interposing[INTERPOS_accept])(s, addr, addrlen));
__libsys_interposing[INTERPOS_accept])(s, addr, addrlen));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_accept4, __accept4);
int
accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
return (((int (*)(int, struct sockaddr *, socklen_t *, int))
__libc_interposing[INTERPOS_accept4])(s, addr, addrlen, flags));
__libsys_interposing[INTERPOS_accept4])(s, addr, addrlen, flags));
}

View file

@ -40,8 +40,7 @@ int
aio_suspend(const struct aiocb * const iocbs[], int niocb,
const struct timespec *timeout)
{
return (((int (*)(const struct aiocb * const[], int,
const struct timespec *))
__libc_interposing[INTERPOS_aio_suspend])(iocbs, niocb, timeout));
__libsys_interposing[INTERPOS_aio_suspend])(iocbs, niocb, timeout));
}

View file

@ -41,9 +41,8 @@ int
clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
struct timespec *rmtp)
{
return (((int (*)(clockid_t, int, const struct timespec *,
struct timespec *))
__libc_interposing[INTERPOS_clock_nanosleep])(clock_id, flags,
__libsys_interposing[INTERPOS_clock_nanosleep])(clock_id, flags,
rqtp, rmtp));
}

View file

@ -40,6 +40,5 @@ __weak_reference(__sys_close, __close);
int
close(int fd)
{
return (((int (*)(int))__libc_interposing[INTERPOS_close])(fd));
return (((int (*)(int))__libsys_interposing[INTERPOS_close])(fd));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_connect, __connect);
int
connect(int s, const struct sockaddr *addr, socklen_t addrlen)
{
return (((int (*)(int, const struct sockaddr *, socklen_t))
__libc_interposing[INTERPOS_connect])(s, addr, addrlen));
__libsys_interposing[INTERPOS_connect])(s, addr, addrlen));
}

View file

@ -50,5 +50,5 @@ fcntl(int fd, int cmd, ...)
va_end(args);
return (((int (*)(int, int, ...))
__libc_interposing[INTERPOS_fcntl])(fd, cmd, arg));
__libsys_interposing[INTERPOS_fcntl])(fd, cmd, arg));
}

View file

@ -37,6 +37,5 @@
int
fdatasync(int fd)
{
return (((int (*)(int))__libc_interposing[INTERPOS_fdatasync])(fd));
return (((int (*)(int))__libsys_interposing[INTERPOS_fdatasync])(fd));
}

View file

@ -39,6 +39,5 @@ __weak_reference(__sys_fork, __fork);
pid_t
fork(void)
{
return (((pid_t (*)(void))__libc_interposing[INTERPOS_fork])());
return (((pid_t (*)(void))__libsys_interposing[INTERPOS_fork])());
}

View file

@ -39,6 +39,5 @@ __weak_reference(__sys_fsync, __fsync);
int
fsync(int fd)
{
return (((int (*)(int))__libc_interposing[INTERPOS_fsync])(fd));
return (((int (*)(int))__libsys_interposing[INTERPOS_fsync])(fd));
}

View file

@ -30,11 +30,22 @@
*/
#include <sys/types.h>
#include <stddef.h>
#include "libc_private.h"
#define SLOT(a, b) \
[INTERPOS_##a] = (interpos_func_t)b
interpos_func_t __libc_interposing[INTERPOS_MAX] = {
SLOT(system, __libc_system),
SLOT(tcdrain, __libc_tcdrain),
SLOT(_pthread_mutex_init_calloc_cb, _pthread_mutex_init_calloc_cb_stub),
SLOT(spinlock, __libc_spinlock_stub),
SLOT(spinunlock, __libc_spinunlock_stub),
SLOT(map_stacks_exec, __libc_map_stacks_exec),
SLOT(distribute_static_tls, __libc_distribute_static_tls),
};
interpos_func_t __libsys_interposing[INTERPOS_MAX] = {
SLOT(accept, __sys_accept),
SLOT(accept4, __sys_accept4),
SLOT(aio_suspend, __sys_aio_suspend),
@ -63,21 +74,14 @@ interpos_func_t __libc_interposing[INTERPOS_MAX] = {
SLOT(sigtimedwait, __sys_sigtimedwait),
SLOT(sigwaitinfo, __sys_sigwaitinfo),
SLOT(swapcontext, __sys_swapcontext),
SLOT(system, __libc_system),
SLOT(tcdrain, __libc_tcdrain),
SLOT(wait4, __sys_wait4),
SLOT(write, __sys_write),
SLOT(writev, __sys_writev),
SLOT(_pthread_mutex_init_calloc_cb, _pthread_mutex_init_calloc_cb_stub),
SLOT(spinlock, __libc_spinlock_stub),
SLOT(spinunlock, __libc_spinunlock_stub),
SLOT(kevent, __sys_kevent),
SLOT(wait6, __sys_wait6),
SLOT(ppoll, __sys_ppoll),
SLOT(map_stacks_exec, __libc_map_stacks_exec),
SLOT(fdatasync, __sys_fdatasync),
SLOT(clock_nanosleep, __sys_clock_nanosleep),
SLOT(distribute_static_tls, __libc_distribute_static_tls),
SLOT(pdfork, __sys_pdfork),
};
#undef SLOT
@ -85,6 +89,14 @@ interpos_func_t __libc_interposing[INTERPOS_MAX] = {
interpos_func_t *
__libc_interposing_slot(int interposno)
{
/* XXX: forward compat. Remove after 15.0-RELEASE. */
if (__libc_interposing[interposno] == NULL)
return (__libsys_interposing_slot(interposno));
return (&__libc_interposing[interposno]);
}
interpos_func_t *
__libsys_interposing_slot(int interposno)
{
return (&__libsys_interposing[interposno]);
}

View file

@ -41,9 +41,8 @@ int
kevent(int kq, const struct kevent *changelist, int nchanges,
struct kevent *eventlist, int nevents, const struct timespec *timeout)
{
return (((int (*)(int, const struct kevent *, int,
struct kevent *, int, const struct timespec *))
__libc_interposing[INTERPOS_kevent])(kq, changelist, nchanges,
__libsys_interposing[INTERPOS_kevent])(kq, changelist, nchanges,
eventlist, nevents, timeout));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_msync, __msync);
int
msync(void *addr, size_t len, int flags)
{
return (((int (*)(void *, size_t, int))
__libc_interposing[INTERPOS_msync])(addr, len, flags));
__libsys_interposing[INTERPOS_msync])(addr, len, flags));
}

View file

@ -39,7 +39,6 @@ __weak_reference(__sys_nanosleep, __nanosleep);
int
nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
return (((int (*)(const struct timespec *, struct timespec *))
__libc_interposing[INTERPOS_nanosleep])(rqtp, rmtp));
__libsys_interposing[INTERPOS_nanosleep])(rqtp, rmtp));
}

View file

@ -51,5 +51,6 @@ open(const char *path, int flags, ...)
mode = 0;
}
return (((int (*)(int, const char *, int, ...))
__libc_interposing[INTERPOS_openat])(AT_FDCWD, path, flags, mode));
__libsys_interposing[INTERPOS_openat])(AT_FDCWD, path, flags,
mode));
}

View file

@ -54,5 +54,5 @@ openat(int fd, const char *path, int flags, ...)
mode = 0;
}
return (((int (*)(int, const char *, int, ...))
__libc_interposing[INTERPOS_openat])(fd, path, flags, mode));
__libsys_interposing[INTERPOS_openat])(fd, path, flags, mode));
}

View file

@ -37,6 +37,6 @@
pid_t
pdfork(int *fdp, int flags)
{
return (((pid_t (*)(int *, int))__libc_interposing[
INTERPOS_pdfork])(fdp, flags));
return (((pid_t (*)(int *, int))__libsys_interposing[INTERPOS_pdfork])
(fdp, flags));
}

View file

@ -39,7 +39,6 @@ __weak_reference(__sys_poll, __poll);
int
poll(struct pollfd pfd[], nfds_t nfds, int timeout)
{
return (((int (*)(struct pollfd *, nfds_t, int))
__libc_interposing[INTERPOS_poll])(pfd, nfds, timeout));
__libsys_interposing[INTERPOS_poll])(pfd, nfds, timeout));
}

View file

@ -40,8 +40,7 @@ int
ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *__restrict
timeout, const sigset_t *__restrict newsigmask)
{
return (((int (*)(struct pollfd *, nfds_t, const struct timespec *,
const sigset_t *)) __libc_interposing[INTERPOS_ppoll])(pfd, nfds,
const sigset_t *)) __libsys_interposing[INTERPOS_ppoll])(pfd, nfds,
timeout, newsigmask));
}

View file

@ -40,8 +40,7 @@ int
pselect(int n, fd_set *rs, fd_set *ws, fd_set *es, const struct timespec *t,
const sigset_t *s)
{
return (((int (*)(int, fd_set *, fd_set *, fd_set *,
const struct timespec *, const sigset_t *))
__libc_interposing[INTERPOS_pselect])(n, rs, ws, es, t, s));
__libsys_interposing[INTERPOS_pselect])(n, rs, ws, es, t, s));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_read, __read);
ssize_t
read(int fd, void *buf, size_t nbytes)
{
return (((ssize_t (*)(int, void *, size_t))
__libc_interposing[INTERPOS_read])(fd, buf, nbytes));
__libsys_interposing[INTERPOS_read])(fd, buf, nbytes));
}

View file

@ -41,7 +41,6 @@ __weak_reference(__sys_readv, __readv);
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{
return (((ssize_t (*)(int, const struct iovec *, int))
__libc_interposing[INTERPOS_readv])(fd, iov, iovcnt));
__libsys_interposing[INTERPOS_readv])(fd, iov, iovcnt));
}

View file

@ -41,9 +41,8 @@ ssize_t
recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr * __restrict from, socklen_t * __restrict fromlen)
{
return (((ssize_t (*)(int, void *, size_t, int,
struct sockaddr *, socklen_t *))
__libc_interposing[INTERPOS_recvfrom])(s, buf, len, flags,
__libsys_interposing[INTERPOS_recvfrom])(s, buf, len, flags,
from, fromlen));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_recvmsg, __recvmsg);
ssize_t
recvmsg(int s, struct msghdr *msg, int flags)
{
return (((int (*)(int, struct msghdr *, int))
__libc_interposing[INTERPOS_recvmsg])(s, msg, flags));
__libsys_interposing[INTERPOS_recvmsg])(s, msg, flags));
}

View file

@ -39,7 +39,6 @@ __weak_reference(__sys_select, __select);
int
select(int n, fd_set *rs, fd_set *ws, fd_set *es, struct timeval *t)
{
return (((int (*)(int, fd_set *, fd_set *, fd_set *, struct timeval *))
__libc_interposing[INTERPOS_select])(n, rs, ws, es, t));
__libsys_interposing[INTERPOS_select])(n, rs, ws, es, t));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_sendmsg, __sendmsg);
ssize_t
sendmsg(int s, const struct msghdr *msg, int flags)
{
return (((int (*)(int, const struct msghdr *, int))
__libc_interposing[INTERPOS_sendmsg])(s, msg, flags));
__libsys_interposing[INTERPOS_sendmsg])(s, msg, flags));
}

View file

@ -41,9 +41,8 @@ ssize_t
sendto(int s, const void *msg, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
return (((ssize_t (*)(int, const void *, size_t, int,
const struct sockaddr *, socklen_t))
__libc_interposing[INTERPOS_sendto])(s, msg, len, flags,
__libsys_interposing[INTERPOS_sendto])(s, msg, len, flags,
to, tolen));
}

View file

@ -42,7 +42,6 @@ __sym_default(setcontext, setcontext, FBSD_1.2);
int
setcontext(const ucontext_t *uc)
{
return (((int (*)(const ucontext_t *))
__libc_interposing[INTERPOS_setcontext])(uc));
__libsys_interposing[INTERPOS_setcontext])(uc));
}

View file

@ -40,7 +40,6 @@ __weak_reference(sigaction, __libc_sigaction);
int
sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
{
return (((int (*)(int, const struct sigaction *, struct sigaction *))
__libc_interposing[INTERPOS_sigaction])(sig, act, oact));
__libsys_interposing[INTERPOS_sigaction])(sig, act, oact));
}

View file

@ -40,7 +40,6 @@ __weak_reference(sigprocmask, __libc_sigprocmask);
int
sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
return (((int (*)(int, const sigset_t *, sigset_t *))
__libc_interposing[INTERPOS_sigprocmask])(how, set, oset));
__libsys_interposing[INTERPOS_sigprocmask])(how, set, oset));
}

View file

@ -40,7 +40,6 @@ __weak_reference(sigsuspend, __libc_sigsuspend);
int
sigsuspend(const sigset_t *set)
{
return (((int (*)(const sigset_t *))
__libc_interposing[INTERPOS_sigsuspend])(set));
__libsys_interposing[INTERPOS_sigsuspend])(set));
}

View file

@ -40,8 +40,7 @@ int
sigtimedwait(const sigset_t * __restrict set, siginfo_t * __restrict info,
const struct timespec * __restrict t)
{
return (((int (*)(const sigset_t *, siginfo_t *,
const struct timespec *))
__libc_interposing[INTERPOS_sigtimedwait])(set, info, t));
__libsys_interposing[INTERPOS_sigtimedwait])(set, info, t));
}

View file

@ -35,9 +35,8 @@ __weak_reference(__libc_sigwait, __sigwait);
int
sigwait(const sigset_t *set, int *sig)
{
return (((int (*)(const sigset_t *, int *))
__libc_interposing[INTERPOS_sigwait])(set, sig));
__libsys_interposing[INTERPOS_sigwait])(set, sig));
}
int

View file

@ -39,7 +39,6 @@ __weak_reference(__sys_sigwaitinfo, __sigwaitinfo);
int
sigwaitinfo(const sigset_t * __restrict set, siginfo_t * __restrict info)
{
return (((int (*)(const sigset_t *, siginfo_t *))
__libc_interposing[INTERPOS_sigwaitinfo])(set, info));
__libsys_interposing[INTERPOS_sigwaitinfo])(set, info));
}

View file

@ -44,7 +44,6 @@ __sym_default(swapcontext, swapcontext, FBSD_1.2);
int
swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
{
return (((int (*)(ucontext_t *, const ucontext_t *))
__libc_interposing[INTERPOS_swapcontext])(oucp, ucp));
__libsys_interposing[INTERPOS_swapcontext])(oucp, ucp));
}

View file

@ -39,7 +39,6 @@ __weak_reference(__sys_wait4, __wait4);
pid_t
wait4(pid_t pid, int *status, int options, struct rusage *ru)
{
return (((pid_t (*)(pid_t, int *, int, struct rusage *))
__libc_interposing[INTERPOS_wait4])(pid, status, options, ru));
__libsys_interposing[INTERPOS_wait4])(pid, status, options, ru));
}

View file

@ -41,8 +41,7 @@ pid_t
wait6(idtype_t idtype, id_t id, int *status, int options, struct __wrusage *ru,
siginfo_t *infop)
{
return (((pid_t (*)(idtype_t, id_t, int *, int, struct __wrusage *,
siginfo_t *))__libc_interposing[INTERPOS_wait6])(idtype, id,
siginfo_t *))__libsys_interposing[INTERPOS_wait6])(idtype, id,
status, options, ru, infop));
}

View file

@ -40,7 +40,6 @@ __weak_reference(__sys_write, __write);
ssize_t
write(int fd, const void *buf, size_t nbytes)
{
return (((ssize_t (*)(int, const void *, size_t))
__libc_interposing[INTERPOS_write])(fd, buf, nbytes));
__libsys_interposing[INTERPOS_write])(fd, buf, nbytes));
}

View file

@ -41,7 +41,6 @@ __weak_reference(__sys_writev, __writev);
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{
return (((ssize_t (*)(int, const struct iovec *, int))
__libc_interposing[INTERPOS_writev])(fd, iov, iovcnt));
__libsys_interposing[INTERPOS_writev])(fd, iov, iovcnt));
}

View file

@ -644,6 +644,16 @@ __thr_interpose_libc(void)
#define SLOT(name) \
*(__libc_interposing_slot(INTERPOS_##name)) = \
(interpos_func_t)__thr_##name;
SLOT(system);
SLOT(tcdrain);
SLOT(spinlock);
SLOT(spinunlock);
SLOT(map_stacks_exec);
#undef SLOT
#define SLOT(name) \
*(__libsys_interposing_slot(INTERPOS_##name)) = \
(interpos_func_t)__thr_##name;
SLOT(accept);
SLOT(accept4);
SLOT(aio_suspend);
@ -672,17 +682,12 @@ __thr_interpose_libc(void)
SLOT(sigtimedwait);
SLOT(sigwaitinfo);
SLOT(swapcontext);
SLOT(system);
SLOT(tcdrain);
SLOT(wait4);
SLOT(write);
SLOT(writev);
SLOT(spinlock);
SLOT(spinunlock);
SLOT(kevent);
SLOT(wait6);
SLOT(ppoll);
SLOT(map_stacks_exec);
SLOT(fdatasync);
SLOT(clock_nanosleep);
SLOT(pdfork);