1 /*
2 * Copyright (c) 2013-2021 The strace developers.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #ifndef STRACE_MMAP_CACHE_H
8 # define STRACE_MMAP_CACHE_H
9
10 /*
11 * Keep a sorted array of cache entries,
12 * so that we can binary search through it.
13 */
14
15 struct mmap_cache_t {
16 struct mmap_cache_entry_t *entry;
17 void (*free_fn)(struct tcb *, const char *caller);
18 unsigned int size;
19 unsigned int generation;
20 };
21
22 struct mmap_cache_entry_t {
23 /**
24 * example entry:
25 * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so
26 *
27 * start_addr is 0x7fabbb09b000
28 * end_addr is 0x7fabbb09f000
29 * mmap_offset is 0x179000
30 * protections is MMAP_CACHE_PROT_READABLE|MMAP_CACHE_PROT_EXECUTABLE
31 * major is 0xfc
32 * minor is 0x00
33 * binary_filename is "/lib/libc-2.11.1.so"
34 */
35 unsigned long start_addr;
36 unsigned long end_addr;
37 unsigned long mmap_offset;
38 unsigned char protections;
39 unsigned long major, minor;
40 char *binary_filename;
41 };
42
43 enum mmap_cache_protection {
44 MMAP_CACHE_PROT_READABLE = 1 << 0,
45 MMAP_CACHE_PROT_WRITABLE = 1 << 1,
46 MMAP_CACHE_PROT_EXECUTABLE = 1 << 2,
47 MMAP_CACHE_PROT_SHARED = 1 << 3,
48 };
49
50 enum mmap_cache_rebuild_result {
51 MMAP_CACHE_REBUILD_NOCACHE,
52 MMAP_CACHE_REBUILD_READY,
53 MMAP_CACHE_REBUILD_RENEWED,
54 };
55
56 typedef bool (*mmap_cache_search_fn)(struct mmap_cache_entry_t *, void *);
57
58 extern void
59 mmap_cache_enable(void);
60
61 extern enum mmap_cache_rebuild_result
62 mmap_cache_rebuild_if_invalid(struct tcb *, const char *caller);
63
64 extern struct mmap_cache_entry_t *
65 mmap_cache_search(struct tcb *, unsigned long ip);
66
67 extern struct mmap_cache_entry_t *
68 mmap_cache_search_custom(struct tcb *, mmap_cache_search_fn, void *);
69
70 #endif /* !STRACE_MMAP_CACHE_H */