Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <fcntl.h> 4 : #include <sys/mman.h> 5 : #include <unistd.h> 6 : 7 : #if HAVE_VALGRIND_VALGRIND_H 8 : # include <valgrind/valgrind.h> 9 : #endif 10 : 11 : #include "fd-util.h" 12 : #include "memory-util.h" 13 : #include "sigbus.h" 14 : #include "tests.h" 15 : 16 1 : int main(int argc, char *argv[]) { 17 1 : _cleanup_close_ int fd = -1; 18 1 : char template[] = "/tmp/sigbus-test-XXXXXX"; 19 1 : void *addr = NULL; 20 : uint8_t *p; 21 : 22 1 : test_setup_logging(LOG_INFO); 23 : 24 : #if HAS_FEATURE_ADDRESS_SANITIZER 25 : return log_tests_skipped("address-sanitizer is enabled"); 26 : #endif 27 : #if HAVE_VALGRIND_VALGRIND_H 28 : if (RUNNING_ON_VALGRIND) 29 : return log_tests_skipped("This test cannot run on valgrind"); 30 : #endif 31 : 32 1 : sigbus_install(); 33 : 34 1 : assert_se(sigbus_pop(&addr) == 0); 35 : 36 1 : assert_se((fd = mkostemp(template, O_RDWR|O_CREAT|O_EXCL)) >= 0); 37 1 : assert_se(unlink(template) >= 0); 38 1 : assert_se(posix_fallocate(fd, 0, page_size() * 8) >= 0); 39 : 40 1 : p = mmap(NULL, page_size() * 16, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 41 1 : assert_se(p != MAP_FAILED); 42 : 43 1 : assert_se(sigbus_pop(&addr) == 0); 44 : 45 1 : p[0] = 0xFF; 46 1 : assert_se(sigbus_pop(&addr) == 0); 47 : 48 1 : p[page_size()] = 0xFF; 49 1 : assert_se(sigbus_pop(&addr) == 0); 50 : 51 1 : p[page_size()*8] = 0xFF; 52 1 : p[page_size()*8+1] = 0xFF; 53 1 : p[page_size()*10] = 0xFF; 54 1 : assert_se(sigbus_pop(&addr) > 0); 55 1 : assert_se(addr == p + page_size() * 8); 56 1 : assert_se(sigbus_pop(&addr) > 0); 57 1 : assert_se(addr == p + page_size() * 10); 58 1 : assert_se(sigbus_pop(&addr) == 0); 59 : 60 1 : sigbus_reset(); 61 : }