1 /* -*- mode: C; c-basic-offset: 3; -*- */ 2 3 /*--------------------------------------------------------------------*/ 4 /*--- Launching valgrind m_launcher.c ---*/ 5 /*--------------------------------------------------------------------*/ 6 7 /* 8 This file is part of Valgrind, a dynamic binary instrumentation 9 framework. 10 11 Copyright (C) 2000-2017 Julian Seward 12 jseward (at) acm.org 13 14 This program is free software; you can redistribute it and/or 15 modify it under the terms of the GNU General Public License as 16 published by the Free Software Foundation; either version 2 of the 17 License, or (at your option) any later version. 18 19 This program is distributed in the hope that it will be useful, but 20 WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 General Public License for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program; if not, write to the Free Software 26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 27 02111-1307, USA. 28 29 The GNU General Public License is contained in the file COPYING. 30 */ 31 32 /* Note: this is a "normal" program and not part of Valgrind proper, 33 and so it doesn't have to conform to Valgrind's arcane rules on 34 no-glibc-usage etc. */ 35 36 /* Include valgrind headers before system headers to avoid problems 37 with the system headers #defining things which are used as names 38 of structure members in vki headers. */ 39 40 #include "pub_core_debuglog.h" 41 #include "pub_core_vki.h" // Avoids warnings from 42 // pub_core_libcfile.h 43 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER 44 #include "pub_core_ume.h" 45 46 #include <assert.h> 47 #include <ctype.h> 48 #include <elf.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #ifndef EM_X86_64 57 #define EM_X86_64 62 // elf.h doesn't define this on some older systems 58 #endif 59 60 #ifndef EM_AARCH64 61 #define EM_AARCH64 183 // ditto 62 #endif 63 64 #ifndef EM_PPC64 65 #define EM_PPC64 21 // ditto 66 #endif 67 68 /* Report fatal errors */ 69 __attribute__((noreturn)) 70 static void barf ( const char *format, ... ) 71 { 72 va_list vargs; 73 74 va_start(vargs, format); 75 fprintf(stderr, "valgrind: Cannot continue: "); 76 vfprintf(stderr, format, vargs); 77 fprintf(stderr, "\n"); 78 va_end(vargs); 79 80 exit(1); 81 /*NOTREACHED*/ 82 assert(0); 83 } 84 85 /* Search the path for the client program */ 86 static const char *find_client(const char *clientname) 87 { 88 char *fullname; 89 const char *path = getenv("PATH"); 90 const char *colon; 91 92 assert(clientname != NULL); 93 94 if (path == NULL) return clientname; 95 96 /* Make the size of the FULLNAME buffer large enough. */ 97 unsigned need = strlen(path) + strlen("/") + strlen(clientname) + 1; 98 99 fullname = malloc(need); 100 if (fullname == NULL) 101 barf("malloc of fullname failed."); 102 103 while (path) 104 { 105 if ((colon = strchr(path, ':')) == NULL) 106 { 107 strcpy(fullname, path); 108 path = NULL; 109 } 110 else 111 { 112 strncpy(fullname, path, colon - path); 113 fullname[colon - path] = '\0'; 114 path = colon + 1; 115 } 116 117 strcat(fullname, "/"); 118 strcat(fullname, clientname); 119 120 if (access(fullname, R_OK|X_OK) == 0) 121 return fullname; 122 } 123 free(fullname); 124 125 return clientname; 126 } 127 128 /* Examine the client and work out which platform it is for */ 129 static const char *select_platform(const char *clientname) 130 { 131 int fd; 132 union { 133 char c[4096]; 134 Elf32_Ehdr ehdr32; 135 Elf64_Ehdr ehdr64; 136 } header; 137 ssize_t n_bytes; 138 const char *platform = NULL; 139 140 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname); 141 142 if (strchr(clientname, '/') == NULL) 143 clientname = find_client(clientname); 144 145 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname); 146 147 if ((fd = open(clientname, O_RDONLY)) < 0) 148 return NULL; 149 // barf("open(%s): %s", clientname, strerror(errno)); 150 151 VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname); 152 153 n_bytes = read(fd, header.c, sizeof(header)); 154 close(fd); 155 if (n_bytes < 2) { 156 return NULL; 157 } 158 159 VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n", 160 (long int)n_bytes, clientname); 161 162 if (header.c[0] == '#' && header.c[1] == '!') { 163 int i = 2; 164 165 STATIC_ASSERT(VKI_BINPRM_BUF_SIZE < sizeof header); 166 if (n_bytes > VKI_BINPRM_BUF_SIZE) 167 n_bytes = VKI_BINPRM_BUF_SIZE - 1; 168 header.c[n_bytes] = '\0'; 169 char *eol = strchr(header.c, '\n'); 170 if (eol != NULL) 171 *eol = '\0'; 172 173 // Skip whitespace. 174 while (header.c[i] == ' '|| header.c[i] == '\t') 175 i++; 176 177 // Get the interpreter name. 178 const char *interp = header.c + i; 179 180 if (header.c[i] == '\0') { 181 // No interpreter was found; fall back to default shell 182 # if defined(VGPV_arm_linux_android) \ 183 || defined(VGPV_x86_linux_android) \ 184 || defined(VGPV_mips32_linux_android) \ 185 || defined(VGPV_arm64_linux_android) 186 interp = "/system/bin/sh"; 187 # else 188 interp = "/bin/sh"; 189 # endif 190 } else { 191 while (header.c[i]) { 192 if (header.c[i] == ' ' || header.c[i] == '\t') break; 193 i++; 194 } 195 header.c[i] = '\0'; 196 } 197 198 platform = select_platform(interp); 199 200 } else if (n_bytes >= SELFMAG && memcmp(header.c, ELFMAG, SELFMAG) == 0) { 201 202 if (n_bytes >= sizeof(Elf32_Ehdr) && header.c[EI_CLASS] == ELFCLASS32) { 203 204 if (header.c[EI_DATA] == ELFDATA2LSB) { 205 # if defined(VGO_solaris) 206 if (header.ehdr32.e_machine == EM_386 && 207 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 208 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) { 209 platform = "x86-solaris"; 210 } 211 else 212 # endif 213 if (header.ehdr32.e_machine == EM_386 && 214 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 215 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 216 platform = "x86-linux"; 217 } 218 else 219 if (header.ehdr32.e_machine == EM_ARM && 220 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 221 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 222 platform = "arm-linux"; 223 } 224 else 225 if (header.ehdr32.e_machine == EM_MIPS && 226 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 227 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 228 platform = "mips32-linux"; 229 } 230 } 231 else if (header.c[EI_DATA] == ELFDATA2MSB) { 232 if (header.ehdr32.e_machine == EM_PPC && 233 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 234 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 235 platform = "ppc32-linux"; 236 } 237 else 238 if (header.ehdr32.e_machine == EM_MIPS && 239 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV || 240 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 241 platform = "mips32-linux"; 242 } 243 } 244 245 } else if (n_bytes >= sizeof(Elf64_Ehdr) && header.c[EI_CLASS] == ELFCLASS64) { 246 247 if (header.c[EI_DATA] == ELFDATA2LSB) { 248 # if defined(VGO_solaris) 249 if (header.ehdr64.e_machine == EM_X86_64 && 250 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 251 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) { 252 platform = "amd64-solaris"; 253 } 254 else 255 # endif 256 if (header.ehdr64.e_machine == EM_X86_64 && 257 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 258 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 259 platform = "amd64-linux"; 260 } else if (header.ehdr64.e_machine == EM_MIPS && 261 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 262 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 263 platform = "mips64-linux"; 264 } else if (header.ehdr64.e_machine == EM_AARCH64 && 265 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 266 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 267 platform = "arm64-linux"; 268 } else if (header.ehdr64.e_machine == EM_PPC64 && 269 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 270 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 271 platform = "ppc64le-linux"; 272 } 273 } else if (header.c[EI_DATA] == ELFDATA2MSB) { 274 # if !defined(VGPV_arm_linux_android) \ 275 && !defined(VGPV_x86_linux_android) \ 276 && !defined(VGPV_mips32_linux_android) \ 277 && !defined(VGPV_arm64_linux_android) 278 if (header.ehdr64.e_machine == EM_PPC64 && 279 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 280 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 281 platform = "ppc64be-linux"; 282 } 283 else 284 if (header.ehdr64.e_machine == EM_S390 && 285 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 286 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 287 platform = "s390x-linux"; 288 } else if (header.ehdr64.e_machine == EM_MIPS && 289 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV || 290 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) { 291 platform = "mips64-linux"; 292 } 293 # endif 294 } 295 } 296 } 297 298 VG_(debugLog)(2, "launcher", "selected platform '%s'\n", 299 platform ? platform : "unknown"); 300 301 return platform; 302 } 303 304 /* Where we expect to find all our aux files */ 305 static const char *valgrind_lib = VG_LIBDIR; 306 307 int main(int argc, char** argv, char** envp) 308 { 309 int i, j, loglevel, r; 310 const char *toolname = NULL; 311 const char *clientname = NULL; 312 const char *platform; 313 const char *default_platform; 314 const char *cp; 315 const char *linkname; 316 char *toolfile; 317 const char *launcher_name; 318 char* new_line; 319 char** new_env; 320 321 /* Start the debugging-log system ASAP. First find out how many 322 "-d"s were specified. This is a pre-scan of the command line. 323 At the same time, look for the tool name. */ 324 loglevel = 0; 325 for (i = 1; i < argc; i++) { 326 if (argv[i][0] != '-') { 327 clientname = argv[i]; 328 break; 329 } 330 if (0 == strcmp(argv[i], "--")) { 331 if (i+1 < argc) 332 clientname = argv[i+1]; 333 break; 334 } 335 if (0 == strcmp(argv[i], "-d")) 336 loglevel++; 337 if (0 == strncmp(argv[i], "--tool=", 7)) 338 toolname = argv[i] + 7; 339 } 340 341 /* ... and start the debug logger. Now we can safely emit logging 342 messages all through startup. */ 343 VG_(debugLog_startup)(loglevel, "Stage 1"); 344 345 /* Make sure we know which tool we're using */ 346 if (toolname) { 347 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname); 348 } else { 349 VG_(debugLog)(1, "launcher", 350 "no tool requested, defaulting to 'memcheck'\n"); 351 toolname = "memcheck"; 352 } 353 354 /* Select a platform to use if we can't decide that by looking at 355 the executable (eg because it's a shell script). VG_PLATFORM is the 356 default_platform. Its value is defined in coregrind/Makefile.am and 357 typically it is the primary build target. Unless the primary build 358 target is not built is not built in which case VG_PLATFORM is the 359 secondary build target. */ 360 # if defined(VGO_linux) 361 if ((0==strcmp(VG_PLATFORM,"x86-linux")) || 362 (0==strcmp(VG_PLATFORM,"amd64-linux")) || 363 (0==strcmp(VG_PLATFORM,"ppc32-linux")) || 364 (0==strcmp(VG_PLATFORM,"ppc64be-linux")) || 365 (0==strcmp(VG_PLATFORM,"ppc64le-linux")) || 366 (0==strcmp(VG_PLATFORM,"arm-linux")) || 367 (0==strcmp(VG_PLATFORM,"arm64-linux")) || 368 (0==strcmp(VG_PLATFORM,"s390x-linux")) || 369 (0==strcmp(VG_PLATFORM,"mips32-linux")) || 370 (0==strcmp(VG_PLATFORM,"mips64-linux"))) 371 default_platform = VG_PLATFORM; 372 # elif defined(VGO_solaris) 373 if ((0==strcmp(VG_PLATFORM,"x86-solaris")) || 374 (0==strcmp(VG_PLATFORM,"amd64-solaris"))) 375 default_platform = SOLARIS_LAUNCHER_DEFAULT_PLATFORM; 376 # else 377 # error Unknown OS 378 # endif 379 else 380 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM); 381 382 /* Work out what platform to use, or use the default platform if 383 not possible. */ 384 if (clientname == NULL) { 385 VG_(debugLog)(1, "launcher", 386 "no client specified, defaulting platform to '%s'\n", 387 default_platform); 388 platform = default_platform; 389 } else if ((platform = select_platform(clientname)) != NULL) { 390 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform); 391 } else { 392 VG_(debugLog)(1, "launcher", 393 "no platform detected, defaulting platform to '%s'\n", 394 default_platform); 395 platform = default_platform; 396 } 397 398 /* Figure out the name of this executable (viz, the launcher), so 399 we can tell stage2. stage2 will use the name for recursive 400 invocations of valgrind on child processes. */ 401 # if defined(VGO_linux) 402 linkname = "/proc/self/exe"; 403 # elif defined(VGO_solaris) 404 linkname = "/proc/self/path/a.out"; 405 # else 406 # error Unknown OS 407 # endif 408 unsigned bufsiz = 0; 409 char *buf = NULL; 410 411 while (42) { 412 bufsiz += 500; 413 buf = realloc(buf, bufsiz); 414 if (buf == NULL) 415 barf("realloc of buf failed."); 416 r = readlink(linkname, buf, bufsiz); 417 if (r == -1) { 418 /* If /proc/self/exe (/proc/self/path/a.out) can't be followed, don't 419 give up. Instead continue with an empty string for VALGRIND_LAUNCHER. 420 In the sys_execve wrapper, this is tested, and if found to be empty, 421 fail the execve. */ 422 fprintf(stderr, "valgrind: warning (non-fatal): " 423 "readlink(\"%s\") failed.\n", linkname); 424 fprintf(stderr, "valgrind: continuing, however --trace-children=yes " 425 "will not work.\n"); 426 launcher_name = ""; 427 break; 428 } 429 if (r == bufsiz) continue; // buffer to small; retry 430 431 assert(r < bufsiz); // paranoia 432 433 buf[r] = '\0'; 434 launcher_name = buf; 435 break; 436 } 437 438 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */ 439 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 440 + strlen(launcher_name) + 1); 441 if (new_line == NULL) 442 barf("malloc of new_line failed."); 443 strcpy(new_line, VALGRIND_LAUNCHER); 444 strcat(new_line, "="); 445 strcat(new_line, launcher_name); 446 447 for (j = 0; envp[j]; j++) 448 ; 449 new_env = malloc((j+2) * sizeof(char*)); 450 if (new_env == NULL) 451 barf("malloc of new_env failed."); 452 for (i = 0; i < j; i++) 453 new_env[i] = envp[i]; 454 new_env[i++] = new_line; 455 new_env[i++] = NULL; 456 assert(i == j+2); 457 458 /* Establish the correct VALGRIND_LIB. */ 459 cp = getenv(VALGRIND_LIB); 460 461 if (cp != NULL) 462 valgrind_lib = cp; 463 464 /* Build the stage2 invocation, and execve it. Bye! */ 465 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3); 466 if (toolfile == NULL) 467 barf("malloc of toolfile failed."); 468 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform); 469 470 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile); 471 472 execve(toolfile, argv, new_env); 473 474 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n", 475 toolname, platform, strerror(errno)); 476 477 exit(1); 478 } 479