Skip to content

Extend the file size which is no expected. #4887

Open
@Userzxcvbvnm

Description

@Userzxcvbvnm

Describe the bug

wasmer fail to print the file size of hard link file.

Steps to reproduce

(1)The 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_00023_gxX49(int fd) {
    printf("Enter function fd_allocate_00023_gxX49\n");
    
    off_t offset = 0;
    off_t len = 1024; // 1KB allocation
    int result = posix_fallocate(fd, 0, 243);

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

int main() {
    int fd = get_fd("softfile_2", O_RDONLY);
    if (fd == -1) {
        return -1; 
    }

    print_file_size(fd);
    fd_allocate_00023_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}



(2)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

(3)Running wasm:
(Before run the Wasm file,softfile_2 exists and is a soft link file which points to file subdir_1/subdir_1/subdir_3/subdir_4/subfile_2, subdir_1/subdir_1/subdir_3/subdir_4/subfile_2 exists and file size is 69.)
wasmer run --dir=. test.wasm

Expected behavior

print:

Get file descriptor of file softfile_2 succeed!
Get file size: 69 bytes.
Enter function fd_allocate_00023_gxX49
Error allocating space in file.
Get file size: 69 bytes.

This is what wasmtime, WAMR and WasmEdge do.
The file is opened wth O_RDONLY, rather than O_WRONLY or O_RDWR, thus the file size is not expected to be extended.

Actual behavior

wasmer print:

Get file descriptor of file softfile_2 succeed!
Get file size: 69 bytes.
Enter function fd_allocate_00023_gxX49
Space allocation in file successful.
Get file size: 243 bytes.

wasmer extend the flie subdir_1/subdir_1/subdir_3/subdir_4/subfile_2 with NULL characters.

Additional context

Ubuntu 20.04
x86_64
wasmer-4.2.2

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpriority-highHigh priority issue📦 lib-vfsAbout wasmer-vfs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions