mmap(2) with MAP_ANON honouring offset although it shouldn't

Alexander Best alexbestms at math.uni-muenster.de
Sat Oct 31 02:38:33 UTC 2009


John Baldwin schrieb am 2009-10-21:
> On Wednesday 21 October 2009 11:51:04 am Alexander Best wrote:
> > although the mmap(2) manual states in section MAP_ANON:

> > "The offset argument is ignored."

> > this doesn't seem to be true. running

> > printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_NONE, MAP_ANON, -1,
> > 0x12345678));

> > and

> > printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_NONE, MAP_ANON, -1,
> > 0));

> > produces different outputs. i've attached a patch to solve the
> > problem. the
> > patch is similar to the one proposed in this PR, but should apply
> > cleanly to
> > CURRENT: http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/71258

> A simpler patch would be to simply set pos = 0 below the MAP_STACK
> line if
> MAP_ANON is set.

how about the following patch. problem seems to be that pos = 0 needs to be
set before pageoff is being calculated.

i've tested mmap with MAP_STACK and the offset gets discarded just as
documented in mmap(2). with the patch the offset handling with MAP_ANON and
MAP_STACK (implies MAP_ANON) are the same.

another short question:

why does the second call when doing

        printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_READ|PROT_WRITE,
        MAP_STACK, -1, 0x0));
        printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_READ|PROT_WRITE,
        MAP_STACK, -1, 0x0));

fail? doesn't MAP_STACK allow mapping the same region twice?

        printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_READ|PROT_WRITE,
        MAP_STACK, -1, 0x0));
        printf("%p\n", mmap((void*)0x2000, 0x1000, PROT_READ|PROT_WRITE,
        MAP_STACK, -1, 0x0));

works just as expected.

cheers.
alex
-------------- next part --------------
--- /usr/src/sys/vm/vm_mmap.c	2009-10-28 21:37:53.000000000 +0100
+++ ./vm_mmap.c	2009-10-31 03:22:44.000000000 +0100
@@ -241,9 +241,11 @@
 		    ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
 			return (EINVAL);
 		flags |= MAP_ANON;
-		pos = 0;
 	}
 
+	if (flags & MAP_ANON)
+		pos = 0;
+
 	/*
 	 * Align the file position to a page boundary,
 	 * and save its page offset component.
@@ -300,7 +302,6 @@
 		handle = NULL;
 		handle_type = OBJT_DEFAULT;
 		maxprot = VM_PROT_ALL;
-		pos = 0;
 	} else {
 		/*
 		 * Mapping file, get fp for validation and


More information about the freebsd-hackers mailing list