1 //===-- sanitizer_symbolizer_linux.cc -------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // Linux-specific implementation of symbolizer parts.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common.h"
14 #include "sanitizer_internal_defs.h"
15 #include "sanitizer_libc.h"
16 #include "sanitizer_placement_new.h"
17 #include "sanitizer_symbolizer.h"
22 #include <sys/socket.h>
23 #include <sys/types.h>
27 #if !defined(__ANDROID__) && !defined(ANDROID)
31 namespace __sanitizer {
33 static const int kSymbolizerStartupTimeMillis = 10;
35 bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
36 int *input_fd, int *output_fd) {
37 if (!FileExists(path_to_symbolizer)) {
38 Report("WARNING: invalid path to external symbolizer!\n");
44 // The client program may close its stdin and/or stdout and/or stderr
45 // thus allowing socketpair to reuse file descriptors 0, 1 or 2.
46 // In this case the communication between the forked processes may be
47 // broken if either the parent or the child tries to close or duplicate
48 // these descriptors. The loop below produces two pairs of file
49 // descriptors, each greater than 2 (stderr).
51 for (int i = 0; i < 5; i++) {
52 if (pipe(sock_pair[i]) == -1) {
53 for (int j = 0; j < i; j++) {
54 internal_close(sock_pair[j][0]);
55 internal_close(sock_pair[j][1]);
57 Report("WARNING: Can't create a socket pair to start "
58 "external symbolizer (errno: %d)\n", errno);
60 } else if (sock_pair[i][0] > 2 && sock_pair[i][1] > 2) {
65 for (int j = 0; j < i; j++) {
66 if (sock_pair[j] == infd) continue;
67 internal_close(sock_pair[j][0]);
68 internal_close(sock_pair[j][1]);
80 internal_close(infd[0]);
81 internal_close(infd[1]);
82 internal_close(outfd[0]);
83 internal_close(outfd[1]);
84 Report("WARNING: failed to fork external symbolizer "
85 " (errno: %d)\n", errno);
87 } else if (pid == 0) {
89 internal_close(STDOUT_FILENO);
90 internal_close(STDIN_FILENO);
91 internal_dup2(outfd[0], STDIN_FILENO);
92 internal_dup2(infd[1], STDOUT_FILENO);
93 internal_close(outfd[0]);
94 internal_close(outfd[1]);
95 internal_close(infd[0]);
96 internal_close(infd[1]);
97 for (int fd = getdtablesize(); fd > 2; fd--)
99 execl(path_to_symbolizer, path_to_symbolizer, (char*)0);
103 // Continue execution in parent process.
104 internal_close(outfd[0]);
105 internal_close(infd[1]);
107 *output_fd = outfd[1];
109 // Check that symbolizer subprocess started successfully.
111 SleepForMillis(kSymbolizerStartupTimeMillis);
112 int exited_pid = waitpid(pid, &pid_status, WNOHANG);
113 if (exited_pid != 0) {
114 // Either waitpid failed, or child has already exited.
115 Report("WARNING: external symbolizer didn't start up correctly!\n");
122 #if defined(__ANDROID__) || defined(ANDROID)
123 uptr GetListOfModules(LoadedModule *modules, uptr max_modules) {
127 typedef ElfW(Phdr) Elf_Phdr;
129 struct DlIteratePhdrData {
130 LoadedModule *modules;
135 static const uptr kMaxPathLength = 512;
137 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
138 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
139 if (data->current_n == data->max_n)
141 InternalScopedBuffer<char> module_name(kMaxPathLength);
142 module_name.data()[0] = '\0';
143 if (data->current_n == 0) {
144 // First module is the binary itself.
145 uptr module_name_len = internal_readlink(
146 "/proc/self/exe", module_name.data(), module_name.size());
147 CHECK_NE(module_name_len, (uptr)-1);
148 CHECK_LT(module_name_len, module_name.size());
149 module_name[module_name_len] = '\0';
150 } else if (info->dlpi_name) {
151 internal_strncpy(module_name.data(), info->dlpi_name, module_name.size());
153 if (module_name.data()[0] == '\0')
155 void *mem = &data->modules[data->current_n];
156 LoadedModule *cur_module = new(mem) LoadedModule(module_name.data(),
159 for (int i = 0; i < info->dlpi_phnum; i++) {
160 const Elf_Phdr *phdr = &info->dlpi_phdr[i];
161 if (phdr->p_type == PT_LOAD) {
162 uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
163 uptr cur_end = cur_beg + phdr->p_memsz;
164 cur_module->addAddressRange(cur_beg, cur_end);
170 uptr GetListOfModules(LoadedModule *modules, uptr max_modules) {
172 DlIteratePhdrData data = {modules, 0, max_modules};
173 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
174 return data.current_n;
178 } // namespace __sanitizer