1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/bsd/vm/perfMemory_bsd.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,1049 @@ 1.4 +/* 1.5 + * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "classfile/vmSymbols.hpp" 1.30 +#include "memory/allocation.inline.hpp" 1.31 +#include "memory/resourceArea.hpp" 1.32 +#include "oops/oop.inline.hpp" 1.33 +#include "os_bsd.inline.hpp" 1.34 +#include "runtime/handles.inline.hpp" 1.35 +#include "runtime/perfMemory.hpp" 1.36 +#include "services/memTracker.hpp" 1.37 +#include "utilities/exceptions.hpp" 1.38 + 1.39 +// put OS-includes here 1.40 +# include <sys/types.h> 1.41 +# include <sys/mman.h> 1.42 +# include <errno.h> 1.43 +# include <stdio.h> 1.44 +# include <unistd.h> 1.45 +# include <sys/stat.h> 1.46 +# include <signal.h> 1.47 +# include <pwd.h> 1.48 + 1.49 +static char* backing_store_file_name = NULL; // name of the backing store 1.50 + // file, if successfully created. 1.51 + 1.52 +// Standard Memory Implementation Details 1.53 + 1.54 +// create the PerfData memory region in standard memory. 1.55 +// 1.56 +static char* create_standard_memory(size_t size) { 1.57 + 1.58 + // allocate an aligned chuck of memory 1.59 + char* mapAddress = os::reserve_memory(size); 1.60 + 1.61 + if (mapAddress == NULL) { 1.62 + return NULL; 1.63 + } 1.64 + 1.65 + // commit memory 1.66 + if (!os::commit_memory(mapAddress, size, !ExecMem)) { 1.67 + if (PrintMiscellaneous && Verbose) { 1.68 + warning("Could not commit PerfData memory\n"); 1.69 + } 1.70 + os::release_memory(mapAddress, size); 1.71 + return NULL; 1.72 + } 1.73 + 1.74 + return mapAddress; 1.75 +} 1.76 + 1.77 +// delete the PerfData memory region 1.78 +// 1.79 +static void delete_standard_memory(char* addr, size_t size) { 1.80 + 1.81 + // there are no persistent external resources to cleanup for standard 1.82 + // memory. since DestroyJavaVM does not support unloading of the JVM, 1.83 + // cleanup of the memory resource is not performed. The memory will be 1.84 + // reclaimed by the OS upon termination of the process. 1.85 + // 1.86 + return; 1.87 +} 1.88 + 1.89 +// save the specified memory region to the given file 1.90 +// 1.91 +// Note: this function might be called from signal handler (by os::abort()), 1.92 +// don't allocate heap memory. 1.93 +// 1.94 +static void save_memory_to_file(char* addr, size_t size) { 1.95 + 1.96 + const char* destfile = PerfMemory::get_perfdata_file_path(); 1.97 + assert(destfile[0] != '\0', "invalid PerfData file path"); 1.98 + 1.99 + int result; 1.100 + 1.101 + RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), 1.102 + result);; 1.103 + if (result == OS_ERR) { 1.104 + if (PrintMiscellaneous && Verbose) { 1.105 + warning("Could not create Perfdata save file: %s: %s\n", 1.106 + destfile, strerror(errno)); 1.107 + } 1.108 + } else { 1.109 + int fd = result; 1.110 + 1.111 + for (size_t remaining = size; remaining > 0;) { 1.112 + 1.113 + RESTARTABLE(::write(fd, addr, remaining), result); 1.114 + if (result == OS_ERR) { 1.115 + if (PrintMiscellaneous && Verbose) { 1.116 + warning("Could not write Perfdata save file: %s: %s\n", 1.117 + destfile, strerror(errno)); 1.118 + } 1.119 + break; 1.120 + } 1.121 + 1.122 + remaining -= (size_t)result; 1.123 + addr += result; 1.124 + } 1.125 + 1.126 + result = ::close(fd); 1.127 + if (PrintMiscellaneous && Verbose) { 1.128 + if (result == OS_ERR) { 1.129 + warning("Could not close %s: %s\n", destfile, strerror(errno)); 1.130 + } 1.131 + } 1.132 + } 1.133 + FREE_C_HEAP_ARRAY(char, destfile, mtInternal); 1.134 +} 1.135 + 1.136 + 1.137 +// Shared Memory Implementation Details 1.138 + 1.139 +// Note: the solaris and bsd shared memory implementation uses the mmap 1.140 +// interface with a backing store file to implement named shared memory. 1.141 +// Using the file system as the name space for shared memory allows a 1.142 +// common name space to be supported across a variety of platforms. It 1.143 +// also provides a name space that Java applications can deal with through 1.144 +// simple file apis. 1.145 +// 1.146 +// The solaris and bsd implementations store the backing store file in 1.147 +// a user specific temporary directory located in the /tmp file system, 1.148 +// which is always a local file system and is sometimes a RAM based file 1.149 +// system. 1.150 + 1.151 +// return the user specific temporary directory name. 1.152 +// 1.153 +// the caller is expected to free the allocated memory. 1.154 +// 1.155 +static char* get_user_tmp_dir(const char* user) { 1.156 + 1.157 + const char* tmpdir = os::get_temp_directory(); 1.158 + const char* perfdir = PERFDATA_NAME; 1.159 + size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; 1.160 + char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); 1.161 + 1.162 + // construct the path name to user specific tmp directory 1.163 + snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); 1.164 + 1.165 + return dirname; 1.166 +} 1.167 + 1.168 +// convert the given file name into a process id. if the file 1.169 +// does not meet the file naming constraints, return 0. 1.170 +// 1.171 +static pid_t filename_to_pid(const char* filename) { 1.172 + 1.173 + // a filename that doesn't begin with a digit is not a 1.174 + // candidate for conversion. 1.175 + // 1.176 + if (!isdigit(*filename)) { 1.177 + return 0; 1.178 + } 1.179 + 1.180 + // check if file name can be converted to an integer without 1.181 + // any leftover characters. 1.182 + // 1.183 + char* remainder = NULL; 1.184 + errno = 0; 1.185 + pid_t pid = (pid_t)strtol(filename, &remainder, 10); 1.186 + 1.187 + if (errno != 0) { 1.188 + return 0; 1.189 + } 1.190 + 1.191 + // check for left over characters. If any, then the filename is 1.192 + // not a candidate for conversion. 1.193 + // 1.194 + if (remainder != NULL && *remainder != '\0') { 1.195 + return 0; 1.196 + } 1.197 + 1.198 + // successful conversion, return the pid 1.199 + return pid; 1.200 +} 1.201 + 1.202 + 1.203 +// check if the given path is considered a secure directory for 1.204 +// the backing store files. Returns true if the directory exists 1.205 +// and is considered a secure location. Returns false if the path 1.206 +// is a symbolic link or if an error occurred. 1.207 +// 1.208 +static bool is_directory_secure(const char* path) { 1.209 + struct stat statbuf; 1.210 + int result = 0; 1.211 + 1.212 + RESTARTABLE(::lstat(path, &statbuf), result); 1.213 + if (result == OS_ERR) { 1.214 + return false; 1.215 + } 1.216 + 1.217 + // the path exists, now check it's mode 1.218 + if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) { 1.219 + // the path represents a link or some non-directory file type, 1.220 + // which is not what we expected. declare it insecure. 1.221 + // 1.222 + return false; 1.223 + } 1.224 + else { 1.225 + // we have an existing directory, check if the permissions are safe. 1.226 + // 1.227 + if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) { 1.228 + // the directory is open for writing and could be subjected 1.229 + // to a symlnk attack. declare it insecure. 1.230 + // 1.231 + return false; 1.232 + } 1.233 + } 1.234 + return true; 1.235 +} 1.236 + 1.237 + 1.238 +// return the user name for the given user id 1.239 +// 1.240 +// the caller is expected to free the allocated memory. 1.241 +// 1.242 +static char* get_user_name(uid_t uid) { 1.243 + 1.244 + struct passwd pwent; 1.245 + 1.246 + // determine the max pwbuf size from sysconf, and hardcode 1.247 + // a default if this not available through sysconf. 1.248 + // 1.249 + long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); 1.250 + if (bufsize == -1) 1.251 + bufsize = 1024; 1.252 + 1.253 + char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); 1.254 + 1.255 + // POSIX interface to getpwuid_r is used on LINUX 1.256 + struct passwd* p; 1.257 + int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p); 1.258 + 1.259 + if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { 1.260 + if (PrintMiscellaneous && Verbose) { 1.261 + if (result != 0) { 1.262 + warning("Could not retrieve passwd entry: %s\n", 1.263 + strerror(result)); 1.264 + } 1.265 + else if (p == NULL) { 1.266 + // this check is added to protect against an observed problem 1.267 + // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0, 1.268 + // indicating success, but has p == NULL. This was observed when 1.269 + // inserting a file descriptor exhaustion fault prior to the call 1.270 + // getpwuid_r() call. In this case, error is set to the appropriate 1.271 + // error condition, but this is undocumented behavior. This check 1.272 + // is safe under any condition, but the use of errno in the output 1.273 + // message may result in an erroneous message. 1.274 + // Bug Id 89052 was opened with RedHat. 1.275 + // 1.276 + warning("Could not retrieve passwd entry: %s\n", 1.277 + strerror(errno)); 1.278 + } 1.279 + else { 1.280 + warning("Could not determine user name: %s\n", 1.281 + p->pw_name == NULL ? "pw_name = NULL" : 1.282 + "pw_name zero length"); 1.283 + } 1.284 + } 1.285 + FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); 1.286 + return NULL; 1.287 + } 1.288 + 1.289 + char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal); 1.290 + strcpy(user_name, p->pw_name); 1.291 + 1.292 + FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); 1.293 + return user_name; 1.294 +} 1.295 + 1.296 +// return the name of the user that owns the process identified by vmid. 1.297 +// 1.298 +// This method uses a slow directory search algorithm to find the backing 1.299 +// store file for the specified vmid and returns the user name, as determined 1.300 +// by the user name suffix of the hsperfdata_<username> directory name. 1.301 +// 1.302 +// the caller is expected to free the allocated memory. 1.303 +// 1.304 +static char* get_user_name_slow(int vmid, TRAPS) { 1.305 + 1.306 + // short circuit the directory search if the process doesn't even exist. 1.307 + if (kill(vmid, 0) == OS_ERR) { 1.308 + if (errno == ESRCH) { 1.309 + THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1.310 + "Process not found"); 1.311 + } 1.312 + else /* EPERM */ { 1.313 + THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); 1.314 + } 1.315 + } 1.316 + 1.317 + // directory search 1.318 + char* oldest_user = NULL; 1.319 + time_t oldest_ctime = 0; 1.320 + 1.321 + const char* tmpdirname = os::get_temp_directory(); 1.322 + 1.323 + DIR* tmpdirp = os::opendir(tmpdirname); 1.324 + 1.325 + if (tmpdirp == NULL) { 1.326 + return NULL; 1.327 + } 1.328 + 1.329 + // for each entry in the directory that matches the pattern hsperfdata_*, 1.330 + // open the directory and check if the file for the given vmid exists. 1.331 + // The file with the expected name and the latest creation date is used 1.332 + // to determine the user name for the process id. 1.333 + // 1.334 + struct dirent* dentry; 1.335 + char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal); 1.336 + errno = 0; 1.337 + while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) { 1.338 + 1.339 + // check if the directory entry is a hsperfdata file 1.340 + if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) { 1.341 + continue; 1.342 + } 1.343 + 1.344 + char* usrdir_name = NEW_C_HEAP_ARRAY(char, 1.345 + strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal); 1.346 + strcpy(usrdir_name, tmpdirname); 1.347 + strcat(usrdir_name, "/"); 1.348 + strcat(usrdir_name, dentry->d_name); 1.349 + 1.350 + DIR* subdirp = os::opendir(usrdir_name); 1.351 + 1.352 + if (subdirp == NULL) { 1.353 + FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); 1.354 + continue; 1.355 + } 1.356 + 1.357 + // Since we don't create the backing store files in directories 1.358 + // pointed to by symbolic links, we also don't follow them when 1.359 + // looking for the files. We check for a symbolic link after the 1.360 + // call to opendir in order to eliminate a small window where the 1.361 + // symlink can be exploited. 1.362 + // 1.363 + if (!is_directory_secure(usrdir_name)) { 1.364 + FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); 1.365 + os::closedir(subdirp); 1.366 + continue; 1.367 + } 1.368 + 1.369 + struct dirent* udentry; 1.370 + char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal); 1.371 + errno = 0; 1.372 + while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) { 1.373 + 1.374 + if (filename_to_pid(udentry->d_name) == vmid) { 1.375 + struct stat statbuf; 1.376 + int result; 1.377 + 1.378 + char* filename = NEW_C_HEAP_ARRAY(char, 1.379 + strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal); 1.380 + 1.381 + strcpy(filename, usrdir_name); 1.382 + strcat(filename, "/"); 1.383 + strcat(filename, udentry->d_name); 1.384 + 1.385 + // don't follow symbolic links for the file 1.386 + RESTARTABLE(::lstat(filename, &statbuf), result); 1.387 + if (result == OS_ERR) { 1.388 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.389 + continue; 1.390 + } 1.391 + 1.392 + // skip over files that are not regular files. 1.393 + if (!S_ISREG(statbuf.st_mode)) { 1.394 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.395 + continue; 1.396 + } 1.397 + 1.398 + // compare and save filename with latest creation time 1.399 + if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) { 1.400 + 1.401 + if (statbuf.st_ctime > oldest_ctime) { 1.402 + char* user = strchr(dentry->d_name, '_') + 1; 1.403 + 1.404 + if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal); 1.405 + oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); 1.406 + 1.407 + strcpy(oldest_user, user); 1.408 + oldest_ctime = statbuf.st_ctime; 1.409 + } 1.410 + } 1.411 + 1.412 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.413 + } 1.414 + } 1.415 + os::closedir(subdirp); 1.416 + FREE_C_HEAP_ARRAY(char, udbuf, mtInternal); 1.417 + FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); 1.418 + } 1.419 + os::closedir(tmpdirp); 1.420 + FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal); 1.421 + 1.422 + return(oldest_user); 1.423 +} 1.424 + 1.425 +// return the name of the user that owns the JVM indicated by the given vmid. 1.426 +// 1.427 +static char* get_user_name(int vmid, TRAPS) { 1.428 + return get_user_name_slow(vmid, CHECK_NULL); 1.429 +} 1.430 + 1.431 +// return the file name of the backing store file for the named 1.432 +// shared memory region for the given user name and vmid. 1.433 +// 1.434 +// the caller is expected to free the allocated memory. 1.435 +// 1.436 +static char* get_sharedmem_filename(const char* dirname, int vmid) { 1.437 + 1.438 + // add 2 for the file separator and a null terminator. 1.439 + size_t nbytes = strlen(dirname) + UINT_CHARS + 2; 1.440 + 1.441 + char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); 1.442 + snprintf(name, nbytes, "%s/%d", dirname, vmid); 1.443 + 1.444 + return name; 1.445 +} 1.446 + 1.447 + 1.448 +// remove file 1.449 +// 1.450 +// this method removes the file specified by the given path 1.451 +// 1.452 +static void remove_file(const char* path) { 1.453 + 1.454 + int result; 1.455 + 1.456 + // if the file is a directory, the following unlink will fail. since 1.457 + // we don't expect to find directories in the user temp directory, we 1.458 + // won't try to handle this situation. even if accidentially or 1.459 + // maliciously planted, the directory's presence won't hurt anything. 1.460 + // 1.461 + RESTARTABLE(::unlink(path), result); 1.462 + if (PrintMiscellaneous && Verbose && result == OS_ERR) { 1.463 + if (errno != ENOENT) { 1.464 + warning("Could not unlink shared memory backing" 1.465 + " store file %s : %s\n", path, strerror(errno)); 1.466 + } 1.467 + } 1.468 +} 1.469 + 1.470 + 1.471 +// remove file 1.472 +// 1.473 +// this method removes the file with the given file name in the 1.474 +// named directory. 1.475 +// 1.476 +static void remove_file(const char* dirname, const char* filename) { 1.477 + 1.478 + size_t nbytes = strlen(dirname) + strlen(filename) + 2; 1.479 + char* path = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); 1.480 + 1.481 + strcpy(path, dirname); 1.482 + strcat(path, "/"); 1.483 + strcat(path, filename); 1.484 + 1.485 + remove_file(path); 1.486 + 1.487 + FREE_C_HEAP_ARRAY(char, path, mtInternal); 1.488 +} 1.489 + 1.490 + 1.491 +// cleanup stale shared memory resources 1.492 +// 1.493 +// This method attempts to remove all stale shared memory files in 1.494 +// the named user temporary directory. It scans the named directory 1.495 +// for files matching the pattern ^$[0-9]*$. For each file found, the 1.496 +// process id is extracted from the file name and a test is run to 1.497 +// determine if the process is alive. If the process is not alive, 1.498 +// any stale file resources are removed. 1.499 +// 1.500 +static void cleanup_sharedmem_resources(const char* dirname) { 1.501 + 1.502 + // open the user temp directory 1.503 + DIR* dirp = os::opendir(dirname); 1.504 + 1.505 + if (dirp == NULL) { 1.506 + // directory doesn't exist, so there is nothing to cleanup 1.507 + return; 1.508 + } 1.509 + 1.510 + if (!is_directory_secure(dirname)) { 1.511 + // the directory is not a secure directory 1.512 + return; 1.513 + } 1.514 + 1.515 + // for each entry in the directory that matches the expected file 1.516 + // name pattern, determine if the file resources are stale and if 1.517 + // so, remove the file resources. Note, instrumented HotSpot processes 1.518 + // for this user may start and/or terminate during this search and 1.519 + // remove or create new files in this directory. The behavior of this 1.520 + // loop under these conditions is dependent upon the implementation of 1.521 + // opendir/readdir. 1.522 + // 1.523 + struct dirent* entry; 1.524 + char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal); 1.525 + errno = 0; 1.526 + while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { 1.527 + 1.528 + pid_t pid = filename_to_pid(entry->d_name); 1.529 + 1.530 + if (pid == 0) { 1.531 + 1.532 + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 1.533 + 1.534 + // attempt to remove all unexpected files, except "." and ".." 1.535 + remove_file(dirname, entry->d_name); 1.536 + } 1.537 + 1.538 + errno = 0; 1.539 + continue; 1.540 + } 1.541 + 1.542 + // we now have a file name that converts to a valid integer 1.543 + // that could represent a process id . if this process id 1.544 + // matches the current process id or the process is not running, 1.545 + // then remove the stale file resources. 1.546 + // 1.547 + // process liveness is detected by sending signal number 0 to 1.548 + // the process id (see kill(2)). if kill determines that the 1.549 + // process does not exist, then the file resources are removed. 1.550 + // if kill determines that that we don't have permission to 1.551 + // signal the process, then the file resources are assumed to 1.552 + // be stale and are removed because the resources for such a 1.553 + // process should be in a different user specific directory. 1.554 + // 1.555 + if ((pid == os::current_process_id()) || 1.556 + (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { 1.557 + 1.558 + remove_file(dirname, entry->d_name); 1.559 + } 1.560 + errno = 0; 1.561 + } 1.562 + os::closedir(dirp); 1.563 + FREE_C_HEAP_ARRAY(char, dbuf, mtInternal); 1.564 +} 1.565 + 1.566 +// make the user specific temporary directory. Returns true if 1.567 +// the directory exists and is secure upon return. Returns false 1.568 +// if the directory exists but is either a symlink, is otherwise 1.569 +// insecure, or if an error occurred. 1.570 +// 1.571 +static bool make_user_tmp_dir(const char* dirname) { 1.572 + 1.573 + // create the directory with 0755 permissions. note that the directory 1.574 + // will be owned by euid::egid, which may not be the same as uid::gid. 1.575 + // 1.576 + if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) { 1.577 + if (errno == EEXIST) { 1.578 + // The directory already exists and was probably created by another 1.579 + // JVM instance. However, this could also be the result of a 1.580 + // deliberate symlink. Verify that the existing directory is safe. 1.581 + // 1.582 + if (!is_directory_secure(dirname)) { 1.583 + // directory is not secure 1.584 + if (PrintMiscellaneous && Verbose) { 1.585 + warning("%s directory is insecure\n", dirname); 1.586 + } 1.587 + return false; 1.588 + } 1.589 + } 1.590 + else { 1.591 + // we encountered some other failure while attempting 1.592 + // to create the directory 1.593 + // 1.594 + if (PrintMiscellaneous && Verbose) { 1.595 + warning("could not create directory %s: %s\n", 1.596 + dirname, strerror(errno)); 1.597 + } 1.598 + return false; 1.599 + } 1.600 + } 1.601 + return true; 1.602 +} 1.603 + 1.604 +// create the shared memory file resources 1.605 +// 1.606 +// This method creates the shared memory file with the given size 1.607 +// This method also creates the user specific temporary directory, if 1.608 +// it does not yet exist. 1.609 +// 1.610 +static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) { 1.611 + 1.612 + // make the user temporary directory 1.613 + if (!make_user_tmp_dir(dirname)) { 1.614 + // could not make/find the directory or the found directory 1.615 + // was not secure 1.616 + return -1; 1.617 + } 1.618 + 1.619 + int result; 1.620 + 1.621 + RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result); 1.622 + if (result == OS_ERR) { 1.623 + if (PrintMiscellaneous && Verbose) { 1.624 + warning("could not create file %s: %s\n", filename, strerror(errno)); 1.625 + } 1.626 + return -1; 1.627 + } 1.628 + 1.629 + // save the file descriptor 1.630 + int fd = result; 1.631 + 1.632 + // set the file size 1.633 + RESTARTABLE(::ftruncate(fd, (off_t)size), result); 1.634 + if (result == OS_ERR) { 1.635 + if (PrintMiscellaneous && Verbose) { 1.636 + warning("could not set shared memory file size: %s\n", strerror(errno)); 1.637 + } 1.638 + ::close(fd); 1.639 + return -1; 1.640 + } 1.641 + 1.642 + // Verify that we have enough disk space for this file. 1.643 + // We'll get random SIGBUS crashes on memory accesses if 1.644 + // we don't. 1.645 + 1.646 + for (size_t seekpos = 0; seekpos < size; seekpos += os::vm_page_size()) { 1.647 + int zero_int = 0; 1.648 + result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos)); 1.649 + if (result == -1 ) break; 1.650 + RESTARTABLE(::write(fd, &zero_int, 1), result); 1.651 + if (result != 1) { 1.652 + if (errno == ENOSPC) { 1.653 + warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename); 1.654 + } 1.655 + break; 1.656 + } 1.657 + } 1.658 + 1.659 + if (result != -1) { 1.660 + return fd; 1.661 + } else { 1.662 + ::close(fd); 1.663 + return -1; 1.664 + } 1.665 +} 1.666 + 1.667 +// open the shared memory file for the given user and vmid. returns 1.668 +// the file descriptor for the open file or -1 if the file could not 1.669 +// be opened. 1.670 +// 1.671 +static int open_sharedmem_file(const char* filename, int oflags, TRAPS) { 1.672 + 1.673 + // open the file 1.674 + int result; 1.675 + RESTARTABLE(::open(filename, oflags), result); 1.676 + if (result == OS_ERR) { 1.677 + if (errno == ENOENT) { 1.678 + THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 1.679 + "Process not found", OS_ERR); 1.680 + } 1.681 + else if (errno == EACCES) { 1.682 + THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 1.683 + "Permission denied", OS_ERR); 1.684 + } 1.685 + else { 1.686 + THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR); 1.687 + } 1.688 + } 1.689 + 1.690 + return result; 1.691 +} 1.692 + 1.693 +// create a named shared memory region. returns the address of the 1.694 +// memory region on success or NULL on failure. A return value of 1.695 +// NULL will ultimately disable the shared memory feature. 1.696 +// 1.697 +// On Solaris and Bsd, the name space for shared memory objects 1.698 +// is the file system name space. 1.699 +// 1.700 +// A monitoring application attaching to a JVM does not need to know 1.701 +// the file system name of the shared memory object. However, it may 1.702 +// be convenient for applications to discover the existence of newly 1.703 +// created and terminating JVMs by watching the file system name space 1.704 +// for files being created or removed. 1.705 +// 1.706 +static char* mmap_create_shared(size_t size) { 1.707 + 1.708 + int result; 1.709 + int fd; 1.710 + char* mapAddress; 1.711 + 1.712 + int vmid = os::current_process_id(); 1.713 + 1.714 + char* user_name = get_user_name(geteuid()); 1.715 + 1.716 + if (user_name == NULL) 1.717 + return NULL; 1.718 + 1.719 + char* dirname = get_user_tmp_dir(user_name); 1.720 + char* filename = get_sharedmem_filename(dirname, vmid); 1.721 + 1.722 + // cleanup any stale shared memory files 1.723 + cleanup_sharedmem_resources(dirname); 1.724 + 1.725 + assert(((size > 0) && (size % os::vm_page_size() == 0)), 1.726 + "unexpected PerfMemory region size"); 1.727 + 1.728 + fd = create_sharedmem_resources(dirname, filename, size); 1.729 + 1.730 + FREE_C_HEAP_ARRAY(char, user_name, mtInternal); 1.731 + FREE_C_HEAP_ARRAY(char, dirname, mtInternal); 1.732 + 1.733 + if (fd == -1) { 1.734 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.735 + return NULL; 1.736 + } 1.737 + 1.738 + mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 1.739 + 1.740 + result = ::close(fd); 1.741 + assert(result != OS_ERR, "could not close file"); 1.742 + 1.743 + if (mapAddress == MAP_FAILED) { 1.744 + if (PrintMiscellaneous && Verbose) { 1.745 + warning("mmap failed - %s\n", strerror(errno)); 1.746 + } 1.747 + remove_file(filename); 1.748 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.749 + return NULL; 1.750 + } 1.751 + 1.752 + // save the file name for use in delete_shared_memory() 1.753 + backing_store_file_name = filename; 1.754 + 1.755 + // clear the shared memory region 1.756 + (void)::memset((void*) mapAddress, 0, size); 1.757 + 1.758 + // it does not go through os api, the operation has to record from here 1.759 + MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); 1.760 + 1.761 + return mapAddress; 1.762 +} 1.763 + 1.764 +// release a named shared memory region 1.765 +// 1.766 +static void unmap_shared(char* addr, size_t bytes) { 1.767 + os::release_memory(addr, bytes); 1.768 +} 1.769 + 1.770 +// create the PerfData memory region in shared memory. 1.771 +// 1.772 +static char* create_shared_memory(size_t size) { 1.773 + 1.774 + // create the shared memory region. 1.775 + return mmap_create_shared(size); 1.776 +} 1.777 + 1.778 +// delete the shared PerfData memory region 1.779 +// 1.780 +static void delete_shared_memory(char* addr, size_t size) { 1.781 + 1.782 + // cleanup the persistent shared memory resources. since DestroyJavaVM does 1.783 + // not support unloading of the JVM, unmapping of the memory resource is 1.784 + // not performed. The memory will be reclaimed by the OS upon termination of 1.785 + // the process. The backing store file is deleted from the file system. 1.786 + 1.787 + assert(!PerfDisableSharedMem, "shouldn't be here"); 1.788 + 1.789 + if (backing_store_file_name != NULL) { 1.790 + remove_file(backing_store_file_name); 1.791 + // Don't.. Free heap memory could deadlock os::abort() if it is called 1.792 + // from signal handler. OS will reclaim the heap memory. 1.793 + // FREE_C_HEAP_ARRAY(char, backing_store_file_name); 1.794 + backing_store_file_name = NULL; 1.795 + } 1.796 +} 1.797 + 1.798 +// return the size of the file for the given file descriptor 1.799 +// or 0 if it is not a valid size for a shared memory file 1.800 +// 1.801 +static size_t sharedmem_filesize(int fd, TRAPS) { 1.802 + 1.803 + struct stat statbuf; 1.804 + int result; 1.805 + 1.806 + RESTARTABLE(::fstat(fd, &statbuf), result); 1.807 + if (result == OS_ERR) { 1.808 + if (PrintMiscellaneous && Verbose) { 1.809 + warning("fstat failed: %s\n", strerror(errno)); 1.810 + } 1.811 + THROW_MSG_0(vmSymbols::java_io_IOException(), 1.812 + "Could not determine PerfMemory size"); 1.813 + } 1.814 + 1.815 + if ((statbuf.st_size == 0) || 1.816 + ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { 1.817 + THROW_MSG_0(vmSymbols::java_lang_Exception(), 1.818 + "Invalid PerfMemory size"); 1.819 + } 1.820 + 1.821 + return (size_t)statbuf.st_size; 1.822 +} 1.823 + 1.824 +// attach to a named shared memory region. 1.825 +// 1.826 +static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { 1.827 + 1.828 + char* mapAddress; 1.829 + int result; 1.830 + int fd; 1.831 + size_t size = 0; 1.832 + const char* luser = NULL; 1.833 + 1.834 + int mmap_prot; 1.835 + int file_flags; 1.836 + 1.837 + ResourceMark rm; 1.838 + 1.839 + // map the high level access mode to the appropriate permission 1.840 + // constructs for the file and the shared memory mapping. 1.841 + if (mode == PerfMemory::PERF_MODE_RO) { 1.842 + mmap_prot = PROT_READ; 1.843 + file_flags = O_RDONLY; 1.844 + } 1.845 + else if (mode == PerfMemory::PERF_MODE_RW) { 1.846 +#ifdef LATER 1.847 + mmap_prot = PROT_READ | PROT_WRITE; 1.848 + file_flags = O_RDWR; 1.849 +#else 1.850 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.851 + "Unsupported access mode"); 1.852 +#endif 1.853 + } 1.854 + else { 1.855 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.856 + "Illegal access mode"); 1.857 + } 1.858 + 1.859 + if (user == NULL || strlen(user) == 0) { 1.860 + luser = get_user_name(vmid, CHECK); 1.861 + } 1.862 + else { 1.863 + luser = user; 1.864 + } 1.865 + 1.866 + if (luser == NULL) { 1.867 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.868 + "Could not map vmid to user Name"); 1.869 + } 1.870 + 1.871 + char* dirname = get_user_tmp_dir(luser); 1.872 + 1.873 + // since we don't follow symbolic links when creating the backing 1.874 + // store file, we don't follow them when attaching either. 1.875 + // 1.876 + if (!is_directory_secure(dirname)) { 1.877 + FREE_C_HEAP_ARRAY(char, dirname, mtInternal); 1.878 + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1.879 + "Process not found"); 1.880 + } 1.881 + 1.882 + char* filename = get_sharedmem_filename(dirname, vmid); 1.883 + 1.884 + // copy heap memory to resource memory. the open_sharedmem_file 1.885 + // method below need to use the filename, but could throw an 1.886 + // exception. using a resource array prevents the leak that 1.887 + // would otherwise occur. 1.888 + char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); 1.889 + strcpy(rfilename, filename); 1.890 + 1.891 + // free the c heap resources that are no longer needed 1.892 + if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal); 1.893 + FREE_C_HEAP_ARRAY(char, dirname, mtInternal); 1.894 + FREE_C_HEAP_ARRAY(char, filename, mtInternal); 1.895 + 1.896 + // open the shared memory file for the give vmid 1.897 + fd = open_sharedmem_file(rfilename, file_flags, CHECK); 1.898 + assert(fd != OS_ERR, "unexpected value"); 1.899 + 1.900 + if (*sizep == 0) { 1.901 + size = sharedmem_filesize(fd, CHECK); 1.902 + } else { 1.903 + size = *sizep; 1.904 + } 1.905 + 1.906 + assert(size > 0, "unexpected size <= 0"); 1.907 + 1.908 + mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); 1.909 + 1.910 + // attempt to close the file - restart if it gets interrupted, 1.911 + // but ignore other failures 1.912 + result = ::close(fd); 1.913 + assert(result != OS_ERR, "could not close file"); 1.914 + 1.915 + if (mapAddress == MAP_FAILED) { 1.916 + if (PrintMiscellaneous && Verbose) { 1.917 + warning("mmap failed: %s\n", strerror(errno)); 1.918 + } 1.919 + THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 1.920 + "Could not map PerfMemory"); 1.921 + } 1.922 + 1.923 + // it does not go through os api, the operation has to record from here 1.924 + MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); 1.925 + 1.926 + *addr = mapAddress; 1.927 + *sizep = size; 1.928 + 1.929 + if (PerfTraceMemOps) { 1.930 + tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " 1.931 + INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress)); 1.932 + } 1.933 +} 1.934 + 1.935 + 1.936 + 1.937 + 1.938 +// create the PerfData memory region 1.939 +// 1.940 +// This method creates the memory region used to store performance 1.941 +// data for the JVM. The memory may be created in standard or 1.942 +// shared memory. 1.943 +// 1.944 +void PerfMemory::create_memory_region(size_t size) { 1.945 + 1.946 + if (PerfDisableSharedMem) { 1.947 + // do not share the memory for the performance data. 1.948 + _start = create_standard_memory(size); 1.949 + } 1.950 + else { 1.951 + _start = create_shared_memory(size); 1.952 + if (_start == NULL) { 1.953 + 1.954 + // creation of the shared memory region failed, attempt 1.955 + // to create a contiguous, non-shared memory region instead. 1.956 + // 1.957 + if (PrintMiscellaneous && Verbose) { 1.958 + warning("Reverting to non-shared PerfMemory region.\n"); 1.959 + } 1.960 + PerfDisableSharedMem = true; 1.961 + _start = create_standard_memory(size); 1.962 + } 1.963 + } 1.964 + 1.965 + if (_start != NULL) _capacity = size; 1.966 + 1.967 +} 1.968 + 1.969 +// delete the PerfData memory region 1.970 +// 1.971 +// This method deletes the memory region used to store performance 1.972 +// data for the JVM. The memory region indicated by the <address, size> 1.973 +// tuple will be inaccessible after a call to this method. 1.974 +// 1.975 +void PerfMemory::delete_memory_region() { 1.976 + 1.977 + assert((start() != NULL && capacity() > 0), "verify proper state"); 1.978 + 1.979 + // If user specifies PerfDataSaveFile, it will save the performance data 1.980 + // to the specified file name no matter whether PerfDataSaveToFile is specified 1.981 + // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag 1.982 + // -XX:+PerfDataSaveToFile. 1.983 + if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { 1.984 + save_memory_to_file(start(), capacity()); 1.985 + } 1.986 + 1.987 + if (PerfDisableSharedMem) { 1.988 + delete_standard_memory(start(), capacity()); 1.989 + } 1.990 + else { 1.991 + delete_shared_memory(start(), capacity()); 1.992 + } 1.993 +} 1.994 + 1.995 +// attach to the PerfData memory region for another JVM 1.996 +// 1.997 +// This method returns an <address, size> tuple that points to 1.998 +// a memory buffer that is kept reasonably synchronized with 1.999 +// the PerfData memory region for the indicated JVM. This 1.1000 +// buffer may be kept in synchronization via shared memory 1.1001 +// or some other mechanism that keeps the buffer updated. 1.1002 +// 1.1003 +// If the JVM chooses not to support the attachability feature, 1.1004 +// this method should throw an UnsupportedOperation exception. 1.1005 +// 1.1006 +// This implementation utilizes named shared memory to map 1.1007 +// the indicated process's PerfData memory region into this JVMs 1.1008 +// address space. 1.1009 +// 1.1010 +void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { 1.1011 + 1.1012 + if (vmid == 0 || vmid == os::current_process_id()) { 1.1013 + *addrp = start(); 1.1014 + *sizep = capacity(); 1.1015 + return; 1.1016 + } 1.1017 + 1.1018 + mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); 1.1019 +} 1.1020 + 1.1021 +// detach from the PerfData memory region of another JVM 1.1022 +// 1.1023 +// This method detaches the PerfData memory region of another 1.1024 +// JVM, specified as an <address, size> tuple of a buffer 1.1025 +// in this process's address space. This method may perform 1.1026 +// arbitrary actions to accomplish the detachment. The memory 1.1027 +// region specified by <address, size> will be inaccessible after 1.1028 +// a call to this method. 1.1029 +// 1.1030 +// If the JVM chooses not to support the attachability feature, 1.1031 +// this method should throw an UnsupportedOperation exception. 1.1032 +// 1.1033 +// This implementation utilizes named shared memory to detach 1.1034 +// the indicated process's PerfData memory region from this 1.1035 +// process's address space. 1.1036 +// 1.1037 +void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { 1.1038 + 1.1039 + assert(addr != 0, "address sanity check"); 1.1040 + assert(bytes > 0, "capacity sanity check"); 1.1041 + 1.1042 + if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { 1.1043 + // prevent accidental detachment of this process's PerfMemory region 1.1044 + return; 1.1045 + } 1.1046 + 1.1047 + unmap_shared(addr, bytes); 1.1048 +} 1.1049 + 1.1050 +char* PerfMemory::backing_store_filename() { 1.1051 + return backing_store_file_name; 1.1052 +}