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

File allocation bug? #3532

Open
Userzxcvbvnm opened this issue Jun 15, 2024 · 2 comments
Open

File allocation bug? #3532

Userzxcvbvnm opened this issue Jun 15, 2024 · 2 comments

Comments

@Userzxcvbvnm
Copy link

Userzxcvbvnm commented Jun 15, 2024

Subject of the issue

Allocate the file size from a value which is larger the file size, WAMR fails to allocate. I'm not sure whether this is a bug.

Test case


#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_00072_gxX49(int fd) {
    printf("Enter function fd_allocate_00072_gxX49\n");
    
    int result = posix_fallocate(fd, 1, 0);

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

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

    print_file_size(fd);
    fd_allocate_00072_gxX49(fd);
    print_file_size(fd);


    closebyfd(fd);

    return 0;
}

Your environment

Ubuntu 20.04
x86_64
WAMR 1.3.2 and WAMR 1.2.3

Steps to reproduce

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, subdir_1/subfile_2 exists.)
iwasm --dir=. test.wasm

Expected behavior

Get file size: 0 bytes.
Enter function fd_allocate_00072_gxX49
Space allocation in file successful.
Get file size: 1 bytes.

Actual behavior

Get file descriptor of file subdir_1/subfile_2 succeed!
Get file size: 0 bytes.
Enter function fd_allocate_00072_gxX49
Error allocating space in file.
Get file size: 0 bytes.
@lum1n0us
Copy link
Collaborator

Might want to change
int result = posix_fallocate(fd, 1, 0);
->
int result = posix_fallocate(fd, 1, 70);

EINVAL offset was less than 0, or len was less than or equal to 0, or the underlying filesystem does not support the operation.

@yamt
Copy link
Collaborator

yamt commented Jun 27, 2024

i agree this is not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants