Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.

Commit 9149f10

Browse files
committed
8241439: jdk.NativeLibraryEvent hooks all opened regular files
Reviewed-by: stuefe, cito
1 parent 7d6c1cf commit 9149f10

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/hotspot/os/linux/os_linux.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,40 +2089,42 @@ void os::print_dll_info(outputStream *st) {
20892089
}
20902090
}
20912091

2092-
int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
2093-
FILE *procmapsFile = NULL;
2094-
2095-
// Open the procfs maps file for the current process
2096-
if ((procmapsFile = fopen("/proc/self/maps", "r")) != NULL) {
2097-
// Allocate PATH_MAX for file name plus a reasonable size for other fields.
2098-
char line[PATH_MAX + 100];
2099-
2100-
// Read line by line from 'file'
2101-
while (fgets(line, sizeof(line), procmapsFile) != NULL) {
2102-
u8 base, top, inode;
2103-
char name[sizeof(line)];
2104-
2105-
// Parse fields from line, discard perms, offset and device
2106-
int matches = sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %*s %*s %*s " INT64_FORMAT " %s",
2107-
&base, &top, &inode, name);
2108-
// the last entry 'name' is empty for some entries, so we might have 3 matches instead of 4 for some lines
2109-
if (matches < 3) continue;
2110-
if (matches == 3) name[0] = '\0';
2111-
2112-
// Filter by inode 0 so that we only get file system mapped files.
2113-
if (inode != 0) {
2114-
2115-
// Call callback with the fields of interest
2116-
if(callback(name, (address)base, (address)top, param)) {
2117-
// Oops abort, callback aborted
2118-
fclose(procmapsFile);
2119-
return 1;
2120-
}
2092+
struct loaded_modules_info_param {
2093+
os::LoadedModulesCallbackFunc callback;
2094+
void *param;
2095+
};
2096+
2097+
static int dl_iterate_callback(struct dl_phdr_info *info, size_t size, void *data) {
2098+
if ((info->dlpi_name == NULL) || (*info->dlpi_name == '\0')) {
2099+
return 0;
2100+
}
2101+
2102+
struct loaded_modules_info_param *callback_param = reinterpret_cast<struct loaded_modules_info_param *>(data);
2103+
address base = NULL;
2104+
address top = NULL;
2105+
for (int idx = 0; idx < info->dlpi_phnum; idx++) {
2106+
const ElfW(Phdr) *phdr = info->dlpi_phdr + idx;
2107+
if (phdr->p_type == PT_LOAD) {
2108+
address raw_phdr_base = reinterpret_cast<address>(info->dlpi_addr + phdr->p_vaddr);
2109+
2110+
address phdr_base = align_down(raw_phdr_base, phdr->p_align);
2111+
if ((base == NULL) || (base > phdr_base)) {
2112+
base = phdr_base;
2113+
}
2114+
2115+
address phdr_top = align_up(raw_phdr_base + phdr->p_memsz, phdr->p_align);
2116+
if ((top == NULL) || (top < phdr_top)) {
2117+
top = phdr_top;
21212118
}
21222119
}
2123-
fclose(procmapsFile);
21242120
}
2125-
return 0;
2121+
2122+
return callback_param->callback(info->dlpi_name, base, top, callback_param->param);
2123+
}
2124+
2125+
int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
2126+
struct loaded_modules_info_param callback_param = {callback, param};
2127+
return dl_iterate_phdr(&dl_iterate_callback, &callback_param);
21262128
}
21272129

21282130
void os::print_os_info_brief(outputStream* st) {

0 commit comments

Comments
 (0)