Debian Codegraph @

gdb/unittests/scoped_mmap-selftests.c

raw symbols SCIP
1 /* Self tests for scoped_mmap for GDB, the GNU debugger.
2
3 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "gdbsupport/filestuff.h"
22 #include "gdbsupport/scoped_mmap.h"
23 #include "config.h"
24
25 #if defined(HAVE_SYS_MMAN_H)
26
27 #include "gdbsupport/selftest.h"
28 #include "gdbsupport/gdb_unlinker.h"
29
30 #include <unistd.h>
31
32 namespace selftests {
33 namespace scoped_mmap {
34
35 /* Test that the file is unmapped. */
36 static void
37 test_destroy ()
38 {
39 void *mem;
40
41 errno = 0;
42 {
43 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
44 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
45
46 mem = smmap.get ();
47 SELF_CHECK (mem != nullptr);
48 }
49
50 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == -1 && errno == ENOMEM);
51 }
52
53 /* Test that the memory can be released. */
54 static void
55 test_release ()
56 {
57 void *mem;
58
59 errno = 0;
60 {
61 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
62 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
63
64 mem = smmap.release ();
65 SELF_CHECK (mem != nullptr);
66 }
67
68 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == 0 || errno != ENOMEM);
69
70 munmap (mem, sysconf (_SC_PAGESIZE));
71 }
72
73 /* Run selftests. */
74 static void
75 run_tests ()
76 {
77 test_destroy ();
78 test_release ();
79 }
80
81 } /* namespace scoped_mmap */
82
83 namespace mmap_file
84 {
85
86 /* Test the standard usage of mmap_file. */
87 static void
88 test_normal ()
89 {
90 char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
91 {
92 scoped_fd fd = gdb_mkostemp_cloexec (filename);
93 SELF_CHECK (fd.get () >= 0);
94
95 SELF_CHECK (write (fd.get (), "Hello!", 7) == 7);
96 }
97
98 gdb::unlinker unlink_test_file (filename);
99
100 {
101 ::scoped_mmap m = ::mmap_file (filename);
102
103 SELF_CHECK (m.get () != MAP_FAILED);
104 SELF_CHECK (m.size () == 7);
105 SELF_CHECK (0 == strcmp ((char *) m.get (), "Hello!"));
106 }
107 }
108
109 /* Calling mmap_file with a non-existent file should throw an exception. */
110 static void
111 test_invalid_filename ()
112 {
113 bool threw = false;
114
115 try {
116 ::scoped_mmap m = ::mmap_file ("/this/file/should/not/exist");
117 } catch (const gdb_exception &e) {
118 threw = true;
119 }
120
121 SELF_CHECK (threw);
122 }
123
124
125 /* Run selftests. */
126 static void
127 run_tests ()
128 {
129 test_normal ();
130 test_invalid_filename ();
131 }
132
133 } /* namespace mmap_file */
134 } /* namespace selftests */
135
136 #endif /* !defined(HAVE_SYS_MMAN_H) */
137
138 void _initialize_scoped_mmap_selftests ();
139 void
140 _initialize_scoped_mmap_selftests ()
141 {
142 #if defined(HAVE_SYS_MMAN_H)
143 selftests::register_test ("scoped_mmap",
144 selftests::scoped_mmap::run_tests);
145 selftests::register_test ("mmap_file",
146 selftests::mmap_file::run_tests);
147 #endif
148 }