Debian Codegraph @

ext/GOTCHA/src/gotcha.c

raw symbols SCIP
1 /*
2 This file is part of GOTCHA. For copyright information see the COPYRIGHT
3 file in the top level directory, or at
4 https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU Lesser General Public License (as published by the Free
7 Software Foundation) version 2.1 dated February 1999. This program is
8 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 PURPOSE. See the terms and conditions of the GNU Lesser General Public License
11 for more details. You should have received a copy of the GNU Lesser General
12 Public License along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 */
15
16 #define _GNU_SOURCE
17 #include "gotcha/gotcha.h"
18
19 #include <link.h>
20
21 #include "elf_ops.h"
22 #include "gotcha/gotcha_types.h"
23 #include "gotcha_auxv.h"
24 #include "gotcha_dl.h"
25 #include "gotcha_utils.h"
26 #include "libc_wrappers.h"
27 #include "tool.h"
28 #include "translations.h"
29
30 static void writeAddress(void *write, void *value) { *(void **)write = value; }
31
32 static void **getBindingAddressPointer(struct gotcha_binding_t *in) {
33 return (void **)in->function_handle;
34 }
35
36 static void setBindingAddressPointer(struct gotcha_binding_t *in, void *value) {
37 void **target = getBindingAddressPointer(in);
38 debug_printf(3, "Updating binding address pointer at %p to %p\n", target,
39 value);
40 writeAddress(target, value);
41 }
42
43 void **getInternalBindingAddressPointer(struct internal_binding_t **in) {
44 return (void **)&((*in)->wrappee_pointer);
45 }
46
47 static void setInternalBindingAddressPointer(void **in, void *value) {
48 void **target =
49 getInternalBindingAddressPointer((struct internal_binding_t **)in);
50 debug_printf(3, "Updating binding address pointer at %p to %p\n", target,
51 value);
52 writeAddress(target, value);
53 }
54
55 long lookup_exported_symbol(const char *name, const struct link_map *lib,
56 void **symbol) {
57 long result;
58 if (is_vdso(lib)) {
59 debug_printf(2, "Skipping VDSO library at 0x%lx with name %s\n",
60 lib->l_addr, LIB_NAME(lib));
61 return -1;
62 }
63 debug_printf(2, "Searching for exported symbols in %s\n", LIB_NAME(lib));
64 INIT_DYNAMIC(lib);
65
66 if (!gnu_hash && !elf_hash) { // GCOVR_EXCL_START
67 debug_printf(3, "Library %s does not export or import symbols\n",
68 LIB_NAME(lib));
69 return -1;
70 } // GCOVR_EXCL_STOP
71 result = -1;
72 if (gnu_hash) {
73 debug_printf(3, "Checking GNU hash for %s in %s\n", name, LIB_NAME(lib));
74 result = lookup_gnu_hash_symbol(name, symtab, versym, strtab,
75 (struct gnu_hash_header *)gnu_hash);
76 }
77 if (elf_hash && result == -1) {
78 debug_printf(3, "Checking ELF hash for %s in %s\n", name, LIB_NAME(lib));
79 result = lookup_elf_hash_symbol(name, symtab, versym, strtab,
80 (ElfW(Word) *)elf_hash);
81 }
82 if (result == -1) {
83 debug_printf(3, "%s not found in %s\n", name, LIB_NAME(lib));
84 return -1;
85 }
86 if (!GOTCHA_CHECK_VISIBILITY(symtab[result])) { // GCOVR_EXCL_START
87 debug_printf(3, "Symbol %s found but not exported in %s\n", name,
88 LIB_NAME(lib));
89 return -1;
90 } // GCOVR_EXCL_STOP
91
92 debug_printf(2, "Symbol %s found in %s at 0x%lx\n", name, LIB_NAME(lib),
93 symtab[result].st_value + lib->l_addr);
94 *symbol = (void *)(symtab[result].st_value + lib->l_addr);
95 return result;
96 }
97
98 int prepare_symbol(struct internal_binding_t *binding) {
99 int result;
100 struct link_map *lib;
101 struct gotcha_binding_t *user_binding = binding->user_binding;
102
103 debug_printf(2, "Looking up exported symbols for %s\n", user_binding->name);
104 for (lib = _r_debug.r_map; lib != 0; lib = lib->l_next) {
105 struct library_t *int_library = get_library(lib);
106 if (!int_library) {
107 debug_printf(3, "Creating new library object for %s\n", LIB_NAME(lib));
108 int_library = add_library(lib);
109 }
110 void *symbol;
111 result = lookup_exported_symbol(user_binding->name, lib, &symbol);
112 if (result != -1) {
113 setInternalBindingAddressPointer(user_binding->function_handle, symbol);
114 return 0;
115 }
116 }
117 debug_printf(1, "Symbol %s was found in program\n", user_binding->name);
118 return -1;
119 }
120
121 static void insert_at_head(struct internal_binding_t *binding,
122 struct internal_binding_t *head) {
123 binding->next_binding = head;
124 setInternalBindingAddressPointer(binding->user_binding->function_handle,
125 head->user_binding->wrapper_pointer);
126 removefrom_hashtable(&function_hash_table,
127 (void *)binding->user_binding->name);
128 addto_hashtable(&function_hash_table, (void *)binding->user_binding->name,
129 (void *)binding);
130 }
131
132 static void insert_after_pos(struct internal_binding_t *binding,
133 struct internal_binding_t *pos) {
134 setInternalBindingAddressPointer(binding->user_binding->function_handle,
135 pos->wrappee_pointer);
136 setInternalBindingAddressPointer(pos->user_binding->function_handle,
137 binding->user_binding->wrapper_pointer);
138 binding->next_binding = pos->next_binding;
139 pos->next_binding = binding;
140 }
141
142 #define RWO_NOCHANGE 0
143 #define RWO_NEED_LOOKUP (1 << 0)
144 #define RWO_NEED_BINDING (1 << 1)
145 static int rewrite_wrapper_orders(struct internal_binding_t *binding) {
146 const char *name = binding->user_binding->name;
147 int insert_priority = get_priority(binding->associated_binding_table->tool);
148
149 if (gotcha_strcmp(name, "main") == 0) {
150 if (!main_wrapped) {
151 debug_printf(2, "Wrapping main with Gotcha's internal wrappers");
152 main_wrapped = 1;
153 gotcha_wrap(libc_main_wrappers, 1, "gotcha");
154 gotcha_wrap(main_wrappers, 1, "gotcha");
155 }
156 }
157
158 debug_printf(2,
159 "gotcha_rewrite_wrapper_orders for binding %s in tool %s of "
160 "priority %d\n",
161 name, binding->associated_binding_table->tool->tool_name,
162 insert_priority);
163
164 struct internal_binding_t *head;
165 int hash_result;
166 hash_result =
167 lookup_hashtable(&function_hash_table, (void *)name, (void **)&head);
168 if (hash_result != 0) {
169 debug_printf(2, "Adding new entry for %s to hash table\n", name);
170 addto_hashtable(&function_hash_table, (void *)name, (void *)binding);
171 return (RWO_NEED_LOOKUP | RWO_NEED_BINDING);
172 }
173
174 int head_priority = get_priority(head->associated_binding_table->tool);
175 if (head_priority < insert_priority) {
176 debug_printf(2,
177 "New binding priority %d is greater than head priority %d, "
178 "adding to head\n",
179 insert_priority, head_priority);
180 insert_at_head(binding, head);
181 return RWO_NEED_BINDING;
182 }
183
184 struct internal_binding_t *cur;
185 for (cur = head; cur->next_binding; cur = cur->next_binding) {
186 int next_priority =
187 get_priority(cur->next_binding->associated_binding_table->tool);
188 debug_printf(
189 3,
190 "Comparing binding for new insertion %d to binding for tool %s at %d\n",
191 insert_priority,
192 cur->next_binding->associated_binding_table->tool->tool_name,
193 next_priority);
194 if (next_priority < insert_priority) {
195 break;
196 }
197 if (cur->user_binding->wrapper_pointer ==
198 binding->user_binding->wrapper_pointer) {
199 debug_printf(3, "Tool is already inserted. Skipping binding rewrite\n");
200 return RWO_NOCHANGE;
201 }
202 }
203 debug_printf(2, "Inserting binding after tool %s\n",
204 cur->associated_binding_table->tool->tool_name);
205 insert_after_pos(binding, cur);
206 return RWO_NOCHANGE;
207 }
208
209 static int update_lib_bindings(ElfW(Sym) * symbol KNOWN_UNUSED, char *name,
210 ElfW(Addr) offset, struct link_map *lmap,
211 hash_table_t *lookuptable) {
212 int result;
213 struct internal_binding_t *internal_binding;
214 void **got_address;
215
216 result = lookup_hashtable(lookuptable, name, (void **)&internal_binding);
217 if (result != 0) return -1;
218 got_address = (void **)(lmap->l_addr + offset);
219 writeAddress(got_address, internal_binding->user_binding->wrapper_pointer);
220 debug_printf(3, "Remapped call to %s at 0x%lx in %s to wrapper at 0x%p\n",
221 name, (lmap->l_addr + offset), LIB_NAME(lmap),
222 internal_binding->user_binding->wrapper_pointer);
223 return 0;
224 }
225
226 #ifndef MAX
227 #define MAX(a, b) (a > b ? a : b)
228 #endif
229 /**
230 * structure used to pass lookup addr and return library address.
231 */
232 struct Boundary {
233 const char *l_name; // input
234 ElfW(Addr) load_addr; // input
235 ElfW(Addr) start_addr; // output
236 ElfW(Addr) end_addr; // output
237 int found;
238 };
239 int find_relro_boundary(struct dl_phdr_info *info, size_t size, void *data) {
240 struct Boundary *boundary = data;
241 int found = 0;
242 for (int i = 0; i < info->dlpi_phnum; ++i) {
243 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
244 if (strcmp(boundary->l_name, info->dlpi_name) == 0 &&
245 boundary->load_addr == info->dlpi_addr) {
246 found = 1;
247 break;
248 }
249 }
250 }
251 if (found) {
252 for (int i = 0; i < info->dlpi_phnum; ++i) {
253 if (info->dlpi_phdr[i].p_type == PT_GNU_RELRO) {
254 boundary->start_addr = boundary->load_addr + info->dlpi_phdr[i].p_vaddr;
255 boundary->end_addr = boundary->start_addr + info->dlpi_phdr[i].p_memsz;
256 boundary->found = 1;
257 return 1;
258 }
259 }
260 }
261 return 0;
262 }
263
264 static int mark_got_writable(struct link_map *lib) {
265 static ElfW(Addr) page_size = 0;
266 INIT_DYNAMIC(lib);
267 if (!got) return 0;
268
269 if (!page_size) page_size = gotcha_getpagesize();
270
271 ElfW(Addr) plt_got_size = MAX(rel_size, page_size);
272 if (plt_got_size % page_size) { // GCOVR_EXCL_START
273 plt_got_size += page_size - ((plt_got_size) % page_size);
274 } // GCOVR_EXCL_STOP
275 ElfW(Addr) plt_got_addr = BOUNDARY_BEFORE(got, (ElfW(Addr))page_size);
276 struct Boundary boundary;
277 boundary.l_name = lib->l_name;
278 boundary.load_addr = lib->l_addr;
279 boundary.found = 0;
280 dl_iterate_phdr(find_relro_boundary, &boundary);
281 int plt_got_written = 0;
282 if (boundary.found) {
283 ElfW(Addr) got_end = MAX(boundary.end_addr, page_size);
284 if (got_end % page_size) {
285 got_end += page_size - ((got_end) % page_size); // GCOVR_EXCL_LINE
286 }
287 ElfW(Addr) got_addr =
288 BOUNDARY_BEFORE(boundary.start_addr, (ElfW(Addr))page_size);
289 ElfW(Addr) got_size = got_end - got_addr;
290 /**
291 * The next two cases are to optimize mprotect calls to do both pages
292 * together if they align. We do not have such usecase yet and hence
293 * ignoring from coverage.
294 */
295 if (got_addr == plt_got_addr + plt_got_size) {
296 // Haven't seen a library till now where got_addr > plt_got_addr
297 // GCOVR_EXCL_START
298 debug_printf(3,
299 "Setting library %s GOT and PLT table "
300 "from %p to +%lu to writeable\n",
301 LIB_NAME(lib), (void *)plt_got_addr,
302 plt_got_size + got_size);
303 int res = gotcha_mprotect((void *)plt_got_addr, plt_got_size + got_size,
304 PROT_READ | PROT_WRITE | PROT_EXEC);
305 // mprotect returns -1 on an error
306 if (res == -1) {
307 error_printf(
308 "GOTCHA attempted to mark both GOT and PLT GOT tables as writable "
309 "and was unable to do so, "
310 "calls to wrapped functions may likely fail.\n");
311 }
312 plt_got_written = 1;
313 // GCOVR_EXCL_STOP
314 } else if (plt_got_addr == got_addr + got_size) {
315 // This is a more common case.
316 debug_printf(3,
317 "Setting library %s GOT and PLT table "
318 "from %p to +%lu to writeable\n",
319 LIB_NAME(lib), (void *)got_addr, plt_got_size + got_size);
320 int res = gotcha_mprotect((void *)got_addr, plt_got_size + got_size,
321 PROT_READ | PROT_WRITE | PROT_EXEC);
322 // mprotect returns -1 on an error
323 if (res == -1) { // GCOVR_EXCL_START
324 error_printf(
325 "GOTCHA attempted to mark both GOT and PLT GOT tables as writable "
326 "and was unable to do so, "
327 "calls to wrapped functions may likely fail.\n");
328 } // GCOVR_EXCL_STOP
329 plt_got_written = 1;
330 } else {
331 debug_printf(
332 3, "Setting library %s only GOT table from %p to +%lu to writeable\n",
333 LIB_NAME(lib), (void *)got_addr, got_size);
334 int res = gotcha_mprotect((void *)got_addr, got_size,
335 PROT_READ | PROT_WRITE | PROT_EXEC);
336 if (res == -1) { // GCOVR_EXCL_START
337 error_printf(
338 "GOTCHA attempted to mark the only GOT table as writable and was "
339 "unable to do so, "
340 "calls to wrapped functions may likely fail.\n");
341 } // GCOVR_EXCL_STOP
342 }
343 }
344 if (!plt_got_written) {
345 debug_printf(
346 3,
347 "Setting library %s only PLT GOT table from %p to +%lu to writeable\n",
348 LIB_NAME(lib), (void *)plt_got_addr, plt_got_size);
349 int res = gotcha_mprotect((void *)plt_got_addr, plt_got_size,
350 PROT_READ | PROT_WRITE | PROT_EXEC);
351 // mprotect returns -1 on an error
352 if (res == -1) { // GCOVR_EXCL_START
353 error_printf(
354 "GOTCHA attempted to mark the only PLT GOT table as writable and was "
355 "unable to do so, "
356 "calls to wrapped functions may likely fail.\n");
357 } // GCOVR_EXCL_STOP
358 }
359 return 0;
360 }
361
362 static int update_library_got(struct link_map *map,
363 hash_table_t *bindingtable) {
364 struct library_t *lib = get_library(map);
365 if (!lib) {
366 debug_printf(3, "Creating new library object for %s\n", LIB_NAME(map));
367 lib = add_library(map);
368 }
369
370 if (!libraryFilterFunc(map)) {
371 debug_printf(3, "Skipping library %s due to libraryFilterFunc\n",
372 LIB_NAME(map));
373 return 0;
374 }
375
376 if (lib->generation == current_generation) {
377 debug_printf(2,
378 "Library %s is already up-to-date. Skipping GOT rewriting\n",
379 LIB_NAME(map));
380 return 0;
381 }
382
383 if (!(lib->flags & LIB_GOT_MARKED_WRITEABLE)) {
384 mark_got_writable(map);
385 lib->flags |= LIB_GOT_MARKED_WRITEABLE;
386 }
387
388 FOR_EACH_PLTREL(map, update_lib_bindings, map, bindingtable);
389 lib->generation = current_generation;
390 return 0;
391 }
392
393 void update_all_library_gots(hash_table_t *bindings) {
394 struct link_map *lib_iter;
395 debug_printf(2, "Searching all callsites for %lu bindings\n",
396 (unsigned long)bindings->entry_count);
397 for (lib_iter = _r_debug.r_map; lib_iter != 0; lib_iter = lib_iter->l_next) {
398 update_library_got(lib_iter, bindings);
399 }
400 }
401
402 GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(
403 struct gotcha_binding_t *user_bindings, int num_actions,
404 const char *tool_name) {
405 int i, not_found = 0, new_bindings_count = 0;
406 tool_t *tool;
407 hash_table_t new_bindings;
408
409 gotcha_init();
410
411 debug_printf(1, "User called gotcha_wrap for tool %s with %d bindings\n",
412 tool_name, num_actions);
413 if (debug_level >= 3) {
414 for (i = 0; i < num_actions; i++) {
415 debug_bare_printf(3, "\t%d: %s will map to %p\n", i,
416 user_bindings[i].name,
417 user_bindings[i].wrapper_pointer);
418 }
419 }
420 debug_printf(3, "Initializing %d user binding entries to NULL\n",
421 num_actions);
422 for (i = 0; i < num_actions; i++) {
423 setBindingAddressPointer(&user_bindings[i], NULL);
424 }
425
426 if (!tool_name) tool_name = "[UNSPECIFIED]";
427 tool = get_tool(tool_name);
428 if (!tool) tool = create_tool(tool_name);
429 if (!tool) {
430 error_printf("Failed to create tool %s\n", tool_name); // GCOVR_EXCL_LINE
431 return GOTCHA_INTERNAL; // GCOVR_EXCL_LINE
432 }
433
434 current_generation++;
435 debug_printf(2, "Moved current_generation to %u in gotcha_wrap\n",
436 current_generation);
437
438 debug_printf(
439 2,
440 "Creating internal binding data structures and adding binding to tool\n");
441 binding_t *bindings = add_binding_to_tool(tool, user_bindings, num_actions);
442 if (!bindings) { // GCOVR_EXCL_START
443 error_printf("Failed to create bindings for tool %s\n", tool_name);
444 return GOTCHA_INTERNAL;
445 } // GCOVR_EXCL_STOP
446
447 debug_printf(2, "Processing %d bindings\n", num_actions);
448 for (i = 0; i < num_actions; i++) {
449 struct internal_binding_t *binding = bindings->internal_bindings + i;
450 int result = rewrite_wrapper_orders(binding);
451 if (result & RWO_NEED_LOOKUP) {
452 debug_printf(2, "Symbol %s needs lookup operation\n",
453 binding->user_binding->name);
454 int presult = prepare_symbol(binding);
455 if (presult == -1) {
456 debug_printf(
457 2,
458 "Stashing %s in notfound_binding table to re-lookup on dlopens\n",
459 binding->user_binding->name);
460 addto_hashtable(&notfound_binding_table,
461 (hash_key_t)binding->user_binding->name,
462 (hash_data_t)binding);
463 not_found++;
464 }
465 }
466 if (result & RWO_NEED_BINDING) {
467 debug_printf(2, "Symbol %s needs binding from application\n",
468 binding->user_binding->name);
469 if (!new_bindings_count) {
470 create_hashtable(&new_bindings, num_actions * 2, (hash_func_t)strhash,
471 (hash_cmp_t)gotcha_strcmp);
472 }
473 addto_hashtable(&new_bindings, (void *)binding->user_binding->name,
474 (void *)binding);
475 new_bindings_count++;
476 }
477 }
478
479 if (new_bindings_count) {
480 update_all_library_gots(&new_bindings);
481 destroy_hashtable(&new_bindings);
482 }
483
484 if (not_found) {
485 debug_printf(1, "Could not find bindings for %d / %d functions\n",
486 not_found, num_actions);
487 return GOTCHA_FUNCTION_NOT_FOUND;
488 }
489 debug_printf(1, "Gotcha wrap completed successfully\n");
490 return GOTCHA_SUCCESS;
491 }
492
493 static enum gotcha_error_t gotcha_configure_int(
494 const char *tool_name, enum gotcha_config_key_t configuration_key,
495 int value) {
496 tool_t *tool = get_tool(tool_name);
497 if (tool == NULL) {
498 tool = create_tool(tool_name);
499 }
500 if (configuration_key == GOTCHA_PRIORITY) {
501 tool->config.priority = value;
502 } else {
503 error_printf("Invalid property being configured on tool %s\n", tool_name);
504 return GOTCHA_INTERNAL;
505 }
506 return GOTCHA_SUCCESS;
507 }
508
509 GOTCHA_EXPORT enum gotcha_error_t gotcha_set_priority(const char *tool_name,
510 int value) {
511 gotcha_init();
512 debug_printf(1, "User called gotcha_set_priority(%s, %d)\n", tool_name,
513 value);
514 enum gotcha_error_t error_on_set =
515 gotcha_configure_int(tool_name, GOTCHA_PRIORITY, value);
516 if (error_on_set != GOTCHA_SUCCESS) {
517 return error_on_set; // GCOVR_EXCL_LINE
518 }
519 tool_t *tool_to_place = get_tool(tool_name);
520 if (!tool_to_place) { // will not happen as gotcha_configure_init creats tool
521 // if not exists
522 tool_to_place = create_tool(tool_name); // GCOVR_EXCL_LINE
523 }
524 remove_tool_from_list(tool_to_place);
525 reorder_tool(tool_to_place);
526 return GOTCHA_SUCCESS;
527 }
528
529 GOTCHA_EXPORT enum gotcha_error_t gotcha_get_priority(const char *tool_name,
530 int *priority) {
531 gotcha_init();
532 return get_configuration_value(tool_name, GOTCHA_PRIORITY, priority);
533 }
534
535 GOTCHA_EXPORT void *gotcha_get_wrappee(gotcha_wrappee_handle_t handle) {
536 return ((struct internal_binding_t *)handle)->wrappee_pointer;
537 }