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

Commit 676a4a7

Browse files
Geod24omerfirmak
andcommitted
CRuntime_Musl: More fixes for time64
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]>
1 parent 3f75cae commit 676a4a7

File tree

16 files changed

+337
-57
lines changed

16 files changed

+337
-57
lines changed

src/core/stdc/time.d

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
module core.stdc.time;
1717

1818
version (Posix)
19+
{
1920
public import core.sys.posix.stdc.time;
21+
import core.sys.posix.sys.types : CRuntime_Musl_Needs_Time64_Compat_Layer;
22+
}
2023
else version (Windows)
24+
{
2125
public import core.sys.windows.stdc.time;
26+
// This enum is defined only for Posix, this file is the only one
27+
// needing it in `core.stdc`.
28+
private enum CRuntime_Musl_Needs_Time64_Compat_Layer = false;
29+
}
2230
else
2331
static assert(0, "unsupported system");
2432

@@ -29,20 +37,43 @@ extern (C):
2937
nothrow:
3038
@nogc:
3139

32-
///
33-
pure double difftime(time_t time1, time_t time0); // MT-Safe
34-
///
35-
@system time_t mktime(scope tm* timeptr); // @system: MT-Safe env locale
36-
///
37-
time_t time(scope time_t* timer);
40+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
41+
{
42+
pure double __difftime64(time_t time1, time_t time0); // MT-Safe
43+
@system time_t __mktime64(scope tm* timeptr); // @system: MT-Safe env locale
44+
time_t __time64(scope time_t* timer);
45+
@system char* __ctime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
46+
@system tm* __gmtime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
47+
@system tm* __localtime64(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
48+
49+
///
50+
alias time = __time64;
51+
///
52+
alias difftime = __difftime64;
53+
///
54+
alias mktime = __mktime64;
55+
///
56+
alias gmtime = __gmtime64;
57+
///
58+
alias localtime = __localtime64;
59+
///
60+
alias ctime = __ctime64;
61+
}
62+
else
63+
{
64+
///
65+
pure double difftime(time_t time1, time_t time0); // MT-Safe
66+
///
67+
@system time_t mktime(scope tm* timeptr); // @system: MT-Safe env locale
68+
///
69+
time_t time(scope time_t* timer);
70+
///
71+
@system char* ctime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
72+
///
73+
@system tm* gmtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
74+
///
75+
@system tm* localtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
76+
}
3877

3978
///
4079
@system char* asctime(const scope tm* timeptr); // @system: MT-Unsafe race:asctime locale
41-
///
42-
@system char* ctime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf race:asctime env locale
43-
///
44-
@system tm* gmtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
45-
///
46-
@system tm* localtime(const scope time_t* timer); // @system: MT-Unsafe race:tmbuf env locale
47-
///
48-
@system size_t strftime(scope char* s, size_t maxsize, const scope char* format, const scope tm* timeptr); // @system: MT-Safe env locale

src/core/sys/linux/sys/socket.d

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ extern(C):
1313
@nogc:
1414
nothrow:
1515

16+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
17+
{
18+
// SO_TIMESTAMP_OLD & friends
19+
// https://www.kernel.org/doc/Documentation/networking/timestamping.txt
20+
enum SO_TIMESTAMP = 29;
21+
enum SO_TIMESTAMPNS = 35;
22+
enum SO_TIMESTAMPING = 37;
23+
24+
}
25+
else
26+
{
27+
enum SO_TIMESTAMP = 63;
28+
enum SO_TIMESTAMPNS = 64;
29+
enum SO_TIMESTAMPING = 65;
30+
}
31+
1632
enum
1733
{
1834
// Protocol families.
@@ -123,14 +139,14 @@ enum
123139
SO_GET_FILTER = SO_ATTACH_FILTER,
124140

125141
SO_PEERNAME = 28,
126-
SO_TIMESTAMP = 29,
142+
// SO_TIMESTAMP See above
127143
SCM_TIMESTAMP = SO_TIMESTAMP,
128144

129145
SO_PASSSEC = 34,
130-
SO_TIMESTAMPNS = 35,
146+
// SO_TIMESTAMPNS See above
131147
SCM_TIMESTAMPNS = SO_TIMESTAMPNS,
132148
SO_MARK = 36,
133-
SO_TIMESTAMPING = 37,
149+
// SO_TIMESTAMPING See above
134150
SCM_TIMESTAMPING = SO_TIMESTAMPING,
135151
SO_RXQ_OVFL = 40,
136152
SO_WIFI_STATUS = 41,

src/core/sys/linux/timerfd.d

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ extern (C):
1515
@nogc:
1616
nothrow:
1717

18+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
19+
{
20+
int __timerfd_settime64(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
21+
int __timerfd_gettime64(int fd, itimerspec* curr_value);
22+
23+
alias timerfd_settime = __timerfd_settime64;
24+
alias timerfd_gettime = __timerfd_gettime64;
25+
}
26+
else
27+
{
28+
int timerfd_settime(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
29+
int timerfd_gettime(int fd, itimerspec* curr_value);
30+
}
31+
1832
int timerfd_create(int clockid, int flags);
19-
int timerfd_settime(int fd, int flags, const itimerspec* new_value, itimerspec* old_value);
20-
int timerfd_gettime(int fd, itimerspec* curr_value);
2133

2234
enum TFD_TIMER_ABSTIME = 1 << 0;
2335
enum TFD_TIMER_CANCEL_ON_SET = 1 << 1;

src/core/sys/posix/aio.d

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,25 @@ else version (CRuntime_UClibc)
488488
int lio_listio(int mode, const(aiocb*)* aiocb_list, int nitems, sigevent* sevp);
489489
}
490490
}
491+
else version (CRuntime_Musl)
492+
{
493+
int aio_read(aiocb* aiocbp);
494+
int aio_write(aiocb* aiocbp);
495+
int aio_fsync(int op, aiocb* aiocbp);
496+
int aio_error(const(aiocb)* aiocbp);
497+
ssize_t aio_return(aiocb* aiocbp);
498+
int aio_cancel(int fd, aiocb* aiocbp);
499+
int lio_listio(int mode, const(aiocb*)* aiocb_list, int nitems, sigevent* sevp);
500+
501+
import core.sys.posix.time : CRuntime_Musl_Needs_Time64_Compat_Layer;
502+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
503+
{
504+
int __aio_suspend_time64(const(aiocb*)* aiocb_list, int nitems, const(timespec)* timeout);
505+
alias aio_suspend = __aio_suspend_time64;
506+
}
507+
else
508+
int aio_suspend(const(aiocb*)* aiocb_list, int nitems, const(timespec)* timeout);
509+
}
491510
else version (OpenBSD)
492511
{
493512
// OpenBSD does not implement aio.h

src/core/sys/posix/dlfcn.d

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ else version (CRuntime_Bionic)
282282
}
283283
else version (CRuntime_Musl)
284284
{
285+
import core.sys.posix.sys.types : CRuntime_Musl_Needs_Time64_Compat_Layer;
286+
285287
enum {
286288
RTLD_LAZY = 1,
287289
RTLD_NOW = 2,
@@ -293,7 +295,13 @@ else version (CRuntime_Musl)
293295
int dlclose(void*);
294296
const(char)* dlerror();
295297
void* dlopen(const scope char*, int);
296-
void* dlsym(void*, const scope char*);
298+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
299+
{
300+
void* __dlsym_time64(void*, const scope char*);
301+
alias dlsym = __dlsym_time64;
302+
}
303+
else
304+
void* dlsym(void*, const scope char*);
297305

298306
int dladdr(scope const void *addr, Dl_info *info);
299307
struct Dl_info

src/core/sys/posix/mqueue.d

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,16 @@ ssize_t mq_receive (mqd_t mqdes, char* msg_ptr, size_t msg_len, uint* msg_prio);
177177
* On success, mq_receive() returns the number of bytes in the received
178178
* message; on error, -1 is returned, with errno set to indicate the error
179179
*/
180-
ssize_t mq_timedreceive (mqd_t mqdes, char* msg_ptr, size_t msg_len,
181-
uint* msg_prio, const(timespec)* abs_timeout);
180+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
181+
{
182+
ssize_t __mq_timedreceive_time64 (mqd_t mqdes, char* msg_ptr, size_t msg_len,
183+
uint* msg_prio, const(timespec)* abs_timeout);
184+
alias mq_timedreceive = __mq_timedreceive_time64;
185+
186+
}
187+
else
188+
ssize_t mq_timedreceive (mqd_t mqdes, char* msg_ptr, size_t msg_len,
189+
uint* msg_prio, const(timespec)* abs_timeout);
182190

183191

184192
/**
@@ -215,5 +223,12 @@ int mq_send (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len, uint msg_prio);
215223
* with errno set to indicate the error.
216224
*
217225
*/
218-
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
219-
uint msg_prio, const(timespec)* abs_timeout);
226+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
227+
{
228+
ssize_t __mq_timedreceive_time64 (mqd_t mqdes, char* msg_ptr, size_t msg_len,
229+
uint* msg_prio, const(timespec)* abs_timeout);
230+
alias mq_timedreceive = __mq_timedreceive_time64;
231+
}
232+
else
233+
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
234+
uint msg_prio, const(timespec)* abs_timeout);

src/core/sys/posix/pthread.d

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,13 @@ int pthread_cond_broadcast(pthread_cond_t*);
719719
int pthread_cond_destroy(pthread_cond_t*);
720720
int pthread_cond_init(const scope pthread_cond_t*, pthread_condattr_t*) @trusted;
721721
int pthread_cond_signal(pthread_cond_t*);
722-
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
722+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
723+
{
724+
int __pthread_cond_timedwait_time64(pthread_cond_t*, pthread_mutex_t*, in timespec*);
725+
alias pthread_cond_timedwait = __pthread_cond_timedwait_time64;
726+
}
727+
else
728+
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
723729
int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*);
724730
int pthread_condattr_destroy(pthread_condattr_t*);
725731
int pthread_condattr_init(pthread_condattr_t*);
@@ -1316,6 +1322,16 @@ else version (CRuntime_Bionic)
13161322
int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const scope timespec*);
13171323
int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const scope timespec*);
13181324
}
1325+
else static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
1326+
{
1327+
int __pthread_mutex_timedlock_time64(pthread_mutex_t*, const scope timespec*);
1328+
int __pthread_rwlock_timedrdlock_time64(pthread_rwlock_t*, const scope timespec*);
1329+
int __pthread_rwlock_timedwrlock_time64(pthread_rwlock_t*, const scope timespec*);
1330+
1331+
alias pthread_mutex_timedlock = __pthread_mutex_timedlock_time64;
1332+
alias pthread_rwlock_timedrdlock = __pthread_rwlock_timedrdlock_time64;
1333+
alias pthread_rwlock_timedwrlock = __pthread_rwlock_timedwrlock_time64;
1334+
}
13191335
else version (CRuntime_Musl)
13201336
{
13211337
int pthread_mutex_timedlock(pthread_mutex_t*, const scope timespec*);

src/core/sys/posix/sched.d

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ else version (CRuntime_Musl)
7575
struct sched_param {
7676
int sched_priority;
7777
int sched_ss_low_priority;
78-
timespec sched_ss_repl_period;
79-
timespec sched_ss_init_budget;
78+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
79+
long[4] reserved;
80+
else
81+
{
82+
timespec sched_ss_repl_period;
83+
timespec sched_ss_init_budget;
84+
}
8085
int sched_ss_max_repl;
8186
}
8287
}
@@ -306,7 +311,13 @@ else version (CRuntime_Musl)
306311
{
307312
int sched_get_priority_max(int);
308313
int sched_get_priority_min(int);
309-
int sched_rr_get_interval(pid_t, timespec*);
314+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
315+
{
316+
int __sched_rr_get_interval_time64(pid_t, timespec*);
317+
alias sched_rr_get_interval = __sched_rr_get_interval_time64;
318+
}
319+
else
320+
int sched_rr_get_interval(pid_t, timespec*);
310321
}
311322
else version (CRuntime_UClibc)
312323
{

src/core/sys/posix/signal.d

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,28 @@ struct timespec
34273427
}
34283428
*/
34293429

3430-
version (linux)
3430+
version (CRuntime_Musl)
3431+
{
3432+
// Musl on 32 bits use 64 bits time_t (time64)
3433+
// See https://git.musl-libc.org/cgit/musl/commit/?id=9b2921bea1d5017832e1b45d1fd64220047a9802
3434+
struct timespec
3435+
{
3436+
time_t tv_sec;
3437+
// 32 bits of padding on 32 bits, or in C:
3438+
// int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER==4321);
3439+
version (BigEndian)
3440+
static if (time_t.sizeof > c_long.sizeof)
3441+
int __padding;
3442+
c_long tv_nsec;
3443+
// Another 32 bits of padding on 32 bits:
3444+
// int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER!=4321);
3445+
version (LittleEndian)
3446+
static if (time_t.sizeof > c_long.sizeof)
3447+
int __padding;
3448+
};
3449+
3450+
}
3451+
else version (linux)
34313452
{
34323453
struct timespec
34333454
{
@@ -3681,6 +3702,10 @@ else version (CRuntime_Musl)
36813702
pthread_attr_t *sigev_notify_attributes;
36823703
char[56 - 3 * c_long.sizeof] __pad = void;
36833704
}
3705+
3706+
// When adding sigtimedwait signature, care for time64 redirection
3707+
// https://git.musl-libc.org/cgit/musl/tree/include/signal.h#n288
3708+
// See CRuntime_Musl_Needs_Time64_Compat_Layer
36843709
}
36853710
else version (CRuntime_UClibc)
36863711
{

src/core/sys/posix/sys/resource.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,9 @@ else version (CRuntime_UClibc)
747747
}
748748
int getrusage(int, rusage*);
749749
}
750+
else version (CRuntime_Musl)
751+
{
752+
// When adding a getrusage signature, care for time64 redirection
753+
// https://git.musl-libc.org/cgit/musl/tree/include/sys/resource.h#n109
754+
// See CRuntime_Musl_Time64_Compat_Layer
755+
}

src/core/sys/posix/sys/select.d

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,19 @@ else version (CRuntime_Musl)
502502
{
503503
fdset.fds_bits[0 .. $] = 0;
504504
}
505-
int pselect(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
506-
int select(int, fd_set*, fd_set*, fd_set*, timeval*);
505+
506+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
507+
{
508+
int __pselect_time64(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
509+
int __select_time64(int, fd_set*, fd_set*, fd_set*, timeval*);
510+
alias select = __select_time64;
511+
alias pselect = __pselect_time64;
512+
}
513+
else
514+
{
515+
int pselect(int, fd_set*, fd_set*, fd_set*, const scope timespec*, const scope sigset_t*);
516+
int select(int, fd_set*, fd_set*, fd_set*, timeval*);
517+
}
507518
}
508519
else version (CRuntime_UClibc)
509520
{
@@ -608,4 +619,3 @@ pure unittest
608619
assert(!FD_ISSET(i, &fd));
609620
}
610621
}
611-

0 commit comments

Comments
 (0)