From patchwork Thu Feb 18 05:52:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1441530 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dh3lr3fs3z9sCD for ; Thu, 18 Feb 2021 16:53:12 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C86843C65FB for ; Thu, 18 Feb 2021 06:53:09 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 969A03C58E2 for ; Thu, 18 Feb 2021 06:52:51 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-2.smtp.seeweb.it (Postfix) with ESMTP id 673666012DB for ; Thu, 18 Feb 2021 06:52:45 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.81,186,1610380800"; d="scan'208";a="104595983" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 18 Feb 2021 13:52:41 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id AC1DA4CE6F85 for ; Thu, 18 Feb 2021 13:52:36 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 13:52:35 +0800 From: Yang Xu To: Date: Thu, 18 Feb 2021 13:52:49 +0800 Message-ID: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.205) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: AC1DA4CE6F85.AF57B X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.4 X-Virus-Scanned: clamav-milter 0.102.4 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test for mallinfo X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "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 --- 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 + */ + +/*\ + * [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 + */ +#ifndef MALLINFO_COMMON_H +#define MALLINFO_COMMON_H + +#include +#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 From patchwork Thu Feb 18 05:52:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1441528 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dh3ll2KYqz9sCD for ; Thu, 18 Feb 2021 16:52:57 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 0C8F43C54E7 for ; Thu, 18 Feb 2021 06:52:52 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [217.194.8.3]) by picard.linux.it (Postfix) with ESMTP id 913963C54E7 for ; Thu, 18 Feb 2021 06:52:49 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-3.smtp.seeweb.it (Postfix) with ESMTP id A2AB01A010FF for ; Thu, 18 Feb 2021 06:52:48 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.81,186,1610380800"; d="scan'208";a="104595986" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 18 Feb 2021 13:52:47 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id E4ACC4CE6F9E for ; Thu, 18 Feb 2021 13:52:40 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 13:52:41 +0800 From: Yang Xu To: Date: Thu, 18 Feb 2021 13:52:50 +0800 Message-ID: <1613627572-5213-2-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> References: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.205) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: E4ACC4CE6F9E.A179E X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.4 X-Virus-Scanned: clamav-milter 0.102.4 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Yang Xu --- 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 -#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) From patchwork Thu Feb 18 05:52:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1441529 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dh3lr02B0z9sVR for ; Thu, 18 Feb 2021 16:53:11 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 845AE3C65F1 for ; Thu, 18 Feb 2021 06:53:01 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [217.194.8.3]) by picard.linux.it (Postfix) with ESMTP id B234A3C54E7 for ; Thu, 18 Feb 2021 06:52:50 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-3.smtp.seeweb.it (Postfix) with ESMTP id E50A01A010F7 for ; Thu, 18 Feb 2021 06:52:49 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.81,186,1610380800"; d="scan'208";a="104595985" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 18 Feb 2021 13:52:47 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id 5FA104CE6F9C for ; Thu, 18 Feb 2021 13:52:45 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 13:52:45 +0800 From: Yang Xu To: Date: Thu, 18 Feb 2021 13:52:51 +0800 Message-ID: <1613627572-5213-3-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> References: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.205) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: 5FA104CE6F9C.AE3EF X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.4 X-Virus-Scanned: clamav-milter 0.102.4 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "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 Reviewed-by: Li Wang --- 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 + */ + +/*\ + * [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 From patchwork Thu Feb 18 05:52:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1441531 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dh3m064vBz9sCD for ; Thu, 18 Feb 2021 16:53:20 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 03C7F3C54E7 for ; Thu, 18 Feb 2021 06:53:18 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [217.194.8.5]) by picard.linux.it (Postfix) with ESMTP id DC4A63C6610 for ; Thu, 18 Feb 2021 06:53:01 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-5.smtp.seeweb.it (Postfix) with ESMTP id 0BEEC60005A for ; Thu, 18 Feb 2021 06:53:00 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.81,186,1610380800"; d="scan'208";a="104595993" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 18 Feb 2021 13:52:59 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id 53B684CE6F85 for ; Thu, 18 Feb 2021 13:52:57 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 13:52:47 +0800 From: Yang Xu To: Date: Thu, 18 Feb 2021 13:52:52 +0800 Message-ID: <1613627572-5213-4-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> References: <1613627572-5213-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.205) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: 53B684CE6F85.AE7A8 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.4 X-Virus-Scanned: clamav-milter 0.102.4 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "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 --- 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 + */ + +/*\ + * [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