Skip to content

Commit c0b6ba6

Browse files
committed
Restore time_t to 64bit
This brings us back in line with upstream musl. The change to 32-bit was only recently made in #16966. The reason we made this change was made was because we had certain C library calls that were implemented in JS that returned `time_t`. Since returning 64-bit values from JS functions is not always easy (we don't always have WASM_BIGINT available) that simplest solution was to define `time_t` to 32-bit which doesn't have issues at the JS boundary. However, in the intervening time many of the `time_t`-returning function have been moved into native code (See #16606 and #16439) with only two remaining: _mktime_js and _timegm_js. So this change redefines just those two functions to return `int` while keeping `time_t` itself as 64-bit. Fixes: #17393
1 parent 338aaf9 commit c0b6ba6

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

ChangeLog.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ See docs/process.md for more on how version tagging works.
3939
- The getWasmTableEntry/setWasmTableEntry library function are no longer
4040
included by default. Add them to `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE` or
4141
`EXPORTED_RUNTIME_METHODS` if you want to use them outside of JS library code.
42+
- The type of `time_t` was restored 64-bit after being converted to 32-bit in
43+
3.1.11. (#17401)
4244

4345
3.1.15 - 07/01/2022
4446
-------------------

system/lib/libc/emscripten_time.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ __attribute__((__weak__)) int daylight = 0;
1919
__attribute__((__weak__)) char *tzname[2] = { 0, 0 };
2020

2121
void _tzset_js(long* timezone, int* daylight, char** tzname);
22-
time_t _timegm_js(struct tm *tm);
23-
time_t _mktime_js(struct tm *tm);
22+
// Declare these functions `int` rather than time_t to avoid int64 at the wasm
23+
// bounday (avoids 64-bit complexity at the boundary when WASM_BIGINT is
24+
// missing).
25+
// TODO(sbc): Covert back to `time_t` before 2038 ...
26+
int _timegm_js(struct tm *tm);
27+
int _mktime_js(struct tm *tm);
2428
void _localtime_js(const time_t *restrict t, struct tm *restrict tm);
2529
void _gmtime_js(const time_t *restrict t, struct tm *restrict tm);
2630
double _emscripten_date_now();

system/lib/libc/musl/arch/emscripten/bits/alltypes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ typedef long double double_t;
7878
#endif
7979

8080
#if defined(__NEED_time_t) && !defined(__DEFINED_time_t)
81-
typedef int time_t; /* XXX EMSCRIPTEN: ensure it's always 32-bits even in wasm64 */
81+
typedef _Int64 time_t;
8282
#define __DEFINED_time_t
8383
#endif
8484

system/lib/libc/musl/include/inttypes.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ uintmax_t strtoumax(const char *__restrict, char **__restrict, int);
2222
intmax_t wcstoimax(const wchar_t *__restrict, wchar_t **__restrict, int);
2323
uintmax_t wcstoumax(const wchar_t *__restrict, wchar_t **__restrict, int);
2424

25-
#if UINTPTR_MAX == UINT64_MAX
26-
#define __PRI64 "l"
27-
#define __PRIPTR "l"
28-
#elif defined(__EMSCRIPTEN__)
25+
#if defined(__EMSCRIPTEN__)
2926
// Under emscripten __PTRDIFF_TYPE__ and therefor intptr_t are defined to
3027
// be `long int` even on wasm32.
3128
#define __PRI64 "ll"
3229
#define __PRIPTR "l"
30+
#elif UINTPTR_MAX == UINT64_MAX
31+
#define __PRI64 "l"
32+
#define __PRIPTR "l"
3333
#else
3434
#define __PRI64 "ll"
3535
#define __PRIPTR ""

tests/reference_struct_info.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1410,30 +1410,30 @@
14101410
"sin6_scope_id": 24
14111411
},
14121412
"stat": {
1413-
"__size__": 88,
1413+
"__size__": 112,
14141414
"__st_dev_padding": 4,
14151415
"__st_ino_truncated": 8,
14161416
"__st_rdev_padding": 32,
14171417
"st_atim": {
1418-
"__size__": 8,
1419-
"tv_nsec": 60,
1418+
"__size__": 16,
1419+
"tv_nsec": 64,
14201420
"tv_sec": 56
14211421
},
14221422
"st_blksize": 48,
14231423
"st_blocks": 52,
14241424
"st_ctim": {
1425-
"__size__": 8,
1426-
"tv_nsec": 76,
1427-
"tv_sec": 72
1425+
"__size__": 16,
1426+
"tv_nsec": 96,
1427+
"tv_sec": 88
14281428
},
14291429
"st_dev": 0,
14301430
"st_gid": 24,
1431-
"st_ino": 80,
1431+
"st_ino": 104,
14321432
"st_mode": 12,
14331433
"st_mtim": {
1434-
"__size__": 8,
1435-
"tv_nsec": 68,
1436-
"tv_sec": 64
1434+
"__size__": 16,
1435+
"tv_nsec": 80,
1436+
"tv_sec": 72
14371437
},
14381438
"st_nlink": 16,
14391439
"st_rdev": 28,
@@ -1461,8 +1461,8 @@
14611461
"timeSpentInStatus": 16
14621462
},
14631463
"timespec": {
1464-
"__size__": 8,
1465-
"tv_nsec": 4,
1464+
"__size__": 16,
1465+
"tv_nsec": 8,
14661466
"tv_sec": 0
14671467
},
14681468
"tm": {

tests/utime/test_utime.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <assert.h>
99
#include <errno.h>
10+
#include <inttypes.h>
1011
#include <signal.h>
1112
#include <stdio.h>
1213
#include <stdlib.h>
@@ -51,9 +52,9 @@ void test() {
5152
stat("writeable", &s);
5253
assert(s.st_atime == s.st_mtime);
5354
time_t diff = s.st_atime - now;
54-
if (abs(diff) > 5) {
55-
fprintf(stderr, "st_atime: %i, now: %i, diff: %i\n ", s.st_atime, now, diff);
56-
assert(abs(diff) <= 5);
55+
if (llabs(diff) > 5) {
56+
fprintf(stderr, "st_atime: %" PRId64 ", now: %" PRId64 ", diff: %" PRId64 "\n ", s.st_atime, now, diff);
57+
assert(llabs(diff) <= 5);
5758
}
5859

5960
// write permissions aren't checked when setting node

0 commit comments

Comments
 (0)