1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Description:
22f08c3bdfSopenharmony_ci *  Verify that,
23f08c3bdfSopenharmony_ci *   1. munmap() fails with -1 return value and sets errno to EINVAL
24f08c3bdfSopenharmony_ci *      if addresses in the range [addr,addr+len) are outside the valid
25f08c3bdfSopenharmony_ci *	range for the address space of a process.
26f08c3bdfSopenharmony_ci *   2. munmap() fails with -1 return value and sets errno to EINVAL
27f08c3bdfSopenharmony_ci *      if the len argument is 0.
28f08c3bdfSopenharmony_ci *   3. munmap() fails with -1 return value and sets errno to EINVAL
29f08c3bdfSopenharmony_ci *      if the addr argument is not a multiple of the page size as
30f08c3bdfSopenharmony_ci *      returned by sysconf().
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci * HISTORY
33f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
34f08c3bdfSopenharmony_ci */
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#include <errno.h>
37f08c3bdfSopenharmony_ci#include <unistd.h>
38f08c3bdfSopenharmony_ci#include <fcntl.h>
39f08c3bdfSopenharmony_ci#include <sys/mman.h>
40f08c3bdfSopenharmony_ci#include <sys/resource.h>
41f08c3bdfSopenharmony_ci#include <sys/stat.h>
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci#include "test.h"
44f08c3bdfSopenharmony_ci#include "safe_macros.h"
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cichar *TCID = "munmap03";
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic size_t page_sz;
49f08c3bdfSopenharmony_cistatic char *global_addr;
50f08c3bdfSopenharmony_cistatic size_t global_maplen;
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void setup(void);
53f08c3bdfSopenharmony_cistatic void cleanup(void);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void test_einval1(void);
56f08c3bdfSopenharmony_cistatic void test_einval2(void);
57f08c3bdfSopenharmony_cistatic void test_einval3(void);
58f08c3bdfSopenharmony_cistatic void (*testfunc[])(void) = { test_einval1, test_einval2, test_einval3 };
59f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(testfunc);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ciint main(int ac, char **av)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	int i, lc;
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	setup();
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
70f08c3bdfSopenharmony_ci		tst_count = 0;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
73f08c3bdfSopenharmony_ci			(*testfunc[i])();
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	cleanup();
77f08c3bdfSopenharmony_ci	tst_exit();
78f08c3bdfSopenharmony_ci}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_cistatic void setup(void)
81f08c3bdfSopenharmony_ci{
82f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	TEST_PAUSE;
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	page_sz = (size_t)sysconf(_SC_PAGESIZE);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	global_maplen = page_sz * 2;
89f08c3bdfSopenharmony_ci	global_addr = SAFE_MMAP(cleanup, NULL, global_maplen, PROT_READ |
90f08c3bdfSopenharmony_ci				PROT_WRITE, MAP_PRIVATE_EXCEPT_UCLINUX |
91f08c3bdfSopenharmony_ci				MAP_ANONYMOUS, -1, 0);
92f08c3bdfSopenharmony_ci}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_cistatic void check_and_print(int expected_errno)
95f08c3bdfSopenharmony_ci{
96f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1) {
97f08c3bdfSopenharmony_ci		if (TEST_ERRNO == expected_errno) {
98f08c3bdfSopenharmony_ci			tst_resm(TPASS | TTERRNO, "failed as expected");
99f08c3bdfSopenharmony_ci		} else {
100f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
101f08c3bdfSopenharmony_ci				 "failed unexpectedly; expected - %d : %s",
102f08c3bdfSopenharmony_ci				 expected_errno, strerror(expected_errno));
103f08c3bdfSopenharmony_ci		}
104f08c3bdfSopenharmony_ci	} else {
105f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "munmap succeeded unexpectedly");
106f08c3bdfSopenharmony_ci	}
107f08c3bdfSopenharmony_ci}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_cistatic void test_einval1(void)
110f08c3bdfSopenharmony_ci{
111f08c3bdfSopenharmony_ci	struct rlimit brkval;
112f08c3bdfSopenharmony_ci	char *addr;
113f08c3bdfSopenharmony_ci	size_t map_len;
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	SAFE_GETRLIMIT(cleanup, RLIMIT_DATA, &brkval);
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	addr = (char *)brkval.rlim_max;
118f08c3bdfSopenharmony_ci	map_len = page_sz * 2;
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	TEST(munmap(addr, map_len));
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	check_and_print(EINVAL);
123f08c3bdfSopenharmony_ci}
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_cistatic void test_einval2(void)
126f08c3bdfSopenharmony_ci{
127f08c3bdfSopenharmony_ci	char *addr = global_addr;
128f08c3bdfSopenharmony_ci	size_t map_len = 0;
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci	TEST(munmap(addr, map_len));
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	check_and_print(EINVAL);
133f08c3bdfSopenharmony_ci}
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_cistatic void test_einval3(void)
136f08c3bdfSopenharmony_ci{
137f08c3bdfSopenharmony_ci	char *addr = (char *)(global_addr + 1);
138f08c3bdfSopenharmony_ci	size_t map_len = page_sz;
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	TEST(munmap(addr, map_len));
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ci	check_and_print(EINVAL);
143f08c3bdfSopenharmony_ci}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_cistatic void cleanup(void)
146f08c3bdfSopenharmony_ci{
147f08c3bdfSopenharmony_ci	if (munmap(global_addr, global_maplen) == -1)
148f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "munmap failed");
149f08c3bdfSopenharmony_ci}
150