xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap15.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2004
4  * Written by Robbie Williamson
5  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6  * Copyright (c) Linux Test Project, 2014-2023
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Verify that, a normal page cannot be mapped into a high memory region,
13  * and mmap() call fails with either ENOMEM or EINVAL errno.
14  */
15 
16 #include "tst_test.h"
17 
18 #ifdef __ia64__
19 # define HIGH_ADDR ((void *)(0xa000000000000000UL))
20 #else
21 # define HIGH_ADDR ((void *)(-page_size))
22 #endif
23 
24 #define TEMPFILE "mmapfile"
25 
26 static long page_size;
27 static int fd;
28 
run(void)29 static void run(void)
30 {
31 	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
32 
33 	TESTPTR(mmap(HIGH_ADDR, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0));
34 
35 	if (TST_RET_PTR != MAP_FAILED) {
36 		tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
37 		SAFE_MUNMAP(TST_RET_PTR, page_size);
38 		return;
39 	}
40 
41 	if (TST_ERR == ENOMEM || TST_ERR == EINVAL)
42 		tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
43 	else
44 		tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
45 
46 	SAFE_CLOSE(fd);
47 }
48 
setup(void)49 static void setup(void)
50 {
51 	page_size = getpagesize();
52 }
53 
cleanup(void)54 static void cleanup(void)
55 {
56 	if (fd > 0)
57 		SAFE_CLOSE(fd);
58 }
59 
60 static struct tst_test test = {
61 	.setup = setup,
62 	.cleanup = cleanup,
63 	.test_all = run,
64 	.needs_tmpdir = 1
65 };
66