Open
Description
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_00021_L4Yu4(int fd) {
printf("Enter function fd_filestat_set_size_00021_L4Yu4\n");
off_t size = lseek(fd, 0, SEEK_END); // Get current file size
printf("Current file size: %ld\n", size);
if (ftruncate(fd, 1) == 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("subfile_2", O_WRONLY | O_TRUNC | O_CREAT); // Open the file in read-write mode
if (fd == -1) {
return 1; // Return from main if get_fd fails
}
fd_filestat_set_size_00021_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, subfile_2 exists and the size is 98.)
wasmer run --dir=. test.wasm
Expected behavior
print:
Get file descriptor of file subfile_2 succeed!
Enter function fd_filestat_set_size_00021_L4Yu4
Current file size: 0
File size adjusted successfully
New file size: 1
And the file size is 1 checking by commad line instruction.
This is what wasmtime, WAMR and WasmEdge do.
Actual behavior
print:
Get file descriptor of file subfile_2 succeed!
Enter function fd_filestat_set_size_00021_L4Yu4
Current file size: 0
File size adjusted successfully
New file size: 1
However the file size is still 98 checking by commad line instruction.
Additional context
Ubuntu 20.04
x86_64
wasmer-4.3.1