Branch data Line data Source code
1 : : /* Find Elf file and cache via Dwflst_Process_Tracker.
2 : : Copyright (C) 2025, Red Hat, Inc.
3 : : This file is part of elfutils.
4 : :
5 : : This file is free software; you can redistribute it and/or modify
6 : : it under the terms of either
7 : :
8 : : * the GNU Lesser General Public License as published by the Free
9 : : Software Foundation; either version 3 of the License, or (at
10 : : your option) any later version
11 : :
12 : : or
13 : :
14 : : * the GNU General Public License as published by the Free
15 : : Software Foundation; either version 2 of the License, or (at
16 : : your option) any later version
17 : :
18 : : or both in parallel, as here.
19 : :
20 : : elfutils is distributed in the hope that it will be useful, but
21 : : WITHOUT ANY WARRANTY; without even the implied warranty of
22 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 : : General Public License for more details.
24 : :
25 : : You should have received copies of the GNU General Public License and
26 : : the GNU Lesser General Public License along with this program. If
27 : : not, see <http://www.gnu.org/licenses/>. */
28 : :
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <sys/stat.h>
35 : : #include "../libelf/libelfP.h"
36 : :
37 : : #include "libdwfl_stacktraceP.h"
38 : :
39 : : unsigned long int
40 : 54 : __libdwfl_stacktrace_elftab_hash_st (const char *module_name,
41 : : dev_t st_dev,
42 : : ino_t st_ino)
43 : : {
44 : 54 : unsigned long int hval = elf_hash(module_name);
45 : 54 : hval ^= (unsigned long int)st_dev;
46 : 54 : hval ^= (unsigned long int)st_ino;
47 : 54 : return hval;
48 : : }
49 : :
50 : : unsigned long int
51 : 18 : __libdwfl_stacktrace_elftab_hash (const char *module_name,
52 : : const char *module_path,
53 : : int fd)
54 : : {
55 : 18 : struct stat sb;
56 : 18 : int rc = -1;
57 [ - + ]: 18 : if (fd >= 0)
58 : 0 : rc = fstat(fd, &sb);
59 [ + - ]: 18 : else if (module_path != NULL)
60 : 18 : rc = stat(module_path, &sb);
61 [ - + ]: 18 : if (rc < 0)
62 : 0 : return elf_hash(module_name);
63 : 18 : return __libdwfl_stacktrace_elftab_hash_st
64 : 18 : (module_name, sb.st_dev, sb.st_ino);
65 : : }
66 : :
67 : : int
68 : 18 : dwflst_tracker_find_cached_elf (Dwflst_Process_Tracker *tracker,
69 : : const char *module_name,
70 : : const char *module_path,
71 : : char **file_name, Elf **elfp)
72 : : {
73 : 18 : dwflst_tracker_elf_info *ent = NULL;
74 : 18 : int rc = -1;
75 : 18 : struct stat sb;
76 : :
77 [ - + ]: 18 : if (module_path == NULL)
78 : 0 : module_path = module_name;
79 : 18 : unsigned long int hval =
80 : 18 : __libdwfl_stacktrace_elftab_hash (module_name, module_path, -1/* no fd */);
81 : :
82 : 18 : rwlock_rdlock(tracker->elftab_lock);
83 : 18 : ent = dwflst_tracker_elftab_find(&tracker->elftab, hval);
84 : :
85 : : /* Guard against collisions.
86 : : TODO: Need proper chaining, dynamicsizehash_concurrent isn't really
87 : : equipped for it. */
88 [ - + ]: 18 : if (ent != NULL)
89 : 0 : rc = fstat(ent->fd, &sb);
90 [ # # # # ]: 0 : if (rc < 0 || strcmp (module_name, ent->module_name) != 0
91 [ # # # # ]: 0 : || ent->dev != sb.st_dev || ent->ino != sb.st_ino)
92 : : {
93 : : rwlock_unlock(tracker->elftab_lock);
94 : : return -1;
95 : : }
96 : :
97 : : /* Verify that ent->fd has not been updated: */
98 : 0 : if (rc < 0 || ent->dev != sb.st_dev || ent->ino != sb.st_ino
99 [ # # ]: 0 : || ent->last_mtime != sb.st_mtime)
100 : : {
101 : : rwlock_unlock(tracker->elftab_lock);
102 : : return -1;
103 : : }
104 : 0 : *elfp = __libelf_keep (ent->elf);
105 : 0 : *file_name = strdup (ent->module_name);
106 : 0 : int fd = ent->fd;
107 : 0 : rwlock_unlock(tracker->elftab_lock);
108 : 0 : return fd;
109 : : }
110 : : INTDEF(dwflst_tracker_find_cached_elf)
111 : :
112 : : bool
113 : 36 : dwflst_tracker_cache_elf (Dwflst_Process_Tracker *tracker,
114 : : const char *module_name,
115 : : const char *file_name __attribute__((unused)),
116 : : Elf *elf, int fd)
117 : : {
118 : 36 : dwflst_tracker_elf_info *ent = NULL;
119 : 36 : int rc = -1;
120 : 36 : struct stat sb;
121 : :
122 [ + - ]: 36 : if (fd >= 0)
123 : 36 : rc = fstat(fd, &sb);
124 [ + - ]: 36 : if (rc < 0)
125 : : return false;
126 : 36 : unsigned long int hval =
127 : 36 : __libdwfl_stacktrace_elftab_hash_st (module_name, sb.st_dev, sb.st_ino);
128 : :
129 : 36 : rwlock_wrlock(tracker->elftab_lock);
130 : 36 : ent = dwflst_tracker_elftab_find(&tracker->elftab, hval);
131 : : /* Guard against collisions.
132 : : TODO: Need proper chaining, dynamicsizehash_concurrent isn't really
133 : : equipped for it. */
134 [ + + + - ]: 36 : if (ent != NULL && (strcmp (module_name, ent->module_name) != 0
135 [ + - + - ]: 18 : || ent->dev != sb.st_dev || ent->ino != sb.st_ino))
136 : : {
137 : : rwlock_unlock(tracker->elftab_lock);
138 : : return false;
139 : : }
140 : 36 : if (ent == NULL)
141 : : {
142 : 18 : ent = calloc (1, sizeof (dwflst_tracker_elf_info));
143 [ - + ]: 18 : if (ent == NULL)
144 : : {
145 : 0 : rwlock_unlock(tracker->elftab_lock);
146 : 0 : __libdwfl_seterrno (DWFL_E_NOMEM);
147 : 0 : return false;
148 : : }
149 : 18 : ent->module_name = strdup(module_name);
150 : :
151 [ - + ]: 18 : if (dwflst_tracker_elftab_insert(&tracker->elftab, hval, ent) != 0)
152 : : {
153 : 0 : free(ent->module_name);
154 : 0 : free(ent);
155 : 0 : rwlock_unlock(tracker->elftab_lock);
156 : 0 : assert(false); /* Should not occur due to the wrlock on elftab. */
157 : : }
158 : : }
159 : : else
160 : : {
161 : : /* TODO: The following assertions are still triggered on certain
162 : : code paths that acquire fds or create Elf structs without
163 : : checking the caching mechanism first. This is not a serious
164 : : problem, and can be fixed incrementally. */
165 : :
166 : : /* assert(ent->elf == NULL || ent->elf == elf); */ /* Guard against redundant/leaked Elf *. */
167 : : /* assert(ent->fd == fd); */ /* Guard against redundant open. */
168 : :
169 : : /* For now, correct behaviour (from dwfl_module_getdwarf.c open_elf)
170 : : is to replace the existing elf, keep module_name. */
171 [ - + - - ]: 18 : if (ent->elf != NULL && ent->elf != elf)
172 : 0 : elf_end(ent->elf);
173 : : }
174 [ + + + - ]: 36 : if (elf != NULL && ent->elf != elf)
175 : 18 : __libelf_keep (elf);
176 : 36 : ent->elf = elf;
177 : 36 : ent->fd = fd;
178 [ + - ]: 36 : if (rc == 0)
179 : : {
180 : 36 : ent->dev = sb.st_dev;
181 : 36 : ent->ino = sb.st_ino;
182 : 36 : ent->last_mtime = sb.st_mtime;
183 : : }
184 : : /* else create a cache entry with 0 values for dev/ino/mtime;
185 : : since dev/ino are hashed, this will not conflict with entries
186 : : for which fstat was successful */
187 : : rwlock_unlock(tracker->elftab_lock);
188 : : return true;
189 : : }
190 : : INTDEF(dwflst_tracker_cache_elf)
191 : :
192 : : Dwflst_Process_Tracker *
193 : 18 : dwflst_module_gettracker (Dwfl_Module *mod)
194 : : {
195 [ + - ]: 18 : if (mod == NULL)
196 : : return NULL;
197 [ + - ]: 18 : if (mod->dwfl == NULL)
198 : : return NULL;
199 : 18 : return mod->dwfl->tracker;
200 : : }
201 : : INTDEF(dwflst_module_gettracker)
202 : :
203 : : int
204 : 18 : dwflst_tracker_linux_proc_find_elf (Dwfl_Module *mod,
205 : : void **userdata __attribute__ ((unused)),
206 : : const char *module_name, Dwarf_Addr base,
207 : : char **file_name, Elf **elfp)
208 : : {
209 : 18 : Dwflst_Process_Tracker *tracker = INTUSE(dwflst_module_gettracker) (mod);
210 : 18 : int fd;
211 : :
212 [ + - ]: 18 : if (tracker != NULL)
213 : : {
214 : 18 : fd = INTUSE(dwflst_tracker_find_cached_elf)
215 : : (tracker, module_name, module_name, file_name, elfp);
216 [ + - ]: 18 : if (fd >= 0)
217 : : return fd;
218 : : }
219 : :
220 : 18 : fd = INTUSE(dwfl_linux_proc_find_elf) (mod, userdata, module_name,
221 : : base, file_name, elfp);
222 : :
223 [ + - + - ]: 18 : if (tracker != NULL && fd >= 0 && *file_name != NULL)
224 : : {
225 : 18 : INTUSE(dwflst_tracker_cache_elf)
226 : : (tracker, module_name, *file_name, *elfp, fd);
227 : : }
228 : : return fd;
229 : : }
|