tools/reorder/mapaddrs.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6
michael@0 7 A program that reverse-maps a binary stream of address references to
michael@0 8 function names.
michael@0 9
michael@0 10 */
michael@0 11
michael@0 12 #include <fstream>
michael@0 13 #include <unistd.h>
michael@0 14 #include <fcntl.h>
michael@0 15 #include <stdio.h>
michael@0 16
michael@0 17 #include "elf_symbol_table.h"
michael@0 18
michael@0 19 #define _GNU_SOURCE
michael@0 20 #include <getopt.h>
michael@0 21
michael@0 22 static int
michael@0 23 map_addrs(int fd, elf_symbol_table &table)
michael@0 24 {
michael@0 25 // Read the binary addresses from stdin.
michael@0 26 unsigned int buf[128];
michael@0 27 ssize_t cb;
michael@0 28
michael@0 29 while ((cb = read(fd, buf, sizeof buf)) > 0) {
michael@0 30 if (cb % sizeof buf[0])
michael@0 31 fprintf(stderr, "unaligned read\n");
michael@0 32
michael@0 33 unsigned int *addr = buf;
michael@0 34 unsigned int *limit = buf + (cb / 4);
michael@0 35
michael@0 36 for (; addr < limit; ++addr) {
michael@0 37 const Elf32_Sym *sym = table.lookup(*addr);
michael@0 38 if (sym)
michael@0 39 cout << table.get_symbol_name(sym) << endl;
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 return 0;
michael@0 44 }
michael@0 45
michael@0 46 static struct option long_options[] = {
michael@0 47 { "exe", required_argument, 0, 'e' },
michael@0 48 { 0, 0, 0, 0 }
michael@0 49 };
michael@0 50
michael@0 51 static void
michael@0 52 usage()
michael@0 53 {
michael@0 54 cerr << "usage: mapaddrs --exe=exe file ..." << endl;
michael@0 55 }
michael@0 56
michael@0 57 int
michael@0 58 main(int argc, char *argv[])
michael@0 59 {
michael@0 60 elf_symbol_table table;
michael@0 61
michael@0 62 while (1) {
michael@0 63 int option_index = 0;
michael@0 64 int c = getopt_long(argc, argv, "e:", long_options, &option_index);
michael@0 65
michael@0 66 if (c < 0)
michael@0 67 break;
michael@0 68
michael@0 69 switch (c) {
michael@0 70 case 'e':
michael@0 71 table.init(optarg);
michael@0 72 break;
michael@0 73
michael@0 74 default:
michael@0 75 usage();
michael@0 76 return 1;
michael@0 77 }
michael@0 78 }
michael@0 79
michael@0 80 if (optind >= argc) {
michael@0 81 map_addrs(STDIN_FILENO, table);
michael@0 82 }
michael@0 83 else {
michael@0 84 do {
michael@0 85 int fd = open(argv[optind], O_RDONLY);
michael@0 86 if (fd < 0) {
michael@0 87 perror(argv[optind]);
michael@0 88 return 1;
michael@0 89 }
michael@0 90
michael@0 91 map_addrs(fd, table);
michael@0 92 close(fd);
michael@0 93 } while (++optind < argc);
michael@0 94 }
michael@0 95
michael@0 96 return 0;
michael@0 97 }

mercurial