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

Commit 305adf8

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 78fe28d commit 305adf8

File tree

16 files changed

+344
-54
lines changed

16 files changed

+344
-54
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: 10 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,14 @@ 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+
299+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
300+
{
301+
void* __dlsym_time64(void*, const scope char*);
302+
alias dlsym = __dlsym_time64;
303+
}
304+
else
305+
void* dlsym(void*, const scope char*);
297306

298307
int dladdr(scope const void *addr, Dl_info *info);
299308
struct Dl_info

src/core/sys/posix/mqueue.d

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

185193

186194
/**
@@ -217,5 +225,12 @@ int mq_send (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len, uint msg_prio);
217225
* with errno set to indicate the error.
218226
*
219227
*/
220-
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
221-
uint msg_prio, const(timespec)* abs_timeout);
228+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
229+
{
230+
ssize_t __mq_timedreceive_time64 (mqd_t mqdes, char* msg_ptr, size_t msg_len,
231+
uint* msg_prio, const(timespec)* abs_timeout);
232+
alias mq_timedreceive = __mq_timedreceive_time64;
233+
}
234+
else
235+
int mq_timedsend (mqd_t mqdes, const(char)* msg_ptr, size_t msg_len,
236+
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
@@ -718,7 +718,13 @@ int pthread_cond_broadcast(pthread_cond_t*);
718718
int pthread_cond_destroy(pthread_cond_t*);
719719
int pthread_cond_init(const scope pthread_cond_t*, pthread_condattr_t*) @trusted;
720720
int pthread_cond_signal(pthread_cond_t*);
721-
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
721+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
722+
{
723+
int __pthread_cond_timedwait_time64(pthread_cond_t*, pthread_mutex_t*, in timespec*);
724+
alias pthread_cond_timedwait = __pthread_cond_timedwait_time64;
725+
}
726+
else
727+
int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
722728
int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*);
723729
int pthread_condattr_destroy(pthread_condattr_t*);
724730
int pthread_condattr_init(pthread_condattr_t*);
@@ -1315,6 +1321,16 @@ else version (CRuntime_Bionic)
13151321
int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const scope timespec*);
13161322
int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const scope timespec*);
13171323
}
1324+
else static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
1325+
{
1326+
int __pthread_mutex_timedlock_time64(pthread_mutex_t*, const scope timespec*);
1327+
int __pthread_rwlock_timedrdlock_time64(pthread_rwlock_t*, const scope timespec*);
1328+
int __pthread_rwlock_timedwrlock_time64(pthread_rwlock_t*, const scope timespec*);
1329+
1330+
alias pthread_mutex_timedlock = __pthread_mutex_timedlock_time64;
1331+
alias pthread_rwlock_timedrdlock = __pthread_rwlock_timedrdlock_time64;
1332+
alias pthread_rwlock_timedwrlock = __pthread_rwlock_timedwrlock_time64;
1333+
}
13181334
else version (CRuntime_Musl)
13191335
{
13201336
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: 31 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,15 @@ else version (CRuntime_Musl)
36813702
pthread_attr_t *sigev_notify_attributes;
36823703
char[56 - 3 * c_long.sizeof] __pad = void;
36833704
}
3705+
3706+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
3707+
{
3708+
int __sigtimedwait_time64(const scope sigset_t*, siginfo_t*, const scope timespec*);
3709+
alias sigtimedwait = __sigtimedwait_time64;
3710+
}
3711+
else
3712+
int sigtimedwait(const scope sigset_t*, siginfo_t*, const scope timespec*);
3713+
36843714
}
36853715
else version (CRuntime_UClibc)
36863716
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,13 @@ else version (CRuntime_UClibc)
747747
}
748748
int getrusage(int, rusage*);
749749
}
750+
else version (CRuntime_Musl)
751+
{
752+
static if (CRuntime_Musl_Needs_Time64_Compat_Layer)
753+
{
754+
int __getrusage_time64(int, rusage*);
755+
alias getrusage = __getrusage_time64;
756+
}
757+
else
758+
int getrusage(int, rusage*);
759+
}

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)