]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sigbus.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / test / test-sigbus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/mman.h>
21
22 #include "fd-util.h"
23 #include "sigbus.h"
24 #include "util.h"
25 #if HAVE_VALGRIND_VALGRIND_H
26 #include <valgrind/valgrind.h>
27 #endif
28
29 int main(int argc, char *argv[]) {
30 _cleanup_close_ int fd = -1;
31 char template[] = "/tmp/sigbus-test-XXXXXX";
32 void *addr = NULL;
33 uint8_t *p;
34
35 #if HAVE_VALGRIND_VALGRIND_H
36 if (RUNNING_ON_VALGRIND)
37 return EXIT_TEST_SKIP;
38 #endif
39
40 #ifdef __SANITIZE_ADDRESS__
41 return EXIT_TEST_SKIP;
42 #endif
43 sigbus_install();
44
45 assert_se(sigbus_pop(&addr) == 0);
46
47 assert_se((fd = mkostemp(template, O_RDWR|O_CREAT|O_EXCL)) >= 0);
48 assert_se(unlink(template) >= 0);
49 assert_se(posix_fallocate(fd, 0, page_size() * 8) >= 0);
50
51 p = mmap(NULL, page_size() * 16, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
52 assert_se(p != MAP_FAILED);
53
54 assert_se(sigbus_pop(&addr) == 0);
55
56 p[0] = 0xFF;
57 assert_se(sigbus_pop(&addr) == 0);
58
59 p[page_size()] = 0xFF;
60 assert_se(sigbus_pop(&addr) == 0);
61
62 p[page_size()*8] = 0xFF;
63 p[page_size()*8+1] = 0xFF;
64 p[page_size()*10] = 0xFF;
65 assert_se(sigbus_pop(&addr) > 0);
66 assert_se(addr == p + page_size() * 8);
67 assert_se(sigbus_pop(&addr) > 0);
68 assert_se(addr == p + page_size() * 10);
69 assert_se(sigbus_pop(&addr) == 0);
70
71 sigbus_reset();
72 }