Skip to content

open with O_RDONLY but File size adjusted successfully #4895

Open
@Userzxcvbvnm

Description

@Userzxcvbvnm

Describe the bug

Read bytes bug.

Steps to reproduce

(1)The test case is :


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

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 %d by descriptor failed!\n", fd);
    }
}

void fd_filestat_set_size_00001_L4Yu4(int fd) {
    printf("Enter function fd_filestat_set_size_00001_L4Yu4\n");
    
    off_t size = lseek(fd, 0, SEEK_END); // Get current file size
    
    printf("Current file size: %ld\n", size);
    
    if (ftruncate(fd, 0) == 0) { // Adjust file size by adding 100 bytes
        printf("File size adjusted successfully\n");
    } else {
        printf("Failed to adjust file size\n");
    }
    
    size = lseek(fd, 0, SEEK_END); // Get new file size after adjustment
    
    printf("New file size: %ld\n", size);
}

int main() {
    int fd = get_fd("subdir_1/subdir_1/subdir_2/subdir_4/subfile_1", O_RDONLY); 
    if (fd == -1) {
        return 1; // Return from main if get_fd fails
    }
    
    fd_filestat_set_size_00001_L4Yu4(fd);
    
    closebyfd(fd); // Close the file
    
    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, subdir_1/subdir_1/subdir_2/subdir_4/subfile_1 exists and the size is 98.)
wasmer run --dir=. test.wasm

Expected behavior

print:

Get file descriptor of file subdir_1/subdir_1/subdir_2/subdir_4/subfile_1 succeed!
Enter function fd_filestat_set_size_00001_L4Yu4
Current file size: 6
Failed to adjust file size
New file size: 6

This is what wasmtime, WAMR and WasmEdge do.

Actual behavior

print:

Get file descriptor of file subdir_1/subdir_1/subdir_2/subdir_4/subfile_1 succeed!
Enter function fd_filestat_set_size_00001_L4Yu4
Current file size: 6
File size adjusted successfully
New file size: 0

This file is opened with O_RDONLY, without O_WRONLY or O_RDWR, but wasmer prints File size adjusted successfully, which is not expected.

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