1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com> 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 7f08c3bdfSopenharmony_ci * (at your option) any later version. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 15f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci/* 20f08c3bdfSopenharmony_ci * Test Description: 21f08c3bdfSopenharmony_ci * Verify error signal SIGBUS. 22f08c3bdfSopenharmony_ci * "Attempted access to a portion of the buffer that does not correspond 23f08c3bdfSopenharmony_ci * to the file." 24f08c3bdfSopenharmony_ci * 25f08c3bdfSopenharmony_ci * Expected Result: 26f08c3bdfSopenharmony_ci * mmap() should succeed returning the address of the mapped region, 27f08c3bdfSopenharmony_ci * and an attempt to access the memory which does not correspond to the file 28f08c3bdfSopenharmony_ci * should rise the signal SIGBUS. 29f08c3bdfSopenharmony_ci */ 30f08c3bdfSopenharmony_ci#include <stdio.h> 31f08c3bdfSopenharmony_ci#include <stdlib.h> 32f08c3bdfSopenharmony_ci#include <sys/types.h> 33f08c3bdfSopenharmony_ci#include <errno.h> 34f08c3bdfSopenharmony_ci#include <unistd.h> 35f08c3bdfSopenharmony_ci#include <fcntl.h> 36f08c3bdfSopenharmony_ci#include <string.h> 37f08c3bdfSopenharmony_ci#include <signal.h> 38f08c3bdfSopenharmony_ci#include <sys/stat.h> 39f08c3bdfSopenharmony_ci#include <sys/mman.h> 40f08c3bdfSopenharmony_ci#include <setjmp.h> 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci#include "test.h" 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci#define TEMPFILE "mmapfile" 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_cichar *TCID = "mmap13"; 47f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic size_t page_sz; 50f08c3bdfSopenharmony_cistatic char *addr; 51f08c3bdfSopenharmony_cistatic int fildes; 52f08c3bdfSopenharmony_cistatic volatile sig_atomic_t pass; 53f08c3bdfSopenharmony_cistatic sigjmp_buf env; 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void setup(void); 56f08c3bdfSopenharmony_cistatic void cleanup(void); 57f08c3bdfSopenharmony_cistatic void sig_handler(int sig); 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 60f08c3bdfSopenharmony_ci{ 61f08c3bdfSopenharmony_ci int lc; 62f08c3bdfSopenharmony_ci char *ch; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci setup(); 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 69f08c3bdfSopenharmony_ci tst_count = 0; 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci addr = mmap(NULL, page_sz * 2, PROT_READ | PROT_WRITE, 72f08c3bdfSopenharmony_ci MAP_FILE | MAP_SHARED, fildes, 0); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci if (addr == MAP_FAILED) { 75f08c3bdfSopenharmony_ci tst_resm(TFAIL | TERRNO, "mmap() failed on %s", 76f08c3bdfSopenharmony_ci TEMPFILE); 77f08c3bdfSopenharmony_ci continue; 78f08c3bdfSopenharmony_ci } 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci if (sigsetjmp(env, 1) == 0) { 81f08c3bdfSopenharmony_ci ch = addr + page_sz + 1; 82f08c3bdfSopenharmony_ci *ch = 0; 83f08c3bdfSopenharmony_ci } 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci if (pass) 86f08c3bdfSopenharmony_ci tst_resm(TPASS, "Got SIGBUS " 87f08c3bdfSopenharmony_ci "as expected"); 88f08c3bdfSopenharmony_ci else 89f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Invalid access not " 90f08c3bdfSopenharmony_ci "rise SIGBUS"); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci if (munmap(addr, page_sz * 2) != 0) 93f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 94f08c3bdfSopenharmony_ci "failed to unmap the mmapped pages"); 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci pass = 0; 97f08c3bdfSopenharmony_ci } 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci cleanup(); 100f08c3bdfSopenharmony_ci tst_exit(); 101f08c3bdfSopenharmony_ci} 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_cistatic void setup(void) 104f08c3bdfSopenharmony_ci{ 105f08c3bdfSopenharmony_ci tst_sig(NOFORK, sig_handler, cleanup); 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci TEST_PAUSE; 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci page_sz = getpagesize(); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci tst_tmpdir(); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0766); 114f08c3bdfSopenharmony_ci if (fildes < 0) 115f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed", 116f08c3bdfSopenharmony_ci TEMPFILE); 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci if (ftruncate(fildes, page_sz / 2) == -1) 119f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate %s failed", 120f08c3bdfSopenharmony_ci TEMPFILE); 121f08c3bdfSopenharmony_ci} 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci/* 124f08c3bdfSopenharmony_ci * This function gets executed when the test process receives 125f08c3bdfSopenharmony_ci * the signal SIGBUS while trying to access the memory which 126f08c3bdfSopenharmony_ci * does not correspond to the file. 127f08c3bdfSopenharmony_ci */ 128f08c3bdfSopenharmony_cistatic void sig_handler(int sig) 129f08c3bdfSopenharmony_ci{ 130f08c3bdfSopenharmony_ci if (sig == SIGBUS) { 131f08c3bdfSopenharmony_ci pass = 1; 132f08c3bdfSopenharmony_ci siglongjmp(env, 1); 133f08c3bdfSopenharmony_ci } else { 134f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "received an unexpected signal"); 135f08c3bdfSopenharmony_ci } 136f08c3bdfSopenharmony_ci} 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_cistatic void cleanup(void) 139f08c3bdfSopenharmony_ci{ 140f08c3bdfSopenharmony_ci close(fildes); 141f08c3bdfSopenharmony_ci tst_rmdir(); 142f08c3bdfSopenharmony_ci} 143