xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap20.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2023 Paulson Raja L <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Test mmap(2) with MAP_SHARED_VALIDATE flag.
10  *
11  * Test expected EOPNOTSUPP errno when testing mmap(2) with MAP_SHARED_VALIDATE
12  * flag and invalid flag.
13  */
14 
15 #include <errno.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include "tst_test.h"
19 #include "lapi/mmap.h"
20 
21 #define TEST_FILE "file_to_mmap"
22 #define TEST_FILE_SIZE 1024
23 #define INVALID_FLAG (1 << 10)
24 
25 static int fd = -1;
26 static void *addr;
27 
setup(void)28 static void setup(void)
29 {
30 	fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0600);
31 
32 	if (tst_fill_file(TEST_FILE, 'a', TEST_FILE_SIZE, 1))
33 		tst_brk(TBROK, "Could not fill the testfile");
34 }
35 
cleanup(void)36 static void cleanup(void)
37 {
38 	if (fd > -1)
39 		SAFE_CLOSE(fd);
40 
41 	if (addr && addr != MAP_FAILED)
42 		SAFE_MUNMAP(addr, TEST_FILE_SIZE);
43 }
44 
test_mmap(void)45 static void test_mmap(void)
46 {
47 	addr = mmap(NULL, TEST_FILE_SIZE, PROT_READ | PROT_WRITE,
48 			      INVALID_FLAG | MAP_SHARED_VALIDATE, fd, 0);
49 
50 	if (addr != MAP_FAILED)
51 		tst_res(TFAIL | TERRNO, "mmap() is successful, but it should have failed");
52 	else if (errno == EOPNOTSUPP)
53 		tst_res(TPASS, "mmap() failed with errno set to EOPNOTSUPP");
54 	else
55 		tst_res(TFAIL | TERRNO, "mmap() failed with unexpected error");
56 }
57 
58 static struct tst_test test = {
59 	.min_kver = "4.15",
60 	.setup = setup,
61 	.cleanup = cleanup,
62 	.test_all = test_mmap,
63 	.needs_tmpdir = 1,
64 };
65