Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libc-bottom-half/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ if (WASI STREQUAL "p3")
)
endif()

# Don't export symbols of generated code in shared libraries, so specifically
# pass `-fvisibility=hidden`.
set_source_files_properties(sources/wasip2.c PROPERTIES
COMPILE_OPTIONS -fvisibility=hidden)
set_source_files_properties(sources/wasip3.c PROPERTIES
COMPILE_OPTIONS -fvisibility=hidden)

add_object_library(bottom-half ${bottom_half_sources})
foreach(obj bottom-half-shared bottom-half-static)
target_link_libraries(${obj} PUBLIC musl-top-half-interface)
Expand Down
39 changes: 5 additions & 34 deletions libc-bottom-half/clocks/clock.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#define _WASI_EMULATED_PROCESS_CLOCKS
#include <common/time.h>
#include <time.h>
#include <wasi/api.h>

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

#if defined(__wasip1__)

// Snapshot of the monotonic clock at the start of the program.
static __wasi_timestamp_t start;
static struct timespec start;

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

// Define the libc symbol as `__clock` so that we can reliably call it
Expand All @@ -24,36 +21,10 @@ clock_t __clock(void) {
// an inherent concept of a process. Note that this means we'll incorrectly
// include time from other processes, so this function is only declared by
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
__wasi_timestamp_t now = 0;
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
return now - start;
}

#elif defined(__wasip2__) || defined(__wasip3__)

// Snapshot of the monotonic clock at the start of the program.
static monotonic_clock_instant_t start;

// Use a priority of 10 to run fairly early in the implementation-reserved
// constructor priority range.
__attribute__((constructor(10))) static void init(void) {
start = monotonic_clock_now();
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (now.tv_sec - start.tv_sec) * 1E9 - start.tv_nsec + now.tv_nsec;
}

// Define the libc symbol as `__clock` so that we can reliably call it
// from elsewhere in libc.
clock_t __clock(void) {
// Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
// an inherent concept of a process. Note that this means we'll incorrectly
// include time from other processes, so this function is only declared by
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
monotonic_clock_instant_t now = monotonic_clock_now();
return now - start;
}

#else
#error "Unknown WASI version"
#endif

// Define a user-visible alias as a weak symbol.
__attribute__((__weak__, __alias__("__clock"))) clock_t clock(void);
19 changes: 3 additions & 16 deletions libc-bottom-half/clocks/times.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <common/time.h>
#include <sys/times.h>
#include <time.h>
#include <wasi/api.h>

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

clock_t times(struct tms *buffer) {
#if defined(__wasip1__)
__wasi_timestamp_t user = __clock();
*buffer = (struct tms){
.tms_utime = user,
// WASI doesn't provide a way to spawn a new process, so always 0.
.tms_cutime = 0};

__wasi_timestamp_t realtime = 0;
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
#elif defined(__wasip2__) || defined(__wasip3__)
clock_t user = __clock();
*buffer = (struct tms){
.tms_utime = user,
// WASI doesn't provide a way to spawn a new process, so always 0.
.tms_cutime = 0};

monotonic_clock_instant_t realtime = monotonic_clock_now();
#else
#error "Unsupported WASI version"
#endif
return realtime;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1E9 + now.tv_nsec;
}
4 changes: 3 additions & 1 deletion libc-bottom-half/headers/public/__macro_PAGESIZE.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* consideration. POSIX has deprecated `getpagesize` in favor of
* `sysconf(_SC_PAGESIZE)` which does not have this problem.
*/
#if __clang_major__ >= 22
// FIXME(#778): `__wasm_first_page_end` is broken for PIC as of this writing and
// will likely require `wasm-ld` changes, at which point we can re-enable it.
#if __clang_major__ >= 22 && !defined __pic__
extern char __wasm_first_page_end;
#define PAGESIZE ((unsigned long)&__wasm_first_page_end)
#else
Expand Down
Loading