Add a test case for mmap MAP_GROWSDOWN flag
PATCH v6 1/1 Add a test case for mmap MAP_GROWSDOWN flag
From: pravin <[email protected]>
This test implements two cases.
First one uses MAP_GROWSDOWN mapping as a thread stack and expects that the thread will grow the stack successfully.
The second one will have a 'break' page allocated into the space the mapping is supposed to grow and expects that the thread will be killed with SIGSEGV.
Resolves #300
--- runtest/syscalls | 1 + testcases/kernel/syscalls/mmap/.gitignore | 1 + testcases/kernel/syscalls/mmap/mmap18.c | 215 ++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 testcases/kernel/syscalls/mmap/mmap18.c
runtest/syscalls +1 -0
@@ -750,6 +750,7 @@ mmap14 mmap14 mmap15 mmap15 mmap16 mmap16 mmap17 mmap17 +mmap18 mmap18 modify_ldt01 modify_ldt01 modify_ldt02 modify_ldt02
testcases/kernel/syscalls/mmap/.gitignore +1 -0
@@ -16,3 +16,4 @@ /mmap15 /mmap16 /mmap17 +/mmap18
testcases/kernel/syscalls/mmap/mmap18.c new file +215 -0
215 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v6 1/1 Add a test case for mmap MAP_GROWSDOWN flag
Quoted 103 lines · Cyril Hrubis
...
+/*
+ * Testmmap()MAP_GROWSDOWNflag
+ *
+ * # Test1:
+ *
+ * We assign the memory region partially allocated withMAP_GROWSDOWN
flag to+ * a thread as a stack and expect the mapping to grow when we touch the + * guard page by calling a recusive function in the thread that uses the + * growable mapping as a stack.+ *
+ * The kernel only grows the memory region when the stack pointer is
within
+ * guard page when the guard page is touched so simply faulting the
guard
+ * page will not cause the mapping to grow.
+ *
+ * Newer kernels does not allow aMAP_GROWSDOWNmapping to grow closer
than
+ * 'stack_guard_gap' pages to an existing mapping. So when we map the
stack we
+ * make sure there is enough of free address space before the lowest
stack
+ * address.
+ *
+ * Kernel default 'stack_guard_gap' size is '256 *getpagesize()'.
+ *
+ * The stack memory map would look like:
+ *
+ * | - - - reserved size - - - |
+ *+ * +-- - - - --+------------+-------------+ + * | 256 pages | unmapped | mapped | + * +-- - - - --+------------+-------------++ * | mapped size |
+ * ^ | - - stack size - - |
+ * start
+ * ^ ^
+ * stack bottom stack top
+ *
+ * # Test2:
+ *
+ * We allocate stack as we do in the first test but we mmap a page in
the
+ * space the stack is supposed to grow into and we expect the thread to
+ * segfault when the guard page is faulted.
+ */testcases/kernel/syscalls/mmap/mmap18.c new file +31
-0+ +#include <unistd.h> +#include <pthread.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <sys/types.h> +#include <stdlib.h> +#include <stdbool.h> + +#include "tst_test.h" +#include "tst_safe_pthread.h" + +static long page_size; + +static bool __attribute__((noinline))check_stackgrow_up (void ) +{ +char local_var; +static char *addr; + +if (!addr) { + addr = &local_var; +return check_stackgrow_up (); + } + +return (addr < &local_var); +} + +static void setup (void ) +{ +if (check_stackgrow_up ()) + tst_brk(TCONF, "Test can't be performed with stack growsup architecture");
testcases/kernel/syscalls/mmap/mmap18.c new file +6
-0+ + page_size =getpagesize (); +} + +/* +* Returns stack lowest address. Note that the address is not mapped and will
+ * be mapped on page fault when we grow the stack to the lowest address possible.
+ */testcases/kernel/syscalls/mmap/mmap18.c new file +11
-0+static void *allocate_stack (size_t stack_size,size_t mapped_size) +{ +void *start, *stack_top, *stack_bottom; + +long reserved_size =256 * page_size + stack_size; + + start =SAFE_MMAP (NULL , reserved_size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +SAFE_MUNMAP (start, reserved_size); + + SAFE_MMAP((start + reserved_size - mapped_size), mapped_size,
PROT_READ|PROT_WRITE,
testcases/kernel/syscalls/mmap/mmap18.c line 101
Quoted 5 lines
testcases/kernel/syscalls/mmap/mmap18.c new file +5
-0+MAP_FIXED |MAP_PRIVATE |MAP_ANONYMOUS |MAP_GROWSDOWN , + -1, 0); + + stack_top = start + reserved_size; + stack_bottom = start + reserved_size - stack_size;
As the stack grows down, shouldn't grow from stack_bottom to stack_top? which
means stack_bottom = start + reserved_size.
* | - - - reserved size - - - |
*
* +-- - - - --+------------+-------------+ * | 256 pages | unmapped | mapped | * +-- - - - --+------------+-------------+
* | mapped size |
* ^ | - - stack size - - |
* start
* ^ ^
* stack top stack bottom
Review
Quoted 3 lines
(quoted diff) +2
-0+ + tst_res(TINFO, "start = %p, stack_top = %p, stack bottom = %p",
a typo here: stack_bottom ^
testcases/kernel/syscalls/mmap/mmap18.c line 108
Quoted 6 lines
testcases/kernel/syscalls/mmap/mmap18.c new file +5
-0+ start, stack_top, stack_bottom); +tst_res (TINFO ,"mapped pages %zu, stack pages %zu" , + mapped_size/page_size, stack_size/page_size); + +return stack_bottom;
return stack_top here?
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200921/5c817a49/attachment.htm>
PATCH v6 1/1 Add a test case for mmap MAP_GROWSDOWN flag
Hi!
Quoted 17 lines
Quoted 17 lines
+ * address.
+ *
+ * Kernel default 'stack_guard_gap' size is '256 *getpagesize()'.
+ *
+ * The stack memory map would look like:
+ *
+ * | - - - reserved size - - - |
+ *+ * +-- - - - --+------------+-------------+ + * | 256 pages | unmapped | mapped | + * +-- - - - --+------------+-------------++ * | mapped size |
+ * ^ | - - stack size - - |
+ * start
+ * ^ ^
+ * stack bottom stack top
+ *
...
Quoted 14 lines
Quoted 14 lines
testcases/kernel/syscalls/mmap/mmap18.c new file +11
-0+static void *allocate_stack (size_t stack_size,size_t mapped_size) +{ +void *start, *stack_top, *stack_bottom; + +long reserved_size =256 * page_size + stack_size; + + start =SAFE_MMAP (NULL , reserved_size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +SAFE_MUNMAP (start, reserved_size); + + SAFE_MMAP((start + reserved_size - mapped_size), mapped_size,
PROT_READ|PROT_WRITE,testcases/kernel/syscalls/mmap/mmap18.c new file +2
-0+MAP_FIXED |MAP_PRIVATE |MAP_ANONYMOUS |MAP_GROWSDOWN , + -1, 0);
Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap18.c line 101
Quoted 4 lines
testcases/kernel/syscalls/mmap/mmap18.c new file +3
-0+ + stack_top = start + reserved_size; + stack_bottom = start + reserved_size - stack_size;As the stack grows down, shouldn't grow from
stack_bottomtostack_top? which
meansstack_bottom= start +reserved_size.
That depends on the definition of top and bottom. For me it makes sense that the the stack grows from top to the bottom because it grows down, which is consistent with the MAP_GROWSDOWN flag.
Also this is really the reason why I added the ASCII art to the top level comment of the test in order to make clear how these terms are used in the code.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v6 1/1 Add a test case for mmap MAP_GROWSDOWN flag
Quoted 9 lines · Cyril Hrubis
...
Quoted 3 lines
As the stack grows down, shouldn't grow from
stack_bottomtostack_top? which
meansstack_bottom= start +reserved_size.That depends on the definition of top and bottom. For me it makes sense that the the stack grows from top to the bottom because it grows down, which is consistent with the
MAP_GROWSDOWNflag.
Ok, it sounds we have different comprehension of the MAP_GROWSDOWN flag. As I thought the flag takes effect on an area of memory and then we regard the
region as a stack, so I said it grows from the bottom. But if we think a stack grows
down, then the definition is reverted here.
As this is a gray zone as you said, so I don't want to stick to my own opinion.
Quoted 4 lines
Also this is really the reason why I added the ASCII art to the top level comment of the test in order to make clear how these terms are used in the code.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200922/affc4958/attachment.htm>
PATCH v6 1/1 Add a test case for mmap MAP_GROWSDOWN flag
Hi!
Thanks for the review, pushed.
Signature · 2 lines
Cyril Hrubis [email protected]