Stream: git-wasmtime

Topic: wasmtime / issue #8814 File allocation bug?


view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 09:55):

Userzxcvbvnm added the bug label to Issue #8814.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 09:55):

Userzxcvbvnm opened issue #8814:

Test Case

The c test case is:

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

void print_file_size(int fd){
    struct stat st;
    if (fstat(fd, &st) == -1) {
        printf("Get file size failed.\n");
    } else {
        printf("Get file size: %ld bytes.\n", st.st_size);
    }
}


int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);

    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}

void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file by descriptor failed!\n");
    }
}

void fd_allocate_00017_gxX49(int fd) {
    printf("Enter function fd_allocate_00017_gxX49\n");

    off_t offset = 0;
    off_t len = 1024; // 1KB allocation
    int result = posix_fallocate(fd, 0, 4);

    if (result == 0) {
        printf("Space allocation in file successful.\n");
    } else {
        printf("Error allocating space in file.\n");
    }
}

int main() {
    int fd = get_fd("subdir_2/subdir_1/subdir_5/subfile_1", O_WRONLY | O_TRUNC);
    if (fd == -1) {
        return -1; // Return from main if get_fd failed
    }

    print_file_size(fd);
    fd_allocate_00017_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}

Steps to Reproduce

(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm:
(Before run the Wasm file, file subdir_2/subdir_1/subdir_5/subfile_1 exists, and file size is 62.)
wasmtime run --dir=. read.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Space allocation in file successful.
Get file size: 4 bytes.

This is what WAMR and WasmEdge do.
First the file is trunced due to the "O_TRUNC" openstyle, and the file size is 0 as printed. Then, 4 bytes are allocated, and the files size change into 4 bytes.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Error allocating space in file.
Get file size: 0 bytes.

wasmtime fail to allocate space.

Versions and Environment

Wasmtime version or commit: 19.0.2
Operating system: Ubuntu 20.04
Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 09:59):

Userzxcvbvnm edited issue #8814:

Test Case

The c test case is:

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

void print_file_size(int fd){
    struct stat st;
    if (fstat(fd, &st) == -1) {
        printf("Get file size failed.\n");
    } else {
        printf("Get file size: %ld bytes.\n", st.st_size);
    }
}


int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);

    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}

void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file by descriptor failed!\n");
    }
}

void fd_allocate_00017_gxX49(int fd) {
    printf("Enter function fd_allocate_00017_gxX49\n");

    int result = posix_fallocate(fd, 0, 4);

    if (result == 0) {
        printf("Space allocation in file successful.\n");
    } else {
        printf("Error allocating space in file.\n");
    }
}

int main() {
    int fd = get_fd("subdir_2/subdir_1/subdir_5/subfile_1", O_WRONLY | O_TRUNC);
    if (fd == -1) {
        return -1; // Return from main if get_fd failed
    }

    print_file_size(fd);
    fd_allocate_00017_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}

Steps to Reproduce

(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm:
(Before run the Wasm file, file subdir_2/subdir_1/subdir_5/subfile_1 exists, and file size is 62.)
wasmtime run --dir=. read.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Space allocation in file successful.
Get file size: 4 bytes.

This is what WAMR and WasmEdge do.
First the file is trunced due to the "O_TRUNC" openstyle, and the file size is 0 as printed. Then, 4 bytes are allocated, and the files size change into 4 bytes.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Error allocating space in file.
Get file size: 0 bytes.

wasmtime fail to allocate space.

Versions and Environment

Wasmtime version or commit: 19.0.2
Operating system: Ubuntu 20.04
Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 10:20):

Userzxcvbvnm edited issue #8814:

Test Case

The c test case is:

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

void print_file_size(int fd){
    struct stat st;
    if (fstat(fd, &st) == -1) {
        printf("Get file size failed.\n");
    } else {
        printf("Get file size: %ld bytes.\n", st.st_size);
    }
}


int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);

    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}

void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file by descriptor failed!\n");
    }
}

void fd_allocate_00017_gxX49(int fd) {
    printf("Enter function fd_allocate_00017_gxX49\n");

    int result = posix_fallocate(fd, 0, 4);

    if (result == 0) {
        printf("Space allocation in file successful.\n");
    } else {
        printf("Error allocating space in file.\n");
    }
}

int main() {
    int fd = get_fd("subdir_2/subdir_1/subdir_5/subfile_1", O_WRONLY | O_TRUNC);
    if (fd == -1) {
        return -1; // Return from main if get_fd failed
    }

    print_file_size(fd);
    fd_allocate_00017_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}

Steps to Reproduce

(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm:
(Before run the Wasm file, file subdir_2/subdir_1/subdir_5/subfile_1 exists, and file size is 62.)
wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Space allocation in file successful.
Get file size: 4 bytes.

This is what WAMR and WasmEdge do.
First the file is trunced due to the "O_TRUNC" openstyle, and the file size is 0 as printed. Then, 4 bytes are allocated, and the files size change into 4 bytes.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Error allocating space in file.
Get file size: 0 bytes.

wasmtime fail to allocate space.

Versions and Environment

Wasmtime version or commit: 19.0.2
Operating system: Ubuntu 20.04
Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 17:47):

pchickey closed issue #8814:

Test Case

The c test case is:

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

void print_file_size(int fd){
    struct stat st;
    if (fstat(fd, &st) == -1) {
        printf("Get file size failed.\n");
    } else {
        printf("Get file size: %ld bytes.\n", st.st_size);
    }
}


int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);

    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}

void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file by descriptor failed!\n");
    }
}

void fd_allocate_00017_gxX49(int fd) {
    printf("Enter function fd_allocate_00017_gxX49\n");

    int result = posix_fallocate(fd, 0, 4);

    if (result == 0) {
        printf("Space allocation in file successful.\n");
    } else {
        printf("Error allocating space in file.\n");
    }
}

int main() {
    int fd = get_fd("subdir_2/subdir_1/subdir_5/subfile_1", O_WRONLY | O_TRUNC);
    if (fd == -1) {
        return -1; // Return from main if get_fd failed
    }

    print_file_size(fd);
    fd_allocate_00017_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}

Steps to Reproduce

(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm:
(Before run the Wasm file, file subdir_2/subdir_1/subdir_5/subfile_1 exists, and file size is 62.)
wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Space allocation in file successful.
Get file size: 4 bytes.

This is what WAMR and WasmEdge do.
First the file is trunced due to the "O_TRUNC" openstyle, and the file size is 0 as printed. Then, 4 bytes are allocated, and the files size change into 4 bytes.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_5/subfile_1 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00017_gxX49
Error allocating space in file.
Get file size: 0 bytes.

wasmtime fail to allocate space.

Versions and Environment

Wasmtime version or commit: 19.0.2
Operating system: Ubuntu 20.04
Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Jun 15 2024 at 17:47):

pchickey commented on issue #8814:

Wasmtime has chosen not to support (return ERRNO_NOTSUP) the fd_allocate import function in Wasi 0.1, because it is not possible to implement that function on all of the common OSs. That function was removed from the Wasi 0.2 spec accordingly. See https://github.com/bytecodealliance/wasmtime/pull/6217 and numerous other issues which refer to this.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 16 2024 at 01:44):

Userzxcvbvnm commented on issue #8814:

Thanks for your reply !


Last updated: Nov 22 2024 at 17:03 UTC