1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2013 FNST, DAN LI <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2024 SUSE LLC Avinesh Kumar <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Verify that, mmap() call succeeds to create a file mapping with length
11*49cdfc7eSAndroid Build Coastguard Worker * argument greater than the file size but any attempt to reference the
12*49cdfc7eSAndroid Build Coastguard Worker * memory region which does not correspond to the file causes SIGBUS signal.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <setjmp.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "mmapfile"
20*49cdfc7eSAndroid Build Coastguard Worker static size_t page_sz;
21*49cdfc7eSAndroid Build Coastguard Worker static char *addr;
22*49cdfc7eSAndroid Build Coastguard Worker static int fd;
23*49cdfc7eSAndroid Build Coastguard Worker static volatile sig_atomic_t pass;
24*49cdfc7eSAndroid Build Coastguard Worker static sigjmp_buf env;
25*49cdfc7eSAndroid Build Coastguard Worker
sig_handler(int sig)26*49cdfc7eSAndroid Build Coastguard Worker static void sig_handler(int sig)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker if (sig == SIGBUS) {
29*49cdfc7eSAndroid Build Coastguard Worker pass = 1;
30*49cdfc7eSAndroid Build Coastguard Worker siglongjmp(env, 1);
31*49cdfc7eSAndroid Build Coastguard Worker }
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker
setup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGNAL(SIGBUS, sig_handler);
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker page_sz = getpagesize();
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
41*49cdfc7eSAndroid Build Coastguard Worker SAFE_FTRUNCATE(fd, page_sz / 2);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
run(void)44*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker char *ch;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker addr = mmap(0, page_sz * 2, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
49*49cdfc7eSAndroid Build Coastguard Worker if (addr == MAP_FAILED) {
50*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "mmap() of %s failed", TEMPFILE);
51*49cdfc7eSAndroid Build Coastguard Worker return;
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker if (sigsetjmp(env, 1) == 0) {
55*49cdfc7eSAndroid Build Coastguard Worker ch = addr + page_sz + 1;
56*49cdfc7eSAndroid Build Coastguard Worker *ch = 0;
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker if (pass == 1)
60*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Received SIGBUS signal as expected");
61*49cdfc7eSAndroid Build Coastguard Worker else
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "SIGBUS signal not received");
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(addr, page_sz * 2);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)67*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
74*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
75*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
76*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
77*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1
78*49cdfc7eSAndroid Build Coastguard Worker };
79