Skip to content

Commit

Permalink
syscalls/mseal02.c: Add new testcase
Browse files Browse the repository at this point in the history
New testcase is added to check various errnos for mseal(2).

Signed-off-by: Xiao Yang <[email protected]>
Reviewed-by: Cyril Hrubis <[email protected]>
  • Loading branch information
yangx-jy authored and metan-ucw committed Sep 13, 2024
1 parent c08ca17 commit bb9f630
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtest/syscalls
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ mremap05 mremap05
mremap06 mremap06

mseal01 mseal01
mseal02 mseal02

msgctl01 msgctl01
msgctl02 msgctl02
Expand Down
1 change: 1 addition & 0 deletions testcases/kernel/syscalls/mseal/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mseal01
mseal02
75 changes: 75 additions & 0 deletions testcases/kernel/syscalls/mseal/mseal02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Author: Xiao Yang <[email protected]>
*/

/*\
* [Description]
*
* Check various errnos for mseal(2).
*
* - mseal() fails with EINVAL if flags is invalid.
* - mseal() fails with EINVAL if the start address is not page aligned.
* - mseal() fails with EINVAL if address range overflows.
* - mseal() fails with ENOMEM if the start address is not allocated.
* - mseal() fails with ENOMEM if the end address is not allocated.
* - mseal() fails with ENOMEM if there is a gap (unallocated memory) between start and end address.
*
* TODO: support both raw syscall and libc wrapper via .test_variants.
*/

#define _GNU_SOURCE

#include "tst_test.h"
#include "lapi/syscalls.h"

static void *start_addr, *unaligned_start_addr, *unallocated_start_addr, *unallocated_end_addr;
static size_t page_size, twopages_size, fourpages_size, overflow_size;

static struct tcase {
void **addr;
size_t *len;
unsigned long flags;
int exp_err;
} tcases[] = {
{&start_addr, &page_size, ULONG_MAX, EINVAL},
{&unaligned_start_addr, &page_size, 0, EINVAL},
{&start_addr, &overflow_size, 0, EINVAL},
{&unallocated_start_addr, &twopages_size, 0, ENOMEM},
{&unallocated_end_addr, &twopages_size, 0, ENOMEM},
{&start_addr, &fourpages_size, 0, ENOMEM},
};

static void run(unsigned int n)
{
struct tcase *tc = &tcases[n];

TST_EXP_FAIL(tst_syscall(__NR_mseal, *tc->addr, *tc->len, tc->flags), tc->exp_err,
"mseal(%p, %lu, %lu)", *tc->addr, *tc->len, tc->flags);
}

static void setup(void)
{
page_size = getpagesize();
twopages_size = page_size * 2;
fourpages_size = page_size * 4;
overflow_size = ULONG_MAX - page_size + 2;
start_addr = SAFE_MMAP(NULL, fourpages_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
unaligned_start_addr = start_addr + 1;
SAFE_MUNMAP(start_addr + twopages_size, page_size);
unallocated_start_addr = start_addr + twopages_size;
unallocated_end_addr = start_addr + page_size;
}

static void cleanup(void)
{
SAFE_MUNMAP(start_addr, fourpages_size);
}

static struct tst_test test = {
.test = run,
.tcnt = ARRAY_SIZE(tcases),
.setup = setup,
.cleanup = cleanup,
};

0 comments on commit bb9f630

Please sign in to comment.