Skip to contentSkip to conversation

Test brk vma code path

PATCH Superseded Liam Howlett 2 participants 5 messages Feb 9, 2021 – Feb 25, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Test brk vma code path

The brk system call uses a slightly different code path through the kernel to expand/contract across VMAs. This new test is written to force the VMA to create a two new entries and shrink back across VMA boundaries.

Liam R. Howlett (1):
brk02: Add test for removing more than one VMA

 testcases/kernel/syscalls/brk/brk02.c | 49 +++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/brk/brk02.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/1 brk02: Add test for removing more than one VMA

When brk expands, it attempts to expand a VMA. This expansion will succeed depending on the anonymous VMA chain and if the vma flags are compatible. This test expands brk() then calls mprotect to ensure the next brk call will create a new VMA, then it calls brk a final time to restore the first brk address. The test is the final brk call which will remove more than an entire VMA from the vm area.

---
 testcases/kernel/syscalls/brk/brk02.c | 49 +++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/brk/brk02.c
testcases/kernel/syscalls/brk/brk02.c new file +49 -0
new file mode 100644
index 000000000..834fe9f2f
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Liam R. Howlett <[email protected]>
+ *
+ *  
+ *  Expand the brk by 2 pages to ensure there is a newly created VMA and not
+ *  expanding the original due to multiple anon pages.  mprotect that new VMA
+ *  then brk back to the original address therefore causing a munmap of at
+ *  least one full VMA.
+ */
+
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/mman.h>
+
+#include "tst_test.h"
+
+void brk_down_vmas(void)
+{
+	void *brk_addr = sbrk(0);
+	unsigned long page_size = getpagesize();
+	void *addr = brk_addr + page_size;
+
+	if (brk(addr))
+		return;
+
+	addr += page_size;
+	if (brk(addr))
+		return;
+
+	if (mprotect(addr - page_size, page_size,
+			PROT_READ|PROT_WRITE|PROT_EXEC))
+		return;
+
+	addr += page_size;
+	if (brk(addr))
+		return;
+
+	if (brk(brk_addr))
+		return;
+
+
+
+	tst_res(TPASS, "munmap two VMAs of brk() passed.");
+}
+
+static struct tst_test test = {
+	.test_all = brk_down_vmas,
+};
Signature · 1 line
2.28.0
#2 lore raw

PATCH 1/1 brk02: Add test for removing more than one VMA

Hi Liam,

thanks for your patch. Minor notes below.

Quoted 19 lines

When brk expands, it attempts to expand a VMA. This expansion will succeed depending on the anonymous VMA chain and if the vma flags are compatible. This test expands brk() then calls mprotect to ensure the next brk call will create a new VMA, then it calls brk a final time to restore the first brk address. The test is the final brk call which will remove more than an entire VMA from the vm area.

---
 testcases/kernel/syscalls/brk/brk02.c | 49 +++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/brk/brk02.c
testcases/kernel/syscalls/brk/brk02.c new file +0 -0
new file mode 100644
index 000000000..834fe9f2f

testcases/kernel/syscalls/brk/brk02.c line 5

Quoted 5 lines
testcases/kernel/syscalls/brk/brk02.c new file +5 -0
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Liam R. Howlett <[email protected]>
+ *
+ *  

nit: blank line

testcases/kernel/syscalls/brk/brk02.c line 13

Quoted 8 lines
testcases/kernel/syscalls/brk/brk02.c new file +8 -0
+ *  Expand the brk by 2 pages to ensure there is a newly created VMA and not
+ *  expanding the original due to multiple anon pages.  mprotect that new VMA
+ *  then brk back to the original address therefore causing a munmap of at
+ *  least one full VMA.
+ */
+
+#include <unistd.h>
+#include <stdint.h>

nit: Not sure if <stdint.h> is needed.

Quoted 6 lines
testcases/kernel/syscalls/brk/brk02.c new file +6 -0
+#include <sys/mman.h>
+
+#include "tst_test.h"
+
+void brk_down_vmas(void)
+{

testcases/kernel/syscalls/brk/brk02.c line 29

Quoted 10 lines
testcases/kernel/syscalls/brk/brk02.c new file +10 -0
+	void *brk_addr = sbrk(0);
+	unsigned long page_size = getpagesize();
+	void *addr = brk_addr + page_size;
+
+	if (brk(addr))
+		return;
+
+	addr += page_size;
+	if (brk(addr))
+		return;

You need to add tst_ret(TFAIL, "failed due ..."); before each return otherwise you get error:
tst_test.c:1080: TBROK: Test haven't reported results!

Quoted 4 lines
testcases/kernel/syscalls/brk/brk02.c new file +4 -0
+
+	if (mprotect(addr - page_size, page_size,
+			PROT_READ|PROT_WRITE|PROT_EXEC))
+		return;

testcases/kernel/syscalls/brk/brk02.c line 43

Quoted 10 lines
testcases/kernel/syscalls/brk/brk02.c new file +10 -0
+
+	addr += page_size;
+	if (brk(addr))
+		return;
+
+	if (brk(brk_addr))
+		return;
+
+
+

Please remove these blank lines.

testcases/kernel/syscalls/brk/brk02.c line 49

Quoted 6 lines
testcases/kernel/syscalls/brk/brk02.c new file +6 -0
+	tst_res(TPASS, "munmap two VMAs of brk() passed.");
+}
+
+static struct tst_test test = {
+	.test_all = brk_down_vmas,
+};

Kind regards,
Petr

#3 lore raw

PATCH 1/1 brk02: Add test for removing more than one VMA

Petr,

Thank you for looking at this patch.

Quoted 17 lines · Petr Vorel

Hi Liam,

thanks for your patch. Minor notes below.

Quoted 12 lines

When brk expands, it attempts to expand a VMA. This expansion will succeed depending on the anonymous VMA chain and if the vma flags are compatible. This test expands brk() then calls mprotect to ensure the next brk call will create a new VMA, then it calls brk a final time to restore the first brk address. The test is the final brk call which will remove more than an entire VMA from the vm area.

---
 testcases/kernel/syscalls/brk/brk02.c | 49 +++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 testcases/kernel/syscalls/brk/brk02.c
Quoted 12 lines

testcases/kernel/syscalls/brk/brk02.c line 5

Quoted 11 lines
testcases/kernel/syscalls/brk/brk02.c new file +5 -0
new file mode 100644
index 000000000..834fe9f2f
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Liam R. Howlett <[email protected]>
+ *
+ *  

nit: blank line

Will do.

Quoted 9 lines

testcases/kernel/syscalls/brk/brk02.c line 13

Quoted 8 lines
testcases/kernel/syscalls/brk/brk02.c new file +8 -0
+ *  Expand the brk by 2 pages to ensure there is a newly created VMA and not
+ *  expanding the original due to multiple anon pages.  mprotect that new VMA
+ *  then brk back to the original address therefore causing a munmap of at
+ *  least one full VMA.
+ */
+
+#include <unistd.h>
+#include <stdint.h>

nit: Not sure if <stdint.h> is needed.

It is not, thank you. I will fix it.

Quoted 12 lines
Quoted 12 lines
testcases/kernel/syscalls/brk/brk02.c new file +11 -0
+#include <sys/mman.h>
+
+#include "tst_test.h"
+
+void brk_down_vmas(void)
+{
+	void *brk_addr = sbrk(0);
+	unsigned long page_size = getpagesize();
+	void *addr = brk_addr + page_size;
+
+	if (brk(addr))
Quoted 8 lines

testcases/kernel/syscalls/brk/brk02.c line 29

Quoted 5 lines
testcases/kernel/syscalls/brk/brk02.c new file +5 -0
+		return;
+
+	addr += page_size;
+	if (brk(addr))
+		return;

You need to add tst_ret(TFAIL, "failed due ..."); before each return otherwise you get error:
tst_test.c:1080: TBROK: Test haven't reported results!

I believe you mean tst_res(). I will fix it.

Quoted 16 lines
Quoted 5 lines
testcases/kernel/syscalls/brk/brk02.c new file +4 -0
+
+	if (mprotect(addr - page_size, page_size,
+			PROT_READ|PROT_WRITE|PROT_EXEC))
+		return;

testcases/kernel/syscalls/brk/brk02.c line 43

Quoted 10 lines
testcases/kernel/syscalls/brk/brk02.c new file +10 -0
+
+	addr += page_size;
+	if (brk(addr))
+		return;
+
+	if (brk(brk_addr))
+		return;
+
+
+

Please remove these blank lines.

Will do.

Thank you,
Liam

Quoted 9 lines

testcases/kernel/syscalls/brk/brk02.c line 49

Quoted 6 lines
testcases/kernel/syscalls/brk/brk02.c new file +6 -0
+	tst_res(TPASS, "munmap two VMAs of brk() passed.");
+}
+
+static struct tst_test test = {
+	.test_all = brk_down_vmas,
+};

Kind regards,
Petr

#4 lore raw

PATCH 1/1 brk02: Add test for removing more than one VMA

Hi Liam,

Quoted 8 lines
Quoted 6 lines

testcases/kernel/syscalls/brk/brk02.c line 29

Quoted 3 lines
testcases/kernel/syscalls/brk/brk02.c new file +3 -0
+	addr += page_size;
+	if (brk(addr))
+		return;

You need to add tst_ret(TFAIL, "failed due ..."); before each return otherwise you get error:
tst_test.c:1080: TBROK: Test haven't reported results!

I believe you mean tst_res(). I will fix it.

Yes, sorry for typo.

Thanks for your work!

Kind regards,
Petr