[LTP] [PATCH v2 3/3] syscalls: munmap03: Convert to new API
Andrea Cervesato
andrea.cervesato@suse.com
Mon Jul 7 10:25:23 CEST 2025
Hi!
On 7/4/25 5:40 PM, Ricardo B. Marlière via ltp wrote:
> From: Ricardo B. Marlière <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
> ---
> testcases/kernel/syscalls/munmap/munmap03.c | 172 ++++++++--------------------
> 1 file changed, 47 insertions(+), 125 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/munmap/munmap03.c b/testcases/kernel/syscalls/munmap/munmap03.c
> index 60bcb93b0e359b579294d6745e1e729340c0d92e..200d473bd5e5b46351b4f63cf17dd237ea1c8c68 100644
> --- a/testcases/kernel/syscalls/munmap/munmap03.c
> +++ b/testcases/kernel/syscalls/munmap/munmap03.c
> @@ -1,148 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * Copyright (c) International Business Machines Corp., 2001
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
> - * the GNU General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + * Copyright (c) International Business Machines Corp., 2001
> + * 07/2001 Ported by Wayne Boyer
> + * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
> */
>
> -/*
> - * Test Description:
> - * Verify that,
> - * 1. munmap() fails with -1 return value and sets errno to EINVAL
> - * if addresses in the range [addr,addr+len) are outside the valid
Mixing parentesys?
> - * range for the address space of a process.
> - * 2. munmap() fails with -1 return value and sets errno to EINVAL
> - * if the len argument is 0.
> - * 3. munmap() fails with -1 return value and sets errno to EINVAL
> - * if the addr argument is not a multiple of the page size as
> - * returned by sysconf().
> +/*\
> + * Verify that, munmap() fails with errno:
> *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> + * - EINVAL, if addresses in the range [addr,addr+len) are outside the valid
> + * range for the address space of a process.
> + * - EINVAL, if the len argument is 0.
> + * - EINVAL, if the addr argument is not a multiple of the page size as
> + * returned by sysconf().
> */
>
> -#include <errno.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -#include <sys/resource.h>
> -#include <sys/stat.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -char *TCID = "munmap03";
> +#include "tst_test.h"
>
> static size_t page_sz;
> -static char *global_addr;
> -static size_t global_maplen;
> -
> -static void setup(void);
> -static void cleanup(void);
> -
> -static void test_einval1(void);
> -static void test_einval2(void);
> -static void test_einval3(void);
> -static void (*testfunc[])(void) = { test_einval1, test_einval2, test_einval3 };
> -int TST_TOTAL = ARRAY_SIZE(testfunc);
> -
> -int main(int ac, char **av)
> +static char *map_addr;
> +static char *map_addr_out;
> +static size_t map_len;
> +static size_t map_len_zero;
> +static int mapped;
mapped is never used. We should use map_addr instead. If it's not
NULL/zero, we try to munmap() in cleanup.
> +
> +static struct tcase {
> + int exp_errno;
> + char **addr;
> + size_t *len;
> +
Space to remove here.
> +} tcases[] = {
> + { EINVAL, &map_addr_out, &map_len },
> + { EINVAL, &map_addr, &map_len_zero },
> + { EINVAL, &map_addr + 1, &map_len },
> +};
> +
> +static void run(unsigned int i)
> {
> - int i, lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> + struct tcase *tc = &tcases[i];
>
> - for (i = 0; i < TST_TOTAL; i++)
> - (*testfunc[i])();
> - }
> -
> - cleanup();
> - tst_exit();
> + TST_EXP_FAIL(munmap(tc->addr, *tc->len), tc->exp_errno);
> }
>
> static void setup(void)
> -{
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - page_sz = (size_t)sysconf(_SC_PAGESIZE);
> -
> - global_maplen = page_sz * 2;
> - global_addr = SAFE_MMAP(cleanup, NULL, global_maplen, PROT_READ |
> - PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> -}
> -
> -static void check_and_print(int expected_errno)
> -{
> - if (TEST_RETURN == -1) {
> - if (TEST_ERRNO == expected_errno) {
> - tst_resm(TPASS | TTERRNO, "failed as expected");
> - } else {
> - tst_resm(TFAIL | TTERRNO,
> - "failed unexpectedly; expected - %d : %s",
> - expected_errno, strerror(expected_errno));
> - }
> - } else {
> - tst_resm(TFAIL, "munmap succeeded unexpectedly");
> - }
> -}
> -
> -static void test_einval1(void)
> {
> struct rlimit brkval;
> - char *addr;
> - size_t map_len;
> -
> - SAFE_GETRLIMIT(cleanup, RLIMIT_DATA, &brkval);
>
> - addr = (char *)brkval.rlim_max;
> + page_sz = SAFE_SYSCONF(_SC_PAGESIZE);
> map_len = page_sz * 2;
> + map_addr = SAFE_MMAP(NULL, map_len, PROT_READ | PROT_WRITE,
> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> + mapped = 1;
>
> - TEST(munmap(addr, map_len));
> -
> - check_and_print(EINVAL);
> -}
> -
> -static void test_einval2(void)
> -{
> - char *addr = global_addr;
> - size_t map_len = 0;
> -
> - TEST(munmap(addr, map_len));
> -
> - check_and_print(EINVAL);
> -}
> -
> -static void test_einval3(void)
> -{
> - char *addr = (char *)(global_addr + 1);
> - size_t map_len = page_sz;
> -
> - TEST(munmap(addr, map_len));
> -
> - check_and_print(EINVAL);
> + SAFE_GETRLIMIT(RLIMIT_DATA, &brkval);
> + map_addr_out = (char *)brkval.rlim_max;
> }
>
> static void cleanup(void)
> {
> - if (munmap(global_addr, global_maplen) == -1)
> - tst_resm(TWARN | TERRNO, "munmap failed");
> + if (mapped)
> + SAFE_MUNMAP(map_addr, map_len);
> }
> +
> +static struct tst_test test = {
> + .test = run,
> + .tcnt = ARRAY_SIZE(tcases),
> + .setup = setup,
> + .cleanup = cleanup,
> +};
>
- Andrea
More information about the ltp
mailing list