Skip to content

Commit dec266f

Browse files
committed
Add test/mremap.c, using MREMAP_FIXED
1 parent f113785 commit dec266f

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/mtcp/mtcp_restart.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ compute_regions_to_munmap(RestoreInfo *rinfo)
647647

648648
NO_OPTIMIZE
649649
static void
650-
restore_vdso_vvar_work(MemRegion *currentRegion, MemRegion *ckptRegion, const char *regionName)
650+
restore_vdso_vvar_work(MemRegion *currentRegion, MemRegion *ckptRegion,
651+
const char *regionName)
651652
{
652653
int mtcp_sys_errno;
653654

@@ -677,7 +678,8 @@ restore_vdso_vvar(RestoreInfo *rinfo)
677678
{
678679
restore_vdso_vvar_work(&rinfo->currentVdso, &rinfo->ckptHdr.vdso, "[vdso]");
679680
restore_vdso_vvar_work(&rinfo->currentVvar, &rinfo->ckptHdr.vvar, "[vvar]");
680-
restore_vdso_vvar_work(&rinfo->currentVvarVClock, &rinfo->ckptHdr.vvarVClock, "[vvar_vclock]");
681+
restore_vdso_vvar_work(&rinfo->currentVvarVClock, &rinfo->ckptHdr.vvarVClock,
682+
"[vvar_vclock]");
681683

682684
if (rinfo->currentVdso.startAddr != 0) {
683685
mtcp_setauxval(rinfo->environ, AT_SYSINFO_EHDR,
@@ -967,9 +969,9 @@ doAreasOverlap(Area *area, MemRegion *memRegion)
967969
}
968970

969971
/* This uses MREMAP_FIXED | MREMAP_MAYMOVE to move a memory segment.
970-
* Note that we need 'MREMAP_MAYMOVE'. With only 'MREMAP_FIXED', the
971-
* kernel can overwrite an existing memory region.
972-
* Note that 'mremap' and hence 'mremap_move' do not allow overlapping src and dest.
972+
* Note that 'MREMAP_MAYMOVE' is required, if using 'MREMAP_FIXED'.
973+
* Note that 'mremap' and hence 'mremap_move' do not allow overlapping
974+
* src and dest.
973975
*/
974976
NO_OPTIMIZE
975977
static int
@@ -987,7 +989,7 @@ mremap_move(void *dest, void *src, size_t size) {
987989
return -1; // Error
988990
} else {
989991
// Else 'MREMAP_MAYMOVE' forced the remap to the wrong location. So, the
990-
// memory was moved to the wrong desgination. Undo the move, and return -1.
992+
// memory was moved to the wrong destination. Undo the move, and return -1.
991993
mremap_move(src, rc, size);
992994
return -1; // Error
993995
}

test/autotest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ def saveResultsNMI():
897897

898898
runTest("mmap1", 1, ["./test/mmap1"])
899899

900+
runTest("mremap", 1, ["./test/mremap"])
901+
900902
if HAS_SELINUX == "yes":
901903
runTest("selinux1", 1, ["./test/selinux1"])
902904

test/mremap.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#define _GNU_SOURCE
2+
#include <assert.h>
3+
#include <unistd.h>
4+
#include <sys/mman.h>
5+
6+
int main() {
7+
long page_size = sysconf(_SC_PAGESIZE);
8+
void *old_addr1, *old_addr2, *new_addr;
9+
while (1) {
10+
old_addr1 = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
11+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
12+
old_addr2 = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
13+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
14+
munmap(old_addr2, page_size);
15+
// We now know that old_addr1 is mapped and old_addr2 is not mapped.
16+
new_addr = mremap(old_addr1, page_size, page_size,
17+
MREMAP_MAYMOVE | MREMAP_FIXED, old_addr2);
18+
assert (new_addr == old_addr2);
19+
munmap(old_addr2, page_size);
20+
sleep(1);
21+
}
22+
return 0;
23+
}

0 commit comments

Comments
 (0)