Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

force mmap to return page-aligned addresses, and check for valid inpu…

…t in munmap
  • Loading branch information...
commit 9f8132c93fc0669e08187f55204f440f8363b9a1 1 parent 7b9f475
@kripken authored
Showing with 37 additions and 9 deletions.
  1. +29 −8 src/library.js
  2. +8 −1 tests/runner.py
View
37 src/library.js
@@ -3548,21 +3548,42 @@ LibraryManager.library = {
* this implementation simply uses malloc underneath the call to
* mmap.
*/
+ if (!_mmap.mappings) _mmap.mappings = {};
if (stream == -1) {
var ptr = _malloc(num);
- _memset(ptr, 0, num);
- return ptr;
+ } else {
+ var info = FS.streams[stream];
+ if (!info) return -1;
+ var contents = info.object.contents;
+ contents = Array.prototype.slice.call(contents, offset, offset+num);
+ ptr = allocate(contents, 'i8', ALLOC_NORMAL);
+ }
+ // align to page size
+ var ret = ptr;
+ if (ptr % PAGE_SIZE != 0) {
+ var old = ptr;
+ ptr = _malloc(num + PAGE_SIZE);
+ _memcpy(ptr, old, num);
+ _free(old);
+ ret = alignMemoryPage(ptr);
}
- var info = FS.streams[stream];
- if (!info) return -1;
- var contents = info.object.contents;
- contents = Array.prototype.slice.call(contents, offset, offset+num);
- return allocate(contents, 'i8', ALLOC_NORMAL);
+ if (stream == -1) {
+ _memset(ret, 0, num);
+ }
+ _mmap.mappings[ret] = { malloc: ptr, num: num };
+ return ret;
},
__01mmap64_: 'mmap',
munmap: function(start, num) {
- _free(start);
+ if (!_mmap.mappings) _mmap.mappings = {};
+ // TODO: support unmmap'ing parts of allocations
+ var info = _mmap.mappings[start];
+ if (!info) return 0;
+ if (num == info.num) {
+ _mmap.mappings[start] = null;
+ _free(info.malloc);
+ }
return 0;
},
View
9 tests/runner.py
@@ -6181,13 +6181,20 @@ def test_mmap(self):
#include <assert.h>
int main(int argc, char *argv[]) {
+ for (int i = 0; i < 10; i++) {
+ int* map = (int*)mmap(0, 5000, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, -1, 0);
+ assert(((int)map) % 4096 == 0); // aligned
+ assert(munmap(map, 5000) == 0);
+ }
+
const int NUM_BYTES = 8 * 1024 * 1024;
const int NUM_INTS = NUM_BYTES / sizeof(int);
int* map = (int*)mmap(0, NUM_BYTES, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0);
assert(map != MAP_FAILED);
-
+
int i;
for (i = 0; i < NUM_INTS; i++) {
Please sign in to comment.
Something went wrong with that request. Please try again.