aoqi@0: /* phh@9507: * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. aoqi@0: * Copyright 2012, 2013 SAP AG. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "os_aix.inline.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/perfMemory.hpp" goetz@7515: #include "services/memTracker.hpp" aoqi@0: #include "utilities/exceptions.hpp" aoqi@0: aoqi@0: // put OS-includes here aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: # include aoqi@0: aoqi@0: static char* backing_store_file_name = NULL; // name of the backing store aoqi@0: // file, if successfully created. aoqi@0: aoqi@0: // Standard Memory Implementation Details aoqi@0: aoqi@0: // create the PerfData memory region in standard memory. aoqi@0: // aoqi@0: static char* create_standard_memory(size_t size) { aoqi@0: aoqi@0: // allocate an aligned chuck of memory aoqi@0: char* mapAddress = os::reserve_memory(size); aoqi@0: aoqi@0: if (mapAddress == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // commit memory aoqi@0: if (!os::commit_memory(mapAddress, size, !ExecMem)) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("Could not commit PerfData memory\n"); aoqi@0: } aoqi@0: os::release_memory(mapAddress, size); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: return mapAddress; aoqi@0: } aoqi@0: aoqi@0: // delete the PerfData memory region aoqi@0: // aoqi@0: static void delete_standard_memory(char* addr, size_t size) { aoqi@0: aoqi@0: // there are no persistent external resources to cleanup for standard aoqi@0: // memory. since DestroyJavaVM does not support unloading of the JVM, aoqi@0: // cleanup of the memory resource is not performed. The memory will be aoqi@0: // reclaimed by the OS upon termination of the process. aoqi@0: // aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // save the specified memory region to the given file aoqi@0: // aoqi@0: // Note: this function might be called from signal handler (by os::abort()), aoqi@0: // don't allocate heap memory. aoqi@0: // aoqi@0: static void save_memory_to_file(char* addr, size_t size) { aoqi@0: aoqi@0: const char* destfile = PerfMemory::get_perfdata_file_path(); aoqi@0: assert(destfile[0] != '\0', "invalid PerfData file path"); aoqi@0: aoqi@0: int result; aoqi@0: aoqi@0: RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), aoqi@0: result);; aoqi@0: if (result == OS_ERR) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("Could not create Perfdata save file: %s: %s\n", aoqi@0: destfile, strerror(errno)); aoqi@0: } aoqi@0: } else { aoqi@0: int fd = result; aoqi@0: aoqi@0: for (size_t remaining = size; remaining > 0;) { aoqi@0: aoqi@0: RESTARTABLE(::write(fd, addr, remaining), result); aoqi@0: if (result == OS_ERR) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("Could not write Perfdata save file: %s: %s\n", aoqi@0: destfile, strerror(errno)); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: remaining -= (size_t)result; aoqi@0: addr += result; aoqi@0: } aoqi@0: aoqi@0: RESTARTABLE(::close(fd), result); aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: if (result == OS_ERR) { aoqi@0: warning("Could not close %s: %s\n", destfile, strerror(errno)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: FREE_C_HEAP_ARRAY(char, destfile, mtInternal); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Shared Memory Implementation Details aoqi@0: aoqi@0: // Note: the solaris and linux shared memory implementation uses the mmap aoqi@0: // interface with a backing store file to implement named shared memory. aoqi@0: // Using the file system as the name space for shared memory allows a aoqi@0: // common name space to be supported across a variety of platforms. It aoqi@0: // also provides a name space that Java applications can deal with through aoqi@0: // simple file apis. aoqi@0: // aoqi@0: // The solaris and linux implementations store the backing store file in aoqi@0: // a user specific temporary directory located in the /tmp file system, aoqi@0: // which is always a local file system and is sometimes a RAM based file aoqi@0: // system. aoqi@0: aoqi@0: // return the user specific temporary directory name. aoqi@0: // aoqi@0: // the caller is expected to free the allocated memory. aoqi@0: // aoqi@0: static char* get_user_tmp_dir(const char* user) { aoqi@0: aoqi@0: const char* tmpdir = os::get_temp_directory(); aoqi@0: const char* perfdir = PERFDATA_NAME; aoqi@0: size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; aoqi@0: char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); aoqi@0: aoqi@0: // construct the path name to user specific tmp directory aoqi@0: snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); aoqi@0: aoqi@0: return dirname; aoqi@0: } aoqi@0: aoqi@0: // convert the given file name into a process id. if the file aoqi@0: // does not meet the file naming constraints, return 0. aoqi@0: // aoqi@0: static pid_t filename_to_pid(const char* filename) { aoqi@0: aoqi@0: // a filename that doesn't begin with a digit is not a aoqi@0: // candidate for conversion. aoqi@0: // aoqi@0: if (!isdigit(*filename)) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: // check if file name can be converted to an integer without aoqi@0: // any leftover characters. aoqi@0: // aoqi@0: char* remainder = NULL; aoqi@0: errno = 0; aoqi@0: pid_t pid = (pid_t)strtol(filename, &remainder, 10); aoqi@0: aoqi@0: if (errno != 0) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: // check for left over characters. If any, then the filename is aoqi@0: // not a candidate for conversion. aoqi@0: // aoqi@0: if (remainder != NULL && *remainder != '\0') { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: // successful conversion, return the pid aoqi@0: return pid; aoqi@0: } aoqi@0: goetz@7515: // Check if the given statbuf is considered a secure directory for goetz@7515: // the backing store files. Returns true if the directory is considered goetz@7515: // a secure location. Returns false if the statbuf is a symbolic link or goetz@7515: // if an error occurred. clanger@8210: // goetz@7515: static bool is_statbuf_secure(struct stat *statp) { goetz@7515: if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) { goetz@7515: // The path represents a link or some non-directory file type, goetz@7515: // which is not what we expected. Declare it insecure. goetz@7515: // goetz@7515: return false; goetz@7515: } goetz@7515: // We have an existing directory, check if the permissions are safe. clanger@8210: // goetz@7515: if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) { goetz@7515: // The directory is open for writing and could be subjected goetz@7515: // to a symlink or a hard link attack. Declare it insecure. clanger@8210: // goetz@7515: return false; goetz@7515: } clanger@8210: // If user is not root then see if the uid of the directory matches the effective uid of the process. clanger@8210: uid_t euid = geteuid(); clanger@8210: if ((euid != 0) && (statp->st_uid != euid)) { goetz@7515: // The directory was not created by this user, declare it insecure. clanger@8210: // goetz@7515: return false; goetz@7515: } goetz@7515: return true; goetz@7515: } aoqi@0: goetz@7515: goetz@7515: // Check if the given path is considered a secure directory for aoqi@0: // the backing store files. Returns true if the directory exists aoqi@0: // and is considered a secure location. Returns false if the path aoqi@0: // is a symbolic link or if an error occurred. clanger@8210: // aoqi@0: static bool is_directory_secure(const char* path) { aoqi@0: struct stat statbuf; aoqi@0: int result = 0; aoqi@0: aoqi@0: RESTARTABLE(::lstat(path, &statbuf), result); aoqi@0: if (result == OS_ERR) { aoqi@0: return false; aoqi@0: } aoqi@0: goetz@7515: // The path exists, see if it is secure. goetz@7515: return is_statbuf_secure(&statbuf); goetz@7515: } goetz@7515: goetz@7515: // (Taken over from Solaris to support the O_NOFOLLOW case on AIX.) goetz@7515: // Check if the given directory file descriptor is considered a secure goetz@7515: // directory for the backing store files. Returns true if the directory goetz@7515: // exists and is considered a secure location. Returns false if the path goetz@7515: // is a symbolic link or if an error occurred. goetz@7515: static bool is_dirfd_secure(int dir_fd) { goetz@7515: struct stat statbuf; goetz@7515: int result = 0; goetz@7515: goetz@7515: RESTARTABLE(::fstat(dir_fd, &statbuf), result); goetz@7515: if (result == OS_ERR) { aoqi@0: return false; aoqi@0: } goetz@7515: goetz@7515: // The path exists, now check its mode. goetz@7515: return is_statbuf_secure(&statbuf); goetz@7515: } goetz@7515: goetz@7515: goetz@7515: // Check to make sure fd1 and fd2 are referencing the same file system object. goetz@7515: static bool is_same_fsobject(int fd1, int fd2) { goetz@7515: struct stat statbuf1; goetz@7515: struct stat statbuf2; goetz@7515: int result = 0; goetz@7515: goetz@7515: RESTARTABLE(::fstat(fd1, &statbuf1), result); goetz@7515: if (result == OS_ERR) { goetz@7515: return false; goetz@7515: } goetz@7515: RESTARTABLE(::fstat(fd2, &statbuf2), result); goetz@7515: if (result == OS_ERR) { goetz@7515: return false; goetz@7515: } goetz@7515: goetz@7515: if ((statbuf1.st_ino == statbuf2.st_ino) && goetz@7515: (statbuf1.st_dev == statbuf2.st_dev)) { goetz@7515: return true; goetz@7515: } else { goetz@7515: return false; goetz@7515: } goetz@7515: } goetz@7515: goetz@7515: // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1. goetz@7515: // We use the jdk6 implementation here. goetz@7515: #ifndef O_NOFOLLOW goetz@7515: // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour goetz@7515: // was done in jdk 5/6 hotspot by Oracle this way goetz@7515: static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) { goetz@7515: struct stat orig_st; goetz@7515: struct stat new_st; goetz@7515: bool create; goetz@7515: int error; goetz@7515: int fd; goetz@7515: goetz@7515: create = false; goetz@7515: goetz@7515: if (lstat(path, &orig_st) != 0) { goetz@7515: if (errno == ENOENT && (oflag & O_CREAT) != 0) { goetz@7515: // File doesn't exist, but_we want to create it, add O_EXCL flag goetz@7515: // to make sure no-one creates it (or a symlink) before us goetz@7515: // This works as we expect with symlinks, from posix man page: goetz@7515: // 'If O_EXCL and O_CREAT are set, and path names a symbolic goetz@7515: // link, open() shall fail and set errno to [EEXIST]'. goetz@7515: oflag |= O_EXCL; goetz@7515: create = true; goetz@7515: } else { goetz@7515: // File doesn't exist, and we are not creating it. goetz@7515: return OS_ERR; aoqi@0: } goetz@7515: } else { goetz@7515: // Lstat success, check if existing file is a link. goetz@7515: if ((orig_st.st_mode & S_IFMT) == S_IFLNK) { goetz@7515: // File is a symlink. goetz@7515: errno = ELOOP; goetz@7515: return OS_ERR; goetz@7515: } goetz@7515: } goetz@7515: goetz@7515: if (use_mode == true) { goetz@7515: fd = open(path, oflag, mode); goetz@7515: } else { goetz@7515: fd = open(path, oflag); goetz@7515: } goetz@7515: goetz@7515: if (fd == OS_ERR) { goetz@7515: return fd; goetz@7515: } goetz@7515: goetz@7515: // Can't do inode checks on before/after if we created the file. goetz@7515: if (create == false) { goetz@7515: if (fstat(fd, &new_st) != 0) { goetz@7515: // Keep errno from fstat, in case close also fails. goetz@7515: error = errno; goetz@7515: ::close(fd); goetz@7515: errno = error; goetz@7515: return OS_ERR; goetz@7515: } goetz@7515: goetz@7515: if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) { goetz@7515: // File was tampered with during race window. goetz@7515: ::close(fd); goetz@7515: errno = EEXIST; goetz@7515: if (PrintMiscellaneous && Verbose) { goetz@7515: warning("possible file tampering attempt detected when opening %s", path); goetz@7515: } goetz@7515: return OS_ERR; goetz@7515: } goetz@7515: } goetz@7515: goetz@7515: return fd; goetz@7515: } goetz@7515: goetz@7515: static int open_o_nofollow(const char* path, int oflag, mode_t mode) { goetz@7515: return open_o_nofollow_impl(path, oflag, mode, true); goetz@7515: } goetz@7515: goetz@7515: static int open_o_nofollow(const char* path, int oflag) { goetz@7515: return open_o_nofollow_impl(path, oflag, 0, false); goetz@7515: } goetz@7515: #endif goetz@7515: goetz@7515: // Open the directory of the given path and validate it. goetz@7515: // Return a DIR * of the open directory. goetz@7515: static DIR *open_directory_secure(const char* dirname) { goetz@7515: // Open the directory using open() so that it can be verified goetz@7515: // to be secure by calling is_dirfd_secure(), opendir() and then check goetz@7515: // to see if they are the same file system object. This method does not goetz@7515: // introduce a window of opportunity for the directory to be attacked that goetz@7515: // calling opendir() and is_directory_secure() does. goetz@7515: int result; goetz@7515: DIR *dirp = NULL; goetz@7515: goetz@7515: // No O_NOFOLLOW defined at buildtime, and it is not documented for open; goetz@7515: // so provide a workaround in this case. goetz@7515: #ifdef O_NOFOLLOW goetz@7515: RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result); goetz@7515: #else goetz@7515: // workaround (jdk6 coding) goetz@7515: RESTARTABLE(::open_o_nofollow(dirname, O_RDONLY), result); goetz@7515: #endif goetz@7515: goetz@7515: if (result == OS_ERR) { goetz@7515: // Directory doesn't exist or is a symlink, so there is nothing to cleanup. goetz@7515: if (PrintMiscellaneous && Verbose) { goetz@7515: if (errno == ELOOP) { goetz@7515: warning("directory %s is a symlink and is not secure\n", dirname); goetz@7515: } else { goetz@7515: warning("could not open directory %s: %s\n", dirname, strerror(errno)); goetz@7515: } goetz@7515: } goetz@7515: return dirp; goetz@7515: } goetz@7515: int fd = result; goetz@7515: goetz@7515: // Determine if the open directory is secure. goetz@7515: if (!is_dirfd_secure(fd)) { goetz@7515: // The directory is not a secure directory. goetz@7515: os::close(fd); goetz@7515: return dirp; goetz@7515: } goetz@7515: goetz@7515: // Open the directory. goetz@7515: dirp = ::opendir(dirname); goetz@7515: if (dirp == NULL) { goetz@7515: // The directory doesn't exist, close fd and return. goetz@7515: os::close(fd); goetz@7515: return dirp; goetz@7515: } goetz@7515: goetz@7515: // Check to make sure fd and dirp are referencing the same file system object. goetz@7515: if (!is_same_fsobject(fd, dirp->dd_fd)) { goetz@7515: // The directory is not secure. goetz@7515: os::close(fd); goetz@7515: os::closedir(dirp); goetz@7515: dirp = NULL; goetz@7515: return dirp; goetz@7515: } goetz@7515: goetz@7515: // Close initial open now that we know directory is secure goetz@7515: os::close(fd); goetz@7515: goetz@7515: return dirp; goetz@7515: } goetz@7515: goetz@7515: // NOTE: The code below uses fchdir(), open() and unlink() because goetz@7515: // fdopendir(), openat() and unlinkat() are not supported on all goetz@7515: // versions. Once the support for fdopendir(), openat() and unlinkat() goetz@7515: // is available on all supported versions the code can be changed goetz@7515: // to use these functions. goetz@7515: goetz@7515: // Open the directory of the given path, validate it and set the goetz@7515: // current working directory to it. goetz@7515: // Return a DIR * of the open directory and the saved cwd fd. goetz@7515: // goetz@7515: static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) { goetz@7515: goetz@7515: // Open the directory. goetz@7515: DIR* dirp = open_directory_secure(dirname); goetz@7515: if (dirp == NULL) { goetz@7515: // Directory doesn't exist or is insecure, so there is nothing to cleanup. goetz@7515: return dirp; goetz@7515: } goetz@7515: int fd = dirp->dd_fd; goetz@7515: goetz@7515: // Open a fd to the cwd and save it off. goetz@7515: int result; goetz@7515: RESTARTABLE(::open(".", O_RDONLY), result); goetz@7515: if (result == OS_ERR) { goetz@7515: *saved_cwd_fd = -1; goetz@7515: } else { goetz@7515: *saved_cwd_fd = result; goetz@7515: } goetz@7515: clanger@8177: // Set the current directory to dirname by using the fd of the directory and clanger@8177: // handle errors, otherwise shared memory files will be created in cwd. goetz@7515: result = fchdir(fd); clanger@8177: if (result == OS_ERR) { clanger@8177: if (PrintMiscellaneous && Verbose) { clanger@8177: warning("could not change to directory %s", dirname); clanger@8177: } clanger@8177: if (*saved_cwd_fd != -1) { clanger@8177: ::close(*saved_cwd_fd); clanger@8177: *saved_cwd_fd = -1; clanger@8177: } clanger@8177: // Close the directory. clanger@8177: os::closedir(dirp); clanger@8177: return NULL; clanger@8177: } else { clanger@8177: return dirp; clanger@8177: } goetz@7515: } goetz@7515: goetz@7515: // Close the directory and restore the current working directory. clanger@8177: // goetz@7515: static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) { goetz@7515: goetz@7515: int result; goetz@7515: // If we have a saved cwd change back to it and close the fd. goetz@7515: if (saved_cwd_fd != -1) { goetz@7515: result = fchdir(saved_cwd_fd); goetz@7515: ::close(saved_cwd_fd); goetz@7515: } goetz@7515: goetz@7515: // Close the directory. goetz@7515: os::closedir(dirp); goetz@7515: } goetz@7515: goetz@7515: // Check if the given file descriptor is considered a secure. goetz@7515: static bool is_file_secure(int fd, const char *filename) { goetz@7515: goetz@7515: int result; goetz@7515: struct stat statbuf; goetz@7515: goetz@7515: // Determine if the file is secure. goetz@7515: RESTARTABLE(::fstat(fd, &statbuf), result); goetz@7515: if (result == OS_ERR) { goetz@7515: if (PrintMiscellaneous && Verbose) { goetz@7515: warning("fstat failed on %s: %s\n", filename, strerror(errno)); goetz@7515: } goetz@7515: return false; goetz@7515: } goetz@7515: if (statbuf.st_nlink > 1) { goetz@7515: // A file with multiple links is not expected. goetz@7515: if (PrintMiscellaneous && Verbose) { goetz@7515: warning("file %s has multiple links\n", filename); goetz@7515: } goetz@7515: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: goetz@7515: // Return the user name for the given user id. aoqi@0: // goetz@7515: // The caller is expected to free the allocated memory. aoqi@0: static char* get_user_name(uid_t uid) { aoqi@0: aoqi@0: struct passwd pwent; aoqi@0: goetz@7515: // Determine the max pwbuf size from sysconf, and hardcode aoqi@0: // a default if this not available through sysconf. aoqi@0: long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); aoqi@0: if (bufsize == -1) aoqi@0: bufsize = 1024; aoqi@0: aoqi@0: char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); aoqi@0: aoqi@0: // POSIX interface to getpwuid_r is used on LINUX aoqi@0: struct passwd* p; aoqi@0: int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p); aoqi@0: aoqi@0: if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: if (result != 0) { aoqi@0: warning("Could not retrieve passwd entry: %s\n", aoqi@0: strerror(result)); aoqi@0: } aoqi@0: else if (p == NULL) { aoqi@0: // this check is added to protect against an observed problem aoqi@0: // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0, aoqi@0: // indicating success, but has p == NULL. This was observed when aoqi@0: // inserting a file descriptor exhaustion fault prior to the call aoqi@0: // getpwuid_r() call. In this case, error is set to the appropriate aoqi@0: // error condition, but this is undocumented behavior. This check aoqi@0: // is safe under any condition, but the use of errno in the output aoqi@0: // message may result in an erroneous message. aoqi@0: // Bug Id 89052 was opened with RedHat. aoqi@0: // aoqi@0: warning("Could not retrieve passwd entry: %s\n", aoqi@0: strerror(errno)); aoqi@0: } aoqi@0: else { aoqi@0: warning("Could not determine user name: %s\n", aoqi@0: p->pw_name == NULL ? "pw_name = NULL" : aoqi@0: "pw_name zero length"); aoqi@0: } aoqi@0: } aoqi@0: FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal); aoqi@0: strcpy(user_name, p->pw_name); aoqi@0: aoqi@0: FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); aoqi@0: return user_name; aoqi@0: } aoqi@0: aoqi@0: // return the name of the user that owns the process identified by vmid. aoqi@0: // aoqi@0: // This method uses a slow directory search algorithm to find the backing aoqi@0: // store file for the specified vmid and returns the user name, as determined aoqi@0: // by the user name suffix of the hsperfdata_ directory name. aoqi@0: // aoqi@0: // the caller is expected to free the allocated memory. aoqi@0: // aoqi@0: static char* get_user_name_slow(int vmid, TRAPS) { aoqi@0: aoqi@0: // short circuit the directory search if the process doesn't even exist. aoqi@0: if (kill(vmid, 0) == OS_ERR) { aoqi@0: if (errno == ESRCH) { aoqi@0: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Process not found"); aoqi@0: } aoqi@0: else /* EPERM */ { aoqi@0: THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // directory search aoqi@0: char* oldest_user = NULL; aoqi@0: time_t oldest_ctime = 0; aoqi@0: aoqi@0: const char* tmpdirname = os::get_temp_directory(); aoqi@0: aoqi@0: DIR* tmpdirp = os::opendir(tmpdirname); aoqi@0: aoqi@0: if (tmpdirp == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // for each entry in the directory that matches the pattern hsperfdata_*, aoqi@0: // open the directory and check if the file for the given vmid exists. aoqi@0: // The file with the expected name and the latest creation date is used aoqi@0: // to determine the user name for the process id. aoqi@0: // aoqi@0: struct dirent* dentry; aoqi@0: errno = 0; andrew@9711: while ((dentry = os::readdir(tmpdirp)) != NULL) { aoqi@0: aoqi@0: // check if the directory entry is a hsperfdata file aoqi@0: if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) { aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: char* usrdir_name = NEW_C_HEAP_ARRAY(char, aoqi@0: strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal); aoqi@0: strcpy(usrdir_name, tmpdirname); aoqi@0: strcat(usrdir_name, "/"); aoqi@0: strcat(usrdir_name, dentry->d_name); aoqi@0: goetz@7515: // Open the user directory. goetz@7515: DIR* subdirp = open_directory_secure(usrdir_name); aoqi@0: aoqi@0: if (subdirp == NULL) { aoqi@0: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // Since we don't create the backing store files in directories aoqi@0: // pointed to by symbolic links, we also don't follow them when aoqi@0: // looking for the files. We check for a symbolic link after the aoqi@0: // call to opendir in order to eliminate a small window where the aoqi@0: // symlink can be exploited. aoqi@0: // aoqi@0: if (!is_directory_secure(usrdir_name)) { aoqi@0: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); aoqi@0: os::closedir(subdirp); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: struct dirent* udentry; aoqi@0: errno = 0; andrew@9711: while ((udentry = os::readdir(subdirp)) != NULL) { aoqi@0: aoqi@0: if (filename_to_pid(udentry->d_name) == vmid) { aoqi@0: struct stat statbuf; aoqi@0: int result; aoqi@0: aoqi@0: char* filename = NEW_C_HEAP_ARRAY(char, aoqi@0: strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal); aoqi@0: aoqi@0: strcpy(filename, usrdir_name); aoqi@0: strcat(filename, "/"); aoqi@0: strcat(filename, udentry->d_name); aoqi@0: aoqi@0: // don't follow symbolic links for the file aoqi@0: RESTARTABLE(::lstat(filename, &statbuf), result); aoqi@0: if (result == OS_ERR) { aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // skip over files that are not regular files. aoqi@0: if (!S_ISREG(statbuf.st_mode)) { aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // compare and save filename with latest creation time aoqi@0: if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) { aoqi@0: aoqi@0: if (statbuf.st_ctime > oldest_ctime) { aoqi@0: char* user = strchr(dentry->d_name, '_') + 1; aoqi@0: aoqi@0: if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal); aoqi@0: oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); aoqi@0: aoqi@0: strcpy(oldest_user, user); aoqi@0: oldest_ctime = statbuf.st_ctime; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: } aoqi@0: } aoqi@0: os::closedir(subdirp); aoqi@0: FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); aoqi@0: } aoqi@0: os::closedir(tmpdirp); aoqi@0: aoqi@0: return(oldest_user); aoqi@0: } aoqi@0: aoqi@0: // return the name of the user that owns the JVM indicated by the given vmid. aoqi@0: // aoqi@0: static char* get_user_name(int vmid, TRAPS) { phh@9507: return get_user_name_slow(vmid, THREAD); aoqi@0: } aoqi@0: aoqi@0: // return the file name of the backing store file for the named aoqi@0: // shared memory region for the given user name and vmid. aoqi@0: // aoqi@0: // the caller is expected to free the allocated memory. aoqi@0: // aoqi@0: static char* get_sharedmem_filename(const char* dirname, int vmid) { aoqi@0: aoqi@0: // add 2 for the file separator and a null terminator. aoqi@0: size_t nbytes = strlen(dirname) + UINT_CHARS + 2; aoqi@0: aoqi@0: char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); aoqi@0: snprintf(name, nbytes, "%s/%d", dirname, vmid); aoqi@0: aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // remove file aoqi@0: // aoqi@0: // this method removes the file specified by the given path aoqi@0: // aoqi@0: static void remove_file(const char* path) { aoqi@0: aoqi@0: int result; aoqi@0: aoqi@0: // if the file is a directory, the following unlink will fail. since aoqi@0: // we don't expect to find directories in the user temp directory, we aoqi@0: // won't try to handle this situation. even if accidentially or aoqi@0: // maliciously planted, the directory's presence won't hurt anything. aoqi@0: // aoqi@0: RESTARTABLE(::unlink(path), result); aoqi@0: if (PrintMiscellaneous && Verbose && result == OS_ERR) { aoqi@0: if (errno != ENOENT) { aoqi@0: warning("Could not unlink shared memory backing" aoqi@0: " store file %s : %s\n", path, strerror(errno)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: goetz@7515: // Cleanup stale shared memory resources aoqi@0: // aoqi@0: // This method attempts to remove all stale shared memory files in aoqi@0: // the named user temporary directory. It scans the named directory aoqi@0: // for files matching the pattern ^$[0-9]*$. For each file found, the aoqi@0: // process id is extracted from the file name and a test is run to aoqi@0: // determine if the process is alive. If the process is not alive, aoqi@0: // any stale file resources are removed. aoqi@0: static void cleanup_sharedmem_resources(const char* dirname) { aoqi@0: goetz@7515: int saved_cwd_fd; goetz@7515: // Open the directory. goetz@7515: DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd); aoqi@0: if (dirp == NULL) { goetz@7515: // Directory doesn't exist or is insecure, so there is nothing to cleanup. aoqi@0: return; aoqi@0: } aoqi@0: goetz@7515: // For each entry in the directory that matches the expected file aoqi@0: // name pattern, determine if the file resources are stale and if aoqi@0: // so, remove the file resources. Note, instrumented HotSpot processes aoqi@0: // for this user may start and/or terminate during this search and aoqi@0: // remove or create new files in this directory. The behavior of this aoqi@0: // loop under these conditions is dependent upon the implementation of aoqi@0: // opendir/readdir. aoqi@0: struct dirent* entry; aoqi@0: errno = 0; andrew@9711: while ((entry = os::readdir(dirp)) != NULL) { aoqi@0: aoqi@0: pid_t pid = filename_to_pid(entry->d_name); aoqi@0: aoqi@0: if (pid == 0) { aoqi@0: aoqi@0: if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { aoqi@0: goetz@7515: // Attempt to remove all unexpected files, except "." and "..". goetz@7515: unlink(entry->d_name); aoqi@0: } aoqi@0: aoqi@0: errno = 0; aoqi@0: continue; aoqi@0: } aoqi@0: goetz@7515: // We now have a file name that converts to a valid integer aoqi@0: // that could represent a process id . if this process id aoqi@0: // matches the current process id or the process is not running, aoqi@0: // then remove the stale file resources. aoqi@0: // goetz@7515: // Process liveness is detected by sending signal number 0 to aoqi@0: // the process id (see kill(2)). if kill determines that the aoqi@0: // process does not exist, then the file resources are removed. aoqi@0: // if kill determines that that we don't have permission to aoqi@0: // signal the process, then the file resources are assumed to aoqi@0: // be stale and are removed because the resources for such a aoqi@0: // process should be in a different user specific directory. aoqi@0: if ((pid == os::current_process_id()) || aoqi@0: (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { aoqi@0: goetz@7515: unlink(entry->d_name); aoqi@0: } aoqi@0: errno = 0; aoqi@0: } goetz@7515: goetz@7515: // Close the directory and reset the current working directory. goetz@7515: close_directory_secure_cwd(dirp, saved_cwd_fd); goetz@7515: aoqi@0: } aoqi@0: goetz@7515: // Make the user specific temporary directory. Returns true if aoqi@0: // the directory exists and is secure upon return. Returns false aoqi@0: // if the directory exists but is either a symlink, is otherwise aoqi@0: // insecure, or if an error occurred. aoqi@0: static bool make_user_tmp_dir(const char* dirname) { aoqi@0: goetz@7515: // Create the directory with 0755 permissions. note that the directory aoqi@0: // will be owned by euid::egid, which may not be the same as uid::gid. aoqi@0: if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) { aoqi@0: if (errno == EEXIST) { aoqi@0: // The directory already exists and was probably created by another aoqi@0: // JVM instance. However, this could also be the result of a aoqi@0: // deliberate symlink. Verify that the existing directory is safe. aoqi@0: if (!is_directory_secure(dirname)) { goetz@7515: // Directory is not secure. aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("%s directory is insecure\n", dirname); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: else { aoqi@0: // we encountered some other failure while attempting aoqi@0: // to create the directory aoqi@0: // aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("could not create directory %s: %s\n", aoqi@0: dirname, strerror(errno)); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // create the shared memory file resources aoqi@0: // aoqi@0: // This method creates the shared memory file with the given size aoqi@0: // This method also creates the user specific temporary directory, if aoqi@0: // it does not yet exist. aoqi@0: // aoqi@0: static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) { aoqi@0: aoqi@0: // make the user temporary directory aoqi@0: if (!make_user_tmp_dir(dirname)) { aoqi@0: // could not make/find the directory or the found directory aoqi@0: // was not secure aoqi@0: return -1; aoqi@0: } aoqi@0: goetz@7515: int saved_cwd_fd; goetz@7515: // Open the directory and set the current working directory to it. goetz@7515: DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd); goetz@7515: if (dirp == NULL) { goetz@7515: // Directory doesn't exist or is insecure, so cannot create shared goetz@7515: // memory file. goetz@7515: return -1; goetz@7515: } goetz@7515: goetz@7515: // Open the filename in the current directory. goetz@7515: // Cannot use O_TRUNC here; truncation of an existing file has to happen goetz@7515: // after the is_file_secure() check below. aoqi@0: int result; aoqi@0: goetz@7515: // No O_NOFOLLOW defined at buildtime, and it is not documented for open; goetz@7515: // so provide a workaround in this case. goetz@7515: #ifdef O_NOFOLLOW goetz@7515: RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result); goetz@7515: #else goetz@7515: // workaround function (jdk6 code) goetz@7515: RESTARTABLE(::open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE), result); goetz@7515: #endif goetz@7515: aoqi@0: if (result == OS_ERR) { aoqi@0: if (PrintMiscellaneous && Verbose) { goetz@7515: if (errno == ELOOP) { goetz@7515: warning("file %s is a symlink and is not secure\n", filename); goetz@7515: } else { goetz@7515: warning("could not create file %s: %s\n", filename, strerror(errno)); goetz@7515: } aoqi@0: } goetz@7515: // Close the directory and reset the current working directory. goetz@7515: close_directory_secure_cwd(dirp, saved_cwd_fd); goetz@7515: aoqi@0: return -1; aoqi@0: } goetz@7515: // Close the directory and reset the current working directory. goetz@7515: close_directory_secure_cwd(dirp, saved_cwd_fd); aoqi@0: aoqi@0: // save the file descriptor aoqi@0: int fd = result; aoqi@0: goetz@7515: // Check to see if the file is secure. goetz@7515: if (!is_file_secure(fd, filename)) { goetz@7515: ::close(fd); goetz@7515: return -1; goetz@7515: } goetz@7515: goetz@7515: // Truncate the file to get rid of any existing data. goetz@7515: RESTARTABLE(::ftruncate(fd, (off_t)0), result); goetz@7515: if (result == OS_ERR) { goetz@7515: if (PrintMiscellaneous && Verbose) { goetz@7515: warning("could not truncate shared memory file: %s\n", strerror(errno)); goetz@7515: } goetz@7515: ::close(fd); goetz@7515: return -1; goetz@7515: } aoqi@0: // set the file size aoqi@0: RESTARTABLE(::ftruncate(fd, (off_t)size), result); aoqi@0: if (result == OS_ERR) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("could not set shared memory file size: %s\n", strerror(errno)); aoqi@0: } aoqi@0: RESTARTABLE(::close(fd), result); aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: return fd; aoqi@0: } aoqi@0: aoqi@0: // open the shared memory file for the given user and vmid. returns aoqi@0: // the file descriptor for the open file or -1 if the file could not aoqi@0: // be opened. aoqi@0: // aoqi@0: static int open_sharedmem_file(const char* filename, int oflags, TRAPS) { aoqi@0: aoqi@0: // open the file aoqi@0: int result; goetz@7515: // No O_NOFOLLOW defined at buildtime, and it is not documented for open; goetz@7515: // so provide a workaround in this case goetz@7515: #ifdef O_NOFOLLOW aoqi@0: RESTARTABLE(::open(filename, oflags), result); goetz@7515: #else goetz@7515: RESTARTABLE(::open_o_nofollow(filename, oflags), result); goetz@7515: #endif goetz@7515: aoqi@0: if (result == OS_ERR) { aoqi@0: if (errno == ENOENT) { aoqi@0: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Process not found"); aoqi@0: } aoqi@0: else if (errno == EACCES) { aoqi@0: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Permission denied"); aoqi@0: } aoqi@0: else { aoqi@0: THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); aoqi@0: } aoqi@0: } goetz@7515: int fd = result; aoqi@0: goetz@7515: // Check to see if the file is secure. goetz@7515: if (!is_file_secure(fd, filename)) { goetz@7515: ::close(fd); goetz@7515: return -1; goetz@7515: } goetz@7515: goetz@7515: return fd; aoqi@0: } aoqi@0: aoqi@0: // create a named shared memory region. returns the address of the aoqi@0: // memory region on success or NULL on failure. A return value of aoqi@0: // NULL will ultimately disable the shared memory feature. aoqi@0: // aoqi@0: // On Solaris and Linux, the name space for shared memory objects aoqi@0: // is the file system name space. aoqi@0: // aoqi@0: // A monitoring application attaching to a JVM does not need to know aoqi@0: // the file system name of the shared memory object. However, it may aoqi@0: // be convenient for applications to discover the existence of newly aoqi@0: // created and terminating JVMs by watching the file system name space aoqi@0: // for files being created or removed. aoqi@0: // aoqi@0: static char* mmap_create_shared(size_t size) { aoqi@0: aoqi@0: int result; aoqi@0: int fd; aoqi@0: char* mapAddress; aoqi@0: aoqi@0: int vmid = os::current_process_id(); aoqi@0: aoqi@0: char* user_name = get_user_name(geteuid()); aoqi@0: aoqi@0: if (user_name == NULL) aoqi@0: return NULL; aoqi@0: aoqi@0: char* dirname = get_user_tmp_dir(user_name); aoqi@0: char* filename = get_sharedmem_filename(dirname, vmid); aoqi@0: goetz@7515: // Get the short filename. goetz@7515: char* short_filename = strrchr(filename, '/'); goetz@7515: if (short_filename == NULL) { goetz@7515: short_filename = filename; goetz@7515: } else { goetz@7515: short_filename++; goetz@7515: } goetz@7515: aoqi@0: // cleanup any stale shared memory files aoqi@0: cleanup_sharedmem_resources(dirname); aoqi@0: aoqi@0: assert(((size > 0) && (size % os::vm_page_size() == 0)), aoqi@0: "unexpected PerfMemory region size"); aoqi@0: goetz@7515: fd = create_sharedmem_resources(dirname, short_filename, size); aoqi@0: aoqi@0: FREE_C_HEAP_ARRAY(char, user_name, mtInternal); aoqi@0: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); aoqi@0: aoqi@0: if (fd == -1) { aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); aoqi@0: aoqi@0: // attempt to close the file - restart it if it was interrupted, aoqi@0: // but ignore other failures aoqi@0: RESTARTABLE(::close(fd), result); aoqi@0: assert(result != OS_ERR, "could not close file"); aoqi@0: aoqi@0: if (mapAddress == MAP_FAILED) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("mmap failed - %s\n", strerror(errno)); aoqi@0: } aoqi@0: remove_file(filename); aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // save the file name for use in delete_shared_memory() aoqi@0: backing_store_file_name = filename; aoqi@0: aoqi@0: // clear the shared memory region aoqi@0: (void)::memset((void*) mapAddress, 0, size); aoqi@0: goetz@7515: // It does not go through os api, the operation has to record from here. goetz@7515: MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal); goetz@7515: aoqi@0: return mapAddress; aoqi@0: } aoqi@0: aoqi@0: // release a named shared memory region aoqi@0: // aoqi@0: static void unmap_shared(char* addr, size_t bytes) { aoqi@0: // Do not rely on os::reserve_memory/os::release_memory to use mmap. aoqi@0: // Use os::reserve_memory/os::release_memory for PerfDisableSharedMem=1, mmap/munmap for PerfDisableSharedMem=0 aoqi@0: if (::munmap(addr, bytes) == -1) { aoqi@0: warning("perfmemory: munmap failed (%d)\n", errno); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // create the PerfData memory region in shared memory. aoqi@0: // aoqi@0: static char* create_shared_memory(size_t size) { aoqi@0: aoqi@0: // create the shared memory region. aoqi@0: return mmap_create_shared(size); aoqi@0: } aoqi@0: aoqi@0: // delete the shared PerfData memory region aoqi@0: // aoqi@0: static void delete_shared_memory(char* addr, size_t size) { aoqi@0: aoqi@0: // cleanup the persistent shared memory resources. since DestroyJavaVM does aoqi@0: // not support unloading of the JVM, unmapping of the memory resource is aoqi@0: // not performed. The memory will be reclaimed by the OS upon termination of aoqi@0: // the process. The backing store file is deleted from the file system. aoqi@0: aoqi@0: assert(!PerfDisableSharedMem, "shouldn't be here"); aoqi@0: aoqi@0: if (backing_store_file_name != NULL) { aoqi@0: remove_file(backing_store_file_name); aoqi@0: // Don't.. Free heap memory could deadlock os::abort() if it is called aoqi@0: // from signal handler. OS will reclaim the heap memory. aoqi@0: // FREE_C_HEAP_ARRAY(char, backing_store_file_name, mtInternal); aoqi@0: backing_store_file_name = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // return the size of the file for the given file descriptor aoqi@0: // or 0 if it is not a valid size for a shared memory file aoqi@0: // aoqi@0: static size_t sharedmem_filesize(int fd, TRAPS) { aoqi@0: aoqi@0: struct stat statbuf; aoqi@0: int result; aoqi@0: aoqi@0: RESTARTABLE(::fstat(fd, &statbuf), result); aoqi@0: if (result == OS_ERR) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("fstat failed: %s\n", strerror(errno)); aoqi@0: } aoqi@0: THROW_MSG_0(vmSymbols::java_io_IOException(), aoqi@0: "Could not determine PerfMemory size"); aoqi@0: } aoqi@0: aoqi@0: if ((statbuf.st_size == 0) || aoqi@0: ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { aoqi@0: THROW_MSG_0(vmSymbols::java_lang_Exception(), aoqi@0: "Invalid PerfMemory size"); aoqi@0: } aoqi@0: aoqi@0: return (size_t)statbuf.st_size; aoqi@0: } aoqi@0: aoqi@0: // attach to a named shared memory region. aoqi@0: // aoqi@0: static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { aoqi@0: aoqi@0: char* mapAddress; aoqi@0: int result; aoqi@0: int fd; goetz@7515: size_t size = 0; aoqi@0: const char* luser = NULL; aoqi@0: aoqi@0: int mmap_prot; aoqi@0: int file_flags; aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // map the high level access mode to the appropriate permission aoqi@0: // constructs for the file and the shared memory mapping. aoqi@0: if (mode == PerfMemory::PERF_MODE_RO) { aoqi@0: mmap_prot = PROT_READ; goetz@7515: goetz@7515: // No O_NOFOLLOW defined at buildtime, and it is not documented for open. goetz@7515: #ifdef O_NOFOLLOW goetz@7515: file_flags = O_RDONLY | O_NOFOLLOW; goetz@7515: #else aoqi@0: file_flags = O_RDONLY; goetz@7515: #endif aoqi@0: } aoqi@0: else if (mode == PerfMemory::PERF_MODE_RW) { aoqi@0: #ifdef LATER aoqi@0: mmap_prot = PROT_READ | PROT_WRITE; goetz@7515: file_flags = O_RDWR | O_NOFOLLOW; aoqi@0: #else aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Unsupported access mode"); aoqi@0: #endif aoqi@0: } aoqi@0: else { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Illegal access mode"); aoqi@0: } aoqi@0: aoqi@0: if (user == NULL || strlen(user) == 0) { aoqi@0: luser = get_user_name(vmid, CHECK); aoqi@0: } aoqi@0: else { aoqi@0: luser = user; aoqi@0: } aoqi@0: aoqi@0: if (luser == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Could not map vmid to user Name"); aoqi@0: } aoqi@0: aoqi@0: char* dirname = get_user_tmp_dir(luser); aoqi@0: aoqi@0: // since we don't follow symbolic links when creating the backing aoqi@0: // store file, we don't follow them when attaching either. aoqi@0: // aoqi@0: if (!is_directory_secure(dirname)) { aoqi@0: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); goetz@7515: if (luser != user) { goetz@7515: FREE_C_HEAP_ARRAY(char, luser, mtInternal); goetz@7515: } aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Process not found"); aoqi@0: } aoqi@0: aoqi@0: char* filename = get_sharedmem_filename(dirname, vmid); aoqi@0: aoqi@0: // copy heap memory to resource memory. the open_sharedmem_file aoqi@0: // method below need to use the filename, but could throw an aoqi@0: // exception. using a resource array prevents the leak that aoqi@0: // would otherwise occur. aoqi@0: char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); aoqi@0: strcpy(rfilename, filename); aoqi@0: aoqi@0: // free the c heap resources that are no longer needed aoqi@0: if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal); aoqi@0: FREE_C_HEAP_ARRAY(char, dirname, mtInternal); aoqi@0: FREE_C_HEAP_ARRAY(char, filename, mtInternal); aoqi@0: aoqi@0: // open the shared memory file for the give vmid aoqi@0: fd = open_sharedmem_file(rfilename, file_flags, CHECK); aoqi@0: assert(fd != OS_ERR, "unexpected value"); aoqi@0: aoqi@0: if (*sizep == 0) { aoqi@0: size = sharedmem_filesize(fd, CHECK); aoqi@0: assert(size != 0, "unexpected size"); aoqi@0: } else { aoqi@0: size = *sizep; aoqi@0: } aoqi@0: aoqi@0: mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); aoqi@0: aoqi@0: // attempt to close the file - restart if it gets interrupted, aoqi@0: // but ignore other failures aoqi@0: RESTARTABLE(::close(fd), result); aoqi@0: assert(result != OS_ERR, "could not close file"); aoqi@0: aoqi@0: if (mapAddress == MAP_FAILED) { aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("mmap failed: %s\n", strerror(errno)); aoqi@0: } aoqi@0: THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), aoqi@0: "Could not map PerfMemory"); aoqi@0: } aoqi@0: goetz@7515: // It does not go through os api, the operation has to record from here. goetz@7515: MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal); goetz@7515: aoqi@0: *addr = mapAddress; aoqi@0: *sizep = size; aoqi@0: aoqi@0: if (PerfTraceMemOps) { aoqi@0: tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " aoqi@0: INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: // create the PerfData memory region aoqi@0: // aoqi@0: // This method creates the memory region used to store performance aoqi@0: // data for the JVM. The memory may be created in standard or aoqi@0: // shared memory. aoqi@0: // aoqi@0: void PerfMemory::create_memory_region(size_t size) { aoqi@0: aoqi@0: if (PerfDisableSharedMem) { aoqi@0: // do not share the memory for the performance data. aoqi@0: _start = create_standard_memory(size); aoqi@0: } aoqi@0: else { aoqi@0: _start = create_shared_memory(size); aoqi@0: if (_start == NULL) { aoqi@0: aoqi@0: // creation of the shared memory region failed, attempt aoqi@0: // to create a contiguous, non-shared memory region instead. aoqi@0: // aoqi@0: if (PrintMiscellaneous && Verbose) { aoqi@0: warning("Reverting to non-shared PerfMemory region.\n"); aoqi@0: } aoqi@0: PerfDisableSharedMem = true; aoqi@0: _start = create_standard_memory(size); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_start != NULL) _capacity = size; aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // delete the PerfData memory region aoqi@0: // aoqi@0: // This method deletes the memory region used to store performance aoqi@0: // data for the JVM. The memory region indicated by the aoqi@0: // tuple will be inaccessible after a call to this method. aoqi@0: // aoqi@0: void PerfMemory::delete_memory_region() { aoqi@0: aoqi@0: assert((start() != NULL && capacity() > 0), "verify proper state"); aoqi@0: aoqi@0: // If user specifies PerfDataSaveFile, it will save the performance data aoqi@0: // to the specified file name no matter whether PerfDataSaveToFile is specified aoqi@0: // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag aoqi@0: // -XX:+PerfDataSaveToFile. aoqi@0: if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { aoqi@0: save_memory_to_file(start(), capacity()); aoqi@0: } aoqi@0: aoqi@0: if (PerfDisableSharedMem) { aoqi@0: delete_standard_memory(start(), capacity()); aoqi@0: } aoqi@0: else { aoqi@0: delete_shared_memory(start(), capacity()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // attach to the PerfData memory region for another JVM aoqi@0: // aoqi@0: // This method returns an tuple that points to aoqi@0: // a memory buffer that is kept reasonably synchronized with aoqi@0: // the PerfData memory region for the indicated JVM. This aoqi@0: // buffer may be kept in synchronization via shared memory aoqi@0: // or some other mechanism that keeps the buffer updated. aoqi@0: // aoqi@0: // If the JVM chooses not to support the attachability feature, aoqi@0: // this method should throw an UnsupportedOperation exception. aoqi@0: // aoqi@0: // This implementation utilizes named shared memory to map aoqi@0: // the indicated process's PerfData memory region into this JVMs aoqi@0: // address space. aoqi@0: // aoqi@0: void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { aoqi@0: aoqi@0: if (vmid == 0 || vmid == os::current_process_id()) { aoqi@0: *addrp = start(); aoqi@0: *sizep = capacity(); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); aoqi@0: } aoqi@0: aoqi@0: // detach from the PerfData memory region of another JVM aoqi@0: // aoqi@0: // This method detaches the PerfData memory region of another aoqi@0: // JVM, specified as an tuple of a buffer aoqi@0: // in this process's address space. This method may perform aoqi@0: // arbitrary actions to accomplish the detachment. The memory aoqi@0: // region specified by will be inaccessible after aoqi@0: // a call to this method. aoqi@0: // aoqi@0: // If the JVM chooses not to support the attachability feature, aoqi@0: // this method should throw an UnsupportedOperation exception. aoqi@0: // aoqi@0: // This implementation utilizes named shared memory to detach aoqi@0: // the indicated process's PerfData memory region from this aoqi@0: // process's address space. aoqi@0: // aoqi@0: void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { aoqi@0: aoqi@0: assert(addr != 0, "address sanity check"); aoqi@0: assert(bytes > 0, "capacity sanity check"); aoqi@0: aoqi@0: if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { aoqi@0: // prevent accidental detachment of this process's PerfMemory region aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: unmap_shared(addr, bytes); aoqi@0: } aoqi@0: aoqi@0: char* PerfMemory::backing_store_filename() { aoqi@0: return backing_store_file_name; aoqi@0: }