Skip to content

Commit 521e712

Browse files
committedOct 27, 2022
8286431: Do not use resource array in posix mmap_attach_shared()
Reviewed-by: dholmes, jsjolen
1 parent 4d9a1cd commit 521e712

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed
 

‎src/hotspot/os/posix/perfMemory_posix.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
10061006
// check to see if the file is secure
10071007
if (!is_file_secure(fd, filename)) {
10081008
::close(fd);
1009-
return -1;
1009+
return OS_ERR;
10101010
}
10111011

10121012
return fd;
@@ -1173,8 +1173,6 @@ static void mmap_attach_shared(int vmid, char** addr, size_t* sizep, TRAPS) {
11731173
int mmap_prot = PROT_READ;
11741174
int file_flags = O_RDONLY | O_NOFOLLOW;
11751175

1176-
ResourceMark rm;
1177-
11781176
// for linux, determine if vmid is for a containerized process
11791177
int nspid = LINUX_ONLY(os::Linux::get_namespace_pid(vmid)) NOT_LINUX(-1);
11801178
const char* luser = get_user_name(vmid, &nspid, CHECK);
@@ -1196,29 +1194,18 @@ static void mmap_attach_shared(int vmid, char** addr, size_t* sizep, TRAPS) {
11961194
"Process not found");
11971195
}
11981196

1197+
// open the shared memory file for the give vmid
11991198
char* filename = get_sharedmem_filename(dirname, vmid, nspid);
12001199

1201-
// copy heap memory to resource memory. the open_sharedmem_file
1202-
// method below need to use the filename, but could throw an
1203-
// exception. using a resource array prevents the leak that
1204-
// would otherwise occur.
1205-
char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1206-
strcpy(rfilename, filename);
1200+
// We don't use CHECK as we need to free the strings even if an exception occurred.
1201+
int fd = open_sharedmem_file(filename, file_flags, THREAD);
12071202

12081203
// free the c heap resources that are no longer needed
12091204
FREE_C_HEAP_ARRAY(char, luser);
12101205
FREE_C_HEAP_ARRAY(char, dirname);
12111206
FREE_C_HEAP_ARRAY(char, filename);
12121207

1213-
// open the shared memory file for the give vmid
1214-
int fd = open_sharedmem_file(rfilename, file_flags, THREAD);
1215-
1216-
if (fd == OS_ERR) {
1217-
return;
1218-
}
1219-
1220-
if (HAS_PENDING_EXCEPTION) {
1221-
::close(fd);
1208+
if (fd == OS_ERR || HAS_PENDING_EXCEPTION) {
12221209
return;
12231210
}
12241211

0 commit comments

Comments
 (0)
Please sign in to comment.