1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "os_aix.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/perfMemory.hpp"
  34 #include "services/memTracker.hpp"
  35 #include "utilities/exceptions.hpp"
  36 
  37 // put OS-includes here
  38 # include <sys/types.h>
  39 # include <sys/mman.h>
  40 # include <errno.h>
  41 # include <stdio.h>
  42 # include <unistd.h>
  43 # include <sys/stat.h>
  44 # include <signal.h>
  45 # include <pwd.h>
  46 
  47 static char* backing_store_file_name = NULL;  // name of the backing store
  48                                               // file, if successfully created.
  49 
  50 // Standard Memory Implementation Details
  51 
  52 // create the PerfData memory region in standard memory.
  53 //
  54 static char* create_standard_memory(size_t size) {
  55 
  56   // allocate an aligned chuck of memory
  57   char* mapAddress = os::reserve_memory(size);
  58 
  59   if (mapAddress == NULL) {
  60     return NULL;
  61   }
  62 
  63   // commit memory
  64   if (!os::commit_memory(mapAddress, size, !ExecMem)) {
  65     if (PrintMiscellaneous && Verbose) {
  66       warning("Could not commit PerfData memory\n");
  67     }
  68     os::release_memory(mapAddress, size);
  69     return NULL;
  70   }
  71 
  72   return mapAddress;
  73 }
  74 
  75 // delete the PerfData memory region
  76 //
  77 static void delete_standard_memory(char* addr, size_t size) {
  78 
  79   // there are no persistent external resources to cleanup for standard
  80   // memory. since DestroyJavaVM does not support unloading of the JVM,
  81   // cleanup of the memory resource is not performed. The memory will be
  82   // reclaimed by the OS upon termination of the process.
  83   //
  84   return;
  85 }
  86 
  87 // save the specified memory region to the given file
  88 //
  89 // Note: this function might be called from signal handler (by os::abort()),
  90 // don't allocate heap memory.
  91 //
  92 static void save_memory_to_file(char* addr, size_t size) {
  93 
  94   const char* destfile = PerfMemory::get_perfdata_file_path();
  95   assert(destfile[0] != '\0', "invalid PerfData file path");
  96 
  97   int result;
  98 
  99   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
 100               result);;
 101   if (result == OS_ERR) {
 102     if (PrintMiscellaneous && Verbose) {
 103       warning("Could not create Perfdata save file: %s: %s\n",
 104               destfile, strerror(errno));
 105     }
 106   } else {
 107     int fd = result;
 108 
 109     for (size_t remaining = size; remaining > 0;) {
 110 
 111       RESTARTABLE(::write(fd, addr, remaining), result);
 112       if (result == OS_ERR) {
 113         if (PrintMiscellaneous && Verbose) {
 114           warning("Could not write Perfdata save file: %s: %s\n",
 115                   destfile, strerror(errno));
 116         }
 117         break;
 118       }
 119 
 120       remaining -= (size_t)result;
 121       addr += result;
 122     }
 123 
 124     RESTARTABLE(::close(fd), result);
 125     if (PrintMiscellaneous && Verbose) {
 126       if (result == OS_ERR) {
 127         warning("Could not close %s: %s\n", destfile, strerror(errno));
 128       }
 129     }
 130   }
 131   FREE_C_HEAP_ARRAY(char, destfile);
 132 }
 133 
 134 
 135 // Shared Memory Implementation Details
 136 
 137 // Note: the solaris and linux shared memory implementation uses the mmap
 138 // interface with a backing store file to implement named shared memory.
 139 // Using the file system as the name space for shared memory allows a
 140 // common name space to be supported across a variety of platforms. It
 141 // also provides a name space that Java applications can deal with through
 142 // simple file apis.
 143 //
 144 // The solaris and linux implementations store the backing store file in
 145 // a user specific temporary directory located in the /tmp file system,
 146 // which is always a local file system and is sometimes a RAM based file
 147 // system.
 148 
 149 // return the user specific temporary directory name.
 150 //
 151 // the caller is expected to free the allocated memory.
 152 //
 153 static char* get_user_tmp_dir(const char* user) {
 154 
 155   const char* tmpdir = os::get_temp_directory();
 156   const char* perfdir = PERFDATA_NAME;
 157   size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
 158   char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 159 
 160   // construct the path name to user specific tmp directory
 161   snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
 162 
 163   return dirname;
 164 }
 165 
 166 // convert the given file name into a process id. if the file
 167 // does not meet the file naming constraints, return 0.
 168 //
 169 static pid_t filename_to_pid(const char* filename) {
 170 
 171   // a filename that doesn't begin with a digit is not a
 172   // candidate for conversion.
 173   //
 174   if (!isdigit(*filename)) {
 175     return 0;
 176   }
 177 
 178   // check if file name can be converted to an integer without
 179   // any leftover characters.
 180   //
 181   char* remainder = NULL;
 182   errno = 0;
 183   pid_t pid = (pid_t)strtol(filename, &remainder, 10);
 184 
 185   if (errno != 0) {
 186     return 0;
 187   }
 188 
 189   // check for left over characters. If any, then the filename is
 190   // not a candidate for conversion.
 191   //
 192   if (remainder != NULL && *remainder != '\0') {
 193     return 0;
 194   }
 195 
 196   // successful conversion, return the pid
 197   return pid;
 198 }
 199 
 200 // Check if the given statbuf is considered a secure directory for
 201 // the backing store files. Returns true if the directory is considered
 202 // a secure location. Returns false if the statbuf is a symbolic link or
 203 // if an error occurred.
 204 //
 205 static bool is_statbuf_secure(struct stat *statp) {
 206   if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
 207     // The path represents a link or some non-directory file type,
 208     // which is not what we expected. Declare it insecure.
 209     //
 210     return false;
 211   }
 212   // We have an existing directory, check if the permissions are safe.
 213   //
 214   if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
 215     // The directory is open for writing and could be subjected
 216     // to a symlink or a hard link attack. Declare it insecure.
 217     //
 218     return false;
 219   }
 220   // If user is not root then see if the uid of the directory matches the effective uid of the process.
 221   uid_t euid = geteuid();
 222   if ((euid != 0) && (statp->st_uid != euid)) {
 223     // The directory was not created by this user, declare it insecure.
 224     //
 225     return false;
 226   }
 227   return true;
 228 }
 229 
 230 
 231 // Check if the given path is considered a secure directory for
 232 // the backing store files. Returns true if the directory exists
 233 // and is considered a secure location. Returns false if the path
 234 // is a symbolic link or if an error occurred.
 235 //
 236 static bool is_directory_secure(const char* path) {
 237   struct stat statbuf;
 238   int result = 0;
 239 
 240   RESTARTABLE(::lstat(path, &statbuf), result);
 241   if (result == OS_ERR) {
 242     return false;
 243   }
 244 
 245   // The path exists, see if it is secure.
 246   return is_statbuf_secure(&statbuf);
 247 }
 248 
 249 // (Taken over from Solaris to support the O_NOFOLLOW case on AIX.)
 250 // Check if the given directory file descriptor is considered a secure
 251 // directory for the backing store files. Returns true if the directory
 252 // exists and is considered a secure location. Returns false if the path
 253 // is a symbolic link or if an error occurred.
 254 static bool is_dirfd_secure(int dir_fd) {
 255   struct stat statbuf;
 256   int result = 0;
 257 
 258   RESTARTABLE(::fstat(dir_fd, &statbuf), result);
 259   if (result == OS_ERR) {
 260     return false;
 261   }
 262 
 263   // The path exists, now check its mode.
 264   return is_statbuf_secure(&statbuf);
 265 }
 266 
 267 
 268 // Check to make sure fd1 and fd2 are referencing the same file system object.
 269 static bool is_same_fsobject(int fd1, int fd2) {
 270   struct stat statbuf1;
 271   struct stat statbuf2;
 272   int result = 0;
 273 
 274   RESTARTABLE(::fstat(fd1, &statbuf1), result);
 275   if (result == OS_ERR) {
 276     return false;
 277   }
 278   RESTARTABLE(::fstat(fd2, &statbuf2), result);
 279   if (result == OS_ERR) {
 280     return false;
 281   }
 282 
 283   if ((statbuf1.st_ino == statbuf2.st_ino) &&
 284       (statbuf1.st_dev == statbuf2.st_dev)) {
 285     return true;
 286   } else {
 287     return false;
 288   }
 289 }
 290 
 291 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
 292 // We use the jdk6 implementation here.
 293 #ifndef O_NOFOLLOW
 294 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
 295 // was done in jdk 5/6 hotspot by Oracle this way
 296 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
 297   struct stat orig_st;
 298   struct stat new_st;
 299   bool create;
 300   int error;
 301   int fd;
 302 
 303   create = false;
 304 
 305   if (lstat(path, &orig_st) != 0) {
 306     if (errno == ENOENT && (oflag & O_CREAT) != 0) {
 307       // File doesn't exist, but_we want to create it, add O_EXCL flag
 308       // to make sure no-one creates it (or a symlink) before us
 309       // This works as we expect with symlinks, from posix man page:
 310       // 'If O_EXCL  and  O_CREAT  are set, and path names a symbolic
 311       // link, open() shall fail and set errno to [EEXIST]'.
 312       oflag |= O_EXCL;
 313       create = true;
 314     } else {
 315       // File doesn't exist, and we are not creating it.
 316       return OS_ERR;
 317     }
 318   } else {
 319     // Lstat success, check if existing file is a link.
 320     if ((orig_st.st_mode & S_IFMT) == S_IFLNK)  {
 321       // File is a symlink.
 322       errno = ELOOP;
 323       return OS_ERR;
 324     }
 325   }
 326 
 327   if (use_mode == true) {
 328     fd = open(path, oflag, mode);
 329   } else {
 330     fd = open(path, oflag);
 331   }
 332 
 333   if (fd == OS_ERR) {
 334     return fd;
 335   }
 336 
 337   // Can't do inode checks on before/after if we created the file.
 338   if (create == false) {
 339     if (fstat(fd, &new_st) != 0) {
 340       // Keep errno from fstat, in case close also fails.
 341       error = errno;
 342       ::close(fd);
 343       errno = error;
 344       return OS_ERR;
 345     }
 346 
 347     if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
 348       // File was tampered with during race window.
 349       ::close(fd);
 350       errno = EEXIST;
 351       if (PrintMiscellaneous && Verbose) {
 352         warning("possible file tampering attempt detected when opening %s", path);
 353       }
 354       return OS_ERR;
 355     }
 356   }
 357 
 358   return fd;
 359 }
 360 
 361 static int open_o_nofollow(const char* path, int oflag, mode_t mode) {
 362   return open_o_nofollow_impl(path, oflag, mode, true);
 363 }
 364 
 365 static int open_o_nofollow(const char* path, int oflag) {
 366   return open_o_nofollow_impl(path, oflag, 0, false);
 367 }
 368 #endif
 369 
 370 // Open the directory of the given path and validate it.
 371 // Return a DIR * of the open directory.
 372 static DIR *open_directory_secure(const char* dirname) {
 373   // Open the directory using open() so that it can be verified
 374   // to be secure by calling is_dirfd_secure(), opendir() and then check
 375   // to see if they are the same file system object.  This method does not
 376   // introduce a window of opportunity for the directory to be attacked that
 377   // calling opendir() and is_directory_secure() does.
 378   int result;
 379   DIR *dirp = NULL;
 380 
 381   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
 382   // so provide a workaround in this case.
 383 #ifdef O_NOFOLLOW
 384   RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
 385 #else
 386   // workaround (jdk6 coding)
 387   RESTARTABLE(::open_o_nofollow(dirname, O_RDONLY), result);
 388 #endif
 389 
 390   if (result == OS_ERR) {
 391     // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
 392     if (PrintMiscellaneous && Verbose) {
 393       if (errno == ELOOP) {
 394         warning("directory %s is a symlink and is not secure\n", dirname);
 395       } else {
 396         warning("could not open directory %s: %s\n", dirname, strerror(errno));
 397       }
 398     }
 399     return dirp;
 400   }
 401   int fd = result;
 402 
 403   // Determine if the open directory is secure.
 404   if (!is_dirfd_secure(fd)) {
 405     // The directory is not a secure directory.
 406     os::close(fd);
 407     return dirp;
 408   }
 409 
 410   // Open the directory.
 411   dirp = ::opendir(dirname);
 412   if (dirp == NULL) {
 413     // The directory doesn't exist, close fd and return.
 414     os::close(fd);
 415     return dirp;
 416   }
 417 
 418   // Check to make sure fd and dirp are referencing the same file system object.
 419   if (!is_same_fsobject(fd, dirp->dd_fd)) {
 420     // The directory is not secure.
 421     os::close(fd);
 422     os::closedir(dirp);
 423     dirp = NULL;
 424     return dirp;
 425   }
 426 
 427   // Close initial open now that we know directory is secure
 428   os::close(fd);
 429 
 430   return dirp;
 431 }
 432 
 433 // NOTE: The code below uses fchdir(), open() and unlink() because
 434 // fdopendir(), openat() and unlinkat() are not supported on all
 435 // versions.  Once the support for fdopendir(), openat() and unlinkat()
 436 // is available on all supported versions the code can be changed
 437 // to use these functions.
 438 
 439 // Open the directory of the given path, validate it and set the
 440 // current working directory to it.
 441 // Return a DIR * of the open directory and the saved cwd fd.
 442 //
 443 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
 444 
 445   // Open the directory.
 446   DIR* dirp = open_directory_secure(dirname);
 447   if (dirp == NULL) {
 448     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 449     return dirp;
 450   }
 451   int fd = dirp->dd_fd;
 452 
 453   // Open a fd to the cwd and save it off.
 454   int result;
 455   RESTARTABLE(::open(".", O_RDONLY), result);
 456   if (result == OS_ERR) {
 457     *saved_cwd_fd = -1;
 458   } else {
 459     *saved_cwd_fd = result;
 460   }
 461 
 462   // Set the current directory to dirname by using the fd of the directory and
 463   // handle errors, otherwise shared memory files will be created in cwd.
 464   result = fchdir(fd);
 465   if (result == OS_ERR) {
 466     if (PrintMiscellaneous && Verbose) {
 467       warning("could not change to directory %s", dirname);
 468     }
 469     if (*saved_cwd_fd != -1) {
 470       ::close(*saved_cwd_fd);
 471       *saved_cwd_fd = -1;
 472     }
 473     // Close the directory.
 474     os::closedir(dirp);
 475     return NULL;
 476   } else {
 477     return dirp;
 478   }
 479 }
 480 
 481 // Close the directory and restore the current working directory.
 482 //
 483 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
 484 
 485   int result;
 486   // If we have a saved cwd change back to it and close the fd.
 487   if (saved_cwd_fd != -1) {
 488     result = fchdir(saved_cwd_fd);
 489     ::close(saved_cwd_fd);
 490   }
 491 
 492   // Close the directory.
 493   os::closedir(dirp);
 494 }
 495 
 496 // Check if the given file descriptor is considered a secure.
 497 static bool is_file_secure(int fd, const char *filename) {
 498 
 499   int result;
 500   struct stat statbuf;
 501 
 502   // Determine if the file is secure.
 503   RESTARTABLE(::fstat(fd, &statbuf), result);
 504   if (result == OS_ERR) {
 505     if (PrintMiscellaneous && Verbose) {
 506       warning("fstat failed on %s: %s\n", filename, strerror(errno));
 507     }
 508     return false;
 509   }
 510   if (statbuf.st_nlink > 1) {
 511     // A file with multiple links is not expected.
 512     if (PrintMiscellaneous && Verbose) {
 513       warning("file %s has multiple links\n", filename);
 514     }
 515     return false;
 516   }
 517   return true;
 518 }
 519 
 520 // Return the user name for the given user id.
 521 //
 522 // The caller is expected to free the allocated memory.
 523 static char* get_user_name(uid_t uid) {
 524 
 525   struct passwd pwent;
 526 
 527   // Determine the max pwbuf size from sysconf, and hardcode
 528   // a default if this not available through sysconf.
 529   long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
 530   if (bufsize == -1)
 531     bufsize = 1024;
 532 
 533   char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
 534 
 535   struct passwd* p;
 536   int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
 537 
 538   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
 539     if (PrintMiscellaneous && Verbose) {
 540       if (result != 0) {
 541         warning("Could not retrieve passwd entry: %s\n",
 542                 strerror(result));
 543       }
 544       else if (p == NULL) {
 545         // this check is added to protect against an observed problem
 546         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
 547         // indicating success, but has p == NULL. This was observed when
 548         // inserting a file descriptor exhaustion fault prior to the call
 549         // getpwuid_r() call. In this case, error is set to the appropriate
 550         // error condition, but this is undocumented behavior. This check
 551         // is safe under any condition, but the use of errno in the output
 552         // message may result in an erroneous message.
 553         // Bug Id 89052 was opened with RedHat.
 554         //
 555         warning("Could not retrieve passwd entry: %s\n",
 556                 strerror(errno));
 557       }
 558       else {
 559         warning("Could not determine user name: %s\n",
 560                 p->pw_name == NULL ? "pw_name = NULL" :
 561                                      "pw_name zero length");
 562       }
 563     }
 564     FREE_C_HEAP_ARRAY(char, pwbuf);
 565     return NULL;
 566   }
 567 
 568   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
 569   strcpy(user_name, p->pw_name);
 570 
 571   FREE_C_HEAP_ARRAY(char, pwbuf);
 572   return user_name;
 573 }
 574 
 575 // return the name of the user that owns the process identified by vmid.
 576 //
 577 // This method uses a slow directory search algorithm to find the backing
 578 // store file for the specified vmid and returns the user name, as determined
 579 // by the user name suffix of the hsperfdata_<username> directory name.
 580 //
 581 // the caller is expected to free the allocated memory.
 582 //
 583 static char* get_user_name_slow(int vmid, TRAPS) {
 584 
 585   // short circuit the directory search if the process doesn't even exist.
 586   if (kill(vmid, 0) == OS_ERR) {
 587     if (errno == ESRCH) {
 588       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 589                   "Process not found");
 590     }
 591     else /* EPERM */ {
 592       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
 593     }
 594   }
 595 
 596   // directory search
 597   char* oldest_user = NULL;
 598   time_t oldest_ctime = 0;
 599 
 600   const char* tmpdirname = os::get_temp_directory();
 601 
 602   DIR* tmpdirp = os::opendir(tmpdirname);
 603 
 604   if (tmpdirp == NULL) {
 605     return NULL;
 606   }
 607 
 608   // for each entry in the directory that matches the pattern hsperfdata_*,
 609   // open the directory and check if the file for the given vmid exists.
 610   // The file with the expected name and the latest creation date is used
 611   // to determine the user name for the process id.
 612   //
 613   struct dirent* dentry;
 614   char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
 615   errno = 0;
 616   while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
 617 
 618     // check if the directory entry is a hsperfdata file
 619     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
 620       continue;
 621     }
 622 
 623     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
 624                               strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
 625     strcpy(usrdir_name, tmpdirname);
 626     strcat(usrdir_name, "/");
 627     strcat(usrdir_name, dentry->d_name);
 628 
 629     // Open the user directory.
 630     DIR* subdirp = open_directory_secure(usrdir_name);
 631 
 632     if (subdirp == NULL) {
 633       FREE_C_HEAP_ARRAY(char, usrdir_name);
 634       continue;
 635     }
 636 
 637     // Since we don't create the backing store files in directories
 638     // pointed to by symbolic links, we also don't follow them when
 639     // looking for the files. We check for a symbolic link after the
 640     // call to opendir in order to eliminate a small window where the
 641     // symlink can be exploited.
 642     //
 643     if (!is_directory_secure(usrdir_name)) {
 644       FREE_C_HEAP_ARRAY(char, usrdir_name);
 645       os::closedir(subdirp);
 646       continue;
 647     }
 648 
 649     struct dirent* udentry;
 650     char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
 651     errno = 0;
 652     while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
 653 
 654       if (filename_to_pid(udentry->d_name) == vmid) {
 655         struct stat statbuf;
 656         int result;
 657 
 658         char* filename = NEW_C_HEAP_ARRAY(char,
 659                             strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
 660 
 661         strcpy(filename, usrdir_name);
 662         strcat(filename, "/");
 663         strcat(filename, udentry->d_name);
 664 
 665         // don't follow symbolic links for the file
 666         RESTARTABLE(::lstat(filename, &statbuf), result);
 667         if (result == OS_ERR) {
 668            FREE_C_HEAP_ARRAY(char, filename);
 669            continue;
 670         }
 671 
 672         // skip over files that are not regular files.
 673         if (!S_ISREG(statbuf.st_mode)) {
 674           FREE_C_HEAP_ARRAY(char, filename);
 675           continue;
 676         }
 677 
 678         // compare and save filename with latest creation time
 679         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
 680 
 681           if (statbuf.st_ctime > oldest_ctime) {
 682             char* user = strchr(dentry->d_name, '_') + 1;
 683 
 684             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
 685             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
 686 
 687             strcpy(oldest_user, user);
 688             oldest_ctime = statbuf.st_ctime;
 689           }
 690         }
 691 
 692         FREE_C_HEAP_ARRAY(char, filename);
 693       }
 694     }
 695     os::closedir(subdirp);
 696     FREE_C_HEAP_ARRAY(char, udbuf);
 697     FREE_C_HEAP_ARRAY(char, usrdir_name);
 698   }
 699   os::closedir(tmpdirp);
 700   FREE_C_HEAP_ARRAY(char, tdbuf);
 701 
 702   return(oldest_user);
 703 }
 704 
 705 // return the name of the user that owns the JVM indicated by the given vmid.
 706 //
 707 static char* get_user_name(int vmid, TRAPS) {
 708   return get_user_name_slow(vmid, THREAD);
 709 }
 710 
 711 // return the file name of the backing store file for the named
 712 // shared memory region for the given user name and vmid.
 713 //
 714 // the caller is expected to free the allocated memory.
 715 //
 716 static char* get_sharedmem_filename(const char* dirname, int vmid) {
 717 
 718   // add 2 for the file separator and a null terminator.
 719   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
 720 
 721   char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 722   snprintf(name, nbytes, "%s/%d", dirname, vmid);
 723 
 724   return name;
 725 }
 726 
 727 
 728 // remove file
 729 //
 730 // this method removes the file specified by the given path
 731 //
 732 static void remove_file(const char* path) {
 733 
 734   int result;
 735 
 736   // if the file is a directory, the following unlink will fail. since
 737   // we don't expect to find directories in the user temp directory, we
 738   // won't try to handle this situation. even if accidentially or
 739   // maliciously planted, the directory's presence won't hurt anything.
 740   //
 741   RESTARTABLE(::unlink(path), result);
 742   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
 743     if (errno != ENOENT) {
 744       warning("Could not unlink shared memory backing"
 745               " store file %s : %s\n", path, strerror(errno));
 746     }
 747   }
 748 }
 749 
 750 // Cleanup stale shared memory resources
 751 //
 752 // This method attempts to remove all stale shared memory files in
 753 // the named user temporary directory. It scans the named directory
 754 // for files matching the pattern ^$[0-9]*$. For each file found, the
 755 // process id is extracted from the file name and a test is run to
 756 // determine if the process is alive. If the process is not alive,
 757 // any stale file resources are removed.
 758 static void cleanup_sharedmem_resources(const char* dirname) {
 759 
 760   int saved_cwd_fd;
 761   // Open the directory.
 762   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 763   if (dirp == NULL) {
 764      // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 765     return;
 766   }
 767 
 768   // For each entry in the directory that matches the expected file
 769   // name pattern, determine if the file resources are stale and if
 770   // so, remove the file resources. Note, instrumented HotSpot processes
 771   // for this user may start and/or terminate during this search and
 772   // remove or create new files in this directory. The behavior of this
 773   // loop under these conditions is dependent upon the implementation of
 774   // opendir/readdir.
 775   struct dirent* entry;
 776   char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
 777 
 778   errno = 0;
 779   while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
 780 
 781     pid_t pid = filename_to_pid(entry->d_name);
 782 
 783     if (pid == 0) {
 784 
 785       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
 786 
 787         // Attempt to remove all unexpected files, except "." and "..".
 788         unlink(entry->d_name);
 789       }
 790 
 791       errno = 0;
 792       continue;
 793     }
 794 
 795     // We now have a file name that converts to a valid integer
 796     // that could represent a process id . if this process id
 797     // matches the current process id or the process is not running,
 798     // then remove the stale file resources.
 799     //
 800     // Process liveness is detected by sending signal number 0 to
 801     // the process id (see kill(2)). if kill determines that the
 802     // process does not exist, then the file resources are removed.
 803     // if kill determines that that we don't have permission to
 804     // signal the process, then the file resources are assumed to
 805     // be stale and are removed because the resources for such a
 806     // process should be in a different user specific directory.
 807     if ((pid == os::current_process_id()) ||
 808         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
 809 
 810         unlink(entry->d_name);
 811     }
 812     errno = 0;
 813   }
 814 
 815   // Close the directory and reset the current working directory.
 816   close_directory_secure_cwd(dirp, saved_cwd_fd);
 817 
 818   FREE_C_HEAP_ARRAY(char, dbuf);
 819 }
 820 
 821 // Make the user specific temporary directory. Returns true if
 822 // the directory exists and is secure upon return. Returns false
 823 // if the directory exists but is either a symlink, is otherwise
 824 // insecure, or if an error occurred.
 825 static bool make_user_tmp_dir(const char* dirname) {
 826 
 827   // Create the directory with 0755 permissions. note that the directory
 828   // will be owned by euid::egid, which may not be the same as uid::gid.
 829   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
 830     if (errno == EEXIST) {
 831       // The directory already exists and was probably created by another
 832       // JVM instance. However, this could also be the result of a
 833       // deliberate symlink. Verify that the existing directory is safe.
 834       if (!is_directory_secure(dirname)) {
 835         // Directory is not secure.
 836         if (PrintMiscellaneous && Verbose) {
 837           warning("%s directory is insecure\n", dirname);
 838         }
 839         return false;
 840       }
 841     }
 842     else {
 843       // we encountered some other failure while attempting
 844       // to create the directory
 845       //
 846       if (PrintMiscellaneous && Verbose) {
 847         warning("could not create directory %s: %s\n",
 848                 dirname, strerror(errno));
 849       }
 850       return false;
 851     }
 852   }
 853   return true;
 854 }
 855 
 856 // create the shared memory file resources
 857 //
 858 // This method creates the shared memory file with the given size
 859 // This method also creates the user specific temporary directory, if
 860 // it does not yet exist.
 861 //
 862 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
 863 
 864   // make the user temporary directory
 865   if (!make_user_tmp_dir(dirname)) {
 866     // could not make/find the directory or the found directory
 867     // was not secure
 868     return -1;
 869   }
 870 
 871   int saved_cwd_fd;
 872   // Open the directory and set the current working directory to it.
 873   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 874   if (dirp == NULL) {
 875     // Directory doesn't exist or is insecure, so cannot create shared
 876     // memory file.
 877     return -1;
 878   }
 879 
 880   // Open the filename in the current directory.
 881   // Cannot use O_TRUNC here; truncation of an existing file has to happen
 882   // after the is_file_secure() check below.
 883   int result;
 884 
 885   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
 886   // so provide a workaround in this case.
 887 #ifdef O_NOFOLLOW
 888   RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
 889 #else
 890   // workaround function (jdk6 code)
 891   RESTARTABLE(::open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE), result);
 892 #endif
 893 
 894   if (result == OS_ERR) {
 895     if (PrintMiscellaneous && Verbose) {
 896       if (errno == ELOOP) {
 897         warning("file %s is a symlink and is not secure\n", filename);
 898       } else {
 899         warning("could not create file %s: %s\n", filename, strerror(errno));
 900       }
 901     }
 902     // Close the directory and reset the current working directory.
 903     close_directory_secure_cwd(dirp, saved_cwd_fd);
 904 
 905     return -1;
 906   }
 907   // Close the directory and reset the current working directory.
 908   close_directory_secure_cwd(dirp, saved_cwd_fd);
 909 
 910   // save the file descriptor
 911   int fd = result;
 912 
 913   // Check to see if the file is secure.
 914   if (!is_file_secure(fd, filename)) {
 915     ::close(fd);
 916     return -1;
 917   }
 918 
 919   // Truncate the file to get rid of any existing data.
 920   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
 921   if (result == OS_ERR) {
 922     if (PrintMiscellaneous && Verbose) {
 923       warning("could not truncate shared memory file: %s\n", strerror(errno));
 924     }
 925     ::close(fd);
 926     return -1;
 927   }
 928   // set the file size
 929   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
 930   if (result == OS_ERR) {
 931     if (PrintMiscellaneous && Verbose) {
 932       warning("could not set shared memory file size: %s\n", strerror(errno));
 933     }
 934     RESTARTABLE(::close(fd), result);
 935     return -1;
 936   }
 937 
 938   return fd;
 939 }
 940 
 941 // open the shared memory file for the given user and vmid. returns
 942 // the file descriptor for the open file or -1 if the file could not
 943 // be opened.
 944 //
 945 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
 946 
 947   // open the file
 948   int result;
 949   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
 950   // so provide a workaround in this case
 951 #ifdef O_NOFOLLOW
 952   RESTARTABLE(::open(filename, oflags), result);
 953 #else
 954   RESTARTABLE(::open_o_nofollow(filename, oflags), result);
 955 #endif
 956 
 957   if (result == OS_ERR) {
 958     if (errno == ENOENT) {
 959       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 960                   "Process not found");
 961     }
 962     else if (errno == EACCES) {
 963       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 964                   "Permission denied");
 965     }
 966     else {
 967       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
 968     }
 969   }
 970   int fd = result;
 971 
 972   // Check to see if the file is secure.
 973   if (!is_file_secure(fd, filename)) {
 974     ::close(fd);
 975     return -1;
 976   }
 977 
 978   return fd;
 979 }
 980 
 981 // create a named shared memory region. returns the address of the
 982 // memory region on success or NULL on failure. A return value of
 983 // NULL will ultimately disable the shared memory feature.
 984 //
 985 // On AIX, Solaris and Linux, the name space for shared memory objects
 986 // is the file system name space.
 987 //
 988 // A monitoring application attaching to a JVM does not need to know
 989 // the file system name of the shared memory object. However, it may
 990 // be convenient for applications to discover the existence of newly
 991 // created and terminating JVMs by watching the file system name space
 992 // for files being created or removed.
 993 //
 994 static char* mmap_create_shared(size_t size) {
 995 
 996   int result;
 997   int fd;
 998   char* mapAddress;
 999 
1000   int vmid = os::current_process_id();
1001 
1002   char* user_name = get_user_name(geteuid());
1003 
1004   if (user_name == NULL)
1005     return NULL;
1006 
1007   char* dirname = get_user_tmp_dir(user_name);
1008   char* filename = get_sharedmem_filename(dirname, vmid);
1009 
1010   // Get the short filename.
1011   char* short_filename = strrchr(filename, '/');
1012   if (short_filename == NULL) {
1013     short_filename = filename;
1014   } else {
1015     short_filename++;
1016   }
1017 
1018   // cleanup any stale shared memory files
1019   cleanup_sharedmem_resources(dirname);
1020 
1021   assert(((size > 0) && (size % os::vm_page_size() == 0)),
1022          "unexpected PerfMemory region size");
1023 
1024   fd = create_sharedmem_resources(dirname, short_filename, size);
1025 
1026   FREE_C_HEAP_ARRAY(char, user_name);
1027   FREE_C_HEAP_ARRAY(char, dirname);
1028 
1029   if (fd == -1) {
1030     FREE_C_HEAP_ARRAY(char, filename);
1031     return NULL;
1032   }
1033 
1034   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1035 
1036   // attempt to close the file - restart it if it was interrupted,
1037   // but ignore other failures
1038   RESTARTABLE(::close(fd), result);
1039   assert(result != OS_ERR, "could not close file");
1040 
1041   if (mapAddress == MAP_FAILED) {
1042     if (PrintMiscellaneous && Verbose) {
1043       warning("mmap failed -  %s\n", strerror(errno));
1044     }
1045     remove_file(filename);
1046     FREE_C_HEAP_ARRAY(char, filename);
1047     return NULL;
1048   }
1049 
1050   // save the file name for use in delete_shared_memory()
1051   backing_store_file_name = filename;
1052 
1053   // clear the shared memory region
1054   (void)::memset((void*) mapAddress, 0, size);
1055 
1056   // It does not go through os api, the operation has to record from here.
1057   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1058 
1059   return mapAddress;
1060 }
1061 
1062 // release a named shared memory region
1063 //
1064 static void unmap_shared(char* addr, size_t bytes) {
1065   // Do not rely on os::reserve_memory/os::release_memory to use mmap.
1066   // Use os::reserve_memory/os::release_memory for PerfDisableSharedMem=1, mmap/munmap for PerfDisableSharedMem=0
1067   if (::munmap(addr, bytes) == -1) {
1068     warning("perfmemory: munmap failed (%d)\n", errno);
1069   }
1070 }
1071 
1072 // create the PerfData memory region in shared memory.
1073 //
1074 static char* create_shared_memory(size_t size) {
1075 
1076   // create the shared memory region.
1077   return mmap_create_shared(size);
1078 }
1079 
1080 // delete the shared PerfData memory region
1081 //
1082 static void delete_shared_memory(char* addr, size_t size) {
1083 
1084   // cleanup the persistent shared memory resources. since DestroyJavaVM does
1085   // not support unloading of the JVM, unmapping of the memory resource is
1086   // not performed. The memory will be reclaimed by the OS upon termination of
1087   // the process. The backing store file is deleted from the file system.
1088 
1089   assert(!PerfDisableSharedMem, "shouldn't be here");
1090 
1091   if (backing_store_file_name != NULL) {
1092     remove_file(backing_store_file_name);
1093     // Don't.. Free heap memory could deadlock os::abort() if it is called
1094     // from signal handler. OS will reclaim the heap memory.
1095     // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
1096     backing_store_file_name = NULL;
1097   }
1098 }
1099 
1100 // return the size of the file for the given file descriptor
1101 // or 0 if it is not a valid size for a shared memory file
1102 //
1103 static size_t sharedmem_filesize(int fd, TRAPS) {
1104 
1105   struct stat statbuf;
1106   int result;
1107 
1108   RESTARTABLE(::fstat(fd, &statbuf), result);
1109   if (result == OS_ERR) {
1110     if (PrintMiscellaneous && Verbose) {
1111       warning("fstat failed: %s\n", strerror(errno));
1112     }
1113     THROW_MSG_0(vmSymbols::java_io_IOException(),
1114                 "Could not determine PerfMemory size");
1115   }
1116 
1117   if ((statbuf.st_size == 0) ||
1118      ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
1119     THROW_MSG_0(vmSymbols::java_lang_Exception(),
1120                 "Invalid PerfMemory size");
1121   }
1122 
1123   return (size_t)statbuf.st_size;
1124 }
1125 
1126 // attach to a named shared memory region.
1127 //
1128 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1129 
1130   char* mapAddress;
1131   int result;
1132   int fd;
1133   size_t size = 0;
1134   const char* luser = NULL;
1135 
1136   int mmap_prot;
1137   int file_flags;
1138 
1139   ResourceMark rm;
1140 
1141   // map the high level access mode to the appropriate permission
1142   // constructs for the file and the shared memory mapping.
1143   if (mode == PerfMemory::PERF_MODE_RO) {
1144     mmap_prot = PROT_READ;
1145 
1146   // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
1147 #ifdef O_NOFOLLOW
1148     file_flags = O_RDONLY | O_NOFOLLOW;
1149 #else
1150     file_flags = O_RDONLY;
1151 #endif
1152   }
1153   else if (mode == PerfMemory::PERF_MODE_RW) {
1154 #ifdef LATER
1155     mmap_prot = PROT_READ | PROT_WRITE;
1156     file_flags = O_RDWR | O_NOFOLLOW;
1157 #else
1158     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1159               "Unsupported access mode");
1160 #endif
1161   }
1162   else {
1163     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1164               "Illegal access mode");
1165   }
1166 
1167   if (user == NULL || strlen(user) == 0) {
1168     luser = get_user_name(vmid, CHECK);
1169   }
1170   else {
1171     luser = user;
1172   }
1173 
1174   if (luser == NULL) {
1175     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1176               "Could not map vmid to user Name");
1177   }
1178 
1179   char* dirname = get_user_tmp_dir(luser);
1180 
1181   // since we don't follow symbolic links when creating the backing
1182   // store file, we don't follow them when attaching either.
1183   //
1184   if (!is_directory_secure(dirname)) {
1185     FREE_C_HEAP_ARRAY(char, dirname);
1186     if (luser != user) {
1187       FREE_C_HEAP_ARRAY(char, luser);
1188     }
1189     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1190               "Process not found");
1191   }
1192 
1193   char* filename = get_sharedmem_filename(dirname, vmid);
1194 
1195   // copy heap memory to resource memory. the open_sharedmem_file
1196   // method below need to use the filename, but could throw an
1197   // exception. using a resource array prevents the leak that
1198   // would otherwise occur.
1199   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1200   strcpy(rfilename, filename);
1201 
1202   // free the c heap resources that are no longer needed
1203   if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
1204   FREE_C_HEAP_ARRAY(char, dirname);
1205   FREE_C_HEAP_ARRAY(char, filename);
1206 
1207   // open the shared memory file for the give vmid
1208   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
1209   assert(fd != OS_ERR, "unexpected value");
1210 
1211   if (*sizep == 0) {
1212     size = sharedmem_filesize(fd, CHECK);
1213     assert(size != 0, "unexpected size");
1214   } else {
1215     size = *sizep;
1216   }
1217 
1218   mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1219 
1220   // attempt to close the file - restart if it gets interrupted,
1221   // but ignore other failures
1222   RESTARTABLE(::close(fd), result);
1223   assert(result != OS_ERR, "could not close file");
1224 
1225   if (mapAddress == MAP_FAILED) {
1226     if (PrintMiscellaneous && Verbose) {
1227       warning("mmap failed: %s\n", strerror(errno));
1228     }
1229     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1230               "Could not map PerfMemory");
1231   }
1232 
1233   // It does not go through os api, the operation has to record from here.
1234   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1235 
1236   *addr = mapAddress;
1237   *sizep = size;
1238 
1239   if (PerfTraceMemOps) {
1240     tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1241                INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
1242   }
1243 }
1244 
1245 
1246 
1247 
1248 // create the PerfData memory region
1249 //
1250 // This method creates the memory region used to store performance
1251 // data for the JVM. The memory may be created in standard or
1252 // shared memory.
1253 //
1254 void PerfMemory::create_memory_region(size_t size) {
1255 
1256   if (PerfDisableSharedMem) {
1257     // do not share the memory for the performance data.
1258     _start = create_standard_memory(size);
1259   }
1260   else {
1261     _start = create_shared_memory(size);
1262     if (_start == NULL) {
1263 
1264       // creation of the shared memory region failed, attempt
1265       // to create a contiguous, non-shared memory region instead.
1266       //
1267       if (PrintMiscellaneous && Verbose) {
1268         warning("Reverting to non-shared PerfMemory region.\n");
1269       }
1270       PerfDisableSharedMem = true;
1271       _start = create_standard_memory(size);
1272     }
1273   }
1274 
1275   if (_start != NULL) _capacity = size;
1276 
1277 }
1278 
1279 // delete the PerfData memory region
1280 //
1281 // This method deletes the memory region used to store performance
1282 // data for the JVM. The memory region indicated by the <address, size>
1283 // tuple will be inaccessible after a call to this method.
1284 //
1285 void PerfMemory::delete_memory_region() {
1286 
1287   assert((start() != NULL && capacity() > 0), "verify proper state");
1288 
1289   // If user specifies PerfDataSaveFile, it will save the performance data
1290   // to the specified file name no matter whether PerfDataSaveToFile is specified
1291   // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
1292   // -XX:+PerfDataSaveToFile.
1293   if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
1294     save_memory_to_file(start(), capacity());
1295   }
1296 
1297   if (PerfDisableSharedMem) {
1298     delete_standard_memory(start(), capacity());
1299   }
1300   else {
1301     delete_shared_memory(start(), capacity());
1302   }
1303 }
1304 
1305 // attach to the PerfData memory region for another JVM
1306 //
1307 // This method returns an <address, size> tuple that points to
1308 // a memory buffer that is kept reasonably synchronized with
1309 // the PerfData memory region for the indicated JVM. This
1310 // buffer may be kept in synchronization via shared memory
1311 // or some other mechanism that keeps the buffer updated.
1312 //
1313 // If the JVM chooses not to support the attachability feature,
1314 // this method should throw an UnsupportedOperation exception.
1315 //
1316 // This implementation utilizes named shared memory to map
1317 // the indicated process's PerfData memory region into this JVMs
1318 // address space.
1319 //
1320 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
1321 
1322   if (vmid == 0 || vmid == os::current_process_id()) {
1323      *addrp = start();
1324      *sizep = capacity();
1325      return;
1326   }
1327 
1328   mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
1329 }
1330 
1331 // detach from the PerfData memory region of another JVM
1332 //
1333 // This method detaches the PerfData memory region of another
1334 // JVM, specified as an <address, size> tuple of a buffer
1335 // in this process's address space. This method may perform
1336 // arbitrary actions to accomplish the detachment. The memory
1337 // region specified by <address, size> will be inaccessible after
1338 // a call to this method.
1339 //
1340 // If the JVM chooses not to support the attachability feature,
1341 // this method should throw an UnsupportedOperation exception.
1342 //
1343 // This implementation utilizes named shared memory to detach
1344 // the indicated process's PerfData memory region from this
1345 // process's address space.
1346 //
1347 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
1348 
1349   assert(addr != 0, "address sanity check");
1350   assert(bytes > 0, "capacity sanity check");
1351 
1352   if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
1353     // prevent accidental detachment of this process's PerfMemory region
1354     return;
1355   }
1356 
1357   unmap_shared(addr, bytes);
1358 }