1 /*
   2  * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #if defined(LINUX) || defined(__APPLE__)
  26 #include <unistd.h>
  27 #include <fcntl.h>
  28 #include <string.h>
  29 #include <stdlib.h>
  30 #include <stddef.h>
  31 #ifdef LINUX
  32 #include <elf.h>
  33 #include <link.h>
  34 #include "proc_service.h"
  35 #include "salibelf.h"
  36 #endif
  37 #include "libproc_impl.h"
  38 #include "cds.h"
  39 
  40 #ifdef __APPLE__
  41 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
  42 #endif
  43 
  44 #ifdef LINUX
  45 // I have no idea why this function is called ps_pread() on macos but ps_pdread on linux.
  46 #define ps_pread ps_pdread
  47 #endif
  48 
  49 // Common code shared between linux/native/libsaproc/ps_core.c and macosx/native/libsaproc/ps_core.c
  50 
  51 //----------------------------------------------------------------------
  52 // ps_prochandle cleanup helper functions
  53 
  54 // close all file descriptors
  55 static void close_files(struct ps_prochandle* ph) {
  56   lib_info* lib = NULL;
  57 
  58   // close core file descriptor
  59   if (ph->core->core_fd >= 0)
  60     close(ph->core->core_fd);
  61 
  62   // close exec file descriptor
  63   if (ph->core->exec_fd >= 0)
  64     close(ph->core->exec_fd);
  65 
  66   // close interp file descriptor
  67   if (ph->core->interp_fd >= 0)
  68     close(ph->core->interp_fd);
  69 
  70   // close class share archive file
  71   if (ph->core->classes_jsa_fd >= 0)
  72     close(ph->core->classes_jsa_fd);
  73 
  74   // close all library file descriptors
  75   lib = ph->libs;
  76   while (lib) {
  77     int fd = lib->fd;
  78     if (fd >= 0 && fd != ph->core->exec_fd) {
  79       close(fd);
  80     }
  81     lib = lib->next;
  82   }
  83 }
  84 
  85 // clean all map_info stuff
  86 static void destroy_map_info(struct ps_prochandle* ph) {
  87   map_info* map = ph->core->maps;
  88   while (map) {
  89     map_info* next = map->next;
  90     free(map);
  91     map = next;
  92   }
  93 
  94   if (ph->core->map_array) {
  95     free(ph->core->map_array);
  96   }
  97 
  98   // Part of the class sharing workaround
  99   map = ph->core->class_share_maps;
 100   while (map) {
 101     map_info* next = map->next;
 102     free(map);
 103     map = next;
 104   }
 105 }
 106 
 107 // ps_prochandle operations
 108 void core_release(struct ps_prochandle* ph) {
 109   if (ph->core) {
 110     close_files(ph);
 111     destroy_map_info(ph);
 112     free(ph->core);
 113   }
 114 }
 115 
 116 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
 117   map_info* map;
 118   if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
 119     print_debug("can't allocate memory for map_info\n");
 120     return NULL;
 121   }
 122 
 123   // initialize map
 124   map->fd     = fd;
 125   map->offset = offset;
 126   map->vaddr  = vaddr;
 127   map->memsz  = memsz;
 128   return map;
 129 }
 130 
 131 // add map info with given fd, offset, vaddr and memsz
 132 map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
 133                        uintptr_t vaddr, size_t memsz) {
 134   map_info* map;
 135   if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
 136     return NULL;
 137   }
 138 
 139   // add this to map list
 140   map->next  = ph->core->maps;
 141   ph->core->maps   = map;
 142   ph->core->num_maps++;
 143 
 144   return map;
 145 }
 146 
 147 // Part of the class sharing workaround
 148 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
 149                              uintptr_t vaddr, size_t memsz) {
 150   map_info* map;
 151   if ((map = allocate_init_map(ph->core->classes_jsa_fd,
 152                                offset, vaddr, memsz)) == NULL) {
 153     return NULL;
 154   }
 155 
 156   map->next = ph->core->class_share_maps;
 157   ph->core->class_share_maps = map;
 158   return map;
 159 }
 160 
 161 // Return the map_info for the given virtual address.  We keep a sorted
 162 // array of pointers in ph->map_array, so we can binary search.
 163 map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
 164   int mid, lo = 0, hi = ph->core->num_maps - 1;
 165   map_info *mp;
 166 
 167   while (hi - lo > 1) {
 168     mid = (lo + hi) / 2;
 169     if (addr >= ph->core->map_array[mid]->vaddr) {
 170       lo = mid;
 171     } else {
 172       hi = mid;
 173     }
 174   }
 175 
 176   if (addr < ph->core->map_array[hi]->vaddr) {
 177     mp = ph->core->map_array[lo];
 178   } else {
 179     mp = ph->core->map_array[hi];
 180   }
 181 
 182   if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
 183     return (mp);
 184   }
 185 
 186 
 187   // Part of the class sharing workaround
 188   // Unfortunately, we have no way of detecting -Xshare state.
 189   // Check out the share maps atlast, if we don't find anywhere.
 190   // This is done this way so to avoid reading share pages
 191   // ahead of other normal maps. For eg. with -Xshare:off we don't
 192   // want to prefer class sharing data to data from core.
 193   mp = ph->core->class_share_maps;
 194   if (mp) {
 195     print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
 196   }
 197   while (mp) {
 198     if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
 199       print_debug("located map_info at 0x%lx from class share maps\n", addr);
 200       return (mp);
 201     }
 202     mp = mp->next;
 203   }
 204 
 205   print_debug("can't locate map_info at 0x%lx\n", addr);
 206   return (NULL);
 207 }
 208 
 209 //---------------------------------------------------------------
 210 // Part of the class sharing workaround:
 211 //
 212 // With class sharing, pages are mapped from classes.jsa file.
 213 // The read-only class sharing pages are mapped as MAP_SHARED,
 214 // PROT_READ pages. These pages are not dumped into core dump.
 215 // With this workaround, these pages are read from classes.jsa.
 216 
 217 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
 218   jboolean i;
 219   if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
 220     *pvalue = i;
 221     return true;
 222   } else {
 223     return false;
 224   }
 225 }
 226 
 227 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
 228   uintptr_t uip;
 229   if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
 230     *pvalue = uip;
 231     return true;
 232   } else {
 233     return false;
 234   }
 235 }
 236 
 237 // used to read strings from debuggee
 238 bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
 239   size_t i = 0;
 240   char  c = ' ';
 241 
 242   while (c != '\0') {
 243     if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
 244       return false;
 245     }
 246     if (i < size - 1) {
 247       buf[i] = c;
 248     } else {
 249       // smaller buffer
 250       return false;
 251     }
 252     i++; addr++;
 253   }
 254   buf[i] = '\0';
 255   return true;
 256 }
 257 
 258 #ifdef LINUX
 259 // mangled name of Arguments::SharedArchivePath
 260 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
 261 #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
 262 #define SHARED_BASE_ADDRESS_SYM "SharedBaseAddress"
 263 #define LIBJVM_NAME "/libjvm.so"
 264 #endif
 265 
 266 #ifdef __APPLE__
 267 // mangled name of Arguments::SharedArchivePath
 268 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
 269 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
 270 #define SHARED_BASE_ADDRESS_SYM "_SharedBaseAddress"
 271 #define LIBJVM_NAME "/libjvm.dylib"
 272 #endif
 273 
 274 bool init_classsharing_workaround(struct ps_prochandle* ph) {
 275   lib_info* lib = ph->libs;
 276   while (lib != NULL) {
 277     // we are iterating over shared objects from the core dump. look for
 278     // libjvm.so.
 279     const char *jvm_name = 0;
 280     if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
 281       char classes_jsa[PATH_MAX];
 282       CDSFileMapHeaderBase header;
 283       int fd = -1;
 284       uintptr_t useSharedSpacesAddr = 0;
 285       uintptr_t sharedBaseAddressAddr = 0, sharedBaseAddress = 0;
 286       uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
 287       jboolean useSharedSpaces = 0;
 288       int m;
 289       size_t n;
 290 
 291       memset(classes_jsa, 0, sizeof(classes_jsa));
 292       jvm_name = lib->name;
 293       useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
 294       if (useSharedSpacesAddr == 0) {
 295         print_debug("can't lookup 'UseSharedSpaces' flag\n");
 296         return false;
 297       }
 298 
 299       // Hotspot vm types are not exported to build this library. So
 300       // using equivalent type jboolean to read the value of
 301       // UseSharedSpaces which is same as hotspot type "bool".
 302       if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
 303         print_debug("can't read the value of 'UseSharedSpaces' flag\n");
 304         return false;
 305       }
 306 
 307       if ((int)useSharedSpaces == 0) {
 308         print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
 309         return true;
 310       }
 311 
 312       sharedBaseAddressAddr = lookup_symbol(ph, jvm_name, SHARED_BASE_ADDRESS_SYM);
 313       if (sharedBaseAddressAddr == 0) {
 314         print_debug("can't lookup 'SharedBaseAddress' flag\n");
 315         return false;
 316       }
 317 
 318       if (read_pointer(ph, sharedBaseAddressAddr, &sharedBaseAddress) != true) {
 319         print_debug("can't read the value of 'SharedBaseAddress' flag\n");
 320         return false;
 321       }
 322 
 323       sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
 324       if (sharedArchivePathAddrAddr == 0) {
 325         print_debug("can't lookup shared archive path symbol\n");
 326         return false;
 327       }
 328 
 329       if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
 330         print_debug("can't read shared archive path pointer\n");
 331         return false;
 332       }
 333 
 334       if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
 335         print_debug("can't read shared archive path value\n");
 336         return false;
 337       }
 338 
 339       print_debug("looking for %s\n", classes_jsa);
 340       // open the class sharing archive file
 341       fd = pathmap_open(classes_jsa);
 342       if (fd < 0) {
 343         print_debug("can't open %s!\n", classes_jsa);
 344         ph->core->classes_jsa_fd = -1;
 345         return false;
 346       } else {
 347         print_debug("opened %s\n", classes_jsa);
 348       }
 349 
 350       // read CDSFileMapHeaderBase from the file
 351       memset(&header, 0, sizeof(CDSFileMapHeaderBase));
 352       if ((n = read(fd, &header, sizeof(CDSFileMapHeaderBase)))
 353            != sizeof(CDSFileMapHeaderBase)) {
 354         print_debug("can't read shared archive file map header from %s\n", classes_jsa);
 355         close(fd);
 356         return false;
 357       }
 358 
 359       // check file magic
 360       if (header._magic != CDS_ARCHIVE_MAGIC) {
 361         print_debug("%s has bad shared archive file magic number 0x%x, expecting 0x%x\n",
 362                     classes_jsa, header._magic, CDS_ARCHIVE_MAGIC);
 363         close(fd);
 364         return false;
 365       }
 366 
 367       // check version
 368       if (header._version != CURRENT_CDS_ARCHIVE_VERSION) {
 369         print_debug("%s has wrong shared archive file version %d, expecting %d\n",
 370                      classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION);
 371         close(fd);
 372         return false;
 373       }
 374 
 375       ph->core->classes_jsa_fd = fd;
 376       // add read-only maps from classes.jsa to the list of maps
 377       for (m = 0; m < NUM_CDS_REGIONS; m++) {
 378         if (header._space[m]._read_only &&
 379             !header._space[m]._is_heap_region &&
 380             !header._space[m]._is_bitmap_region) {
 381           // With *some* linux versions, the core file doesn't include read-only mmap'ed
 382           // files regions, so let's add them here. This is harmless if the core file also
 383           // include these regions.
 384           uintptr_t base = sharedBaseAddress + (uintptr_t) header._space[m]._mapping_offset;
 385           size_t size = header._space[m]._used;
 386           // no need to worry about the fractional pages at-the-end.
 387           // possible fractional pages are handled by core_read_data.
 388           add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
 389                                    base, size);
 390           print_debug("added a share archive map [%d] at 0x%lx (size 0x%lx bytes)\n", m, base, size);
 391         }
 392       }
 393       return true;
 394    }
 395    lib = lib->next;
 396   }
 397   return true;
 398 }
 399 
 400 #endif // defined(LINUX) || defined(__APPLE__)