Skip to content

Commit

Permalink
Allow failed MADV_REMOVEs when the only target mappings (if any) are …
Browse files Browse the repository at this point in the history
…MAP_PRIVATE

tcmalloc does this and it's perfectly fine because such MADV_REMOVEs have no effect.
  • Loading branch information
rocallahan committed Jan 11, 2024
1 parent 60e3ddc commit fa33606
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1076,9 +1076,10 @@ set(BASIC_TESTS
legacy_ugid
x86/lsl
madvise
madvise_free
madvise_dontneed_private
madvise_free
madvise_misc
madvise_remove
madvise_wipeonfork
map_fixed
map_shared_syscall
Expand Down
17 changes: 14 additions & 3 deletions src/record_syscall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6488,10 +6488,21 @@ static void record_madvise(RecordTask* t) {
ranges.insert(ranges.end(), pages_present.begin(), pages_present.end());
}
break;
case MADV_REMOVE:
// We don't handle this yet...
ASSERT(t, false) << "Possibly-partial MADV_REMOVEs not handled yet";
case MADV_REMOVE: {
for (const auto& m : t->vm()->maps_containing_or_after(start)) {
if (m.map.start() >= end) {
break;
}
if (m.map.flags() & MAP_PRIVATE) {
// Private mappings fail cleanly with MADV_REMOVE so we
// don't need to worry about any effects on them.
continue;
}
// We don't handle this yet...
ASSERT(t, false) << "Possibly-partial MADV_REMOVEs not handled yet";
}
break;
}
default:
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/madvise_remove.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */

#include "util.h"

int main(void) {
size_t page_size = sysconf(_SC_PAGESIZE);
char* p =
mmap(NULL, page_size*3, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
test_assert(p != MAP_FAILED);
test_assert(0 == munmap(p + page_size, page_size));

test_assert(-1 == madvise(p, page_size, MADV_REMOVE));
test_assert(errno == EINVAL);

atomic_puts("EXIT-SUCCESS");

return 0;
}

0 comments on commit fa33606

Please sign in to comment.