Skip to content
Permalink
Browse files
8261710: SA DSO objects have sizes that are too large
Reviewed-by: sspitsyn, cjplummer
  • Loading branch information
YaSuenag committed Mar 2, 2021
1 parent fdd1093 commit 3b350ad87f182c2800ba17458911de877ae24a6d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 39 deletions.
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, NTT DATA.
* Copyright (c) 2019, 2021, NTT DATA.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -190,18 +190,18 @@ static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_p
// add load objects
n = get_num_libs(ph);
for (i = 0; i < n; i++) {
uintptr_t base;
uintptr_t base, memsz;
const char* name;
jobject loadObject;
jobject loadObjectList;
jstring str;

base = get_lib_base(ph, i);
get_lib_addr_range(ph, i, &base, &memsz);
name = get_lib_name(ph, i);

str = env->NewStringUTF(name);
CHECK_EXCEPTION;
loadObject = env->CallObjectMethod(this_obj, createLoadObject_ID, str, (jlong)0, (jlong)base);
loadObject = env->CallObjectMethod(this_obj, createLoadObject_ID, str, (jlong)memsz, (jlong)base);
CHECK_EXCEPTION;
loadObjectList = env->GetObjectField(this_obj, loadObjectList_ID);
CHECK_EXCEPTION;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
@@ -97,6 +97,9 @@ const char* get_lib_name(struct ps_prochandle* ph, int index);
// get base of lib
uintptr_t get_lib_base(struct ps_prochandle* ph, int index);

// get address range of lib
void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz);

// returns true if given library is found in lib list
bool find_lib(struct ps_prochandle* ph, const char *lib_name);

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
@@ -159,13 +159,20 @@ lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t
return add_lib_info_fd(ph, libname, -1, base);
}

static bool fill_instr_info(lib_info* lib) {
static inline uintptr_t align_down(uintptr_t ptr, size_t page_size) {
return (ptr & ~(page_size - 1));
}

static inline uintptr_t align_up(uintptr_t ptr, size_t page_size) {
return ((ptr + page_size - 1) & ~(page_size - 1));
}

static bool fill_addr_info(lib_info* lib) {
off_t current_pos;
ELF_EHDR ehdr;
ELF_PHDR* phbuf = NULL;
ELF_PHDR* ph = NULL;
int cnt;
long align = sysconf(_SC_PAGE_SIZE);

current_pos = lseek(lib->fd, (off_t)0L, SEEK_CUR);
lseek(lib->fd, (off_t)0L, SEEK_SET);
@@ -175,32 +182,35 @@ static bool fill_instr_info(lib_info* lib) {
return false;
}

lib->end = (uintptr_t)-1L;
lib->exec_start = (uintptr_t)-1L;
lib->exec_end = (uintptr_t)-1L;
for (ph = phbuf, cnt = 0; cnt < ehdr.e_phnum; cnt++, ph++) {
if ((ph->p_type == PT_LOAD) && (ph->p_flags & PF_X)) {
print_debug("[%d] vaddr = 0x%lx, memsz = 0x%lx, filesz = 0x%lx\n", cnt, ph->p_vaddr, ph->p_memsz, ph->p_filesz);
if ((lib->exec_start == -1L) || (lib->exec_start > ph->p_vaddr)) {
lib->exec_start = ph->p_vaddr;
if (ph->p_type == PT_LOAD) {
uintptr_t aligned_start = align_down(lib->base + ph->p_vaddr, ph->p_align);
uintptr_t aligned_end = align_up(aligned_start + ph->p_filesz, ph->p_align);
if ((lib->end == (uintptr_t)-1L) || (lib->end < aligned_end)) {
lib->end = aligned_end;
}
if ((lib->exec_end == (uintptr_t)-1L) || (lib->exec_end < (ph->p_vaddr + ph->p_memsz))) {
lib->exec_end = ph->p_vaddr + ph->p_memsz;
print_debug("%s [%d] 0x%lx-0x%lx: base = 0x%lx, "
"vaddr = 0x%lx, memsz = 0x%lx, filesz = 0x%lx\n",
lib->name, cnt, aligned_start, aligned_end, lib->base,
ph->p_vaddr, ph->p_memsz, ph->p_filesz);
if (ph->p_flags & PF_X) {
if ((lib->exec_start == -1L) || (lib->exec_start > aligned_start)) {
lib->exec_start = aligned_start;
}
if ((lib->exec_end == (uintptr_t)-1L) || (lib->exec_end < aligned_end)) {
lib->exec_end = aligned_end;
}
}
align = ph->p_align;
}
}

free(phbuf);
lseek(lib->fd, current_pos, SEEK_SET);

if ((lib->exec_start == -1L) || (lib->exec_end == -1L)) {
return false;
} else {
lib->exec_start = (lib->exec_start + lib->base) & ~(align - 1);
lib->exec_end = (lib->exec_end + lib->base + align) & ~(align - 1);
return true;
}

return (lib->end != -1L) && (lib->exec_start != -1L) && (lib->exec_end != -1L);
}

bool read_eh_frame(struct ps_prochandle* ph, lib_info* lib) {
@@ -275,7 +285,7 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
print_debug("symbol table build failed for %s\n", newlib->name);
}

if (fill_instr_info(newlib)) {
if (fill_addr_info(newlib)) {
if (!read_eh_frame(ph, newlib)) {
print_debug("Could not find .eh_frame section in %s\n", newlib->name);
}
@@ -431,6 +441,21 @@ uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
return (uintptr_t)NULL;
}

// get address range of lib
void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz) {
int count = 0;
lib_info* lib = ph->libs;
while (lib) {
if (count == index) {
*base = lib->base;
*memsz = lib->end - lib->base;
return;
}
count++;
lib = lib->next;
}
}

bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
lib_info *p = ph->libs;
while (p) {
@@ -46,6 +46,7 @@ typedef struct eh_frame_info {
typedef struct lib_info {
char name[BUF_SIZE];
uintptr_t base;
uintptr_t end;
uintptr_t exec_start;
uintptr_t exec_end;
eh_frame_info eh_frame;
@@ -973,17 +973,17 @@ static void fillLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle*
// add load objects
n = get_num_libs(ph);
for (i = 0; i < n; i++) {
uintptr_t base;
uintptr_t base, memsz;
const char* name;
jobject loadObject;
jstring nameString;

base = get_lib_base(ph, i);
get_lib_addr_range(ph, i, &base, &memsz);
name = get_lib_name(ph, i);
nameString = (*env)->NewStringUTF(env, name);
CHECK_EXCEPTION;
loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
nameString, (jlong)0, (jlong)base);
nameString, (jlong)memsz, (jlong)base);
CHECK_EXCEPTION;
(*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
CHECK_EXCEPTION;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
@@ -132,6 +132,9 @@ const char* get_lib_name(struct ps_prochandle* ph, int index);
// get base of lib
uintptr_t get_lib_base(struct ps_prochandle* ph, int index);

// get address range of lib
void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz);

// returns true if given library is found in lib list
bool find_lib(struct ps_prochandle* ph, const char *lib_name);

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
@@ -486,6 +486,21 @@ uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
return (uintptr_t)NULL;
}

// get address range of lib
void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz) {
int count = 0;
lib_info* lib = ph->libs;
while (lib) {
if (count == index) {
*base = lib->base;
*memsz = lib->memsz;
return;
}
count++;
lib = lib->next;
}
}

bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
lib_info *p = ph->libs;
while (p) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, 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
@@ -24,7 +24,6 @@

package sun.jvm.hotspot.debugger.bsd;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@@ -85,11 +84,10 @@ private ClosestSymbol createClosestSymbol(String name, long offset) {
}

// called by native method attach0
private LoadObject createLoadObject(String fileName, long textsize,
private LoadObject createLoadObject(String fileName, long size,
long base) {
File f = new File(fileName);
Address baseAddr = newAddress(base);
return new SharedObject(this, fileName, f.length(), baseAddr);
return new SharedObject(this, fileName, size, baseAddr);
}

// native methods
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, 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
@@ -24,7 +24,6 @@

package sun.jvm.hotspot.debugger.linux;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
@@ -91,11 +90,10 @@ private ClosestSymbol createClosestSymbol(String name, long offset) {
}

// called by native method attach0
private LoadObject createLoadObject(String fileName, long textsize,
private LoadObject createLoadObject(String fileName, long size,
long base) {
File f = new File(fileName);
Address baseAddr = newAddress(base);
return new SharedObject(this, fileName, f.length(), baseAddr);
return new SharedObject(this, fileName, size, baseAddr);
}

// native methods

0 comments on commit 3b350ad

Please sign in to comment.