Skip to content

Commit 0db888c

Browse files
alexcrichtondicej
andauthored
Build and run all tests as shared libraries (#780)
This PR is derived from #779 to build and run dynamically-linked tests in wasi-libc to help exercise shared-library-related support. This requires the fix from #779, for example, to get tests passing. This takes a different approach to testing, however, to build and run all tests as dynamically linked instead of only some particular tests. This avoids the need for a one-off test and additionally helps reuse all existing infrastructure for tests. This expands the test coverage more and documents a few cases where dynamic libraries don't work (long-float intrinsics and setjmp) --------- Co-authored-by: Joel Dice <joel.dice@akamai.com>
1 parent 38c09e3 commit 0db888c

6 files changed

Lines changed: 126 additions & 109 deletions

File tree

libc-bottom-half/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ if (WASI STREQUAL "p3")
179179
)
180180
endif()
181181

182+
# Don't export symbols of generated code in shared libraries, so specifically
183+
# pass `-fvisibility=hidden`.
184+
set_source_files_properties(sources/wasip2.c PROPERTIES
185+
COMPILE_OPTIONS -fvisibility=hidden)
186+
set_source_files_properties(sources/wasip3.c PROPERTIES
187+
COMPILE_OPTIONS -fvisibility=hidden)
188+
182189
add_object_library(bottom-half ${bottom_half_sources})
183190
foreach(obj bottom-half-shared bottom-half-static)
184191
target_link_libraries(${obj} PUBLIC musl-top-half-interface)

libc-bottom-half/clocks/clock.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
#define _WASI_EMULATED_PROCESS_CLOCKS
22
#include <common/time.h>
33
#include <time.h>
4-
#include <wasi/api.h>
54

65
_Static_assert(CLOCKS_PER_SEC == NSEC_PER_SEC,
76
"This implementation assumes that `clock` is in nanoseconds");
87

9-
#if defined(__wasip1__)
10-
118
// Snapshot of the monotonic clock at the start of the program.
12-
static __wasi_timestamp_t start;
9+
static struct timespec start;
1310

1411
// Use a priority of 10 to run fairly early in the implementation-reserved
1512
// constructor priority range.
1613
__attribute__((constructor(10))) static void init(void) {
17-
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &start);
14+
clock_gettime(CLOCK_MONOTONIC, &start);
1815
}
1916

2017
// Define the libc symbol as `__clock` so that we can reliably call it
@@ -24,36 +21,10 @@ clock_t __clock(void) {
2421
// an inherent concept of a process. Note that this means we'll incorrectly
2522
// include time from other processes, so this function is only declared by
2623
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
27-
__wasi_timestamp_t now = 0;
28-
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
29-
return now - start;
30-
}
31-
32-
#elif defined(__wasip2__) || defined(__wasip3__)
33-
34-
// Snapshot of the monotonic clock at the start of the program.
35-
static monotonic_clock_instant_t start;
36-
37-
// Use a priority of 10 to run fairly early in the implementation-reserved
38-
// constructor priority range.
39-
__attribute__((constructor(10))) static void init(void) {
40-
start = monotonic_clock_now();
24+
struct timespec now;
25+
clock_gettime(CLOCK_MONOTONIC, &now);
26+
return (now.tv_sec - start.tv_sec) * 1E9 - start.tv_nsec + now.tv_nsec;
4127
}
4228

43-
// Define the libc symbol as `__clock` so that we can reliably call it
44-
// from elsewhere in libc.
45-
clock_t __clock(void) {
46-
// Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
47-
// an inherent concept of a process. Note that this means we'll incorrectly
48-
// include time from other processes, so this function is only declared by
49-
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
50-
monotonic_clock_instant_t now = monotonic_clock_now();
51-
return now - start;
52-
}
53-
54-
#else
55-
#error "Unknown WASI version"
56-
#endif
57-
5829
// Define a user-visible alias as a weak symbol.
5930
__attribute__((__weak__, __alias__("__clock"))) clock_t clock(void);

libc-bottom-half/clocks/times.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <common/time.h>
33
#include <sys/times.h>
44
#include <time.h>
5-
#include <wasi/api.h>
65

76
_Static_assert(CLOCKS_PER_SEC == NSEC_PER_SEC,
87
"This implementation assumes that `clock` is in nanoseconds");
@@ -12,25 +11,13 @@ _Static_assert(CLOCKS_PER_SEC == NSEC_PER_SEC,
1211
clock_t __clock(void);
1312

1413
clock_t times(struct tms *buffer) {
15-
#if defined(__wasip1__)
16-
__wasi_timestamp_t user = __clock();
17-
*buffer = (struct tms){
18-
.tms_utime = user,
19-
// WASI doesn't provide a way to spawn a new process, so always 0.
20-
.tms_cutime = 0};
21-
22-
__wasi_timestamp_t realtime = 0;
23-
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
24-
#elif defined(__wasip2__) || defined(__wasip3__)
2514
clock_t user = __clock();
2615
*buffer = (struct tms){
2716
.tms_utime = user,
2817
// WASI doesn't provide a way to spawn a new process, so always 0.
2918
.tms_cutime = 0};
3019

31-
monotonic_clock_instant_t realtime = monotonic_clock_now();
32-
#else
33-
#error "Unsupported WASI version"
34-
#endif
35-
return realtime;
20+
struct timespec now;
21+
clock_gettime(CLOCK_MONOTONIC, &now);
22+
return now.tv_sec * 1E9 + now.tv_nsec;
3623
}

libc-bottom-half/headers/public/__macro_PAGESIZE.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* consideration. POSIX has deprecated `getpagesize` in favor of
1515
* `sysconf(_SC_PAGESIZE)` which does not have this problem.
1616
*/
17-
#if __clang_major__ >= 22
17+
// FIXME(#778): `__wasm_first_page_end` is broken for PIC as of this writing and
18+
// will likely require `wasm-ld` changes, at which point we can re-enable it.
19+
#if __clang_major__ >= 22 && !defined __pic__
1820
extern char __wasm_first_page_end;
1921
#define PAGESIZE ((unsigned long)&__wasm_first_page_end)
2022
#else

0 commit comments

Comments
 (0)