8241439: jdk.NativeLibraryEvent hooks all opened regular files

Reviewed-by: stuefe, cito
This commit is contained in:
Yasumasa Suenaga 2020-06-08 17:21:09 +09:00
parent 7d6c1cf4a8
commit 9149f10fa4

View file

@ -2089,40 +2089,42 @@ void os::print_dll_info(outputStream *st) {
} }
} }
int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) { struct loaded_modules_info_param {
FILE *procmapsFile = NULL; os::LoadedModulesCallbackFunc callback;
void *param;
};
// Open the procfs maps file for the current process static int dl_iterate_callback(struct dl_phdr_info *info, size_t size, void *data) {
if ((procmapsFile = fopen("/proc/self/maps", "r")) != NULL) { if ((info->dlpi_name == NULL) || (*info->dlpi_name == '\0')) {
// Allocate PATH_MAX for file name plus a reasonable size for other fields.
char line[PATH_MAX + 100];
// Read line by line from 'file'
while (fgets(line, sizeof(line), procmapsFile) != NULL) {
u8 base, top, inode;
char name[sizeof(line)];
// Parse fields from line, discard perms, offset and device
int matches = sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %*s %*s %*s " INT64_FORMAT " %s",
&base, &top, &inode, name);
// the last entry 'name' is empty for some entries, so we might have 3 matches instead of 4 for some lines
if (matches < 3) continue;
if (matches == 3) name[0] = '\0';
// Filter by inode 0 so that we only get file system mapped files.
if (inode != 0) {
// Call callback with the fields of interest
if(callback(name, (address)base, (address)top, param)) {
// Oops abort, callback aborted
fclose(procmapsFile);
return 1;
}
}
}
fclose(procmapsFile);
}
return 0; return 0;
}
struct loaded_modules_info_param *callback_param = reinterpret_cast<struct loaded_modules_info_param *>(data);
address base = NULL;
address top = NULL;
for (int idx = 0; idx < info->dlpi_phnum; idx++) {
const ElfW(Phdr) *phdr = info->dlpi_phdr + idx;
if (phdr->p_type == PT_LOAD) {
address raw_phdr_base = reinterpret_cast<address>(info->dlpi_addr + phdr->p_vaddr);
address phdr_base = align_down(raw_phdr_base, phdr->p_align);
if ((base == NULL) || (base > phdr_base)) {
base = phdr_base;
}
address phdr_top = align_up(raw_phdr_base + phdr->p_memsz, phdr->p_align);
if ((top == NULL) || (top < phdr_top)) {
top = phdr_top;
}
}
}
return callback_param->callback(info->dlpi_name, base, top, callback_param->param);
}
int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
struct loaded_modules_info_param callback_param = {callback, param};
return dl_iterate_phdr(&dl_iterate_callback, &callback_param);
} }
void os::print_os_info_brief(outputStream* st) { void os::print_os_info_brief(outputStream* st) {