Skip to content

Commit cd78df6

Browse files
committed
Restore time_t and other core types 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 cd78df6

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-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

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ 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

8585
#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t)
86-
typedef int suseconds_t; /* XXX EMSCRIPTEN: ensure it's always 32-bits even in wasm64 */
86+
typedef _Int64 suseconds_t;
8787
#define __DEFINED_suseconds_t
8888
#endif
8989

@@ -248,27 +248,27 @@ typedef unsigned _Int64 ino_t;
248248
#endif
249249

250250
#if defined(__NEED_dev_t) && !defined(__DEFINED_dev_t)
251-
typedef unsigned int dev_t;
251+
typedef unsigned _Int64 dev_t;
252252
#define __DEFINED_dev_t
253253
#endif
254254

255255
#if defined(__NEED_blksize_t) && !defined(__DEFINED_blksize_t)
256-
typedef int blksize_t; /* XXX EMSCRIPTEN: ensure it's always 32-bits even in wasm64 */
257-
#define __DEFINED_blksize_t
256+
typedef long blksize_t;
257+
#define __DEF_Int64
258258
#endif
259259

260260
#if defined(__NEED_blkcnt_t) && !defined(__DEFINED_blkcnt_t)
261-
typedef int blkcnt_t;
261+
typedef _Int64 blkcnt_t;
262262
#define __DEFINED_blkcnt_t
263263
#endif
264264

265265
#if defined(__NEED_fsblkcnt_t) && !defined(__DEFINED_fsblkcnt_t)
266-
typedef unsigned int fsblkcnt_t;
266+
typedef unsigned _Int64 fsblkcnt_t;
267267
#define __DEFINED_fsblkcnt_t
268268
#endif
269269

270270
#if defined(__NEED_fsfilcnt_t) && !defined(__DEFINED_fsfilcnt_t)
271-
typedef unsigned int fsfilcnt_t;
271+
typedef unsigned _Int64 fsfilcnt_t;
272272
#define __DEFINED_fsfilcnt_t
273273
#endif
274274

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/core/test_statvfs.c

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

88
#include <stdio.h>
99
#include <errno.h>
10+
#include <inttypes.h>
1011
#include <sys/statvfs.h>
1112

1213
int main() {
@@ -17,12 +18,12 @@ int main() {
1718

1819
printf("f_bsize: %lu\n", s.f_bsize);
1920
printf("f_frsize: %lu\n", s.f_frsize);
20-
printf("f_blocks: %u\n", s.f_blocks);
21-
printf("f_bfree: %u\n", s.f_bfree);
22-
printf("f_bavail: %u\n", s.f_bavail);
21+
printf("f_blocks: %" PRId64 "\n", s.f_blocks);
22+
printf("f_bfree: %" PRId64 "\n", s.f_bfree);
23+
printf("f_bavail: %" PRId64 "\n", s.f_bavail);
2324
printf("f_files: %d\n", s.f_files > 5);
24-
printf("f_ffree: %u\n", s.f_ffree);
25-
printf("f_favail: %u\n", s.f_favail);
25+
printf("f_ffree: %" PRId64 "\n", s.f_ffree);
26+
printf("f_favail: %" PRId64 "\n", s.f_favail);
2627
printf("f_fsid: %lu\n", s.f_fsid);
2728
printf("f_flag: %lu\n", s.f_flag);
2829
printf("f_namemax: %lu\n", s.f_namemax);

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)