Skip to content

Commit 872b3bc

Browse files
committed
add test for reading file after munmap
1 parent 93de74f commit 872b3bc

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

tests/wasix/read-after-munmap/main.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
#include <sys/mman.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
#include <string.h>
8+
9+
int main()
10+
{
11+
int fd;
12+
char *data;
13+
14+
fd = open("data/my_file.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
15+
if (fd == -1)
16+
{
17+
printf("open");
18+
exit(1);
19+
}
20+
21+
write(fd, "abcdef", 6);
22+
23+
struct stat statbuf;
24+
fstat(fd, &statbuf);
25+
size_t filesize = statbuf.st_size;
26+
27+
data = mmap(NULL, 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 2);
28+
if (data == MAP_FAILED)
29+
{
30+
printf("mmap");
31+
exit(1);
32+
}
33+
34+
memcpy(data, "hi", 2);
35+
36+
munmap(data, 2);
37+
38+
off_t offset = lseek(fd, 0, SEEK_SET);
39+
if (offset == -1)
40+
{
41+
printf("lseek");
42+
}
43+
44+
char buffer[filesize];
45+
ssize_t bytes_read = read(fd, buffer, filesize);
46+
if (bytes_read == -1)
47+
{
48+
printf("read");
49+
exit(1);
50+
}
51+
52+
if (strncmp(buffer, "abhief", filesize) != 0)
53+
{
54+
printf("Error: Expected content 'abhief', got '%s'\n", buffer);
55+
exit(1);
56+
}
57+
58+
printf("0");
59+
close(fd);
60+
return 0;
61+
}

tests/wasix/read-after-munmap/run.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
$WASMER -q run main.wasm --mapdir=/data:. > output
4+
5+
printf "0" | diff -u output - 1>/dev/null
6+
7+
rm my_file.txt

0 commit comments

Comments
 (0)