1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio 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 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <fcntl.h>
25 #include <sys/mman.h>
26
27 #include <check.h>
28
29 #include <pulsecore/memtrap.h>
30 #include <pulsecore/core-util.h>
31
START_TEST(sigbus_test)32 START_TEST (sigbus_test) {
33 void *p;
34 int fd;
35 pa_memtrap *m;
36 const size_t page_size = pa_page_size();
37
38 pa_log_set_level(PA_LOG_DEBUG);
39 pa_memtrap_install();
40
41 /* Create the memory map */
42 fail_unless((fd = open("sigbus-test-map", O_RDWR|O_TRUNC|O_CREAT, 0660)) >= 0);
43 fail_unless(unlink("sigbus-test-map") == 0);
44 fail_unless(ftruncate(fd, page_size) >= 0);
45 fail_unless((p = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED);
46
47 /* Register memory map */
48 m = pa_memtrap_add(p, page_size);
49
50 /* Use memory map */
51 pa_snprintf(p, page_size, "This is a test that should work fine.");
52
53 /* Verify memory map */
54 pa_log("Let's see if this worked: %s", (char*) p);
55 pa_log("And memtrap says it is good: %s", pa_yes_no(pa_memtrap_is_good(m)));
56 fail_unless(pa_memtrap_is_good(m) == true);
57
58 /* Invalidate mapping */
59 fail_unless(ftruncate(fd, 0) >= 0);
60
61 /* Use memory map */
62 pa_snprintf(p, page_size, "This is a test that should fail but get caught.");
63
64 /* Verify memory map */
65 pa_log("Let's see if this worked: %s", (char*) p);
66 pa_log("And memtrap says it is good: %s", pa_yes_no(pa_memtrap_is_good(m)));
67 fail_unless(pa_memtrap_is_good(m) == false);
68
69 pa_memtrap_remove(m);
70 munmap(p, page_size);
71 close(fd);
72 }
73 END_TEST
74
main(int argc,char * argv[])75 int main(int argc, char *argv[]) {
76 int failed = 0;
77 Suite *s;
78 TCase *tc;
79 SRunner *sr;
80
81 s = suite_create("Sig Bus");
82 tc = tcase_create("sigbus");
83 tcase_add_test(tc, sigbus_test);
84 suite_add_tcase(s, tc);
85
86 sr = srunner_create(s);
87 srunner_run_all(sr, CK_NORMAL);
88 failed = srunner_ntests_failed(sr);
89 srunner_free(sr);
90
91 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
92 }
93