wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX and WASI
https://wasmer.io
MIT License
19.07k stars 819 forks source link

Error file size ? #4271

Open orangeC23 opened 1 year ago

orangeC23 commented 1 year ago

Steps to reproduce

(1)The c file is :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <sys/stat.h>

int main() {
    const char* file_name = "Data/hello.txt";
    int open_style= O_WRONLY | O_CREAT;
    int fd = get_fd(file_name, open_style);
    fd_pwrite7FAS6BAKLT(fd);
    snapshot(fd);
    return 0;
}

int fd_pwrite7FAS6BAKLT(int fd) {
    char buffer1[] = "xJ4lgHSgN50d";
    off_t offset_value = 0;
    int start_value = 129;
    return fd_pwrite(fd, buffer1, offset_value, start_value);
}

int get_fd(const char* file_name, int open_style){
    int fd = open(file_name, open_style);
    if (fd == -1) {
        perror("Failed to open the file");
        return 1;
    }

    return fd;
}

int fd_pwrite(int fd, char buffer1[], off_t offset_value, int start_value) {
    printf("Enter fd_pwrite\n");

    struct iovec iov[1];
    iov[0].iov_base = buffer1;
    iov[0].iov_len = strlen(buffer1);

    off_t new_offset = offset_value;

    if (lseek(fd, new_offset, SEEK_SET) == -1) {
        perror("Error setting new offset");
        close(fd);
        return 1;
    }

    off_t current_offset = lseek(fd, 0, SEEK_CUR);
    if (current_offset == -1) {
        perror("Error getting current offset");
    }
    printf("Current offset: %lld\n", (long long)current_offset);

    off_t offset = start_value;
    ssize_t bytes_written = pwritev(fd, iov, 1, offset);
    if (bytes_written == -1) {
        perror("Error writing to the file");
        close(fd);
        return 1;
    }

    printf("Wrote %zd bytes to the file.\n", bytes_written);

    printf("Leave fd_pwrite\n");
    return fd;
}

int snapshot(int myfd){   
    printf("Enter snapshot\n");

    struct stat file_info;
    if (fstat(myfd, &file_info) == -1) {
        perror("Error getting file attributes");
        close(myfd);
        return 1;
    }

    printf("File Size: %lld bytes \n", (long long)file_info.st_size);

    off_t cur_offset = lseek(myfd, 0, SEEK_CUR);
    if (cur_offset == -1) {
        perror("Error getting current offset");
    }
    printf("Current offset: %lld \n", (long long)cur_offset);

    if (close(myfd) == -1) {
        perror("Error closing file");
        return 1;
    }

    printf("Leave snapshot\n");
}

(2) compile the c file into wasm: ./wasi-sdk-16.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-16.0/share/wasi-sysroot test.c -o test.wasm (3)exeute open.wasm wasmer run --dir=./Data test.wasm The permission of Data/hello.txt is 0700, user1 create the Data/hello.txt file and user1 execute the Wasm file.

Expected behavior

wasmtime, wazero, wamr, wasmedge print:

Enter fd_pwrite
Current offset: 0
Wrote 12 bytes to the file.
Leave fd_pwrite
Enter snapshot
File Size: 141 bytes 
Current offset: 0 
Leave snapshot

And using ls -al ./Data/hello.txt to check the file size is 141.

Actual behavior

wasmer prints:

Enter fd_pwrite
Current offset: 0
Wrote 12 bytes to the file.
Leave fd_pwrite
Enter snapshot
File Size: 42 bytes 
Current offset: 0 
Leave snapshot

And using ls -al ./Data/hello.txt to check the file size is 141. This is different from the printing value 42. Maybe this is a bug?

version

wasmer 4.2.2

ubuntu 20.04

Arshia001 commented 1 year ago

probably related to #4259, #4265 and #4270.

stale[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.