Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write success, but not expected. #8818

Closed
Userzxcvbvnm opened this issue Jun 17, 2024 · 1 comment · Fixed by #8822
Closed

write success, but not expected. #8818

Userzxcvbvnm opened this issue Jun 17, 2024 · 1 comment · Fixed by #8822
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@Userzxcvbvnm
Copy link

Test Case

The c test case is:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/uio.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_pwrite_00067_nEEKq(int fd) {
    printf("Enter function fd_pwrite_00067_nEEKq\n");
    off_t size = lseek(fd, 0, SEEK_END); 
    printf("Current file size before: %ld\n", size);

    
    struct iovec iov[1];
    iov[0].iov_base = "";
    iov[0].iov_len = 0;
    
    off_t offset = lseek(fd, 0, SEEK_CUR);
    if (offset == -1) {
        printf("Failed to get current offset\n");
        return;
    }
    
    ssize_t bytes_written = pwritev(fd, iov, 1, 9);
    if (bytes_written == -1) {
        printf("pwritev failed\n");
    } else {
        printf("pwritev successful. %zd bytes written\n", bytes_written);
    }
    size = lseek(fd, 0, SEEK_END); 
    printf("Current file size after: %ld\n", size);

}

int main() {
    int fd = get_fd("subdir_2/subdir_1/subdir_2/subdir_3/subfile_3", O_RDONLY);
    if (fd == -1) {
        return 1;
    }
    
    fd_pwrite_00067_nEEKq(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_2/subdir_3/subfile_3 exists.)
wasmtime run --dir=. test.wasm

Expected Results

Print:

Get file descriptor of file subdir_2/subdir_1/subdir_2/subdir_3/subfile_3 succeed!
Enter function fd_pwrite_00067_nEEKq
Current file size before: 87
pwritev failed
Current file size after: 87

This is what WAMR, WasmEdge and Linux native code do.

Actual Results

wasmtime prints:

Get file descriptor of file subdir_2/subdir_1/subdir_2/subdir_3/subfile_3 succeed!
Enter function fd_pwrite_00067_nEEKq
Current file size before: 87
pwritev successful. 0 bytes written
Current file size after: 87

The file is opened with O_RDONLY.
Maybe write successful message is not expected.

Versions and Environment

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

@Userzxcvbvnm Userzxcvbvnm added the bug Incorrect behavior in the current implementation that needs fixing label Jun 17, 2024
alexcrichton added a commit to alexcrichton/wasmtime that referenced this issue Jun 17, 2024
When a 0-length write is performed try to send the write all the way to
the underlying file descriptor to at least check that it's valid to
write.

Closes bytecodealliance#8818
alexcrichton added a commit to alexcrichton/wasmtime that referenced this issue Jun 17, 2024
When a 0-length write is performed try to send the write all the way to
the underlying file descriptor to at least check that it's valid to
write.

Closes bytecodealliance#8818
@alexcrichton
Copy link
Member

Thanks for the detailed report!

(and for the others too)

github-merge-queue bot pushed a commit that referenced this issue Jun 17, 2024
* Force some more permission checks with 0-length writes

When a 0-length write is performed try to send the write all the way to
the underlying file descriptor to at least check that it's valid to
write.

Closes #8818

* Update crates/test-programs/src/bin/preview1_file_write.rs

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>

* Allow a second error for Windows as well

---------

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants