1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker /*
20*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
21*49cdfc7eSAndroid Build Coastguard Worker * Call mmap() to map a file creating a mapped region with execute access
22*49cdfc7eSAndroid Build Coastguard Worker * under the following conditions -
23*49cdfc7eSAndroid Build Coastguard Worker * - The prot parameter is set to PROT_EXE
24*49cdfc7eSAndroid Build Coastguard Worker * - The file descriptor is open for read
25*49cdfc7eSAndroid Build Coastguard Worker * - The file being mapped has execute permission bit set.
26*49cdfc7eSAndroid Build Coastguard Worker * - The minimum file permissions should be 0555.
27*49cdfc7eSAndroid Build Coastguard Worker *
28*49cdfc7eSAndroid Build Coastguard Worker * The call should succeed to map the file creating mapped memory with the
29*49cdfc7eSAndroid Build Coastguard Worker * required attributes.
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * Expected Result:
32*49cdfc7eSAndroid Build Coastguard Worker * mmap() should succeed returning the address of the mapped region,
33*49cdfc7eSAndroid Build Coastguard Worker * and the mapped region should contain the contents of the mapped file.
34*49cdfc7eSAndroid Build Coastguard Worker * but with ia64 and PARISC/hppa,
35*49cdfc7eSAndroid Build Coastguard Worker * an attempt to access the contents of the mapped region should give
36*49cdfc7eSAndroid Build Coastguard Worker * rise to the signal SIGSEGV.
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
39*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
40*49cdfc7eSAndroid Build Coastguard Worker */
41*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <setjmp.h>
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "mmapfile"
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmap03";
58*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static size_t page_sz;
61*49cdfc7eSAndroid Build Coastguard Worker static char *addr;
62*49cdfc7eSAndroid Build Coastguard Worker static char *dummy;
63*49cdfc7eSAndroid Build Coastguard Worker static int fildes;
64*49cdfc7eSAndroid Build Coastguard Worker static volatile int pass = 0;
65*49cdfc7eSAndroid Build Coastguard Worker static sigjmp_buf env;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
68*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
69*49cdfc7eSAndroid Build Coastguard Worker static void sig_handler(int sig);
70*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)71*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker int lc;
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker setup();
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker /*
84*49cdfc7eSAndroid Build Coastguard Worker * Call mmap to map the temporary file 'TEMPFILE'
85*49cdfc7eSAndroid Build Coastguard Worker * with execute access.
86*49cdfc7eSAndroid Build Coastguard Worker */
87*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
88*49cdfc7eSAndroid Build Coastguard Worker addr = mmap(0, page_sz, PROT_EXEC,
89*49cdfc7eSAndroid Build Coastguard Worker MAP_FILE | MAP_SHARED, fildes, 0);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker /* Check for the return value of mmap() */
92*49cdfc7eSAndroid Build Coastguard Worker if (addr == MAP_FAILED) {
93*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
94*49cdfc7eSAndroid Build Coastguard Worker TEMPFILE);
95*49cdfc7eSAndroid Build Coastguard Worker continue;
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker /*
99*49cdfc7eSAndroid Build Coastguard Worker * Read the file contents into the dummy
100*49cdfc7eSAndroid Build Coastguard Worker * variable.
101*49cdfc7eSAndroid Build Coastguard Worker */
102*49cdfc7eSAndroid Build Coastguard Worker if (read(fildes, dummy, page_sz) < 0) {
103*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker "reading %s failed", TEMPFILE);
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker /*
108*49cdfc7eSAndroid Build Coastguard Worker * Check whether the mapped memory region
109*49cdfc7eSAndroid Build Coastguard Worker * has the file contents.
110*49cdfc7eSAndroid Build Coastguard Worker *
111*49cdfc7eSAndroid Build Coastguard Worker * with ia64 and PARISC/hppa, this should
112*49cdfc7eSAndroid Build Coastguard Worker * generate a SIGSEGV which will be caught below.
113*49cdfc7eSAndroid Build Coastguard Worker *
114*49cdfc7eSAndroid Build Coastguard Worker */
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker if (sigsetjmp(env, 1) == 0) {
117*49cdfc7eSAndroid Build Coastguard Worker if (memcmp(dummy, addr, page_sz)) {
118*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
119*49cdfc7eSAndroid Build Coastguard Worker "mapped memory region "
120*49cdfc7eSAndroid Build Coastguard Worker "contains invalid data");
121*49cdfc7eSAndroid Build Coastguard Worker } else {
122*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS,
123*49cdfc7eSAndroid Build Coastguard Worker "mmap() functionality is "
124*49cdfc7eSAndroid Build Coastguard Worker "correct");
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker #if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
128*49cdfc7eSAndroid Build Coastguard Worker if (pass) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "Got SIGSEGV as expected");
130*49cdfc7eSAndroid Build Coastguard Worker } else {
131*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Mapped memory region with NO "
132*49cdfc7eSAndroid Build Coastguard Worker "access is accessible");
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker #endif
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker /* Clean up things in case we are looping */
137*49cdfc7eSAndroid Build Coastguard Worker /* Unmap the mapped memory */
138*49cdfc7eSAndroid Build Coastguard Worker if (munmap(addr, page_sz) != 0) {
139*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup,
140*49cdfc7eSAndroid Build Coastguard Worker "failed to unmap the mmapped pages");
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker pass = 0;
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker cleanup();
147*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker
setup(void)150*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
151*49cdfc7eSAndroid Build Coastguard Worker {
152*49cdfc7eSAndroid Build Coastguard Worker char *tst_buff;
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, sig_handler, cleanup);
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker page_sz = getpagesize();
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker /* Allocate space for the test buffer */
161*49cdfc7eSAndroid Build Coastguard Worker if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
162*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
163*49cdfc7eSAndroid Build Coastguard Worker }
164*49cdfc7eSAndroid Build Coastguard Worker
165*49cdfc7eSAndroid Build Coastguard Worker /* Fill the test buffer with the known data */
166*49cdfc7eSAndroid Build Coastguard Worker memset(tst_buff, 'A', page_sz);
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker /* Creat a temporary file used for mapping */
171*49cdfc7eSAndroid Build Coastguard Worker if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
172*49cdfc7eSAndroid Build Coastguard Worker free(tst_buff);
173*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
174*49cdfc7eSAndroid Build Coastguard Worker TEMPFILE);
175*49cdfc7eSAndroid Build Coastguard Worker }
176*49cdfc7eSAndroid Build Coastguard Worker
177*49cdfc7eSAndroid Build Coastguard Worker /* Write test buffer contents into temporary file */
178*49cdfc7eSAndroid Build Coastguard Worker if (write(fildes, tst_buff, page_sz) < (long)page_sz) {
179*49cdfc7eSAndroid Build Coastguard Worker free(tst_buff);
180*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup, "writing to %s failed",
181*49cdfc7eSAndroid Build Coastguard Worker TEMPFILE);
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker /* Free the memory allocated for test buffer */
185*49cdfc7eSAndroid Build Coastguard Worker free(tst_buff);
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker /* Make sure proper permissions set on file */
188*49cdfc7eSAndroid Build Coastguard Worker if (fchmod(fildes, 0555) < 0) {
189*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE);
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker
192*49cdfc7eSAndroid Build Coastguard Worker /* Close the temporary file opened for write */
193*49cdfc7eSAndroid Build Coastguard Worker if (close(fildes) < 0) {
194*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
195*49cdfc7eSAndroid Build Coastguard Worker TEMPFILE);
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker
198*49cdfc7eSAndroid Build Coastguard Worker /* Allocate and initialize dummy string of system page size bytes */
199*49cdfc7eSAndroid Build Coastguard Worker if ((dummy = calloc(page_sz, sizeof(char))) == NULL) {
200*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup, "calloc failed (dummy)");
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker /* Open the temporary file again for reading */
204*49cdfc7eSAndroid Build Coastguard Worker if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
205*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL | TERRNO, cleanup,
206*49cdfc7eSAndroid Build Coastguard Worker "opening %s read-only failed", TEMPFILE);
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker }
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker /*
211*49cdfc7eSAndroid Build Coastguard Worker * This function gets executed when the test process receives
212*49cdfc7eSAndroid Build Coastguard Worker * the signal SIGSEGV while trying to access the contents of memory which
213*49cdfc7eSAndroid Build Coastguard Worker * is not accessible.
214*49cdfc7eSAndroid Build Coastguard Worker */
sig_handler(int sig)215*49cdfc7eSAndroid Build Coastguard Worker static void sig_handler(int sig)
216*49cdfc7eSAndroid Build Coastguard Worker {
217*49cdfc7eSAndroid Build Coastguard Worker if (sig == SIGSEGV) {
218*49cdfc7eSAndroid Build Coastguard Worker /* set the global variable and jump back */
219*49cdfc7eSAndroid Build Coastguard Worker pass = 1;
220*49cdfc7eSAndroid Build Coastguard Worker siglongjmp(env, 1);
221*49cdfc7eSAndroid Build Coastguard Worker } else
222*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "received an unexpected signal");
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)225*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
226*49cdfc7eSAndroid Build Coastguard Worker {
227*49cdfc7eSAndroid Build Coastguard Worker close(fildes);
228*49cdfc7eSAndroid Build Coastguard Worker free(dummy);
229*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
230*49cdfc7eSAndroid Build Coastguard Worker }
231