* [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api
2021-02-04 12:12 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
@ 2021-02-04 12:12 ` Yang Xu
2021-02-04 12:12 ` [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
` (3 subsequent siblings)
4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
1 file changed, 3 insertions(+), 19 deletions(-)
diff --git a/testcases/kernel/syscalls/mallopt/mallopt01.c b/testcases/kernel/syscalls/mallopt/mallopt01.c
index f799aaf9d..271079c12 100644
--- a/testcases/kernel/syscalls/mallopt/mallopt01.c
+++ b/testcases/kernel/syscalls/mallopt/mallopt01.c
@@ -12,10 +12,9 @@
* Basic mallinfo() and mallopt() testing.
\*/
-#include <malloc.h>
-
#include "tst_test.h"
#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
#ifdef HAVE_MALLOPT
@@ -23,21 +22,6 @@
struct mallinfo info;
-void print_mallinfo(void)
-{
- tst_res(TINFO, "mallinfo structure:");
- tst_res(TINFO, "mallinfo.arena = %d", info.arena);
- tst_res(TINFO, "mallinfo.ordblks = %d", info.ordblks);
- tst_res(TINFO, "mallinfo.smblks = %d", info.smblks);
- tst_res(TINFO, "mallinfo.hblkhd = %d", info.hblkhd);
- tst_res(TINFO, "mallinfo.hblks = %d", info.hblks);
- tst_res(TINFO, "mallinfo.usmblks = %d", info.usmblks);
- tst_res(TINFO, "mallinfo.fsmblks = %d", info.fsmblks);
- tst_res(TINFO, "mallinfo.uordblks = %d", info.uordblks);
- tst_res(TINFO, "mallinfo.fordblks = %d", info.fordblks);
- tst_res(TINFO, "mallinfo.keepcost = %d", info.keepcost);
-}
-
void test_mallopt(void)
{
char *buf;
@@ -46,11 +30,11 @@ void test_mallopt(void)
info = mallinfo();
if (info.uordblks < 20480) {
- print_mallinfo();
+ tst_print_mallinfo("Test uordblks", &info);
tst_res(TFAIL, "mallinfo() failed: uordblks < 20K");
}
if (info.smblks != 0) {
- print_mallinfo();
+ tst_print_mallinfo("Test smblks", &info);
tst_res(TFAIL, "mallinfo() failed: smblks != 0");
}
if (info.uordblks >= 20480 && info.smblks == 0)
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo
2021-02-04 12:12 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
2021-02-04 12:12 ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
@ 2021-02-04 12:12 ` Yang Xu
2021-02-04 12:12 ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
` (2 subsequent siblings)
4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
To: ltp
Referring to glibc test tst-mallinfo2.c[1], add a test to test mallinfo.
Also check mallinfo in autotools.
[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
v1->v2
1.use tst_print_mallinfo api
2.use buf array
3.use 160 multiple instead of 16
4.add free chunk check
runtest/syscalls | 2 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
testcases/kernel/syscalls/mallinfo/Makefile | 8 ++
.../kernel/syscalls/mallinfo/mallinfo01.c | 82 +++++++++++++++++++
4 files changed, 93 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/.gitignore
create mode 100644 testcases/kernel/syscalls/mallinfo/Makefile
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo01.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 11a1e83c2..753340068 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -682,6 +682,8 @@ lstat01_64 lstat01_64
lstat02 lstat02
lstat02_64 lstat02_64
+mallinfo01 mallinfo01
+
mallopt01 mallopt01
mbind01 mbind01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
new file mode 100644
index 000000000..a7e32a637
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -0,0 +1 @@
+/mallinfo01
diff --git a/testcases/kernel/syscalls/mallinfo/Makefile b/testcases/kernel/syscalls/mallinfo/Makefile
new file mode 100644
index 000000000..044619fb8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines Corp., 2001
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
new file mode 100644
index 000000000..add3c0df3
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test mallinfo2 test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+#define M_NUM 20
+static struct mallinfo info1;
+static void *buf[M_NUM + 1];
+
+static void cleanup(void)
+{
+ int i;
+
+ for (i = M_NUM; i > 0; i--) {
+ if (buf[i]) {
+ free(buf[i]);
+ buf[i] = NULL;
+ }
+ }
+}
+
+void test_mallinfo(void)
+{
+ int i;
+ int total = 0;
+ struct mallinfo info2;
+
+ for (i = 1; i <= M_NUM; i++) {
+ buf[i] = SAFE_MALLOC(160 * i);
+ total += 160 * i;
+ }
+ info2 = mallinfo();
+ tst_print_mallinfo("Test uordblks", &info2);
+ if (info2.uordblks > info1.uordblks + total)
+ tst_res(TPASS, "mallinfo() uordblks passed");
+ else
+ tst_res(TFAIL, "mallinfo() uordblks failed");
+
+ //Create another free chunk
+ free(buf[M_NUM/2]);
+ buf[M_NUM/2] = NULL;
+ info2 = mallinfo();
+ tst_print_mallinfo("Test ordblks", &info2);
+ if (info2.ordblks == info1.ordblks + 1)
+ tst_res(TPASS, "mallinfo() ordblks passed");
+ else
+ tst_res(TFAIL, "mallinfo() ordblks failed");
+
+ cleanup();
+}
+
+static void setup(void)
+{
+ if (sizeof(info1.arena) != sizeof(int))
+ tst_res(TFAIL, "The member of mallinfo struct is not int");
+
+ info1 = mallinfo();
+ tst_print_mallinfo("Start", &info1);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = test_mallinfo,
+ .cleanup = cleanup,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-04 12:12 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
2021-02-04 12:12 ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
2021-02-04 12:12 ` [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
@ 2021-02-04 12:12 ` Yang Xu
2021-02-04 13:51 ` Li Wang
2021-02-04 12:12 ` [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2021-02-04 13:29 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
4 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
To: ltp
According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).".
For allocations greater than or equal to 128K and that can't be satisfied from
the free list, the memory-allocation functions employ mmap(2) instead of increasing
the program break using sbrk(2).
In this case, we test 20k size to use sbrk and 128k size to use mmap.
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
v1->v2:
1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold
2.Use tst_print_malinfo api
runtest/syscalls | 1 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
.../kernel/syscalls/mallinfo/mallinfo02.c | 64 +++++++++++++++++++
3 files changed, 66 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 753340068..a8fa3f7bf 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -683,6 +683,7 @@ lstat02 lstat02
lstat02_64 lstat02_64
mallinfo01 mallinfo01
+mallinfo02 mallinfo02
mallopt01 mallopt01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index a7e32a637..678ac277e 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1 +1,2 @@
/mallinfo01
+/mallinfo02
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
new file mode 100644
index 000000000..d5bed45a8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test for malloc() using sbrk or mmap.
+ * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+void test_mallopt(void)
+{
+ struct mallinfo info;
+ int size;
+ char *buf;
+
+ buf = SAFE_MALLOC(20480);
+
+ info = mallinfo();
+ if (info.uordblks > 20480 && info.hblkhd == 0) {
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+ } else {
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");
+ tst_print_mallinfo("Test malloc(20480)", &info);
+ }
+ free(buf);
+
+ info = mallinfo();
+ size = MAX(info.fordblks, 131072);
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd > size && info.hblks > 0) {
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+ } else {
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
+ tst_print_mallinfo("Test malloc(1024*128)", &info);
+ }
+
+ free(buf);
+}
+
+static void setup(void)
+{
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = test_mallopt,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-04 12:12 ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-04 13:51 ` Li Wang
2021-02-05 7:51 ` Yang Xu
` (2 more replies)
0 siblings, 3 replies; 39+ messages in thread
From: Li Wang @ 2021-02-04 13:51 UTC (permalink / raw)
To: ltp
On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com> wrote:
> According mallinfo man-page, hblkhd member represents
> "The number of bytes in blocks currently allocated using mmap(2).".
> For allocations greater than or equal to 128K and that can't be satisfied
> from
> the free list, the memory-allocation functions employ mmap(2) instead of
> increasing
> the program break using sbrk(2).
>
> In this case, we test 20k size to use sbrk and 128k size to use mmap.
>
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
> v1->v2:
> 1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold
> 2.Use tst_print_malinfo api
> runtest/syscalls | 1 +
> testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
> .../kernel/syscalls/mallinfo/mallinfo02.c | 64 +++++++++++++++++++
> 3 files changed, 66 insertions(+)
> create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 753340068..a8fa3f7bf 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -683,6 +683,7 @@ lstat02 lstat02
> lstat02_64 lstat02_64
>
> mallinfo01 mallinfo01
> +mallinfo02 mallinfo02
>
> mallopt01 mallopt01
>
> diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore
> b/testcases/kernel/syscalls/mallinfo/.gitignore
> index a7e32a637..678ac277e 100644
> --- a/testcases/kernel/syscalls/mallinfo/.gitignore
> +++ b/testcases/kernel/syscalls/mallinfo/.gitignore
> @@ -1 +1,2 @@
> /mallinfo01
> +/mallinfo02
> diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> new file mode 100644
> index 000000000..d5bed45a8
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> @@ -0,0 +1,64 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +
> +/*\
> + * [DESCRIPTION]
> + *
> + * Basic mallinfo() test for malloc() using sbrk or mmap.
> + * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
> +\*/
> +
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "tst_mallinfo.h"
> +
> +#ifdef HAVE_MALLINFO
> +void test_mallopt(void)
>
what about renaming to test_mallinfo(void) ?
> +{
> + struct mallinfo info;
> + int size;
> + char *buf;
> +
> + buf = SAFE_MALLOC(20480);
> +
> + info = mallinfo();
> + if (info.uordblks > 20480 && info.hblkhd == 0) {
> + tst_res(TPASS, "malloc() uses sbrk when size < 128k");
> + } else {
> + tst_res(TFAIL, "malloc() use mmap when size < 128k");
> + tst_print_mallinfo("Test malloc(20480)", &info);
> + }
> + free(buf);
> +
> + info = mallinfo();
> + size = MAX(info.fordblks, 131072);
> +
> + buf = SAFE_MALLOC(size);
> + info = mallinfo();
> + if (info.hblkhd > size && info.hblks > 0) {
> + tst_res(TPASS, "malloc() uses mmap when size >= 128k");
> + } else {
> + tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
>
Why not 'TFAIL | TERRNO' ?
> + tst_print_mallinfo("Test malloc(1024*128)", &info);
> + }
> +
> + free(buf);
> +}
> +
> +static void setup(void)
> +{
> + if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
> + tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
>
Here as well.
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .test_all = test_mallopt,
> +};
> +
> +#else
> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
> +#endif
> --
> 2.23.0
>
>
>
>
--
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/4e0843f5/attachment-0001.htm>
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-04 13:51 ` Li Wang
@ 2021-02-05 7:51 ` Yang Xu
2021-02-05 8:15 ` Yang Xu
2021-02-05 9:00 ` Yang Xu
2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-05 7:51 UTC (permalink / raw)
To: ltp
Hi Li
>
>
> On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com
> <mailto:xuyang2018.jy@cn.fujitsu.com>> wrote:
>
> According mallinfo man-page, hblkhd member represents
> "The number of bytes in blocks currently allocated using mmap(2).".
> For allocations greater than or equal to 128K and that can't be
> satisfied from
> the free list, the memory-allocation functions employ mmap(2)
> instead of increasing
> the program break using sbrk(2).
>
> In this case, we test 20k size to use sbrk and 128k size to use mmap.
>
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com
> <mailto:xuyang2018.jy@cn.fujitsu.com>>
> ---
> v1->v2:
> 1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold
> 2.Use tst_print_malinfo api
> runtest/syscalls | 1 +
> testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
> .../kernel/syscalls/mallinfo/mallinfo02.c | 64 +++++++++++++++++++
> 3 files changed, 66 insertions(+)
> create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 753340068..a8fa3f7bf 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -683,6 +683,7 @@ lstat02 lstat02
> lstat02_64 lstat02_64
>
> mallinfo01 mallinfo01
> +mallinfo02 mallinfo02
>
> mallopt01 mallopt01
>
> diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore
> b/testcases/kernel/syscalls/mallinfo/.gitignore
> index a7e32a637..678ac277e 100644
> --- a/testcases/kernel/syscalls/mallinfo/.gitignore
> +++ b/testcases/kernel/syscalls/mallinfo/.gitignore
> @@ -1 +1,2 @@
> /mallinfo01
> +/mallinfo02
> diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> new file mode 100644
> index 000000000..d5bed45a8
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
> @@ -0,0 +1,64 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com
> <mailto:xuyang2018.jy@cn.fujitsu.com>>
> + */
> +
> +/*\
> + * [DESCRIPTION]
> + *
> + * Basic mallinfo() test for malloc() using sbrk or mmap.
> + * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
> +\*/
> +
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "tst_mallinfo.h"
> +
> +#ifdef HAVE_MALLINFO
> +void test_mallopt(void)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for typo.
>
> +{
> + struct mallinfo info;
> + int size;
> + char *buf;
> +
> + buf = SAFE_MALLOC(20480);
> +
> + info = mallinfo();
> + if (info.uordblks > 20480 && info.hblkhd == 0) {
> + tst_res(TPASS, "malloc() uses sbrk when size < 128k");
> + } else {
> + tst_res(TFAIL, "malloc() use mmap when size < 128k");
> + tst_print_mallinfo("Test malloc(20480)", &info);
> + }
> + free(buf);
> +
> + info = mallinfo();
> + size = MAX(info.fordblks, 131072);
> +
> + buf = SAFE_MALLOC(size);
> + info = mallinfo();
> + if (info.hblkhd > size && info.hblks > 0) {
> + tst_res(TPASS, "malloc() uses mmap when size >= 128k");
> + } else {
> + tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
>
>
> Why not 'TFAIL | TERRNO' ?
I don't see mallinfo() will fail and it should always succeed.
>
> + tst_print_mallinfo("Test malloc(1024*128)", &info);
> + }
> +
> + free(buf);
> +}
> +
> +static void setup(void)
> +{
> + if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
> + tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
>
>
> Here as well.
Look mallopt man-pages, it said "
RETURN VALUE
On success, mallopt() returns 1. On error, it returns 0.
ERRORS
On error, errno is not set
"
That is why I don't use TERRNO.
>
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .test_all = test_mallopt,
> +};
> +
> +#else
> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
> +#endif
> --
> 2.23.0
>
>
>
>
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-04 13:51 ` Li Wang
2021-02-05 7:51 ` Yang Xu
@ 2021-02-05 8:15 ` Yang Xu
2021-02-05 9:00 ` Yang Xu
2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-05 8:15 UTC (permalink / raw)
To: ltp
Hi Li
I have reply this email, but I don't see it on inbox or patchwork, so again
> +
> +#ifdef HAVE_MALLINFO
> +void test_mallopt(void)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for this typo.
>
> +{
> + struct mallinfo info;
> + int size;
> + char *buf;
> +
> + buf = SAFE_MALLOC(20480);
> +
> + info = mallinfo();
> + if (info.uordblks > 20480 && info.hblkhd == 0) {
> + tst_res(TPASS, "malloc() uses sbrk when size < 128k");
> + } else {
> + tst_res(TFAIL, "malloc() use mmap when size < 128k");
> + tst_print_mallinfo("Test malloc(20480)", &info);
> + }
> + free(buf);
> +
> + info = mallinfo();
> + size = MAX(info.fordblks, 131072);
> +
> + buf = SAFE_MALLOC(size);
> + info = mallinfo();
> + if (info.hblkhd > size && info.hblks > 0) {
> + tst_res(TPASS, "malloc() uses mmap when size >= 128k");
> + } else {
> + tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
>
>
> Why not 'TFAIL | TERRNO' ?
mallinfo should always succeed.
>
> + tst_print_mallinfo("Test malloc(1024*128)", &info);
> + }
> +
> + free(buf);
> +}
> +
> +static void setup(void)
> +{
> + if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
> + tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
>
>
> Here as well.
mallopt man-pages said even mallopt fails, return 0 and it doesn't set
errno.
>
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .test_all = test_mallopt,
> +};
> +
> +#else
> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
> +#endif
> --
> 2.23.0
>
>
>
>
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-04 13:51 ` Li Wang
2021-02-05 7:51 ` Yang Xu
2021-02-05 8:15 ` Yang Xu
@ 2021-02-05 9:00 ` Yang Xu
2021-02-05 9:20 ` Li Wang
2 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-05 9:00 UTC (permalink / raw)
To: ltp
Hi Li
I have reply this email, but I don't see it on inbox or patchwork, so again
> +#ifdef HAVE_MALLINFO
> +void test_mallopt(void)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for this typo.
>
> +{
> + struct mallinfo info;
> + int size;
> + char *buf;
> +
> + buf = SAFE_MALLOC(20480);
> +
> + info = mallinfo();
> + if (info.uordblks > 20480 && info.hblkhd == 0) {
> + tst_res(TPASS, "malloc() uses sbrk when size < 128k");
> + } else {
> + tst_res(TFAIL, "malloc() use mmap when size < 128k");
> + tst_print_mallinfo("Test malloc(20480)", &info);
> + }
> + free(buf);
> +
> + info = mallinfo();
> + size = MAX(info.fordblks, 131072);
> +
> + buf = SAFE_MALLOC(size);
> + info = mallinfo();
> + if (info.hblkhd > size && info.hblks > 0) {
> + tst_res(TPASS, "malloc() uses mmap when size >= 128k");
> + } else {
> + tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
>
>
> Why not 'TFAIL | TERRNO' ?
mallinfo should succeed always.
>
> + tst_print_mallinfo("Test malloc(1024*128)", &info);
> + }
> +
> + free(buf);
> +}
> +
> +static void setup(void)
> +{
> + if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
> + tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
>
>
> Here as well.
>
From mallopt man-page, even it fail, it doesn't set errno and only return 0
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .test_all = test_mallopt,
> +};
> +
> +#else
> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
> +#endif
> --
> 2.23.0
>
>
>
>
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size
2021-02-04 12:12 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
` (2 preceding siblings ...)
2021-02-04 12:12 ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-04 12:12 ` Yang Xu
2021-02-04 13:29 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
To: ltp
Since these members of mallinfo struct use int data type, it will overflow
when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2()
in the future.
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
v1->v2
1.use tst_print_mallinfo api
2.skip test if we can't malloc 2g space
runtest/syscalls | 1 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
.../kernel/syscalls/mallinfo/mallinfo03.c | 51 +++++++++++++++++++
3 files changed, 53 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c
diff --git a/runtest/syscalls b/runtest/syscalls
index a8fa3f7bf..c5cf78381 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -684,6 +684,7 @@ lstat02_64 lstat02_64
mallinfo01 mallinfo01
mallinfo02 mallinfo02
+mallinfo03 mallinfo03
mallopt01 mallopt01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@
/mallinfo01
/mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..36d9e5d85
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+
+void test_mallinfo(void)
+{
+ struct mallinfo info;
+ char *buf;
+ size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+ buf = malloc(size);
+ if (!buf) {
+ tst_res(TCONF, "Current system can not malloc 2G space, skip it");
+ return;
+ }
+ info = mallinfo();
+ if (info.hblkhd < 0) {
+ tst_print_mallinfo("Test malloc 2G", &info);
+ tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
+ } else {
+ /*We will never get here*/
+ tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
+ }
+
+ free(buf);
+}
+
+static struct tst_test test = {
+ .test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
2021-02-04 12:12 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
` (3 preceding siblings ...)
2021-02-04 12:12 ` [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
@ 2021-02-04 13:29 ` Li Wang
2021-02-05 7:44 ` Yang Xu
4 siblings, 1 reply; 39+ messages in thread
From: Li Wang @ 2021-02-04 13:29 UTC (permalink / raw)
To: ltp
Hi Xu,
Thanks for your work.
--- /dev/null
> +++ b/include/tst_mallinfo.h
>
Do we really need to export this function into the LTP library?
(I assumed no other tests will using this lib function anymore)
So I prefer to create a common header file as:
../syscalls/mallinfo/mallinfo_common.h,
and just use it locally.
If you're hoping mallopt01.c also benefits from it, only need to
build something like mallinfo_commo.o via Makefile and include
it in mallopt01.c by "../mallinfo/mallinfo_common.h".
--
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/b556dd12/attachment.htm>
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
2021-02-04 13:29 ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
@ 2021-02-05 7:44 ` Yang Xu
2021-02-05 8:00 ` Li Wang
0 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-05 7:44 UTC (permalink / raw)
To: ltp
Hi Li
> Hi Xu,
>
> Thanks for your work.
>
> --- /dev/null
> +++ b/include/tst_mallinfo.h
>
>
> Do we really need to export this function into the LTP library?
> (I assumed no other tests will using this lib function anymore)
Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case
can use this header.
>
> So I prefer to create a common header file as:
> ../syscalls/mallinfo/mallinfo_common.h,
> and just use it locally.
>
> If you're hoping mallopt01.c also benefits from it, only need to
> build something like mallinfo_commo.o via Makefile and include
> it in mallopt01.c by "../mallinfo/mallinfo_common.h".
Just want to avoid random makefile. I am not sure which way is better.
Let's listen advise from other maintainers.
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
2021-02-05 7:44 ` Yang Xu
@ 2021-02-05 8:00 ` Li Wang
2021-02-05 11:21 ` Petr Vorel
2021-02-08 15:30 ` Cyril Hrubis
0 siblings, 2 replies; 39+ messages in thread
From: Li Wang @ 2021-02-05 8:00 UTC (permalink / raw)
To: ltp
On Fri, Feb 5, 2021 at 3:44 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com> wrote:
> Hi Li
> > Hi Xu,
> >
> > Thanks for your work.
> >
> > --- /dev/null
> > +++ b/include/tst_mallinfo.h
> >
> >
> > Do we really need to export this function into the LTP library?
> > (I assumed no other tests will using this lib function anymore)
> Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case
> can use this header.
>
Or put it into the tst_memutils.h, I guess this header file is a
general-purpose for providing kinds of memory helpers.
>
> >
> > So I prefer to create a common header file as:
> > ../syscalls/mallinfo/mallinfo_common.h,
> > and just use it locally.
> >
> > If you're hoping mallopt01.c also benefits from it, only need to
> > build something like mallinfo_commo.o via Makefile and include
> > it in mallopt01.c by "../mallinfo/mallinfo_common.h".
> Just want to avoid random makefile. I am not sure which way is better.
> Let's listen advise from other maintainers.
>
Sure, my pleasure to hear more voice~
--
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210205/c6b2fe84/attachment.htm>
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
2021-02-05 8:00 ` Li Wang
@ 2021-02-05 11:21 ` Petr Vorel
2021-02-08 15:30 ` Cyril Hrubis
1 sibling, 0 replies; 39+ messages in thread
From: Petr Vorel @ 2021-02-05 11:21 UTC (permalink / raw)
To: ltp
Hi Xu, Li,
> > > --- /dev/null
> > > +++ b/include/tst_mallinfo.h
> > > Do we really need to export this function into the LTP library?
> > > (I assumed no other tests will using this lib function anymore)
> > Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case
> > can use this header.
> Or put it into the tst_memutils.h, I guess this header file is a
> general-purpose for providing kinds of memory helpers.
Either ways are ok for me. tst_memutils.h is a bit cleaner approach,
but given the fact that not many tests will use it having it in
into testcases/kernel/syscalls/mallinfo/ and source it with relative path in
mallopt01.c is also ok for me.
Kind regards,
Petr
> > > So I prefer to create a common header file as:
> > > ../syscalls/mallinfo/mallinfo_common.h,
> > > and just use it locally.
> > > If you're hoping mallopt01.c also benefits from it, only need to
> > > build something like mallinfo_commo.o via Makefile and include
> > > it in mallopt01.c by "../mallinfo/mallinfo_common.h".
> > Just want to avoid random makefile. I am not sure which way is better.
> > Let's listen advise from other maintainers.
> Sure, my pleasure to hear more voice~
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
2021-02-05 8:00 ` Li Wang
2021-02-05 11:21 ` Petr Vorel
@ 2021-02-08 15:30 ` Cyril Hrubis
2021-02-18 5:52 ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
1 sibling, 1 reply; 39+ messages in thread
From: Cyril Hrubis @ 2021-02-08 15:30 UTC (permalink / raw)
To: ltp
Hi!
> > Just want to avoid random makefile. I am not sure which way is better.
> > Let's listen advise from other maintainers.
Given that its only one single function I would just put it into a
header as a static inline function.
Also mallopt is glibc specific so I guess that it makes sense to keep it
in a separate header.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test for mallinfo
2021-02-08 15:30 ` Cyril Hrubis
@ 2021-02-18 5:52 ` Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
` (2 more replies)
0 siblings, 3 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18 5:52 UTC (permalink / raw)
To: ltp
Referring to glibc test tst-mallinfo2.c[1], add a test to test mallinfo.
Also check mallinfo in autotools.
[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
configure.ac | 1 +
runtest/syscalls | 2 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
testcases/kernel/syscalls/mallinfo/Makefile | 8 ++
.../kernel/syscalls/mallinfo/mallinfo01.c | 80 +++++++++++++++++++
.../syscalls/mallinfo/mallinfo_common.h | 31 +++++++
6 files changed, 123 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/.gitignore
create mode 100644 testcases/kernel/syscalls/mallinfo/Makefile
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo01.c
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo_common.h
diff --git a/configure.ac b/configure.ac
index d4bef5e45..2e1552cf0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -97,6 +97,7 @@ AC_CHECK_FUNCS_ONCE([ \
io_uring_register \
io_uring_enter \
kcmp \
+ mallinfo \
mallopt \
mkdirat \
mknodat \
diff --git a/runtest/syscalls b/runtest/syscalls
index ae47a6d5e..b3f4660f4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -685,6 +685,8 @@ lstat01_64 lstat01_64
lstat02 lstat02
lstat02_64 lstat02_64
+mallinfo01 mallinfo01
+
mallopt01 mallopt01
mbind01 mbind01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
new file mode 100644
index 000000000..a7e32a637
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -0,0 +1 @@
+/mallinfo01
diff --git a/testcases/kernel/syscalls/mallinfo/Makefile b/testcases/kernel/syscalls/mallinfo/Makefile
new file mode 100644
index 000000000..044619fb8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines Corp., 2001
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
new file mode 100644
index 000000000..ba5366896
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test mallinfo2 test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+#define M_NUM 20
+static struct mallinfo info1;
+static void *buf[M_NUM + 1];
+
+static void cleanup(void)
+{
+ int i;
+
+ for (i = M_NUM; i > 0; i--) {
+ if (buf[i]) {
+ free(buf[i]);
+ buf[i] = NULL;
+ }
+ }
+}
+
+void test_mallinfo(void)
+{
+ int i;
+ int total = 0;
+ struct mallinfo info2;
+
+ for (i = 1; i <= M_NUM; i++) {
+ buf[i] = SAFE_MALLOC(160 * i);
+ total += 160 * i;
+ }
+ info2 = mallinfo();
+ print_mallinfo("Test uordblks", &info2);
+ if (info2.uordblks >= info1.uordblks + total)
+ tst_res(TPASS, "mallinfo() uordblks passed");
+ else
+ tst_res(TFAIL, "mallinfo() uordblks failed");
+
+ //Create another free chunk
+ free(buf[M_NUM/2]);
+ buf[M_NUM/2] = NULL;
+ info2 = mallinfo();
+ print_mallinfo("Test ordblks", &info2);
+ if (info2.ordblks == info1.ordblks + 1)
+ tst_res(TPASS, "mallinfo() ordblks passed");
+ else
+ tst_res(TFAIL, "mallinfo() ordblks failed");
+
+ cleanup();
+}
+
+static void setup(void)
+{
+ if (sizeof(info1.arena) != sizeof(int))
+ tst_res(TFAIL, "The member of mallinfo struct is not int");
+
+ info1 = mallinfo();
+ print_mallinfo("Start", &info1);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = test_mallinfo,
+ .cleanup = cleanup,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo_common.h b/testcases/kernel/syscalls/mallinfo/mallinfo_common.h
new file mode 100644
index 000000000..a00cc7a64
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo_common.h
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#ifndef MALLINFO_COMMON_H
+#define MALLINFO_COMMON_H
+
+#include <malloc.h>
+#include "tst_test.h"
+#include "config.h"
+
+#ifdef HAVE_MALLINFO
+static inline void print_mallinfo(const char *msg, struct mallinfo *m)
+{
+ tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+ P(arena);
+ P(ordblks);
+ P(smblks);
+ P(hblks);
+ P(hblkhd);
+ P(usmblks);
+ P(fsmblks);
+ P(uordblks);
+ P(fordblks);
+ P(keepcost);
+}
+#endif
+
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api
2021-02-18 5:52 ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
@ 2021-02-18 5:52 ` Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18 5:52 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
1 file changed, 3 insertions(+), 19 deletions(-)
diff --git a/testcases/kernel/syscalls/mallopt/mallopt01.c b/testcases/kernel/syscalls/mallopt/mallopt01.c
index f799aaf9d..e270b0bda 100644
--- a/testcases/kernel/syscalls/mallopt/mallopt01.c
+++ b/testcases/kernel/syscalls/mallopt/mallopt01.c
@@ -12,9 +12,8 @@
* Basic mallinfo() and mallopt() testing.
\*/
-#include <malloc.h>
-#include "tst_test.h"
+#include "../mallinfo/mallinfo_common.h"
#include "tst_safe_macros.h"
#ifdef HAVE_MALLOPT
@@ -23,21 +22,6 @@
struct mallinfo info;
-void print_mallinfo(void)
-{
- tst_res(TINFO, "mallinfo structure:");
- tst_res(TINFO, "mallinfo.arena = %d", info.arena);
- tst_res(TINFO, "mallinfo.ordblks = %d", info.ordblks);
- tst_res(TINFO, "mallinfo.smblks = %d", info.smblks);
- tst_res(TINFO, "mallinfo.hblkhd = %d", info.hblkhd);
- tst_res(TINFO, "mallinfo.hblks = %d", info.hblks);
- tst_res(TINFO, "mallinfo.usmblks = %d", info.usmblks);
- tst_res(TINFO, "mallinfo.fsmblks = %d", info.fsmblks);
- tst_res(TINFO, "mallinfo.uordblks = %d", info.uordblks);
- tst_res(TINFO, "mallinfo.fordblks = %d", info.fordblks);
- tst_res(TINFO, "mallinfo.keepcost = %d", info.keepcost);
-}
-
void test_mallopt(void)
{
char *buf;
@@ -46,11 +30,11 @@ void test_mallopt(void)
info = mallinfo();
if (info.uordblks < 20480) {
- print_mallinfo();
+ print_mallinfo("Test uordblks", &info);
tst_res(TFAIL, "mallinfo() failed: uordblks < 20K");
}
if (info.smblks != 0) {
- print_mallinfo();
+ print_mallinfo("Test smblks", &info);
tst_res(TFAIL, "mallinfo() failed: smblks != 0");
}
if (info.uordblks >= 20480 && info.smblks == 0)
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-18 5:52 ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
@ 2021-02-18 5:52 ` Yang Xu
2021-02-22 6:32 ` Li Wang
2021-02-18 5:52 ` [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-18 5:52 UTC (permalink / raw)
To: ltp
According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).".
For allocations greater than or equal to 128K and that can't be satisfied from
the free list, the memory-allocation functions employ mmap(2) instead of increasing
the program break using sbrk(2).
In this case, we test 20k size to use sbrk and 128k size to use mmap.
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
.../kernel/syscalls/mallinfo/mallinfo02.c | 63 +++++++++++++++++++
3 files changed, 65 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
diff --git a/runtest/syscalls b/runtest/syscalls
index b3f4660f4..312544659 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -686,6 +686,7 @@ lstat02 lstat02
lstat02_64 lstat02_64
mallinfo01 mallinfo01
+mallinfo02 mallinfo02
mallopt01 mallopt01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index a7e32a637..678ac277e 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1 +1,2 @@
/mallinfo01
+/mallinfo02
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
new file mode 100644
index 000000000..945d3227c
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test for malloc() using sbrk or mmap.
+ * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
+\*/
+
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+void test_mallinfo(void)
+{
+ struct mallinfo info;
+ int size;
+ char *buf;
+
+ buf = SAFE_MALLOC(20480);
+
+ info = mallinfo();
+ if (info.uordblks > 20480 && info.hblkhd == 0) {
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+ } else {
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");
+ print_mallinfo("Test malloc(20480)", &info);
+ }
+ free(buf);
+
+ info = mallinfo();
+ size = MAX(info.fordblks, 131072);
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd > size && info.hblks > 0) {
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+ } else {
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
+ print_mallinfo("Test malloc(1024*128)", &info);
+ }
+
+ free(buf);
+}
+
+static void setup(void)
+{
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-18 5:52 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-22 6:32 ` Li Wang
2021-02-22 6:40 ` Yang Xu
2021-02-23 1:45 ` Yang Xu
0 siblings, 2 replies; 39+ messages in thread
From: Li Wang @ 2021-02-22 6:32 UTC (permalink / raw)
To: ltp
Hi Xu,
For patch 1/4 ~ 3/4, looks good to me.
Reviewed-by: Li Wang <liwang@redhat.com>
For 4/4, I slightly think it does not make much sense to test, because
it just verifies the info.hblkhd integer overflow at unsuggested usage.
Anyway, that's only my thoughts, maybe a one-sided view:).
--
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210222/182c9e58/attachment.htm>
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-22 6:32 ` Li Wang
@ 2021-02-22 6:40 ` Yang Xu
2021-02-23 1:45 ` Yang Xu
1 sibling, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-22 6:40 UTC (permalink / raw)
To: ltp
Hi Li
> Hi Xu,
>
> For patch 1/4 ~ 3/4, looks good to me.
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>
>
> For 4/4, I slightly think it does not make much sense to test, because
> it just verifies the info.hblkhd integer overflow at unsuggested usage.
> Anyway, that's only my thoughts, maybe a one-sided view:).
I guess we can just add a valid test for mallinfo2 to test it still can
get the correct info when setting 2G size instead of this case.
ps: I need to find a new enough system to have supported mallinfo2.
Best Regards
Yang Xu
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
2021-02-22 6:32 ` Li Wang
2021-02-22 6:40 ` Yang Xu
@ 2021-02-23 1:45 ` Yang Xu
1 sibling, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-23 1:45 UTC (permalink / raw)
To: ltp
Hi Li
> Hi Xu,
>
> For patch 1/4 ~ 3/4, looks good to me.
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>
Thanks for you review. I have merged this patchset and drop the last one.
>
> For 4/4, I slightly think it does not make much sense to test, because
> it just verifies the info.hblkhd integer overflow at unsuggested usage.
> Anyway, that's only my thoughts, maybe a one-sided view:).
>
> --
> Regards,
> Li Wang
^ permalink raw reply [flat|nested] 39+ messages in thread
* [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size
2021-02-18 5:52 ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
2021-02-18 5:52 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-18 5:52 ` Yang Xu
2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18 5:52 UTC (permalink / raw)
To: ltp
Since these members of mallinfo struct use int type, it will overflow
when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2()
in the future.
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
.../kernel/syscalls/mallinfo/mallinfo03.c | 50 +++++++++++++++++++
3 files changed, 52 insertions(+)
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 312544659..7d6d42072 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -687,6 +687,7 @@ lstat02_64 lstat02_64
mallinfo01 mallinfo01
mallinfo02 mallinfo02
+mallinfo03 mallinfo03
mallopt01 mallopt01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@
/mallinfo01
/mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..d3c6dd52d
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+
+void test_mallinfo(void)
+{
+ struct mallinfo info;
+ char *buf;
+ size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+ buf = malloc(size);
+ if (!buf) {
+ tst_res(TCONF, "Current system can not malloc 2G space, skip it");
+ return;
+ }
+ info = mallinfo();
+ if (info.hblkhd < 0) {
+ print_mallinfo("Test malloc 2G", &info);
+ tst_res(TPASS, "The member of struct mallinfo overflow, we should use mallinfo2");
+ } else {
+ /*We will never get here*/
+ tst_res(TFAIL, "The member of struct mallinfo doesn't overflow");
+ }
+
+ free(buf);
+}
+
+static struct tst_test test = {
+ .test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--
2.23.0
^ permalink raw reply related [flat|nested] 39+ messages in thread