Home | History | Annotate | Download | only in asan
      1 #include <elf.h>
      2 #include <fcntl.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <sys/stat.h>
      6 #include <sys/types.h>
      7 #include <unistd.h>
      8 
      9 #include <string>
     10 
     11 void usage(const char* me) {
     12   static const char* usage_s = "Usage:\n"
     13     "  %s /system/bin/app_process <args>\n"
     14     "or, better:\n"
     15     "  setprop wrap.<nicename> %s\n";
     16   fprintf(stderr, usage_s, me, me);
     17   exit(1);
     18 }
     19 
     20 void env_prepend(const char* name, const char* value, const char* delim) {
     21   const char* value_old = getenv(name);
     22   std::string value_new = value;
     23   if (value_old) {
     24     value_new += delim;
     25     value_new += value_old;
     26   }
     27   setenv(name, value_new.c_str(), 1);
     28 }
     29 
     30 bool elf_is_64bit(const char* path) {
     31   int fd = open(path, O_RDONLY);
     32   if (fd == -1) return false;
     33 
     34   const size_t kBufSize = EI_CLASS + 1;
     35   char buf[kBufSize];
     36   ssize_t sz = read(fd, buf, kBufSize);
     37   if (sz != kBufSize) {
     38     close(fd);
     39     return false;
     40   }
     41 
     42   if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1 ||
     43       buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
     44     return false;
     45 
     46   bool is_64bit = buf[EI_CLASS] == ELFCLASS64;
     47   close(fd);
     48   return is_64bit;
     49 }
     50 
     51 int main(int argc, char** argv) {
     52   if (argc < 2) {
     53     usage(argv[0]);
     54   }
     55   char** args = new char*[argc];
     56   // If we are wrapping app_process, replace it with app_process_asan.
     57   // TODO(eugenis): rewrite to <dirname>/asan/<basename>, if exists?
     58   if (strcmp(argv[1], "/system/bin/app_process") == 0) {
     59     args[0] = (char*)"/system/bin/asan/app_process";
     60   } else if (strcmp(argv[1], "/system/bin/app_process32") == 0) {
     61     args[0] = (char*)"/system/bin/asan/app_process32";
     62   } else if (strcmp(argv[1], "/system/bin/app_process64") == 0) {
     63     args[0] = (char*)"/system/bin/asan/app_process64";
     64   } else {
     65     args[0] = argv[1];
     66   }
     67 
     68   for (int i = 1; i < argc - 1; ++i)
     69     args[i] = argv[i + 1];
     70   args[argc - 1] = 0;
     71 
     72   if (elf_is_64bit(args[0])) {
     73     env_prepend("LD_LIBRARY_PATH", "/system/lib64/asan:/system/lib64", ":");
     74   } else {
     75     env_prepend("LD_LIBRARY_PATH", "/system/lib/asan:/system/lib", ":");
     76   }
     77   env_prepend("ASAN_OPTIONS", "allow_user_segv_handler=1", ",");
     78 
     79   printf("ASAN_OPTIONS: %s\n", getenv("ASAN_OPTIONS"));
     80   printf("LD_LIBRARY_PATH: %s\n", getenv("LD_LIBRARY_PATH"));
     81   for (int i = 0; i < argc - 1; ++i)
     82     printf("argv[%d] = %s\n", i, args[i]);
     83 
     84   execv(args[0], args);
     85 }
     86