]> granicus.if.org Git - strace/blob - mmap_cache.c
print_fields.h: whitespace fix in PRINT_FIELD_HEX_ARRAY
[strace] / mmap_cache.c
1 /*
2  * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3  * Copyright (c) 2013-2018 The strace developers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "defs.h"
29 #include <limits.h>
30
31 #include "largefile_wrappers.h"
32 #include "mmap_cache.h"
33 #include "xstring.h"
34
35 static unsigned int mmap_cache_generation;
36 static bool use_mmap_cache;
37
38 extern void mmap_cache_enable(void)
39 {
40         use_mmap_cache = true;
41 }
42
43 extern bool mmap_cache_is_enabled(void)
44 {
45         return use_mmap_cache;
46 }
47
48 /*
49  * caching of /proc/ID/maps for each process to speed up stack tracing
50  *
51  * The cache must be refreshed after syscalls that affect memory mappings,
52  * e.g. mmap, mprotect, munmap, execve.
53  */
54 static void
55 build_mmap_cache(struct tcb *tcp)
56 {
57         FILE *fp;
58         struct mmap_cache_t *cache_head = NULL;
59         /* start with a small dynamically-allocated array and then expand it */
60         size_t cur_array_size = 0;
61         char filename[sizeof("/proc/4294967296/maps")];
62         char buffer[PATH_MAX + 80];
63
64         xsprintf(filename, "/proc/%u/maps", tcp->pid);
65         fp = fopen_stream(filename, "r");
66         if (!fp) {
67                 perror_msg("fopen: %s", filename);
68                 return;
69         }
70
71         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
72                 struct mmap_cache_t *entry;
73                 unsigned long start_addr, end_addr, mmap_offset;
74                 char read_bit;
75                 char write_bit;
76                 char exec_bit;
77                 char shared_bit;
78                 unsigned long major, minor;
79                 char binary_path[sizeof(buffer)];
80
81                 if (sscanf(buffer, "%lx-%lx %c%c%c%c %lx %lx:%lx %*d %[^\n]",
82                            &start_addr, &end_addr,
83                            &read_bit, &write_bit, &exec_bit, &shared_bit,
84                            &mmap_offset,
85                            &major, &minor,
86                            binary_path) != 10)
87                         continue;
88
89                 /* skip mappings that have unknown protection */
90                 if (!(read_bit == '-' || read_bit == 'r'))
91                         continue;
92                 if (!(write_bit == '-' || write_bit == 'w'))
93                         continue;
94                 if (!(exec_bit == '-' || exec_bit == 'x'))
95                         continue;
96                 if (!(shared_bit == 'p' || shared_bit == 's'))
97                         continue;
98
99                 if (end_addr < start_addr) {
100                         error_msg("%s: unrecognized file format", filename);
101                         break;
102                 }
103
104                 /*
105                  * sanity check to make sure that we're storing
106                  * non-overlapping regions in ascending order
107                  */
108                 if (tcp->mmap_cache_size > 0) {
109                         entry = &cache_head[tcp->mmap_cache_size - 1];
110                         if (entry->start_addr == start_addr &&
111                             entry->end_addr == end_addr) {
112                                 /* duplicate entry, e.g. [vsyscall] */
113                                 continue;
114                         }
115                         if (start_addr <= entry->start_addr ||
116                             start_addr < entry->end_addr) {
117                                 debug_msg("%s: overlapping memory region: "
118                                           "\"%s\" [%08lx-%08lx] overlaps with "
119                                           "\"%s\" [%08lx-%08lx]",
120                                           filename, binary_path, start_addr,
121                                           end_addr, entry->binary_filename,
122                                           entry->start_addr, entry->end_addr);
123                                 continue;
124                         }
125                 }
126
127                 if (tcp->mmap_cache_size >= cur_array_size)
128                         cache_head = xgrowarray(cache_head, &cur_array_size,
129                                                 sizeof(*cache_head));
130
131                 entry = &cache_head[tcp->mmap_cache_size];
132                 entry->start_addr = start_addr;
133                 entry->end_addr = end_addr;
134                 entry->mmap_offset = mmap_offset;
135                 entry->protections = (
136                         0
137                         | ((read_bit   == 'r')? MMAP_CACHE_PROT_READABLE  : 0)
138                         | ((write_bit  == 'w')? MMAP_CACHE_PROT_WRITABLE  : 0)
139                         | ((exec_bit   == 'x')? MMAP_CACHE_PROT_EXECUTABLE: 0)
140                         | ((shared_bit == 's')? MMAP_CACHE_PROT_SHARED    : 0)
141                         );
142                 entry->major = major;
143                 entry->minor = minor;
144                 entry->binary_filename = xstrdup(binary_path);
145                 tcp->mmap_cache_size++;
146         }
147         fclose(fp);
148         tcp->mmap_cache = cache_head;
149         tcp->mmap_cache_generation = mmap_cache_generation;
150
151         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
152                        tcp->mmap_cache_generation,
153                        mmap_cache_generation,
154                        tcp, tcp->mmap_cache);
155 }
156
157 /* deleting the cache */
158 extern void
159 mmap_cache_delete(struct tcb *tcp, const char *caller)
160 {
161         unsigned int i;
162
163         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
164                        tcp->mmap_cache_generation,
165                        mmap_cache_generation,
166                        tcp, tcp->mmap_cache, caller);
167
168         for (i = 0; i < tcp->mmap_cache_size; i++) {
169                 free(tcp->mmap_cache[i].binary_filename);
170                 tcp->mmap_cache[i].binary_filename = NULL;
171         }
172         free(tcp->mmap_cache);
173         tcp->mmap_cache = NULL;
174         tcp->mmap_cache_size = 0;
175 }
176
177 extern enum mmap_cache_rebuild_result
178 mmap_cache_rebuild_if_invalid(struct tcb *tcp, const char *caller)
179 {
180         enum mmap_cache_rebuild_result r = MMAP_CACHE_REBUILD_READY;
181         if ((tcp->mmap_cache_generation != mmap_cache_generation)
182             && tcp->mmap_cache)
183                 mmap_cache_delete(tcp, caller);
184
185         if (!tcp->mmap_cache) {
186                 r = MMAP_CACHE_REBUILD_RENEWED;
187                 build_mmap_cache(tcp);
188         }
189
190         if (!(tcp->mmap_cache && tcp->mmap_cache_size))
191                 r = MMAP_CACHE_REBUILD_NOCACHE;
192
193         return r;
194 }
195
196 void
197 mmap_cache_invalidate(struct tcb *tcp)
198 {
199 #if SUPPORTED_PERSONALITIES > 1
200         if (tcp->currpers != DEFAULT_PERSONALITY) {
201                 /* disable stack trace */
202                 return;
203         }
204 #endif
205         mmap_cache_generation++;
206         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
207                        tcp->mmap_cache_generation,
208                        mmap_cache_generation,
209                        tcp, tcp->mmap_cache);
210 }
211
212 struct mmap_cache_t *
213 mmap_cache_search(struct tcb *tcp, unsigned long ip)
214 {
215         int lower = 0;
216         int upper = (int) tcp->mmap_cache_size - 1;
217
218         while (lower <= upper) {
219                 struct mmap_cache_t *cur_mmap_cache;
220                 int mid = (upper + lower) / 2;
221
222                 cur_mmap_cache = &tcp->mmap_cache[mid];
223
224                 if (ip >= cur_mmap_cache->start_addr &&
225                     ip < cur_mmap_cache->end_addr)
226                         return cur_mmap_cache;
227                 else if (ip < cur_mmap_cache->start_addr)
228                         upper = mid - 1;
229                 else
230                         lower = mid + 1;
231         }
232         return NULL;
233 }