blob: 11bd212caa84377ecd456fd6cd7a484f9255c378 [file] [log] [blame]
Alexey Samsonovff0ce7f2013-06-11 08:14:24 +00001//===-- sanitizer_symbolizer_posix_libcdep.cc -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12// POSIX-specific implementation of symbolizer parts.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_platform.h"
16#if SANITIZER_POSIX
17#include "sanitizer_common.h"
18#include "sanitizer_internal_defs.h"
19#include "sanitizer_symbolizer.h"
20
21#include <errno.h>
22#include <sys/wait.h>
23#include <unistd.h>
24
25namespace __sanitizer {
26
Alexey Samsonov52d08d82013-06-28 12:13:31 +000027#if defined(__x86_64__)
28static const char* const kSymbolizerArch = "--default-arch=x86_64";
29#elif defined(__i386__)
30static const char* const kSymbolizerArch = "--default-arch=i386";
31#else
32static const char* const kSymbolizerArch = "";
33#endif
34
Alexey Samsonovff0ce7f2013-06-11 08:14:24 +000035bool 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");
39 return false;
40 }
41
42 int *infd = NULL;
43 int *outfd = NULL;
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).
50 int sock_pair[5][2];
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]);
56 }
57 Report("WARNING: Can't create a socket pair to start "
58 "external symbolizer (errno: %d)\n", errno);
59 return false;
60 } else if (sock_pair[i][0] > 2 && sock_pair[i][1] > 2) {
61 if (infd == NULL) {
62 infd = sock_pair[i];
63 } else {
64 outfd = sock_pair[i];
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]);
69 }
70 break;
71 }
72 }
73 }
74 CHECK(infd);
75 CHECK(outfd);
76
77 int pid = fork();
78 if (pid == -1) {
79 // Fork() failed.
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);
86 return false;
87 } else if (pid == 0) {
88 // Child subprocess.
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--)
98 internal_close(fd);
Alexey Samsonov52d08d82013-06-28 12:13:31 +000099 execl(path_to_symbolizer, path_to_symbolizer, kSymbolizerArch, (char*)0);
Alexey Samsonovff0ce7f2013-06-11 08:14:24 +0000100 internal__exit(1);
101 }
102
103 // Continue execution in parent process.
104 internal_close(outfd[0]);
105 internal_close(infd[1]);
106 *input_fd = infd[0];
107 *output_fd = outfd[1];
108
109 // Check that symbolizer subprocess started successfully.
110 int pid_status;
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");
116 return false;
117 }
118
119 return true;
120}
121
122
123} // namespace __sanitizer
124
125#endif // SANITIZER_POSIX