# HG changeset patch # User ccheung # Date 1381436751 25200 # Node ID c01f4910f5f5d8ff9a81b0004ea2da497017cf62 # Parent d25557d03ec04219c46d13768b03bb6538954741# Parent deec468baebd9775f538def036d964c81ae9a4a5 Merge diff -r deec468baebd -r c01f4910f5f5 agent/src/os/bsd/ps_core.c --- a/agent/src/os/bsd/ps_core.c Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/os/bsd/ps_core.c Thu Oct 10 13:25:51 2013 -0700 @@ -44,6 +44,7 @@ // close all file descriptors static void close_files(struct ps_prochandle* ph) { lib_info* lib = NULL; + // close core file descriptor if (ph->core->core_fd >= 0) close(ph->core->core_fd); @@ -149,8 +150,7 @@ // Return the map_info for the given virtual address. We keep a sorted // array of pointers in ph->map_array, so we can binary search. -static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) -{ +static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { int mid, lo = 0, hi = ph->core->num_maps - 1; map_info *mp; @@ -230,9 +230,9 @@ size_t _used; // for setting space top on read // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with - // the C type matching the C++ bool type on any given platform. For - // Hotspot on BSD we assume the corresponding C type is char but - // licensees on BSD versions may need to adjust the type of these fields. + // the C type matching the C++ bool type on any given platform. + // We assume the corresponding C type is char but licensees + // may need to adjust the type of these fields. char _read_only; // read only space? char _allow_exec; // executable code in space? @@ -286,10 +286,12 @@ #define USE_SHARED_SPACES_SYM "_UseSharedSpaces" // mangled name of Arguments::SharedArchivePath #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" +#define LIBJVM_NAME "/libjvm.dylib" #else #define USE_SHARED_SPACES_SYM "UseSharedSpaces" // mangled name of Arguments::SharedArchivePath #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE" +#define LIBJVM_NAME "/libjvm.so" #endif // __APPLE_ static bool init_classsharing_workaround(struct ps_prochandle* ph) { @@ -300,12 +302,7 @@ // we are iterating over shared objects from the core dump. look for // libjvm.so. const char *jvm_name = 0; -#ifdef __APPLE__ - if ((jvm_name = strstr(lib->name, "/libjvm.dylib")) != 0) -#else - if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0) -#endif // __APPLE__ - { + if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { char classes_jsa[PATH_MAX]; struct FileMapHeader header; int fd = -1; @@ -399,8 +396,8 @@ } } return true; - } - lib = lib->next; + } + lib = lib->next; } return true; } @@ -432,8 +429,8 @@ // allocate map_array map_info** array; if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { - print_debug("can't allocate memory for map array\n"); - return false; + print_debug("can't allocate memory for map array\n"); + return false; } // add maps to array @@ -450,7 +447,7 @@ ph->core->map_array = array; // sort the map_info array by base virtual address. qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), - core_cmp_mapping); + core_cmp_mapping); // print map if (is_debug()) { @@ -458,7 +455,7 @@ print_debug("---- sorted virtual address map ----\n"); for (j = 0; j < ph->core->num_maps; j++) { print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr, - ph->core->map_array[j]->memsz); + ph->core->map_array[j]->memsz); } } @@ -1091,9 +1088,9 @@ notep->n_type, notep->n_descsz); if (notep->n_type == NT_PRSTATUS) { - if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { - return false; - } + if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { + return false; + } } p = descdata + ROUNDUP(notep->n_descsz, 4); } @@ -1121,7 +1118,7 @@ * contains a set of saved /proc structures), and PT_LOAD (which * represents a memory mapping from the process's address space). * - * Difference b/w Solaris PT_NOTE and BSD PT_NOTE: + * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE: * * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) * contains /proc structs in the pre-2.6 unstructured /proc format. the last @@ -1167,32 +1164,61 @@ // read segments of a shared object static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { - int i = 0; - ELF_PHDR* phbuf; - ELF_PHDR* lib_php = NULL; + int i = 0; + ELF_PHDR* phbuf; + ELF_PHDR* lib_php = NULL; + + int page_size=sysconf(_SC_PAGE_SIZE); - if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) - return false; + if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) { + return false; + } + + // we want to process only PT_LOAD segments that are not writable. + // i.e., text segments. The read/write/exec (data) segments would + // have been already added from core file segments. + for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { + if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { + + uintptr_t target_vaddr = lib_php->p_vaddr + lib_base; + map_info *existing_map = core_lookup(ph, target_vaddr); - // we want to process only PT_LOAD segments that are not writable. - // i.e., text segments. The read/write/exec (data) segments would - // have been already added from core file segments. - for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { - if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { - if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL) - goto err; + if (existing_map == NULL){ + if (add_map_info(ph, lib_fd, lib_php->p_offset, + target_vaddr, lib_php->p_filesz) == NULL) { + goto err; + } + } else { + if ((existing_map->memsz != page_size) && + (existing_map->fd != lib_fd) && + (existing_map->memsz != lib_php->p_filesz)){ + + print_debug("address conflict @ 0x%lx (size = %ld, flags = %d\n)", + target_vaddr, lib_php->p_filesz, lib_php->p_flags); + goto err; + } + + /* replace PT_LOAD segment with library segment */ + print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n", + existing_map->memsz, lib_php->p_filesz); + + existing_map->fd = lib_fd; + existing_map->offset = lib_php->p_offset; + existing_map->memsz = lib_php->p_filesz; } - lib_php++; - } + } + + lib_php++; + } - free(phbuf); - return true; + free(phbuf); + return true; err: - free(phbuf); - return false; + free(phbuf); + return false; } -// process segments from interpreter (ld-elf.so.1) +// process segments from interpreter (ld.so or ld-linux.so or ld-elf.so) static bool read_interp_segments(struct ps_prochandle* ph) { ELF_EHDR interp_ehdr; @@ -1303,32 +1329,34 @@ debug_base = dyn.d_un.d_ptr; // at debug_base we have struct r_debug. This has first link map in r_map field if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, - &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { + &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { print_debug("can't read first link map address\n"); return false; } // read ld_base address from struct r_debug - // XXX: There is no r_ldbase member on BSD - /* +#if 0 // There is no r_ldbase member on BSD if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, sizeof(uintptr_t)) != PS_OK) { print_debug("can't read ld base address\n"); return false; } ph->core->ld_base_addr = ld_base_addr; - */ +#else ph->core->ld_base_addr = 0; +#endif print_debug("interpreter base address is 0x%lx\n", ld_base_addr); - // now read segments from interp (i.e ld-elf.so.1) - if (read_interp_segments(ph) != true) + // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so) + if (read_interp_segments(ph) != true) { return false; + } // after adding interpreter (ld.so) mappings sort again - if (sort_map_array(ph) != true) + if (sort_map_array(ph) != true) { return false; + } print_debug("first link map is at 0x%lx\n", first_link_map_addr); @@ -1380,8 +1408,9 @@ add_lib_info_fd(ph, lib_name, lib_fd, lib_base); // Map info is added for the library (lib_name) so // we need to re-sort it before calling the p_pdread. - if (sort_map_array(ph) != true) + if (sort_map_array(ph) != true) { return false; + } } else { print_debug("can't read ELF header for shared object %s\n", lib_name); close(lib_fd); @@ -1392,7 +1421,7 @@ // read next link_map address if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, - &link_map_addr, sizeof(uintptr_t)) != PS_OK) { + &link_map_addr, sizeof(uintptr_t)) != PS_OK) { print_debug("can't read next link in link_map\n"); return false; } @@ -1408,7 +1437,7 @@ struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); if (ph == NULL) { - print_debug("cant allocate ps_prochandle\n"); + print_debug("can't allocate ps_prochandle\n"); return NULL; } @@ -1444,38 +1473,45 @@ } if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { - print_debug("executable file is not a valid ELF ET_EXEC file\n"); - goto err; + print_debug("executable file is not a valid ELF ET_EXEC file\n"); + goto err; } // process core file segments - if (read_core_segments(ph, &core_ehdr) != true) - goto err; + if (read_core_segments(ph, &core_ehdr) != true) { + goto err; + } // process exec file segments - if (read_exec_segments(ph, &exec_ehdr) != true) - goto err; + if (read_exec_segments(ph, &exec_ehdr) != true) { + goto err; + } // exec file is also treated like a shared object for symbol search if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, - (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) - goto err; + (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) { + goto err; + } // allocate and sort maps into map_array, we need to do this // here because read_shared_lib_info needs to read from debuggee // address space - if (sort_map_array(ph) != true) + if (sort_map_array(ph) != true) { goto err; + } - if (read_shared_lib_info(ph) != true) + if (read_shared_lib_info(ph) != true) { goto err; + } // sort again because we have added more mappings from shared objects - if (sort_map_array(ph) != true) + if (sort_map_array(ph) != true) { goto err; + } - if (init_classsharing_workaround(ph) != true) + if (init_classsharing_workaround(ph) != true) { goto err; + } print_debug("Leave Pgrab_core\n"); return ph; diff -r deec468baebd -r c01f4910f5f5 agent/src/os/linux/ps_core.c --- a/agent/src/os/linux/ps_core.c Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/os/linux/ps_core.c Thu Oct 10 13:25:51 2013 -0700 @@ -41,155 +41,158 @@ // ps_prochandle cleanup helper functions // close all file descriptors -static void close_elf_files(struct ps_prochandle* ph) { - lib_info* lib = NULL; +static void close_files(struct ps_prochandle* ph) { + lib_info* lib = NULL; - // close core file descriptor - if (ph->core->core_fd >= 0) - close(ph->core->core_fd); + // close core file descriptor + if (ph->core->core_fd >= 0) + close(ph->core->core_fd); - // close exec file descriptor - if (ph->core->exec_fd >= 0) - close(ph->core->exec_fd); + // close exec file descriptor + if (ph->core->exec_fd >= 0) + close(ph->core->exec_fd); - // close interp file descriptor - if (ph->core->interp_fd >= 0) - close(ph->core->interp_fd); + // close interp file descriptor + if (ph->core->interp_fd >= 0) + close(ph->core->interp_fd); - // close class share archive file - if (ph->core->classes_jsa_fd >= 0) - close(ph->core->classes_jsa_fd); + // close class share archive file + if (ph->core->classes_jsa_fd >= 0) + close(ph->core->classes_jsa_fd); - // close all library file descriptors - lib = ph->libs; - while (lib) { - int fd = lib->fd; - if (fd >= 0 && fd != ph->core->exec_fd) close(fd); - lib = lib->next; - } + // close all library file descriptors + lib = ph->libs; + while (lib) { + int fd = lib->fd; + if (fd >= 0 && fd != ph->core->exec_fd) { + close(fd); + } + lib = lib->next; + } } // clean all map_info stuff static void destroy_map_info(struct ps_prochandle* ph) { map_info* map = ph->core->maps; while (map) { - map_info* next = map->next; - free(map); - map = next; + map_info* next = map->next; + free(map); + map = next; } if (ph->core->map_array) { - free(ph->core->map_array); + free(ph->core->map_array); } // Part of the class sharing workaround map = ph->core->class_share_maps; while (map) { - map_info* next = map->next; - free(map); - map = next; + map_info* next = map->next; + free(map); + map = next; } } // ps_prochandle operations static void core_release(struct ps_prochandle* ph) { - if (ph->core) { - close_elf_files(ph); - destroy_map_info(ph); - free(ph->core); - } + if (ph->core) { + close_files(ph); + destroy_map_info(ph); + free(ph->core); + } } static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { - map_info* map; - if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { - print_debug("can't allocate memory for map_info\n"); - return NULL; - } + map_info* map; + if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { + print_debug("can't allocate memory for map_info\n"); + return NULL; + } - // initialize map - map->fd = fd; - map->offset = offset; - map->vaddr = vaddr; - map->memsz = memsz; - return map; + // initialize map + map->fd = fd; + map->offset = offset; + map->vaddr = vaddr; + map->memsz = memsz; + return map; } // add map info with given fd, offset, vaddr and memsz static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, uintptr_t vaddr, size_t memsz) { - map_info* map; - if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { - return NULL; - } + map_info* map; + if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { + return NULL; + } - // add this to map list - map->next = ph->core->maps; - ph->core->maps = map; - ph->core->num_maps++; + // add this to map list + map->next = ph->core->maps; + ph->core->maps = map; + ph->core->num_maps++; - return map; + return map; } // Part of the class sharing workaround -static void add_class_share_map_info(struct ps_prochandle* ph, off_t offset, +static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, uintptr_t vaddr, size_t memsz) { - map_info* map; - if ((map = allocate_init_map(ph->core->classes_jsa_fd, - offset, vaddr, memsz)) == NULL) { - return; - } + map_info* map; + if ((map = allocate_init_map(ph->core->classes_jsa_fd, + offset, vaddr, memsz)) == NULL) { + return NULL; + } - map->next = ph->core->class_share_maps; - ph->core->class_share_maps = map; + map->next = ph->core->class_share_maps; + ph->core->class_share_maps = map; + return map; } // Return the map_info for the given virtual address. We keep a sorted // array of pointers in ph->map_array, so we can binary search. -static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) -{ - int mid, lo = 0, hi = ph->core->num_maps - 1; - map_info *mp; +static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { + int mid, lo = 0, hi = ph->core->num_maps - 1; + map_info *mp; - while (hi - lo > 1) { - mid = (lo + hi) / 2; - if (addr >= ph->core->map_array[mid]->vaddr) - lo = mid; - else - hi = mid; - } + while (hi - lo > 1) { + mid = (lo + hi) / 2; + if (addr >= ph->core->map_array[mid]->vaddr) { + lo = mid; + } else { + hi = mid; + } + } - if (addr < ph->core->map_array[hi]->vaddr) - mp = ph->core->map_array[lo]; - else - mp = ph->core->map_array[hi]; + if (addr < ph->core->map_array[hi]->vaddr) { + mp = ph->core->map_array[lo]; + } else { + mp = ph->core->map_array[hi]; + } - if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) - return (mp); + if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { + return (mp); + } - // Part of the class sharing workaround - // Unfortunately, we have no way of detecting -Xshare state. - // Check out the share maps atlast, if we don't find anywhere. - // This is done this way so to avoid reading share pages - // ahead of other normal maps. For eg. with -Xshare:off we don't - // want to prefer class sharing data to data from core. - mp = ph->core->class_share_maps; - if (mp) { - print_debug("can't locate map_info at 0x%lx, trying class share maps\n", - addr); - } - while (mp) { - if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { - print_debug("located map_info at 0x%lx from class share maps\n", - addr); - return (mp); - } - mp = mp->next; - } + // Part of the class sharing workaround + // Unfortunately, we have no way of detecting -Xshare state. + // Check out the share maps atlast, if we don't find anywhere. + // This is done this way so to avoid reading share pages + // ahead of other normal maps. For eg. with -Xshare:off we don't + // want to prefer class sharing data to data from core. + mp = ph->core->class_share_maps; + if (mp) { + print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); + } + while (mp) { + if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { + print_debug("located map_info at 0x%lx from class share maps\n", addr); + return (mp); + } + mp = mp->next; + } - print_debug("can't locate map_info at 0x%lx\n", addr); - return (NULL); + print_debug("can't locate map_info at 0x%lx\n", addr); + return (NULL); } //--------------------------------------------------------------- @@ -226,9 +229,9 @@ size_t _used; // for setting space top on read // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with - // the C type matching the C++ bool type on any given platform. For - // Hotspot on Linux we assume the corresponding C type is char but - // licensees on Linux versions may need to adjust the type of these fields. + // the C type matching the C++ bool type on any given platform. + // We assume the corresponding C type is char but licensees + // may need to adjust the type of these fields. char _read_only; // read only space? char _allow_exec; // executable code in space? @@ -238,154 +241,159 @@ }; static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { - jboolean i; - if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { - *pvalue = i; - return true; - } else { - return false; - } + jboolean i; + if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { + *pvalue = i; + return true; + } else { + return false; + } } static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { - uintptr_t uip; - if (ps_pdread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) { - *pvalue = uip; - return true; - } else { - return false; - } + uintptr_t uip; + if (ps_pdread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { + *pvalue = uip; + return true; + } else { + return false; + } } // used to read strings from debuggee static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { - size_t i = 0; - char c = ' '; + size_t i = 0; + char c = ' '; - while (c != '\0') { - if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) - return false; - if (i < size - 1) - buf[i] = c; - else // smaller buffer - return false; - i++; addr++; - } + while (c != '\0') { + if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { + return false; + } + if (i < size - 1) { + buf[i] = c; + } else { + // smaller buffer + return false; + } + i++; addr++; + } - buf[i] = '\0'; - return true; + buf[i] = '\0'; + return true; } #define USE_SHARED_SPACES_SYM "UseSharedSpaces" // mangled name of Arguments::SharedArchivePath #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" +#define LIBJVM_NAME "/libjvm.so" static bool init_classsharing_workaround(struct ps_prochandle* ph) { - lib_info* lib = ph->libs; - while (lib != NULL) { - // we are iterating over shared objects from the core dump. look for - // libjvm.so. - const char *jvm_name = 0; - if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0) { - char classes_jsa[PATH_MAX]; - struct FileMapHeader header; - size_t n = 0; - int fd = -1, m = 0; - uintptr_t base = 0, useSharedSpacesAddr = 0; - uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; - jboolean useSharedSpaces = 0; - map_info* mi = 0; + lib_info* lib = ph->libs; + while (lib != NULL) { + // we are iterating over shared objects from the core dump. look for + // libjvm.so. + const char *jvm_name = 0; + if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { + char classes_jsa[PATH_MAX]; + struct FileMapHeader header; + int fd = -1; + int m = 0; + size_t n = 0; + uintptr_t base = 0, useSharedSpacesAddr = 0; + uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; + jboolean useSharedSpaces = 0; + map_info* mi = 0; - memset(classes_jsa, 0, sizeof(classes_jsa)); - jvm_name = lib->name; - useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); - if (useSharedSpacesAddr == 0) { - print_debug("can't lookup 'UseSharedSpaces' flag\n"); - return false; - } + memset(classes_jsa, 0, sizeof(classes_jsa)); + jvm_name = lib->name; + useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); + if (useSharedSpacesAddr == 0) { + print_debug("can't lookup 'UseSharedSpaces' flag\n"); + return false; + } - // Hotspot vm types are not exported to build this library. So - // using equivalent type jboolean to read the value of - // UseSharedSpaces which is same as hotspot type "bool". - if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { - print_debug("can't read the value of 'UseSharedSpaces' flag\n"); - return false; - } + // Hotspot vm types are not exported to build this library. So + // using equivalent type jboolean to read the value of + // UseSharedSpaces which is same as hotspot type "bool". + if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { + print_debug("can't read the value of 'UseSharedSpaces' flag\n"); + return false; + } - if ((int)useSharedSpaces == 0) { - print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); - return true; - } + if ((int)useSharedSpaces == 0) { + print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); + return true; + } - sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); - if (sharedArchivePathAddrAddr == 0) { - print_debug("can't lookup shared archive path symbol\n"); - return false; - } + sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); + if (sharedArchivePathAddrAddr == 0) { + print_debug("can't lookup shared archive path symbol\n"); + return false; + } - if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { - print_debug("can't read shared archive path pointer\n"); - return false; - } + if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { + print_debug("can't read shared archive path pointer\n"); + return false; + } - if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { - print_debug("can't read shared archive path value\n"); - return false; - } + if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { + print_debug("can't read shared archive path value\n"); + return false; + } - print_debug("looking for %s\n", classes_jsa); - // open the class sharing archive file - fd = pathmap_open(classes_jsa); - if (fd < 0) { - print_debug("can't open %s!\n", classes_jsa); - ph->core->classes_jsa_fd = -1; - return false; - } else { - print_debug("opened %s\n", classes_jsa); - } + print_debug("looking for %s\n", classes_jsa); + // open the class sharing archive file + fd = pathmap_open(classes_jsa); + if (fd < 0) { + print_debug("can't open %s!\n", classes_jsa); + ph->core->classes_jsa_fd = -1; + return false; + } else { + print_debug("opened %s\n", classes_jsa); + } - // read FileMapHeader from the file - memset(&header, 0, sizeof(struct FileMapHeader)); - if ((n = read(fd, &header, sizeof(struct FileMapHeader))) - != sizeof(struct FileMapHeader)) { - print_debug("can't read shared archive file map header from %s\n", classes_jsa); - close(fd); - return false; - } + // read FileMapHeader from the file + memset(&header, 0, sizeof(struct FileMapHeader)); + if ((n = read(fd, &header, sizeof(struct FileMapHeader))) + != sizeof(struct FileMapHeader)) { + print_debug("can't read shared archive file map header from %s\n", classes_jsa); + close(fd); + return false; + } - // check file magic - if (header._magic != 0xf00baba2) { - print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", - classes_jsa, header._magic); - close(fd); - return false; - } + // check file magic + if (header._magic != 0xf00baba2) { + print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", + classes_jsa, header._magic); + close(fd); + return false; + } - // check version - if (header._version != CURRENT_ARCHIVE_VERSION) { - print_debug("%s has wrong shared archive file version %d, expecting %d\n", - classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); - close(fd); - return false; - } + // check version + if (header._version != CURRENT_ARCHIVE_VERSION) { + print_debug("%s has wrong shared archive file version %d, expecting %d\n", + classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); + close(fd); + return false; + } - ph->core->classes_jsa_fd = fd; - // add read-only maps from classes.jsa to the list of maps - for (m = 0; m < NUM_SHARED_MAPS; m++) { - if (header._space[m]._read_only) { - base = (uintptr_t) header._space[m]._base; - // no need to worry about the fractional pages at-the-end. - // possible fractional pages are handled by core_read_data. - add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, - base, (size_t) header._space[m]._used); - print_debug("added a share archive map at 0x%lx\n", base); - } - } - return true; + ph->core->classes_jsa_fd = fd; + // add read-only maps from classes.jsa to the list of maps + for (m = 0; m < NUM_SHARED_MAPS; m++) { + if (header._space[m]._read_only) { + base = (uintptr_t) header._space[m]._base; + // no need to worry about the fractional pages at-the-end. + // possible fractional pages are handled by core_read_data. + add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, + base, (size_t) header._space[m]._used); + print_debug("added a share archive map at 0x%lx\n", base); + } } - lib = lib->next; + return true; } - return true; + lib = lib->next; + } + return true; } @@ -396,54 +404,58 @@ // callback for sorting the array of map_info pointers. static int core_cmp_mapping(const void *lhsp, const void *rhsp) { - const map_info *lhs = *((const map_info **)lhsp); - const map_info *rhs = *((const map_info **)rhsp); + const map_info *lhs = *((const map_info **)lhsp); + const map_info *rhs = *((const map_info **)rhsp); - if (lhs->vaddr == rhs->vaddr) - return (0); + if (lhs->vaddr == rhs->vaddr) { + return (0); + } - return (lhs->vaddr < rhs->vaddr ? -1 : 1); + return (lhs->vaddr < rhs->vaddr ? -1 : 1); } // we sort map_info by starting virtual address so that we can do // binary search to read from an address. static bool sort_map_array(struct ps_prochandle* ph) { - size_t num_maps = ph->core->num_maps; - map_info* map = ph->core->maps; - int i = 0; + size_t num_maps = ph->core->num_maps; + map_info* map = ph->core->maps; + int i = 0; - // allocate map_array - map_info** array; - if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { - print_debug("can't allocate memory for map array\n"); - return false; - } + // allocate map_array + map_info** array; + if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { + print_debug("can't allocate memory for map array\n"); + return false; + } - // add maps to array - while (map) { - array[i] = map; - i++; - map = map->next; - } + // add maps to array + while (map) { + array[i] = map; + i++; + map = map->next; + } - // sort is called twice. If this is second time, clear map array - if (ph->core->map_array) free(ph->core->map_array); - ph->core->map_array = array; - // sort the map_info array by base virtual address. - qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), - core_cmp_mapping); + // sort is called twice. If this is second time, clear map array + if (ph->core->map_array) { + free(ph->core->map_array); + } + + ph->core->map_array = array; + // sort the map_info array by base virtual address. + qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), + core_cmp_mapping); - // print map - if (is_debug()) { - int j = 0; - print_debug("---- sorted virtual address map ----\n"); - for (j = 0; j < ph->core->num_maps; j++) { - print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, - ph->core->map_array[j]->memsz); - } - } + // print map + if (is_debug()) { + int j = 0; + print_debug("---- sorted virtual address map ----\n"); + for (j = 0; j < ph->core->num_maps; j++) { + print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, + ph->core->map_array[j]->memsz); + } + } - return true; + return true; } #ifndef MIN @@ -460,16 +472,18 @@ off_t off; int fd; - if (mp == NULL) + if (mp == NULL) { break; /* No mapping for this address */ + } fd = mp->fd; mapoff = addr - mp->vaddr; len = MIN(resid, mp->memsz - mapoff); off = mp->offset + mapoff; - if ((len = pread(fd, buf, len, off)) <= 0) + if ((len = pread(fd, buf, len, off)) <= 0) { break; + } resid -= len; addr += len; @@ -625,8 +639,9 @@ notep->n_type, notep->n_descsz); if (notep->n_type == NT_PRSTATUS) { - if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) - return false; + if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { + return false; + } } p = descdata + ROUNDUP(notep->n_descsz, 4); } @@ -654,7 +669,7 @@ * contains a set of saved /proc structures), and PT_LOAD (which * represents a memory mapping from the process's address space). * - * Difference b/w Solaris PT_NOTE and Linux PT_NOTE: + * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE: * * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) * contains /proc structs in the pre-2.6 unstructured /proc format. the last @@ -674,7 +689,9 @@ for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { switch (core_php->p_type) { case PT_NOTE: - if (core_handle_note(ph, core_php) != true) goto err; + if (core_handle_note(ph, core_php) != true) { + goto err; + } break; case PT_LOAD: { @@ -832,60 +849,62 @@ // read shared library info from runtime linker's data structures. // This work is done by librtlb_db in Solaris static bool read_shared_lib_info(struct ps_prochandle* ph) { - uintptr_t addr = ph->core->dynamic_addr; - uintptr_t debug_base; - uintptr_t first_link_map_addr; - uintptr_t ld_base_addr; - uintptr_t link_map_addr; - uintptr_t lib_base_diff; - uintptr_t lib_base; - uintptr_t lib_name_addr; - char lib_name[BUF_SIZE]; - ELF_DYN dyn; - ELF_EHDR elf_ehdr; - int lib_fd; + uintptr_t addr = ph->core->dynamic_addr; + uintptr_t debug_base; + uintptr_t first_link_map_addr; + uintptr_t ld_base_addr; + uintptr_t link_map_addr; + uintptr_t lib_base_diff; + uintptr_t lib_base; + uintptr_t lib_name_addr; + char lib_name[BUF_SIZE]; + ELF_DYN dyn; + ELF_EHDR elf_ehdr; + int lib_fd; - // _DYNAMIC has information of the form - // [tag] [data] [tag] [data] ..... - // Both tag and data are pointer sized. - // We look for dynamic info with DT_DEBUG. This has shared object info. - // refer to struct r_debug in link.h + // _DYNAMIC has information of the form + // [tag] [data] [tag] [data] ..... + // Both tag and data are pointer sized. + // We look for dynamic info with DT_DEBUG. This has shared object info. + // refer to struct r_debug in link.h + + dyn.d_tag = DT_NULL; + while (dyn.d_tag != DT_DEBUG) { + if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { + print_debug("can't read debug info from _DYNAMIC\n"); + return false; + } + addr += sizeof(ELF_DYN); + } - dyn.d_tag = DT_NULL; - while (dyn.d_tag != DT_DEBUG) { - if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { - print_debug("can't read debug info from _DYNAMIC\n"); - return false; - } - addr += sizeof(ELF_DYN); - } - - // we have got Dyn entry with DT_DEBUG - debug_base = dyn.d_un.d_ptr; - // at debug_base we have struct r_debug. This has first link map in r_map field - if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, + // we have got Dyn entry with DT_DEBUG + debug_base = dyn.d_un.d_ptr; + // at debug_base we have struct r_debug. This has first link map in r_map field + if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { - print_debug("can't read first link map address\n"); - return false; - } + print_debug("can't read first link map address\n"); + return false; + } - // read ld_base address from struct r_debug - if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, + // read ld_base address from struct r_debug + if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, sizeof(uintptr_t)) != PS_OK) { - print_debug("can't read ld base address\n"); - return false; - } - ph->core->ld_base_addr = ld_base_addr; + print_debug("can't read ld base address\n"); + return false; + } + ph->core->ld_base_addr = ld_base_addr; + + print_debug("interpreter base address is 0x%lx\n", ld_base_addr); - print_debug("interpreter base address is 0x%lx\n", ld_base_addr); - - // now read segments from interp (i.e ld.so or ld-linux.so) - if (read_interp_segments(ph) != true) + // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so) + if (read_interp_segments(ph) != true) { return false; + } - // after adding interpreter (ld.so) mappings sort again - if (sort_map_array(ph) != true) - return false; + // after adding interpreter (ld.so) mappings sort again + if (sort_map_array(ph) != true) { + return false; + } print_debug("first link map is at 0x%lx\n", first_link_map_addr); @@ -950,95 +969,102 @@ } } - // read next link_map address - if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, - &link_map_addr, sizeof(uintptr_t)) != PS_OK) { - print_debug("can't read next link in link_map\n"); - return false; - } - } + // read next link_map address + if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, + &link_map_addr, sizeof(uintptr_t)) != PS_OK) { + print_debug("can't read next link in link_map\n"); + return false; + } + } - return true; + return true; } // the one and only one exposed stuff from this file struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { - ELF_EHDR core_ehdr; - ELF_EHDR exec_ehdr; - ELF_EHDR lib_ehdr; + ELF_EHDR core_ehdr; + ELF_EHDR exec_ehdr; + ELF_EHDR lib_ehdr; - struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); - if (ph == NULL) { - print_debug("can't allocate ps_prochandle\n"); - return NULL; - } + struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); + if (ph == NULL) { + print_debug("can't allocate ps_prochandle\n"); + return NULL; + } - if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { - free(ph); - print_debug("can't allocate ps_prochandle\n"); - return NULL; - } + if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { + free(ph); + print_debug("can't allocate ps_prochandle\n"); + return NULL; + } - // initialize ph - ph->ops = &core_ops; - ph->core->core_fd = -1; - ph->core->exec_fd = -1; - ph->core->interp_fd = -1; + // initialize ph + ph->ops = &core_ops; + ph->core->core_fd = -1; + ph->core->exec_fd = -1; + ph->core->interp_fd = -1; - // open the core file - if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { - print_debug("can't open core file\n"); - goto err; - } + // open the core file + if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { + print_debug("can't open core file\n"); + goto err; + } - // read core file ELF header - if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { - print_debug("core file is not a valid ELF ET_CORE file\n"); - goto err; - } + // read core file ELF header + if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { + print_debug("core file is not a valid ELF ET_CORE file\n"); + goto err; + } + + if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { + print_debug("can't open executable file\n"); + goto err; + } - if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { - print_debug("can't open executable file\n"); - goto err; - } + if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { + print_debug("executable file is not a valid ELF ET_EXEC file\n"); + goto err; + } + + // process core file segments + if (read_core_segments(ph, &core_ehdr) != true) { + goto err; + } - if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { - print_debug("executable file is not a valid ELF ET_EXEC file\n"); - goto err; - } + // process exec file segments + if (read_exec_segments(ph, &exec_ehdr) != true) { + goto err; + } - // process core file segments - if (read_core_segments(ph, &core_ehdr) != true) - goto err; - - // process exec file segments - if (read_exec_segments(ph, &exec_ehdr) != true) - goto err; + // exec file is also treated like a shared object for symbol search + if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, + (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) { + goto err; + } - // exec file is also treated like a shared object for symbol search - if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, - (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) - goto err; + // allocate and sort maps into map_array, we need to do this + // here because read_shared_lib_info needs to read from debuggee + // address space + if (sort_map_array(ph) != true) { + goto err; + } - // allocate and sort maps into map_array, we need to do this - // here because read_shared_lib_info needs to read from debuggee - // address space - if (sort_map_array(ph) != true) - goto err; + if (read_shared_lib_info(ph) != true) { + goto err; + } - if (read_shared_lib_info(ph) != true) - goto err; + // sort again because we have added more mappings from shared objects + if (sort_map_array(ph) != true) { + goto err; + } - // sort again because we have added more mappings from shared objects - if (sort_map_array(ph) != true) - goto err; + if (init_classsharing_workaround(ph) != true) { + goto err; + } - if (init_classsharing_workaround(ph) != true) - goto err; - - return ph; + return ph; err: - Prelease(ph); - return NULL; + Prelease(ph); + return NULL; } diff -r deec468baebd -r c01f4910f5f5 agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java --- a/agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java Thu Oct 10 13:25:51 2013 -0700 @@ -67,6 +67,13 @@ String libname = "hsdis"; String arch = System.getProperty("os.arch"); if (os.lastIndexOf("Windows", 0) != -1) { + if (arch.equals("x86")) { + libname += "-i386"; + } else if (arch.equals("amd64")) { + libname += "-amd64"; + } else { + libname += "-" + arch; + } path.append(sep + "bin" + sep); libname += ".dll"; } else if (os.lastIndexOf("SunOS", 0) != -1) { diff -r deec468baebd -r c01f4910f5f5 agent/src/share/classes/sun/jvm/hotspot/memory/SymbolTable.java --- a/agent/src/share/classes/sun/jvm/hotspot/memory/SymbolTable.java Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/share/classes/sun/jvm/hotspot/memory/SymbolTable.java Thu Oct 10 13:25:51 2013 -0700 @@ -44,12 +44,10 @@ private static synchronized void initialize(TypeDataBase db) { Type type = db.lookupType("SymbolTable"); theTableField = type.getAddressField("_the_table"); - symbolTableSize = db.lookupIntConstant("SymbolTable::symbol_table_size").intValue(); } // Fields private static AddressField theTableField; - private static int symbolTableSize; // Accessors public static SymbolTable getTheTable() { @@ -57,10 +55,6 @@ return (SymbolTable) VMObjectFactory.newObject(SymbolTable.class, tmp); } - public static int getSymbolTableSize() { - return symbolTableSize; - } - public SymbolTable(Address addr) { super(addr); } diff -r deec468baebd -r c01f4910f5f5 agent/src/share/classes/sun/jvm/hotspot/utilities/AbstractHeapGraphWriter.java --- a/agent/src/share/classes/sun/jvm/hotspot/utilities/AbstractHeapGraphWriter.java Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/share/classes/sun/jvm/hotspot/utilities/AbstractHeapGraphWriter.java Thu Oct 10 13:25:51 2013 -0700 @@ -59,6 +59,7 @@ public boolean doObj(Oop oop) { try { + writeHeapRecordPrologue(); if (oop instanceof TypeArray) { writePrimitiveArray((TypeArray)oop); } else if (oop instanceof ObjArray) { @@ -97,6 +98,7 @@ // not-a-Java-visible oop writeInternalObject(oop); } + writeHeapRecordEpilogue(); } catch (IOException exp) { throw new RuntimeException(exp); } @@ -416,6 +418,12 @@ protected void writeHeapFooter() throws IOException { } + protected void writeHeapRecordPrologue() throws IOException { + } + + protected void writeHeapRecordEpilogue() throws IOException { + } + // HeapVisitor, OopVisitor methods can't throw any non-runtime // exception. But, derived class write methods (which are called // from visitor callbacks) may throw IOException. Hence, we throw diff -r deec468baebd -r c01f4910f5f5 agent/src/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java --- a/agent/src/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java Fri Oct 04 14:19:56 2013 -0700 +++ b/agent/src/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java Thu Oct 10 13:25:51 2013 -0700 @@ -44,7 +44,7 @@ * WARNING: This format is still under development, and is subject to * change without notice. * - * header "JAVA PROFILE 1.0.1" (0-terminated) + * header "JAVA PROFILE 1.0.1" or "JAVA PROFILE 1.0.2" (0-terminated) * u4 size of identifiers. Identifiers are used to represent * UTF8 strings, objects, stack traces, etc. They usually * have the same size as host pointers. For example, on @@ -292,11 +292,34 @@ * 0x00000002: cpu sampling on/off * u2 stack trace depth * + * + * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally + * be generated as a sequence of heap dump segments. This sequence is + * terminated by an end record. The additional tags allowed by format + * "JAVA PROFILE 1.0.2" are: + * + * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment + * + * [heap dump sub-records]* + * The same sub-record types allowed by HPROF_HEAP_DUMP + * + * HPROF_HEAP_DUMP_END denotes the end of a heap dump + * */ public class HeapHprofBinWriter extends AbstractHeapGraphWriter { + + // The heap size threshold used to determine if segmented format + // ("JAVA PROFILE 1.0.2") should be used. + private static final long HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD = 2L * 0x40000000; + + // The approximate size of a heap segment. Used to calculate when to create + // a new segment. + private static final long HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE = 1L * 0x40000000; + // hprof binary file header - private static final String HPROF_HEADER = "JAVA PROFILE 1.0.1"; + private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1"; + private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2"; // constants in enum HprofTag private static final int HPROF_UTF8 = 0x01; @@ -312,6 +335,10 @@ private static final int HPROF_CPU_SAMPLES = 0x0D; private static final int HPROF_CONTROL_SETTINGS = 0x0E; + // 1.0.2 record types + private static final int HPROF_HEAP_DUMP_SEGMENT = 0x1C; + private static final int HPROF_HEAP_DUMP_END = 0x2C; + // Heap dump constants // constants in enum HprofGcTag private static final int HPROF_GC_ROOT_UNKNOWN = 0xFF; @@ -352,11 +379,9 @@ private static final int JVM_SIGNATURE_ARRAY = '['; private static final int JVM_SIGNATURE_CLASS = 'L'; - public synchronized void write(String fileName) throws IOException { // open file stream and create buffered data output stream - FileOutputStream fos = new FileOutputStream(fileName); - FileChannel chn = fos.getChannel(); + fos = new FileOutputStream(fileName); out = new DataOutputStream(new BufferedOutputStream(fos)); VM vm = VM.getVM(); @@ -385,6 +410,9 @@ FLOAT_SIZE = objectHeap.getFloatSize(); DOUBLE_SIZE = objectHeap.getDoubleSize(); + // Check weather we should dump the heap as segments + useSegmentedHeapDump = vm.getUniverse().heap().used() > HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD; + // hprof bin format header writeFileHeader(); @@ -394,38 +422,87 @@ // hprof UTF-8 symbols section writeSymbols(); + // HPROF_LOAD_CLASS records for all classes writeClasses(); - // write heap data now - out.writeByte((byte)HPROF_HEAP_DUMP); - out.writeInt(0); // relative timestamp - - // remember position of dump length, we will fixup - // length later - hprof format requires length. - out.flush(); - long dumpStart = chn.position(); - - // write dummy length of 0 and we'll fix it later. - out.writeInt(0); - // write CLASS_DUMP records writeClassDumpRecords(); // this will write heap data into the buffer stream super.write(); + // flush buffer stream. + out.flush(); + + // Fill in final length + fillInHeapRecordLength(); + + if (useSegmentedHeapDump) { + // Write heap segment-end record + out.writeByte((byte) HPROF_HEAP_DUMP_END); + out.writeInt(0); + out.writeInt(0); + } + // flush buffer stream and throw it. out.flush(); out = null; + // close the file stream + fos.close(); + } + + @Override + protected void writeHeapRecordPrologue() throws IOException { + if (currentSegmentStart == 0) { + // write heap data header, depending on heap size use segmented heap + // format + out.writeByte((byte) (useSegmentedHeapDump ? HPROF_HEAP_DUMP_SEGMENT + : HPROF_HEAP_DUMP)); + out.writeInt(0); + + // remember position of dump length, we will fixup + // length later - hprof format requires length. + out.flush(); + currentSegmentStart = fos.getChannel().position(); + + // write dummy length of 0 and we'll fix it later. + out.writeInt(0); + } + } + + @Override + protected void writeHeapRecordEpilogue() throws IOException { + if (useSegmentedHeapDump) { + out.flush(); + if ((fos.getChannel().position() - currentSegmentStart - 4) >= HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE) { + fillInHeapRecordLength(); + currentSegmentStart = 0; + } + } + } + + private void fillInHeapRecordLength() throws IOException { + // now get current position to calculate length - long dumpEnd = chn.position(); + long dumpEnd = fos.getChannel().position(); + // calculate length of heap data - int dumpLen = (int) (dumpEnd - dumpStart - 4); + long dumpLenLong = (dumpEnd - currentSegmentStart - 4L); + + // Check length boundary, overflow could happen but is _very_ unlikely + if(dumpLenLong >= (4L * 0x40000000)){ + throw new RuntimeException("Heap segment size overflow."); + } + + // Save the current position + long currentPosition = fos.getChannel().position(); // seek the position to write length - chn.position(dumpStart); + fos.getChannel().position(currentSegmentStart); + + int dumpLen = (int) dumpLenLong; // write length as integer fos.write((dumpLen >>> 24) & 0xFF); @@ -433,8 +510,8 @@ fos.write((dumpLen >>> 8) & 0xFF); fos.write((dumpLen >>> 0) & 0xFF); - // close the file stream - fos.close(); + //Reset to previous current position + fos.getChannel().position(currentPosition); } private void writeClassDumpRecords() throws IOException { @@ -443,7 +520,9 @@ sysDict.allClassesDo(new SystemDictionary.ClassVisitor() { public void visit(Klass k) { try { + writeHeapRecordPrologue(); writeClassDumpRecord(k); + writeHeapRecordEpilogue(); } catch (IOException e) { throw new RuntimeException(e); } @@ -884,7 +963,12 @@ // writes hprof binary file header private void writeFileHeader() throws IOException { // version string - out.writeBytes(HPROF_HEADER); + if(useSegmentedHeapDump) { + out.writeBytes(HPROF_HEADER_1_0_2); + } + else { + out.writeBytes(HPROF_HEADER_1_0_1); + } out.writeByte((byte)'\0'); // write identifier size. we use pointers as identifiers. @@ -976,6 +1060,7 @@ private static final int EMPTY_FRAME_DEPTH = -1; private DataOutputStream out; + private FileOutputStream fos; private Debugger dbg; private ObjectHeap objectHeap; private SymbolTable symTbl; @@ -983,6 +1068,10 @@ // oopSize of the debuggee private int OBJ_ID_SIZE; + // Added for hprof file format 1.0.2 support + private boolean useSegmentedHeapDump; + private long currentSegmentStart; + private long BOOLEAN_BASE_OFFSET; private long BYTE_BASE_OFFSET; private long CHAR_BASE_OFFSET; @@ -1005,6 +1094,7 @@ private static class ClassData { int instSize; List fields; + ClassData(int instSize, List fields) { this.instSize = instSize; this.fields = fields; diff -r deec468baebd -r c01f4910f5f5 make/windows/makefiles/compile.make --- a/make/windows/makefiles/compile.make Fri Oct 04 14:19:56 2013 -0700 +++ b/make/windows/makefiles/compile.make Thu Oct 10 13:25:51 2013 -0700 @@ -44,6 +44,7 @@ # /GS Inserts security stack checks in some functions (VS2005 default) # /Oi Use intrinsics (in /O2) # /Od Disable all optimizations +# /MP Use multiple cores for compilation # # NOTE: Normally following any of the above with a '-' will turn off that flag # @@ -206,6 +207,7 @@ DEBUG_OPT_OPTION = /Od GX_OPTION = /EHsc LD_FLAGS = /manifest $(LD_FLAGS) +MP_FLAG = /MP # Manifest Tool - used in VS2005 and later to adjust manifests stored # as resources inside build artifacts. !if "x$(MT)" == "x" @@ -219,6 +221,7 @@ DEBUG_OPT_OPTION = /Od GX_OPTION = /EHsc LD_FLAGS = /manifest $(LD_FLAGS) +MP_FLAG = /MP # Manifest Tool - used in VS2005 and later to adjust manifests stored # as resources inside build artifacts. !if "x$(MT)" == "x" @@ -235,6 +238,7 @@ DEBUG_OPT_OPTION = /Od GX_OPTION = /EHsc LD_FLAGS = /manifest $(LD_FLAGS) +MP_FLAG = /MP # Manifest Tool - used in VS2005 and later to adjust manifests stored # as resources inside build artifacts. !if "x$(MT)" == "x" @@ -245,6 +249,8 @@ !endif !endif +CXX_FLAGS = $(CXX_FLAGS) $(MP_FLAG) + # If NO_OPTIMIZATIONS is defined in the environment, turn everything off !ifdef NO_OPTIMIZATIONS PRODUCT_OPT_OPTION = $(DEBUG_OPT_OPTION) diff -r deec468baebd -r c01f4910f5f5 make/windows/makefiles/fastdebug.make --- a/make/windows/makefiles/fastdebug.make Fri Oct 04 14:19:56 2013 -0700 +++ b/make/windows/makefiles/fastdebug.make Thu Oct 10 13:25:51 2013 -0700 @@ -38,7 +38,7 @@ !include ../local.make !include compile.make -CXX_FLAGS=$(CXX_FLAGS) $(FASTDEBUG_OPT_OPTION) /D "CHECK_UNHANDLED_OOPS" +CXX_FLAGS=$(CXX_FLAGS) $(FASTDEBUG_OPT_OPTION) !include $(WorkSpace)/make/windows/makefiles/vm.make !include local.make diff -r deec468baebd -r c01f4910f5f5 make/windows/makefiles/sa.make --- a/make/windows/makefiles/sa.make Fri Oct 04 14:19:56 2013 -0700 +++ b/make/windows/makefiles/sa.make Thu Oct 10 13:25:51 2013 -0700 @@ -102,28 +102,33 @@ !if "$(MT)" != "" SA_LD_FLAGS = -manifest $(SA_LD_FLAGS) !endif -SASRCFILE = $(AGENT_DIR)/src/os/win32/windbg/sawindbg.cpp + +SASRCFILES = $(AGENT_DIR)/src/os/win32/windbg/sawindbg.cpp \ + $(AGENT_DIR)/src/share/native/sadis.c + SA_LFLAGS = $(SA_LD_FLAGS) -nologo -subsystem:console -machine:$(MACHINE) !if "$(ENABLE_FULL_DEBUG_SYMBOLS)" == "1" SA_LFLAGS = $(SA_LFLAGS) -map -debug !endif +SA_CFLAGS = $(SA_CFLAGS) $(MP_FLAG) + # Note that we do not keep sawindbj.obj around as it would then # get included in the dumpbin command in build_vm_def.sh # In VS2005 or VS2008 the link command creates a .manifest file that we want # to insert into the linked artifact so we do not need to track it separately. # Use ";#2" for .dll and ";#1" for .exe in the MT command below: -$(SAWINDBG): $(SASRCFILE) +$(SAWINDBG): $(SASRCFILES) set INCLUDE=$(SA_INCLUDE)$(INCLUDE) $(CXX) @<< -I"$(BootStrapDir)/include" -I"$(BootStrapDir)/include/win32" -I"$(GENERATED)" $(SA_CFLAGS) - $(SASRCFILE) + $(SASRCFILES) -out:$*.obj << set LIB=$(SA_LIB)$(LIB) - $(LD) -out:$@ -DLL $*.obj dbgeng.lib $(SA_LFLAGS) + $(LD) -out:$@ -DLL sawindbg.obj sadis.obj dbgeng.lib $(SA_LFLAGS) !if "$(MT)" != "" $(MT) -manifest $(@F).manifest -outputresource:$(@F);#2 !endif diff -r deec468baebd -r c01f4910f5f5 src/os/bsd/vm/osThread_bsd.hpp --- a/src/os/bsd/vm/osThread_bsd.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/os/bsd/vm/osThread_bsd.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -42,7 +42,7 @@ #ifdef __APPLE__ typedef thread_t thread_id_t; #else - typedef pthread_t thread_id_t; + typedef pid_t thread_id_t; #endif // _pthread_id is the pthread id, which is used by library calls diff -r deec468baebd -r c01f4910f5f5 src/os/bsd/vm/os_bsd.cpp --- a/src/os/bsd/vm/os_bsd.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/os/bsd/vm/os_bsd.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -100,6 +100,7 @@ # include # include # include +# include #if defined(__FreeBSD__) || defined(__NetBSD__) # include @@ -152,6 +153,7 @@ // utility functions static int SR_initialize(); +static void unpackTime(timespec* absTime, bool isAbsolute, jlong time); julong os::available_memory() { return Bsd::available_memory(); @@ -247,7 +249,17 @@ * since it returns a 64 bit value) */ mib[0] = CTL_HW; + +#if defined (HW_MEMSIZE) // Apple mib[1] = HW_MEMSIZE; +#elif defined(HW_PHYSMEM) // Most of BSD + mib[1] = HW_PHYSMEM; +#elif defined(HW_REALMEM) // Old FreeBSD + mib[1] = HW_REALMEM; +#else + #error No ways to get physmem +#endif + len = sizeof(mem_val); if (sysctl(mib, 2, &mem_val, &len, NULL, 0) != -1) { assert(len == sizeof(mem_val), "unexpected data size"); @@ -679,18 +691,12 @@ return NULL; } + osthread->set_thread_id(os::Bsd::gettid()); + #ifdef __APPLE__ - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); - guarantee(thread_id != 0, "thread id missing from pthreads"); - osthread->set_thread_id(thread_id); - - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); guarantee(unique_thread_id != 0, "unique thread id was not found"); osthread->set_unique_thread_id(unique_thread_id); -#else - // thread_id is pthread_id on BSD - osthread->set_thread_id(::pthread_self()); #endif // initialize signal mask for this thread os::Bsd::hotspot_sigmask(thread); @@ -847,18 +853,13 @@ return false; } + osthread->set_thread_id(os::Bsd::gettid()); + // Store pthread info into the OSThread #ifdef __APPLE__ - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); - guarantee(thread_id != 0, "just checking"); - osthread->set_thread_id(thread_id); - - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); guarantee(unique_thread_id != 0, "just checking"); osthread->set_unique_thread_id(unique_thread_id); -#else - osthread->set_thread_id(::pthread_self()); #endif osthread->set_pthread_id(::pthread_self()); @@ -1125,6 +1126,30 @@ return n; } +// Information of current thread in variety of formats +pid_t os::Bsd::gettid() { + int retval = -1; + +#ifdef __APPLE__ //XNU kernel + // despite the fact mach port is actually not a thread id use it + // instead of syscall(SYS_thread_selfid) as it certainly fits to u4 + retval = ::pthread_mach_thread_np(::pthread_self()); + guarantee(retval != 0, "just checking"); + return retval; + +#elif __FreeBSD__ + retval = syscall(SYS_thr_self); +#elif __OpenBSD__ + retval = syscall(SYS_getthrid); +#elif __NetBSD__ + retval = (pid_t) syscall(SYS__lwp_self); +#endif + + if (retval == -1) { + return getpid(); + } +} + intx os::current_thread_id() { #ifdef __APPLE__ return (intx)::pthread_mach_thread_np(::pthread_self()); @@ -1132,6 +1157,7 @@ return (intx)::pthread_self(); #endif } + int os::current_process_id() { // Under the old bsd thread library, bsd gives each thread @@ -1904,7 +1930,7 @@ bool timedwait(unsigned int sec, int nsec); private: jlong currenttime() const; - semaphore_t _semaphore; + os_semaphore_t _semaphore; }; Semaphore::Semaphore() : _semaphore(0) { @@ -1972,7 +1998,7 @@ bool Semaphore::timedwait(unsigned int sec, int nsec) { struct timespec ts; - jlong endtime = unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); + unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); while (1) { int result = sem_timedwait(&_semaphore, &ts); diff -r deec468baebd -r c01f4910f5f5 src/os/bsd/vm/os_bsd.hpp --- a/src/os/bsd/vm/os_bsd.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/os/bsd/vm/os_bsd.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -84,6 +84,7 @@ static void hotspot_sigmask(Thread* thread); static bool is_initial_thread(void); + static pid_t gettid(); static int page_size(void) { return _page_size; } static void set_page_size(int val) { _page_size = val; } diff -r deec468baebd -r c01f4910f5f5 src/share/vm/classfile/classFileParser.cpp --- a/src/share/vm/classfile/classFileParser.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/classfile/classFileParser.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -4080,8 +4080,7 @@ // Generate any default methods - default methods are interface methods // that have a default implementation. This is new with Lambda project. - if (has_default_methods && !access_flags.is_interface() && - local_interfaces->length() > 0) { + if (has_default_methods && !access_flags.is_interface() ) { DefaultMethods::generate_default_methods( this_klass(), &all_mirandas, CHECK_(nullHandle)); } diff -r deec468baebd -r c01f4910f5f5 src/share/vm/classfile/defaultMethods.cpp --- a/src/share/vm/classfile/defaultMethods.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/classfile/defaultMethods.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -345,7 +345,6 @@ } Symbol* generate_no_defaults_message(TRAPS) const; - Symbol* generate_abstract_method_message(Method* method, TRAPS) const; Symbol* generate_conflicts_message(GrowableArray* methods, TRAPS) const; public: @@ -404,20 +403,19 @@ _exception_message = generate_no_defaults_message(CHECK); _exception_name = vmSymbols::java_lang_AbstractMethodError(); } else if (qualified_methods.length() == 1) { + // leave abstract methods alone, they will be found via normal search path Method* method = qualified_methods.at(0); - if (method->is_abstract()) { - _exception_message = generate_abstract_method_message(method, CHECK); - _exception_name = vmSymbols::java_lang_AbstractMethodError(); - } else { + if (!method->is_abstract()) { _selected_target = qualified_methods.at(0); } } else { _exception_message = generate_conflicts_message(&qualified_methods,CHECK); _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError(); + if (TraceDefaultMethods) { + _exception_message->print_value_on(tty); + tty->print_cr(""); + } } - - assert((has_target() ^ throws_exception()) == 1, - "One and only one must be true"); } bool contains_signature(Symbol* query) { @@ -475,20 +473,6 @@ return SymbolTable::new_symbol("No qualifying defaults found", CHECK_NULL); } -Symbol* MethodFamily::generate_abstract_method_message(Method* method, TRAPS) const { - Symbol* klass = method->klass_name(); - Symbol* name = method->name(); - Symbol* sig = method->signature(); - stringStream ss; - ss.print("Method "); - ss.write((const char*)klass->bytes(), klass->utf8_length()); - ss.print("."); - ss.write((const char*)name->bytes(), name->utf8_length()); - ss.write((const char*)sig->bytes(), sig->utf8_length()); - ss.print(" is abstract"); - return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL); -} - Symbol* MethodFamily::generate_conflicts_message(GrowableArray* methods, TRAPS) const { stringStream ss; ss.print("Conflicting default methods:"); @@ -595,6 +579,18 @@ #endif // ndef PRODUCT }; +static bool already_in_vtable_slots(GrowableArray* slots, Method* m) { + bool found = false; + for (int j = 0; j < slots->length(); ++j) { + if (slots->at(j)->name() == m->name() && + slots->at(j)->signature() == m->signature() ) { + found = true; + break; + } + } + return found; +} + static GrowableArray* find_empty_vtable_slots( InstanceKlass* klass, GrowableArray* mirandas, TRAPS) { @@ -604,8 +600,10 @@ // All miranda methods are obvious candidates for (int i = 0; i < mirandas->length(); ++i) { - EmptyVtableSlot* slot = new EmptyVtableSlot(mirandas->at(i)); - slots->append(slot); + Method* m = mirandas->at(i); + if (!already_in_vtable_slots(slots, m)) { + slots->append(new EmptyVtableSlot(m)); + } } // Also any overpasses in our superclasses, that we haven't implemented. @@ -621,7 +619,26 @@ // unless we have a real implementation of it in the current class. Method* impl = klass->lookup_method(m->name(), m->signature()); if (impl == NULL || impl->is_overpass()) { - slots->append(new EmptyVtableSlot(m)); + if (!already_in_vtable_slots(slots, m)) { + slots->append(new EmptyVtableSlot(m)); + } + } + } + } + + // also any default methods in our superclasses + if (super->default_methods() != NULL) { + for (int i = 0; i < super->default_methods()->length(); ++i) { + Method* m = super->default_methods()->at(i); + // m is a method that would have been a miranda if not for the + // default method processing that occurred on behalf of our superclass, + // so it's a method we want to re-examine in this new context. That is, + // unless we have a real implementation of it in the current class. + Method* impl = klass->lookup_method(m->name(), m->signature()); + if (impl == NULL || impl->is_overpass()) { + if (!already_in_vtable_slots(slots, m)) { + slots->append(new EmptyVtableSlot(m)); + } } } } @@ -679,7 +696,7 @@ // private interface methods are not candidates for default methods // invokespecial to private interface methods doesn't use default method logic // future: take access controls into account for superclass methods - if (m != NULL && (!iklass->is_interface() || m->is_public())) { + if (m != NULL && !m->is_static() && (!iklass->is_interface() || m->is_public())) { if (_family == NULL) { _family = new StatefulMethodFamily(); } @@ -700,7 +717,7 @@ -static void create_overpasses( +static void create_defaults_and_exceptions( GrowableArray* slots, InstanceKlass* klass, TRAPS); static void generate_erased_defaults( @@ -721,6 +738,8 @@ static void merge_in_new_methods(InstanceKlass* klass, GrowableArray* new_methods, TRAPS); +static void create_default_methods( InstanceKlass* klass, + GrowableArray* new_methods, TRAPS); // This is the guts of the default methods implementation. This is called just // after the classfile has been parsed if some ancestor has default methods. @@ -782,7 +801,7 @@ } #endif // ndef PRODUCT - create_overpasses(empty_slots, klass, CHECK); + create_defaults_and_exceptions(empty_slots, klass, CHECK); #ifndef PRODUCT if (TraceDefaultMethods) { @@ -791,66 +810,6 @@ #endif // ndef PRODUCT } - - -#ifdef ASSERT -// Return true is broad type is a covariant return of narrow type -static bool covariant_return_type(BasicType narrow, BasicType broad) { - if (narrow == broad) { - return true; - } - if (broad == T_OBJECT) { - return true; - } - return false; -} -#endif - -static int assemble_redirect( - BytecodeConstantPool* cp, BytecodeBuffer* buffer, - Symbol* incoming, Method* target, TRAPS) { - - BytecodeAssembler assem(buffer, cp); - - SignatureStream in(incoming, true); - SignatureStream out(target->signature(), true); - u2 parameter_count = 0; - - assem.aload(parameter_count++); // load 'this' - - while (!in.at_return_type()) { - assert(!out.at_return_type(), "Parameter counts do not match"); - BasicType bt = in.type(); - assert(out.type() == bt, "Parameter types are not compatible"); - assem.load(bt, parameter_count); - if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) { - assem.checkcast(out.as_symbol(THREAD)); - } else if (bt == T_LONG || bt == T_DOUBLE) { - ++parameter_count; // longs and doubles use two slots - } - ++parameter_count; - in.next(); - out.next(); - } - assert(out.at_return_type(), "Parameter counts do not match"); - assert(covariant_return_type(out.type(), in.type()), "Return types are not compatible"); - - if (parameter_count == 1 && (in.type() == T_LONG || in.type() == T_DOUBLE)) { - ++parameter_count; // need room for return value - } - if (target->method_holder()->is_interface()) { - assem.invokespecial(target); - } else { - assem.invokevirtual(target); - } - - if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) { - assem.checkcast(in.as_symbol(THREAD)); - } - assem._return(in.type()); - return parameter_count; -} - static int assemble_method_error( BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) { @@ -924,18 +883,18 @@ } } -// A "bridge" is a method created by javac to bridge the gap between -// an implementation and a generically-compatible, but different, signature. -// Bridges have actual bytecode implementation in classfiles. -// An "overpass", on the other hand, performs the same function as a bridge -// but does not occur in a classfile; the VM creates overpass itself, -// when it needs a path to get from a call site to an default method, and -// a bridge doesn't exist. -static void create_overpasses( +// Create default_methods list for the current class. +// With the VM only processing erased signatures, the VM only +// creates an overpass in a conflict case or a case with no candidates. +// This allows virtual methods to override the overpass, but ensures +// that a local method search will find the exception rather than an abstract +// or default method that is not a valid candidate. +static void create_defaults_and_exceptions( GrowableArray* slots, InstanceKlass* klass, TRAPS) { GrowableArray overpasses; + GrowableArray defaults; BytecodeConstantPool bpool(klass->constants()); for (int i = 0; i < slots->length(); ++i) { @@ -943,7 +902,6 @@ if (slot->is_bound()) { MethodFamily* method = slot->get_binding(); - int max_stack = 0; BytecodeBuffer buffer; #ifndef PRODUCT @@ -953,26 +911,27 @@ tty->print_cr(""); if (method->has_target()) { method->print_selected(tty, 1); - } else { + } else if (method->throws_exception()) { method->print_exception(tty, 1); } } #endif // ndef PRODUCT + if (method->has_target()) { Method* selected = method->get_selected_target(); if (selected->method_holder()->is_interface()) { - max_stack = assemble_redirect( - &bpool, &buffer, slot->signature(), selected, CHECK); + defaults.push(selected); } } else if (method->throws_exception()) { - max_stack = assemble_method_error(&bpool, &buffer, method->get_exception_name(), method->get_exception_message(), CHECK); - } - if (max_stack != 0) { + int max_stack = assemble_method_error(&bpool, &buffer, + method->get_exception_name(), method->get_exception_message(), CHECK); AccessFlags flags = accessFlags_from( JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE); - Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(), + Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(), flags, max_stack, slot->size_of_parameters(), ConstMethod::OVERPASS, CHECK); + // We push to the methods list: + // overpass methods which are exception throwing methods if (m != NULL) { overpasses.push(m); } @@ -983,11 +942,31 @@ #ifndef PRODUCT if (TraceDefaultMethods) { tty->print_cr("Created %d overpass methods", overpasses.length()); + tty->print_cr("Created %d default methods", defaults.length()); } #endif // ndef PRODUCT - switchover_constant_pool(&bpool, klass, &overpasses, CHECK); - merge_in_new_methods(klass, &overpasses, CHECK); + if (overpasses.length() > 0) { + switchover_constant_pool(&bpool, klass, &overpasses, CHECK); + merge_in_new_methods(klass, &overpasses, CHECK); + } + if (defaults.length() > 0) { + create_default_methods(klass, &defaults, CHECK); + } +} + +static void create_default_methods( InstanceKlass* klass, + GrowableArray* new_methods, TRAPS) { + + int new_size = new_methods->length(); + Array* total_default_methods = MetadataFactory::new_array( + klass->class_loader_data(), new_size, NULL, CHECK); + for (int index = 0; index < new_size; index++ ) { + total_default_methods->at_put(index, new_methods->at(index)); + } + Method::sort_methods(total_default_methods, false, false); + + klass->set_default_methods(total_default_methods); } static void sort_methods(GrowableArray* methods) { diff -r deec468baebd -r c01f4910f5f5 src/share/vm/classfile/javaClasses.cpp --- a/src/share/vm/classfile/javaClasses.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/classfile/javaClasses.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1376,8 +1376,15 @@ const char* klass_name = holder->external_name(); int buf_len = (int)strlen(klass_name); - // pushing to the stack trace added one. + // The method id may point to an obsolete method, can't get more stack information Method* method = holder->method_with_idnum(method_id); + if (method == NULL) { + char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64); + // This is what the java code prints in this case - added Redefined + sprintf(buf, "\tat %s.null (Redefined)", klass_name); + return buf; + } + char* method_name = method->name()->as_C_string(); buf_len += (int)strlen(method_name); @@ -1773,7 +1780,8 @@ return element; } -oop java_lang_StackTraceElement::create(Handle mirror, int method_id, int version, int bci, TRAPS) { +oop java_lang_StackTraceElement::create(Handle mirror, int method_id, + int version, int bci, TRAPS) { // Allocate java.lang.StackTraceElement instance Klass* k = SystemDictionary::StackTraceElement_klass(); assert(k != NULL, "must be loaded in 1.4+"); @@ -1790,8 +1798,16 @@ oop classname = StringTable::intern((char*) str, CHECK_0); java_lang_StackTraceElement::set_declaringClass(element(), classname); + Method* method = holder->method_with_idnum(method_id); + // Method on stack may be obsolete because it was redefined so cannot be + // found by idnum. + if (method == NULL) { + // leave name and fileName null + java_lang_StackTraceElement::set_lineNumber(element(), -1); + return element(); + } + // Fill in method name - Method* method = holder->method_with_idnum(method_id); oop methodname = StringTable::intern(method->name(), CHECK_0); java_lang_StackTraceElement::set_methodName(element(), methodname); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/classfile/symbolTable.hpp --- a/src/share/vm/classfile/symbolTable.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/classfile/symbolTable.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -107,18 +107,13 @@ add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); } - // Table size - enum { - symbol_table_size = 20011 - }; - Symbol* lookup(int index, const char* name, int len, unsigned int hash); SymbolTable() - : Hashtable(symbol_table_size, sizeof (HashtableEntry)) {} + : Hashtable(SymbolTableSize, sizeof (HashtableEntry)) {} SymbolTable(HashtableBucket* t, int number_of_entries) - : Hashtable(symbol_table_size, sizeof (HashtableEntry), t, + : Hashtable(SymbolTableSize, sizeof (HashtableEntry), t, number_of_entries) {} // Arena for permanent symbols (null class loader) that are never unloaded @@ -136,6 +131,9 @@ // The symbol table static SymbolTable* the_table() { return _the_table; } + // Size of one bucket in the string table. Used when checking for rollover. + static uint bucket_size() { return sizeof(HashtableBucket); } + static void create_table() { assert(_the_table == NULL, "One symbol table allowed."); _the_table = new SymbolTable(); @@ -145,8 +143,11 @@ static void create_table(HashtableBucket* t, int length, int number_of_entries) { assert(_the_table == NULL, "One symbol table allowed."); - assert(length == symbol_table_size * sizeof(HashtableBucket), - "bad shared symbol size."); + + // If CDS archive used a different symbol table size, use that size instead + // which is better than giving an error. + SymbolTableSize = length/bucket_size(); + _the_table = new SymbolTable(t, number_of_entries); // if CDS give symbol table a default arena size since most symbols // are already allocated in the shared misc section. diff -r deec468baebd -r c01f4910f5f5 src/share/vm/classfile/verifier.cpp --- a/src/share/vm/classfile/verifier.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/classfile/verifier.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -2442,10 +2442,16 @@ bool subtype = ref_class_type.is_assignable_from( current_type(), this, CHECK_VERIFY(this)); if (!subtype) { - verify_error(ErrorContext::bad_code(bci), - "Bad invokespecial instruction: " - "current class isn't assignable to reference class."); - return; + if (current_class()->is_anonymous()) { + subtype = ref_class_type.is_assignable_from(VerificationType::reference_type( + current_class()->host_klass()->name()), this, CHECK_VERIFY(this)); + } + if (!subtype) { + verify_error(ErrorContext::bad_code(bci), + "Bad invokespecial instruction: " + "current class isn't assignable to reference class."); + return; + } } } // Match method descriptor with operand stack @@ -2461,7 +2467,28 @@ } else { // other methods // Ensures that target class is assignable to method class. if (opcode == Bytecodes::_invokespecial) { - current_frame->pop_stack(current_type(), CHECK_VERIFY(this)); + if (!current_class()->is_anonymous()) { + current_frame->pop_stack(current_type(), CHECK_VERIFY(this)); + } else { + // anonymous class invokespecial calls: either the + // operand stack/objectref is a subtype of the current class OR + // the objectref is a subtype of the host_klass of the current class + // to allow an anonymous class to reference methods in the host_klass + VerificationType top = current_frame->pop_stack(CHECK_VERIFY(this)); + bool subtype = current_type().is_assignable_from(top, this, CHECK_VERIFY(this)); + if (!subtype) { + VerificationType hosttype = + VerificationType::reference_type(current_class()->host_klass()->name()); + subtype = hosttype.is_assignable_from(top, this, CHECK_VERIFY(this)); + } + if (!subtype) { + verify_error( ErrorContext::bad_type(current_frame->offset(), + current_frame->stack_top_ctx(), + TypeOrigin::implicit(top)), + "Bad type on operand stack"); + return; + } + } } else if (opcode == Bytecodes::_invokevirtual) { VerificationType stack_object_type = current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this)); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/code/dependencies.cpp --- a/src/share/vm/code/dependencies.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/code/dependencies.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -812,8 +812,8 @@ Klass* k = ctxk; Method* lm = k->lookup_method(m->name(), m->signature()); if (lm == NULL && k->oop_is_instance()) { - // It might be an abstract interface method, devoid of mirandas. - lm = ((InstanceKlass*)k)->lookup_method_in_all_interfaces(m->name(), + // It might be an interface method + lm = ((InstanceKlass*)k)->lookup_method_in_ordered_interfaces(m->name(), m->signature()); } if (lm == m) diff -r deec468baebd -r c01f4910f5f5 src/share/vm/interpreter/linkResolver.cpp --- a/src/share/vm/interpreter/linkResolver.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/interpreter/linkResolver.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1,4 +1,5 @@ /* + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -221,8 +222,17 @@ // // According to JVM spec. $5.4.3c & $5.4.3d +// Look up method in klasses, including static methods +// Then look up local default methods void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { Method* result_oop = klass->uncached_lookup_method(name, signature); + if (result_oop == NULL) { + Array* default_methods = InstanceKlass::cast(klass())->default_methods(); + if (default_methods != NULL) { + result_oop = InstanceKlass::find_method(default_methods, name, signature); + } + } + if (EnableInvokeDynamic && result_oop != NULL) { vmIntrinsics::ID iid = result_oop->intrinsic_id(); if (MethodHandles::is_signature_polymorphic(iid)) { @@ -234,6 +244,7 @@ } // returns first instance method +// Looks up method in classes, then looks up local default methods void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { Method* result_oop = klass->uncached_lookup_method(name, signature); result = methodHandle(THREAD, result_oop); @@ -241,13 +252,38 @@ klass = KlassHandle(THREAD, result->method_holder()->super()); result = methodHandle(THREAD, klass->uncached_lookup_method(name, signature)); } + + if (result.is_null()) { + Array* default_methods = InstanceKlass::cast(klass())->default_methods(); + if (default_methods != NULL) { + result = methodHandle(InstanceKlass::find_method(default_methods, name, signature)); + assert(result.is_null() || !result->is_static(), "static defaults not allowed"); + } + } } +int LinkResolver::vtable_index_of_interface_method(KlassHandle klass, + methodHandle resolved_method, TRAPS) { -int LinkResolver::vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { - ResourceMark rm(THREAD); - klassVtable *vt = InstanceKlass::cast(klass())->vtable(); - return vt->index_of_miranda(name, signature); + int vtable_index = Method::invalid_vtable_index; + Symbol* name = resolved_method->name(); + Symbol* signature = resolved_method->signature(); + + // First check in default method array + if (!resolved_method->is_abstract() && + (InstanceKlass::cast(klass())->default_methods() != NULL)) { + int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), name, signature); + if (index >= 0 ) { + vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index); + } + } + if (vtable_index == Method::invalid_vtable_index) { + // get vtable_index for miranda methods + ResourceMark rm(THREAD); + klassVtable *vt = InstanceKlass::cast(klass())->vtable(); + vtable_index = vt->index_of_miranda(name, signature); + } + return vtable_index; } void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { @@ -625,6 +661,12 @@ resolved_method->method_holder()->internal_name() ); resolved_method->access_flags().print_on(tty); + if (resolved_method->is_default_method()) { + tty->print("default"); + } + if (resolved_method->is_overpass()) { + tty->print("overpass"); + } tty->cr(); } } @@ -853,6 +895,7 @@ resolved_method->signature())); THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); } + if (TraceItables && Verbose) { ResourceMark rm(THREAD); tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", @@ -864,8 +907,7 @@ resolved_method->method_holder()->internal_name() ); resolved_method->access_flags().print_on(tty); - if (resolved_method->method_holder()->is_interface() && - !resolved_method->is_abstract()) { + if (resolved_method->is_default_method()) { tty->print("default"); } if (resolved_method->is_overpass()) { @@ -945,10 +987,12 @@ sel_method->method_holder()->internal_name() ); sel_method->access_flags().print_on(tty); - if (sel_method->method_holder()->is_interface() && - !sel_method->is_abstract()) { + if (sel_method->is_default_method()) { tty->print("default"); } + if (sel_method->is_overpass()) { + tty->print("overpass"); + } tty->cr(); } @@ -996,26 +1040,25 @@ THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); } - if (PrintVtables && Verbose) { - ResourceMark rm(THREAD); - tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", - (current_klass.is_null() ? "" : current_klass->internal_name()), - (resolved_klass.is_null() ? "" : resolved_klass->internal_name()), - Method::name_and_sig_as_C_string(resolved_klass(), - resolved_method->name(), - resolved_method->signature()), - resolved_method->method_holder()->internal_name() - ); - resolved_method->access_flags().print_on(tty); - if (resolved_method->method_holder()->is_interface() && - !resolved_method->is_abstract()) { - tty->print("default"); - } - if (resolved_method->is_overpass()) { - tty->print("overpass"); - } - tty->cr(); - } + if (PrintVtables && Verbose) { + ResourceMark rm(THREAD); + tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", + (current_klass.is_null() ? "" : current_klass->internal_name()), + (resolved_klass.is_null() ? "" : resolved_klass->internal_name()), + Method::name_and_sig_as_C_string(resolved_klass(), + resolved_method->name(), + resolved_method->signature()), + resolved_method->method_holder()->internal_name() + ); + resolved_method->access_flags().print_on(tty); + if (resolved_method->is_default_method()) { + tty->print("default"); + } + if (resolved_method->is_overpass()) { + tty->print("overpass"); + } + tty->cr(); + } } // throws runtime exceptions @@ -1045,10 +1088,8 @@ // do lookup based on receiver klass using the vtable index if (resolved_method->method_holder()->is_interface()) { // miranda method - vtable_index = vtable_index_of_miranda_method(resolved_klass, - resolved_method->name(), - resolved_method->signature(), CHECK); - + vtable_index = vtable_index_of_interface_method(resolved_klass, + resolved_method, CHECK); assert(vtable_index >= 0 , "we should have valid vtable index at this point"); InstanceKlass* inst = InstanceKlass::cast(recv_klass()); @@ -1104,11 +1145,10 @@ vtable_index ); selected_method->access_flags().print_on(tty); - if (selected_method->method_holder()->is_interface() && - !selected_method->is_abstract()) { + if (selected_method->is_default_method()) { tty->print("default"); } - if (resolved_method->is_overpass()) { + if (selected_method->is_overpass()) { tty->print("overpass"); } tty->cr(); @@ -1191,7 +1231,6 @@ sel_method->name(), sel_method->signature())); } - // check if abstract if (check_null_and_abstract && sel_method->is_abstract()) { ResourceMark rm(THREAD); @@ -1220,11 +1259,10 @@ sel_method->method_holder()->internal_name() ); sel_method->access_flags().print_on(tty); - if (sel_method->method_holder()->is_interface() && - !sel_method->is_abstract()) { + if (sel_method->is_default_method()) { tty->print("default"); } - if (resolved_method->is_overpass()) { + if (sel_method->is_overpass()) { tty->print("overpass"); } tty->cr(); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/interpreter/linkResolver.hpp --- a/src/share/vm/interpreter/linkResolver.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/interpreter/linkResolver.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -130,8 +130,7 @@ static void lookup_polymorphic_method (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, KlassHandle current_klass, Handle *appendix_result_or_null, Handle *method_type_result, TRAPS); - static int vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); - + static int vtable_index_of_interface_method(KlassHandle klass, methodHandle resolved_method, TRAPS); static void resolve_klass (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); static void resolve_pool (KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/memory/heapInspection.hpp --- a/src/share/vm/memory/heapInspection.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/memory/heapInspection.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -73,6 +73,10 @@ "Number of bytes used by the InstanceKlass::methods() array") \ f(method_ordering_bytes, IK_method_ordering, \ "Number of bytes used by the InstanceKlass::method_ordering() array") \ + f(default_methods_array_bytes, IK_default_methods, \ + "Number of bytes used by the InstanceKlass::default_methods() array") \ + f(default_vtable_indices_bytes, IK_default_vtable_indices, \ + "Number of bytes used by the InstanceKlass::default_vtable_indices() array") \ f(local_interfaces_bytes, IK_local_interfaces, \ "Number of bytes used by the InstanceKlass::local_interfaces() array") \ f(transitive_interfaces_bytes, IK_transitive_interfaces, \ diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/instanceKlass.cpp --- a/src/share/vm/oops/instanceKlass.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/instanceKlass.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -238,6 +238,13 @@ } } +// create a new array of vtable_indices for default methods +Array* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) { + Array* vtable_indices = MetadataFactory::new_array(class_loader_data(), len, CHECK_NULL); + assert(default_vtable_indices() == NULL, "only create once"); + set_default_vtable_indices(vtable_indices); + return vtable_indices; +} InstanceKlass::InstanceKlass(int vtable_len, int itable_len, @@ -263,6 +270,8 @@ set_array_klasses(NULL); set_methods(NULL); set_method_ordering(NULL); + set_default_methods(NULL); + set_default_vtable_indices(NULL); set_local_interfaces(NULL); set_transitive_interfaces(NULL); init_implementor(); @@ -376,6 +385,21 @@ } set_method_ordering(NULL); + // default methods can be empty + if (default_methods() != NULL && + default_methods() != Universe::the_empty_method_array()) { + MetadataFactory::free_array(loader_data, default_methods()); + } + // Do NOT deallocate the default methods, they are owned by superinterfaces. + set_default_methods(NULL); + + // default methods vtable indices can be empty + if (default_vtable_indices() != NULL) { + MetadataFactory::free_array(loader_data, default_vtable_indices()); + } + set_default_vtable_indices(NULL); + + // This array is in Klass, but remove it with the InstanceKlass since // this place would be the only caller and it can share memory with transitive // interfaces. @@ -456,14 +480,14 @@ return java_lang_Class::signers(java_mirror()); } -volatile oop InstanceKlass::init_lock() const { +oop InstanceKlass::init_lock() const { // return the init lock from the mirror return java_lang_Class::init_lock(java_mirror()); } void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) { EXCEPTION_MARK; - volatile oop init_lock = this_oop->init_lock(); + oop init_lock = this_oop->init_lock(); ObjectLocker ol(init_lock, THREAD); // abort if someone beat us to the initialization @@ -608,7 +632,7 @@ // verification & rewriting { - volatile oop init_lock = this_oop->init_lock(); + oop init_lock = this_oop->init_lock(); ObjectLocker ol(init_lock, THREAD); // rewritten will have been set if loader constraint error found // on an earlier link attempt @@ -731,7 +755,7 @@ // refer to the JVM book page 47 for description of steps // Step 1 { - volatile oop init_lock = this_oop->init_lock(); + oop init_lock = this_oop->init_lock(); ObjectLocker ol(init_lock, THREAD); Thread *self = THREAD; // it's passed the current thread @@ -879,7 +903,7 @@ } void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) { - volatile oop init_lock = this_oop->init_lock(); + oop init_lock = this_oop->init_lock(); ObjectLocker ol(init_lock, THREAD); this_oop->set_init_state(state); ol.notify_all(CHECK); @@ -1354,32 +1378,44 @@ return -1; } +// find_method looks up the name/signature in the local methods array Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { return InstanceKlass::find_method(methods(), name, signature); } +// find_method looks up the name/signature in the local methods array Method* InstanceKlass::find_method( Array* methods, Symbol* name, Symbol* signature) { + int hit = find_method_index(methods, name, signature); + return hit >= 0 ? methods->at(hit): NULL; +} + +// Used directly for default_methods to find the index into the +// default_vtable_indices, and indirectly by find_method +// find_method_index looks in the local methods array to return the index +// of the matching name/signature +int InstanceKlass::find_method_index( + Array* methods, Symbol* name, Symbol* signature) { int hit = binary_search(methods, name); if (hit != -1) { Method* m = methods->at(hit); // Do linear search to find matching signature. First, quick check // for common case - if (m->signature() == signature) return m; + if (m->signature() == signature) return hit; // search downwards through overloaded methods int i; for (i = hit - 1; i >= 0; --i) { Method* m = methods->at(i); assert(m->is_method(), "must be method"); if (m->name() != name) break; - if (m->signature() == signature) return m; + if (m->signature() == signature) return i; } // search upwards for (i = hit + 1; i < methods->length(); ++i) { Method* m = methods->at(i); assert(m->is_method(), "must be method"); if (m->name() != name) break; - if (m->signature() == signature) return m; + if (m->signature() == signature) return i; } // not found #ifdef ASSERT @@ -1387,9 +1423,8 @@ assert(index == -1, err_msg("binary search should have found entry %d", index)); #endif } - return NULL; + return -1; } - int InstanceKlass::find_method_by_name(Symbol* name, int* end) { return find_method_by_name(methods(), name, end); } @@ -1408,6 +1443,7 @@ return -1; } +// lookup_method searches both the local methods array and all superclasses methods arrays Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const { Klass* klass = const_cast(this); while (klass != NULL) { @@ -1418,6 +1454,21 @@ return NULL; } +// lookup a method in the default methods list then in all transitive interfaces +// Do NOT return private or static methods +Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, + Symbol* signature) const { + Method* m = NULL; + if (default_methods() != NULL) { + m = find_method(default_methods(), name, signature); + } + // Look up interfaces + if (m == NULL) { + m = lookup_method_in_all_interfaces(name, signature); + } + return m; +} + // lookup a method in all the interfaces that this class implements // Do NOT return private or static methods, new in JDK8 which are not externally visible // They should only be found in the initial InterfaceMethodRef @@ -2548,6 +2599,42 @@ return m; } + +#if INCLUDE_JVMTI +// update default_methods for redefineclasses for methods that are +// not yet in the vtable due to concurrent subclass define and superinterface +// redefinition +// Note: those in the vtable, should have been updated via adjust_method_entries +void InstanceKlass::adjust_default_methods(Method** old_methods, Method** new_methods, + int methods_length, bool* trace_name_printed) { + // search the default_methods for uses of either obsolete or EMCP methods + if (default_methods() != NULL) { + for (int j = 0; j < methods_length; j++) { + Method* old_method = old_methods[j]; + Method* new_method = new_methods[j]; + + for (int index = 0; index < default_methods()->length(); index ++) { + if (default_methods()->at(index) == old_method) { + default_methods()->at_put(index, new_method); + if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { + if (!(*trace_name_printed)) { + // RC_TRACE_MESG macro has an embedded ResourceMark + RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s", + external_name(), + old_method->method_holder()->external_name())); + *trace_name_printed = true; + } + RC_TRACE(0x00100000, ("default method update: %s(%s) ", + new_method->name()->as_C_string(), + new_method->signature()->as_C_string())); + } + } + } + } + } +} +#endif // INCLUDE_JVMTI + // On-stack replacement stuff void InstanceKlass::add_osr_nmethod(nmethod* n) { // only one compilation can be active @@ -2742,11 +2829,21 @@ st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr(); if (Verbose || WizardMode) { Array* method_array = methods(); - for(int i = 0; i < method_array->length(); i++) { + for (int i = 0; i < method_array->length(); i++) { st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); } } - st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr(); + st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr(); + st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr(); + if (Verbose && default_methods() != NULL) { + Array* method_array = default_methods(); + for (int i = 0; i < method_array->length(); i++) { + st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); + } + } + if (default_vtable_indices() != NULL) { + st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr(); + } st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr(); st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr(); st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr(); @@ -3099,6 +3196,19 @@ } } + // Verify default methods + if (default_methods() != NULL) { + Array* methods = this->default_methods(); + for (int j = 0; j < methods->length(); j++) { + guarantee(methods->at(j)->is_method(), "non-method in methods array"); + } + for (int j = 0; j < methods->length() - 1; j++) { + Method* m1 = methods->at(j); + Method* m2 = methods->at(j + 1); + guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly"); + } + } + // Verify JNI static field identifiers if (jni_ids() != NULL) { jni_ids()->verify(this); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/instanceKlass.hpp --- a/src/share/vm/oops/instanceKlass.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/instanceKlass.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -269,12 +269,18 @@ // Method array. Array* _methods; + // Default Method Array, concrete methods inherited from interfaces + Array* _default_methods; // Interface (Klass*s) this class declares locally to implement. Array* _local_interfaces; // Interface (Klass*s) this class implements transitively. Array* _transitive_interfaces; // Int array containing the original order of method in the class file (for JVMTI). Array* _method_ordering; + // Int array containing the vtable_indices for default_methods + // offset matches _default_methods offset + Array* _default_vtable_indices; + // Instance and static variable information, starts with 6-tuples of shorts // [access, name index, sig index, initval index, low_offset, high_offset] // for all fields, followed by the generic signature data at the end of @@ -356,6 +362,15 @@ void set_method_ordering(Array* m) { _method_ordering = m; } void copy_method_ordering(intArray* m, TRAPS); + // default_methods + Array* default_methods() const { return _default_methods; } + void set_default_methods(Array* a) { _default_methods = a; } + + // default method vtable_indices + Array* default_vtable_indices() const { return _default_vtable_indices; } + void set_default_vtable_indices(Array* v) { _default_vtable_indices = v; } + Array* create_new_default_vtable_indices(int len, TRAPS); + // interfaces Array* local_interfaces() const { return _local_interfaces; } void set_local_interfaces(Array* a) { @@ -501,12 +516,18 @@ Method* find_method(Symbol* name, Symbol* signature) const; static Method* find_method(Array* methods, Symbol* name, Symbol* signature); + // find a local method index in default_methods (returns -1 if not found) + static int find_method_index(Array* methods, Symbol* name, Symbol* signature); + // lookup operation (returns NULL if not found) Method* uncached_lookup_method(Symbol* name, Symbol* signature) const; // lookup a method in all the interfaces that this class implements // (returns NULL if not found) Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature) const; + // lookup a method in local defaults then in all interfaces + // (returns NULL if not found) + Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const; // Find method indices by name. If a method with the specified name is // found the index to the first method is returned, and 'end' is filled in @@ -910,6 +931,11 @@ klassItable* itable() const; // return new klassItable wrapper Method* method_at_itable(Klass* holder, int index, TRAPS); +#if INCLUDE_JVMTI + void adjust_default_methods(Method** old_methods, Method** new_methods, + int methods_length, bool* trace_name_printed); +#endif // INCLUDE_JVMTI + // Garbage collection void oop_follow_contents(oop obj); int oop_adjust_pointers(oop obj); @@ -995,7 +1021,7 @@ // Must be one per class and it has to be a VM internal object so java code // cannot lock it (like the mirror). // It has to be an object not a Mutex because it's held through java calls. - volatile oop init_lock() const; + oop init_lock() const; private: // Static methods that are used to implement member methods where an exposed this pointer diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/klassVtable.cpp --- a/src/share/vm/oops/klassVtable.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/klassVtable.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -83,7 +83,7 @@ GrowableArray new_mirandas(20); // compute the number of mirandas methods that must be added to the end - get_mirandas(&new_mirandas, all_mirandas, super, methods, local_interfaces); + get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); *num_new_mirandas = new_mirandas.length(); vtable_length += *num_new_mirandas * vtableEntry::size(); @@ -186,7 +186,7 @@ assert(methods->at(i)->is_method(), "must be a Method*"); methodHandle mh(THREAD, methods->at(i)); - bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK); + bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK); if (needs_new_entry) { put_method_at(mh(), initialized); @@ -195,7 +195,35 @@ } } - // add miranda methods to end of vtable. + // update vtable with default_methods + Array* default_methods = ik()->default_methods(); + if (default_methods != NULL) { + len = default_methods->length(); + if (len > 0) { + Array* def_vtable_indices = NULL; + if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) { + def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK); + } else { + assert(def_vtable_indices->length() == len, "reinit vtable len?"); + } + for (int i = 0; i < len; i++) { + HandleMark hm(THREAD); + assert(default_methods->at(i)->is_method(), "must be a Method*"); + methodHandle mh(THREAD, default_methods->at(i)); + + bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK); + + // needs new entry + if (needs_new_entry) { + put_method_at(mh(), initialized); + def_vtable_indices->at_put(i, initialized); //set vtable index + initialized++; + } + } + } + } + + // add miranda methods; it will also return the updated initialized initialized = fill_in_mirandas(initialized); // In class hierarchies where the accessibility is not increasing (i.e., going from private -> @@ -230,14 +258,19 @@ #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm(THREAD); + char* sig = target_method()->name_and_sig_as_C_string(); tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ", supersuperklass->internal_name(), - _klass->internal_name(), (target_method() != NULL) ? - target_method()->name()->as_C_string() : "", vtable_index); + _klass->internal_name(), sig, vtable_index); super_method->access_flags().print_on(tty); + if (super_method->is_default_method()) { + tty->print("default"); + } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); - tty->cr(); + if (target_method->is_default_method()) { + tty->print("default"); + } } #endif /*PRODUCT*/ break; // return found superk @@ -258,16 +291,31 @@ // OR return true if a new vtable entry is required. // Only called for InstanceKlass's, i.e. not for arrays // If that changed, could not use _klass as handle for klass -bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, - bool checkconstraints, TRAPS) { +bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, + int super_vtable_len, int default_index, + bool checkconstraints, TRAPS) { ResourceMark rm; bool allocate_new = true; assert(klass->oop_is_instance(), "must be InstanceKlass"); - assert(klass == target_method()->method_holder(), "caller resp."); - // Initialize the method's vtable index to "nonvirtual". - // If we allocate a vtable entry, we will update it to a non-negative number. - target_method()->set_vtable_index(Method::nonvirtual_vtable_index); + Array* def_vtable_indices = NULL; + bool is_default = false; + // default methods are concrete methods in superinterfaces which are added to the vtable + // with their real method_holder + // Since vtable and itable indices share the same storage, don't touch + // the default method's real vtable/itable index + // default_vtable_indices stores the vtable value relative to this inheritor + if (default_index >= 0 ) { + is_default = true; + def_vtable_indices = klass->default_vtable_indices(); + assert(def_vtable_indices != NULL, "def vtable alloc?"); + assert(default_index <= def_vtable_indices->length(), "def vtable len?"); + } else { + assert(klass == target_method()->method_holder(), "caller resp."); + // Initialize the method's vtable index to "nonvirtual". + // If we allocate a vtable entry, we will update it to a non-negative number. + target_method()->set_vtable_index(Method::nonvirtual_vtable_index); + } // Static and methods are never in if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { @@ -284,6 +332,8 @@ // An interface never allocates new vtable slots, only inherits old ones. // This method will either be assigned its own itable index later, // or be assigned an inherited vtable index in the loop below. + // default methods store their vtable indices in the inheritors default_vtable_indices + assert (default_index == -1, "interfaces don't store resolved default methods"); target_method()->set_vtable_index(Method::pending_itable_index); } @@ -307,8 +357,15 @@ Symbol* name = target_method()->name(); Symbol* signature = target_method()->signature(); - Handle target_loader(THREAD, _klass()->class_loader()); - Symbol* target_classname = _klass->name(); + + KlassHandle target_klass(THREAD, target_method()->method_holder()); + if (target_klass == NULL) { + target_klass = _klass; + } + + Handle target_loader(THREAD, target_klass->class_loader()); + + Symbol* target_classname = target_klass->name(); for(int i = 0; i < super_vtable_len; i++) { Method* super_method = method_at(i); // Check if method name matches @@ -317,10 +374,14 @@ // get super_klass for method_holder for the found method InstanceKlass* super_klass = super_method->method_holder(); - if ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) || - ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) - && ((super_klass = find_transitive_override(super_klass, target_method, i, target_loader, - target_classname, THREAD)) != (InstanceKlass*)NULL))) { + if (is_default + || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) + || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) + && ((super_klass = find_transitive_override(super_klass, + target_method, i, target_loader, + target_classname, THREAD)) + != (InstanceKlass*)NULL)))) + { // overriding, so no new entry allocate_new = false; @@ -347,7 +408,7 @@ "%s used in the signature"; char* sig = target_method()->name_and_sig_as_C_string(); const char* loader1 = SystemDictionary::loader_name(target_loader()); - char* current = _klass->name()->as_C_string(); + char* current = target_klass->name()->as_C_string(); const char* loader2 = SystemDictionary::loader_name(super_loader()); char* failed_type_name = failed_type_symbol->as_C_string(); size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + @@ -360,16 +421,39 @@ } } - put_method_at(target_method(), i); - target_method()->set_vtable_index(i); + put_method_at(target_method(), i); + if (!is_default) { + target_method()->set_vtable_index(i); + } else { + if (def_vtable_indices != NULL) { + def_vtable_indices->at_put(default_index, i); + } + assert(super_method->is_default_method() || super_method->is_overpass() + || super_method->is_abstract(), "default override error"); + } + + #ifndef PRODUCT if (PrintVtables && Verbose) { + ResourceMark rm(THREAD); + char* sig = target_method()->name_and_sig_as_C_string(); tty->print("overriding with %s::%s index %d, original flags: ", - _klass->internal_name(), (target_method() != NULL) ? - target_method()->name()->as_C_string() : "", i); + target_klass->internal_name(), sig, i); super_method->access_flags().print_on(tty); + if (super_method->is_default_method()) { + tty->print("default"); + } + if (super_method->is_overpass()) { + tty->print("overpass"); + } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); + if (target_method->is_default_method()) { + tty->print("default"); + } + if (target_method->is_overpass()) { + tty->print("overpass"); + } tty->cr(); } #endif /*PRODUCT*/ @@ -378,12 +462,25 @@ // but not override another. Once we override one, not need new #ifndef PRODUCT if (PrintVtables && Verbose) { + ResourceMark rm(THREAD); + char* sig = target_method()->name_and_sig_as_C_string(); tty->print("NOT overriding with %s::%s index %d, original flags: ", - _klass->internal_name(), (target_method() != NULL) ? - target_method()->name()->as_C_string() : "", i); + target_klass->internal_name(), sig,i); super_method->access_flags().print_on(tty); + if (super_method->is_default_method()) { + tty->print("default"); + } + if (super_method->is_overpass()) { + tty->print("overpass"); + } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); + if (target_method->is_default_method()) { + tty->print("default"); + } + if (target_method->is_overpass()) { + tty->print("overpass"); + } tty->cr(); } #endif /*PRODUCT*/ @@ -438,6 +535,14 @@ return false; } + // Concrete interface methods do not need new entries, they override + // abstract method entries using default inheritance rules + if (target_method()->method_holder() != NULL && + target_method()->method_holder()->is_interface() && + !target_method()->is_abstract() ) { + return false; + } + // we need a new entry if there is no superclass if (super == NULL) { return true; @@ -446,7 +551,7 @@ // private methods in classes always have a new entry in the vtable // specification interpretation since classic has // private methods not overriding - // JDK8 adds private methods in interfaces which require invokespecial + // JDK8 adds private methods in interfaces which require invokespecial if (target_method()->is_private()) { return true; } @@ -526,35 +631,40 @@ if (mhk->is_interface()) { assert(m->is_public(), "should be public"); assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); - assert(is_miranda(m, ik()->methods(), ik()->super()), "should be a miranda_method"); + assert(is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super()), "should be a miranda_method"); return true; } return false; } -// check if a method is a miranda method, given a class's methods table and its super -// "miranda" means not static, not defined by this class, and not defined -// in super unless it is private and therefore inaccessible to this class. +// check if a method is a miranda method, given a class's methods table, +// its default_method table and its super +// "miranda" means not static, not defined by this class. +// private methods in interfaces do not belong in the miranda list. // the caller must make sure that the method belongs to an interface implemented by the class // Miranda methods only include public interface instance methods -// Not private methods, not static methods, not default = concrete abstract -bool klassVtable::is_miranda(Method* m, Array* class_methods, Klass* super) { - if (m->is_static()) { +// Not private methods, not static methods, not default == concrete abstract +bool klassVtable::is_miranda(Method* m, Array* class_methods, + Array* default_methods, Klass* super) { + if (m->is_static() || m->is_private()) { return false; } Symbol* name = m->name(); Symbol* signature = m->signature(); if (InstanceKlass::find_method(class_methods, name, signature) == NULL) { // did not find it in the method table of the current class - if (super == NULL) { - // super doesn't exist - return true; - } + if ((default_methods == NULL) || + InstanceKlass::find_method(default_methods, name, signature) == NULL) { + if (super == NULL) { + // super doesn't exist + return true; + } - Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); - if (mo == NULL || mo->access_flags().is_private() ) { - // super class hierarchy does not implement it or protection is different - return true; + Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); + if (mo == NULL || mo->access_flags().is_private() ) { + // super class hierarchy does not implement it or protection is different + return true; + } } } @@ -562,7 +672,7 @@ } // Scans current_interface_methods for miranda methods that do not -// already appear in new_mirandas and are also not defined-and-non-private +// already appear in new_mirandas, or default methods, and are also not defined-and-non-private // in super (superclass). These mirandas are added to all_mirandas if it is // not null; in addition, those that are not duplicates of miranda methods // inherited by super from its interfaces are added to new_mirandas. @@ -572,7 +682,8 @@ void klassVtable::add_new_mirandas_to_lists( GrowableArray* new_mirandas, GrowableArray* all_mirandas, Array* current_interface_methods, Array* class_methods, - Klass* super) { + Array* default_methods, Klass* super) { + // iterate thru the current interface's method to see if it a miranda int num_methods = current_interface_methods->length(); for (int i = 0; i < num_methods; i++) { @@ -590,7 +701,7 @@ } if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable - if (is_miranda(im, class_methods, super)) { // is it a miranda at all? + if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all? InstanceKlass *sk = InstanceKlass::cast(super); // check if it is a duplicate of a super's miranda if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) { @@ -607,6 +718,7 @@ void klassVtable::get_mirandas(GrowableArray* new_mirandas, GrowableArray* all_mirandas, Klass* super, Array* class_methods, + Array* default_methods, Array* local_interfaces) { assert((new_mirandas->length() == 0) , "current mirandas must be 0"); @@ -615,14 +727,16 @@ for (int i = 0; i < num_local_ifs; i++) { InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i)); add_new_mirandas_to_lists(new_mirandas, all_mirandas, - ik->methods(), class_methods, super); + ik->methods(), class_methods, + default_methods, super); // iterate thru each local's super interfaces Array* super_ifs = ik->transitive_interfaces(); int num_super_ifs = super_ifs->length(); for (int j = 0; j < num_super_ifs; j++) { InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j)); add_new_mirandas_to_lists(new_mirandas, all_mirandas, - sik->methods(), class_methods, super); + sik->methods(), class_methods, + default_methods, super); } } } @@ -633,8 +747,22 @@ int klassVtable::fill_in_mirandas(int initialized) { GrowableArray mirandas(20); get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), - ik()->local_interfaces()); + ik()->default_methods(), ik()->local_interfaces()); for (int i = 0; i < mirandas.length(); i++) { + if (PrintVtables && Verbose) { + Method* meth = mirandas.at(i); + ResourceMark rm(Thread::current()); + if (meth != NULL) { + char* sig = meth->name_and_sig_as_C_string(); + tty->print("fill in mirandas with %s index %d, flags: ", + sig, initialized); + meth->access_flags().print_on(tty); + if (meth->is_default_method()) { + tty->print("default"); + } + tty->cr(); + } + } put_method_at(mirandas.at(i), initialized); ++initialized; } @@ -648,6 +776,26 @@ } #if INCLUDE_JVMTI +bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) { + // If old_method is default, find this vtable index in default_vtable_indices + // and replace that method in the _default_methods list + bool updated = false; + + Array* default_methods = ik()->default_methods(); + if (default_methods != NULL) { + int len = default_methods->length(); + for (int idx = 0; idx < len; idx++) { + if (vtable_index == ik()->default_vtable_indices()->at(idx)) { + if (default_methods->at(idx) == old_method) { + default_methods->at_put(idx, new_method); + updated = true; + } + break; + } + } + } + return updated; +} void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods, int methods_length, bool * trace_name_printed) { // search the vtable for uses of either obsolete or EMCP methods @@ -663,18 +811,26 @@ for (int index = 0; index < length(); index++) { if (unchecked_method_at(index) == old_method) { put_method_at(new_method, index); + // For default methods, need to update the _default_methods array + // which can only have one method entry for a given signature + bool updated_default = false; + if (old_method->is_default_method()) { + updated_default = adjust_default_method(index, old_method, new_method); + } if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { if (!(*trace_name_printed)) { // RC_TRACE_MESG macro has an embedded ResourceMark - RC_TRACE_MESG(("adjust: name=%s", + RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s", + klass()->external_name(), old_method->method_holder()->external_name())); *trace_name_printed = true; } // RC_TRACE macro has an embedded ResourceMark - RC_TRACE(0x00100000, ("vtable method update: %s(%s)", + RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s", new_method->name()->as_C_string(), - new_method->signature()->as_C_string())); + new_method->signature()->as_C_string(), + updated_default ? "true" : "false")); } // cannot 'break' here; see for-loop comment above. } @@ -701,6 +857,12 @@ if (m != NULL) { tty->print(" (%5d) ", i); m->access_flags().print_on(tty); + if (m->is_default_method()) { + tty->print("default"); + } + if (m->is_overpass()) { + tty->print("overpass"); + } tty->print(" -- "); m->print_name(tty); tty->cr(); @@ -757,9 +919,9 @@ // Initialization void klassItable::initialize_itable(bool checkconstraints, TRAPS) { if (_klass->is_interface()) { - // This needs to go after vtable indexes are assigned but - // before implementors need to know the number of itable indexes. - assign_itable_indexes_for_interface(_klass()); + // This needs to go after vtable indices are assigned but + // before implementors need to know the number of itable indices. + assign_itable_indices_for_interface(_klass()); } // Cannot be setup doing bootstrapping, interfaces don't have @@ -803,7 +965,7 @@ return true; } -int klassItable::assign_itable_indexes_for_interface(Klass* klass) { +int klassItable::assign_itable_indices_for_interface(Klass* klass) { // an interface does not have an itable, but its methods need to be numbered if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count, klass->name()->as_C_string()); @@ -846,7 +1008,7 @@ } nof_methods -= 1; } - // no methods have itable indexes + // no methods have itable indices return 0; } @@ -907,6 +1069,21 @@ int ime_num = m->itable_index(); assert(ime_num < ime_count, "oob"); itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); + if (TraceItables && Verbose) { + ResourceMark rm(THREAD); + if (target() != NULL) { + char* sig = target()->name_and_sig_as_C_string(); + tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ", + interf_h()->internal_name(), ime_num, sig, + target()->method_holder()->internal_name()); + tty->print("target_method flags: "); + target()->access_flags().print_on(tty); + if (target()->is_default_method()) { + tty->print("default"); + } + tty->cr(); + } + } } } } @@ -980,6 +1157,9 @@ if (m != NULL) { tty->print(" (%5d) ", i); m->access_flags().print_on(tty); + if (m->is_default_method()) { + tty->print("default"); + } tty->print(" -- "); m->print_name(tty); tty->cr(); @@ -1116,7 +1296,7 @@ Array* methods = InstanceKlass::cast(intf)->methods(); if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) - return NULL; // help caller defend against bad indexes + return NULL; // help caller defend against bad indices int index = itable_index; Method* m = methods->at(index); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/klassVtable.hpp --- a/src/share/vm/oops/klassVtable.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/klassVtable.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -97,6 +97,7 @@ // trace_name_printed is set to true if the current call has // printed the klass name so that other routines in the adjust_* // group don't print the klass name. + bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method); void adjust_method_entries(Method** old_methods, Method** new_methods, int methods_length, bool * trace_name_printed); bool check_no_old_or_obsolete_entries(); @@ -118,24 +119,28 @@ void put_method_at(Method* m, int index); static bool needs_new_vtable_entry(methodHandle m, Klass* super, Handle classloader, Symbol* classname, AccessFlags access_flags, TRAPS); - bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, bool checkconstraints, TRAPS); + bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS); InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index, Handle target_loader, Symbol* target_classname, Thread* THREAD); // support for miranda methods bool is_miranda_entry_at(int i); int fill_in_mirandas(int initialized); - static bool is_miranda(Method* m, Array* class_methods, Klass* super); + static bool is_miranda(Method* m, Array* class_methods, + Array* default_methods, Klass* super); static void add_new_mirandas_to_lists( GrowableArray* new_mirandas, GrowableArray* all_mirandas, - Array* current_interface_methods, Array* class_methods, + Array* current_interface_methods, + Array* class_methods, + Array* default_methods, Klass* super); static void get_mirandas( GrowableArray* new_mirandas, GrowableArray* all_mirandas, Klass* super, - Array* class_methods, Array* local_interfaces); - + Array* class_methods, + Array* default_methods, + Array* local_interfaces); void verify_against(outputStream* st, klassVtable* vt, int index); inline InstanceKlass* ik() const; }; @@ -290,7 +295,7 @@ #endif // INCLUDE_JVMTI // Setup of itable - static int assign_itable_indexes_for_interface(Klass* klass); + static int assign_itable_indices_for_interface(Klass* klass); static int method_count_for_interface(Klass* klass); static int compute_itable_size(Array* transitive_interfaces); static void setup_itable_offset_table(instanceKlassHandle klass); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/method.cpp --- a/src/share/vm/oops/method.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/method.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -511,9 +511,9 @@ bool Method::is_final_method(AccessFlags class_access_flags) const { // or "does_not_require_vtable_entry" - // overpass can occur, is not final (reuses vtable entry) + // default method or overpass can occur, is not final (reuses vtable entry) // private methods get vtable entries for backward class compatibility. - if (is_overpass()) return false; + if (is_overpass() || is_default_method()) return false; return is_final() || class_access_flags.is_final(); } @@ -521,11 +521,24 @@ return is_final_method(method_holder()->access_flags()); } +bool Method::is_default_method() const { + if (method_holder() != NULL && + method_holder()->is_interface() && + !is_abstract()) { + return true; + } else { + return false; + } +} + bool Method::can_be_statically_bound(AccessFlags class_access_flags) const { if (is_final_method(class_access_flags)) return true; #ifdef ASSERT + ResourceMark rm; bool is_nonv = (vtable_index() == nonvirtual_vtable_index); - if (class_access_flags.is_interface()) assert(is_nonv == is_static(), err_msg("is_nonv=%s", is_nonv)); + if (class_access_flags.is_interface()) { + assert(is_nonv == is_static(), err_msg("is_nonv=%s", name_and_sig_as_C_string())); + } #endif assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question"); return vtable_index() == nonvirtual_vtable_index; @@ -1371,7 +1384,8 @@ } // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array -void Method::sort_methods(Array* methods, bool idempotent) { +// default_methods also uses this without the ordering for fast find_method +void Method::sort_methods(Array* methods, bool idempotent, bool set_idnums) { int length = methods->length(); if (length > 1) { { @@ -1379,14 +1393,15 @@ QuickSort::sort(methods->data(), length, method_comparator, idempotent); } // Reset method ordering - for (int i = 0; i < length; i++) { - Method* m = methods->at(i); - m->set_method_idnum(i); + if (set_idnums) { + for (int i = 0; i < length; i++) { + Method* m = methods->at(i); + m->set_method_idnum(i); + } } } } - //----------------------------------------------------------------------------------- // Non-product code unless JVM/TI needs it diff -r deec468baebd -r c01f4910f5f5 src/share/vm/oops/method.hpp --- a/src/share/vm/oops/method.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/oops/method.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -567,6 +567,7 @@ // checks method and its method holder bool is_final_method() const; bool is_final_method(AccessFlags class_access_flags) const; + bool is_default_method() const; // true if method needs no dynamic dispatch (final and/or no vtable entry) bool can_be_statically_bound() const; @@ -846,7 +847,7 @@ #endif // Helper routine used for method sorting - static void sort_methods(Array* methods, bool idempotent = false); + static void sort_methods(Array* methods, bool idempotent = false, bool set_idnums = true); // Deallocation function for redefine classes or if an error occurs void deallocate_contents(ClassLoaderData* loader_data); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jni.cpp --- a/src/share/vm/prims/jni.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jni.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1591,10 +1591,8 @@ } } else { m = klass->lookup_method(name, signature); - // Look up interfaces - if (m == NULL && klass->oop_is_instance()) { - m = InstanceKlass::cast(klass())->lookup_method_in_all_interfaces(name, - signature); + if (m == NULL && klass->oop_is_instance()) { + m = InstanceKlass::cast(klass())->lookup_method_in_ordered_interfaces(name, signature); } } if (m == NULL || (m->is_static() != is_static)) { @@ -3210,7 +3208,11 @@ HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY( env, string); #endif /* USDT2 */ - jsize ret = java_lang_String::length(JNIHandles::resolve_non_null(string)); + jsize ret = 0; + oop s = JNIHandles::resolve_non_null(string); + if (java_lang_String::value(s) != NULL) { + ret = java_lang_String::length(s); + } #ifndef USDT2 DTRACE_PROBE1(hotspot_jni, GetStringLength__return, ret); #else /* USDT2 */ @@ -3230,20 +3232,23 @@ HOTSPOT_JNI_GETSTRINGCHARS_ENTRY( env, string, (uintptr_t *) isCopy); #endif /* USDT2 */ + jchar* buf = NULL; oop s = JNIHandles::resolve_non_null(string); - int s_len = java_lang_String::length(s); typeArrayOop s_value = java_lang_String::value(s); - int s_offset = java_lang_String::offset(s); - jchar* buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination - /* JNI Specification states return NULL on OOM */ - if (buf != NULL) { - if (s_len > 0) { - memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len); - } - buf[s_len] = 0; - //%note jni_5 - if (isCopy != NULL) { - *isCopy = JNI_TRUE; + if (s_value != NULL) { + int s_len = java_lang_String::length(s); + int s_offset = java_lang_String::offset(s); + buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination + /* JNI Specification states return NULL on OOM */ + if (buf != NULL) { + if (s_len > 0) { + memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len); + } + buf[s_len] = 0; + //%note jni_5 + if (isCopy != NULL) { + *isCopy = JNI_TRUE; + } } } #ifndef USDT2 @@ -3313,7 +3318,11 @@ HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY( env, string); #endif /* USDT2 */ - jsize ret = java_lang_String::utf8_length(JNIHandles::resolve_non_null(string)); + jsize ret = 0; + oop java_string = JNIHandles::resolve_non_null(string); + if (java_lang_String::value(java_string) != NULL) { + ret = java_lang_String::utf8_length(java_string); + } #ifndef USDT2 DTRACE_PROBE1(hotspot_jni, GetStringUTFLength__return, ret); #else /* USDT2 */ @@ -3332,14 +3341,17 @@ HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY( env, string, (uintptr_t *) isCopy); #endif /* USDT2 */ + char* result = NULL; oop java_string = JNIHandles::resolve_non_null(string); - size_t length = java_lang_String::utf8_length(java_string); - /* JNI Specification states return NULL on OOM */ - char* result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL); - if (result != NULL) { - java_lang_String::as_utf8_string(java_string, result, (int) length + 1); - if (isCopy != NULL) { - *isCopy = JNI_TRUE; + if (java_lang_String::value(java_string) != NULL) { + size_t length = java_lang_String::utf8_length(java_string); + /* JNI Specification states return NULL on OOM */ + result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL); + if (result != NULL) { + java_lang_String::as_utf8_string(java_string, result, (int) length + 1); + if (isCopy != NULL) { + *isCopy = JNI_TRUE; + } } } #ifndef USDT2 diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jniCheck.cpp --- a/src/share/vm/prims/jniCheck.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jniCheck.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1324,18 +1324,19 @@ IN_VM( checkString(thr, str); ) + jchar* newResult = NULL; const jchar *result = UNCHECKED()->GetStringChars(env,str,isCopy); assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringChars didn't return a copy as expected"); - - size_t len = UNCHECKED()->GetStringLength(env,str) + 1; // + 1 for NULL termination - jint* tagLocation = (jint*) AllocateHeap(len * sizeof(jchar) + sizeof(jint), mtInternal); - *tagLocation = STRING_TAG; - jchar* newResult = (jchar*) (tagLocation + 1); - memcpy(newResult, result, len * sizeof(jchar)); - // Avoiding call to UNCHECKED()->ReleaseStringChars() since that will fire unexpected dtrace probes - // Note that the dtrace arguments for the allocated memory will not match up with this solution. - FreeHeap((char*)result); - + if (result != NULL) { + size_t len = UNCHECKED()->GetStringLength(env,str) + 1; // + 1 for NULL termination + jint* tagLocation = (jint*) AllocateHeap(len * sizeof(jchar) + sizeof(jint), mtInternal); + *tagLocation = STRING_TAG; + newResult = (jchar*) (tagLocation + 1); + memcpy(newResult, result, len * sizeof(jchar)); + // Avoiding call to UNCHECKED()->ReleaseStringChars() since that will fire unexpected dtrace probes + // Note that the dtrace arguments for the allocated memory will not match up with this solution. + FreeHeap((char*)result); + } functionExit(env); return newResult; JNI_END @@ -1394,18 +1395,19 @@ IN_VM( checkString(thr, str); ) + char* newResult = NULL; const char *result = UNCHECKED()->GetStringUTFChars(env,str,isCopy); assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringUTFChars didn't return a copy as expected"); - - size_t len = strlen(result) + 1; // + 1 for NULL termination - jint* tagLocation = (jint*) AllocateHeap(len + sizeof(jint), mtInternal); - *tagLocation = STRING_UTF_TAG; - char* newResult = (char*) (tagLocation + 1); - strcpy(newResult, result); - // Avoiding call to UNCHECKED()->ReleaseStringUTFChars() since that will fire unexpected dtrace probes - // Note that the dtrace arguments for the allocated memory will not match up with this solution. - FreeHeap((char*)result, mtInternal); - + if (result != NULL) { + size_t len = strlen(result) + 1; // + 1 for NULL termination + jint* tagLocation = (jint*) AllocateHeap(len + sizeof(jint), mtInternal); + *tagLocation = STRING_UTF_TAG; + newResult = (char*) (tagLocation + 1); + strcpy(newResult, result); + // Avoiding call to UNCHECKED()->ReleaseStringUTFChars() since that will fire unexpected dtrace probes + // Note that the dtrace arguments for the allocated memory will not match up with this solution. + FreeHeap((char*)result, mtInternal); + } functionExit(env); return newResult; JNI_END diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jvm.cpp --- a/src/share/vm/prims/jvm.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jvm.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -668,13 +668,12 @@ JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth)) JVMWrapper("JVM_GetCallerClass"); - // Pre-JDK 8 and early builds of JDK 8 don't have a CallerSensitive annotation. - if (SystemDictionary::reflect_CallerSensitive_klass() == NULL) { + // Pre-JDK 8 and early builds of JDK 8 don't have a CallerSensitive annotation; or + // sun.reflect.Reflection.getCallerClass with a depth parameter is provided + // temporarily for existing code to use until a replacement API is defined. + if (SystemDictionary::reflect_CallerSensitive_klass() == NULL || depth != JVM_CALLER_DEPTH) { Klass* k = thread->security_get_caller_class(depth); return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, k->java_mirror()); - } else { - // Basic handshaking with Java_sun_reflect_Reflection_getCallerClass - assert(depth == -1, "wrong handshake depth"); } // Getting the class of the caller frame. @@ -3954,248 +3953,6 @@ } -// Serialization -JVM_ENTRY(void, JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, - jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)) - assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier"); - - typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes)); - typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data)); - typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs)); - oop o = JNIHandles::resolve(obj); - - if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) { - THROW(vmSymbols::java_lang_NullPointerException()); - } - - jsize nfids = fids->length(); - if (nfids == 0) return; - - if (tcodes->length() < nfids) { - THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); - } - - jsize off = 0; - /* loop through fields, setting values */ - for (jsize i = 0; i < nfids; i++) { - jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i); - int field_offset; - if (fid != NULL) { - // NULL is a legal value for fid, but retrieving the field offset - // trigger assertion in that case - field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); - } - - switch (tcodes->char_at(i)) { - case 'Z': - if (fid != NULL) { - jboolean val = (dbuf->byte_at(off) != 0) ? JNI_TRUE : JNI_FALSE; - o->bool_field_put(field_offset, val); - } - off++; - break; - - case 'B': - if (fid != NULL) { - o->byte_field_put(field_offset, dbuf->byte_at(off)); - } - off++; - break; - - case 'C': - if (fid != NULL) { - jchar val = ((dbuf->byte_at(off + 0) & 0xFF) << 8) - + ((dbuf->byte_at(off + 1) & 0xFF) << 0); - o->char_field_put(field_offset, val); - } - off += 2; - break; - - case 'S': - if (fid != NULL) { - jshort val = ((dbuf->byte_at(off + 0) & 0xFF) << 8) - + ((dbuf->byte_at(off + 1) & 0xFF) << 0); - o->short_field_put(field_offset, val); - } - off += 2; - break; - - case 'I': - if (fid != NULL) { - jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24) - + ((dbuf->byte_at(off + 1) & 0xFF) << 16) - + ((dbuf->byte_at(off + 2) & 0xFF) << 8) - + ((dbuf->byte_at(off + 3) & 0xFF) << 0); - o->int_field_put(field_offset, ival); - } - off += 4; - break; - - case 'F': - if (fid != NULL) { - jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24) - + ((dbuf->byte_at(off + 1) & 0xFF) << 16) - + ((dbuf->byte_at(off + 2) & 0xFF) << 8) - + ((dbuf->byte_at(off + 3) & 0xFF) << 0); - jfloat fval = (*int_bits_to_float_fn)(env, NULL, ival); - o->float_field_put(field_offset, fval); - } - off += 4; - break; - - case 'J': - if (fid != NULL) { - jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56) - + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48) - + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40) - + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32) - + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24) - + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16) - + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8) - + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0); - o->long_field_put(field_offset, lval); - } - off += 8; - break; - - case 'D': - if (fid != NULL) { - jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56) - + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48) - + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40) - + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32) - + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24) - + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16) - + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8) - + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0); - jdouble dval = (*long_bits_to_double_fn)(env, NULL, lval); - o->double_field_put(field_offset, dval); - } - off += 8; - break; - - default: - // Illegal typecode - THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode"); - } - } -JVM_END - - -JVM_ENTRY(void, JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, - jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)) - assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier"); - - typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes)); - typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data)); - typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs)); - oop o = JNIHandles::resolve(obj); - - if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) { - THROW(vmSymbols::java_lang_NullPointerException()); - } - - jsize nfids = fids->length(); - if (nfids == 0) return; - - if (tcodes->length() < nfids) { - THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); - } - - /* loop through fields, fetching values */ - jsize off = 0; - for (jsize i = 0; i < nfids; i++) { - jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i); - if (fid == NULL) { - THROW(vmSymbols::java_lang_NullPointerException()); - } - int field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); - - switch (tcodes->char_at(i)) { - case 'Z': - { - jboolean val = o->bool_field(field_offset); - dbuf->byte_at_put(off++, (val != 0) ? 1 : 0); - } - break; - - case 'B': - dbuf->byte_at_put(off++, o->byte_field(field_offset)); - break; - - case 'C': - { - jchar val = o->char_field(field_offset); - dbuf->byte_at_put(off++, (val >> 8) & 0xFF); - dbuf->byte_at_put(off++, (val >> 0) & 0xFF); - } - break; - - case 'S': - { - jshort val = o->short_field(field_offset); - dbuf->byte_at_put(off++, (val >> 8) & 0xFF); - dbuf->byte_at_put(off++, (val >> 0) & 0xFF); - } - break; - - case 'I': - { - jint val = o->int_field(field_offset); - dbuf->byte_at_put(off++, (val >> 24) & 0xFF); - dbuf->byte_at_put(off++, (val >> 16) & 0xFF); - dbuf->byte_at_put(off++, (val >> 8) & 0xFF); - dbuf->byte_at_put(off++, (val >> 0) & 0xFF); - } - break; - - case 'F': - { - jfloat fval = o->float_field(field_offset); - jint ival = (*float_to_int_bits_fn)(env, NULL, fval); - dbuf->byte_at_put(off++, (ival >> 24) & 0xFF); - dbuf->byte_at_put(off++, (ival >> 16) & 0xFF); - dbuf->byte_at_put(off++, (ival >> 8) & 0xFF); - dbuf->byte_at_put(off++, (ival >> 0) & 0xFF); - } - break; - - case 'J': - { - jlong val = o->long_field(field_offset); - dbuf->byte_at_put(off++, (val >> 56) & 0xFF); - dbuf->byte_at_put(off++, (val >> 48) & 0xFF); - dbuf->byte_at_put(off++, (val >> 40) & 0xFF); - dbuf->byte_at_put(off++, (val >> 32) & 0xFF); - dbuf->byte_at_put(off++, (val >> 24) & 0xFF); - dbuf->byte_at_put(off++, (val >> 16) & 0xFF); - dbuf->byte_at_put(off++, (val >> 8) & 0xFF); - dbuf->byte_at_put(off++, (val >> 0) & 0xFF); - } - break; - - case 'D': - { - jdouble dval = o->double_field(field_offset); - jlong lval = (*double_to_long_bits_fn)(env, NULL, dval); - dbuf->byte_at_put(off++, (lval >> 56) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 48) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 40) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 32) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 24) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 16) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 8) & 0xFF); - dbuf->byte_at_put(off++, (lval >> 0) & 0xFF); - } - break; - - default: - // Illegal typecode - THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode"); - } - } -JVM_END - // Shared JNI/JVM entry points ////////////////////////////////////////////////////////////// diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jvm.h --- a/src/share/vm/prims/jvm.h Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jvm.h Thu Oct 10 13:25:51 2013 -0700 @@ -374,6 +374,9 @@ /* * java.lang.Class and java.lang.ClassLoader */ + +#define JVM_CALLER_DEPTH -1 + /* * Returns the class in which the code invoking the native method * belongs. diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jvm_misc.hpp --- a/src/share/vm/prims/jvm_misc.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jvm_misc.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -36,22 +36,6 @@ void trace_class_resolution(Klass* to_class); /* - * Support for Serialization and RMI. Currently used by HotSpot only. - */ - -extern "C" { - -void JNICALL -JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, - jlongArray fieldIDs, jcharArray typecodes, jbyteArray data); - -void JNICALL -JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, - jlongArray fieldIDs, jcharArray typecodes, jbyteArray data); - -} - -/* * Support for -Xcheck:jni */ diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/jvmtiRedefineClasses.cpp --- a/src/share/vm/prims/jvmtiRedefineClasses.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/jvmtiRedefineClasses.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -2755,13 +2755,26 @@ // InstanceKlass around to hold obsolete methods so we don't have // any other InstanceKlass embedded vtables to update. The vtable // holds the Method*s for virtual (but not final) methods. - if (ik->vtable_length() > 0 && ik->is_subtype_of(_the_class_oop)) { + // Default methods, or concrete methods in interfaces are stored + // in the vtable, so if an interface changes we need to check + // adjust_method_entries() for every InstanceKlass, which will also + // adjust the default method vtable indices. + // We also need to adjust any default method entries that are + // not yet in the vtable, because the vtable setup is in progress. + // This must be done after we adjust the default_methods and + // default_vtable_indices for methods already in the vtable. + if (ik->vtable_length() > 0 && (_the_class_oop->is_interface() + || ik->is_subtype_of(_the_class_oop))) { // ik->vtable() creates a wrapper object; rm cleans it up ResourceMark rm(_thread); ik->vtable()->adjust_method_entries(_matching_old_methods, _matching_new_methods, _matching_methods_length, &trace_name_printed); + ik->adjust_default_methods(_matching_old_methods, + _matching_new_methods, + _matching_methods_length, + &trace_name_printed); } // If the current class has an itable and we are either redefining an @@ -2931,7 +2944,8 @@ old_method->set_is_obsolete(); obsolete_count++; - // obsolete methods need a unique idnum + // obsolete methods need a unique idnum so they become new entries in + // the jmethodID cache in InstanceKlass u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum(); if (num != ConstMethod::UNSET_IDNUM) { old_method->set_method_idnum(num); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/methodHandles.cpp --- a/src/share/vm/prims/methodHandles.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/methodHandles.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -187,12 +187,34 @@ receiver_limit = m->method_holder(); assert(receiver_limit->verify_itable_index(vmindex), ""); flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); + if (TraceInvokeDynamic) { + ResourceMark rm; + tty->print_cr("memberName: invokeinterface method_holder::method: %s, receiver: %s, itableindex: %d, access_flags:", + Method::name_and_sig_as_C_string(receiver_limit(), m->name(), m->signature()), + receiver_limit()->internal_name(), vmindex); + m->access_flags().print_on(tty); + if (!m->is_abstract()) { + tty->print("default"); + } + tty->cr(); + } break; case CallInfo::vtable_call: vmindex = info.vtable_index(); flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); assert(receiver_limit->is_subtype_of(m->method_holder()), "virtual call must be type-safe"); + if (TraceInvokeDynamic) { + ResourceMark rm; + tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:", + Method::name_and_sig_as_C_string(receiver_limit(), m->name(), m->signature()), + receiver_limit()->internal_name(), vmindex); + m->access_flags().print_on(tty); + if (m->is_default_method()) { + tty->print("default"); + } + tty->cr(); + } break; case CallInfo::direct_call: diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/nativeLookup.cpp --- a/src/share/vm/prims/nativeLookup.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/nativeLookup.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -129,10 +129,6 @@ #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) static JNINativeMethod lookup_special_native_methods[] = { - // Next two functions only exist for compatibility with 1.3.1 and earlier. - { CC"Java_java_io_ObjectOutputStream_getPrimitiveFieldValues", NULL, FN_PTR(JVM_GetPrimitiveFieldValues) }, // intercept ObjectOutputStream getPrimitiveFieldValues for faster serialization - { CC"Java_java_io_ObjectInputStream_setPrimitiveFieldValues", NULL, FN_PTR(JVM_SetPrimitiveFieldValues) }, // intercept ObjectInputStream setPrimitiveFieldValues for faster serialization - { CC"Java_sun_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterUnsafeMethods) }, { CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) }, { CC"Java_sun_misc_Perf_registerNatives", NULL, FN_PTR(JVM_RegisterPerfMethods) }, @@ -140,9 +136,8 @@ }; static address lookup_special_native(char* jni_name) { - int i = !JDK_Version::is_gte_jdk14x_version() ? 0 : 2; // see comment in lookup_special_native_methods int count = sizeof(lookup_special_native_methods) / sizeof(JNINativeMethod); - for (; i < count; i++) { + for (int i = 0; i < count; i++) { // NB: To ignore the jni prefix and jni postfix strstr is used matching. if (strstr(jni_name, lookup_special_native_methods[i].name) != NULL) { return CAST_FROM_FN_PTR(address, lookup_special_native_methods[i].fnPtr); diff -r deec468baebd -r c01f4910f5f5 src/share/vm/prims/wbtestmethods/parserTests.cpp --- a/src/share/vm/prims/wbtestmethods/parserTests.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/prims/wbtestmethods/parserTests.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -117,11 +117,12 @@ const char* c_cmdline = java_lang_String::as_utf8_string(JNIHandles::resolve(j_cmdline)); objArrayOop argumentArray = objArrayOop(JNIHandles::resolve_non_null(arguments)); + objArrayHandle argumentArray_ah(THREAD, argumentArray); - int length = argumentArray->length(); + int length = argumentArray_ah->length(); for (int i = 0; i < length; i++) { - oop argument_oop = argumentArray->obj_at(i); + oop argument_oop = argumentArray_ah->obj_at(i); fill_in_parser(&parser, argument_oop); } @@ -130,19 +131,20 @@ Klass* k = SystemDictionary::Object_klass(); objArrayOop returnvalue_array = oopFactory::new_objArray(k, parser.num_arguments() * 2, CHECK_NULL); + objArrayHandle returnvalue_array_ah(THREAD, returnvalue_array); GrowableArray*parsedArgNames = parser.argument_name_array(); for (int i = 0; i < parser.num_arguments(); i++) { oop parsedName = java_lang_String::create_oop_from_str(parsedArgNames->at(i), CHECK_NULL); - returnvalue_array->obj_at_put(i*2, parsedName); + returnvalue_array_ah->obj_at_put(i*2, parsedName); GenDCmdArgument* arg = parser.lookup_dcmd_option(parsedArgNames->at(i), strlen(parsedArgNames->at(i))); char buf[VALUE_MAXLEN]; arg->value_as_str(buf, sizeof(buf)); oop parsedValue = java_lang_String::create_oop_from_str(buf, CHECK_NULL); - returnvalue_array->obj_at_put(i*2+1, parsedValue); + returnvalue_array_ah->obj_at_put(i*2+1, parsedValue); } - return (jobjectArray) JNIHandles::make_local(returnvalue_array); + return (jobjectArray) JNIHandles::make_local(returnvalue_array_ah()); WB_END diff -r deec468baebd -r c01f4910f5f5 src/share/vm/runtime/arguments.cpp --- a/src/share/vm/runtime/arguments.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/runtime/arguments.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -2045,6 +2045,9 @@ status = status && verify_interval(StringTableSize, minimumStringTableSize, (max_uintx / StringTable::bucket_size()), "StringTable size"); + status = status && verify_interval(SymbolTableSize, minimumSymbolTableSize, + (max_uintx / SymbolTable::bucket_size()), "SymbolTable size"); + if (MinHeapFreeRatio > MaxHeapFreeRatio) { jio_fprintf(defaultStream::error_stream(), "MinHeapFreeRatio (" UINTX_FORMAT ") must be less than or " diff -r deec468baebd -r c01f4910f5f5 src/share/vm/runtime/globals.hpp --- a/src/share/vm/runtime/globals.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/runtime/globals.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -3727,6 +3727,9 @@ product(uintx, StringTableSize, defaultStringTableSize, \ "Number of buckets in the interned String table") \ \ + experimental(uintx, SymbolTableSize, defaultSymbolTableSize, \ + "Number of buckets in the JVM internal Symbol table") \ + \ develop(bool, TraceDefaultMethods, false, \ "Trace the default method processing steps") \ \ diff -r deec468baebd -r c01f4910f5f5 src/share/vm/runtime/reflectionUtils.cpp --- a/src/share/vm/runtime/reflectionUtils.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/runtime/reflectionUtils.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,8 +27,11 @@ #include "memory/universe.inline.hpp" #include "runtime/reflectionUtils.hpp" -KlassStream::KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only) { - _klass = klass; +KlassStream::KlassStream(instanceKlassHandle klass, bool local_only, + bool classes_only, bool walk_defaults) { + _klass = _base_klass = klass; + _base_class_search_defaults = false; + _defaults_checked = false; if (classes_only) { _interfaces = Universe::the_empty_klass_array(); } else { @@ -37,6 +40,7 @@ _interface_index = _interfaces->length(); _local_only = local_only; _classes_only = classes_only; + _walk_defaults = walk_defaults; } bool KlassStream::eos() { @@ -45,7 +49,13 @@ if (!_klass->is_interface() && _klass->super() != NULL) { // go up superclass chain (not for interfaces) _klass = _klass->super(); + // Next for method walks, walk default methods + } else if (_walk_defaults && (_defaults_checked == false) && (_base_klass->default_methods() != NULL)) { + _base_class_search_defaults = true; + _klass = _base_klass; + _defaults_checked = true; } else { + // Next walk transitive interfaces if (_interface_index > 0) { _klass = _interfaces->at(--_interface_index); } else { diff -r deec468baebd -r c01f4910f5f5 src/share/vm/runtime/reflectionUtils.hpp --- a/src/share/vm/runtime/reflectionUtils.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/runtime/reflectionUtils.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -38,7 +38,7 @@ // and (super)interfaces. Streaming is done in reverse order (subclasses first, // interfaces last). // -// for (KlassStream st(k, false, false); !st.eos(); st.next()) { +// for (KlassStream st(k, false, false, false); !st.eos(); st.next()) { // Klass* k = st.klass(); // ... // } @@ -46,17 +46,21 @@ class KlassStream VALUE_OBJ_CLASS_SPEC { protected: instanceKlassHandle _klass; // current klass/interface iterated over - Array* _interfaces; // transitive interfaces for initial class + instanceKlassHandle _base_klass; // initial klass/interface to iterate over + Array* _interfaces; // transitive interfaces for initial class int _interface_index; // current interface being processed bool _local_only; // process initial class/interface only bool _classes_only; // process classes only (no interfaces) + bool _walk_defaults; // process default methods + bool _base_class_search_defaults; // time to process default methods + bool _defaults_checked; // already checked for default methods int _index; - virtual int length() const = 0; + virtual int length() = 0; public: // constructor - KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only); + KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only, bool walk_defaults); // testing bool eos(); @@ -67,6 +71,8 @@ // accessors instanceKlassHandle klass() const { return _klass; } int index() const { return _index; } + bool base_class_search_defaults() const { return _base_class_search_defaults; } + void base_class_search_defaults(bool b) { _base_class_search_defaults = b; } }; @@ -81,17 +87,24 @@ class MethodStream : public KlassStream { private: - int length() const { return methods()->length(); } - Array* methods() const { return _klass->methods(); } + int length() { return methods()->length(); } + Array* methods() { + if (base_class_search_defaults()) { + base_class_search_defaults(false); + return _klass->default_methods(); + } else { + return _klass->methods(); + } + } public: MethodStream(instanceKlassHandle klass, bool local_only, bool classes_only) - : KlassStream(klass, local_only, classes_only) { + : KlassStream(klass, local_only, classes_only, true) { _index = length(); next(); } void next() { _index--; } - Method* method() const { return methods()->at(index()); } + Method* method() { return methods()->at(index()); } }; @@ -107,13 +120,13 @@ class FieldStream : public KlassStream { private: - int length() const { return _klass->java_fields_count(); } + int length() { return _klass->java_fields_count(); } fieldDescriptor _fd_buf; public: FieldStream(instanceKlassHandle klass, bool local_only, bool classes_only) - : KlassStream(klass, local_only, classes_only) { + : KlassStream(klass, local_only, classes_only, false) { _index = length(); next(); } diff -r deec468baebd -r c01f4910f5f5 src/share/vm/runtime/vmStructs.cpp --- a/src/share/vm/runtime/vmStructs.cpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/runtime/vmStructs.cpp Thu Oct 10 13:25:51 2013 -0700 @@ -27,7 +27,6 @@ #include "classfile/javaClasses.hpp" #include "classfile/loaderConstraints.hpp" #include "classfile/placeholders.hpp" -#include "classfile/symbolTable.hpp" #include "classfile/systemDictionary.hpp" #include "ci/ciField.hpp" #include "ci/ciInstance.hpp" @@ -289,6 +288,7 @@ nonstatic_field(ConstantPoolCache, _constant_pool, ConstantPool*) \ nonstatic_field(InstanceKlass, _array_klasses, Klass*) \ nonstatic_field(InstanceKlass, _methods, Array*) \ + nonstatic_field(InstanceKlass, _default_methods, Array*) \ nonstatic_field(InstanceKlass, _local_interfaces, Array*) \ nonstatic_field(InstanceKlass, _transitive_interfaces, Array*) \ nonstatic_field(InstanceKlass, _fields, Array*) \ @@ -323,6 +323,7 @@ nonstatic_field(nmethodBucket, _count, int) \ nonstatic_field(nmethodBucket, _next, nmethodBucket*) \ nonstatic_field(InstanceKlass, _method_ordering, Array*) \ + nonstatic_field(InstanceKlass, _default_vtable_indices, Array*) \ nonstatic_field(Klass, _super_check_offset, juint) \ nonstatic_field(Klass, _secondary_super_cache, Klass*) \ nonstatic_field(Klass, _secondary_supers, Array*) \ @@ -2247,12 +2248,6 @@ declare_preprocessor_constant("PERFDATA_BIG_ENDIAN", PERFDATA_BIG_ENDIAN) \ declare_preprocessor_constant("PERFDATA_LITTLE_ENDIAN", PERFDATA_LITTLE_ENDIAN) \ \ - /***************/ \ - /* SymbolTable */ \ - /***************/ \ - \ - declare_constant(SymbolTable::symbol_table_size) \ - \ /***********************************/ \ /* LoaderConstraintTable constants */ \ /***********************************/ \ diff -r deec468baebd -r c01f4910f5f5 src/share/vm/utilities/globalDefinitions.hpp --- a/src/share/vm/utilities/globalDefinitions.hpp Fri Oct 04 14:19:56 2013 -0700 +++ b/src/share/vm/utilities/globalDefinitions.hpp Thu Oct 10 13:25:51 2013 -0700 @@ -333,6 +333,9 @@ const int defaultStringTableSize = NOT_LP64(1009) LP64_ONLY(60013); const int minimumStringTableSize=1009; +const int defaultSymbolTableSize = 20011; +const int minimumSymbolTableSize = 1009; + //---------------------------------------------------------------------------------------------------- // HotSwap - for JVMTI aka Class File Replacement and PopFrame diff -r deec468baebd -r c01f4910f5f5 test/TEST.groups --- a/test/TEST.groups Fri Oct 04 14:19:56 2013 -0700 +++ b/test/TEST.groups Thu Oct 10 13:25:51 2013 -0700 @@ -65,7 +65,6 @@ gc/metaspace/CompressedClassSpaceSizeInJmapHeap.java \ gc/metaspace/TestMetaspacePerfCounters.java \ runtime/6819213/TestBootNativeLibraryPath.java \ - runtime/6878713/Test6878713.sh \ runtime/6925573/SortMethodsTest.java \ runtime/7107135/Test7107135.sh \ runtime/7158988/FieldMonitor.java \ @@ -85,7 +84,9 @@ runtime/NMT/VirtualAllocTestType.java \ runtime/RedefineObject/TestRedefineObject.java \ runtime/XCheckJniJsig/XCheckJSig.java \ - serviceability/attach/AttachWithStalePidFile.java + serviceability/attach/AttachWithStalePidFile.java \ + serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java + # JRE adds further tests to compact3 # diff -r deec468baebd -r c01f4910f5f5 test/runtime/6888954/vmerrors.sh --- a/test/runtime/6888954/vmerrors.sh Fri Oct 04 14:19:56 2013 -0700 +++ b/test/runtime/6888954/vmerrors.sh Thu Oct 10 13:25:51 2013 -0700 @@ -1,3 +1,25 @@ +# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# + # @test # @bug 6888954 # @bug 8015884 @@ -63,6 +85,7 @@ [ $i -lt 10 ] && i2=0$i "$TESTJAVA/bin/java" $TESTVMOPTS -XX:+IgnoreUnrecognizedVMOptions \ + -XX:-TransmitErrorReport \ -XX:ErrorHandlerTest=${i} -version > ${i2}.out 2>&1 # If ErrorHandlerTest is ignored (product build), stop. diff -r deec468baebd -r c01f4910f5f5 test/runtime/memory/ReserveMemory.java --- a/test/runtime/memory/ReserveMemory.java Fri Oct 04 14:19:56 2013 -0700 +++ b/test/runtime/memory/ReserveMemory.java Thu Oct 10 13:25:51 2013 -0700 @@ -56,6 +56,7 @@ "-Xbootclasspath/a:.", "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", + "-XX:-TransmitErrorReport", "ReserveMemory", "test"); diff -r deec468baebd -r c01f4910f5f5 test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapProc.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapProc.java Thu Oct 10 13:25:51 2013 -0700 @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import sun.management.VMManagement; + +public class JMapHProfLargeHeapProc { + private static final List heapGarbage = new ArrayList<>(); + + public static void main(String[] args) throws Exception { + + buildLargeHeap(args); + + // Print our pid on stdout + System.out.println("PID[" + getProcessId() + "]"); + + // Wait for input before termination + System.in.read(); + } + + private static void buildLargeHeap(String[] args) { + for (long i = 0; i < Integer.parseInt(args[0]); i++) { + heapGarbage.add(new byte[1024]); + } + } + + public static int getProcessId() throws Exception { + + // Get the current process id using a reflection hack + RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); + Field jvm = runtime.getClass().getDeclaredField("jvm"); + + jvm.setAccessible(true); + VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); + + Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId"); + + pid_method.setAccessible(true); + + int pid = (Integer) pid_method.invoke(mgmt); + + return pid; + } + +} diff -r deec468baebd -r c01f4910f5f5 test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java Thu Oct 10 13:25:51 2013 -0700 @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.nio.CharBuffer; +import java.util.Arrays; +import java.util.Scanner; + +import com.oracle.java.testlibrary.Asserts; +import com.oracle.java.testlibrary.JDKToolFinder; +import com.oracle.java.testlibrary.JDKToolLauncher; +import com.oracle.java.testlibrary.OutputAnalyzer; +import com.oracle.java.testlibrary.Platform; +import com.oracle.java.testlibrary.ProcessTools; + +/* + * @test + * @bug 6313383 + * @key regression + * @summary Regression test for hprof export issue due to large heaps (>2G) + * @library /testlibrary + * @compile JMapHProfLargeHeapProc.java + * @run main JMapHProfLargeHeapTest + */ + +public class JMapHProfLargeHeapTest { + private static final String HEAP_DUMP_FILE_NAME = "heap.hprof"; + private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1"; + private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2"; + private static final long M = 1024L; + private static final long G = 1024L * M; + + public static void main(String[] args) throws Exception { + // If we are on MacOSX, test if JMap tool is signed, otherwise return + // since test will fail with privilege error. + if (Platform.isOSX()) { + String jmapToolPath = JDKToolFinder.getCurrentJDKTool("jmap"); + ProcessBuilder codesignProcessBuilder = new ProcessBuilder( + "codesign", "-v", jmapToolPath); + Process codesignProcess = codesignProcessBuilder.start(); + OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess); + try { + analyser.shouldNotContain("code object is not signed at all"); + System.out.println("Signed jmap found at: " + jmapToolPath); + } catch (Exception e) { + // Abort since we can't know if the test will work + System.out + .println("Test aborted since we are on MacOSX and the jmap tool is not signed."); + return; + } + } + + // Small heap 22 megabytes, should create 1.0.1 file format + testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1); + + /** + * This test was deliberately commented out since the test system lacks + * support to handle the requirements for this kind of heap size in a + * good way. If or when it becomes possible to run this kind of tests in + * the test environment the test should be enabled again. + * */ + // Large heap 2,2 gigabytes, should create 1.0.2 file format + // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2); + } + + private static void testHProfFileFormat(String vmArgs, long heapSize, + String expectedFormat) throws Exception, IOException, + InterruptedException, FileNotFoundException { + ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder( + vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize)); + procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); + Process largeHeapProc = procBuilder.start(); + + try (Scanner largeHeapScanner = new Scanner( + largeHeapProc.getInputStream());) { + String pidstring = null; + while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) { + Thread.sleep(500); + } + int pid = Integer.parseInt(pidstring.substring(4, + pidstring.length() - 1)); + System.out.println("Extracted pid: " + pid); + + JDKToolLauncher jMapLauncher = JDKToolLauncher + .create("jmap", false); + jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-" + + HEAP_DUMP_FILE_NAME); + jMapLauncher.addToolArg(String.valueOf(pid)); + + ProcessBuilder jMapProcessBuilder = new ProcessBuilder( + jMapLauncher.getCommand()); + System.out.println("jmap command: " + + Arrays.toString(jMapLauncher.getCommand())); + + Process jMapProcess = jMapProcessBuilder.start(); + OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess); + analyzer.shouldHaveExitValue(0); + analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME); + analyzer.shouldContain("Heap dump file created"); + + largeHeapProc.getOutputStream().write('\n'); + + File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME); + Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found."); + + try (Reader reader = new BufferedReader(new FileReader(dumpFile))) { + CharBuffer buf = CharBuffer.allocate(expectedFormat.length()); + reader.read(buf); + buf.clear(); + Asserts.assertEQ(buf.toString(), expectedFormat, + "Wrong file format. Expected '" + expectedFormat + + "', but found '" + buf.toString() + "'"); + } + + System.out.println("Success!"); + + } finally { + largeHeapProc.destroyForcibly(); + } + } +} diff -r deec468baebd -r c01f4910f5f5 test/testlibrary/com/oracle/java/testlibrary/JDKToolLauncher.java --- a/test/testlibrary/com/oracle/java/testlibrary/JDKToolLauncher.java Fri Oct 04 14:19:56 2013 -0700 +++ b/test/testlibrary/com/oracle/java/testlibrary/JDKToolLauncher.java Thu Oct 10 13:25:51 2013 -0700 @@ -23,20 +23,17 @@ package com.oracle.java.testlibrary; -import java.util.List; import java.util.ArrayList; import java.util.Arrays; - -import com.oracle.java.testlibrary.JDKToolFinder; -import com.oracle.java.testlibrary.ProcessTools; +import java.util.List; /** * A utility for constructing command lines for starting JDK tool processes. * * The JDKToolLauncher can in particular be combined with a - * java.lang.ProcessBuilder to easily run a JDK tool. For example, the - * following code run {@code jmap -heap} against a process with GC logging - * turned on for the {@code jmap} process: + * java.lang.ProcessBuilder to easily run a JDK tool. For example, the following + * code run {@code jmap -heap} against a process with GC logging turned on for + * the {@code jmap} process: * *
  * {@code
@@ -55,19 +52,39 @@
     private final List vmArgs = new ArrayList();
     private final List toolArgs = new ArrayList();
 
-    private JDKToolLauncher(String tool) {
-        executable = JDKToolFinder.getJDKTool(tool);
+    private JDKToolLauncher(String tool, boolean useCompilerJDK) {
+        if (useCompilerJDK) {
+            executable = JDKToolFinder.getJDKTool(tool);
+        } else {
+            executable = JDKToolFinder.getCurrentJDKTool(tool);
+        }
         vmArgs.addAll(Arrays.asList(ProcessTools.getPlatformSpecificVMArgs()));
     }
 
     /**
+     * Creates a new JDKToolLauncher for the specified tool. Using tools path
+     * from the compiler JDK.
+     *
+     * @param tool
+     *            The name of the tool
+     * @return A new JDKToolLauncher
+     */
+    public static JDKToolLauncher create(String tool) {
+        return new JDKToolLauncher(tool, true);
+    }
+
+    /**
      * Creates a new JDKToolLauncher for the specified tool.
      *
-     * @param tool The name of the tool
+     * @param tool
+     *            The name of the tool
+     * @param useCompilerPath
+     *            If true use the compiler JDK path, otherwise use the tested
+     *            JDK path.
      * @return A new JDKToolLauncher
      */
-    public static JDKToolLauncher create(String tool) {
-        return new JDKToolLauncher(tool);
+    public static JDKToolLauncher create(String tool, boolean useCompilerJDK) {
+        return new JDKToolLauncher(tool, useCompilerJDK);
     }
 
     /**
@@ -80,7 +97,8 @@
      * automatically added.
      *
      *
-     * @param arg The argument to VM running the tool
+     * @param arg
+     *            The argument to VM running the tool
      * @return The JDKToolLauncher instance
      */
     public JDKToolLauncher addVMArg(String arg) {
@@ -91,7 +109,8 @@
     /**
      * Adds an argument to the tool.
      *
-     * @param arg The argument to the tool
+     * @param arg
+     *            The argument to the tool
      * @return The JDKToolLauncher instance
      */
     public JDKToolLauncher addToolArg(String arg) {