bytecodealliance / wasmtime

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

File size do not extend. #8815

Closed Userzxcvbvnm closed 2 weeks ago

Userzxcvbvnm commented 2 weeks ago

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_00030_gxX49(int fd) {
    printf("Enter function fd_allocate_00030_gxX49\n");

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

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

int main() {
    int fd = get_fd("hardfile_1", O_WRONLY | O_APPEND);
    if (fd == -1) {
        return -1; // Return from main if get_fd failed
    }

    print_file_size(fd);
    fd_allocate_00030_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 hardfile_1 exists, 'hardfile_1' -> 'subdir_1/subdir_1/subfile_2'and the file size is 93.) wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file hardfile_1 succeed!
Get file size: 93 bytes.
Enter function fd_allocate_00030_gxX49
Space allocation in file successful.
Get file size: 406 bytes.

This is what WAMR and WasmEdge do.

Actual Results

wasmtime prints:

Get file descriptor of file hardfile_1 succeed!
Get file size: 93 bytes.
Enter function fd_allocate_00030_gxX49
Error allocating space in file.
Get file size: 93 bytes.

wasmtime fail to allocate space and the file size do not change.

Versions and Environment

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

pchickey commented 2 weeks ago

This is a duplicate of #8814.