Skip to content

utimensat() with timespec=NULL sets wrong time #4184

@tiran

Description

@tiran

WASI libc or wasmtime set wrong atime and mtime when utimensat is used with NULL timespec. The call is suppose to set the atime and mtime of the file to NOW, but it sets both to 0. I can't say if the problem is in wasi-libc __wasilibc_nocwd_utimensat, utimens_get_timestamps, or in wasmtime path_filestat_set_times implementation.

Test Case

Reproducer:

#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define TESTFN "utime.test"

int pstat(const char *name) {
    struct stat st;
    if (stat(TESTFN, &st) < 0) {
        perror(TESTFN);
        exit(1);
    }
    printf(
        "atime: %lld.%09ld, mtime: %lld.%09ld\n",
        st.st_atim.tv_sec, st.st_atim.tv_nsec,
        st.st_mtim.tv_sec, st.st_mtim.tv_nsec
    );
    return 0;
}

int main(void) {
    int fd;
    
    fd = open(TESTFN, O_CREAT | O_WRONLY);
    write(fd, "test\n", 5);
    close(fd);

    pstat(TESTFN);
    utimensat(AT_FDCWD, TESTFN, NULL, 0);
    pstat(TESTFN);

    return 0;
}

Steps to Reproduce

Compile and run the reproducer with WASI-SDK's clang and wasmtime

$ gcc -o utime utime.c && ./utime
$ /opt/wasi-sdk/bin/clang -o utime.wasm utime.c && wasmtime run --dir . -- utime.wasm

Expected Results

utimensat with timespec NULL should set the mtime and atime of the file to the current time.

$ gcc -o utime utime.c && ./utime
atime: 1653412556.499033757, mtime: 1653412556.499033757
atime: 1653412558.795059730, mtime: 1653412558.795059730

Actual Results

wasmtime sets atime and mtime to 0.

$ /opt/wasi-sdk/bin/clang -o utime.wasm utime.c && wasmtime run --dir . -- utime.wasm 
atime: 0.000000000, mtime: 1653412625.340814414
atime: 0.000000000, mtime: 0.000000000

Versions and Environment

WASI SDK: 15.0

Wasmtime version or commit: 0.36.0

Operating system: Linux (Ubuntu 20.04 in podman on Fedora 36)

Architecture: x86_64

Extra Info

Anything else you'd like to add?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior in the current implementation that needs fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions