Skip to content

Commit 40674f4

Browse files
fix: handle mmap offset
add and use offset parameter in doMsync add test case for shared mmap with offset fixes #10094
1 parent 9de3bd4 commit 40674f4

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@ a license to everyone to use it as detailed in LICENSE.)
443443
* John Granström <granstrom.john@gmail.com>
444444
* Clemens Backes <clemensb@google.com> (copyright owned by Google, Inc.)
445445
* Tibor Klajnscek <tiborkl@numfum.com>
446-
* Benjamin Golinvaux <benjamin@golinvaux.com>
446+
* Benjamin Golinvaux <benjamin@golinvaux.com>
447+
* Peter Salomonsen <pjsalomonsen@gmail.com>

src/library_syscall.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ var SyscallsLibrary = {
6969
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
7070
return 0;
7171
},
72-
doMsync: function(addr, stream, len, flags) {
72+
doMsync: function(addr, stream, len, flags, offset) {
7373
var buffer = new Uint8Array(HEAPU8.subarray(addr, addr + len));
74-
FS.msync(stream, buffer, 0, len, flags);
74+
FS.msync(stream, buffer, offset, len, flags);
7575
},
7676
doMkdir: function(path, mode) {
7777
// remove a trailing slash, if one - /a/b/ has basename of '', but
@@ -254,7 +254,7 @@ var SyscallsLibrary = {
254254
ptr = res.ptr;
255255
allocated = res.allocated;
256256
}
257-
SYSCALLS.mappings[ptr] = { malloc: ptr, len: len, allocated: allocated, fd: fd, flags: flags };
257+
SYSCALLS.mappings[ptr] = { malloc: ptr, len: len, allocated: allocated, fd: fd, flags: flags, offset: off };
258258
return ptr;
259259
},
260260

@@ -272,7 +272,7 @@ var SyscallsLibrary = {
272272
if (!info) return 0;
273273
if (len === info.len) {
274274
var stream = FS.getStream(info.fd);
275-
SYSCALLS.doMsync(addr, stream, len, info.flags);
275+
SYSCALLS.doMsync(addr, stream, len, info.flags, info.offset);
276276
FS.munmap(stream);
277277
SYSCALLS.mappings[addr] = null;
278278
if (info.allocated) {
@@ -855,7 +855,7 @@ var SyscallsLibrary = {
855855
var addr = SYSCALLS.get(), len = SYSCALLS.get(), flags = SYSCALLS.get();
856856
var info = SYSCALLS.mappings[addr];
857857
if (!info) return 0;
858-
SYSCALLS.doMsync(addr, FS.getStream(info.fd), len, info.flags);
858+
SYSCALLS.doMsync(addr, FS.getStream(info.fd), len, info.flags, 0);
859859
return 0;
860860
},
861861
__syscall147__deps: ['$PROCINFO'],

tests/fs/test_mmap.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,55 @@ int main() {
126126
fclose(fd);
127127
}
128128

129+
// MAP_SHARED with offset
130+
{
131+
const char* text = "written shared mmap with offset";
132+
const char* path = "/yolo/sharedoffset.txt";
133+
134+
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
135+
assert(fd != -1);
136+
137+
size_t textsize = strlen(text) + 1; // + \0 null character
138+
// offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).
139+
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
140+
141+
assert(lseek(fd, textsize + offset - 1, SEEK_SET) != -1);
142+
143+
// need to write something first to allow us to mmap
144+
assert(write(fd, "", 1) != -1);
145+
146+
char *map;
147+
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset - 1);
148+
// assert failure if offset is not a multiple of page size
149+
assert(map == MAP_FAILED);
150+
151+
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
152+
assert(map != MAP_FAILED);
153+
154+
for (size_t i = 0; i < textsize; i++) {
155+
map[i] = text[i];
156+
}
157+
158+
assert(msync(map, textsize, MS_SYNC) != -1);
159+
160+
assert(munmap(map, textsize) != -1);
161+
162+
close(fd);
163+
}
164+
165+
{
166+
FILE* fd = fopen("/yolo/sharedoffset.txt", "r");
167+
if (fd == NULL) {
168+
printf("failed to open /yolo/sharedoffset.txt\n");
169+
return 1;
170+
}
171+
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
172+
173+
char buffer[offset + 31];
174+
fread(buffer, 1, offset + 32, fd);
175+
// expect text written from mmap operation to appear at offset in the file
176+
printf("/yolo/sharedoffset.txt content=%s %d\n", buffer + offset, offset);
177+
fclose(fd);
178+
}
129179
return 0;
130180
}

tests/fs/test_mmap.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/yolo/in.txt content=mmap ftw!
22
/yolo/out.txt content=written mmap
33
/yolo/private.txt content=
4+
/yolo/sharedoffset.txt content=written shared mmap with offset 32768

0 commit comments

Comments
 (0)