Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
CRuntime_Musl: More fixes for time64
Browse files Browse the repository at this point in the history
The original PR (#3275) missed quite a few spots and conversions,
which led to the build on Alpine Linux failing with Aithmetic Exception
on core.time module constructor.
Links to the two offending commits are included.
For further issues / investigation, search for 'time64' in the git repository.

Co-Authored-By: Ömer Faruk IRMAK <[email protected]>
  • Loading branch information
Geod24 and omerfirmak committed Feb 26, 2021
1 parent 23c6cc0 commit c29b303
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 54 deletions.
59 changes: 45 additions & 14 deletions src/core/stdc/time.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
module core.stdc.time;

version (Posix)
{
public import core.sys.posix.stdc.time;
import core.sys.posix.sys.types : CRuntime_Musl_Needs_Time64_Compat_Layer;
}
else version (Windows)
{
public import core.sys.windows.stdc.time;
// This enum is defined only for Posix, this file is the only one
// needing it in `core.stdc`.
private enum CRuntime_Musl_Needs_Time64_Compat_Layer = false;
}
else
static assert(0, "unsupported system");

Expand All @@ -29,20 +37,43 @@ extern (C):
nothrow:
@nogc:

///
pure double difftime(time_t time1, time_t time0); // MT-Safe
///
@system time_t mktime(scope tm* timeptr); // @system: MT-Safe env locale
///
time_t time(scope time_t* timer);
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
pure double __difftime64(time_t time1, time_t time0); // MT-Safe
@system time_t __mktime64(scope tm* timeptr); // @system: MT-Safe env locale
time_t __time64(scope time_t* timer);
@system char* __ctime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
@system tm* __gmtime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
@system tm* __localtime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale

///
alias time = __time64;
///
alias difftime = __difftime64;
///
alias mktime = __mktime64;
///
alias gmtime = __gmtime64;
///
alias localtime = __localtime64;
///
alias ctime = __ctime64;
}
else
{
///
pure double difftime(time_t time1, time_t time0); // MT-Safe
///
@system time_t mktime(scope tm* timeptr); // @system: MT-Safe env locale
///
time_t time(scope time_t* timer);
///
@system char* ctime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
///
@system tm* gmtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
///
@system tm* localtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
}

///
@system char* asctime(const scope tm* timeptr); // @system: MT-Unsafe race:asctime locale
///
@system char* ctime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
///
@system tm* gmtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
///
@system tm* localtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
///
@system size_t strftime(scope char* s, size_t maxsize, const scope char* format, const scope tm* timeptr); // @system: MT-Safe env locale
22 changes: 19 additions & 3 deletions src/core/sys/linux/sys/socket.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ extern(C):
@nogc:
nothrow:

static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
// SO_TIMESTAMP_OLD & friends
// https://www.kernel.org/doc/Documentation/networking/timestamping.txt
enum SO_TIMESTAMP = 29;
enum SO_TIMESTAMPNS = 35;
enum SO_TIMESTAMPING = 37;

}
else
{
enum SO_TIMESTAMP = 63;
enum SO_TIMESTAMPNS = 64;
enum SO_TIMESTAMPING = 65;
}

enum
{
// Protocol families.
Expand Down Expand Up @@ -123,14 +139,14 @@ enum
SO_GET_FILTER = SO_ATTACH_FILTER,

SO_PEERNAME = 28,
SO_TIMESTAMP = 29,
// SO_TIMESTAMP See above
SCM_TIMESTAMP = SO_TIMESTAMP,

SO_PASSSEC = 34,
SO_TIMESTAMPNS = 35,
// SO_TIMESTAMPNS See above
SCM_TIMESTAMPNS = SO_TIMESTAMPNS,
SO_MARK = 36,
SO_TIMESTAMPING = 37,
// SO_TIMESTAMPING See above
SCM_TIMESTAMPING = SO_TIMESTAMPING,
SO_RXQ_OVFL = 40,
SO_WIFI_STATUS = 41,
Expand Down
16 changes: 14 additions & 2 deletions src/core/sys/linux/timerfd.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ extern (C):
@nogc:
nothrow:

static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __timerfd_settime64(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
int __timerfd_gettime64(int fd, itimerspec* curr_value);

alias timerfd_settime = __timerfd_settime64;
alias timerfd_gettime = __timerfd_gettime64;
}
else
{
int timerfd_settime(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
int timerfd_gettime(int fd, itimerspec* curr_value);
}

int timerfd_create(int clockid, int flags);
int timerfd_settime(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
int timerfd_gettime(int fd, itimerspec* curr_value);

enum TFD_TIMER_ABSTIME = 1 << 0;
enum TFD_TIMER_CANCEL_ON_SET = 1 << 1;
Expand Down
19 changes: 19 additions & 0 deletions src/core/sys/posix/aio.d
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,25 @@ else version (CRuntime_UClibc)
int lio_listio(int mode, const(aiocb*)* aiocb_list, int nitems, sigevent* sevp);
}
}
else version (CRuntime_Musl)
{
int aio_read(aiocb* aiocbp);
int aio_write(aiocb* aiocbp);
int aio_fsync(int op, aiocb* aiocbp);
int aio_error(const(aiocb)* aiocbp);
ssize_t aio_return(aiocb* aiocbp);
int aio_cancel(int fd, aiocb* aiocbp);
int lio_listio(int mode, const(aiocb*)* aiocb_list, int nitems, sigevent* sevp);

import core.sys.posix.time : CRuntime_Musl_Needs_Time64_Compat_Layer;
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __aio_suspend_time64(const(aiocb*)* aiocb_list, int nitems, const(timespec)* timeout);
alias aio_suspend = __aio_suspend_time64;
}
else
int aio_suspend(const(aiocb*)* aiocb_list, int nitems, const(timespec)* timeout);
}
else version (OpenBSD)
{
// OpenBSD does not implement aio.h
Expand Down
11 changes: 10 additions & 1 deletion src/core/sys/posix/dlfcn.d
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ else version (CRuntime_Bionic)
}
else version (CRuntime_Musl)
{
import core.sys.posix.sys.types : CRuntime_Musl_Needs_Time64_Compat_Layer;

enum {
RTLD_LAZY = 1,
RTLD_NOW = 2,
Expand All @@ -293,7 +295,14 @@ else version (CRuntime_Musl)
int dlclose(void*);
const(char)* dlerror();
void* dlopen(const scope char*, int);
void* dlsym(void*, const scope char*);

static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
void* __dlsym_time64(void*, const scope char*);
alias dlsym = __dlsym_time64;
}
else
void* dlsym(void*, const scope char*);
}
else version (CRuntime_UClibc)
{
Expand Down
23 changes: 19 additions & 4 deletions src/core/sys/posix/mqueue.d
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,16 @@ ssize_t mq_receive (mqd_t mqdes, char* msg_ptr, size_t msg_len, uint* msg_prio);
* On success, mq_receive() returns the number of bytes in the received
* message; on error, -1 is returned, with errno set to indicate the error
*/
ssize_t mq_timedreceive (mqd_t mqdes, char* msg_ptr, size_t msg_len,
uint* msg_prio, const(timespec)* abs_timeout);
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
ssize_t __mq_timedreceive_time64 (mqd_t mqdes, char* msg_ptr, size_t msg_len,
uint* msg_prio, const(timespec)* abs_timeout);
alias mq_timedreceive = __mq_timedreceive_time64;

}
else
ssize_t mq_timedreceive (mqd_t mqdes, char* msg_ptr, size_t msg_len,
uint* msg_prio, const(timespec)* abs_timeout);


/**
Expand Down Expand Up @@ -217,5 +225,12 @@ int mq_send (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len, uint msg_prio);
* with errno set to indicate the error.
*
*/
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
uint msg_prio, const(timespec)* abs_timeout);
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
ssize_t __mq_timedreceive_time64 (mqd_t mqdes, char* msg_ptr, size_t msg_len,
uint* msg_prio, const(timespec)* abs_timeout);
alias mq_timedreceive = __mq_timedreceive_time64;
}
else
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
uint msg_prio, const(timespec)* abs_timeout);
18 changes: 17 additions & 1 deletion src/core/sys/posix/pthread.d
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,13 @@ int pthread_cond_broadcast(pthread_cond_t*);
int pthread_cond_destroy(pthread_cond_t*);
int pthread_cond_init(const scope pthread_cond_t*, pthread_condattr_t*) @trusted;
int pthread_cond_signal(pthread_cond_t*);
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __pthread_cond_timedwait_time64(pthread_cond_t*, pthread_mutex_t*, in timespec*);
alias pthread_cond_timedwait = __pthread_cond_timedwait_time64;
}
else
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*);
int pthread_condattr_destroy(pthread_condattr_t*);
int pthread_condattr_init(pthread_condattr_t*);
Expand Down Expand Up @@ -1315,6 +1321,16 @@ else version (CRuntime_Bionic)
int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const scope timespec*);
int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const scope timespec*);
}
else static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __pthread_mutex_timedlock_time64(pthread_mutex_t*, const scope timespec*);
int __pthread_rwlock_timedrdlock_time64(pthread_rwlock_t*, const scope timespec*);
int __pthread_rwlock_timedwrlock_time64(pthread_rwlock_t*, const scope timespec*);

alias pthread_mutex_timedlock = __pthread_mutex_timedlock_time64;
alias pthread_rwlock_timedrdlock = __pthread_rwlock_timedrdlock_time64;
alias pthread_rwlock_timedwrlock = __pthread_rwlock_timedwrlock_time64;
}
else version (CRuntime_Musl)
{
int pthread_mutex_timedlock(pthread_mutex_t*, const scope timespec*);
Expand Down
17 changes: 14 additions & 3 deletions src/core/sys/posix/sched.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ else version (CRuntime_Musl)
struct sched_param {
int sched_priority;
int sched_ss_low_priority;
timespec sched_ss_repl_period;
timespec sched_ss_init_budget;
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
long[4] reserved;
else
{
timespec sched_ss_repl_period;
timespec sched_ss_init_budget;
}
int sched_ss_max_repl;
}
}
Expand Down Expand Up @@ -306,7 +311,13 @@ else version (CRuntime_Musl)
{
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_rr_get_interval(pid_t, timespec*);
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __sched_rr_get_interval_time64(pid_t, timespec*);
alias sched_rr_get_interval = __sched_rr_get_interval_time64;
}
else
int sched_rr_get_interval(pid_t, timespec*);
}
else version (CRuntime_UClibc)
{
Expand Down
32 changes: 31 additions & 1 deletion src/core/sys/posix/signal.d
Original file line number Diff line number Diff line change
Expand Up @@ -3427,7 +3427,28 @@ struct timespec
}
*/

version (linux)
version (CRuntime_Musl)
{
// Musl on 32 bits use 64 bits time_t (time64)
// See https://git.musl-libc.org/cgit/musl/commit/?id=9b2921bea1d5017832e1b45d1fd64220047a9802
struct timespec
{
time_t tv_sec;
// 32 bits of padding on 32 bits, or in C:
// int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER==4321);
version (BigEndian)
static if (time_t.sizeof > c_long.sizeof)
int __padding;
c_long tv_nsec;
// Another 32 bits of padding on 32 bits:
// int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER!=4321);
version (LittleEndian)
static if (time_t.sizeof > c_long.sizeof)
int __padding;
};

}
else version (linux)
{
struct timespec
{
Expand Down Expand Up @@ -3681,6 +3702,15 @@ else version (CRuntime_Musl)
pthread_attr_t *sigev_notify_attributes;
char[56 - 3 * c_long.sizeof] __pad = void;
}

static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __sigtimedwait_time64(const scope sigset_t*, siginfo_t*, const scope timespec*);
alias sigtimedwait = __sigtimedwait_time64;
}
else
int sigtimedwait(const scope sigset_t*, siginfo_t*, const scope timespec*);

}
else version (CRuntime_UClibc)
{
Expand Down
10 changes: 10 additions & 0 deletions src/core/sys/posix/sys/resource.d
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,13 @@ else version (CRuntime_UClibc)
}
int getrusage(int, rusage*);
}
else version (CRuntime_Musl)
{
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __getrusage_time64(int, rusage*);
alias getrusage = __getrusage_time64;
}
else
int getrusage(int, rusage*);
}
16 changes: 13 additions & 3 deletions src/core/sys/posix/sys/select.d
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,19 @@ else version (CRuntime_Musl)
{
fdset.fds_bits[0 .. $] = 0;
}
int pselect(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
int select(int, fd_set*, fd_set*, fd_set*, timeval*);

static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
{
int __pselect_time64(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
int __select_time64(int, fd_set*, fd_set*, fd_set*, timeval*);
alias select = __select_time64;
alias pselect = __pselect_time64;
}
else
{
int pselect(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
int select(int, fd_set*, fd_set*, fd_set*, timeval*);
}
}
else version (CRuntime_UClibc)
{
Expand Down Expand Up @@ -608,4 +619,3 @@ pure unittest
assert(!FD_ISSET(i, &fd));
}
}

Loading

0 comments on commit c29b303

Please sign in to comment.