bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
15.44k stars 1.31k forks source link

clock time difference? #8623

Closed Userzxcvbvnm closed 6 months ago

Userzxcvbvnm commented 6 months ago

Test Case

The c program is as follows:


#include <stdio.h>
#include <time.h>

void clock_time_get_00002_obtwo() {
    printf("Enter function clock_time_get_00002_obtwo\n");

    struct timespec ts;
    if(clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
        perror("clock_gettime error");
        return;
    }

    printf("Current time: %ld seconds, %ld nanoseconds\n", ts.tv_sec, ts.tv_nsec);
}

int main() {
    printf("Enter function main\n");

    clock_time_get_00002_obtwo();

    return 0;
}

Steps to Reproduce

(1)compiler it into Wasm binaries by ./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

Expected Results

Print:

Enter function main
Enter function clock_time_get_00002_obtwo
Current time: 2177580 seconds, 0 nanoseconds

And this is what other Wasm runtimes print, including wasmer, wamr and wasmedge.

Actual Results

Print:

Enter function main
Enter function clock_time_get_00002_obtwo
Current time: 0 seconds, 0 nanoseconds

Versions and Environment

Wasmtime version or commit: 19.0.2

Operating system: Ubuntu 20.04

Architecture: amd64

alexcrichton commented 6 months ago

Using wasi-sdk it generates a warning:

foo.c:14:60: warning: format specifies type 'long' but the argument has type 'time_t' (aka 'long long') [-Wformat]
   14 |     printf("Current time: %ld seconds, %ld nanoseconds\n", ts.tv_sec, ts.tv_nsec);
      |                           ~~~                              ^~~~~~~~~
      |                           %lld

which when fixed causes the program to print:

Enter function main
Enter function clock_time_get_00002_obtwo
Current time: 0 seconds, 571458 nanoseconds

which is the expected behavior of Wasmtime. The monotonic clock is relative to an arbitrary point and Wasmtime chooses instance creation as the relative point.