8286560: Remove user parameter from jdk.internal.perf.Perf.attach()

Reviewed-by: dholmes, alanb
This commit is contained in:
Ioi Lam 2022-05-12 18:17:31 +00:00
parent 160944bc6b
commit 74eee28a71
5 changed files with 26 additions and 121 deletions
src
hotspot
java.base/share/classes/jdk/internal/perf

@ -1097,13 +1097,7 @@ static size_t sharedmem_filesize(int fd, TRAPS) {
// attach to a named shared memory region.
//
static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t* sizep, TRAPS) {
char* mapAddress;
int result;
int fd;
size_t size = 0;
const char* luser = NULL;
static void mmap_attach_shared(int vmid, char** addr, size_t* sizep, TRAPS) {
int mmap_prot = PROT_READ;
int file_flags = O_RDONLY | O_NOFOLLOW;
@ -1112,13 +1106,7 @@ static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t*
// for linux, determine if vmid is for a containerized process
int nspid = LINUX_ONLY(os::Linux::get_namespace_pid(vmid)) NOT_LINUX(-1);
if (user == NULL || strlen(user) == 0) {
luser = get_user_name(vmid, &nspid, CHECK);
}
else {
luser = user;
}
const char* luser = get_user_name(vmid, &nspid, CHECK);
if (luser == NULL) {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
@ -1132,9 +1120,7 @@ static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t*
//
if (!is_directory_secure(dirname)) {
FREE_C_HEAP_ARRAY(char, dirname);
if (luser != user) {
FREE_C_HEAP_ARRAY(char, luser);
}
FREE_C_HEAP_ARRAY(char, luser);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Process not found");
}
@ -1149,12 +1135,12 @@ static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t*
strcpy(rfilename, filename);
// free the c heap resources that are no longer needed
if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
FREE_C_HEAP_ARRAY(char, luser);
FREE_C_HEAP_ARRAY(char, dirname);
FREE_C_HEAP_ARRAY(char, filename);
// open the shared memory file for the give vmid
fd = open_sharedmem_file(rfilename, file_flags, THREAD);
int fd = open_sharedmem_file(rfilename, file_flags, THREAD);
if (fd == OS_ERR) {
return;
@ -1165,6 +1151,7 @@ static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t*
return;
}
size_t size;
if (*sizep == 0) {
size = sharedmem_filesize(fd, CHECK);
} else {
@ -1173,9 +1160,9 @@ static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t*
assert(size > 0, "unexpected size <= 0");
mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
char* mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
result = ::close(fd);
int result = ::close(fd);
assert(result != OS_ERR, "could not close file");
if (mapAddress == MAP_FAILED) {
@ -1268,7 +1255,7 @@ void PerfMemory::delete_memory_region() {
// the indicated process's PerfData memory region into this JVMs
// address space.
//
void PerfMemory::attach(const char* user, int vmid, char** addrp, size_t* sizep, TRAPS) {
void PerfMemory::attach(int vmid, char** addrp, size_t* sizep, TRAPS) {
if (vmid == 0 || vmid == os::current_process_id()) {
*addrp = start();
@ -1276,7 +1263,7 @@ void PerfMemory::attach(const char* user, int vmid, char** addrp, size_t* sizep,
return;
}
mmap_attach_shared(user, vmid, addrp, sizep, CHECK);
mmap_attach_shared(vmid, addrp, sizep, CHECK);
}
// detach from the PerfData memory region of another JVM

@ -1573,26 +1573,12 @@ static size_t sharedmem_filesize(const char* filename, TRAPS) {
// this method opens a file mapping object and maps the object
// into the address space of the process
//
static void open_file_mapping(const char* user, int vmid, char** addrp, size_t* sizep, TRAPS) {
static void open_file_mapping(int vmid, char** addrp, size_t* sizep, TRAPS) {
ResourceMark rm;
void *mapAddress = 0;
size_t size = 0;
HANDLE fmh;
DWORD ofm_access = FILE_MAP_READ;
DWORD mv_access = FILE_MAP_READ;
const char* luser = NULL;
// if a user name wasn't specified, then find the user name for
// the owner of the target vm.
if (user == NULL || strlen(user) == 0) {
luser = get_user_name(vmid);
}
else {
luser = user;
}
const char* luser = get_user_name(vmid);
if (luser == NULL) {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
@ -1607,7 +1593,7 @@ static void open_file_mapping(const char* user, int vmid, char** addrp, size_t*
//
if (!is_directory_secure(dirname)) {
FREE_C_HEAP_ARRAY(char, dirname);
if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
FREE_C_HEAP_ARRAY(char, luser);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Process not found");
}
@ -1626,11 +1612,12 @@ static void open_file_mapping(const char* user, int vmid, char** addrp, size_t*
strcpy(robjectname, objectname);
// free the c heap resources that are no longer needed
if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
FREE_C_HEAP_ARRAY(char, luser);
FREE_C_HEAP_ARRAY(char, dirname);
FREE_C_HEAP_ARRAY(char, filename);
FREE_C_HEAP_ARRAY(char, objectname);
size_t size;
if (*sizep == 0) {
size = sharedmem_filesize(rfilename, CHECK);
} else {
@ -1640,12 +1627,11 @@ static void open_file_mapping(const char* user, int vmid, char** addrp, size_t*
assert(size > 0, "unexpected size <= 0");
// Open the file mapping object with the given name
fmh = open_sharedmem_object(robjectname, ofm_access, CHECK);
HANDLE fmh = open_sharedmem_object(robjectname, ofm_access, CHECK);
assert(fmh != INVALID_HANDLE_VALUE, "unexpected handle value");
// map the entire file into the address space
mapAddress = MapViewOfFile(
void* mapAddress = MapViewOfFile(
fmh, /* HANDLE Handle of file mapping object */
mv_access, /* DWORD access flags */
0, /* DWORD High word of offset */
@ -1777,8 +1763,7 @@ void PerfMemory::delete_memory_region() {
// the indicated process's PerfData memory region into this JVMs
// address space.
//
void PerfMemory::attach(const char* user, int vmid,
char** addrp, size_t* sizep, TRAPS) {
void PerfMemory::attach(int vmid, char** addrp, size_t* sizep, TRAPS) {
if (vmid == 0 || vmid == os::current_process_id()) {
*addrp = start();
@ -1786,7 +1771,7 @@ void PerfMemory::attach(const char* user, int vmid,
return;
}
open_file_mapping(user, vmid, addrp, sizep, CHECK);
open_file_mapping(vmid, addrp, sizep, CHECK);
}
// detach from the PerfData memory region of another JVM

@ -64,24 +64,15 @@ static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
return utfstr;
}
PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, jstring user, int vmid))
PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
PerfWrapper("Perf_Attach");
char* address = 0;
size_t capacity = 0;
const char* user_utf = NULL;
ResourceMark rm;
{
ThreadToNativeFromVM ttnfv(thread);
user_utf = user == NULL ? NULL : jstr_to_utf(env, user, CHECK_NULL);
}
// attach to the PerfData memory region for the specified VM
PerfMemory::attach(user_utf, vmid, &address, &capacity, CHECK_NULL);
PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
{
ThreadToNativeFromVM ttnfv(thread);
@ -294,7 +285,7 @@ PERF_END
static JNINativeMethod perfmethods[] = {
{CC "attach0", CC "(" JLS "I)" BB, FN_PTR(Perf_Attach)},
{CC "attach0", CC "(I)" BB, FN_PTR(Perf_Attach)},
{CC "detach", CC "(" BB ")V", FN_PTR(Perf_Detach)},
{CC "createLong", CL_ARGS, FN_PTR(Perf_CreateLong)},
{CC "createByteArray", CBA_ARGS, FN_PTR(Perf_CreateByteArray)},

@ -143,7 +143,7 @@ class PerfMemory : AllStatic {
// methods for attaching to and detaching from the PerfData
// memory segment of another JVM process on the same system.
static void attach(const char* user, int vmid, char** addrp, size_t* size, TRAPS);
static void attach(int vmid, char** addrp, size_t* size, TRAPS);
static void detach(char* addr, size_t bytes);
static void initialize();

@ -179,63 +179,9 @@ public final class Perf {
* into the virtual machine's address space.
* @see java.nio.ByteBuffer
*/
public ByteBuffer attach(int lvmid)
throws IllegalArgumentException, IOException
public ByteBuffer attach(int lvmid) throws IOException
{
return attachImpl(null, lvmid);
}
/**
* Attach to the instrumentation buffer for the specified Java virtual
* machine owned by the given user.
* <p>
* This method behaves just as the <code>attach(int lvmid)
* </code> method, except that it only searches for Java virtual machines
* owned by the specified user.
*
* @param user A <code>String</code> object containing the
* name of the user that owns the target Java
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
* @see java.nio.ByteBuffer
*/
public ByteBuffer attach(String user, int lvmid)
throws IllegalArgumentException, IOException
{
return attachImpl(user, lvmid);
}
/**
* Call the implementation specific attach method.
* <p>
* This method calls into the Java virtual machine to perform the platform
* specific attach method. Buffers returned from this method are
* internally managed as <code>PhantomRefereces</code> to provide for
* guaranteed, secure release of the native resources.
*
* @param user A <code>String</code> object containing the
* name of the user that owns the target Java
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
*/
private ByteBuffer attachImpl(String user, int lvmid)
throws IllegalArgumentException, IOException
{
final ByteBuffer b = attach0(user, lvmid);
final ByteBuffer b = attach0(lvmid);
if (lvmid == 0) {
// The native instrumentation buffer for this Java virtual
@ -286,9 +232,6 @@ public final class Perf {
* target Java virtual machine will result in two distinct ByteBuffer
* objects returned by this method. This may change in a future release.
*
* @param user A <code>String</code> object containing the
* name of the user that owns the target Java
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @return ByteBuffer a direct allocated byte buffer
@ -298,8 +241,7 @@ public final class Perf {
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
*/
private native ByteBuffer attach0(String user, int lvmid)
throws IllegalArgumentException, IOException;
private native ByteBuffer attach0(int lvmid) throws IOException;
/**
* Native method to perform the implementation specific detach mechanism.